Avogadro  1.1.0
Dynamic Display example

Rationale

The Dynamic Display extension implements a dialog box that counts the number of hydrogen atoms in the currently displayed molecule. It is designed as an example of how to extract data from the molecule when it is loaded, and also shows how to call functions when the user edits a molecule.

Note:

This may give incorrect counts when the molecule is edited, especially when "Adjust Hydrogens" is enabled. This has been fixed in newer versions of Avogadro. See http://gold.cryos.net:8080/#change,257

Overview

The extension consists of five files located in libavogadro/examples/thirdPartyExtensions/02-DynamicDisplay/:

Specifics

This extension makes use of the Extension::setMolecule virtual to be notified when a new molecule is loaded in Avogadro. This function will update the cached Molecule pointer, disconnecting the old pointer and reconnecting the new pointer to call moleculeUpdated when atoms are added, removed, or modified.

Here, the cached Molecule pointer is inspected, and the appropriate signal is emitted -- either reset if the pointer is null, or update if the pointer if valid.

This function updates the label ui.label_numHydrogens with "N/A", indicating that there is not a valid molecule loaded.

Here, the number of hydrogen atoms are count by iterating through the atoms in the passed Molecule using the QList of Atom pointers provided via Molecule::atoms(). The atomic number of each atom is checked, and if it is a hydrogen, the counter is incremented. Finally, the label is updated with the number of hydrogens found.

Next Steps

If you're feeling adventurous, try out the following: