source: src/UIElements/Views/Qt4/MoleculeList/QtMoleculeList.cpp@ 50246c

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 50246c was 0a558d, checked in by Frederik Heber <heber@…>, 9 years ago

MoleculeList discerns between internal and external name change.

  • Property mode set to 100644
File size: 22.7 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)));
[3054f4a]83 connect(&observer, SIGNAL(MoleculeRemoved(ObservedValue_Index_t)),
84 this, SLOT(moleculeRemoved(ObservedValue_Index_t)));
[5771e6]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)));
[b47bfc]91}
92
[0eb7bf3]93QtMoleculeList::~QtMoleculeList()
[ca1535]94{}
[b47bfc]95
[1c3390]96QVariant QtMoleculeList::headerData(int section, Qt::Orientation orientation, int role) const
97{
98 if (role == Qt::DisplayRole) {
99 if (orientation == Qt::Horizontal) {
[fcdf05]100 if (section < QtMoleculeItem::COLUMNTYPES_MAX)
[53c1ff]101 return QString(QtMoleculeItemFactory::COLUMNNAMES[section]);
[1c3390]102 }
103 }
104 return QVariant();
105}
106
[1b07b1]107void QtMoleculeList::moleculeInserted(QtObservedMolecule::ptr _mol)
[ca1535]108{
[5771e6]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));
[ca1535]114}
115
[3054f4a]116void QtMoleculeList::moleculeRemoved(ObservedValue_Index_t _id)
[ca1535]117{
[5771e6]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 }
[ca1535]133 }
134}
135
[3054f4a]136bool QtMoleculeList::isMoleculeItemPresent(ObservedValue_Index_t _molid) const
[fcdf05]137{
138 MoleculeItemBiMap_t::left_const_iterator iter =
139 MoleculeItemBiMap.left.find(_molid);
140 return ( iter != MoleculeItemBiMap.left.end());
141}
142
[3054f4a]143QtMoleculeItem * QtMoleculeList::MoleculeIdToItem(ObservedValue_Index_t _molid) const
[8ccf3b]144{
145 MoleculeItemBiMap_t::left_const_iterator iter =
[69b434]146 MoleculeItemBiMap.left.find(_molid);
[fcdf05]147 ASSERT( iter != MoleculeItemBiMap.left.end(),
148 "QtMoleculeList::MoleculeIdToItem() - could not find item to id "
[3054f4a]149 +toString(observer.getIdtoIndex(_molid)));
[fcdf05]150 return iter->second;
[8ccf3b]151}
152
[3054f4a]153ObservedValue_Index_t QtMoleculeList::ItemToMoleculeId(const QtMoleculeItem * const _item) const
[53c1ff]154{
155 const MoleculeItemBiMap_t::right_const_iterator iter =
156 MoleculeItemBiMap.right.find(const_cast<QtMoleculeItem * const>(_item));
[d2dbb5d]157 if (iter != MoleculeItemBiMap.right.end())
158 return iter->second;
159 else
[3054f4a]160 return NULL;
[53c1ff]161}
162
[fcdf05]163QtMoleculeItem * 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
173bool 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
180QStandardItem * 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
190const 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
201QStandardItem * 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
[3054f4a]208const QModelIndex QtMoleculeList::MoleculeIdToIndex(ObservedValue_Index_t _id) const
[015f8c]209{
210 QtMoleculeItem * const item = MoleculeIdToItem(_id);
211 ASSERT(item != NULL,
212 "QtMoleculeList::MoleculeIdToIndex() - could not find item to "
[3054f4a]213 +toString(observer.getIdtoIndex(_id)));
[015f8c]214 return indexFromItem(item);
215}
[fcdf05]216
[3054f4a]217ObservedValue_Index_t QtMoleculeList::IndexToMoleculeId(const QModelIndex &_index) const
[53c1ff]218{
219 QtMoleculeItem * const item = dynamic_cast<QtMoleculeItem *>(itemFromIndex(_index));
220 if (item == NULL)
[3054f4a]221 return NULL;
[53c1ff]222 else
[69b434]223 return ItemToMoleculeId(item);
[53c1ff]224}
225
[6770fa]226void QtMoleculeList::addGroupItem(
[8ccf3b]227 QStandardItem *&mainitem,
[6770fa]228 const std::string &_molecule_formula)
229{
[53c1ff]230 QList<QStandardItem *> groupItems =
[ea2663]231 QtMoleculeItemFactory::createGroupItems(_molecule_formula);
[53c1ff]232 mainitem = groupItems.front();
[5771e6]233 FormulaItemBiMap.left.insert( std::make_pair(_molecule_formula, mainitem) );
[bff202]234 if (FormulaVisibilityCountMap.count(_molecule_formula) == 0)
235 FormulaVisibilityCountMap.insert( std::make_pair(_molecule_formula, 0));
[8ccf3b]236 invisibleRootItem()->appendRow(groupItems);
[53c1ff]237}
238
[a39d72]239QList<QStandardItem *> QtMoleculeList::createMoleculeItems(
[ca1535]240 QtObservedMolecule::ptr &_ObservedMolecule,
[a39d72]241 std::string &_molecule_formula)
[6770fa]242{
[53c1ff]243 QList<QStandardItem *> molItems =
[ea2663]244 QtMoleculeItemFactory::createMoleculeItems(_ObservedMolecule);
[53c1ff]245 QtMoleculeItem *mol_item = dynamic_cast<QtMoleculeItem *>(molItems.front());
246 ASSERT( mol_item != NULL,
[a39d72]247 "QtMoleculeList::createMoleculeItems() - item from factory was not a QtMoleculeItem?");
[3054f4a]248 MoleculeItemBiMap.left.insert( std::make_pair(_ObservedMolecule->getIndex(), mol_item) );
[a39d72]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();
[ca1535]254
255 LOG(1, "Adding " << _molecule_formula << " for "
256 << _ObservedMolecule->getMolIndex() << " to MoleculeFormulaMap.");
[3054f4a]257 MoleculeFormulaMap.insert( std::make_pair( _ObservedMolecule->getIndex(), _molecule_formula) );
[68989c]258// LOG(1, "Inserting molecule " << _molid << ": " << _molecule_formula);
[a39d72]259 return molItems;
[6770fa]260}
261
[ca1535]262std::string QtMoleculeList::addMolecule(QtObservedMolecule::ptr &_ObservedMolecule)
[6770fa]263{
264 // find group if already in list
[8ccf3b]265 QStandardItem *groupItem = NULL;
[6770fa]266
[a39d72]267 // create molecule items and obtain the molecule's formula
268 std::string molecule_formula;
[ca1535]269 QList<QStandardItem *> molItems = createMoleculeItems(_ObservedMolecule, molecule_formula);
[6770fa]270
271 // new molecule type -> create new group
[fcdf05]272 if (!isGroupItemPresent(molecule_formula)){
[6770fa]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 {
[fcdf05]286 groupItem = FormulaToGroupItem(molecule_formula);
[6770fa]287 }
288 ASSERT( groupItem != NULL,
[ca1535]289 "QtMoleculeList::addMolecule() - item with id "+toString(_ObservedMolecule->getMolIndex())
[fcdf05]290 +" has no parent?");
[a39d72]291 groupItem->appendRow(molItems);
[fcdf05]292
293 return molecule_formula;
[6770fa]294}
295
[fcdf05]296void QtMoleculeList::removeMoleculeItem(QtMoleculeItem * const _item)
[6770fa]297{
[53c1ff]298 const QModelIndex mol_index = indexFromItem(_item);
299 QStandardItem *groupitem = _item->parent();
300 const QModelIndex group_index = groupitem->index();
[fcdf05]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 "
[3054f4a]312 +toString(observer.getIdtoIndex(removeiter->second))+" in MoleculeFormulaMap.");
[fcdf05]313 LOG(1, "Removing " << removeformulaiter->second << " for "
314 << removeformulaiter->first << " from MoleculeFormulaMap.");
315 MoleculeFormulaMap.erase( removeformulaiter );
316 }
317 MoleculeItemBiMap.right.erase(removeiter);
318 }
[53c1ff]319 removeRows(mol_index.row(), 1, group_index);
320}
[6770fa]321
[2696b1]322void QtMoleculeList::checkForVisibilityChange(QStandardItem* _item)
[739ee9]323{
[2050b2]324// qDebug() << "Item changed called.";
[2696b1]325
[fcdf05]326 if (_item->index().column() == QtMoleculeItem::VISIBILITY) {
[2050b2]327// qDebug() << "visibilityItem changed: " << (_item->checkState() ? "checked" : "unchecked");
[5771e6]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 }
[2696b1]335 }
336}
[3eb91c]337
[2696b1]338void QtMoleculeList::setVisibilityForMoleculeItem(QtMoleculeItem* _item)
339{
340 const bool visible = _item->checkState();
[3054f4a]341 const ObservedValue_Index_t molid = _item->getMoleculeIndex();
[fcdf05]342 std::string molecule_formula("illegal");
343 {
[5771e6]344 const MoleculeFormulaMap_t::const_iterator formulaiter =
[fcdf05]345 MoleculeFormulaMap.find(molid);
346 ASSERT( formulaiter != MoleculeFormulaMap.end(),
347 "QtMoleculeList::setVisibilityForMoleculeItem() - formula of molecule "
[3054f4a]348 +toString(observer.getIdtoIndex(molid))+" unknown.");
[fcdf05]349 molecule_formula = formulaiter->second;
350 }
351 ASSERT( FormulaVisibilityCountMap.count(molecule_formula) != 0,
[7d0ddb]352 "QtMoleculeList::setVisibilityForMoleculeItem() - molecule with formula " +molecule_formula
353 +" is not present in FormulaVisibilityCountMap.");
[3eb91c]354
[2696b1]355 // get parent
356 QStandardItem *groupItem = _item->parent();
[fcdf05]357 QStandardItem *visgroupItem = getSpecificGroupItem(groupItem, QtMoleculeItem::VISIBILITY);
[2696b1]358 ASSERT( groupItem != NULL,
[3054f4a]359 "QtMoleculeList::setVisibilityForMoleculeItem() - item to "
360 +toString(observer.getIdtoIndex(molid))+" has not parent?");
[2696b1]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 }
[3eb91c]375
[3054f4a]376 emit moleculesVisibilityChanged(molid, visible);
[2696b1]377}
[3eb91c]378
[2696b1]379void QtMoleculeList::setVisibilityForGroupItem(QStandardItem* _item)
380{
381 // go through all children, but don't enter for groupItem once more
382 const bool visible = _item->checkState();
[fcdf05]383 QStandardItem *groupitem = getSpecificGroupItem(_item, QtMoleculeItem::NAME);
[2696b1]384 for (int i=0;i<groupitem->rowCount();++i) {
[5771e6]385 QtMoleculeItem * const molItem = dynamic_cast<QtMoleculeItem *>(
[fcdf05]386 groupitem->child(i, QtMoleculeItem::VISIBILITY));
[2696b1]387 if (molItem->checkState() != visible) {
388 molItem->setCheckState(visible ? Qt::Checked : Qt::Unchecked);
389
390 // emit signal
[3054f4a]391 emit moleculesVisibilityChanged(molItem->getMoleculeIndex(), visible);
[3eb91c]392 }
[2696b1]393 }
394 // set current number of visible children
395 const std::string molecule_formula =
[fcdf05]396 GroupItemToFormula( getSpecificGroupItem(_item, QtMoleculeItem::NAME) );
[2696b1]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;
[739ee9]403}
[2696b1]404
[07b800]405static
406MoleCuilder::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());
[b47bfc]421 }
[07b800]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
434void 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.");
[3054f4a]442 const ObservedValue_Index_t index = iter->second;
443 const moleculeId_t molid = board->getMoleculeIdToIndex(index);
[07b800]444 // change the name
[0a558d]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 }
[3054f4a]462 }
[07b800]463 }
[5771e6]464 }
[07b800]465}
[b14efe]466
[53c1ff]467int QtMoleculeList::setOccurrence(QStandardItem * const _groupitem)
468{
[5771e6]469 ASSERT( _groupitem != NULL,
470 "QtMoleculeList::setOccurrence() - group item at "+toString(_groupitem)
471 +" is NULL");
[53c1ff]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?");
[fcdf05]481 QStandardItem *occ_item = parent_item->child(index, QtMoleculeItem::OCCURRENCE);
[53c1ff]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();
[fcdf05]489 FormulaItemBiMap.left.erase(molecule_formula);
[53c1ff]490 FormulaVisibilityCountMap.erase(molecule_formula);
491 return index;
492 } else {
493 occ_item->setText(QString::number(count));
494 return -1;
495 }
496}
497
[5771e6]498void QtMoleculeList::moveItem(
499 QtMoleculeItem *_molitem,
500 const std::string &_new_formula)
[53c1ff]501{
[55443e]502 QStandardItem *groupitem = NULL;
[53c1ff]503 // use takeRows of molecule ..
[55443e]504 const QList<QStandardItem *> mol_row = _molitem->parent()->takeRow(_molitem->index().row());
[53c1ff]505 // .. and re-add where new formula fits
[5771e6]506 if (!isGroupItemPresent(_new_formula)) {
[a39d72]507 // add new group item and formula entry
[5771e6]508 addGroupItem(groupitem, _new_formula);
[fcdf05]509 } else {
[5771e6]510 groupitem = FormulaToGroupItem(_new_formula);
[53c1ff]511 }
[a39d72]512 ASSERT( groupitem != NULL,
513 "QtMoleculeList::readdItem() - failed to create a sensible new groupitem");
514 // finally add again
515 groupitem->appendRow(mol_row);
[53c1ff]516}
517
518
[5771e6]519void QtMoleculeList::formulaChanged(const QtObservedMolecule::ptr _mol)
520{
[743eab]521 // we need the id as identifier to the item
[3054f4a]522 const ObservedValue_Index_t molid = _mol->getIndex();
[743eab]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
[3054f4a]533 LOG(3, "DEBUG: Moving item to id " << observer.getIdtoIndex(molid));
[5771e6]534 const MoleculeFormulaMap_t::iterator formulaiter =
535 MoleculeFormulaMap.find(molid);
536 ASSERT( formulaiter != MoleculeFormulaMap.end(),
537 "QtMoleculeList::updateItemStates() - formula of molecule "
[3054f4a]538 +toString(observer.getIdtoIndex(molid))+" unknown.");
[5771e6]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();
[2696b1]542
[5771e6]543 // then we move the item if necessary
544 if (old_formula != new_formula) {
545 LOG(3, "DEBUG: Moving item " << molitem);
[ca1535]546 // remove from formula<->molecule bimap with old formula
[5771e6]547 LOG(4, "DEBUG: Removing " << old_formula << " for " << formulaiter->first << " from MoleculeFormulaMap.");
[ca1535]548 MoleculeFormulaMap.erase( formulaiter );
[5771e6]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());
[fcdf05]555 }
[5771e6]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?");
[53c1ff]563 }
[5771e6]564}
[53c1ff]565
[743eab]566void QtMoleculeList::atomcountChanged(const QtObservedMolecule::ptr _mol)
567{
568 // we need the id as identifier to the items
[3054f4a]569 const ObservedValue_Index_t molid = _mol->getIndex();
[743eab]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
[5771e6]579void QtMoleculeList::nameChanged(const QtObservedMolecule::ptr _mol)
580{
[b65879]581 // we need the id as identifier to the items
[3054f4a]582 const ObservedValue_Index_t molid = _mol->getIndex();
[743eab]583 LOG(3, "DEBUG: QtMoleculeList got nameChanged for id " << molid);
[b65879]584 QtMoleculeItem *const molitem = MoleculeIdToItem(molid);
585 // update items
[743eab]586 QtMoleculeItem *const nameitem = getSpecificMoleculeItem(molitem, QtMoleculeItem::NAME);
587 ASSERT(nameitem != NULL,
588 "QtMoleculeList::nameChanged() - could not item for NAME.");
589 nameitem->updateState(_mol);
[53c1ff]590}
Note: See TracBrowser for help on using the repository browser.