[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"
|
---|
| 42 |
|
---|
| 43 | #include <boost/bind.hpp>
|
---|
[fcdf05] | 44 | #include <boost/thread/locks.hpp>
|
---|
[b47bfc] | 45 | #include <iostream>
|
---|
| 46 |
|
---|
[ad011c] | 47 | #include "CodePatterns/MemDebug.hpp"
|
---|
[bbbad5] | 48 |
|
---|
[53c1ff] | 49 | #include "CodePatterns/Log.hpp"
|
---|
[6770fa] | 50 | #include "CodePatterns/Observer/Notification.hpp"
|
---|
| 51 |
|
---|
[6f0841] | 52 | #include "Atom/atom.hpp"
|
---|
[d2dbb5d] | 53 | #include "Descriptors/MoleculeIdDescriptor.hpp"
|
---|
[26cf17] | 54 | #include "Formula.hpp"
|
---|
[b47bfc] | 55 | #include "molecule.hpp"
|
---|
[42127c] | 56 | #include "MoleculeListClass.hpp"
|
---|
[b47bfc] | 57 |
|
---|
| 58 | using namespace std;
|
---|
| 59 |
|
---|
[53c1ff] | 60 | const unsigned int QtMoleculeList::update_times_per_second = 20;
|
---|
[b47bfc] | 61 |
|
---|
[8ccf3b] | 62 | QtMoleculeList::QtMoleculeList() :
|
---|
[53c1ff] | 63 | Observer("QtMoleculeList"),
|
---|
| 64 | ChangingChildrensVisibility(false),
|
---|
| 65 | update_timer(NULL),
|
---|
[cc2976] | 66 | callback_DirtyItems(boost::bind(&QtMoleculeList::informDirtyState, this, _1, _2, _3)),
|
---|
| 67 | callback_subjectKilledItems(boost::bind(&QtMoleculeList::receiveSubjectKilled, this, _1))
|
---|
[b47bfc] | 68 | {
|
---|
[53c1ff] | 69 | setColumnCount(QtMoleculeItemFactory::COLUMNCOUNT);
|
---|
[b47bfc] | 70 |
|
---|
[3eb91c] | 71 | World::getInstance().signOn(this, World::MoleculeInserted);
|
---|
[41815a3] | 72 |
|
---|
[a39006] | 73 | refill();
|
---|
[b47bfc] | 74 |
|
---|
[8ccf3b] | 75 | // qRegisterMetaType<QItemSelection>("QItemSelection");
|
---|
[79b59b] | 76 | //connect(this,SIGNAL(cellChanged(int,int)),this,SLOT(moleculeChanged(int,int)));
|
---|
[8ccf3b] | 77 | // connect(selectionModel(),SIGNAL(selectionChanged(QItemSelection, QItemSelection)),this,SLOT(rowsSelected(QItemSelection, QItemSelection)));
|
---|
[fcdf05] | 78 | connect(this, SIGNAL(itemChanged(QStandardItem*)), this, SLOT(checkForVisibilityChange(QStandardItem*)), Qt::DirectConnection);
|
---|
[b47bfc] | 79 | }
|
---|
| 80 |
|
---|
[0eb7bf3] | 81 | QtMoleculeList::~QtMoleculeList()
|
---|
[b47bfc] | 82 | {
|
---|
[3eb91c] | 83 | World::getInstance().signOff(this, World::MoleculeInserted);
|
---|
[b47bfc] | 84 | }
|
---|
| 85 |
|
---|
[1c3390] | 86 | QVariant QtMoleculeList::headerData(int section, Qt::Orientation orientation, int role) const
|
---|
| 87 | {
|
---|
| 88 | if (role == Qt::DisplayRole) {
|
---|
| 89 | if (orientation == Qt::Horizontal) {
|
---|
[fcdf05] | 90 | if (section < QtMoleculeItem::COLUMNTYPES_MAX)
|
---|
[53c1ff] | 91 | return QString(QtMoleculeItemFactory::COLUMNNAMES[section]);
|
---|
[1c3390] | 92 | }
|
---|
| 93 | }
|
---|
| 94 | return QVariant();
|
---|
| 95 | }
|
---|
| 96 |
|
---|
[0eb7bf3] | 97 | void QtMoleculeList::update(Observable *publisher) {
|
---|
[3eb91c] | 98 | ASSERT(0,
|
---|
| 99 | "QtMoleculeList::update() - we did not sign up for any global updates.");
|
---|
| 100 | }
|
---|
| 101 |
|
---|
[fcdf05] | 102 | bool QtMoleculeList::isMoleculeItemPresent(const moleculeId_t _molid) const
|
---|
| 103 | {
|
---|
| 104 | boost::recursive_mutex::scoped_lock lock(map_mutex);
|
---|
| 105 | MoleculeItemBiMap_t::left_const_iterator iter =
|
---|
| 106 | MoleculeItemBiMap.left.find(_molid);
|
---|
| 107 | return ( iter != MoleculeItemBiMap.left.end());
|
---|
| 108 | }
|
---|
| 109 |
|
---|
[69b434] | 110 | QtMoleculeItem * QtMoleculeList::MoleculeIdToItem(const moleculeId_t _molid) const
|
---|
[8ccf3b] | 111 | {
|
---|
[fcdf05] | 112 | boost::recursive_mutex::scoped_lock lock(map_mutex);
|
---|
[8ccf3b] | 113 | MoleculeItemBiMap_t::left_const_iterator iter =
|
---|
[69b434] | 114 | MoleculeItemBiMap.left.find(_molid);
|
---|
[fcdf05] | 115 | ASSERT( iter != MoleculeItemBiMap.left.end(),
|
---|
| 116 | "QtMoleculeList::MoleculeIdToItem() - could not find item to id "
|
---|
| 117 | +toString(_molid));
|
---|
| 118 | return iter->second;
|
---|
[8ccf3b] | 119 | }
|
---|
| 120 |
|
---|
[69b434] | 121 | const moleculeId_t QtMoleculeList::ItemToMoleculeId(const QtMoleculeItem * const _item) const
|
---|
[53c1ff] | 122 | {
|
---|
[fcdf05] | 123 | boost::recursive_mutex::scoped_lock lock(map_mutex);
|
---|
[53c1ff] | 124 | const MoleculeItemBiMap_t::right_const_iterator iter =
|
---|
| 125 | MoleculeItemBiMap.right.find(const_cast<QtMoleculeItem * const>(_item));
|
---|
[d2dbb5d] | 126 | if (iter != MoleculeItemBiMap.right.end())
|
---|
| 127 | return iter->second;
|
---|
| 128 | else
|
---|
[69b434] | 129 | return -1;
|
---|
[53c1ff] | 130 | }
|
---|
| 131 |
|
---|
[fcdf05] | 132 | QtMoleculeItem * QtMoleculeList::getSpecificMoleculeItem(
|
---|
| 133 | const QtMoleculeItem * const _item,
|
---|
| 134 | const enum QtMoleculeItem::COLUMNTYPES _type) const
|
---|
| 135 | {
|
---|
| 136 | QStandardItem *parent_item = _item->parent();
|
---|
| 137 | ASSERT( parent_item != NULL,
|
---|
| 138 | "QtMoleculeList::getSpecificMoleculeItem() - parent of molecule item is NULL");
|
---|
| 139 | return static_cast<QtMoleculeItem *>(parent_item->child(_item->index().row(), _type));
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | bool QtMoleculeList::isGroupItemPresent(const std::string &_formula) const
|
---|
| 143 | {
|
---|
| 144 | boost::recursive_mutex::scoped_lock lock(map_mutex);
|
---|
| 145 | FormulaTreeItemBiMap_t::left_const_iterator iter =
|
---|
| 146 | FormulaItemBiMap.left.find(_formula);
|
---|
| 147 | return ( iter != FormulaItemBiMap.left.end());
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | QStandardItem * QtMoleculeList::FormulaToGroupItem(const std::string &_formula) const
|
---|
| 151 | {
|
---|
| 152 | boost::recursive_mutex::scoped_lock lock(map_mutex);
|
---|
| 153 | FormulaTreeItemBiMap_t::left_const_iterator iter =
|
---|
| 154 | FormulaItemBiMap.left.find(_formula);
|
---|
| 155 | ASSERT( iter != FormulaItemBiMap.left.end(),
|
---|
| 156 | "QtMoleculeList::FormulaToGroupItem() - could not find item to formula "
|
---|
| 157 | +toString(_formula));
|
---|
| 158 | return iter->second;
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 | const std::string& QtMoleculeList::GroupItemToFormula(const QStandardItem * const _item) const
|
---|
| 162 | {
|
---|
| 163 | boost::recursive_mutex::scoped_lock lock(map_mutex);
|
---|
| 164 | static std::string emptystring;
|
---|
| 165 | const FormulaTreeItemBiMap_t::right_const_iterator iter =
|
---|
| 166 | FormulaItemBiMap.right.find(const_cast<QStandardItem * const>(_item));
|
---|
| 167 | if (iter != FormulaItemBiMap.right.end())
|
---|
| 168 | return iter->second;
|
---|
| 169 | else
|
---|
| 170 | return emptystring;
|
---|
| 171 | }
|
---|
| 172 |
|
---|
| 173 | QStandardItem * QtMoleculeList::getSpecificGroupItem(
|
---|
| 174 | const QStandardItem * const _item,
|
---|
| 175 | const enum QtMoleculeItem::COLUMNTYPES _type) const
|
---|
| 176 | {
|
---|
| 177 | return invisibleRootItem()->child(_item->index().row(), _type);
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 |
|
---|
[69b434] | 181 | const moleculeId_t QtMoleculeList::IndexToMoleculeId(const QModelIndex &_index) const
|
---|
[53c1ff] | 182 | {
|
---|
[fcdf05] | 183 | boost::recursive_mutex::scoped_lock lock(refill_mutex);
|
---|
[53c1ff] | 184 | QtMoleculeItem * const item = dynamic_cast<QtMoleculeItem *>(itemFromIndex(_index));
|
---|
| 185 | if (item == NULL)
|
---|
[69b434] | 186 | return -1;
|
---|
[53c1ff] | 187 | else
|
---|
[69b434] | 188 | return ItemToMoleculeId(item);
|
---|
[53c1ff] | 189 | }
|
---|
| 190 |
|
---|
[cc2976] | 191 | void QtMoleculeList::receiveSubjectKilled(const moleculeId_t _id)
|
---|
| 192 | {
|
---|
| 193 | boost::recursive_mutex::scoped_lock lock(map_mutex);
|
---|
| 194 | KilledItemsPerMolecule_t::iterator iter = KilledItemsPerMolecule.find(_id);
|
---|
| 195 | if (iter == KilledItemsPerMolecule.end())
|
---|
| 196 | KilledItemsPerMolecule.insert( std::make_pair(_id, 1));
|
---|
| 197 | else
|
---|
| 198 | ++(iter->second);
|
---|
| 199 | if (iter->second == QtMoleculeItem::COLUMNTYPES_MAX) {
|
---|
| 200 | boost::recursive_mutex::scoped_lock lock(listAccessing_mutex);
|
---|
| 201 | removedMolecules.push_back( _id );
|
---|
| 202 | }
|
---|
| 203 | }
|
---|
| 204 |
|
---|
[6770fa] | 205 | void QtMoleculeList::recieveNotification(Observable *publisher, Notification_ptr notification)
|
---|
| 206 | {
|
---|
| 207 | if (dynamic_cast<World *>(publisher) != NULL) {
|
---|
| 208 | switch (notification->getChannelNo()) {
|
---|
| 209 | case World::MoleculeInserted:
|
---|
| 210 | {
|
---|
[fcdf05] | 211 | boost::recursive_mutex::scoped_lock lock(listAccessing_mutex);
|
---|
[54bdaa] | 212 | const moleculeId_t molid = const_cast<const World &>(World::getInstance()).lastChangedMolId();
|
---|
[fcdf05] | 213 | if (molid != (unsigned int )-1)
|
---|
| 214 | newMolecules.push_back( molid );
|
---|
[6770fa] | 215 | break;
|
---|
| 216 | }
|
---|
| 217 | default:
|
---|
| 218 | ASSERT(0, "QtMoleculeList::recieveNotification() - cannot get here, not subscribed to channel "
|
---|
| 219 | +toString(notification->getChannelNo()));
|
---|
| 220 | break;
|
---|
| 221 | }
|
---|
| 222 | }
|
---|
[a39006] | 223 | }
|
---|
| 224 |
|
---|
[6770fa] | 225 | void QtMoleculeList::addGroupItem(
|
---|
[8ccf3b] | 226 | QStandardItem *&mainitem,
|
---|
[6770fa] | 227 | const std::string &_molecule_formula)
|
---|
| 228 | {
|
---|
[53c1ff] | 229 | QList<QStandardItem *> groupItems =
|
---|
| 230 | QtMoleculeItemFactory::getInstance().createGroupItems(_molecule_formula);
|
---|
| 231 | mainitem = groupItems.front();
|
---|
[fcdf05] | 232 | {
|
---|
| 233 | boost::recursive_mutex::scoped_lock lock(map_mutex);
|
---|
| 234 | FormulaItemBiMap.left.insert( std::make_pair(_molecule_formula, mainitem) );
|
---|
| 235 | }
|
---|
[8ccf3b] | 236 | invisibleRootItem()->appendRow(groupItems);
|
---|
[53c1ff] | 237 | }
|
---|
| 238 |
|
---|
[6770fa] | 239 | void QtMoleculeList::addMoleculeItem(
|
---|
[8ccf3b] | 240 | QStandardItem *_groupitem,
|
---|
[69b434] | 241 | const moleculeId_t _molid,
|
---|
[6770fa] | 242 | const std::string &_molecule_formula)
|
---|
| 243 | {
|
---|
[53c1ff] | 244 | QList<QStandardItem *> molItems =
|
---|
[cc2976] | 245 | QtMoleculeItemFactory::getInstance().createMoleculeItems(
|
---|
| 246 | _molid,
|
---|
| 247 | callback_DirtyItems,
|
---|
| 248 | callback_subjectKilledItems);
|
---|
[53c1ff] | 249 | QtMoleculeItem *mol_item = dynamic_cast<QtMoleculeItem *>(molItems.front());
|
---|
| 250 | ASSERT( mol_item != NULL,
|
---|
| 251 | "QtMoleculeList::addMoleculeItem() - item from factory was not a QtMoleculeItem?");
|
---|
[fcdf05] | 252 | {
|
---|
| 253 | boost::recursive_mutex::scoped_lock lock(map_mutex);
|
---|
| 254 | MoleculeItemBiMap.left.insert( std::make_pair(_molid, mol_item) );
|
---|
| 255 | LOG(1, "Adding" << _molecule_formula << " for " << _molid << " to MoleculeFormulaMap.");
|
---|
| 256 | MoleculeFormulaMap.insert( std::make_pair( _molid, _molecule_formula) );
|
---|
| 257 | }
|
---|
[2050b2] | 258 | // LOG(1, "Inserting molecule " << _mol->getId() << ": " << _mol);
|
---|
[8ccf3b] | 259 | _groupitem->appendRow(molItems);
|
---|
[6770fa] | 260 | }
|
---|
| 261 |
|
---|
[fcdf05] | 262 | std::string QtMoleculeList::addMolecule(const molecule * const _mol)
|
---|
[6770fa] | 263 | {
|
---|
| 264 | // find group if already in list
|
---|
[8ccf3b] | 265 | QStandardItem *groupItem = NULL;
|
---|
[6770fa] | 266 |
|
---|
[fcdf05] | 267 | const std::string molecule_formula = _mol->getFormula().toString();
|
---|
[6770fa] | 268 |
|
---|
| 269 | // new molecule type -> create new group
|
---|
[fcdf05] | 270 | if (!isGroupItemPresent(molecule_formula)){
|
---|
[6770fa] | 271 | // insert new formula entry into visibility
|
---|
| 272 | #ifndef NDEBUG
|
---|
| 273 | std::pair< FormulaVisibilityCountMap_t::iterator, bool> visibilityinserter =
|
---|
| 274 | #endif
|
---|
| 275 | FormulaVisibilityCountMap.insert(
|
---|
| 276 | std::make_pair( molecule_formula, (unsigned int)0) );
|
---|
| 277 | ASSERT( visibilityinserter.second,
|
---|
| 278 | "QtMoleculeList::refill() - molecule with formula "
|
---|
| 279 | +molecule_formula+" already in FormulaVisibilityCountMap.");
|
---|
| 280 |
|
---|
| 281 | // create item and place into Map with formula as key
|
---|
| 282 | addGroupItem(groupItem, molecule_formula);
|
---|
| 283 | } else {
|
---|
[fcdf05] | 284 | groupItem = FormulaToGroupItem(molecule_formula);
|
---|
[6770fa] | 285 | }
|
---|
| 286 | ASSERT( groupItem != NULL,
|
---|
[53c1ff] | 287 | "QtMoleculeList::addMolecule() - item with id "+toString(_mol->getId())
|
---|
[fcdf05] | 288 | +" has no parent?");
|
---|
[6770fa] | 289 |
|
---|
| 290 | // add molecule
|
---|
[69b434] | 291 | addMoleculeItem(groupItem, _mol->getId(), molecule_formula);
|
---|
[fcdf05] | 292 |
|
---|
| 293 | return molecule_formula;
|
---|
[6770fa] | 294 | }
|
---|
| 295 |
|
---|
[fcdf05] | 296 | void QtMoleculeList::removeMoleculeItem(QtMoleculeItem * const _item)
|
---|
[6770fa] | 297 | {
|
---|
[fcdf05] | 298 | boost::recursive_mutex::scoped_lock lock(refill_mutex);
|
---|
[53c1ff] | 299 | const QModelIndex mol_index = indexFromItem(_item);
|
---|
| 300 | QStandardItem *groupitem = _item->parent();
|
---|
| 301 | const QModelIndex group_index = groupitem->index();
|
---|
[fcdf05] | 302 | {
|
---|
| 303 | boost::recursive_mutex::scoped_lock lock(map_mutex);
|
---|
| 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 "
|
---|
| 314 | +toString(removeiter->second)+" in MoleculeFormulaMap.");
|
---|
| 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 |
|
---|
[53c1ff] | 324 | void QtMoleculeList::refill()
|
---|
| 325 | {
|
---|
| 326 | // check timer's presence
|
---|
| 327 | if (update_timer == NULL) {
|
---|
| 328 | update_timer = new QTimer(this);
|
---|
| 329 | connect( update_timer, SIGNAL(timeout()), this, SLOT(checkState()));
|
---|
| 330 | } else
|
---|
| 331 | update_timer->stop();
|
---|
[6770fa] | 332 |
|
---|
[fcdf05] | 333 | {
|
---|
| 334 | boost::recursive_mutex::scoped_lock refill_lock(refill_mutex);
|
---|
| 335 | boost::recursive_mutex::scoped_lock listAccessing_lock(listAccessing_mutex);
|
---|
| 336 |
|
---|
| 337 | // LOG(1, "Clearing list.");
|
---|
| 338 |
|
---|
| 339 | clear();
|
---|
| 340 | FormulaVisibilityCountMap.clear();
|
---|
| 341 | {
|
---|
| 342 | boost::recursive_mutex::scoped_lock lock(map_mutex);
|
---|
| 343 | FormulaItemBiMap.clear();
|
---|
| 344 | MoleculeFormulaMap.clear();
|
---|
| 345 | MoleculeItemBiMap.clear();
|
---|
[cc2976] | 346 | KilledItemsPerMolecule.clear();
|
---|
[fcdf05] | 347 | }
|
---|
| 348 | dirtyMolItems.clear();
|
---|
| 349 | visibilityMolItems.clear();
|
---|
| 350 | visibilityGroupItems.clear();
|
---|
| 351 | newMolecules.clear();
|
---|
| 352 | removedMolecules.clear();
|
---|
| 353 | toBeMovedItems.clear();
|
---|
| 354 | }
|
---|
[3eb91c] | 355 |
|
---|
[1259df] | 356 | const std::vector<const molecule*> &molecules =
|
---|
| 357 | const_cast<const World &>(World::getInstance()).getAllMolecules();
|
---|
| 358 | for (std::vector<const molecule*>::const_iterator iter = molecules.begin();
|
---|
[41815a3] | 359 | iter != molecules.end();
|
---|
[53c1ff] | 360 | iter++)
|
---|
| 361 | addMolecule(*iter);
|
---|
[b47bfc] | 362 |
|
---|
[53c1ff] | 363 | // activate timer
|
---|
| 364 | update_timer->start(1000/update_times_per_second);
|
---|
[a39006] | 365 | }
|
---|
| 366 |
|
---|
[53c1ff] | 367 | bool QtMoleculeList::areAnyItemsDirty()
|
---|
[a39006] | 368 | {
|
---|
[53c1ff] | 369 | // get whether any items are dirty
|
---|
[fcdf05] | 370 | boost::recursive_mutex::scoped_lock lock(listAccessing_mutex);
|
---|
[53c1ff] | 371 | bool dirty = false;
|
---|
[fcdf05] | 372 | dirty |= !dirtyMolItems.empty();
|
---|
| 373 | dirty |= !visibilityMolItems.empty();
|
---|
| 374 | dirty |= !dirtyGroupItems.empty();
|
---|
| 375 | dirty |= !visibilityGroupItems.empty();
|
---|
| 376 | dirty |= !newMolecules.empty();
|
---|
| 377 | dirty |= !removedMolecules.empty();
|
---|
| 378 | dirty |= !toBeMovedItems.empty();
|
---|
[53c1ff] | 379 | return dirty;
|
---|
[b47bfc] | 380 | }
|
---|
| 381 |
|
---|
[53c1ff] | 382 | void QtMoleculeList::checkState()
|
---|
| 383 | {
|
---|
| 384 | const bool dirty = areAnyItemsDirty();
|
---|
| 385 | // update if required
|
---|
| 386 | if (dirty)
|
---|
| 387 | updateItemStates();
|
---|
[b47bfc] | 388 | }
|
---|
| 389 |
|
---|
[53c1ff] | 390 | void QtMoleculeList::subjectKilled(Observable *publisher)
|
---|
| 391 | {}
|
---|
| 392 |
|
---|
[2696b1] | 393 | void QtMoleculeList::checkForVisibilityChange(QStandardItem* _item)
|
---|
[739ee9] | 394 | {
|
---|
[2050b2] | 395 | // qDebug() << "Item changed called.";
|
---|
[2696b1] | 396 |
|
---|
[fcdf05] | 397 | boost::recursive_mutex::scoped_lock lock(refill_mutex);
|
---|
| 398 | if (_item->index().column() == QtMoleculeItem::VISIBILITY) {
|
---|
[2050b2] | 399 | // qDebug() << "visibilityItem changed: " << (_item->checkState() ? "checked" : "unchecked");
|
---|
[fcdf05] | 400 | boost::recursive_mutex::scoped_lock lock(listAccessing_mutex);
|
---|
[2696b1] | 401 | if ((_item->parent() == NULL) || (_item->parent() == invisibleRootItem()))
|
---|
[fcdf05] | 402 | visibilityGroupItems.insert( std::make_pair(
|
---|
| 403 | GroupItemToFormula(_item->parent()), QtMoleculeItem::VISIBILITY) );
|
---|
[2696b1] | 404 | else
|
---|
[fcdf05] | 405 | visibilityMolItems.insert(
|
---|
| 406 | static_cast<QtMoleculeItem *>(_item)->getMoleculeId()
|
---|
| 407 | );
|
---|
[2696b1] | 408 | }
|
---|
| 409 | }
|
---|
[3eb91c] | 410 |
|
---|
[2696b1] | 411 | void QtMoleculeList::setVisibilityForMoleculeItem(QtMoleculeItem* _item)
|
---|
| 412 | {
|
---|
| 413 | if (ChangingChildrensVisibility)
|
---|
| 414 | return;
|
---|
[3eb91c] | 415 |
|
---|
[fcdf05] | 416 | boost::recursive_mutex::scoped_lock lock(refill_mutex);
|
---|
[2696b1] | 417 | const bool visible = _item->checkState();
|
---|
[fcdf05] | 418 | const moleculeId_t molid = _item->getMoleculeId();
|
---|
| 419 | std::string molecule_formula("illegal");
|
---|
| 420 | {
|
---|
| 421 | boost::recursive_mutex::scoped_lock lock(map_mutex);
|
---|
| 422 | MoleculeFormulaMap_t::const_iterator formulaiter =
|
---|
| 423 | MoleculeFormulaMap.find(molid);
|
---|
| 424 | ASSERT( formulaiter != MoleculeFormulaMap.end(),
|
---|
| 425 | "QtMoleculeList::setVisibilityForMoleculeItem() - formula of molecule "
|
---|
| 426 | +toString(molid)+" unknown.");
|
---|
| 427 | molecule_formula = formulaiter->second;
|
---|
| 428 | }
|
---|
| 429 | ASSERT( FormulaVisibilityCountMap.count(molecule_formula) != 0,
|
---|
[7d0ddb] | 430 | "QtMoleculeList::setVisibilityForMoleculeItem() - molecule with formula " +molecule_formula
|
---|
| 431 | +" is not present in FormulaVisibilityCountMap.");
|
---|
[3eb91c] | 432 |
|
---|
[2696b1] | 433 | // get parent
|
---|
| 434 | QStandardItem *groupItem = _item->parent();
|
---|
[fcdf05] | 435 | QStandardItem *visgroupItem = getSpecificGroupItem(groupItem, QtMoleculeItem::VISIBILITY);
|
---|
[2696b1] | 436 | ASSERT( groupItem != NULL,
|
---|
| 437 | "QtMoleculeList::setVisibilityForMoleculeItem() - item with id "
|
---|
[7d0ddb] | 438 | +toString(_item->getMoleculeId())+" has not parent?");
|
---|
[2696b1] | 439 | // check whether we have to set the group item
|
---|
| 440 |
|
---|
| 441 | ChangingChildrensVisibility = true;
|
---|
| 442 | if (visible) {
|
---|
| 443 | ++(FormulaVisibilityCountMap[molecule_formula]);
|
---|
| 444 | // compare with occurence/total number of molecules
|
---|
| 445 | if (FormulaVisibilityCountMap[molecule_formula] ==
|
---|
| 446 | (unsigned int)(groupItem->rowCount()))
|
---|
| 447 | visgroupItem->setCheckState(Qt::Checked);
|
---|
| 448 | } else {
|
---|
| 449 | --(FormulaVisibilityCountMap[molecule_formula]);
|
---|
| 450 | // none selected anymore?
|
---|
| 451 | if (FormulaVisibilityCountMap[molecule_formula] == 0)
|
---|
| 452 | visgroupItem->setCheckState(Qt::Unchecked);
|
---|
| 453 | }
|
---|
| 454 | ChangingChildrensVisibility = false;
|
---|
[3eb91c] | 455 |
|
---|
[2696b1] | 456 | emit moleculesVisibilityChanged(_item->getMolecule()->getId(), visible);
|
---|
| 457 | }
|
---|
[3eb91c] | 458 |
|
---|
[2696b1] | 459 | void QtMoleculeList::setVisibilityForGroupItem(QStandardItem* _item)
|
---|
| 460 | {
|
---|
| 461 | if (ChangingChildrensVisibility)
|
---|
| 462 | return;
|
---|
| 463 |
|
---|
| 464 | ChangingChildrensVisibility = true;
|
---|
| 465 |
|
---|
[fcdf05] | 466 | boost::recursive_mutex::scoped_lock lock(refill_mutex);
|
---|
[2696b1] | 467 | // go through all children, but don't enter for groupItem once more
|
---|
| 468 | const bool visible = _item->checkState();
|
---|
[fcdf05] | 469 | QStandardItem *groupitem = getSpecificGroupItem(_item, QtMoleculeItem::NAME);
|
---|
[2696b1] | 470 | for (int i=0;i<groupitem->rowCount();++i) {
|
---|
| 471 | QtMoleculeItem *molItem = dynamic_cast<QtMoleculeItem *>(
|
---|
[fcdf05] | 472 | groupitem->child(i, QtMoleculeItem::VISIBILITY));
|
---|
[2696b1] | 473 | if (molItem->checkState() != visible) {
|
---|
| 474 | molItem->setCheckState(visible ? Qt::Checked : Qt::Unchecked);
|
---|
| 475 |
|
---|
| 476 | // emit signal
|
---|
| 477 | emit moleculesVisibilityChanged(molItem->getMolecule()->getId(), visible);
|
---|
[3eb91c] | 478 | }
|
---|
[2696b1] | 479 | }
|
---|
| 480 | // set current number of visible children
|
---|
| 481 | const std::string molecule_formula =
|
---|
[fcdf05] | 482 | GroupItemToFormula( getSpecificGroupItem(_item, QtMoleculeItem::NAME) );
|
---|
[2696b1] | 483 | FormulaVisibilityCountMap_t::iterator countiter =
|
---|
| 484 | FormulaVisibilityCountMap.find(molecule_formula);
|
---|
| 485 | ASSERT( countiter != FormulaVisibilityCountMap.end(),
|
---|
| 486 | "QtMoleculeList::setVisibilityForGroupItem() - molecules "+molecule_formula
|
---|
| 487 | +" have no entry in visibility count map?");
|
---|
| 488 | countiter->second = visible ? groupitem->rowCount() : 0;
|
---|
| 489 |
|
---|
| 490 | ChangingChildrensVisibility = false;
|
---|
[739ee9] | 491 | }
|
---|
[2696b1] | 492 |
|
---|
[739ee9] | 493 |
|
---|
[0eb7bf3] | 494 | void QtMoleculeList::moleculeChanged() {
|
---|
[79b59b] | 495 | /*int idx = verticalHeaderItem(row)->data(Qt::UserRole).toInt();
|
---|
[b47bfc] | 496 | molecule *mol = molecules->ReturnIndex(idx);
|
---|
[fcdf05] | 497 | string cellValue = item(row,QtMoleculeItem::NAME)->text().toStdString();
|
---|
[b47bfc] | 498 | if(mol->getName() != cellValue && cellValue !="") {
|
---|
| 499 | mol->setName(cellValue);
|
---|
| 500 | }
|
---|
| 501 | else if(cellValue==""){
|
---|
[fcdf05] | 502 | item(row,QtMoleculeItem::NAME)->setText(QString(mol->getName().c_str()));
|
---|
[79b59b] | 503 | }*/
|
---|
[b47bfc] | 504 | }
|
---|
| 505 |
|
---|
[b14efe] | 506 |
|
---|
[53c1ff] | 507 | int QtMoleculeList::setOccurrence(QStandardItem * const _groupitem)
|
---|
| 508 | {
|
---|
[fcdf05] | 509 | boost::recursive_mutex::scoped_lock lock(refill_mutex);
|
---|
[53c1ff] | 510 | QModelIndex modelindex = _groupitem->index();
|
---|
| 511 | ASSERT( modelindex.isValid(),
|
---|
| 512 | "QtMoleculeList::setOccurrence() - groupitem not associated to model anymore.");
|
---|
| 513 | const int index = modelindex.row();
|
---|
| 514 | QStandardItem *parent_item =
|
---|
| 515 | _groupitem->parent() == NULL ? invisibleRootItem() : _groupitem->parent();
|
---|
| 516 | ASSERT( parent_item != NULL,
|
---|
| 517 | "QtMoleculeList::setOccurrence() - group item at "+toString(index)
|
---|
| 518 | +" does not have a parent?");
|
---|
[fcdf05] | 519 | QStandardItem *occ_item = parent_item->child(index, QtMoleculeItem::OCCURRENCE);
|
---|
[53c1ff] | 520 | ASSERT( occ_item != NULL,
|
---|
| 521 | "QtMoleculeList::setOccurrence() - group item at "+toString(index)
|
---|
| 522 | +" does not have an occurrence?");
|
---|
| 523 | const int count = _groupitem->rowCount();
|
---|
| 524 | if (count == 0) {
|
---|
| 525 | // we have to remove the group item completely
|
---|
[fcdf05] | 526 | boost::recursive_mutex::scoped_lock lock(map_mutex);
|
---|
[53c1ff] | 527 | const std::string molecule_formula = _groupitem->text().toStdString();
|
---|
[fcdf05] | 528 | FormulaItemBiMap.left.erase(molecule_formula);
|
---|
[53c1ff] | 529 | FormulaVisibilityCountMap.erase(molecule_formula);
|
---|
| 530 | return index;
|
---|
| 531 | } else {
|
---|
| 532 | occ_item->setText(QString::number(count));
|
---|
| 533 | return -1;
|
---|
| 534 | }
|
---|
| 535 | }
|
---|
| 536 |
|
---|
[fcdf05] | 537 | std::string QtMoleculeList::readdItem(QtMoleculeItem *_molitem)
|
---|
[53c1ff] | 538 | {
|
---|
[fcdf05] | 539 | boost::recursive_mutex::scoped_lock lock(refill_mutex);
|
---|
[53c1ff] | 540 | // use takeRows of molecule ..
|
---|
| 541 | QStandardItem *groupitem = _molitem->parent();
|
---|
| 542 | ASSERT( groupitem != NULL,
|
---|
| 543 | "QtMoleculeList::readdItem() - mol item at "+toString(_molitem->index().row())
|
---|
| 544 | +" does not have a groupitem?");
|
---|
[fcdf05] | 545 | const molecule * const mol = _molitem->getMolecule();
|
---|
[53c1ff] | 546 | QList<QStandardItem *> mol_row = _molitem->parent()->takeRow(_molitem->index().row());
|
---|
| 547 | // .. and re-add where new formula fits
|
---|
[7d0ddb] | 548 | std::string molecule_formula;
|
---|
| 549 | if (mol != NULL) {
|
---|
| 550 | molecule_formula = mol->getFormula().toString();
|
---|
[fcdf05] | 551 | if (!isGroupItemPresent(molecule_formula)) {
|
---|
[7d0ddb] | 552 | // add new group item and formula entry
|
---|
| 553 | addGroupItem(groupitem, molecule_formula);
|
---|
| 554 | } else {
|
---|
[fcdf05] | 555 | groupitem = FormulaToGroupItem(molecule_formula);
|
---|
[7d0ddb] | 556 | }
|
---|
| 557 | ASSERT( groupitem != NULL,
|
---|
| 558 | "QtMoleculeList::readdItem() - failed to create a sensible new groupitem");
|
---|
| 559 | // finally add again
|
---|
| 560 | groupitem->appendRow(mol_row);
|
---|
[fcdf05] | 561 | } else {
|
---|
| 562 | for (QList<QStandardItem *>::iterator iter = mol_row.begin();
|
---|
| 563 | iter != mol_row.end(); ++iter)
|
---|
| 564 | delete *iter;
|
---|
[53c1ff] | 565 | }
|
---|
[fcdf05] | 566 |
|
---|
| 567 | return molecule_formula;
|
---|
[53c1ff] | 568 | }
|
---|
| 569 |
|
---|
| 570 | void QtMoleculeList::informDirtyState(
|
---|
[fcdf05] | 571 | const moleculeId_t _id,
|
---|
| 572 | const QtMoleculeItem::COLUMNTYPES _type,
|
---|
| 573 | const QtMoleculeItem::MoveTypes _movetype)
|
---|
[53c1ff] | 574 | {
|
---|
[7d0ddb] | 575 | listAccessing_mutex.lock();
|
---|
[fcdf05] | 576 | dirtyMolItems.insert( std::make_pair(_id, _type) );
|
---|
[7d0ddb] | 577 | listAccessing_mutex.unlock();
|
---|
[95f49f] | 578 |
|
---|
[fcdf05] | 579 | if (_movetype == QtMoleculeItem::NeedsMove) {
|
---|
[53c1ff] | 580 | // we have to convert whatever item raised the dirty signal to the first
|
---|
| 581 | // item in the row as otherwise multiple items in the row are selected
|
---|
| 582 | // as to be moved, i.e. the same row is moved multiple times
|
---|
[7d0ddb] | 583 | listAccessing_mutex.lock();
|
---|
[fcdf05] | 584 | toBeMovedItems.insert(_id);
|
---|
[7d0ddb] | 585 | listAccessing_mutex.unlock();
|
---|
[53c1ff] | 586 | }
|
---|
| 587 | }
|
---|
| 588 |
|
---|
| 589 | void QtMoleculeList::updateItemStates()
|
---|
| 590 | {
|
---|
| 591 | /// copy lists such that new signals for dirty/.. may come in right away
|
---|
| 592 | // TODO: if we had move semantics ...
|
---|
[7d0ddb] | 593 | listAccessing_mutex.lock();
|
---|
[fcdf05] | 594 | list_of_molecule_items_t dirtyMolItems_copy = dirtyMolItems;
|
---|
| 595 | dirtyMolItems.clear();
|
---|
| 596 | list_of_molecules_t visibilityMolItems_copy = visibilityMolItems;
|
---|
| 597 | visibilityMolItems.clear();
|
---|
| 598 | list_of_group_items_t dirtyGroupItems_copy = dirtyGroupItems;
|
---|
| 599 | dirtyGroupItems.clear();
|
---|
| 600 | list_of_group_items_t visibilityGroupItems_copy = visibilityGroupItems;
|
---|
| 601 | visibilityGroupItems.clear();
|
---|
| 602 | std::vector<moleculeId_t> newMolecules_copy = newMolecules;
|
---|
| 603 | newMolecules.clear();
|
---|
| 604 | std::vector<moleculeId_t> removedMolecules_copy = removedMolecules;
|
---|
| 605 | removedMolecules.clear();
|
---|
| 606 | list_of_molecules_t toBeMovedItems_copy = toBeMovedItems;
|
---|
[53c1ff] | 607 | toBeMovedItems.clear();
|
---|
[7d0ddb] | 608 | listAccessing_mutex.unlock();
|
---|
[53c1ff] | 609 |
|
---|
[95f49f] | 610 | // wait till initial refill has been executed
|
---|
[fcdf05] | 611 | boost::recursive_mutex::scoped_lock lock(refill_mutex);
|
---|
[95f49f] | 612 |
|
---|
[fcdf05] | 613 | // LOG(1, "Starting update.");
|
---|
[95f49f] | 614 |
|
---|
[fcdf05] | 615 | // remove removedMolecules from other lists.
|
---|
| 616 | for (std::vector<moleculeId_t>::const_iterator removeiter = removedMolecules_copy.begin();
|
---|
| 617 | removeiter != removedMolecules_copy.end(); ++removeiter) {
|
---|
| 618 | for (unsigned int i=0;i< QtMoleculeItem::COLUMNTYPES_MAX; ++i)
|
---|
| 619 | dirtyMolItems_copy.erase( std::make_pair(*removeiter,(QtMoleculeItem::COLUMNTYPES)i) );
|
---|
| 620 | toBeMovedItems_copy.erase(*removeiter);
|
---|
| 621 | visibilityMolItems_copy.erase(*removeiter);
|
---|
[2696b1] | 622 | }
|
---|
[8ccf3b] | 623 |
|
---|
[2696b1] | 624 | /// 1a. do the update for each dirty item
|
---|
[fcdf05] | 625 | for (list_of_molecule_items_t::const_iterator dirtyiter = dirtyMolItems_copy.begin();
|
---|
| 626 | dirtyiter != dirtyMolItems_copy.end(); ++dirtyiter) {
|
---|
| 627 | if (!isMoleculeItemPresent(dirtyiter->first))
|
---|
| 628 | continue;
|
---|
| 629 | QtMoleculeItem * const mol_item =
|
---|
| 630 | getSpecificMoleculeItem(
|
---|
| 631 | MoleculeIdToItem(dirtyiter->first),
|
---|
| 632 | dirtyiter->second);
|
---|
| 633 | // LOG(1, "Updating item " << mol_item);
|
---|
| 634 | mol_item->updateState();
|
---|
[53c1ff] | 635 | }
|
---|
| 636 |
|
---|
[2696b1] | 637 | /// 1b. do the visibility update for each dirty item
|
---|
[fcdf05] | 638 | for (list_of_molecules_t::const_iterator visiter = visibilityMolItems_copy.begin();
|
---|
| 639 | visiter != visibilityMolItems_copy.end(); ++visiter) {
|
---|
| 640 | if (!isMoleculeItemPresent(*visiter))
|
---|
| 641 | continue;
|
---|
| 642 | QtMoleculeItem * const visitem =
|
---|
| 643 | getSpecificMoleculeItem(
|
---|
| 644 | MoleculeIdToItem(*visiter),
|
---|
| 645 | QtMoleculeItem::VISIBILITY );
|
---|
| 646 | // LOG(1, "Updating visibility of item " << visitem);
|
---|
| 647 | setVisibilityForMoleculeItem(visitem);
|
---|
[2696b1] | 648 | }
|
---|
| 649 |
|
---|
[53c1ff] | 650 | /// 2. move all items that need to be moved
|
---|
[fcdf05] | 651 | typedef std::set<std::string> formulas_t;
|
---|
| 652 | formulas_t toBeSetOccurrence;
|
---|
| 653 | for (list_of_molecules_t::const_iterator moveiter = toBeMovedItems_copy.begin();
|
---|
[53c1ff] | 654 | moveiter != toBeMovedItems_copy.end(); ++moveiter) {
|
---|
[fcdf05] | 655 | boost::recursive_mutex::scoped_lock lock(map_mutex);
|
---|
| 656 | // LOG(1, "Moving item " << molitem);
|
---|
| 657 | MoleculeFormulaMap_t::iterator formulaiter =
|
---|
| 658 | MoleculeFormulaMap.find(*moveiter);
|
---|
| 659 | ASSERT( formulaiter != MoleculeFormulaMap.end(),
|
---|
| 660 | "QtMoleculeList::updateItemStates() - formula of molecule "
|
---|
| 661 | +toString(*moveiter)+" unknown.");
|
---|
| 662 | // LOG(1, "Adding " << formulaiter->second << " to toBeSetOccurrence.");
|
---|
| 663 | toBeSetOccurrence.insert( formulaiter->second );
|
---|
| 664 | if (!isMoleculeItemPresent(*moveiter))
|
---|
| 665 | continue;
|
---|
| 666 | QtMoleculeItem *const molitem = MoleculeIdToItem(*moveiter);
|
---|
| 667 | LOG(1, "Moving item " << molitem);
|
---|
| 668 | const molecule *mol = molitem->getMolecule();
|
---|
[cc2976] | 669 | if (mol != NULL) {
|
---|
[fcdf05] | 670 | // remove from formula<->molecule bimap with old formula
|
---|
| 671 | LOG(1, "Removing " << formulaiter->second << " for " << formulaiter->first << " from MoleculeFormulaMap.");
|
---|
| 672 | MoleculeFormulaMap.erase( formulaiter );
|
---|
| 673 | const std::string formula = readdItem(molitem);
|
---|
| 674 | // and add to formula<->molecule bimap with updated formula
|
---|
| 675 | LOG(1, "Adding " << formula << " for " << *moveiter << " to MoleculeFormulaMap.");
|
---|
| 676 | MoleculeFormulaMap.insert( std::make_pair(*moveiter, formula) );
|
---|
| 677 | // LOG(1, "Adding " << formula << " to toBeSetOccurrence.");
|
---|
| 678 | toBeSetOccurrence.insert( formula );
|
---|
| 679 | }
|
---|
| 680 | }
|
---|
| 681 |
|
---|
[53c1ff] | 682 | /// 3. remove all items whose molecules have been removed
|
---|
[fcdf05] | 683 | for (std::vector<moleculeId_t>::const_iterator removeiter = removedMolecules_copy.begin();
|
---|
| 684 | removeiter != removedMolecules_copy.end(); ++removeiter) {
|
---|
| 685 | // LOG(1, "Removing molecule " << *removeiter);
|
---|
| 686 | if (!isMoleculeItemPresent(*removeiter))
|
---|
| 687 | continue;
|
---|
| 688 | QtMoleculeItem *item = MoleculeIdToItem(*removeiter);
|
---|
| 689 | if (item != NULL) {
|
---|
| 690 | const std::string formula = item->parent()->text().toStdString();
|
---|
| 691 | // LOG(1, "Adding " << formula << " to toBeSetOccurrence.");
|
---|
| 692 | toBeSetOccurrence.insert( formula );
|
---|
| 693 | removeMoleculeItem(item);
|
---|
[cc2976] | 694 | KilledItemsPerMolecule.erase( *removeiter );
|
---|
[fcdf05] | 695 | }
|
---|
[53c1ff] | 696 | }
|
---|
| 697 |
|
---|
[cc2976] | 698 | // throw out items that we added by an update() while we are in this function
|
---|
| 699 | listAccessing_mutex.lock();
|
---|
| 700 | for (std::vector<moleculeId_t>::const_iterator removeiter = removedMolecules_copy.begin();
|
---|
| 701 | removeiter != removedMolecules_copy.end(); ++removeiter) {
|
---|
| 702 | for (unsigned int i=0;i< QtMoleculeItem::COLUMNTYPES_MAX; ++i)
|
---|
| 703 | dirtyMolItems.erase( std::make_pair(*removeiter,(QtMoleculeItem::COLUMNTYPES)i) );
|
---|
| 704 | toBeMovedItems.erase(*removeiter);
|
---|
| 705 | visibilityMolItems.erase(*removeiter);
|
---|
| 706 | }
|
---|
| 707 | listAccessing_mutex.unlock();
|
---|
| 708 | // after that it is not a problem as items have been removed (hence signOff() was called)
|
---|
| 709 |
|
---|
[53c1ff] | 710 | /// 4. instantiate all new items
|
---|
[fcdf05] | 711 | for (std::vector<moleculeId_t>::const_iterator moliter = newMolecules_copy.begin();
|
---|
| 712 | moliter != newMolecules_copy.end(); ++moliter) {
|
---|
| 713 | // LOG(1, "Adding molecule " << *moliter);
|
---|
[53c1ff] | 714 | // check that World knows the molecule still
|
---|
[63fb7a] | 715 | const molecule * const mol = const_cast<const World &>(World::getInstance()).
|
---|
[fcdf05] | 716 | getMolecule(MoleculeById(*moliter));
|
---|
| 717 | if ((mol != NULL) && (mol->getId() == *moliter)) {
|
---|
| 718 | const std::string formula = addMolecule(mol);;
|
---|
| 719 | // LOG(1, "Adding " << formula << " to toBeSetOccurrence.");
|
---|
| 720 | toBeSetOccurrence.insert( formula );
|
---|
| 721 | } else {
|
---|
| 722 | ELOG(2, "Molecule " << *moliter
|
---|
| 723 | << " disappeared before we could render it in QtMoleculeList.");
|
---|
[53c1ff] | 724 | }
|
---|
| 725 | }
|
---|
| 726 |
|
---|
[2696b1] | 727 | /// 5a. update the group item's occurrence and visibility
|
---|
[53c1ff] | 728 | std::set<int> RowsToRemove;
|
---|
[fcdf05] | 729 | for (std::set<std::string>::const_iterator groupiter = toBeSetOccurrence.begin();
|
---|
| 730 | groupiter != toBeSetOccurrence.end(); ++groupiter) {
|
---|
| 731 | // LOG(1, "Updating group item's occurence " << *groupiter);
|
---|
| 732 | QStandardItem *groupitem = FormulaToGroupItem(*groupiter);
|
---|
| 733 | const int index = setOccurrence(groupitem);
|
---|
| 734 | if (index != -1) {
|
---|
| 735 | // LOG(1, "Removing row of group item " << groupitem);
|
---|
[53c1ff] | 736 | RowsToRemove.insert(index);
|
---|
[fcdf05] | 737 | }
|
---|
[53c1ff] | 738 | }
|
---|
[fcdf05] | 739 | toBeSetOccurrence.clear();
|
---|
[53c1ff] | 740 |
|
---|
[2696b1] | 741 | // remove all visibility updates whose row is removed
|
---|
[fcdf05] | 742 | for (list_of_group_items_t::iterator visiter = visibilityGroupItems_copy.begin();
|
---|
| 743 | visiter != visibilityGroupItems_copy.end(); ) {
|
---|
| 744 | QStandardItem * const groupitem = FormulaToGroupItem(visiter->first);
|
---|
| 745 | if (RowsToRemove.count(groupitem->index().row()) != 0) {
|
---|
| 746 | // LOG(1, "Removing vis item " << *visiter << " because of removed group item.");
|
---|
| 747 | visibilityGroupItems_copy.erase(visiter++);
|
---|
| 748 | } else
|
---|
[2696b1] | 749 | ++visiter;
|
---|
| 750 | }
|
---|
| 751 |
|
---|
| 752 | // update visibility of all group items
|
---|
[fcdf05] | 753 | for (list_of_group_items_t::iterator visiter = visibilityGroupItems_copy.begin();
|
---|
| 754 | visiter != visibilityGroupItems_copy.end(); ++visiter) {
|
---|
[2050b2] | 755 | // LOG(1, "Updating visibility of item " << *visiter);
|
---|
[fcdf05] | 756 | QStandardItem * const groupitem =
|
---|
| 757 | getSpecificGroupItem(FormulaToGroupItem(visiter->first),
|
---|
| 758 | visiter->second);
|
---|
| 759 | setVisibilityForGroupItem(groupitem);
|
---|
[2696b1] | 760 | }
|
---|
| 761 |
|
---|
[53c1ff] | 762 | /// 5b. remove all rows with 0 occurrence starting from last
|
---|
| 763 | for (std::set<int>::reverse_iterator riter = RowsToRemove.rbegin();
|
---|
| 764 | riter != RowsToRemove.rend(); ++riter) {
|
---|
[2050b2] | 765 | // LOG(1, "Removing group item at row " << *riter);
|
---|
[53c1ff] | 766 | removeRows(*riter, 1, invisibleRootItem()->index());
|
---|
| 767 | }
|
---|
| 768 |
|
---|
| 769 | // and done
|
---|
[fcdf05] | 770 | // LOG(1, "Done with update.");
|
---|
[53c1ff] | 771 | }
|
---|