| 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 <QMetaMethod>
|
|---|
| 38 |
|
|---|
| 39 | #include <iostream>
|
|---|
| 40 |
|
|---|
| 41 | #include "CodePatterns/MemDebug.hpp"
|
|---|
| 42 |
|
|---|
| 43 | #include "CodePatterns/Observer/Notification.hpp"
|
|---|
| 44 |
|
|---|
| 45 | #include "Atom/atom.hpp"
|
|---|
| 46 | #include "Formula.hpp"
|
|---|
| 47 | #include "molecule.hpp"
|
|---|
| 48 | #include "MoleculeListClass.hpp"
|
|---|
| 49 | #include "Actions/SelectionAction/Molecules/MoleculeByIdAction.hpp"
|
|---|
| 50 | #include "Actions/SelectionAction/Molecules/NotMoleculeByIdAction.hpp"
|
|---|
| 51 |
|
|---|
| 52 | using namespace std;
|
|---|
| 53 |
|
|---|
| 54 | // maybe this should go with the definition of molecules
|
|---|
| 55 |
|
|---|
| 56 | // some attributes need to be easier to find for molecules
|
|---|
| 57 | // these attributes are skipped so far
|
|---|
| 58 | const int QtMoleculeList::COLUMNCOUNT = COLUMNTYPES_MAX;
|
|---|
| 59 | const char *QtMoleculeList::COLUMNNAMES[QtMoleculeList::COLUMNCOUNT]={"Name","Visibility", "Atoms","Formula","Occurrence"/*,"Size"*/};
|
|---|
| 60 |
|
|---|
| 61 | QtMoleculeList::QtMoleculeList() :
|
|---|
| 62 | Observer("QtMoleculeList")
|
|---|
| 63 | {
|
|---|
| 64 | setColumnCount(COLUMNCOUNT);
|
|---|
| 65 | // setSelectionMode(QAbstractItemView::MultiSelection);
|
|---|
| 66 |
|
|---|
| 67 | World::getInstance().signOn(this, World::MoleculeInserted);
|
|---|
| 68 | World::getInstance().signOn(this, World::MoleculeRemoved);
|
|---|
| 69 |
|
|---|
| 70 | dirty = true;
|
|---|
| 71 | clearing = false;
|
|---|
| 72 | selecting = false;
|
|---|
| 73 | changing = false;
|
|---|
| 74 | ChangingChildrensVisibility = false;
|
|---|
| 75 | refill();
|
|---|
| 76 |
|
|---|
| 77 | // qRegisterMetaType<QItemSelection>("QItemSelection");
|
|---|
| 78 | //connect(this,SIGNAL(cellChanged(int,int)),this,SLOT(moleculeChanged(int,int)));
|
|---|
| 79 | // connect(selectionModel(),SIGNAL(selectionChanged(QItemSelection, QItemSelection)),this,SLOT(rowsSelected(QItemSelection, QItemSelection)));
|
|---|
| 80 | // connect(this, SIGNAL(itemChanged(QStandardItem*, int)), this, SLOT(visibilityChanged(QStandardItem*, int)));
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | QtMoleculeList::~QtMoleculeList()
|
|---|
| 84 | {
|
|---|
| 85 | World::getInstance().signOff(this, World::MoleculeInserted);
|
|---|
| 86 | World::getInstance().signOff(this, World::MoleculeRemoved);
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | QVariant QtMoleculeList::headerData(int section, Qt::Orientation orientation, int role) const
|
|---|
| 90 | {
|
|---|
| 91 | if (role == Qt::DisplayRole) {
|
|---|
| 92 | if (orientation == Qt::Horizontal) {
|
|---|
| 93 | if (section < COLUMNTYPES_MAX)
|
|---|
| 94 | return QString(COLUMNNAMES[section]);
|
|---|
| 95 | }
|
|---|
| 96 | }
|
|---|
| 97 | return QVariant();
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | void QtMoleculeList::update(Observable *publisher) {
|
|---|
| 101 | ASSERT(0,
|
|---|
| 102 | "QtMoleculeList::update() - we did not sign up for any global updates.");
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | QStandardItem * QtMoleculeList::MoleculeToItem(const molecule * const _mol)
|
|---|
| 106 | {
|
|---|
| 107 | MoleculeItemBiMap_t::left_const_iterator iter =
|
|---|
| 108 | MoleculeItemBiMap.left.find(_mol);
|
|---|
| 109 | ASSERT( iter != MoleculeItemBiMap.left.end(),
|
|---|
| 110 | "QtMoleculeList - could not find molecule "+_mol->getName()+" in my list.");
|
|---|
| 111 | return iter->second;
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 | void QtMoleculeList::recieveNotification(Observable *publisher, Notification_ptr notification)
|
|---|
| 115 | {
|
|---|
| 116 | if (dynamic_cast<World *>(publisher) != NULL) {
|
|---|
| 117 | switch (notification->getChannelNo()) {
|
|---|
| 118 | case World::MoleculeInserted:
|
|---|
| 119 | {
|
|---|
| 120 | const molecule * const mol = World::getInstance().lastChanged<molecule>();
|
|---|
| 121 | addItem( mol );
|
|---|
| 122 | break;
|
|---|
| 123 | }
|
|---|
| 124 | case World::MoleculeRemoved:
|
|---|
| 125 | {
|
|---|
| 126 | const molecule * const mol = World::getInstance().lastChanged<molecule>();
|
|---|
| 127 | removeItem( mol );
|
|---|
| 128 | break;
|
|---|
| 129 | }
|
|---|
| 130 | default:
|
|---|
| 131 | ASSERT(0, "QtMoleculeList::recieveNotification() - cannot get here, not subscribed to channel "
|
|---|
| 132 | +toString(notification->getChannelNo()));
|
|---|
| 133 | break;
|
|---|
| 134 | }
|
|---|
| 135 | }
|
|---|
| 136 |
|
|---|
| 137 | if (selecting)
|
|---|
| 138 | return;
|
|---|
| 139 |
|
|---|
| 140 | dirty = true;
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | void QtMoleculeList::addGroupItem(
|
|---|
| 144 | QStandardItem *&mainitem,
|
|---|
| 145 | const std::string &_molecule_formula)
|
|---|
| 146 | {
|
|---|
| 147 | QList<QStandardItem *> groupItems;
|
|---|
| 148 | // fill item list
|
|---|
| 149 | mainitem = new QStandardItem(QString("default"));
|
|---|
| 150 | mainitem->setFlags(mainitem->flags() ^ Qt::ItemIsSelectable);
|
|---|
| 151 | formula.insert( std::make_pair(_molecule_formula, mainitem) );
|
|---|
| 152 | groupItems << mainitem;
|
|---|
| 153 | QStandardItem *visitem = new QStandardItem;
|
|---|
| 154 | visitem->setCheckState(Qt::Unchecked);
|
|---|
| 155 | visitem->setFlags(mainitem->flags() | Qt::ItemIsUserCheckable);
|
|---|
| 156 | groupItems << visitem;
|
|---|
| 157 | groupItems << new QStandardItem(QString::number(0));
|
|---|
| 158 | groupItems << new QStandardItem(QString(""));
|
|---|
| 159 | groupItems << new QStandardItem(QString::number(0));
|
|---|
| 160 | invisibleRootItem()->appendRow(groupItems);
|
|---|
| 161 | // _groupItem->setData(0, Qt::UserRole, QVariant(-1));
|
|---|
| 162 | }
|
|---|
| 163 |
|
|---|
| 164 | void QtMoleculeList::addMoleculeItem(
|
|---|
| 165 | QStandardItem *_groupitem,
|
|---|
| 166 | const molecule *_mol,
|
|---|
| 167 | const std::string &_molecule_formula)
|
|---|
| 168 | {
|
|---|
| 169 | QList<QStandardItem *> molItems;
|
|---|
| 170 | QStandardItem *mainitem = new QStandardItem(QString(_mol->getName().c_str()));
|
|---|
| 171 | mainitem->setFlags(mainitem->flags() | Qt::ItemIsSelectable);
|
|---|
| 172 | // mainitem->setSelected(World::getInstance().isSelected(_mol));
|
|---|
| 173 | MoleculeItemBiMap.left.insert( std::make_pair(_mol, mainitem) );
|
|---|
| 174 | molItems << mainitem;
|
|---|
| 175 | QStandardItem *visitem = new QStandardItem();
|
|---|
| 176 | visitem->setCheckState(Qt::Unchecked);
|
|---|
| 177 | visitem->setFlags(mainitem->flags() | Qt::ItemIsUserCheckable);
|
|---|
| 178 | molItems << visitem;
|
|---|
| 179 | molItems << new QStandardItem(QString::number(_mol->getAtomCount()));
|
|---|
| 180 | molItems << new QStandardItem(QString(_molecule_formula.c_str()));
|
|---|
| 181 | molItems << new QStandardItem(QString::number(0));
|
|---|
| 182 | // const int index = _mol->getId();
|
|---|
| 183 | // molItem->setData(0, Qt::UserRole, QVariant(index));
|
|---|
| 184 | _groupitem->appendRow(molItems);
|
|---|
| 185 | }
|
|---|
| 186 |
|
|---|
| 187 | void QtMoleculeList::addItem(const molecule *_mol)
|
|---|
| 188 | {
|
|---|
| 189 | if (changing)
|
|---|
| 190 | return;
|
|---|
| 191 | changing = true;
|
|---|
| 192 | // find group if already in list
|
|---|
| 193 | QStandardItem *groupItem = NULL;
|
|---|
| 194 |
|
|---|
| 195 | const std::string &molecule_formula = _mol->getFormula().toString();
|
|---|
| 196 | FormulaTreeItemMap_t::const_iterator formulaiter =
|
|---|
| 197 | formula.find(molecule_formula);
|
|---|
| 198 |
|
|---|
| 199 | // new molecule type -> create new group
|
|---|
| 200 | if (formulaiter == formula.end()){
|
|---|
| 201 | // insert new formula entry into visibility
|
|---|
| 202 | #ifndef NDEBUG
|
|---|
| 203 | std::pair< FormulaVisibilityCountMap_t::iterator, bool> visibilityinserter =
|
|---|
| 204 | #endif
|
|---|
| 205 | FormulaVisibilityCountMap.insert(
|
|---|
| 206 | std::make_pair( molecule_formula, (unsigned int)0) );
|
|---|
| 207 | ASSERT( visibilityinserter.second,
|
|---|
| 208 | "QtMoleculeList::refill() - molecule with formula "
|
|---|
| 209 | +molecule_formula+" already in FormulaVisibilityCountMap.");
|
|---|
| 210 |
|
|---|
| 211 | // create item and place into Map with formula as key
|
|---|
| 212 | addGroupItem(groupItem, molecule_formula);
|
|---|
| 213 | } else {
|
|---|
| 214 | groupItem = formulaiter->second;
|
|---|
| 215 | }
|
|---|
| 216 | ASSERT( groupItem != NULL,
|
|---|
| 217 | "QtMoleculeList::addItem() - item with id "+toString(_mol->getId())
|
|---|
| 218 | +" has not parent?");
|
|---|
| 219 |
|
|---|
| 220 | // add molecule
|
|---|
| 221 | addMoleculeItem(groupItem, _mol, molecule_formula);
|
|---|
| 222 |
|
|---|
| 223 | // increase group occurrence
|
|---|
| 224 | const int index = groupItem->index().row();
|
|---|
| 225 | QStandardItem *occ_item = invisibleRootItem()->child(index, OCCURRENCE);
|
|---|
| 226 | int count = occ_item->text().toInt() + 1;
|
|---|
| 227 | occ_item->setText(QString::number(count));
|
|---|
| 228 |
|
|---|
| 229 | changing = false;
|
|---|
| 230 | }
|
|---|
| 231 |
|
|---|
| 232 | void QtMoleculeList::removeItem(const molecule *_mol)
|
|---|
| 233 | {
|
|---|
| 234 | if (changing)
|
|---|
| 235 | return;
|
|---|
| 236 | changing = true;
|
|---|
| 237 |
|
|---|
| 238 | dirty = true;
|
|---|
| 239 | // TODO: We have to get some Model into the Table in order to "find" items right
|
|---|
| 240 | // away
|
|---|
| 241 | // QTreeWidgetItem *molItem = itemFromIndex(mol->getId());
|
|---|
| 242 | // delete molItem1;
|
|---|
| 243 |
|
|---|
| 244 | changing = false;
|
|---|
| 245 | }
|
|---|
| 246 |
|
|---|
| 247 | void QtMoleculeList::refill() {
|
|---|
| 248 | clearing = true;
|
|---|
| 249 | const std::vector<molecule*> &molecules = World::getInstance().getAllMolecules();
|
|---|
| 250 |
|
|---|
| 251 | clear();
|
|---|
| 252 | formula.clear();
|
|---|
| 253 | FormulaVisibilityCountMap.clear();
|
|---|
| 254 | MoleculeItemBiMap.clear();
|
|---|
| 255 |
|
|---|
| 256 | for (std::vector<molecule*>::const_iterator iter = molecules.begin();
|
|---|
| 257 | iter != molecules.end();
|
|---|
| 258 | iter++) {
|
|---|
| 259 |
|
|---|
| 260 | addItem(*iter);
|
|---|
| 261 | }
|
|---|
| 262 | dirty = false;
|
|---|
| 263 | clearing = false;
|
|---|
| 264 | }
|
|---|
| 265 |
|
|---|
| 266 | /*
|
|---|
| 267 | void QtMoleculeList::paintEvent(QPaintEvent * event)
|
|---|
| 268 | {
|
|---|
| 269 | if (dirty)
|
|---|
| 270 | refill();
|
|---|
| 271 | QTreeWidget::paintEvent(event);
|
|---|
| 272 | }
|
|---|
| 273 | */
|
|---|
| 274 |
|
|---|
| 275 | void QtMoleculeList::subjectKilled(Observable *publisher) {
|
|---|
| 276 | }
|
|---|
| 277 |
|
|---|
| 278 | /*
|
|---|
| 279 | void QtMoleculeList::visibilityChanged(QStandardItem* item, int column)
|
|---|
| 280 | {
|
|---|
| 281 | if ((!changing) && (!clearing) && (!ChangingChildrensVisibility))
|
|---|
| 282 | if (column == VISIBILITY) {
|
|---|
| 283 | const moleculeId_t molid = item->data(0, Qt::UserRole).toInt();
|
|---|
| 284 | const bool visible = item->checkState(VISIBILITY);
|
|---|
| 285 | if (molid != (unsigned int)-1) { // molecule item
|
|---|
| 286 | World::MoleculeConstIterator moliter =
|
|---|
| 287 | const_cast<const World &>(World::getInstance()).getMoleculeIter(MoleculeById(molid));
|
|---|
| 288 | const molecule * const _molecule = *moliter;
|
|---|
| 289 | ASSERT( _molecule != NULL,
|
|---|
| 290 | "QtMoleculeList::visibilityChanged() - molecule with id "
|
|---|
| 291 | +toString(molid)+" is not known to World.");
|
|---|
| 292 | const std::string &molecule_formula = _molecule->getFormula().toString();
|
|---|
| 293 | ASSERT( FormulaVisibilityCountMap.count(molecule_formula) != 0,
|
|---|
| 294 | "QtMoleculeList::visibilityChanged() - molecule with formula " +molecule_formula
|
|---|
| 295 | +" is not present in FormulaVisibilityCountMap.");
|
|---|
| 296 |
|
|---|
| 297 | // get parent
|
|---|
| 298 | QTreeWidgetItem *groupItem = item->parent();
|
|---|
| 299 | ASSERT( groupItem != NULL,
|
|---|
| 300 | "QtMoleculeList::visibilityChanged() - item with id "+toString(molid)
|
|---|
| 301 | +" has not parent?");
|
|---|
| 302 | // check whether we have to set the group item
|
|---|
| 303 |
|
|---|
| 304 | ChangingChildrensVisibility = true;
|
|---|
| 305 | if (visible) {
|
|---|
| 306 | ++(FormulaVisibilityCountMap[molecule_formula]);
|
|---|
| 307 | // compare with occurence/total number of molecules
|
|---|
| 308 | if (FormulaVisibilityCountMap[molecule_formula] ==
|
|---|
| 309 | (unsigned int)(groupItem->text(OCCURRENCE).toInt()))
|
|---|
| 310 | groupItem->setCheckState(VISIBILITY, Qt::Checked);
|
|---|
| 311 | } else {
|
|---|
| 312 | --(FormulaVisibilityCountMap[molecule_formula]);
|
|---|
| 313 | // none selected anymore?
|
|---|
| 314 | if (FormulaVisibilityCountMap[molecule_formula] == 0)
|
|---|
| 315 | groupItem->setCheckState(VISIBILITY, Qt::Unchecked);
|
|---|
| 316 | }
|
|---|
| 317 | ChangingChildrensVisibility = false;
|
|---|
| 318 |
|
|---|
| 319 | emit moleculesVisibilityChanged(molid, visible);
|
|---|
| 320 |
|
|---|
| 321 | } else { // group item
|
|---|
| 322 |
|
|---|
| 323 | // go through all children, but don't enter for groupItem once more
|
|---|
| 324 | ChangingChildrensVisibility = true;
|
|---|
| 325 | for (int i=0;i<item->childCount();++i) {
|
|---|
| 326 | QTreeWidgetItem *molItem = item->child(i);
|
|---|
| 327 | const moleculeId_t molid = molItem->data(0, Qt::UserRole).toInt();
|
|---|
| 328 | ASSERT( molid != (unsigned int)-1,
|
|---|
| 329 | "QtMoleculeList::visibilityChanged() - to child with index"
|
|---|
| 330 | +toString(i)+" there is no molecule?");
|
|---|
| 331 | molItem->setCheckState(VISIBILITY, visible ? Qt::Checked : Qt::Unchecked);
|
|---|
| 332 |
|
|---|
| 333 | // emit signal
|
|---|
| 334 | emit moleculesVisibilityChanged(molid, visible);
|
|---|
| 335 | }
|
|---|
| 336 | // set current number of visible children
|
|---|
| 337 | const std::string molecule_formula =
|
|---|
| 338 | item->text(FORMULA).toStdString();
|
|---|
| 339 | FormulaVisibilityCountMap[molecule_formula] =
|
|---|
| 340 | visible ? item->text(OCCURRENCE).toInt() : 0;
|
|---|
| 341 |
|
|---|
| 342 | ChangingChildrensVisibility = false;
|
|---|
| 343 | }
|
|---|
| 344 | }
|
|---|
| 345 | }
|
|---|
| 346 | */
|
|---|
| 347 |
|
|---|
| 348 | void QtMoleculeList::moleculeChanged() {
|
|---|
| 349 | /*int idx = verticalHeaderItem(row)->data(Qt::UserRole).toInt();
|
|---|
| 350 | molecule *mol = molecules->ReturnIndex(idx);
|
|---|
| 351 | string cellValue = item(row,NAME)->text().toStdString();
|
|---|
| 352 | if(mol->getName() != cellValue && cellValue !="") {
|
|---|
| 353 | mol->setName(cellValue);
|
|---|
| 354 | }
|
|---|
| 355 | else if(cellValue==""){
|
|---|
| 356 | item(row,NAME)->setText(QString(mol->getName().c_str()));
|
|---|
| 357 | }*/
|
|---|
| 358 | }
|
|---|
| 359 |
|
|---|
| 360 | /*
|
|---|
| 361 | void QtMoleculeList::rowsSelected(const QItemSelection & selected, const QItemSelection & deselected){
|
|---|
| 362 |
|
|---|
| 363 | if (clearing)
|
|---|
| 364 | return;
|
|---|
| 365 | if (selecting)
|
|---|
| 366 | return;
|
|---|
| 367 | selecting = true;
|
|---|
| 368 |
|
|---|
| 369 | // Select all molecules which belong to newly selected rows.
|
|---|
| 370 | QModelIndex index;
|
|---|
| 371 | {
|
|---|
| 372 | QModelIndexList items = selected.indexes();
|
|---|
| 373 | molids_t ids;
|
|---|
| 374 | ids.reserve(items.size());
|
|---|
| 375 | foreach (index, items)
|
|---|
| 376 | if (index.column() == 0){
|
|---|
| 377 | int mol_id = model()->data(index, Qt::UserRole).toInt();
|
|---|
| 378 | if (mol_id < 0)
|
|---|
| 379 | continue;
|
|---|
| 380 | ids.push_back(mol_id);
|
|---|
| 381 | //std::cout << "select molecule" << std::endl;
|
|---|
| 382 | }
|
|---|
| 383 | MoleCuilder::SelectionMoleculeById(ids);
|
|---|
| 384 | }
|
|---|
| 385 |
|
|---|
| 386 | // Unselect all molecules which belong to newly unselected rows.
|
|---|
| 387 | {
|
|---|
| 388 | QModelIndexList items = deselected.indexes();
|
|---|
| 389 | molids_t ids;
|
|---|
| 390 | ids.reserve(items.size());
|
|---|
| 391 | foreach (index, items)
|
|---|
| 392 | if (index.column() == 0){
|
|---|
| 393 | int mol_id = model()->data(index, Qt::UserRole).toInt();
|
|---|
| 394 | if (mol_id < 0)
|
|---|
| 395 | continue;
|
|---|
| 396 | //std::cout << "unselect molecule" << std::endl;
|
|---|
| 397 | ids.push_back(mol_id);
|
|---|
| 398 | }
|
|---|
| 399 | MoleCuilder::SelectionNotMoleculeById(ids);
|
|---|
| 400 | }
|
|---|
| 401 |
|
|---|
| 402 | selecting = false;
|
|---|
| 403 | }
|
|---|
| 404 |
|
|---|
| 405 | */
|
|---|