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