1 | /*
|
---|
2 | * Project: MoleCuilder
|
---|
3 | * Description: creates and alters molecular systems
|
---|
4 | * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
|
---|
5 | *
|
---|
6 | *
|
---|
7 | * This file is part of MoleCuilder.
|
---|
8 | *
|
---|
9 | * MoleCuilder 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.
|
---|
13 | *
|
---|
14 | * MoleCuilder 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.
|
---|
18 | *
|
---|
19 | * You should have received a copy of the GNU General Public License
|
---|
20 | * along with MoleCuilder. If not, see <http://www.gnu.org/licenses/>.
|
---|
21 | */
|
---|
22 |
|
---|
23 | /*
|
---|
24 | * QtMoleculeList.cpp
|
---|
25 | *
|
---|
26 | * Created on: Jan 21, 2010
|
---|
27 | * Author: crueger
|
---|
28 | */
|
---|
29 |
|
---|
30 | // include config.h
|
---|
31 | #ifdef HAVE_CONFIG_H
|
---|
32 | #include <config.h>
|
---|
33 | #endif
|
---|
34 |
|
---|
35 | #include "Views/Qt4/QtMoleculeList.hpp"
|
---|
36 |
|
---|
37 | #include <QMetaMethod>
|
---|
38 |
|
---|
39 | #include <iostream>
|
---|
40 |
|
---|
41 | #include "CodePatterns/MemDebug.hpp"
|
---|
42 |
|
---|
43 | #include "CodePatterns/Observer/Notification.hpp"
|
---|
44 |
|
---|
45 | #include "Atom/atom.hpp"
|
---|
46 | #include "Formula.hpp"
|
---|
47 | #include "molecule.hpp"
|
---|
48 | #include "MoleculeListClass.hpp"
|
---|
49 | #include "Actions/SelectionAction/Molecules/MoleculeByIdAction.hpp"
|
---|
50 | #include "Actions/SelectionAction/Molecules/NotMoleculeByIdAction.hpp"
|
---|
51 |
|
---|
52 | using namespace std;
|
---|
53 |
|
---|
54 | // maybe this should go with the definition of molecules
|
---|
55 |
|
---|
56 | // some attributes need to be easier to find for molecules
|
---|
57 | // these attributes are skipped so far
|
---|
58 | const int QtMoleculeList::COLUMNCOUNT = COLUMNTYPES_MAX;
|
---|
59 | const char *QtMoleculeList::COLUMNNAMES[QtMoleculeList::COLUMNCOUNT]={"Name","Visibility", "Atoms","Formula","Occurrence"/*,"Size"*/};
|
---|
60 |
|
---|
61 | QtMoleculeList::QtMoleculeList() :
|
---|
62 | Observer("QtMoleculeList")
|
---|
63 | {
|
---|
64 | setColumnCount(COLUMNCOUNT);
|
---|
65 | // setSelectionMode(QAbstractItemView::MultiSelection);
|
---|
66 |
|
---|
67 | // QStringList header;
|
---|
68 | // for(int i=0; i<COLUMNCOUNT;++i)
|
---|
69 | // header << COLUMNNAMES[i];
|
---|
70 | // setHeaderLabels(header);
|
---|
71 |
|
---|
72 | World::getInstance().signOn(this, World::MoleculeInserted);
|
---|
73 | World::getInstance().signOn(this, World::MoleculeRemoved);
|
---|
74 |
|
---|
75 | dirty = true;
|
---|
76 | clearing = false;
|
---|
77 | selecting = false;
|
---|
78 | changing = false;
|
---|
79 | ChangingChildrensVisibility = false;
|
---|
80 | refill();
|
---|
81 |
|
---|
82 | // qRegisterMetaType<QItemSelection>("QItemSelection");
|
---|
83 | //connect(this,SIGNAL(cellChanged(int,int)),this,SLOT(moleculeChanged(int,int)));
|
---|
84 | // connect(selectionModel(),SIGNAL(selectionChanged(QItemSelection, QItemSelection)),this,SLOT(rowsSelected(QItemSelection, QItemSelection)));
|
---|
85 | // connect(this, SIGNAL(itemChanged(QStandardItem*, int)), this, SLOT(visibilityChanged(QStandardItem*, int)));
|
---|
86 | }
|
---|
87 |
|
---|
88 | QtMoleculeList::~QtMoleculeList()
|
---|
89 | {
|
---|
90 | World::getInstance().signOff(this, World::MoleculeInserted);
|
---|
91 | World::getInstance().signOff(this, World::MoleculeRemoved);
|
---|
92 | }
|
---|
93 |
|
---|
94 | void QtMoleculeList::update(Observable *publisher) {
|
---|
95 | ASSERT(0,
|
---|
96 | "QtMoleculeList::update() - we did not sign up for any global updates.");
|
---|
97 | }
|
---|
98 |
|
---|
99 | QStandardItem * QtMoleculeList::MoleculeToItem(const molecule * const _mol)
|
---|
100 | {
|
---|
101 | MoleculeItemBiMap_t::left_const_iterator iter =
|
---|
102 | MoleculeItemBiMap.left.find(_mol);
|
---|
103 | ASSERT( iter != MoleculeItemBiMap.left.end(),
|
---|
104 | "QtMoleculeList - could not find molecule "+_mol->getName()+" in my list.");
|
---|
105 | return iter->second;
|
---|
106 | }
|
---|
107 |
|
---|
108 | void QtMoleculeList::recieveNotification(Observable *publisher, Notification_ptr notification)
|
---|
109 | {
|
---|
110 | if (dynamic_cast<World *>(publisher) != NULL) {
|
---|
111 | switch (notification->getChannelNo()) {
|
---|
112 | case World::MoleculeInserted:
|
---|
113 | {
|
---|
114 | const molecule * const mol = World::getInstance().lastChanged<molecule>();
|
---|
115 | addItem( mol );
|
---|
116 | break;
|
---|
117 | }
|
---|
118 | case World::MoleculeRemoved:
|
---|
119 | {
|
---|
120 | const molecule * const mol = World::getInstance().lastChanged<molecule>();
|
---|
121 | removeItem( mol );
|
---|
122 | break;
|
---|
123 | }
|
---|
124 | default:
|
---|
125 | ASSERT(0, "QtMoleculeList::recieveNotification() - cannot get here, not subscribed to channel "
|
---|
126 | +toString(notification->getChannelNo()));
|
---|
127 | break;
|
---|
128 | }
|
---|
129 | }
|
---|
130 |
|
---|
131 | if (selecting)
|
---|
132 | return;
|
---|
133 |
|
---|
134 | dirty = true;
|
---|
135 | }
|
---|
136 |
|
---|
137 | void QtMoleculeList::addGroupItem(
|
---|
138 | QStandardItem *&mainitem,
|
---|
139 | const std::string &_molecule_formula)
|
---|
140 | {
|
---|
141 | QList<QStandardItem *> groupItems;
|
---|
142 | // fill item list
|
---|
143 | mainitem = new QStandardItem(QString("default"));
|
---|
144 | mainitem->setFlags(mainitem->flags() ^ Qt::ItemIsSelectable);
|
---|
145 | formula.insert( std::make_pair(_molecule_formula, mainitem) );
|
---|
146 | groupItems << mainitem;
|
---|
147 | QStandardItem *visitem = new QStandardItem;
|
---|
148 | visitem->setCheckState(Qt::Unchecked);
|
---|
149 | visitem->setFlags(mainitem->flags() | Qt::ItemIsUserCheckable);
|
---|
150 | groupItems << visitem;
|
---|
151 | groupItems << new QStandardItem(QString::number(0));
|
---|
152 | groupItems << new QStandardItem(QString(""));
|
---|
153 | groupItems << new QStandardItem(QString::number(0));
|
---|
154 | invisibleRootItem()->appendRow(groupItems);
|
---|
155 | // _groupItem->setData(0, Qt::UserRole, QVariant(-1));
|
---|
156 | }
|
---|
157 |
|
---|
158 | void QtMoleculeList::addMoleculeItem(
|
---|
159 | QStandardItem *_groupitem,
|
---|
160 | const molecule *_mol,
|
---|
161 | const std::string &_molecule_formula)
|
---|
162 | {
|
---|
163 | QList<QStandardItem *> molItems;
|
---|
164 | QStandardItem *mainitem = new QStandardItem(QString(_mol->getName().c_str()));
|
---|
165 | mainitem->setFlags(mainitem->flags() | Qt::ItemIsSelectable);
|
---|
166 | // mainitem->setSelected(World::getInstance().isSelected(_mol));
|
---|
167 | MoleculeItemBiMap.left.insert( std::make_pair(_mol, mainitem) );
|
---|
168 | molItems << mainitem;
|
---|
169 | QStandardItem *visitem = new QStandardItem();
|
---|
170 | visitem->setCheckState(Qt::Unchecked);
|
---|
171 | visitem->setFlags(mainitem->flags() | Qt::ItemIsUserCheckable);
|
---|
172 | molItems << visitem;
|
---|
173 | molItems << new QStandardItem(QString::number(_mol->getAtomCount()));
|
---|
174 | molItems << new QStandardItem(QString(_molecule_formula.c_str()));
|
---|
175 | molItems << new QStandardItem(QString::number(0));
|
---|
176 | // const int index = _mol->getId();
|
---|
177 | // molItem->setData(0, Qt::UserRole, QVariant(index));
|
---|
178 | _groupitem->appendRow(molItems);
|
---|
179 | }
|
---|
180 |
|
---|
181 | void QtMoleculeList::addItem(const molecule *_mol)
|
---|
182 | {
|
---|
183 | if (changing)
|
---|
184 | return;
|
---|
185 | changing = true;
|
---|
186 | // find group if already in list
|
---|
187 | QStandardItem *groupItem = NULL;
|
---|
188 |
|
---|
189 | const std::string &molecule_formula = _mol->getFormula().toString();
|
---|
190 | FormulaTreeItemMap_t::const_iterator formulaiter =
|
---|
191 | formula.find(molecule_formula);
|
---|
192 |
|
---|
193 | // new molecule type -> create new group
|
---|
194 | if (formulaiter == formula.end()){
|
---|
195 | // insert new formula entry into visibility
|
---|
196 | #ifndef NDEBUG
|
---|
197 | std::pair< FormulaVisibilityCountMap_t::iterator, bool> visibilityinserter =
|
---|
198 | #endif
|
---|
199 | FormulaVisibilityCountMap.insert(
|
---|
200 | std::make_pair( molecule_formula, (unsigned int)0) );
|
---|
201 | ASSERT( visibilityinserter.second,
|
---|
202 | "QtMoleculeList::refill() - molecule with formula "
|
---|
203 | +molecule_formula+" already in FormulaVisibilityCountMap.");
|
---|
204 |
|
---|
205 | // create item and place into Map with formula as key
|
---|
206 | addGroupItem(groupItem, molecule_formula);
|
---|
207 | } else {
|
---|
208 | groupItem = formulaiter->second;
|
---|
209 | }
|
---|
210 | ASSERT( groupItem != NULL,
|
---|
211 | "QtMoleculeList::addItem() - item with id "+toString(_mol->getId())
|
---|
212 | +" has not parent?");
|
---|
213 |
|
---|
214 | // add molecule
|
---|
215 | addMoleculeItem(groupItem, _mol, molecule_formula);
|
---|
216 |
|
---|
217 | // increase group occurrence
|
---|
218 | const int index = groupItem->index().row();
|
---|
219 | QStandardItem *occ_item = invisibleRootItem()->child(index, OCCURRENCE);
|
---|
220 | int count = occ_item->text().toInt() + 1;
|
---|
221 | occ_item->setText(QString::number(count));
|
---|
222 |
|
---|
223 | changing = false;
|
---|
224 | }
|
---|
225 |
|
---|
226 | void QtMoleculeList::removeItem(const molecule *_mol)
|
---|
227 | {
|
---|
228 | if (changing)
|
---|
229 | return;
|
---|
230 | changing = true;
|
---|
231 |
|
---|
232 | dirty = true;
|
---|
233 | // TODO: We have to get some Model into the Table in order to "find" items right
|
---|
234 | // away
|
---|
235 | // QTreeWidgetItem *molItem = itemFromIndex(mol->getId());
|
---|
236 | // delete molItem1;
|
---|
237 |
|
---|
238 | changing = false;
|
---|
239 | }
|
---|
240 |
|
---|
241 | void QtMoleculeList::refill() {
|
---|
242 | clearing = true;
|
---|
243 | const std::vector<molecule*> &molecules = World::getInstance().getAllMolecules();
|
---|
244 |
|
---|
245 | clear();
|
---|
246 | formula.clear();
|
---|
247 | FormulaVisibilityCountMap.clear();
|
---|
248 | MoleculeItemBiMap.clear();
|
---|
249 |
|
---|
250 | for (std::vector<molecule*>::const_iterator iter = molecules.begin();
|
---|
251 | iter != molecules.end();
|
---|
252 | iter++) {
|
---|
253 |
|
---|
254 | addItem(*iter);
|
---|
255 | }
|
---|
256 | dirty = false;
|
---|
257 | clearing = false;
|
---|
258 | }
|
---|
259 |
|
---|
260 | /*
|
---|
261 | void QtMoleculeList::paintEvent(QPaintEvent * event)
|
---|
262 | {
|
---|
263 | if (dirty)
|
---|
264 | refill();
|
---|
265 | QTreeWidget::paintEvent(event);
|
---|
266 | }
|
---|
267 | */
|
---|
268 |
|
---|
269 | void QtMoleculeList::subjectKilled(Observable *publisher) {
|
---|
270 | }
|
---|
271 |
|
---|
272 | /*
|
---|
273 | void QtMoleculeList::visibilityChanged(QStandardItem* item, int column)
|
---|
274 | {
|
---|
275 | if ((!changing) && (!clearing) && (!ChangingChildrensVisibility))
|
---|
276 | if (column == VISIBILITY) {
|
---|
277 | const moleculeId_t molid = item->data(0, Qt::UserRole).toInt();
|
---|
278 | const bool visible = item->checkState(VISIBILITY);
|
---|
279 | if (molid != (unsigned int)-1) { // molecule item
|
---|
280 | World::MoleculeConstIterator moliter =
|
---|
281 | const_cast<const World &>(World::getInstance()).getMoleculeIter(MoleculeById(molid));
|
---|
282 | const molecule * const _molecule = *moliter;
|
---|
283 | ASSERT( _molecule != NULL,
|
---|
284 | "QtMoleculeList::visibilityChanged() - molecule with id "
|
---|
285 | +toString(molid)+" is not known to World.");
|
---|
286 | const std::string &molecule_formula = _molecule->getFormula().toString();
|
---|
287 | ASSERT( FormulaVisibilityCountMap.count(molecule_formula) != 0,
|
---|
288 | "QtMoleculeList::visibilityChanged() - molecule with formula " +molecule_formula
|
---|
289 | +" is not present in FormulaVisibilityCountMap.");
|
---|
290 |
|
---|
291 | // get parent
|
---|
292 | QTreeWidgetItem *groupItem = item->parent();
|
---|
293 | ASSERT( groupItem != NULL,
|
---|
294 | "QtMoleculeList::visibilityChanged() - item with id "+toString(molid)
|
---|
295 | +" has not parent?");
|
---|
296 | // check whether we have to set the group item
|
---|
297 |
|
---|
298 | ChangingChildrensVisibility = true;
|
---|
299 | if (visible) {
|
---|
300 | ++(FormulaVisibilityCountMap[molecule_formula]);
|
---|
301 | // compare with occurence/total number of molecules
|
---|
302 | if (FormulaVisibilityCountMap[molecule_formula] ==
|
---|
303 | (unsigned int)(groupItem->text(OCCURRENCE).toInt()))
|
---|
304 | groupItem->setCheckState(VISIBILITY, Qt::Checked);
|
---|
305 | } else {
|
---|
306 | --(FormulaVisibilityCountMap[molecule_formula]);
|
---|
307 | // none selected anymore?
|
---|
308 | if (FormulaVisibilityCountMap[molecule_formula] == 0)
|
---|
309 | groupItem->setCheckState(VISIBILITY, Qt::Unchecked);
|
---|
310 | }
|
---|
311 | ChangingChildrensVisibility = false;
|
---|
312 |
|
---|
313 | emit moleculesVisibilityChanged(molid, visible);
|
---|
314 |
|
---|
315 | } else { // group item
|
---|
316 |
|
---|
317 | // go through all children, but don't enter for groupItem once more
|
---|
318 | ChangingChildrensVisibility = true;
|
---|
319 | for (int i=0;i<item->childCount();++i) {
|
---|
320 | QTreeWidgetItem *molItem = item->child(i);
|
---|
321 | const moleculeId_t molid = molItem->data(0, Qt::UserRole).toInt();
|
---|
322 | ASSERT( molid != (unsigned int)-1,
|
---|
323 | "QtMoleculeList::visibilityChanged() - to child with index"
|
---|
324 | +toString(i)+" there is no molecule?");
|
---|
325 | molItem->setCheckState(VISIBILITY, visible ? Qt::Checked : Qt::Unchecked);
|
---|
326 |
|
---|
327 | // emit signal
|
---|
328 | emit moleculesVisibilityChanged(molid, visible);
|
---|
329 | }
|
---|
330 | // set current number of visible children
|
---|
331 | const std::string molecule_formula =
|
---|
332 | item->text(FORMULA).toStdString();
|
---|
333 | FormulaVisibilityCountMap[molecule_formula] =
|
---|
334 | visible ? item->text(OCCURRENCE).toInt() : 0;
|
---|
335 |
|
---|
336 | ChangingChildrensVisibility = false;
|
---|
337 | }
|
---|
338 | }
|
---|
339 | }
|
---|
340 | */
|
---|
341 |
|
---|
342 | void QtMoleculeList::moleculeChanged() {
|
---|
343 | /*int idx = verticalHeaderItem(row)->data(Qt::UserRole).toInt();
|
---|
344 | molecule *mol = molecules->ReturnIndex(idx);
|
---|
345 | string cellValue = item(row,NAME)->text().toStdString();
|
---|
346 | if(mol->getName() != cellValue && cellValue !="") {
|
---|
347 | mol->setName(cellValue);
|
---|
348 | }
|
---|
349 | else if(cellValue==""){
|
---|
350 | item(row,NAME)->setText(QString(mol->getName().c_str()));
|
---|
351 | }*/
|
---|
352 | }
|
---|
353 |
|
---|
354 | /*
|
---|
355 | void QtMoleculeList::rowsSelected(const QItemSelection & selected, const QItemSelection & deselected){
|
---|
356 |
|
---|
357 | if (clearing)
|
---|
358 | return;
|
---|
359 | if (selecting)
|
---|
360 | return;
|
---|
361 | selecting = true;
|
---|
362 |
|
---|
363 | // Select all molecules which belong to newly selected rows.
|
---|
364 | QModelIndex index;
|
---|
365 | {
|
---|
366 | QModelIndexList items = selected.indexes();
|
---|
367 | molids_t ids;
|
---|
368 | ids.reserve(items.size());
|
---|
369 | foreach (index, items)
|
---|
370 | if (index.column() == 0){
|
---|
371 | int mol_id = model()->data(index, Qt::UserRole).toInt();
|
---|
372 | if (mol_id < 0)
|
---|
373 | continue;
|
---|
374 | ids.push_back(mol_id);
|
---|
375 | //std::cout << "select molecule" << std::endl;
|
---|
376 | }
|
---|
377 | MoleCuilder::SelectionMoleculeById(ids);
|
---|
378 | }
|
---|
379 |
|
---|
380 | // Unselect all molecules which belong to newly unselected rows.
|
---|
381 | {
|
---|
382 | QModelIndexList items = deselected.indexes();
|
---|
383 | molids_t ids;
|
---|
384 | ids.reserve(items.size());
|
---|
385 | foreach (index, items)
|
---|
386 | if (index.column() == 0){
|
---|
387 | int mol_id = model()->data(index, Qt::UserRole).toInt();
|
---|
388 | if (mol_id < 0)
|
---|
389 | continue;
|
---|
390 | //std::cout << "unselect molecule" << std::endl;
|
---|
391 | ids.push_back(mol_id);
|
---|
392 | }
|
---|
393 | MoleCuilder::SelectionNotMoleculeById(ids);
|
---|
394 | }
|
---|
395 |
|
---|
396 | selecting = false;
|
---|
397 | }
|
---|
398 |
|
---|
399 | */
|
---|