1 /**********************************************************************
2 MoleculeTreeView - Tree View of a Molecule
4 Copyright (C) 2007 Donald Ephraim Curtis <donald-curtis@uiowa.edu>
6 This file is part of the Avogadro molecular editor project.
7 For more information, see <http://avogadro.sourceforge.net/>
9 Avogadro is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 Avogadro is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 **********************************************************************/
25 #ifndef __MOLECULETREEVIEW_H
26 #define __MOLECULETREEVIEW_H
28 #include <avogadro/global.h>
29 #include <avogadro/primitive.h>
31 #include <QTreeWidget>
32 #include <QItemDelegate>
37 * @class MoleculeTreeView
38 * @brief Tree based view of a Molecule.
39 * @author Donald Ephraim Curtis <donald-curtis@uiowa.edu>
41 * This widget provides a list of the molecule's primitives (Atoms, Bonds, Residues)
42 * grouped by their type. In terms of a Model-View architecture we consider
43 * the Molecule the model and MoleculeTreeView a view of this model.
45 class A_EXPORT MoleculeTreeView
: public QTreeWidget
51 * Construct a new molecule treeview.
52 * @param parent the widget parent
54 MoleculeTreeView(QWidget
*parent
=0);
56 /** Construct a new molecule treeview. Don't stress yourself out.
58 * @param molecule the molecule to view
59 * @param parent the widget parent
61 explicit MoleculeTreeView(Molecule
*molecule
, QWidget
*parent
=0);
64 * Set the molecule which is the model for the treeview.
65 * @param molecule the molecule to view
67 void setMolecule(Molecule
*molecule
);
71 * Add the primitive to the widget. This slot is called whenever
72 * a new primitive is added to our molecule model. It adds the
73 * primitive to the list in the appropriate group.
75 * @param primitive pointer to a primitive to add to the view
76 * @return the QTreeWidgetItem which was created as a result of this
79 QTreeWidgetItem
* addPrimitive(Primitive
*primitive
);
82 * Update a primitive. This slot is called whenever a primitive of our
83 * molecule model has been changed and we need to check our view.
85 * @note In some cases we are passed the molecule itself meaning that more
86 * than one thing has changed in the molecule.
88 * @param primitive primitive that was changed
90 void updatePrimitive(Primitive
*primitive
);
92 /** Remove a primitive. This slot is called whenever a primitive of our
93 * molecule model has been removed and we need to take it off our list.
94 * Additionally we need to update other items in our view that are impacted
97 * @param primitive primitive that was removed
99 void removePrimitive(Primitive
*primitive
);
102 * Update the entire model. This goes though all items in the list and
103 * updates their view.
108 * Handles when the mouse is pressed on a certain item.
110 * @param item item which the mouse pressed
112 void handleMousePress(QTreeWidgetItem
*item
);
116 * Current molecule for this view.
118 Molecule
*m_molecule
;
120 * Groups of item widgets for our various primitive types. Each group
121 * is essentially a single QTreeWidgetItem which has as it's children
122 * all the members of the group.
124 QVector
<QTreeWidgetItem
*> m_groups
;
127 * A helper function to generate our item strings based off the
128 * primitive information.
130 * @param primitive primitive to generate the string for
131 * @return item string (ie. "Atom X", "Bond (x, y)")
133 QString
primitiveToItemText(Primitive
*primitive
);
136 * Return the index in our list for the primitive. Causes a search
137 * on all our items O(n).
139 * @param primitive primitive to find
140 * @return index of the specified primitive or -1 if not found
142 int primitiveToItemIndex(Primitive
*primitive
);
145 * Update a group of items. Given a parent item it updates
146 * all the children items.
148 * @param group the parent item of the group to update
150 void updateGroup(QTreeWidgetItem
*group
);
153 * Update a single item.
155 * @param item item to update
157 void updatePrimitiveItem(QTreeWidgetItem
*item
);
160 * Add a new group for a primitive type.
162 * @param type the type of the group to add
163 * @return the parent item for the group
165 QTreeWidgetItem
* addGroup(enum Primitive::Type type
);
170 * @param name name of the group
171 * @param type group type
172 * @return the item which is the parent item for the group
174 QTreeWidgetItem
* addGroup(const QString
& name
, enum Primitive::Type type
);
178 * Common constructor function.
184 * @internal Used by the MoleculeTreeView to draw items in the list.
185 * @class MoleculeItemDelegate
186 * @author Donald Ephraim Curtis <donald-curtis@uiowa.edu>
188 class MoleculeItemDelegate
: public QItemDelegate
192 MoleculeItemDelegate(QTreeView
*view
, QWidget
*parent
);
194 virtual void paint(QPainter
*painter
, const QStyleOptionViewItem
&option
, const QModelIndex
&index
) const;
195 virtual QSize
sizeHint(const QStyleOptionViewItem
&opt
, const QModelIndex
&index
) const;