source: src/UIElements/Views/Qt4/MoleculeList/QtMoleculeList.cpp@ 59eabc

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 59eabc was bff202, checked in by Frederik Heber <heber@…>, 9 years ago

FIX: QtMoleculeList's FormulaVisibilityCountMap was not inserted into on new group item.

  • Property mode set to 100644
File size: 24.4 KB
RevLine 
[bcf653]1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
[0aa122]4 * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
[94d5ac6]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/>.
[bcf653]21 */
22
[b47bfc]23/*
[0eb7bf3]24 * QtMoleculeList.cpp
[b47bfc]25 *
26 * Created on: Jan 21, 2010
27 * Author: crueger
28 */
29
[bf3817]30// include config.h
31#ifdef HAVE_CONFIG_H
32#include <config.h>
33#endif
[bbbad5]34
[f62e60]35#include "QtMoleculeList.hpp"
[b47bfc]36
[53c1ff]37#include <QModelIndex>
[2696b1]38#include <QDebug>
[41815a3]39
[53c1ff]40#include "UIElements/Views/Qt4/MoleculeList/QtMoleculeItem.hpp"
41#include "UIElements/Views/Qt4/MoleculeList/QtMoleculeItemFactory.hpp"
[ca1535]42#include "UIElements/Qt4/InstanceBoard/QtObservedInstanceBoard.hpp"
[53c1ff]43
44#include <boost/bind.hpp>
[fcdf05]45#include <boost/thread/locks.hpp>
[b47bfc]46#include <iostream>
47
[ad011c]48#include "CodePatterns/MemDebug.hpp"
[bbbad5]49
[53c1ff]50#include "CodePatterns/Log.hpp"
[6770fa]51#include "CodePatterns/Observer/Notification.hpp"
52
[6f0841]53#include "Atom/atom.hpp"
[07b800]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"
[d2dbb5d]62#include "Descriptors/MoleculeIdDescriptor.hpp"
[26cf17]63#include "Formula.hpp"
[b47bfc]64#include "molecule.hpp"
65
66using namespace std;
67
[ca1535]68QtMoleculeList::QtMoleculeList(
[b2c2e4]69 QtObservedInstanceBoard *_board,
70 QObject *_parent) :
71 QStandardItemModel(_parent),
[5771e6]72 board(_board),
73 observer(_board)
[b47bfc]74{
[53c1ff]75 setColumnCount(QtMoleculeItemFactory::COLUMNCOUNT);
[b47bfc]76
[5771e6]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)));
[743eab]89 connect(&observer, SIGNAL(AtomCountChanged(const QtObservedMolecule::ptr)),
90 this, SLOT(atomcountChanged(const QtObservedMolecule::ptr)));
[ca1535]91 connect(board, SIGNAL(moleculeIndexChanged(const moleculeId_t,const moleculeId_t)),
92 this, SLOT(moleculeIndexChanged(const moleculeId_t, const moleculeId_t)));
[b47bfc]93}
94
[0eb7bf3]95QtMoleculeList::~QtMoleculeList()
[ca1535]96{}
[b47bfc]97
[1c3390]98QVariant QtMoleculeList::headerData(int section, Qt::Orientation orientation, int role) const
99{
100 if (role == Qt::DisplayRole) {
101 if (orientation == Qt::Horizontal) {
[fcdf05]102 if (section < QtMoleculeItem::COLUMNTYPES_MAX)
[53c1ff]103 return QString(QtMoleculeItemFactory::COLUMNNAMES[section]);
[1c3390]104 }
105 }
106 return QVariant();
107}
108
[1b07b1]109void QtMoleculeList::moleculeInserted(QtObservedMolecule::ptr _mol)
[ca1535]110{
[5771e6]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));
[ca1535]116}
117
118void QtMoleculeList::moleculeRemoved(const moleculeId_t _id)
119{
[5771e6]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 }
[ca1535]135 }
136}
137
138template<class T>
139void 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
152template<class T>
153void 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
166template<class T>
167void 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
185void 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 }
[3eb91c]199}
200
[fcdf05]201bool 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
[69b434]208QtMoleculeItem * QtMoleculeList::MoleculeIdToItem(const moleculeId_t _molid) const
[8ccf3b]209{
210 MoleculeItemBiMap_t::left_const_iterator iter =
[69b434]211 MoleculeItemBiMap.left.find(_molid);
[fcdf05]212 ASSERT( iter != MoleculeItemBiMap.left.end(),
213 "QtMoleculeList::MoleculeIdToItem() - could not find item to id "
214 +toString(_molid));
215 return iter->second;
[8ccf3b]216}
217
[69b434]218const moleculeId_t QtMoleculeList::ItemToMoleculeId(const QtMoleculeItem * const _item) const
[53c1ff]219{
220 const MoleculeItemBiMap_t::right_const_iterator iter =
221 MoleculeItemBiMap.right.find(const_cast<QtMoleculeItem * const>(_item));
[d2dbb5d]222 if (iter != MoleculeItemBiMap.right.end())
223 return iter->second;
224 else
[69b434]225 return -1;
[53c1ff]226}
227
[fcdf05]228QtMoleculeItem * 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
238bool 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
245QStandardItem * 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
255const 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
266QStandardItem * 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
[015f8c]273const 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}
[fcdf05]281
[69b434]282const moleculeId_t QtMoleculeList::IndexToMoleculeId(const QModelIndex &_index) const
[53c1ff]283{
284 QtMoleculeItem * const item = dynamic_cast<QtMoleculeItem *>(itemFromIndex(_index));
285 if (item == NULL)
[69b434]286 return -1;
[53c1ff]287 else
[69b434]288 return ItemToMoleculeId(item);
[53c1ff]289}
290
[6770fa]291void QtMoleculeList::addGroupItem(
[8ccf3b]292 QStandardItem *&mainitem,
[6770fa]293 const std::string &_molecule_formula)
294{
[53c1ff]295 QList<QStandardItem *> groupItems =
[ea2663]296 QtMoleculeItemFactory::createGroupItems(_molecule_formula);
[53c1ff]297 mainitem = groupItems.front();
[5771e6]298 FormulaItemBiMap.left.insert( std::make_pair(_molecule_formula, mainitem) );
[bff202]299 if (FormulaVisibilityCountMap.count(_molecule_formula) == 0)
300 FormulaVisibilityCountMap.insert( std::make_pair(_molecule_formula, 0));
[8ccf3b]301 invisibleRootItem()->appendRow(groupItems);
[53c1ff]302}
303
[a39d72]304QList<QStandardItem *> QtMoleculeList::createMoleculeItems(
[ca1535]305 QtObservedMolecule::ptr &_ObservedMolecule,
[a39d72]306 std::string &_molecule_formula)
[6770fa]307{
[53c1ff]308 QList<QStandardItem *> molItems =
[ea2663]309 QtMoleculeItemFactory::createMoleculeItems(_ObservedMolecule);
[53c1ff]310 QtMoleculeItem *mol_item = dynamic_cast<QtMoleculeItem *>(molItems.front());
311 ASSERT( mol_item != NULL,
[a39d72]312 "QtMoleculeList::createMoleculeItems() - item from factory was not a QtMoleculeItem?");
[ca1535]313 MoleculeItemBiMap.left.insert( std::make_pair(_ObservedMolecule->getMolIndex(), mol_item) );
[a39d72]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();
[ca1535]319
320 LOG(1, "Adding " << _molecule_formula << " for "
321 << _ObservedMolecule->getMolIndex() << " to MoleculeFormulaMap.");
322 MoleculeFormulaMap.insert( std::make_pair( _ObservedMolecule->getMolIndex(), _molecule_formula) );
[68989c]323// LOG(1, "Inserting molecule " << _molid << ": " << _molecule_formula);
[a39d72]324 return molItems;
[6770fa]325}
326
[ca1535]327std::string QtMoleculeList::addMolecule(QtObservedMolecule::ptr &_ObservedMolecule)
[6770fa]328{
329 // find group if already in list
[8ccf3b]330 QStandardItem *groupItem = NULL;
[6770fa]331
[a39d72]332 // create molecule items and obtain the molecule's formula
333 std::string molecule_formula;
[ca1535]334 QList<QStandardItem *> molItems = createMoleculeItems(_ObservedMolecule, molecule_formula);
[6770fa]335
336 // new molecule type -> create new group
[fcdf05]337 if (!isGroupItemPresent(molecule_formula)){
[6770fa]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 {
[fcdf05]351 groupItem = FormulaToGroupItem(molecule_formula);
[6770fa]352 }
353 ASSERT( groupItem != NULL,
[ca1535]354 "QtMoleculeList::addMolecule() - item with id "+toString(_ObservedMolecule->getMolIndex())
[fcdf05]355 +" has no parent?");
[a39d72]356 groupItem->appendRow(molItems);
[fcdf05]357
358 return molecule_formula;
[6770fa]359}
360
[fcdf05]361void QtMoleculeList::removeMoleculeItem(QtMoleculeItem * const _item)
[6770fa]362{
[53c1ff]363 const QModelIndex mol_index = indexFromItem(_item);
364 QStandardItem *groupitem = _item->parent();
365 const QModelIndex group_index = groupitem->index();
[fcdf05]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 }
[53c1ff]384 removeRows(mol_index.row(), 1, group_index);
385}
[6770fa]386
[2696b1]387void QtMoleculeList::checkForVisibilityChange(QStandardItem* _item)
[739ee9]388{
[2050b2]389// qDebug() << "Item changed called.";
[2696b1]390
[fcdf05]391 if (_item->index().column() == QtMoleculeItem::VISIBILITY) {
[2050b2]392// qDebug() << "visibilityItem changed: " << (_item->checkState() ? "checked" : "unchecked");
[5771e6]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 }
[2696b1]400 }
401}
[3eb91c]402
[2696b1]403void QtMoleculeList::setVisibilityForMoleculeItem(QtMoleculeItem* _item)
404{
405 const bool visible = _item->checkState();
[fcdf05]406 const moleculeId_t molid = _item->getMoleculeId();
407 std::string molecule_formula("illegal");
408 {
[5771e6]409 const MoleculeFormulaMap_t::const_iterator formulaiter =
[fcdf05]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,
[7d0ddb]417 "QtMoleculeList::setVisibilityForMoleculeItem() - molecule with formula " +molecule_formula
418 +" is not present in FormulaVisibilityCountMap.");
[3eb91c]419
[2696b1]420 // get parent
421 QStandardItem *groupItem = _item->parent();
[fcdf05]422 QStandardItem *visgroupItem = getSpecificGroupItem(groupItem, QtMoleculeItem::VISIBILITY);
[2696b1]423 ASSERT( groupItem != NULL,
424 "QtMoleculeList::setVisibilityForMoleculeItem() - item with id "
[7d0ddb]425 +toString(_item->getMoleculeId())+" has not parent?");
[2696b1]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 }
[3eb91c]440
[68989c]441 emit moleculesVisibilityChanged(_item->getMoleculeId(), visible);
[2696b1]442}
[3eb91c]443
[2696b1]444void QtMoleculeList::setVisibilityForGroupItem(QStandardItem* _item)
445{
446 // go through all children, but don't enter for groupItem once more
447 const bool visible = _item->checkState();
[fcdf05]448 QStandardItem *groupitem = getSpecificGroupItem(_item, QtMoleculeItem::NAME);
[2696b1]449 for (int i=0;i<groupitem->rowCount();++i) {
[5771e6]450 QtMoleculeItem * const molItem = dynamic_cast<QtMoleculeItem *>(
[fcdf05]451 groupitem->child(i, QtMoleculeItem::VISIBILITY));
[2696b1]452 if (molItem->checkState() != visible) {
453 molItem->setCheckState(visible ? Qt::Checked : Qt::Unchecked);
454
455 // emit signal
[68989c]456 emit moleculesVisibilityChanged(molItem->getMoleculeId(), visible);
[3eb91c]457 }
[2696b1]458 }
459 // set current number of visible children
460 const std::string molecule_formula =
[fcdf05]461 GroupItemToFormula( getSpecificGroupItem(_item, QtMoleculeItem::NAME) );
[2696b1]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;
[739ee9]468}
[2696b1]469
[07b800]470static
471MoleCuilder::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());
[b47bfc]486 }
[07b800]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
499void 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();
[5771e6]510 if (!cellValue.empty()) {
[07b800]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);
[5771e6]518 } else {
[b65879]519 board->getObservedMolecule(
520 assert_cast<QtMoleculeItem *>(item)->getMoleculeId());
[07b800]521 }
[5771e6]522 }
[07b800]523}
[b14efe]524
[53c1ff]525int QtMoleculeList::setOccurrence(QStandardItem * const _groupitem)
526{
[5771e6]527 ASSERT( _groupitem != NULL,
528 "QtMoleculeList::setOccurrence() - group item at "+toString(_groupitem)
529 +" is NULL");
[53c1ff]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?");
[fcdf05]539 QStandardItem *occ_item = parent_item->child(index, QtMoleculeItem::OCCURRENCE);
[53c1ff]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();
[fcdf05]547 FormulaItemBiMap.left.erase(molecule_formula);
[53c1ff]548 FormulaVisibilityCountMap.erase(molecule_formula);
549 return index;
550 } else {
551 occ_item->setText(QString::number(count));
552 return -1;
553 }
554}
555
[5771e6]556void QtMoleculeList::moveItem(
557 QtMoleculeItem *_molitem,
558 const std::string &_new_formula)
[53c1ff]559{
[55443e]560 QStandardItem *groupitem = NULL;
[53c1ff]561 // use takeRows of molecule ..
[55443e]562 const QList<QStandardItem *> mol_row = _molitem->parent()->takeRow(_molitem->index().row());
[53c1ff]563 // .. and re-add where new formula fits
[5771e6]564 if (!isGroupItemPresent(_new_formula)) {
[a39d72]565 // add new group item and formula entry
[5771e6]566 addGroupItem(groupitem, _new_formula);
[fcdf05]567 } else {
[5771e6]568 groupitem = FormulaToGroupItem(_new_formula);
[53c1ff]569 }
[a39d72]570 ASSERT( groupitem != NULL,
571 "QtMoleculeList::readdItem() - failed to create a sensible new groupitem");
572 // finally add again
573 groupitem->appendRow(mol_row);
[53c1ff]574}
575
576
[5771e6]577void QtMoleculeList::formulaChanged(const QtObservedMolecule::ptr _mol)
578{
[743eab]579 // we need the id as identifier to the item
[5771e6]580 const moleculeId_t molid = _mol->getMolIndex();
[743eab]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
[5771e6]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();
[2696b1]600
[5771e6]601 // then we move the item if necessary
602 if (old_formula != new_formula) {
603 LOG(3, "DEBUG: Moving item " << molitem);
[ca1535]604 // remove from formula<->molecule bimap with old formula
[5771e6]605 LOG(4, "DEBUG: Removing " << old_formula << " for " << formulaiter->first << " from MoleculeFormulaMap.");
[ca1535]606 MoleculeFormulaMap.erase( formulaiter );
[5771e6]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());
[fcdf05]613 }
[5771e6]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?");
[53c1ff]621 }
[5771e6]622}
[53c1ff]623
[743eab]624void 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
[5771e6]637void QtMoleculeList::nameChanged(const QtObservedMolecule::ptr _mol)
638{
[b65879]639 // we need the id as identifier to the items
640 const moleculeId_t molid = _mol->getMolIndex();
[743eab]641 LOG(3, "DEBUG: QtMoleculeList got nameChanged for id " << molid);
[b65879]642 QtMoleculeItem *const molitem = MoleculeIdToItem(molid);
643 // update items
[743eab]644 QtMoleculeItem *const nameitem = getSpecificMoleculeItem(molitem, QtMoleculeItem::NAME);
645 ASSERT(nameitem != NULL,
646 "QtMoleculeList::nameChanged() - could not item for NAME.");
647 nameitem->updateState(_mol);
[53c1ff]648}
Note: See TracBrowser for help on using the repository browser.