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