| 1 | /* | 
|---|
| 2 | * Project: MoleCuilder | 
|---|
| 3 | * Description: creates and alters molecular systems | 
|---|
| 4 | * Copyright (C)  2010-2012 University of Bonn. All rights reserved. | 
|---|
| 5 | * | 
|---|
| 6 | * | 
|---|
| 7 | *   This file is part of MoleCuilder. | 
|---|
| 8 | * | 
|---|
| 9 | *    MoleCuilder is free software: you can redistribute it and/or modify | 
|---|
| 10 | *    it under the terms of the GNU General Public License as published by | 
|---|
| 11 | *    the Free Software Foundation, either version 2 of the License, or | 
|---|
| 12 | *    (at your option) any later version. | 
|---|
| 13 | * | 
|---|
| 14 | *    MoleCuilder is distributed in the hope that it will be useful, | 
|---|
| 15 | *    but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 16 | *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
| 17 | *    GNU General Public License for more details. | 
|---|
| 18 | * | 
|---|
| 19 | *    You should have received a copy of the GNU General Public License | 
|---|
| 20 | *    along with MoleCuilder.  If not, see <http://www.gnu.org/licenses/>. | 
|---|
| 21 | */ | 
|---|
| 22 |  | 
|---|
| 23 | /* | 
|---|
| 24 | * QtMoleculeList.cpp | 
|---|
| 25 | * | 
|---|
| 26 | *  Created on: Jan 21, 2010 | 
|---|
| 27 | *      Author: crueger | 
|---|
| 28 | */ | 
|---|
| 29 |  | 
|---|
| 30 | // include config.h | 
|---|
| 31 | #ifdef HAVE_CONFIG_H | 
|---|
| 32 | #include <config.h> | 
|---|
| 33 | #endif | 
|---|
| 34 |  | 
|---|
| 35 | #include "QtMoleculeList.hpp" | 
|---|
| 36 |  | 
|---|
| 37 | #include <QModelIndex> | 
|---|
| 38 | #include <QDebug> | 
|---|
| 39 |  | 
|---|
| 40 | #include "UIElements/Views/Qt4/MoleculeList/QtMoleculeItem.hpp" | 
|---|
| 41 | #include "UIElements/Views/Qt4/MoleculeList/QtMoleculeItemFactory.hpp" | 
|---|
| 42 | #include "UIElements/Qt4/InstanceBoard/QtObservedInstanceBoard.hpp" | 
|---|
| 43 |  | 
|---|
| 44 | #include <boost/bind.hpp> | 
|---|
| 45 | #include <boost/thread/locks.hpp> | 
|---|
| 46 | #include <iostream> | 
|---|
| 47 |  | 
|---|
| 48 | //#include "CodePatterns/MemDebug.hpp" | 
|---|
| 49 |  | 
|---|
| 50 | #include "CodePatterns/Log.hpp" | 
|---|
| 51 | #include "CodePatterns/Observer/Notification.hpp" | 
|---|
| 52 |  | 
|---|
| 53 | #include "Atom/atom.hpp" | 
|---|
| 54 | #include "Actions/MoleculeAction/ChangeNameAction.hpp" | 
|---|
| 55 | #include "Actions/SelectionAction/Molecules/PopMoleculesAction.hpp" | 
|---|
| 56 | #include "Actions/SelectionAction/Molecules/PushMoleculesAction.hpp" | 
|---|
| 57 | #include "Actions/SelectionAction/Molecules/MoleculeByIdAction.hpp" | 
|---|
| 58 | #include "Actions/ActionQueue.hpp" | 
|---|
| 59 | #include "Actions/ActionSequence.hpp" | 
|---|
| 60 | #include "Actions/ActionTrait.hpp" | 
|---|
| 61 | #include "Actions/MakroAction.hpp" | 
|---|
| 62 | #include "Descriptors/MoleculeIdDescriptor.hpp" | 
|---|
| 63 | #include "Formula.hpp" | 
|---|
| 64 | #include "molecule.hpp" | 
|---|
| 65 |  | 
|---|
| 66 | using namespace std; | 
|---|
| 67 |  | 
|---|
| 68 | QtMoleculeList::QtMoleculeList( | 
|---|
| 69 | QtObservedInstanceBoard *_board, | 
|---|
| 70 | QObject *_parent) : | 
|---|
| 71 | QStandardItemModel(_parent), | 
|---|
| 72 | board(_board), | 
|---|
| 73 | observer(_board), | 
|---|
| 74 | nameIsChanged(false) | 
|---|
| 75 | { | 
|---|
| 76 | setColumnCount(QtMoleculeItemFactory::COLUMNCOUNT); | 
|---|
| 77 |  | 
|---|
| 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))); | 
|---|
| 84 | connect(&observer, SIGNAL(MoleculeRemoved(ObservedValue_Index_t)), | 
|---|
| 85 | this, SLOT(moleculeRemoved(ObservedValue_Index_t))); | 
|---|
| 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))); | 
|---|
| 90 | connect(&observer, SIGNAL(AtomCountChanged(const QtObservedMolecule::ptr)), | 
|---|
| 91 | this, SLOT(atomcountChanged(const QtObservedMolecule::ptr))); | 
|---|
| 92 | } | 
|---|
| 93 |  | 
|---|
| 94 | QtMoleculeList::~QtMoleculeList() | 
|---|
| 95 | {} | 
|---|
| 96 |  | 
|---|
| 97 | QVariant QtMoleculeList::headerData(int section, Qt::Orientation orientation, int role) const | 
|---|
| 98 | { | 
|---|
| 99 | if (role == Qt::DisplayRole) { | 
|---|
| 100 | if (orientation == Qt::Horizontal) { | 
|---|
| 101 | if (section < QtMoleculeItem::COLUMNTYPES_MAX) | 
|---|
| 102 | return QString(QtMoleculeItemFactory::COLUMNNAMES[section]); | 
|---|
| 103 | } | 
|---|
| 104 | } | 
|---|
| 105 | return QVariant(); | 
|---|
| 106 | } | 
|---|
| 107 |  | 
|---|
| 108 | void QtMoleculeList::moleculeInserted(QtObservedMolecule::ptr _mol) | 
|---|
| 109 | { | 
|---|
| 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)); | 
|---|
| 115 | } | 
|---|
| 116 |  | 
|---|
| 117 | void QtMoleculeList::moleculeRemoved(ObservedValue_Index_t _id) | 
|---|
| 118 | { | 
|---|
| 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."); | 
|---|
| 128 | QStandardItem * const groupitem = FormulaToGroupItem(formula); | 
|---|
| 129 | // first remove molitem to reduce count | 
|---|
| 130 | removeMoleculeItem(item); | 
|---|
| 131 | const int removeindex = setOccurrence(groupitem); | 
|---|
| 132 | if (removeindex != -1) { | 
|---|
| 133 | LOG(1, "Removing row of group item to " << formula); | 
|---|
| 134 | removeRows(removeindex, 1, invisibleRootItem()->index()); | 
|---|
| 135 | } | 
|---|
| 136 | } | 
|---|
| 137 | } | 
|---|
| 138 |  | 
|---|
| 139 | bool QtMoleculeList::isMoleculeItemPresent(ObservedValue_Index_t _molid) const | 
|---|
| 140 | { | 
|---|
| 141 | MoleculeItemBiMap_t::left_const_iterator iter = | 
|---|
| 142 | MoleculeItemBiMap.left.find(_molid); | 
|---|
| 143 | return ( iter != MoleculeItemBiMap.left.end()); | 
|---|
| 144 | } | 
|---|
| 145 |  | 
|---|
| 146 | QtMoleculeItem * QtMoleculeList::MoleculeIdToItem(ObservedValue_Index_t _molid) const | 
|---|
| 147 | { | 
|---|
| 148 | MoleculeItemBiMap_t::left_const_iterator iter = | 
|---|
| 149 | MoleculeItemBiMap.left.find(_molid); | 
|---|
| 150 | ASSERT( iter != MoleculeItemBiMap.left.end(), | 
|---|
| 151 | "QtMoleculeList::MoleculeIdToItem() - could not find item to id " | 
|---|
| 152 | +toString(_molid)); | 
|---|
| 153 | return iter->second; | 
|---|
| 154 | } | 
|---|
| 155 |  | 
|---|
| 156 | ObservedValue_Index_t QtMoleculeList::ItemToMoleculeId(const QtMoleculeItem * const _item) const | 
|---|
| 157 | { | 
|---|
| 158 | const MoleculeItemBiMap_t::right_const_iterator iter = | 
|---|
| 159 | MoleculeItemBiMap.right.find(const_cast<QtMoleculeItem * const>(_item)); | 
|---|
| 160 | if (iter != MoleculeItemBiMap.right.end()) | 
|---|
| 161 | return iter->second; | 
|---|
| 162 | else | 
|---|
| 163 | return NULL; | 
|---|
| 164 | } | 
|---|
| 165 |  | 
|---|
| 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 |  | 
|---|
| 211 | const QModelIndex QtMoleculeList::MoleculeIdToIndex(ObservedValue_Index_t _id) const | 
|---|
| 212 | { | 
|---|
| 213 | QtMoleculeItem * const item = MoleculeIdToItem(_id); | 
|---|
| 214 | ASSERT(item != NULL, | 
|---|
| 215 | "QtMoleculeList::MoleculeIdToIndex() - could not find item to " | 
|---|
| 216 | +toString(_id)); | 
|---|
| 217 | return indexFromItem(item); | 
|---|
| 218 | } | 
|---|
| 219 |  | 
|---|
| 220 | ObservedValue_Index_t QtMoleculeList::IndexToMoleculeId(const QModelIndex &_index) const | 
|---|
| 221 | { | 
|---|
| 222 | QtMoleculeItem * const item = dynamic_cast<QtMoleculeItem *>(itemFromIndex(_index)); | 
|---|
| 223 | if (item == NULL) | 
|---|
| 224 | return NULL; | 
|---|
| 225 | else | 
|---|
| 226 | return ItemToMoleculeId(item); | 
|---|
| 227 | } | 
|---|
| 228 |  | 
|---|
| 229 | void QtMoleculeList::addGroupItem( | 
|---|
| 230 | QStandardItem *&mainitem, | 
|---|
| 231 | const std::string &_molecule_formula) | 
|---|
| 232 | { | 
|---|
| 233 | QList<QStandardItem *> groupItems = | 
|---|
| 234 | QtMoleculeItemFactory::createGroupItems(_molecule_formula); | 
|---|
| 235 | mainitem = groupItems.front(); | 
|---|
| 236 | FormulaItemBiMap.left.insert( std::make_pair(_molecule_formula, mainitem) ); | 
|---|
| 237 | if (FormulaVisibilityCountMap.count(_molecule_formula) == 0) | 
|---|
| 238 | FormulaVisibilityCountMap.insert( std::make_pair(_molecule_formula, 0)); | 
|---|
| 239 | invisibleRootItem()->appendRow(groupItems); | 
|---|
| 240 | } | 
|---|
| 241 |  | 
|---|
| 242 | QList<QStandardItem *> QtMoleculeList::createMoleculeItems( | 
|---|
| 243 | QtObservedMolecule::ptr &_ObservedMolecule, | 
|---|
| 244 | std::string &_molecule_formula) | 
|---|
| 245 | { | 
|---|
| 246 | QList<QStandardItem *> molItems = | 
|---|
| 247 | QtMoleculeItemFactory::createMoleculeItems(_ObservedMolecule); | 
|---|
| 248 | QtMoleculeItem *mol_item = dynamic_cast<QtMoleculeItem *>(molItems.front()); | 
|---|
| 249 | ASSERT( mol_item != NULL, | 
|---|
| 250 | "QtMoleculeList::createMoleculeItems() - item from factory was not a QtMoleculeItem?"); | 
|---|
| 251 | MoleculeItemBiMap.left.insert( std::make_pair(_ObservedMolecule->getIndex(), mol_item) ); | 
|---|
| 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(); | 
|---|
| 257 |  | 
|---|
| 258 | LOG(1, "Adding " << _molecule_formula << " for " | 
|---|
| 259 | << _ObservedMolecule->getMolIndex() << " to MoleculeFormulaMap."); | 
|---|
| 260 | MoleculeFormulaMap.insert( std::make_pair( _ObservedMolecule->getIndex(), _molecule_formula) ); | 
|---|
| 261 | //  LOG(1, "Inserting molecule " << _molid << ": " << _molecule_formula); | 
|---|
| 262 | return molItems; | 
|---|
| 263 | } | 
|---|
| 264 |  | 
|---|
| 265 | std::string QtMoleculeList::addMolecule(QtObservedMolecule::ptr &_ObservedMolecule) | 
|---|
| 266 | { | 
|---|
| 267 | // find group if already in list | 
|---|
| 268 | QStandardItem *groupItem = NULL; | 
|---|
| 269 |  | 
|---|
| 270 | // create molecule items and obtain the molecule's formula | 
|---|
| 271 | std::string molecule_formula; | 
|---|
| 272 | QList<QStandardItem *> molItems = createMoleculeItems(_ObservedMolecule, molecule_formula); | 
|---|
| 273 |  | 
|---|
| 274 | // new molecule type -> create new group | 
|---|
| 275 | if (!isGroupItemPresent(molecule_formula)){ | 
|---|
| 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 { | 
|---|
| 289 | groupItem = FormulaToGroupItem(molecule_formula); | 
|---|
| 290 | } | 
|---|
| 291 | ASSERT( groupItem != NULL, | 
|---|
| 292 | "QtMoleculeList::addMolecule() - item with id "+toString(_ObservedMolecule->getMolIndex()) | 
|---|
| 293 | +" has no parent?"); | 
|---|
| 294 | groupItem->appendRow(molItems); | 
|---|
| 295 |  | 
|---|
| 296 | return molecule_formula; | 
|---|
| 297 | } | 
|---|
| 298 |  | 
|---|
| 299 | void QtMoleculeList::removeMoleculeItem(QtMoleculeItem * const _item) | 
|---|
| 300 | { | 
|---|
| 301 | const QModelIndex mol_index = indexFromItem(_item); | 
|---|
| 302 | QStandardItem *groupitem = _item->parent(); | 
|---|
| 303 | const QModelIndex group_index = groupitem->index(); | 
|---|
| 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 " | 
|---|
| 315 | +toString(removeiter->second)+" in MoleculeFormulaMap."); | 
|---|
| 316 | LOG(1, "Removing " << removeformulaiter->second << " for " | 
|---|
| 317 | << removeformulaiter->first << " from MoleculeFormulaMap."); | 
|---|
| 318 | MoleculeFormulaMap.erase( removeformulaiter ); | 
|---|
| 319 | } | 
|---|
| 320 | MoleculeItemBiMap.right.erase(removeiter); | 
|---|
| 321 | } | 
|---|
| 322 | removeRows(mol_index.row(), 1, group_index); | 
|---|
| 323 | } | 
|---|
| 324 |  | 
|---|
| 325 | void QtMoleculeList::checkForVisibilityChange(QStandardItem* _item) | 
|---|
| 326 | { | 
|---|
| 327 | //  qDebug() << "Item changed called."; | 
|---|
| 328 |  | 
|---|
| 329 | if (_item->index().column() == QtMoleculeItem::VISIBILITY) { | 
|---|
| 330 | //    qDebug() << "visibilityItem changed: " << (_item->checkState() ? "checked" : "unchecked"); | 
|---|
| 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 | } | 
|---|
| 338 | } | 
|---|
| 339 | } | 
|---|
| 340 |  | 
|---|
| 341 | void QtMoleculeList::setVisibilityForMoleculeItem(QtMoleculeItem* _item) | 
|---|
| 342 | { | 
|---|
| 343 | const bool visible = _item->checkState(); | 
|---|
| 344 | const ObservedValue_Index_t molid = _item->getMoleculeIndex(); | 
|---|
| 345 | std::string molecule_formula("illegal"); | 
|---|
| 346 | { | 
|---|
| 347 | const MoleculeFormulaMap_t::const_iterator formulaiter = | 
|---|
| 348 | MoleculeFormulaMap.find(molid); | 
|---|
| 349 | ASSERT( formulaiter != MoleculeFormulaMap.end(), | 
|---|
| 350 | "QtMoleculeList::setVisibilityForMoleculeItem() - formula of molecule " | 
|---|
| 351 | +toString(molid)+" unknown."); | 
|---|
| 352 | molecule_formula = formulaiter->second; | 
|---|
| 353 | } | 
|---|
| 354 | ASSERT( FormulaVisibilityCountMap.count(molecule_formula) != 0, | 
|---|
| 355 | "QtMoleculeList::setVisibilityForMoleculeItem() - molecule with formula " +molecule_formula | 
|---|
| 356 | +" is not present in FormulaVisibilityCountMap."); | 
|---|
| 357 |  | 
|---|
| 358 | // get parent | 
|---|
| 359 | QStandardItem *groupItem = _item->parent(); | 
|---|
| 360 | QStandardItem *visgroupItem = getSpecificGroupItem(groupItem, QtMoleculeItem::VISIBILITY); | 
|---|
| 361 | ASSERT( groupItem != NULL, | 
|---|
| 362 | "QtMoleculeList::setVisibilityForMoleculeItem() - item to " | 
|---|
| 363 | +toString(molid)+" has not parent?"); | 
|---|
| 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 | } | 
|---|
| 378 |  | 
|---|
| 379 | emit moleculesVisibilityChanged(molid, visible); | 
|---|
| 380 | } | 
|---|
| 381 |  | 
|---|
| 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(); | 
|---|
| 386 | QStandardItem *groupitem = getSpecificGroupItem(_item, QtMoleculeItem::NAME); | 
|---|
| 387 | for (int i=0;i<groupitem->rowCount();++i) { | 
|---|
| 388 | QtMoleculeItem * const molItem = dynamic_cast<QtMoleculeItem *>( | 
|---|
| 389 | groupitem->child(i, QtMoleculeItem::VISIBILITY)); | 
|---|
| 390 | if (molItem->checkState() != visible) { | 
|---|
| 391 | molItem->setCheckState(visible ? Qt::Checked : Qt::Unchecked); | 
|---|
| 392 |  | 
|---|
| 393 | // emit signal | 
|---|
| 394 | emit moleculesVisibilityChanged(molItem->getMoleculeIndex(), visible); | 
|---|
| 395 | } | 
|---|
| 396 | } | 
|---|
| 397 | // set current number of visible children | 
|---|
| 398 | const std::string molecule_formula = | 
|---|
| 399 | GroupItemToFormula( getSpecificGroupItem(_item, QtMoleculeItem::NAME) ); | 
|---|
| 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; | 
|---|
| 406 | } | 
|---|
| 407 |  | 
|---|
| 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()); | 
|---|
| 424 | } | 
|---|
| 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 | { | 
|---|
| 439 | // check whether name change came from outside | 
|---|
| 440 | if (nameIsChanged) | 
|---|
| 441 | return; | 
|---|
| 442 |  | 
|---|
| 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."); | 
|---|
| 449 | const ObservedValue_Index_t index = iter->second; | 
|---|
| 450 | const moleculeId_t molid = board->getMoleculeIdToIndex(index); | 
|---|
| 451 | // change the name | 
|---|
| 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) { | 
|---|
| 465 | LOG(2, "WARNING: Not setting changing molecule " << mol->getMolName() | 
|---|
| 466 | << " to empty."); | 
|---|
| 467 | QtMoleculeItem * const molitem = assert_cast<QtMoleculeItem *>(item); | 
|---|
| 468 | molitem->updateState(mol); | 
|---|
| 469 | } | 
|---|
| 470 | } | 
|---|
| 471 | } | 
|---|
| 472 | } | 
|---|
| 473 | } | 
|---|
| 474 |  | 
|---|
| 475 | int QtMoleculeList::setOccurrence(QStandardItem * const _groupitem) | 
|---|
| 476 | { | 
|---|
| 477 | ASSERT( _groupitem != NULL, | 
|---|
| 478 | "QtMoleculeList::setOccurrence() - group item at "+toString(_groupitem) | 
|---|
| 479 | +" is NULL"); | 
|---|
| 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?"); | 
|---|
| 489 | QStandardItem *occ_item = parent_item->child(index, QtMoleculeItem::OCCURRENCE); | 
|---|
| 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(); | 
|---|
| 497 | FormulaItemBiMap.left.erase(molecule_formula); | 
|---|
| 498 | FormulaVisibilityCountMap.erase(molecule_formula); | 
|---|
| 499 | return index; | 
|---|
| 500 | } else { | 
|---|
| 501 | occ_item->setText(QString::number(count)); | 
|---|
| 502 | return -1; | 
|---|
| 503 | } | 
|---|
| 504 | } | 
|---|
| 505 |  | 
|---|
| 506 | void QtMoleculeList::moveItem( | 
|---|
| 507 | QtMoleculeItem *_molitem, | 
|---|
| 508 | const std::string &_new_formula) | 
|---|
| 509 | { | 
|---|
| 510 | QStandardItem *groupitem = NULL; | 
|---|
| 511 | // prohibit starting of selection actions | 
|---|
| 512 | emit MayNotStartSelections(); | 
|---|
| 513 | // use takeRows of molecule .. | 
|---|
| 514 | const QList<QStandardItem *> mol_row = _molitem->parent()->takeRow(_molitem->index().row()); | 
|---|
| 515 | // ..  and re-add where new formula fits | 
|---|
| 516 | if (!isGroupItemPresent(_new_formula)) { | 
|---|
| 517 | // add new group item and formula entry | 
|---|
| 518 | addGroupItem(groupitem, _new_formula); | 
|---|
| 519 | } else { | 
|---|
| 520 | groupitem = FormulaToGroupItem(_new_formula); | 
|---|
| 521 | } | 
|---|
| 522 | ASSERT( groupitem != NULL, | 
|---|
| 523 | "QtMoleculeList::readdItem() - failed to create a sensible new groupitem"); | 
|---|
| 524 | // finally add again | 
|---|
| 525 | groupitem->appendRow(mol_row); | 
|---|
| 526 | emit MayStartSelections(); | 
|---|
| 527 | } | 
|---|
| 528 |  | 
|---|
| 529 |  | 
|---|
| 530 | void QtMoleculeList::formulaChanged(const QtObservedMolecule::ptr _mol) | 
|---|
| 531 | { | 
|---|
| 532 | // we need the id as identifier to the item | 
|---|
| 533 | const ObservedValue_Index_t molid = _mol->getIndex(); | 
|---|
| 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 |  | 
|---|
| 544 | LOG(3, "DEBUG: Moving item to id " << molid); | 
|---|
| 545 | const MoleculeFormulaMap_t::iterator formulaiter = | 
|---|
| 546 | MoleculeFormulaMap.find(molid); | 
|---|
| 547 | ASSERT( formulaiter != MoleculeFormulaMap.end(), | 
|---|
| 548 | "QtMoleculeList::updateItemStates() - formula of molecule " | 
|---|
| 549 | +toString(molid)+" unknown."); | 
|---|
| 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(); | 
|---|
| 553 |  | 
|---|
| 554 | // then we move the item if necessary | 
|---|
| 555 | if (old_formula != new_formula) { | 
|---|
| 556 | LOG(3, "DEBUG: Moving item " << molitem); | 
|---|
| 557 | // remove from formula<->molecule bimap with old formula | 
|---|
| 558 | LOG(4, "DEBUG: Removing " << old_formula << " for " << formulaiter->first << " from MoleculeFormulaMap."); | 
|---|
| 559 | MoleculeFormulaMap.erase( formulaiter ); | 
|---|
| 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()); | 
|---|
| 566 | } | 
|---|
| 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?"); | 
|---|
| 574 | } | 
|---|
| 575 | } | 
|---|
| 576 |  | 
|---|
| 577 | void QtMoleculeList::atomcountChanged(const QtObservedMolecule::ptr _mol) | 
|---|
| 578 | { | 
|---|
| 579 | // we need the id as identifier to the items | 
|---|
| 580 | const ObservedValue_Index_t molid = _mol->getIndex(); | 
|---|
| 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 |  | 
|---|
| 590 | void QtMoleculeList::nameChanged(const QtObservedMolecule::ptr _mol) | 
|---|
| 591 | { | 
|---|
| 592 | nameIsChanged = true; | 
|---|
| 593 | // we need the id as identifier to the items | 
|---|
| 594 | const ObservedValue_Index_t molid = _mol->getIndex(); | 
|---|
| 595 | LOG(3, "DEBUG: QtMoleculeList got nameChanged for id " << molid); | 
|---|
| 596 | QtMoleculeItem *const molitem = MoleculeIdToItem(molid); | 
|---|
| 597 | // update items | 
|---|
| 598 | QtMoleculeItem *const nameitem = getSpecificMoleculeItem(molitem, QtMoleculeItem::NAME); | 
|---|
| 599 | ASSERT(nameitem != NULL, | 
|---|
| 600 | "QtMoleculeList::nameChanged() - could not item for NAME."); | 
|---|
| 601 | nameitem->updateState(_mol); | 
|---|
| 602 | nameIsChanged = false; | 
|---|
| 603 | } | 
|---|