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 "QtMoleculeList.hpp"
|
---|
36 |
|
---|
37 | #include <QModelIndex>
|
---|
38 | #include <QDebug>
|
---|
39 |
|
---|
40 | #include "UIElements/Views/Qt4/MoleculeList/QtMoleculeItem.hpp"
|
---|
41 | #include "UIElements/Views/Qt4/MoleculeList/QtMoleculeItemFactory.hpp"
|
---|
42 |
|
---|
43 | #include <boost/bind.hpp>
|
---|
44 | #include <boost/thread/locks.hpp>
|
---|
45 | #include <iostream>
|
---|
46 |
|
---|
47 | #include "CodePatterns/MemDebug.hpp"
|
---|
48 |
|
---|
49 | #include "CodePatterns/Log.hpp"
|
---|
50 | #include "CodePatterns/Observer/Notification.hpp"
|
---|
51 |
|
---|
52 | #include "Atom/atom.hpp"
|
---|
53 | #include "Descriptors/MoleculeIdDescriptor.hpp"
|
---|
54 | #include "Formula.hpp"
|
---|
55 | #include "molecule.hpp"
|
---|
56 | #include "MoleculeListClass.hpp"
|
---|
57 |
|
---|
58 | using namespace std;
|
---|
59 |
|
---|
60 | const unsigned int QtMoleculeList::update_times_per_second = 20;
|
---|
61 |
|
---|
62 | QtMoleculeList::QtMoleculeList() :
|
---|
63 | Observer("QtMoleculeList"),
|
---|
64 | ChangingChildrensVisibility(false),
|
---|
65 | update_timer(NULL),
|
---|
66 | callback_DirtyItems(boost::bind(&QtMoleculeList::informDirtyState, this, _1, _2, _3)),
|
---|
67 | callback_subjectKilledItems(boost::bind(&QtMoleculeList::receiveSubjectKilled, this, _1))
|
---|
68 | {
|
---|
69 | setColumnCount(QtMoleculeItemFactory::COLUMNCOUNT);
|
---|
70 |
|
---|
71 | World::getInstance().signOn(this, World::MoleculeInserted);
|
---|
72 |
|
---|
73 | refill();
|
---|
74 |
|
---|
75 | // qRegisterMetaType<QItemSelection>("QItemSelection");
|
---|
76 | //connect(this,SIGNAL(cellChanged(int,int)),this,SLOT(moleculeChanged(int,int)));
|
---|
77 | // connect(selectionModel(),SIGNAL(selectionChanged(QItemSelection, QItemSelection)),this,SLOT(rowsSelected(QItemSelection, QItemSelection)));
|
---|
78 | connect(this, SIGNAL(itemChanged(QStandardItem*)), this, SLOT(checkForVisibilityChange(QStandardItem*)), Qt::DirectConnection);
|
---|
79 | }
|
---|
80 |
|
---|
81 | QtMoleculeList::~QtMoleculeList()
|
---|
82 | {
|
---|
83 | World::getInstance().signOff(this, World::MoleculeInserted);
|
---|
84 | }
|
---|
85 |
|
---|
86 | QVariant QtMoleculeList::headerData(int section, Qt::Orientation orientation, int role) const
|
---|
87 | {
|
---|
88 | if (role == Qt::DisplayRole) {
|
---|
89 | if (orientation == Qt::Horizontal) {
|
---|
90 | if (section < QtMoleculeItem::COLUMNTYPES_MAX)
|
---|
91 | return QString(QtMoleculeItemFactory::COLUMNNAMES[section]);
|
---|
92 | }
|
---|
93 | }
|
---|
94 | return QVariant();
|
---|
95 | }
|
---|
96 |
|
---|
97 | void QtMoleculeList::update(Observable *publisher) {
|
---|
98 | ASSERT(0,
|
---|
99 | "QtMoleculeList::update() - we did not sign up for any global updates.");
|
---|
100 | }
|
---|
101 |
|
---|
102 | bool QtMoleculeList::isMoleculeItemPresent(const moleculeId_t _molid) const
|
---|
103 | {
|
---|
104 | boost::recursive_mutex::scoped_lock lock(map_mutex);
|
---|
105 | MoleculeItemBiMap_t::left_const_iterator iter =
|
---|
106 | MoleculeItemBiMap.left.find(_molid);
|
---|
107 | return ( iter != MoleculeItemBiMap.left.end());
|
---|
108 | }
|
---|
109 |
|
---|
110 | QtMoleculeItem * QtMoleculeList::MoleculeIdToItem(const moleculeId_t _molid) const
|
---|
111 | {
|
---|
112 | boost::recursive_mutex::scoped_lock lock(map_mutex);
|
---|
113 | MoleculeItemBiMap_t::left_const_iterator iter =
|
---|
114 | MoleculeItemBiMap.left.find(_molid);
|
---|
115 | ASSERT( iter != MoleculeItemBiMap.left.end(),
|
---|
116 | "QtMoleculeList::MoleculeIdToItem() - could not find item to id "
|
---|
117 | +toString(_molid));
|
---|
118 | return iter->second;
|
---|
119 | }
|
---|
120 |
|
---|
121 | const moleculeId_t QtMoleculeList::ItemToMoleculeId(const QtMoleculeItem * const _item) const
|
---|
122 | {
|
---|
123 | boost::recursive_mutex::scoped_lock lock(map_mutex);
|
---|
124 | const MoleculeItemBiMap_t::right_const_iterator iter =
|
---|
125 | MoleculeItemBiMap.right.find(const_cast<QtMoleculeItem * const>(_item));
|
---|
126 | if (iter != MoleculeItemBiMap.right.end())
|
---|
127 | return iter->second;
|
---|
128 | else
|
---|
129 | return -1;
|
---|
130 | }
|
---|
131 |
|
---|
132 | QtMoleculeItem * QtMoleculeList::getSpecificMoleculeItem(
|
---|
133 | const QtMoleculeItem * const _item,
|
---|
134 | const enum QtMoleculeItem::COLUMNTYPES _type) const
|
---|
135 | {
|
---|
136 | QStandardItem *parent_item = _item->parent();
|
---|
137 | ASSERT( parent_item != NULL,
|
---|
138 | "QtMoleculeList::getSpecificMoleculeItem() - parent of molecule item is NULL");
|
---|
139 | return static_cast<QtMoleculeItem *>(parent_item->child(_item->index().row(), _type));
|
---|
140 | }
|
---|
141 |
|
---|
142 | bool QtMoleculeList::isGroupItemPresent(const std::string &_formula) const
|
---|
143 | {
|
---|
144 | boost::recursive_mutex::scoped_lock lock(map_mutex);
|
---|
145 | FormulaTreeItemBiMap_t::left_const_iterator iter =
|
---|
146 | FormulaItemBiMap.left.find(_formula);
|
---|
147 | return ( iter != FormulaItemBiMap.left.end());
|
---|
148 | }
|
---|
149 |
|
---|
150 | QStandardItem * QtMoleculeList::FormulaToGroupItem(const std::string &_formula) const
|
---|
151 | {
|
---|
152 | boost::recursive_mutex::scoped_lock lock(map_mutex);
|
---|
153 | FormulaTreeItemBiMap_t::left_const_iterator iter =
|
---|
154 | FormulaItemBiMap.left.find(_formula);
|
---|
155 | ASSERT( iter != FormulaItemBiMap.left.end(),
|
---|
156 | "QtMoleculeList::FormulaToGroupItem() - could not find item to formula "
|
---|
157 | +toString(_formula));
|
---|
158 | return iter->second;
|
---|
159 | }
|
---|
160 |
|
---|
161 | const std::string& QtMoleculeList::GroupItemToFormula(const QStandardItem * const _item) const
|
---|
162 | {
|
---|
163 | boost::recursive_mutex::scoped_lock lock(map_mutex);
|
---|
164 | static std::string emptystring;
|
---|
165 | const FormulaTreeItemBiMap_t::right_const_iterator iter =
|
---|
166 | FormulaItemBiMap.right.find(const_cast<QStandardItem * const>(_item));
|
---|
167 | if (iter != FormulaItemBiMap.right.end())
|
---|
168 | return iter->second;
|
---|
169 | else
|
---|
170 | return emptystring;
|
---|
171 | }
|
---|
172 |
|
---|
173 | QStandardItem * QtMoleculeList::getSpecificGroupItem(
|
---|
174 | const QStandardItem * const _item,
|
---|
175 | const enum QtMoleculeItem::COLUMNTYPES _type) const
|
---|
176 | {
|
---|
177 | return invisibleRootItem()->child(_item->index().row(), _type);
|
---|
178 | }
|
---|
179 |
|
---|
180 |
|
---|
181 | const moleculeId_t QtMoleculeList::IndexToMoleculeId(const QModelIndex &_index) const
|
---|
182 | {
|
---|
183 | boost::recursive_mutex::scoped_lock lock(refill_mutex);
|
---|
184 | QtMoleculeItem * const item = dynamic_cast<QtMoleculeItem *>(itemFromIndex(_index));
|
---|
185 | if (item == NULL)
|
---|
186 | return -1;
|
---|
187 | else
|
---|
188 | return ItemToMoleculeId(item);
|
---|
189 | }
|
---|
190 |
|
---|
191 | void QtMoleculeList::receiveSubjectKilled(const moleculeId_t _id)
|
---|
192 | {
|
---|
193 | boost::recursive_mutex::scoped_lock lock(map_mutex);
|
---|
194 | KilledItemsPerMolecule_t::iterator iter = KilledItemsPerMolecule.find(_id);
|
---|
195 | if (iter == KilledItemsPerMolecule.end())
|
---|
196 | KilledItemsPerMolecule.insert( std::make_pair(_id, 1));
|
---|
197 | else
|
---|
198 | ++(iter->second);
|
---|
199 | if (iter->second == QtMoleculeItem::COLUMNTYPES_MAX) {
|
---|
200 | boost::recursive_mutex::scoped_lock lock(listAccessing_mutex);
|
---|
201 | removedMolecules.push_back( _id );
|
---|
202 | }
|
---|
203 | }
|
---|
204 |
|
---|
205 | void QtMoleculeList::recieveNotification(Observable *publisher, Notification_ptr notification)
|
---|
206 | {
|
---|
207 | if (dynamic_cast<World *>(publisher) != NULL) {
|
---|
208 | switch (notification->getChannelNo()) {
|
---|
209 | case World::MoleculeInserted:
|
---|
210 | {
|
---|
211 | boost::recursive_mutex::scoped_lock lock(listAccessing_mutex);
|
---|
212 | const moleculeId_t molid = const_cast<const World &>(World::getInstance()).lastChangedMolId();
|
---|
213 | if (molid != (unsigned int )-1)
|
---|
214 | newMolecules.push_back( molid );
|
---|
215 | break;
|
---|
216 | }
|
---|
217 | default:
|
---|
218 | ASSERT(0, "QtMoleculeList::recieveNotification() - cannot get here, not subscribed to channel "
|
---|
219 | +toString(notification->getChannelNo()));
|
---|
220 | break;
|
---|
221 | }
|
---|
222 | }
|
---|
223 | }
|
---|
224 |
|
---|
225 | void QtMoleculeList::addGroupItem(
|
---|
226 | QStandardItem *&mainitem,
|
---|
227 | const std::string &_molecule_formula)
|
---|
228 | {
|
---|
229 | QList<QStandardItem *> groupItems =
|
---|
230 | QtMoleculeItemFactory::getInstance().createGroupItems(_molecule_formula);
|
---|
231 | mainitem = groupItems.front();
|
---|
232 | {
|
---|
233 | boost::recursive_mutex::scoped_lock lock(map_mutex);
|
---|
234 | FormulaItemBiMap.left.insert( std::make_pair(_molecule_formula, mainitem) );
|
---|
235 | }
|
---|
236 | invisibleRootItem()->appendRow(groupItems);
|
---|
237 | }
|
---|
238 |
|
---|
239 | void QtMoleculeList::addMoleculeItem(
|
---|
240 | QStandardItem *_groupitem,
|
---|
241 | const moleculeId_t _molid,
|
---|
242 | const std::string &_molecule_formula)
|
---|
243 | {
|
---|
244 | QList<QStandardItem *> molItems =
|
---|
245 | QtMoleculeItemFactory::getInstance().createMoleculeItems(
|
---|
246 | _molid,
|
---|
247 | callback_DirtyItems,
|
---|
248 | callback_subjectKilledItems);
|
---|
249 | QtMoleculeItem *mol_item = dynamic_cast<QtMoleculeItem *>(molItems.front());
|
---|
250 | ASSERT( mol_item != NULL,
|
---|
251 | "QtMoleculeList::addMoleculeItem() - item from factory was not a QtMoleculeItem?");
|
---|
252 | {
|
---|
253 | boost::recursive_mutex::scoped_lock lock(map_mutex);
|
---|
254 | MoleculeItemBiMap.left.insert( std::make_pair(_molid, mol_item) );
|
---|
255 | LOG(1, "Adding" << _molecule_formula << " for " << _molid << " to MoleculeFormulaMap.");
|
---|
256 | MoleculeFormulaMap.insert( std::make_pair( _molid, _molecule_formula) );
|
---|
257 | }
|
---|
258 | // LOG(1, "Inserting molecule " << _mol->getId() << ": " << _mol);
|
---|
259 | _groupitem->appendRow(molItems);
|
---|
260 | }
|
---|
261 |
|
---|
262 | std::string QtMoleculeList::addMolecule(const molecule * const _mol)
|
---|
263 | {
|
---|
264 | // find group if already in list
|
---|
265 | QStandardItem *groupItem = NULL;
|
---|
266 |
|
---|
267 | const std::string molecule_formula = _mol->getFormula().toString();
|
---|
268 |
|
---|
269 | // new molecule type -> create new group
|
---|
270 | if (!isGroupItemPresent(molecule_formula)){
|
---|
271 | // insert new formula entry into visibility
|
---|
272 | #ifndef NDEBUG
|
---|
273 | std::pair< FormulaVisibilityCountMap_t::iterator, bool> visibilityinserter =
|
---|
274 | #endif
|
---|
275 | FormulaVisibilityCountMap.insert(
|
---|
276 | std::make_pair( molecule_formula, (unsigned int)0) );
|
---|
277 | ASSERT( visibilityinserter.second,
|
---|
278 | "QtMoleculeList::refill() - molecule with formula "
|
---|
279 | +molecule_formula+" already in FormulaVisibilityCountMap.");
|
---|
280 |
|
---|
281 | // create item and place into Map with formula as key
|
---|
282 | addGroupItem(groupItem, molecule_formula);
|
---|
283 | } else {
|
---|
284 | groupItem = FormulaToGroupItem(molecule_formula);
|
---|
285 | }
|
---|
286 | ASSERT( groupItem != NULL,
|
---|
287 | "QtMoleculeList::addMolecule() - item with id "+toString(_mol->getId())
|
---|
288 | +" has no parent?");
|
---|
289 |
|
---|
290 | // add molecule
|
---|
291 | addMoleculeItem(groupItem, _mol->getId(), molecule_formula);
|
---|
292 |
|
---|
293 | return molecule_formula;
|
---|
294 | }
|
---|
295 |
|
---|
296 | void QtMoleculeList::removeMoleculeItem(QtMoleculeItem * const _item)
|
---|
297 | {
|
---|
298 | boost::recursive_mutex::scoped_lock lock(refill_mutex);
|
---|
299 | const QModelIndex mol_index = indexFromItem(_item);
|
---|
300 | QStandardItem *groupitem = _item->parent();
|
---|
301 | const QModelIndex group_index = groupitem->index();
|
---|
302 | {
|
---|
303 | boost::recursive_mutex::scoped_lock lock(map_mutex);
|
---|
304 | MoleculeItemBiMap_t::right_iterator removeiter =
|
---|
305 | MoleculeItemBiMap.right.find(_item);
|
---|
306 | ASSERT( removeiter != MoleculeItemBiMap.right.end(),
|
---|
307 | "QtMoleculeList::removeMoleculeItem() - could not find item in MoleculeBiMap.");
|
---|
308 | // LOG(1, "Erasing molecule " << (removeiter->second));
|
---|
309 | {
|
---|
310 | MoleculeFormulaMap_t::iterator removeformulaiter =
|
---|
311 | MoleculeFormulaMap.find(removeiter->second);
|
---|
312 | ASSERT( removeformulaiter != MoleculeFormulaMap.end(),
|
---|
313 | "QtMoleculeList::removeMoleculeItem() - could not find id "
|
---|
314 | +toString(removeiter->second)+" in MoleculeFormulaMap.");
|
---|
315 | LOG(1, "Removing " << removeformulaiter->second << " for "
|
---|
316 | << removeformulaiter->first << " from MoleculeFormulaMap.");
|
---|
317 | MoleculeFormulaMap.erase( removeformulaiter );
|
---|
318 | }
|
---|
319 | MoleculeItemBiMap.right.erase(removeiter);
|
---|
320 | }
|
---|
321 | removeRows(mol_index.row(), 1, group_index);
|
---|
322 | }
|
---|
323 |
|
---|
324 | void QtMoleculeList::refill()
|
---|
325 | {
|
---|
326 | // check timer's presence
|
---|
327 | if (update_timer == NULL) {
|
---|
328 | update_timer = new QTimer(this);
|
---|
329 | connect( update_timer, SIGNAL(timeout()), this, SLOT(checkState()));
|
---|
330 | } else
|
---|
331 | update_timer->stop();
|
---|
332 |
|
---|
333 | {
|
---|
334 | boost::recursive_mutex::scoped_lock refill_lock(refill_mutex);
|
---|
335 | boost::recursive_mutex::scoped_lock listAccessing_lock(listAccessing_mutex);
|
---|
336 |
|
---|
337 | // LOG(1, "Clearing list.");
|
---|
338 |
|
---|
339 | clear();
|
---|
340 | FormulaVisibilityCountMap.clear();
|
---|
341 | {
|
---|
342 | boost::recursive_mutex::scoped_lock lock(map_mutex);
|
---|
343 | FormulaItemBiMap.clear();
|
---|
344 | MoleculeFormulaMap.clear();
|
---|
345 | MoleculeItemBiMap.clear();
|
---|
346 | KilledItemsPerMolecule.clear();
|
---|
347 | }
|
---|
348 | dirtyMolItems.clear();
|
---|
349 | visibilityMolItems.clear();
|
---|
350 | visibilityGroupItems.clear();
|
---|
351 | newMolecules.clear();
|
---|
352 | removedMolecules.clear();
|
---|
353 | toBeMovedItems.clear();
|
---|
354 | }
|
---|
355 |
|
---|
356 | const std::vector<const molecule*> &molecules =
|
---|
357 | const_cast<const World &>(World::getInstance()).getAllMolecules();
|
---|
358 | for (std::vector<const molecule*>::const_iterator iter = molecules.begin();
|
---|
359 | iter != molecules.end();
|
---|
360 | iter++)
|
---|
361 | addMolecule(*iter);
|
---|
362 |
|
---|
363 | // activate timer
|
---|
364 | update_timer->start(1000/update_times_per_second);
|
---|
365 | }
|
---|
366 |
|
---|
367 | bool QtMoleculeList::areAnyItemsDirty()
|
---|
368 | {
|
---|
369 | // get whether any items are dirty
|
---|
370 | boost::recursive_mutex::scoped_lock lock(listAccessing_mutex);
|
---|
371 | bool dirty = false;
|
---|
372 | dirty |= !dirtyMolItems.empty();
|
---|
373 | dirty |= !visibilityMolItems.empty();
|
---|
374 | dirty |= !dirtyGroupItems.empty();
|
---|
375 | dirty |= !visibilityGroupItems.empty();
|
---|
376 | dirty |= !newMolecules.empty();
|
---|
377 | dirty |= !removedMolecules.empty();
|
---|
378 | dirty |= !toBeMovedItems.empty();
|
---|
379 | return dirty;
|
---|
380 | }
|
---|
381 |
|
---|
382 | void QtMoleculeList::checkState()
|
---|
383 | {
|
---|
384 | const bool dirty = areAnyItemsDirty();
|
---|
385 | // update if required
|
---|
386 | if (dirty)
|
---|
387 | updateItemStates();
|
---|
388 | }
|
---|
389 |
|
---|
390 | void QtMoleculeList::subjectKilled(Observable *publisher)
|
---|
391 | {}
|
---|
392 |
|
---|
393 | void QtMoleculeList::checkForVisibilityChange(QStandardItem* _item)
|
---|
394 | {
|
---|
395 | // qDebug() << "Item changed called.";
|
---|
396 |
|
---|
397 | boost::recursive_mutex::scoped_lock lock(refill_mutex);
|
---|
398 | if (_item->index().column() == QtMoleculeItem::VISIBILITY) {
|
---|
399 | // qDebug() << "visibilityItem changed: " << (_item->checkState() ? "checked" : "unchecked");
|
---|
400 | boost::recursive_mutex::scoped_lock lock(listAccessing_mutex);
|
---|
401 | if ((_item->parent() == NULL) || (_item->parent() == invisibleRootItem()))
|
---|
402 | visibilityGroupItems.insert( std::make_pair(
|
---|
403 | GroupItemToFormula(_item->parent()), QtMoleculeItem::VISIBILITY) );
|
---|
404 | else
|
---|
405 | visibilityMolItems.insert(
|
---|
406 | static_cast<QtMoleculeItem *>(_item)->getMoleculeId()
|
---|
407 | );
|
---|
408 | }
|
---|
409 | }
|
---|
410 |
|
---|
411 | void QtMoleculeList::setVisibilityForMoleculeItem(QtMoleculeItem* _item)
|
---|
412 | {
|
---|
413 | if (ChangingChildrensVisibility)
|
---|
414 | return;
|
---|
415 |
|
---|
416 | boost::recursive_mutex::scoped_lock lock(refill_mutex);
|
---|
417 | const bool visible = _item->checkState();
|
---|
418 | const moleculeId_t molid = _item->getMoleculeId();
|
---|
419 | std::string molecule_formula("illegal");
|
---|
420 | {
|
---|
421 | boost::recursive_mutex::scoped_lock lock(map_mutex);
|
---|
422 | MoleculeFormulaMap_t::const_iterator formulaiter =
|
---|
423 | MoleculeFormulaMap.find(molid);
|
---|
424 | ASSERT( formulaiter != MoleculeFormulaMap.end(),
|
---|
425 | "QtMoleculeList::setVisibilityForMoleculeItem() - formula of molecule "
|
---|
426 | +toString(molid)+" unknown.");
|
---|
427 | molecule_formula = formulaiter->second;
|
---|
428 | }
|
---|
429 | ASSERT( FormulaVisibilityCountMap.count(molecule_formula) != 0,
|
---|
430 | "QtMoleculeList::setVisibilityForMoleculeItem() - molecule with formula " +molecule_formula
|
---|
431 | +" is not present in FormulaVisibilityCountMap.");
|
---|
432 |
|
---|
433 | // get parent
|
---|
434 | QStandardItem *groupItem = _item->parent();
|
---|
435 | QStandardItem *visgroupItem = getSpecificGroupItem(groupItem, QtMoleculeItem::VISIBILITY);
|
---|
436 | ASSERT( groupItem != NULL,
|
---|
437 | "QtMoleculeList::setVisibilityForMoleculeItem() - item with id "
|
---|
438 | +toString(_item->getMoleculeId())+" has not parent?");
|
---|
439 | // check whether we have to set the group item
|
---|
440 |
|
---|
441 | ChangingChildrensVisibility = true;
|
---|
442 | if (visible) {
|
---|
443 | ++(FormulaVisibilityCountMap[molecule_formula]);
|
---|
444 | // compare with occurence/total number of molecules
|
---|
445 | if (FormulaVisibilityCountMap[molecule_formula] ==
|
---|
446 | (unsigned int)(groupItem->rowCount()))
|
---|
447 | visgroupItem->setCheckState(Qt::Checked);
|
---|
448 | } else {
|
---|
449 | --(FormulaVisibilityCountMap[molecule_formula]);
|
---|
450 | // none selected anymore?
|
---|
451 | if (FormulaVisibilityCountMap[molecule_formula] == 0)
|
---|
452 | visgroupItem->setCheckState(Qt::Unchecked);
|
---|
453 | }
|
---|
454 | ChangingChildrensVisibility = false;
|
---|
455 |
|
---|
456 | emit moleculesVisibilityChanged(_item->getMolecule()->getId(), visible);
|
---|
457 | }
|
---|
458 |
|
---|
459 | void QtMoleculeList::setVisibilityForGroupItem(QStandardItem* _item)
|
---|
460 | {
|
---|
461 | if (ChangingChildrensVisibility)
|
---|
462 | return;
|
---|
463 |
|
---|
464 | ChangingChildrensVisibility = true;
|
---|
465 |
|
---|
466 | boost::recursive_mutex::scoped_lock lock(refill_mutex);
|
---|
467 | // go through all children, but don't enter for groupItem once more
|
---|
468 | const bool visible = _item->checkState();
|
---|
469 | QStandardItem *groupitem = getSpecificGroupItem(_item, QtMoleculeItem::NAME);
|
---|
470 | for (int i=0;i<groupitem->rowCount();++i) {
|
---|
471 | QtMoleculeItem *molItem = dynamic_cast<QtMoleculeItem *>(
|
---|
472 | groupitem->child(i, QtMoleculeItem::VISIBILITY));
|
---|
473 | if (molItem->checkState() != visible) {
|
---|
474 | molItem->setCheckState(visible ? Qt::Checked : Qt::Unchecked);
|
---|
475 |
|
---|
476 | // emit signal
|
---|
477 | emit moleculesVisibilityChanged(molItem->getMolecule()->getId(), visible);
|
---|
478 | }
|
---|
479 | }
|
---|
480 | // set current number of visible children
|
---|
481 | const std::string molecule_formula =
|
---|
482 | GroupItemToFormula( getSpecificGroupItem(_item, QtMoleculeItem::NAME) );
|
---|
483 | FormulaVisibilityCountMap_t::iterator countiter =
|
---|
484 | FormulaVisibilityCountMap.find(molecule_formula);
|
---|
485 | ASSERT( countiter != FormulaVisibilityCountMap.end(),
|
---|
486 | "QtMoleculeList::setVisibilityForGroupItem() - molecules "+molecule_formula
|
---|
487 | +" have no entry in visibility count map?");
|
---|
488 | countiter->second = visible ? groupitem->rowCount() : 0;
|
---|
489 |
|
---|
490 | ChangingChildrensVisibility = false;
|
---|
491 | }
|
---|
492 |
|
---|
493 |
|
---|
494 | void QtMoleculeList::moleculeChanged() {
|
---|
495 | /*int idx = verticalHeaderItem(row)->data(Qt::UserRole).toInt();
|
---|
496 | molecule *mol = molecules->ReturnIndex(idx);
|
---|
497 | string cellValue = item(row,QtMoleculeItem::NAME)->text().toStdString();
|
---|
498 | if(mol->getName() != cellValue && cellValue !="") {
|
---|
499 | mol->setName(cellValue);
|
---|
500 | }
|
---|
501 | else if(cellValue==""){
|
---|
502 | item(row,QtMoleculeItem::NAME)->setText(QString(mol->getName().c_str()));
|
---|
503 | }*/
|
---|
504 | }
|
---|
505 |
|
---|
506 |
|
---|
507 | int QtMoleculeList::setOccurrence(QStandardItem * const _groupitem)
|
---|
508 | {
|
---|
509 | boost::recursive_mutex::scoped_lock lock(refill_mutex);
|
---|
510 | QModelIndex modelindex = _groupitem->index();
|
---|
511 | ASSERT( modelindex.isValid(),
|
---|
512 | "QtMoleculeList::setOccurrence() - groupitem not associated to model anymore.");
|
---|
513 | const int index = modelindex.row();
|
---|
514 | QStandardItem *parent_item =
|
---|
515 | _groupitem->parent() == NULL ? invisibleRootItem() : _groupitem->parent();
|
---|
516 | ASSERT( parent_item != NULL,
|
---|
517 | "QtMoleculeList::setOccurrence() - group item at "+toString(index)
|
---|
518 | +" does not have a parent?");
|
---|
519 | QStandardItem *occ_item = parent_item->child(index, QtMoleculeItem::OCCURRENCE);
|
---|
520 | ASSERT( occ_item != NULL,
|
---|
521 | "QtMoleculeList::setOccurrence() - group item at "+toString(index)
|
---|
522 | +" does not have an occurrence?");
|
---|
523 | const int count = _groupitem->rowCount();
|
---|
524 | if (count == 0) {
|
---|
525 | // we have to remove the group item completely
|
---|
526 | boost::recursive_mutex::scoped_lock lock(map_mutex);
|
---|
527 | const std::string molecule_formula = _groupitem->text().toStdString();
|
---|
528 | FormulaItemBiMap.left.erase(molecule_formula);
|
---|
529 | FormulaVisibilityCountMap.erase(molecule_formula);
|
---|
530 | return index;
|
---|
531 | } else {
|
---|
532 | occ_item->setText(QString::number(count));
|
---|
533 | return -1;
|
---|
534 | }
|
---|
535 | }
|
---|
536 |
|
---|
537 | std::string QtMoleculeList::readdItem(QtMoleculeItem *_molitem)
|
---|
538 | {
|
---|
539 | boost::recursive_mutex::scoped_lock lock(refill_mutex);
|
---|
540 | // use takeRows of molecule ..
|
---|
541 | QStandardItem *groupitem = _molitem->parent();
|
---|
542 | ASSERT( groupitem != NULL,
|
---|
543 | "QtMoleculeList::readdItem() - mol item at "+toString(_molitem->index().row())
|
---|
544 | +" does not have a groupitem?");
|
---|
545 | const molecule * const mol = _molitem->getMolecule();
|
---|
546 | QList<QStandardItem *> mol_row = _molitem->parent()->takeRow(_molitem->index().row());
|
---|
547 | // .. and re-add where new formula fits
|
---|
548 | std::string molecule_formula;
|
---|
549 | if (mol != NULL) {
|
---|
550 | molecule_formula = mol->getFormula().toString();
|
---|
551 | if (!isGroupItemPresent(molecule_formula)) {
|
---|
552 | // add new group item and formula entry
|
---|
553 | addGroupItem(groupitem, molecule_formula);
|
---|
554 | } else {
|
---|
555 | groupitem = FormulaToGroupItem(molecule_formula);
|
---|
556 | }
|
---|
557 | ASSERT( groupitem != NULL,
|
---|
558 | "QtMoleculeList::readdItem() - failed to create a sensible new groupitem");
|
---|
559 | // finally add again
|
---|
560 | groupitem->appendRow(mol_row);
|
---|
561 | } else {
|
---|
562 | for (QList<QStandardItem *>::iterator iter = mol_row.begin();
|
---|
563 | iter != mol_row.end(); ++iter)
|
---|
564 | delete *iter;
|
---|
565 | }
|
---|
566 |
|
---|
567 | return molecule_formula;
|
---|
568 | }
|
---|
569 |
|
---|
570 | void QtMoleculeList::informDirtyState(
|
---|
571 | const moleculeId_t _id,
|
---|
572 | const QtMoleculeItem::COLUMNTYPES _type,
|
---|
573 | const QtMoleculeItem::MoveTypes _movetype)
|
---|
574 | {
|
---|
575 | listAccessing_mutex.lock();
|
---|
576 | dirtyMolItems.insert( std::make_pair(_id, _type) );
|
---|
577 | listAccessing_mutex.unlock();
|
---|
578 |
|
---|
579 | if (_movetype == QtMoleculeItem::NeedsMove) {
|
---|
580 | // we have to convert whatever item raised the dirty signal to the first
|
---|
581 | // item in the row as otherwise multiple items in the row are selected
|
---|
582 | // as to be moved, i.e. the same row is moved multiple times
|
---|
583 | listAccessing_mutex.lock();
|
---|
584 | toBeMovedItems.insert(_id);
|
---|
585 | listAccessing_mutex.unlock();
|
---|
586 | }
|
---|
587 | }
|
---|
588 |
|
---|
589 | void QtMoleculeList::updateItemStates()
|
---|
590 | {
|
---|
591 | /// copy lists such that new signals for dirty/.. may come in right away
|
---|
592 | // TODO: if we had move semantics ...
|
---|
593 | listAccessing_mutex.lock();
|
---|
594 | list_of_molecule_items_t dirtyMolItems_copy = dirtyMolItems;
|
---|
595 | dirtyMolItems.clear();
|
---|
596 | list_of_molecules_t visibilityMolItems_copy = visibilityMolItems;
|
---|
597 | visibilityMolItems.clear();
|
---|
598 | list_of_group_items_t dirtyGroupItems_copy = dirtyGroupItems;
|
---|
599 | dirtyGroupItems.clear();
|
---|
600 | list_of_group_items_t visibilityGroupItems_copy = visibilityGroupItems;
|
---|
601 | visibilityGroupItems.clear();
|
---|
602 | std::vector<moleculeId_t> newMolecules_copy = newMolecules;
|
---|
603 | newMolecules.clear();
|
---|
604 | std::vector<moleculeId_t> removedMolecules_copy = removedMolecules;
|
---|
605 | removedMolecules.clear();
|
---|
606 | list_of_molecules_t toBeMovedItems_copy = toBeMovedItems;
|
---|
607 | toBeMovedItems.clear();
|
---|
608 | listAccessing_mutex.unlock();
|
---|
609 |
|
---|
610 | // wait till initial refill has been executed
|
---|
611 | boost::recursive_mutex::scoped_lock lock(refill_mutex);
|
---|
612 |
|
---|
613 | // LOG(1, "Starting update.");
|
---|
614 |
|
---|
615 | // remove removedMolecules from other lists.
|
---|
616 | for (std::vector<moleculeId_t>::const_iterator removeiter = removedMolecules_copy.begin();
|
---|
617 | removeiter != removedMolecules_copy.end(); ++removeiter) {
|
---|
618 | for (unsigned int i=0;i< QtMoleculeItem::COLUMNTYPES_MAX; ++i)
|
---|
619 | dirtyMolItems_copy.erase( std::make_pair(*removeiter,(QtMoleculeItem::COLUMNTYPES)i) );
|
---|
620 | toBeMovedItems_copy.erase(*removeiter);
|
---|
621 | visibilityMolItems_copy.erase(*removeiter);
|
---|
622 | }
|
---|
623 |
|
---|
624 | /// 1a. do the update for each dirty item
|
---|
625 | for (list_of_molecule_items_t::const_iterator dirtyiter = dirtyMolItems_copy.begin();
|
---|
626 | dirtyiter != dirtyMolItems_copy.end(); ++dirtyiter) {
|
---|
627 | if (!isMoleculeItemPresent(dirtyiter->first))
|
---|
628 | continue;
|
---|
629 | QtMoleculeItem * const mol_item =
|
---|
630 | getSpecificMoleculeItem(
|
---|
631 | MoleculeIdToItem(dirtyiter->first),
|
---|
632 | dirtyiter->second);
|
---|
633 | // LOG(1, "Updating item " << mol_item);
|
---|
634 | mol_item->updateState();
|
---|
635 | }
|
---|
636 |
|
---|
637 | /// 1b. do the visibility update for each dirty item
|
---|
638 | for (list_of_molecules_t::const_iterator visiter = visibilityMolItems_copy.begin();
|
---|
639 | visiter != visibilityMolItems_copy.end(); ++visiter) {
|
---|
640 | if (!isMoleculeItemPresent(*visiter))
|
---|
641 | continue;
|
---|
642 | QtMoleculeItem * const visitem =
|
---|
643 | getSpecificMoleculeItem(
|
---|
644 | MoleculeIdToItem(*visiter),
|
---|
645 | QtMoleculeItem::VISIBILITY );
|
---|
646 | // LOG(1, "Updating visibility of item " << visitem);
|
---|
647 | setVisibilityForMoleculeItem(visitem);
|
---|
648 | }
|
---|
649 |
|
---|
650 | /// 2. move all items that need to be moved
|
---|
651 | typedef std::set<std::string> formulas_t;
|
---|
652 | formulas_t toBeSetOccurrence;
|
---|
653 | for (list_of_molecules_t::const_iterator moveiter = toBeMovedItems_copy.begin();
|
---|
654 | moveiter != toBeMovedItems_copy.end(); ++moveiter) {
|
---|
655 | boost::recursive_mutex::scoped_lock lock(map_mutex);
|
---|
656 | // LOG(1, "Moving item " << molitem);
|
---|
657 | MoleculeFormulaMap_t::iterator formulaiter =
|
---|
658 | MoleculeFormulaMap.find(*moveiter);
|
---|
659 | ASSERT( formulaiter != MoleculeFormulaMap.end(),
|
---|
660 | "QtMoleculeList::updateItemStates() - formula of molecule "
|
---|
661 | +toString(*moveiter)+" unknown.");
|
---|
662 | // LOG(1, "Adding " << formulaiter->second << " to toBeSetOccurrence.");
|
---|
663 | toBeSetOccurrence.insert( formulaiter->second );
|
---|
664 | if (!isMoleculeItemPresent(*moveiter))
|
---|
665 | continue;
|
---|
666 | QtMoleculeItem *const molitem = MoleculeIdToItem(*moveiter);
|
---|
667 | LOG(1, "Moving item " << molitem);
|
---|
668 | const molecule *mol = molitem->getMolecule();
|
---|
669 | if (mol != NULL) {
|
---|
670 | // remove from formula<->molecule bimap with old formula
|
---|
671 | LOG(1, "Removing " << formulaiter->second << " for " << formulaiter->first << " from MoleculeFormulaMap.");
|
---|
672 | MoleculeFormulaMap.erase( formulaiter );
|
---|
673 | const std::string formula = readdItem(molitem);
|
---|
674 | // and add to formula<->molecule bimap with updated formula
|
---|
675 | LOG(1, "Adding " << formula << " for " << *moveiter << " to MoleculeFormulaMap.");
|
---|
676 | MoleculeFormulaMap.insert( std::make_pair(*moveiter, formula) );
|
---|
677 | // LOG(1, "Adding " << formula << " to toBeSetOccurrence.");
|
---|
678 | toBeSetOccurrence.insert( formula );
|
---|
679 | }
|
---|
680 | }
|
---|
681 |
|
---|
682 | /// 3. remove all items whose molecules have been removed
|
---|
683 | for (std::vector<moleculeId_t>::const_iterator removeiter = removedMolecules_copy.begin();
|
---|
684 | removeiter != removedMolecules_copy.end(); ++removeiter) {
|
---|
685 | // LOG(1, "Removing molecule " << *removeiter);
|
---|
686 | if (!isMoleculeItemPresent(*removeiter))
|
---|
687 | continue;
|
---|
688 | QtMoleculeItem *item = MoleculeIdToItem(*removeiter);
|
---|
689 | if (item != NULL) {
|
---|
690 | const std::string formula = item->parent()->text().toStdString();
|
---|
691 | // LOG(1, "Adding " << formula << " to toBeSetOccurrence.");
|
---|
692 | toBeSetOccurrence.insert( formula );
|
---|
693 | removeMoleculeItem(item);
|
---|
694 | KilledItemsPerMolecule.erase( *removeiter );
|
---|
695 | }
|
---|
696 | }
|
---|
697 |
|
---|
698 | // throw out items that we added by an update() while we are in this function
|
---|
699 | listAccessing_mutex.lock();
|
---|
700 | for (std::vector<moleculeId_t>::const_iterator removeiter = removedMolecules_copy.begin();
|
---|
701 | removeiter != removedMolecules_copy.end(); ++removeiter) {
|
---|
702 | for (unsigned int i=0;i< QtMoleculeItem::COLUMNTYPES_MAX; ++i)
|
---|
703 | dirtyMolItems.erase( std::make_pair(*removeiter,(QtMoleculeItem::COLUMNTYPES)i) );
|
---|
704 | toBeMovedItems.erase(*removeiter);
|
---|
705 | visibilityMolItems.erase(*removeiter);
|
---|
706 | }
|
---|
707 | listAccessing_mutex.unlock();
|
---|
708 | // after that it is not a problem as items have been removed (hence signOff() was called)
|
---|
709 |
|
---|
710 | /// 4. instantiate all new items
|
---|
711 | for (std::vector<moleculeId_t>::const_iterator moliter = newMolecules_copy.begin();
|
---|
712 | moliter != newMolecules_copy.end(); ++moliter) {
|
---|
713 | // LOG(1, "Adding molecule " << *moliter);
|
---|
714 | // check that World knows the molecule still
|
---|
715 | const molecule * const mol = const_cast<const World &>(World::getInstance()).
|
---|
716 | getMolecule(MoleculeById(*moliter));
|
---|
717 | if ((mol != NULL) && (mol->getId() == *moliter)) {
|
---|
718 | const std::string formula = addMolecule(mol);;
|
---|
719 | // LOG(1, "Adding " << formula << " to toBeSetOccurrence.");
|
---|
720 | toBeSetOccurrence.insert( formula );
|
---|
721 | } else {
|
---|
722 | ELOG(2, "Molecule " << *moliter
|
---|
723 | << " disappeared before we could render it in QtMoleculeList.");
|
---|
724 | }
|
---|
725 | }
|
---|
726 |
|
---|
727 | /// 5a. update the group item's occurrence and visibility
|
---|
728 | std::set<int> RowsToRemove;
|
---|
729 | for (std::set<std::string>::const_iterator groupiter = toBeSetOccurrence.begin();
|
---|
730 | groupiter != toBeSetOccurrence.end(); ++groupiter) {
|
---|
731 | // LOG(1, "Updating group item's occurence " << *groupiter);
|
---|
732 | QStandardItem *groupitem = FormulaToGroupItem(*groupiter);
|
---|
733 | const int index = setOccurrence(groupitem);
|
---|
734 | if (index != -1) {
|
---|
735 | // LOG(1, "Removing row of group item " << groupitem);
|
---|
736 | RowsToRemove.insert(index);
|
---|
737 | }
|
---|
738 | }
|
---|
739 | toBeSetOccurrence.clear();
|
---|
740 |
|
---|
741 | // remove all visibility updates whose row is removed
|
---|
742 | for (list_of_group_items_t::iterator visiter = visibilityGroupItems_copy.begin();
|
---|
743 | visiter != visibilityGroupItems_copy.end(); ) {
|
---|
744 | QStandardItem * const groupitem = FormulaToGroupItem(visiter->first);
|
---|
745 | if (RowsToRemove.count(groupitem->index().row()) != 0) {
|
---|
746 | // LOG(1, "Removing vis item " << *visiter << " because of removed group item.");
|
---|
747 | visibilityGroupItems_copy.erase(visiter++);
|
---|
748 | } else
|
---|
749 | ++visiter;
|
---|
750 | }
|
---|
751 |
|
---|
752 | // update visibility of all group items
|
---|
753 | for (list_of_group_items_t::iterator visiter = visibilityGroupItems_copy.begin();
|
---|
754 | visiter != visibilityGroupItems_copy.end(); ++visiter) {
|
---|
755 | // LOG(1, "Updating visibility of item " << *visiter);
|
---|
756 | QStandardItem * const groupitem =
|
---|
757 | getSpecificGroupItem(FormulaToGroupItem(visiter->first),
|
---|
758 | visiter->second);
|
---|
759 | setVisibilityForGroupItem(groupitem);
|
---|
760 | }
|
---|
761 |
|
---|
762 | /// 5b. remove all rows with 0 occurrence starting from last
|
---|
763 | for (std::set<int>::reverse_iterator riter = RowsToRemove.rbegin();
|
---|
764 | riter != RowsToRemove.rend(); ++riter) {
|
---|
765 | // LOG(1, "Removing group item at row " << *riter);
|
---|
766 | removeRows(*riter, 1, invisibleRootItem()->index());
|
---|
767 | }
|
---|
768 |
|
---|
769 | // and done
|
---|
770 | // LOG(1, "Done with update.");
|
---|
771 | }
|
---|