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 | #include "UIElements/Qt4/InstanceBoard/QtObservedInstanceBoard.hpp"
|
---|
43 |
|
---|
44 | #include <boost/bind.hpp>
|
---|
45 | #include <boost/thread/locks.hpp>
|
---|
46 | #include <iostream>
|
---|
47 |
|
---|
48 | #include "CodePatterns/MemDebug.hpp"
|
---|
49 |
|
---|
50 | #include "CodePatterns/Log.hpp"
|
---|
51 | #include "CodePatterns/Observer/Notification.hpp"
|
---|
52 |
|
---|
53 | #include "Atom/atom.hpp"
|
---|
54 | #include "Actions/MoleculeAction/ChangeNameAction.hpp"
|
---|
55 | #include "Actions/SelectionAction/Molecules/PopMoleculesAction.hpp"
|
---|
56 | #include "Actions/SelectionAction/Molecules/PushMoleculesAction.hpp"
|
---|
57 | #include "Actions/SelectionAction/Molecules/MoleculeByIdAction.hpp"
|
---|
58 | #include "Actions/ActionQueue.hpp"
|
---|
59 | #include "Actions/ActionSequence.hpp"
|
---|
60 | #include "Actions/ActionTrait.hpp"
|
---|
61 | #include "Actions/MakroAction.hpp"
|
---|
62 | #include "Descriptors/MoleculeIdDescriptor.hpp"
|
---|
63 | #include "Formula.hpp"
|
---|
64 | #include "molecule.hpp"
|
---|
65 |
|
---|
66 | using namespace std;
|
---|
67 |
|
---|
68 | QtMoleculeList::QtMoleculeList(
|
---|
69 | QtObservedInstanceBoard *_board,
|
---|
70 | QObject *_parent) :
|
---|
71 | QStandardItemModel(_parent),
|
---|
72 | board(_board),
|
---|
73 | observer(_board)
|
---|
74 | {
|
---|
75 | setColumnCount(QtMoleculeItemFactory::COLUMNCOUNT);
|
---|
76 |
|
---|
77 | connect(this,SIGNAL(itemChanged(QStandardItem*)),
|
---|
78 | this,SLOT(moleculeNameChanged(QStandardItem*)));
|
---|
79 | connect(this, SIGNAL(itemChanged(QStandardItem*)),
|
---|
80 | this, SLOT(checkForVisibilityChange(QStandardItem*)));
|
---|
81 | connect(&observer, SIGNAL(MoleculeInserted(QtObservedMolecule::ptr)),
|
---|
82 | this, SLOT(moleculeInserted(QtObservedMolecule::ptr)));
|
---|
83 | connect(&observer, SIGNAL(MoleculeRemoved(const moleculeId_t)),
|
---|
84 | this, SLOT(moleculeRemoved(const moleculeId_t)));
|
---|
85 | connect(&observer, SIGNAL(FormulaChanged(const QtObservedMolecule::ptr)),
|
---|
86 | this, SLOT(formulaChanged(const QtObservedMolecule::ptr)));
|
---|
87 | connect(&observer, SIGNAL(NameChanged(const QtObservedMolecule::ptr)),
|
---|
88 | this, SLOT(nameChanged(const QtObservedMolecule::ptr)));
|
---|
89 | connect(&observer, SIGNAL(AtomCountChanged(const QtObservedMolecule::ptr)),
|
---|
90 | this, SLOT(atomcountChanged(const QtObservedMolecule::ptr)));
|
---|
91 | connect(board, SIGNAL(moleculeIndexChanged(const moleculeId_t,const moleculeId_t)),
|
---|
92 | this, SLOT(moleculeIndexChanged(const moleculeId_t, const moleculeId_t)));
|
---|
93 | }
|
---|
94 |
|
---|
95 | QtMoleculeList::~QtMoleculeList()
|
---|
96 | {}
|
---|
97 |
|
---|
98 | QVariant QtMoleculeList::headerData(int section, Qt::Orientation orientation, int role) const
|
---|
99 | {
|
---|
100 | if (role == Qt::DisplayRole) {
|
---|
101 | if (orientation == Qt::Horizontal) {
|
---|
102 | if (section < QtMoleculeItem::COLUMNTYPES_MAX)
|
---|
103 | return QString(QtMoleculeItemFactory::COLUMNNAMES[section]);
|
---|
104 | }
|
---|
105 | }
|
---|
106 | return QVariant();
|
---|
107 | }
|
---|
108 |
|
---|
109 | void QtMoleculeList::moleculeInserted(QtObservedMolecule::ptr _mol)
|
---|
110 | {
|
---|
111 | LOG(1, "Adding molecule " << _mol->getMolName());
|
---|
112 | // check that World knows the molecule still
|
---|
113 | const std::string formula = addMolecule(_mol);
|
---|
114 | LOG(1, "Adding " << formula << " to toBeSetOccurrence.");
|
---|
115 | setOccurrence(FormulaToGroupItem(formula));
|
---|
116 | }
|
---|
117 |
|
---|
118 | void QtMoleculeList::moleculeRemoved(const moleculeId_t _id)
|
---|
119 | {
|
---|
120 | LOG(1, "Removing molecule " << _id);
|
---|
121 | if (!isMoleculeItemPresent(_id)) {
|
---|
122 | ELOG(1, "QtMoleculeItem to id " << _id << " has disappeared before removal.");
|
---|
123 | return;
|
---|
124 | }
|
---|
125 | QtMoleculeItem *item = MoleculeIdToItem(_id);
|
---|
126 | if (item != NULL) {
|
---|
127 | const std::string formula = item->parent()->text().toStdString();
|
---|
128 | LOG(1, "Adding " << formula << " to toBeSetOccurrence.");
|
---|
129 | const int removeindex = setOccurrence(FormulaToGroupItem(formula));
|
---|
130 | removeMoleculeItem(item);
|
---|
131 | if (removeindex != -1) {
|
---|
132 | LOG(1, "Removing row of group item to " << formula);
|
---|
133 | removeRows(removeindex, 1, invisibleRootItem()->index());
|
---|
134 | }
|
---|
135 | }
|
---|
136 | }
|
---|
137 |
|
---|
138 | template<class T>
|
---|
139 | void exchangeKeys(
|
---|
140 | T &_container,
|
---|
141 | const moleculeId_t _oldid,
|
---|
142 | const moleculeId_t _newid)
|
---|
143 | {
|
---|
144 | typename T::iterator iter = _container.find(_oldid);
|
---|
145 | ASSERT(_container.find(_newid) == _container.end(),
|
---|
146 | "exchangeKeys() - new id "+toString(_newid)
|
---|
147 | +" already exists in container.");
|
---|
148 | _container.insert( std::make_pair(_newid, iter->second) );
|
---|
149 | _container.erase(iter);
|
---|
150 | }
|
---|
151 |
|
---|
152 | template<class T>
|
---|
153 | void exchangeKeysInSet(
|
---|
154 | T &_container,
|
---|
155 | const moleculeId_t _oldid,
|
---|
156 | const moleculeId_t _newid)
|
---|
157 | {
|
---|
158 | typename T::iterator iter = _container.find(_oldid);
|
---|
159 | ASSERT(_container.find(_newid) == _container.end(),
|
---|
160 | "exchangeKeys() - new id "+toString(_newid)
|
---|
161 | +" already exists in container.");
|
---|
162 | _container.insert( _newid );
|
---|
163 | _container.erase(iter);
|
---|
164 | }
|
---|
165 |
|
---|
166 | template<class T>
|
---|
167 | void exchangeKeysOverAllColumns(
|
---|
168 | T &_container,
|
---|
169 | const moleculeId_t _oldid,
|
---|
170 | const moleculeId_t _newid)
|
---|
171 | {
|
---|
172 | for (int i=0;i<QtMoleculeItem::COLUMNTYPES_MAX;++i) {
|
---|
173 | typename T::iterator iter =
|
---|
174 | _container.find( std::make_pair(_oldid, (enum QtMoleculeItem::COLUMNTYPES)i) );
|
---|
175 | if (iter == _container.end())
|
---|
176 | continue;
|
---|
177 | ASSERT(_container.find( std::make_pair(_newid,(enum QtMoleculeItem::COLUMNTYPES)i) ) == _container.end(),
|
---|
178 | "exchangeKeys() - new id "+toString(_newid)
|
---|
179 | +" already exists in container.");
|
---|
180 | _container.insert( std::make_pair(_newid, (enum QtMoleculeItem::COLUMNTYPES)i) );
|
---|
181 | _container.erase(iter);
|
---|
182 | }
|
---|
183 | }
|
---|
184 |
|
---|
185 | void QtMoleculeList::moleculeIndexChanged(
|
---|
186 | const moleculeId_t _oldid,
|
---|
187 | const moleculeId_t _newid)
|
---|
188 | {
|
---|
189 | // go through all list and change keys
|
---|
190 | exchangeKeys(MoleculeFormulaMap, _oldid, _newid);
|
---|
191 | {
|
---|
192 | MoleculeItemBiMap_t::left_iterator iter = MoleculeItemBiMap.left.find(_oldid);
|
---|
193 | ASSERT(MoleculeItemBiMap.left.count(_newid),
|
---|
194 | "QtMoleculeList::moleculeIndexChanged() - new id "+toString(_newid)
|
---|
195 | +" already exists in MoleculeItemBiMap.");
|
---|
196 | MoleculeItemBiMap.left.insert( std::make_pair(_newid, iter->second) );
|
---|
197 | MoleculeItemBiMap.left.erase(iter);
|
---|
198 | }
|
---|
199 | }
|
---|
200 |
|
---|
201 | bool QtMoleculeList::isMoleculeItemPresent(const moleculeId_t _molid) const
|
---|
202 | {
|
---|
203 | MoleculeItemBiMap_t::left_const_iterator iter =
|
---|
204 | MoleculeItemBiMap.left.find(_molid);
|
---|
205 | return ( iter != MoleculeItemBiMap.left.end());
|
---|
206 | }
|
---|
207 |
|
---|
208 | QtMoleculeItem * QtMoleculeList::MoleculeIdToItem(const moleculeId_t _molid) const
|
---|
209 | {
|
---|
210 | MoleculeItemBiMap_t::left_const_iterator iter =
|
---|
211 | MoleculeItemBiMap.left.find(_molid);
|
---|
212 | ASSERT( iter != MoleculeItemBiMap.left.end(),
|
---|
213 | "QtMoleculeList::MoleculeIdToItem() - could not find item to id "
|
---|
214 | +toString(_molid));
|
---|
215 | return iter->second;
|
---|
216 | }
|
---|
217 |
|
---|
218 | const moleculeId_t QtMoleculeList::ItemToMoleculeId(const QtMoleculeItem * const _item) const
|
---|
219 | {
|
---|
220 | const MoleculeItemBiMap_t::right_const_iterator iter =
|
---|
221 | MoleculeItemBiMap.right.find(const_cast<QtMoleculeItem * const>(_item));
|
---|
222 | if (iter != MoleculeItemBiMap.right.end())
|
---|
223 | return iter->second;
|
---|
224 | else
|
---|
225 | return -1;
|
---|
226 | }
|
---|
227 |
|
---|
228 | QtMoleculeItem * QtMoleculeList::getSpecificMoleculeItem(
|
---|
229 | const QtMoleculeItem * const _item,
|
---|
230 | const enum QtMoleculeItem::COLUMNTYPES _type) const
|
---|
231 | {
|
---|
232 | QStandardItem *parent_item = _item->parent();
|
---|
233 | ASSERT( parent_item != NULL,
|
---|
234 | "QtMoleculeList::getSpecificMoleculeItem() - parent of molecule item is NULL");
|
---|
235 | return static_cast<QtMoleculeItem *>(parent_item->child(_item->index().row(), _type));
|
---|
236 | }
|
---|
237 |
|
---|
238 | bool QtMoleculeList::isGroupItemPresent(const std::string &_formula) const
|
---|
239 | {
|
---|
240 | FormulaTreeItemBiMap_t::left_const_iterator iter =
|
---|
241 | FormulaItemBiMap.left.find(_formula);
|
---|
242 | return ( iter != FormulaItemBiMap.left.end());
|
---|
243 | }
|
---|
244 |
|
---|
245 | QStandardItem * QtMoleculeList::FormulaToGroupItem(const std::string &_formula) const
|
---|
246 | {
|
---|
247 | FormulaTreeItemBiMap_t::left_const_iterator iter =
|
---|
248 | FormulaItemBiMap.left.find(_formula);
|
---|
249 | ASSERT( iter != FormulaItemBiMap.left.end(),
|
---|
250 | "QtMoleculeList::FormulaToGroupItem() - could not find item to formula "
|
---|
251 | +toString(_formula));
|
---|
252 | return iter->second;
|
---|
253 | }
|
---|
254 |
|
---|
255 | const std::string& QtMoleculeList::GroupItemToFormula(const QStandardItem * const _item) const
|
---|
256 | {
|
---|
257 | static std::string emptystring;
|
---|
258 | const FormulaTreeItemBiMap_t::right_const_iterator iter =
|
---|
259 | FormulaItemBiMap.right.find(const_cast<QStandardItem * const>(_item));
|
---|
260 | if (iter != FormulaItemBiMap.right.end())
|
---|
261 | return iter->second;
|
---|
262 | else
|
---|
263 | return emptystring;
|
---|
264 | }
|
---|
265 |
|
---|
266 | QStandardItem * QtMoleculeList::getSpecificGroupItem(
|
---|
267 | const QStandardItem * const _item,
|
---|
268 | const enum QtMoleculeItem::COLUMNTYPES _type) const
|
---|
269 | {
|
---|
270 | return invisibleRootItem()->child(_item->index().row(), _type);
|
---|
271 | }
|
---|
272 |
|
---|
273 | const QModelIndex QtMoleculeList::MoleculeIdToIndex(const moleculeId_t _id) const
|
---|
274 | {
|
---|
275 | QtMoleculeItem * const item = MoleculeIdToItem(_id);
|
---|
276 | ASSERT(item != NULL,
|
---|
277 | "QtMoleculeList::MoleculeIdToIndex() - could not find item to "
|
---|
278 | +toString(_id));
|
---|
279 | return indexFromItem(item);
|
---|
280 | }
|
---|
281 |
|
---|
282 | const moleculeId_t QtMoleculeList::IndexToMoleculeId(const QModelIndex &_index) const
|
---|
283 | {
|
---|
284 | QtMoleculeItem * const item = dynamic_cast<QtMoleculeItem *>(itemFromIndex(_index));
|
---|
285 | if (item == NULL)
|
---|
286 | return -1;
|
---|
287 | else
|
---|
288 | return ItemToMoleculeId(item);
|
---|
289 | }
|
---|
290 |
|
---|
291 | void QtMoleculeList::addGroupItem(
|
---|
292 | QStandardItem *&mainitem,
|
---|
293 | const std::string &_molecule_formula)
|
---|
294 | {
|
---|
295 | QList<QStandardItem *> groupItems =
|
---|
296 | QtMoleculeItemFactory::createGroupItems(_molecule_formula);
|
---|
297 | mainitem = groupItems.front();
|
---|
298 | FormulaItemBiMap.left.insert( std::make_pair(_molecule_formula, mainitem) );
|
---|
299 | if (FormulaVisibilityCountMap.count(_molecule_formula) == 0)
|
---|
300 | FormulaVisibilityCountMap.insert( std::make_pair(_molecule_formula, 0));
|
---|
301 | invisibleRootItem()->appendRow(groupItems);
|
---|
302 | }
|
---|
303 |
|
---|
304 | QList<QStandardItem *> QtMoleculeList::createMoleculeItems(
|
---|
305 | QtObservedMolecule::ptr &_ObservedMolecule,
|
---|
306 | std::string &_molecule_formula)
|
---|
307 | {
|
---|
308 | QList<QStandardItem *> molItems =
|
---|
309 | QtMoleculeItemFactory::createMoleculeItems(_ObservedMolecule);
|
---|
310 | QtMoleculeItem *mol_item = dynamic_cast<QtMoleculeItem *>(molItems.front());
|
---|
311 | ASSERT( mol_item != NULL,
|
---|
312 | "QtMoleculeList::createMoleculeItems() - item from factory was not a QtMoleculeItem?");
|
---|
313 | MoleculeItemBiMap.left.insert( std::make_pair(_ObservedMolecule->getMolIndex(), mol_item) );
|
---|
314 |
|
---|
315 | QStandardItem *formulaitem = molItems.at(QtMoleculeItem::FORMULA);
|
---|
316 | ASSERT( formulaitem != NULL,
|
---|
317 | "QtMoleculeList::createMoleculeItems() - Formula item not created by factory?");
|
---|
318 | _molecule_formula = formulaitem->text().toStdString();
|
---|
319 |
|
---|
320 | LOG(1, "Adding " << _molecule_formula << " for "
|
---|
321 | << _ObservedMolecule->getMolIndex() << " to MoleculeFormulaMap.");
|
---|
322 | MoleculeFormulaMap.insert( std::make_pair( _ObservedMolecule->getMolIndex(), _molecule_formula) );
|
---|
323 | // LOG(1, "Inserting molecule " << _molid << ": " << _molecule_formula);
|
---|
324 | return molItems;
|
---|
325 | }
|
---|
326 |
|
---|
327 | std::string QtMoleculeList::addMolecule(QtObservedMolecule::ptr &_ObservedMolecule)
|
---|
328 | {
|
---|
329 | // find group if already in list
|
---|
330 | QStandardItem *groupItem = NULL;
|
---|
331 |
|
---|
332 | // create molecule items and obtain the molecule's formula
|
---|
333 | std::string molecule_formula;
|
---|
334 | QList<QStandardItem *> molItems = createMoleculeItems(_ObservedMolecule, molecule_formula);
|
---|
335 |
|
---|
336 | // new molecule type -> create new group
|
---|
337 | if (!isGroupItemPresent(molecule_formula)){
|
---|
338 | // insert new formula entry into visibility
|
---|
339 | #ifndef NDEBUG
|
---|
340 | std::pair< FormulaVisibilityCountMap_t::iterator, bool> visibilityinserter =
|
---|
341 | #endif
|
---|
342 | FormulaVisibilityCountMap.insert(
|
---|
343 | std::make_pair( molecule_formula, (unsigned int)0) );
|
---|
344 | ASSERT( visibilityinserter.second,
|
---|
345 | "QtMoleculeList::refill() - molecule with formula "
|
---|
346 | +molecule_formula+" already in FormulaVisibilityCountMap.");
|
---|
347 |
|
---|
348 | // create item and place into Map with formula as key
|
---|
349 | addGroupItem(groupItem, molecule_formula);
|
---|
350 | } else {
|
---|
351 | groupItem = FormulaToGroupItem(molecule_formula);
|
---|
352 | }
|
---|
353 | ASSERT( groupItem != NULL,
|
---|
354 | "QtMoleculeList::addMolecule() - item with id "+toString(_ObservedMolecule->getMolIndex())
|
---|
355 | +" has no parent?");
|
---|
356 | groupItem->appendRow(molItems);
|
---|
357 |
|
---|
358 | return molecule_formula;
|
---|
359 | }
|
---|
360 |
|
---|
361 | void QtMoleculeList::removeMoleculeItem(QtMoleculeItem * const _item)
|
---|
362 | {
|
---|
363 | const QModelIndex mol_index = indexFromItem(_item);
|
---|
364 | QStandardItem *groupitem = _item->parent();
|
---|
365 | const QModelIndex group_index = groupitem->index();
|
---|
366 | {
|
---|
367 | MoleculeItemBiMap_t::right_iterator removeiter =
|
---|
368 | MoleculeItemBiMap.right.find(_item);
|
---|
369 | ASSERT( removeiter != MoleculeItemBiMap.right.end(),
|
---|
370 | "QtMoleculeList::removeMoleculeItem() - could not find item in MoleculeBiMap.");
|
---|
371 | // LOG(1, "Erasing molecule " << (removeiter->second));
|
---|
372 | {
|
---|
373 | MoleculeFormulaMap_t::iterator removeformulaiter =
|
---|
374 | MoleculeFormulaMap.find(removeiter->second);
|
---|
375 | ASSERT( removeformulaiter != MoleculeFormulaMap.end(),
|
---|
376 | "QtMoleculeList::removeMoleculeItem() - could not find id "
|
---|
377 | +toString(removeiter->second)+" in MoleculeFormulaMap.");
|
---|
378 | LOG(1, "Removing " << removeformulaiter->second << " for "
|
---|
379 | << removeformulaiter->first << " from MoleculeFormulaMap.");
|
---|
380 | MoleculeFormulaMap.erase( removeformulaiter );
|
---|
381 | }
|
---|
382 | MoleculeItemBiMap.right.erase(removeiter);
|
---|
383 | }
|
---|
384 | removeRows(mol_index.row(), 1, group_index);
|
---|
385 | }
|
---|
386 |
|
---|
387 | void QtMoleculeList::checkForVisibilityChange(QStandardItem* _item)
|
---|
388 | {
|
---|
389 | // qDebug() << "Item changed called.";
|
---|
390 |
|
---|
391 | if (_item->index().column() == QtMoleculeItem::VISIBILITY) {
|
---|
392 | // qDebug() << "visibilityItem changed: " << (_item->checkState() ? "checked" : "unchecked");
|
---|
393 | if ((_item->parent() == NULL) || (_item->parent() == invisibleRootItem())) {
|
---|
394 | LOG(1, "Updating visibility of group item " << _item);
|
---|
395 | setVisibilityForGroupItem(_item);
|
---|
396 | } else {
|
---|
397 | LOG(1, "Updating visibility of item " << _item);
|
---|
398 | setVisibilityForMoleculeItem( assert_cast<QtMoleculeItem *>(_item) );
|
---|
399 | }
|
---|
400 | }
|
---|
401 | }
|
---|
402 |
|
---|
403 | void QtMoleculeList::setVisibilityForMoleculeItem(QtMoleculeItem* _item)
|
---|
404 | {
|
---|
405 | const bool visible = _item->checkState();
|
---|
406 | const moleculeId_t molid = _item->getMoleculeId();
|
---|
407 | std::string molecule_formula("illegal");
|
---|
408 | {
|
---|
409 | const MoleculeFormulaMap_t::const_iterator formulaiter =
|
---|
410 | MoleculeFormulaMap.find(molid);
|
---|
411 | ASSERT( formulaiter != MoleculeFormulaMap.end(),
|
---|
412 | "QtMoleculeList::setVisibilityForMoleculeItem() - formula of molecule "
|
---|
413 | +toString(molid)+" unknown.");
|
---|
414 | molecule_formula = formulaiter->second;
|
---|
415 | }
|
---|
416 | ASSERT( FormulaVisibilityCountMap.count(molecule_formula) != 0,
|
---|
417 | "QtMoleculeList::setVisibilityForMoleculeItem() - molecule with formula " +molecule_formula
|
---|
418 | +" is not present in FormulaVisibilityCountMap.");
|
---|
419 |
|
---|
420 | // get parent
|
---|
421 | QStandardItem *groupItem = _item->parent();
|
---|
422 | QStandardItem *visgroupItem = getSpecificGroupItem(groupItem, QtMoleculeItem::VISIBILITY);
|
---|
423 | ASSERT( groupItem != NULL,
|
---|
424 | "QtMoleculeList::setVisibilityForMoleculeItem() - item with id "
|
---|
425 | +toString(_item->getMoleculeId())+" has not parent?");
|
---|
426 | // check whether we have to set the group item
|
---|
427 |
|
---|
428 | if (visible) {
|
---|
429 | ++(FormulaVisibilityCountMap[molecule_formula]);
|
---|
430 | // compare with occurence/total number of molecules
|
---|
431 | if (FormulaVisibilityCountMap[molecule_formula] ==
|
---|
432 | (unsigned int)(groupItem->rowCount()))
|
---|
433 | visgroupItem->setCheckState(Qt::Checked);
|
---|
434 | } else {
|
---|
435 | --(FormulaVisibilityCountMap[molecule_formula]);
|
---|
436 | // none selected anymore?
|
---|
437 | if (FormulaVisibilityCountMap[molecule_formula] == 0)
|
---|
438 | visgroupItem->setCheckState(Qt::Unchecked);
|
---|
439 | }
|
---|
440 |
|
---|
441 | emit moleculesVisibilityChanged(_item->getMoleculeId(), visible);
|
---|
442 | }
|
---|
443 |
|
---|
444 | void QtMoleculeList::setVisibilityForGroupItem(QStandardItem* _item)
|
---|
445 | {
|
---|
446 | // go through all children, but don't enter for groupItem once more
|
---|
447 | const bool visible = _item->checkState();
|
---|
448 | QStandardItem *groupitem = getSpecificGroupItem(_item, QtMoleculeItem::NAME);
|
---|
449 | for (int i=0;i<groupitem->rowCount();++i) {
|
---|
450 | QtMoleculeItem * const molItem = dynamic_cast<QtMoleculeItem *>(
|
---|
451 | groupitem->child(i, QtMoleculeItem::VISIBILITY));
|
---|
452 | if (molItem->checkState() != visible) {
|
---|
453 | molItem->setCheckState(visible ? Qt::Checked : Qt::Unchecked);
|
---|
454 |
|
---|
455 | // emit signal
|
---|
456 | emit moleculesVisibilityChanged(molItem->getMoleculeId(), visible);
|
---|
457 | }
|
---|
458 | }
|
---|
459 | // set current number of visible children
|
---|
460 | const std::string molecule_formula =
|
---|
461 | GroupItemToFormula( getSpecificGroupItem(_item, QtMoleculeItem::NAME) );
|
---|
462 | FormulaVisibilityCountMap_t::iterator countiter =
|
---|
463 | FormulaVisibilityCountMap.find(molecule_formula);
|
---|
464 | ASSERT( countiter != FormulaVisibilityCountMap.end(),
|
---|
465 | "QtMoleculeList::setVisibilityForGroupItem() - molecules "+molecule_formula
|
---|
466 | +" have no entry in visibility count map?");
|
---|
467 | countiter->second = visible ? groupitem->rowCount() : 0;
|
---|
468 | }
|
---|
469 |
|
---|
470 | static
|
---|
471 | MoleCuilder::MakroAction *constructMakroRenameAction(
|
---|
472 | MoleCuilder::ActionSequence &sequence,
|
---|
473 | const std::string &_new_name,
|
---|
474 | const moleculeId_t _molid
|
---|
475 | )
|
---|
476 | {
|
---|
477 | MoleCuilder::ActionQueue &AQ = MoleCuilder::ActionQueue::getInstance();
|
---|
478 | MoleCuilder::ActionTrait trait("change-single-molecule-name");
|
---|
479 | sequence.addAction(AQ.getActionByName("push-molecule-selection").clone(MoleCuilder::Action::NonInteractive));
|
---|
480 | MoleCuilder::Action * const selectaction =
|
---|
481 | AQ.getActionByName("select-molecule-by-id").clone(MoleCuilder::Action::NonInteractive);
|
---|
482 | {
|
---|
483 | std::stringstream molid_string;
|
---|
484 | molid_string << toString(_molid);
|
---|
485 | selectaction->setOptionValue("select-molecule-by-id", molid_string.str());
|
---|
486 | }
|
---|
487 | sequence.addAction(selectaction);
|
---|
488 | MoleCuilder::Action * const changeaction =
|
---|
489 | AQ.getActionByName("change-molname").clone(MoleCuilder::Action::NonInteractive);
|
---|
490 | changeaction->setOptionValue("change-molname", _new_name);
|
---|
491 | sequence.addAction(changeaction);
|
---|
492 | sequence.addAction(AQ.getActionByName("pop-molecule-selection").clone(MoleCuilder::Action::NonInteractive));
|
---|
493 |
|
---|
494 | MoleCuilder::MakroAction* makroaction =
|
---|
495 | new MoleCuilder::MakroAction(trait, sequence);
|
---|
496 | return makroaction;
|
---|
497 | }
|
---|
498 |
|
---|
499 | void QtMoleculeList::moleculeNameChanged(QStandardItem* item)
|
---|
500 | {
|
---|
501 | // obtain molecule id
|
---|
502 | if ( item->index().column() == QtMoleculeItem::NAME) {
|
---|
503 | QtMoleculeItem *molitem = assert_cast<QtMoleculeItem *>(item);
|
---|
504 | MoleculeItemBiMap_t::right_const_iterator iter = MoleculeItemBiMap.right.find(molitem);
|
---|
505 | ASSERT( iter != MoleculeItemBiMap.right.end(),
|
---|
506 | "QtMoleculeList::moleculeChanged() - name of unknown molecule changed.");
|
---|
507 | const moleculeId_t molid = iter->second;
|
---|
508 | // change the name
|
---|
509 | std::string cellValue = item->text().toStdString();
|
---|
510 | if (!cellValue.empty()) {
|
---|
511 | // create actions such that we may undo
|
---|
512 | static MoleCuilder::ActionSequence sequence;
|
---|
513 | MoleCuilder::MakroAction *makroaction =
|
---|
514 | constructMakroRenameAction(sequence, cellValue, molid);
|
---|
515 | MoleCuilder::ActionQueue &AQ = MoleCuilder::ActionQueue::getInstance();
|
---|
516 | AQ.registerAction(makroaction);
|
---|
517 | AQ.queueAction("change-single-molecule-name", MoleCuilder::Action::NonInteractive);
|
---|
518 | } else {
|
---|
519 | board->getObservedMolecule(
|
---|
520 | assert_cast<QtMoleculeItem *>(item)->getMoleculeId());
|
---|
521 | }
|
---|
522 | }
|
---|
523 | }
|
---|
524 |
|
---|
525 | int QtMoleculeList::setOccurrence(QStandardItem * const _groupitem)
|
---|
526 | {
|
---|
527 | ASSERT( _groupitem != NULL,
|
---|
528 | "QtMoleculeList::setOccurrence() - group item at "+toString(_groupitem)
|
---|
529 | +" is NULL");
|
---|
530 | QModelIndex modelindex = _groupitem->index();
|
---|
531 | ASSERT( modelindex.isValid(),
|
---|
532 | "QtMoleculeList::setOccurrence() - groupitem not associated to model anymore.");
|
---|
533 | const int index = modelindex.row();
|
---|
534 | QStandardItem *parent_item =
|
---|
535 | _groupitem->parent() == NULL ? invisibleRootItem() : _groupitem->parent();
|
---|
536 | ASSERT( parent_item != NULL,
|
---|
537 | "QtMoleculeList::setOccurrence() - group item at "+toString(index)
|
---|
538 | +" does not have a parent?");
|
---|
539 | QStandardItem *occ_item = parent_item->child(index, QtMoleculeItem::OCCURRENCE);
|
---|
540 | ASSERT( occ_item != NULL,
|
---|
541 | "QtMoleculeList::setOccurrence() - group item at "+toString(index)
|
---|
542 | +" does not have an occurrence?");
|
---|
543 | const int count = _groupitem->rowCount();
|
---|
544 | if (count == 0) {
|
---|
545 | // we have to remove the group item completely
|
---|
546 | const std::string molecule_formula = _groupitem->text().toStdString();
|
---|
547 | FormulaItemBiMap.left.erase(molecule_formula);
|
---|
548 | FormulaVisibilityCountMap.erase(molecule_formula);
|
---|
549 | return index;
|
---|
550 | } else {
|
---|
551 | occ_item->setText(QString::number(count));
|
---|
552 | return -1;
|
---|
553 | }
|
---|
554 | }
|
---|
555 |
|
---|
556 | void QtMoleculeList::moveItem(
|
---|
557 | QtMoleculeItem *_molitem,
|
---|
558 | const std::string &_new_formula)
|
---|
559 | {
|
---|
560 | QStandardItem *groupitem = NULL;
|
---|
561 | // use takeRows of molecule ..
|
---|
562 | const QList<QStandardItem *> mol_row = _molitem->parent()->takeRow(_molitem->index().row());
|
---|
563 | // .. and re-add where new formula fits
|
---|
564 | if (!isGroupItemPresent(_new_formula)) {
|
---|
565 | // add new group item and formula entry
|
---|
566 | addGroupItem(groupitem, _new_formula);
|
---|
567 | } else {
|
---|
568 | groupitem = FormulaToGroupItem(_new_formula);
|
---|
569 | }
|
---|
570 | ASSERT( groupitem != NULL,
|
---|
571 | "QtMoleculeList::readdItem() - failed to create a sensible new groupitem");
|
---|
572 | // finally add again
|
---|
573 | groupitem->appendRow(mol_row);
|
---|
574 | }
|
---|
575 |
|
---|
576 |
|
---|
577 | void QtMoleculeList::formulaChanged(const QtObservedMolecule::ptr _mol)
|
---|
578 | {
|
---|
579 | // we need the id as identifier to the item
|
---|
580 | const moleculeId_t molid = _mol->getMolIndex();
|
---|
581 | LOG(3, "DEBUG: QtMoleculeList got formulaChanged for id " << molid);
|
---|
582 | QtMoleculeItem *const molitem = MoleculeIdToItem(molid);
|
---|
583 | // update item
|
---|
584 | {
|
---|
585 | QtMoleculeItem *const formulaitem = getSpecificMoleculeItem(molitem, QtMoleculeItem::FORMULA);
|
---|
586 | ASSERT(formulaitem != NULL,
|
---|
587 | "QtMoleculeList::formulaChanged() - could not item for FORMULA.");
|
---|
588 | formulaitem->updateState(_mol);
|
---|
589 | }
|
---|
590 |
|
---|
591 | LOG(3, "DEBUG: Moving item to id " << molid);
|
---|
592 | const MoleculeFormulaMap_t::iterator formulaiter =
|
---|
593 | MoleculeFormulaMap.find(molid);
|
---|
594 | ASSERT( formulaiter != MoleculeFormulaMap.end(),
|
---|
595 | "QtMoleculeList::updateItemStates() - formula of molecule "
|
---|
596 | +toString(molid)+" unknown.");
|
---|
597 | // we get old formula from stored map and new formula from the ObservedMolecule
|
---|
598 | const std::string old_formula = formulaiter->second;
|
---|
599 | const std::string new_formula = _mol->getMolFormula();
|
---|
600 |
|
---|
601 | // then we move the item if necessary
|
---|
602 | if (old_formula != new_formula) {
|
---|
603 | LOG(3, "DEBUG: Moving item " << molitem);
|
---|
604 | // remove from formula<->molecule bimap with old formula
|
---|
605 | LOG(4, "DEBUG: Removing " << old_formula << " for " << formulaiter->first << " from MoleculeFormulaMap.");
|
---|
606 | MoleculeFormulaMap.erase( formulaiter );
|
---|
607 | moveItem(molitem, new_formula);
|
---|
608 | // changing occurrences for old_formula with possible removal
|
---|
609 | const int removeindex = setOccurrence( FormulaToGroupItem(old_formula) );
|
---|
610 | if (removeindex != -1) {
|
---|
611 | LOG(3, "DEBUG: Removing row of group item to " << old_formula);
|
---|
612 | removeRows(removeindex, 1, invisibleRootItem()->index());
|
---|
613 | }
|
---|
614 | // and add to formula<->molecule bimap with updated new_formula
|
---|
615 | LOG(4, "DEBUG: Adding " << new_formula << " for " << molid << " to MoleculeFormulaMap.");
|
---|
616 | MoleculeFormulaMap.insert( std::make_pair(molid, new_formula) );
|
---|
617 | const int addindex = setOccurrence( FormulaToGroupItem(new_formula) );
|
---|
618 | ASSERT( addindex == -1,
|
---|
619 | "QtMoleculeList::formulaChanged() - add mol to new formula "+
|
---|
620 | toString(new_formula)+" made the row to be removed?");
|
---|
621 | }
|
---|
622 | }
|
---|
623 |
|
---|
624 | void QtMoleculeList::atomcountChanged(const QtObservedMolecule::ptr _mol)
|
---|
625 | {
|
---|
626 | // we need the id as identifier to the items
|
---|
627 | const moleculeId_t molid = _mol->getMolIndex();
|
---|
628 | LOG(3, "DEBUG: QtMoleculeList got atomcountChanged for id " << molid);
|
---|
629 | QtMoleculeItem *const molitem = MoleculeIdToItem(molid);
|
---|
630 | // update items
|
---|
631 | QtMoleculeItem *const atomcountitem = getSpecificMoleculeItem(molitem, QtMoleculeItem::ATOMCOUNT);
|
---|
632 | ASSERT(atomcountitem != NULL,
|
---|
633 | "QtMoleculeList::atomcountChanged() - could not item for ATOMCOUNT.");
|
---|
634 | atomcountitem->updateState(_mol);
|
---|
635 | }
|
---|
636 |
|
---|
637 | void QtMoleculeList::nameChanged(const QtObservedMolecule::ptr _mol)
|
---|
638 | {
|
---|
639 | // we need the id as identifier to the items
|
---|
640 | const moleculeId_t molid = _mol->getMolIndex();
|
---|
641 | LOG(3, "DEBUG: QtMoleculeList got nameChanged for id " << molid);
|
---|
642 | QtMoleculeItem *const molitem = MoleculeIdToItem(molid);
|
---|
643 | // update items
|
---|
644 | QtMoleculeItem *const nameitem = getSpecificMoleculeItem(molitem, QtMoleculeItem::NAME);
|
---|
645 | ASSERT(nameitem != NULL,
|
---|
646 | "QtMoleculeList::nameChanged() - could not item for NAME.");
|
---|
647 | nameitem->updateState(_mol);
|
---|
648 | }
|
---|