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