source: src/UIElements/Views/Qt4/MoleculeList/QtMoleculeList.cpp@ 507f3c

Action_Thermostats Add_AtomRandomPerturbation Add_FitFragmentPartialChargesAction Add_RotateAroundBondAction Add_SelectAtomByNameAction Added_ParseSaveFragmentResults AddingActions_SaveParseParticleParameters Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_ParticleName_to_Atom Adding_StructOpt_integration_tests AtomFragments Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.5.4 Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator CombiningParticlePotentialParsing Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_BoundInBox_CenterInBox_MoleculeActions Fix_ChargeSampling_PBC Fix_ChronosMutex Fix_FitPartialCharges Fix_FitPotential_needs_atomicnumbers Fix_ForceAnnealing Fix_IndependentFragmentGrids Fix_ParseParticles Fix_ParseParticles_split_forward_backward_Actions Fix_PopActions Fix_QtFragmentList_sorted_selection Fix_Restrictedkeyset_FragmentMolecule Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns Fix_fitting_potentials Fixes ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion FragmentAction_writes_AtomFragments FragmentMolecule_checks_bonddegrees GeometryObjects Gui_Fixes Gui_displays_atomic_force_velocity ImplicitCharges IndependentFragmentGrids IndependentFragmentGrids_IndividualZeroInstances IndependentFragmentGrids_IntegrationTest IndependentFragmentGrids_Sole_NN_Calculation JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix MoreRobust_FragmentAutomation ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PdbParser_setsAtomName PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks Rewrite_FitPartialCharges RotateToPrincipalAxisSystem_UndoRedo SaturateAtoms_findBestMatching SaturateAtoms_singleDegree StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_LinearAlgebra Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg Switchable_LogView ThirdParty_MPQC_rebuilt_buildsystem TrajectoryDependenant_MaxOrder TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps TremoloParser_setsAtomName Ubuntu_1604_changes stable
Last change on this file since 507f3c was 375bc3f, checked in by Frederik Heber <heber@…>, 9 years ago

FIX: Empty row in QtMoleculeList was not deleted.

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