[bcf653] | 1 | /*
|
---|
| 2 | * Project: MoleCuilder
|
---|
| 3 | * Description: creates and alters molecular systems
|
---|
[0aa122] | 4 | * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
|
---|
[bcf653] | 5 | * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
|
---|
| 6 | */
|
---|
| 7 |
|
---|
[b47bfc] | 8 | /*
|
---|
[0eb7bf3] | 9 | * QtMoleculeList.cpp
|
---|
[b47bfc] | 10 | *
|
---|
| 11 | * Created on: Jan 21, 2010
|
---|
| 12 | * Author: crueger
|
---|
| 13 | */
|
---|
| 14 |
|
---|
[bf3817] | 15 | // include config.h
|
---|
| 16 | #ifdef HAVE_CONFIG_H
|
---|
| 17 | #include <config.h>
|
---|
| 18 | #endif
|
---|
[bbbad5] | 19 |
|
---|
[0eb7bf3] | 20 | #include "Views/Qt4/QtMoleculeList.hpp"
|
---|
[b47bfc] | 21 |
|
---|
[41815a3] | 22 | #include <QMetaMethod>
|
---|
| 23 |
|
---|
[b47bfc] | 24 | #include <iostream>
|
---|
| 25 |
|
---|
[ad011c] | 26 | #include "CodePatterns/MemDebug.hpp"
|
---|
[bbbad5] | 27 |
|
---|
[6f0841] | 28 | #include "Atom/atom.hpp"
|
---|
[26cf17] | 29 | #include "Formula.hpp"
|
---|
[b47bfc] | 30 | #include "molecule.hpp"
|
---|
[42127c] | 31 | #include "MoleculeListClass.hpp"
|
---|
[b14efe] | 32 | #include "Actions/SelectionAction/Molecules/MoleculeByIdAction.hpp"
|
---|
| 33 | #include "Actions/SelectionAction/Molecules/NotMoleculeByIdAction.hpp"
|
---|
[b47bfc] | 34 |
|
---|
| 35 | using namespace std;
|
---|
| 36 |
|
---|
| 37 | // maybe this should go with the definition of molecules
|
---|
| 38 |
|
---|
| 39 | // some attributes need to be easier to find for molecules
|
---|
[6cab535] | 40 | // these attributes are skipped so far
|
---|
[0eb7bf3] | 41 | const int QtMoleculeList::COLUMNCOUNT = COLUMNTYPES_MAX;
|
---|
| 42 | const char *QtMoleculeList::COLUMNNAMES[QtMoleculeList::COLUMNCOUNT]={"Name","Atoms","Formula","Occurrence"/*,"Size"*/};
|
---|
[b47bfc] | 43 |
|
---|
[0eb7bf3] | 44 | QtMoleculeList::QtMoleculeList(QWidget * _parent) :
|
---|
[79b59b] | 45 | QTreeWidget (_parent),
|
---|
[0eb7bf3] | 46 | Observer("QtMoleculeList")
|
---|
[b47bfc] | 47 | {
|
---|
| 48 | setColumnCount(COLUMNCOUNT);
|
---|
[79b59b] | 49 | setSelectionMode(QAbstractItemView::MultiSelection);
|
---|
[b47bfc] | 50 |
|
---|
[79b59b] | 51 | QStringList header;
|
---|
| 52 | for(int i=0; i<COLUMNCOUNT;++i)
|
---|
| 53 | header << COLUMNNAMES[i];
|
---|
| 54 | setHeaderLabels(header);
|
---|
[b47bfc] | 55 |
|
---|
[41815a3] | 56 | World::getInstance().signOn(this);//, World::MoleculeInserted);
|
---|
| 57 | //World::getInstance().signOn(this, World::MoleculeRemoved);
|
---|
| 58 |
|
---|
| 59 |
|
---|
[a39006] | 60 | dirty = true;
|
---|
[99e8ea] | 61 | clearing = false;
|
---|
[41815a3] | 62 | selecting = false;
|
---|
[a39006] | 63 | refill();
|
---|
[b47bfc] | 64 |
|
---|
[79b59b] | 65 | //connect(this,SIGNAL(cellChanged(int,int)),this,SLOT(moleculeChanged(int,int)));
|
---|
[41815a3] | 66 | connect(selectionModel(),SIGNAL(selectionChanged(QItemSelection, QItemSelection)),this,SLOT(rowsSelected(QItemSelection, QItemSelection)));
|
---|
[b47bfc] | 67 |
|
---|
| 68 | }
|
---|
| 69 |
|
---|
[0eb7bf3] | 70 | QtMoleculeList::~QtMoleculeList()
|
---|
[b47bfc] | 71 | {
|
---|
[41815a3] | 72 | World::getInstance().signOff(this);//, World::MoleculeInserted);
|
---|
| 73 | //World::getInstance().signOff(this, World::MoleculeRemoved);
|
---|
[b47bfc] | 74 | }
|
---|
| 75 |
|
---|
[0eb7bf3] | 76 | void QtMoleculeList::update(Observable *publisher) {
|
---|
[a39006] | 77 |
|
---|
[41815a3] | 78 | if (selecting)
|
---|
| 79 | return;
|
---|
| 80 |
|
---|
[a39006] | 81 | dirty = true;
|
---|
| 82 |
|
---|
| 83 | // force an update from Qt...
|
---|
[99e8ea] | 84 | clearing = true;
|
---|
[a39006] | 85 | clear();
|
---|
[99e8ea] | 86 | clearing = false;
|
---|
[a39006] | 87 | }
|
---|
| 88 |
|
---|
[0eb7bf3] | 89 | void QtMoleculeList::refill() {
|
---|
[6cab535] | 90 | clearing = true;
|
---|
[41815a3] | 91 | const std::vector<molecule*> &molecules = World::getInstance().getAllMolecules();
|
---|
| 92 |
|
---|
[79b59b] | 93 | clear();
|
---|
| 94 |
|
---|
| 95 | // list of (unique) formulas in the world
|
---|
| 96 | std::vector<Formula> formula;
|
---|
| 97 |
|
---|
[41815a3] | 98 | for (std::vector<molecule*>::const_iterator iter = molecules.begin();
|
---|
| 99 | iter != molecules.end();
|
---|
| 100 | iter++) {
|
---|
[b47bfc] | 101 |
|
---|
[79b59b] | 102 | // find group if already in list
|
---|
| 103 | QTreeWidgetItem *groupItem = NULL;
|
---|
| 104 | for (unsigned int j=0;j<formula.size();j++)
|
---|
| 105 | if ((*iter)->getFormula() == formula[j]){
|
---|
| 106 | groupItem = topLevelItem(j);
|
---|
| 107 | break;
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | // new molecule type -> create new group
|
---|
| 111 | if (!groupItem){
|
---|
| 112 | formula.push_back((*iter)->getFormula());
|
---|
| 113 | groupItem = new QTreeWidgetItem(this);
|
---|
| 114 | groupItem->setText(0, QString((*iter)->getName().c_str()));
|
---|
| 115 | groupItem->setText(1, QString::number((*iter)->getAtomCount()));
|
---|
| 116 | groupItem->setText(2, QString((*iter)->getFormula().toString().c_str()));
|
---|
| 117 | groupItem->setText(3, "0");
|
---|
[41815a3] | 118 | groupItem->setData(0, Qt::UserRole, QVariant(-1));
|
---|
[79b59b] | 119 | }
|
---|
| 120 |
|
---|
| 121 | // add molecule
|
---|
| 122 | QTreeWidgetItem *molItem = new QTreeWidgetItem(groupItem);
|
---|
| 123 | molItem->setText(0, QString((*iter)->getName().c_str()));
|
---|
| 124 | molItem->setText(1, QString::number((*iter)->getAtomCount()));
|
---|
| 125 | molItem->setText(2, QString((*iter)->getFormula().toString().c_str()));
|
---|
[41815a3] | 126 | const int index = (*iter)->getId();
|
---|
[79b59b] | 127 | molItem->setData(0, Qt::UserRole, QVariant(index));
|
---|
[b14efe] | 128 | molItem->setSelected(World::getInstance().isSelected(*iter));
|
---|
[79b59b] | 129 |
|
---|
| 130 |
|
---|
| 131 | // increase group occurrence
|
---|
| 132 | int count = groupItem->text(3).toInt() + 1;
|
---|
| 133 | groupItem->setText(3, QString::number(count));
|
---|
[b47bfc] | 134 | }
|
---|
[a39006] | 135 | dirty = false;
|
---|
[6cab535] | 136 | clearing = false;
|
---|
[a39006] | 137 | }
|
---|
| 138 |
|
---|
[0eb7bf3] | 139 | void QtMoleculeList::paintEvent(QPaintEvent * event)
|
---|
[a39006] | 140 | {
|
---|
| 141 | if (dirty)
|
---|
| 142 | refill();
|
---|
| 143 | QTreeWidget::paintEvent(event);
|
---|
[b47bfc] | 144 | }
|
---|
| 145 |
|
---|
[0eb7bf3] | 146 | void QtMoleculeList::subjectKilled(Observable *publisher) {
|
---|
[b47bfc] | 147 | }
|
---|
| 148 |
|
---|
[0eb7bf3] | 149 | void QtMoleculeList::moleculeChanged() {
|
---|
[79b59b] | 150 | /*int idx = verticalHeaderItem(row)->data(Qt::UserRole).toInt();
|
---|
[b47bfc] | 151 | molecule *mol = molecules->ReturnIndex(idx);
|
---|
| 152 | string cellValue = item(row,NAME)->text().toStdString();
|
---|
| 153 | if(mol->getName() != cellValue && cellValue !="") {
|
---|
| 154 | mol->setName(cellValue);
|
---|
| 155 | }
|
---|
| 156 | else if(cellValue==""){
|
---|
| 157 | item(row,NAME)->setText(QString(mol->getName().c_str()));
|
---|
[79b59b] | 158 | }*/
|
---|
[b47bfc] | 159 | }
|
---|
| 160 |
|
---|
[0eb7bf3] | 161 | void QtMoleculeList::rowsSelected(const QItemSelection & selected, const QItemSelection & deselected){
|
---|
[b14efe] | 162 |
|
---|
[99e8ea] | 163 | if (clearing)
|
---|
| 164 | return;
|
---|
[41815a3] | 165 | if (selecting)
|
---|
| 166 | return;
|
---|
| 167 | selecting = true;
|
---|
| 168 |
|
---|
| 169 | // Select all molecules which belong to newly selected rows.
|
---|
| 170 | QModelIndex index;
|
---|
| 171 | QModelIndexList items = selected.indexes();
|
---|
| 172 | foreach (index, items)
|
---|
| 173 | if (index.column() == 0){
|
---|
| 174 | int mol_id = model()->data(index, Qt::UserRole).toInt();
|
---|
| 175 | if (mol_id < 0)
|
---|
| 176 | continue;
|
---|
| 177 | //std::cout << "select molecule" << std::endl;
|
---|
| 178 | MoleCuilder::SelectionMoleculeById(mol_id);
|
---|
| 179 | }
|
---|
[99e8ea] | 180 |
|
---|
[41815a3] | 181 | // Unselect all molecules which belong to newly unselected rows.
|
---|
| 182 | items = deselected.indexes();
|
---|
| 183 | foreach (index, items)
|
---|
| 184 | if (index.column() == 0){
|
---|
| 185 | int mol_id = model()->data(index, Qt::UserRole).toInt();
|
---|
| 186 | if (mol_id < 0)
|
---|
| 187 | continue;
|
---|
| 188 | //std::cout << "unselect molecule" << std::endl;
|
---|
| 189 | MoleCuilder::SelectionNotMoleculeById(mol_id);
|
---|
[b47bfc] | 190 | }
|
---|
[41815a3] | 191 |
|
---|
| 192 | selecting = false;
|
---|
[b47bfc] | 193 | }
|
---|