[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 | /*
|
---|
[4cf323d] | 9 | * QtWorldView.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 |
|
---|
[4cf323d] | 20 | #include "Views/Qt4/QtWorldView.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
|
---|
| 40 | // these attributes are skiped so far
|
---|
[4cf323d] | 41 | const int QtWorldView::COLUMNCOUNT = COLUMNTYPES_MAX;
|
---|
[79b59b] | 42 | const char *QtWorldView::COLUMNNAMES[QtWorldView::COLUMNCOUNT]={"Name","Atoms","Formula","Occurrence"/*,"Size"*/};
|
---|
[b47bfc] | 43 |
|
---|
[4cf323d] | 44 | QtWorldView::QtWorldView(QWidget * _parent) :
|
---|
[79b59b] | 45 | QTreeWidget (_parent),
|
---|
[4cf323d] | 46 | Observer("QtWorldView")
|
---|
[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 |
|
---|
| 60 | //for (int i=0;i<metaObject()->methodCount();i++)
|
---|
| 61 | // std::cout << metaObject()->method(i).signature() << " - " << metaObject()->method(i).methodType() << "\n";
|
---|
| 62 |
|
---|
| 63 |
|
---|
[a39006] | 64 |
|
---|
| 65 | dirty = true;
|
---|
[99e8ea] | 66 | clearing = false;
|
---|
[41815a3] | 67 | selecting = false;
|
---|
[a39006] | 68 | refill();
|
---|
[b47bfc] | 69 |
|
---|
[79b59b] | 70 | //connect(this,SIGNAL(cellChanged(int,int)),this,SLOT(moleculeChanged(int,int)));
|
---|
[41815a3] | 71 | connect(selectionModel(),SIGNAL(selectionChanged(QItemSelection, QItemSelection)),this,SLOT(rowsSelected(QItemSelection, QItemSelection)));
|
---|
[b47bfc] | 72 |
|
---|
| 73 | }
|
---|
| 74 |
|
---|
[4cf323d] | 75 | QtWorldView::~QtWorldView()
|
---|
[b47bfc] | 76 | {
|
---|
[41815a3] | 77 | World::getInstance().signOff(this);//, World::MoleculeInserted);
|
---|
| 78 | //World::getInstance().signOff(this, World::MoleculeRemoved);
|
---|
[b47bfc] | 79 | }
|
---|
| 80 |
|
---|
[4cf323d] | 81 | void QtWorldView::update(Observable *publisher) {
|
---|
[a39006] | 82 |
|
---|
[41815a3] | 83 | if (selecting)
|
---|
| 84 | return;
|
---|
| 85 |
|
---|
[a39006] | 86 | dirty = true;
|
---|
| 87 |
|
---|
| 88 | // force an update from Qt...
|
---|
[99e8ea] | 89 | clearing = true;
|
---|
[a39006] | 90 | clear();
|
---|
[99e8ea] | 91 | clearing = false;
|
---|
[a39006] | 92 | }
|
---|
| 93 |
|
---|
| 94 | void QtWorldView::refill() {
|
---|
[41815a3] | 95 | const std::vector<molecule*> &molecules = World::getInstance().getAllMolecules();
|
---|
| 96 |
|
---|
[79b59b] | 97 | clear();
|
---|
| 98 |
|
---|
| 99 | // list of (unique) formulas in the world
|
---|
| 100 | std::vector<Formula> formula;
|
---|
| 101 |
|
---|
[41815a3] | 102 | for (std::vector<molecule*>::const_iterator iter = molecules.begin();
|
---|
| 103 | iter != molecules.end();
|
---|
| 104 | iter++) {
|
---|
[b47bfc] | 105 |
|
---|
[79b59b] | 106 | // find group if already in list
|
---|
| 107 | QTreeWidgetItem *groupItem = NULL;
|
---|
| 108 | for (unsigned int j=0;j<formula.size();j++)
|
---|
| 109 | if ((*iter)->getFormula() == formula[j]){
|
---|
| 110 | groupItem = topLevelItem(j);
|
---|
| 111 | break;
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | // new molecule type -> create new group
|
---|
| 115 | if (!groupItem){
|
---|
| 116 | formula.push_back((*iter)->getFormula());
|
---|
| 117 | groupItem = new QTreeWidgetItem(this);
|
---|
| 118 | groupItem->setText(0, QString((*iter)->getName().c_str()));
|
---|
| 119 | groupItem->setText(1, QString::number((*iter)->getAtomCount()));
|
---|
| 120 | groupItem->setText(2, QString((*iter)->getFormula().toString().c_str()));
|
---|
| 121 | groupItem->setText(3, "0");
|
---|
[41815a3] | 122 | groupItem->setData(0, Qt::UserRole, QVariant(-1));
|
---|
[79b59b] | 123 | }
|
---|
| 124 |
|
---|
| 125 | // add molecule
|
---|
| 126 | QTreeWidgetItem *molItem = new QTreeWidgetItem(groupItem);
|
---|
| 127 | molItem->setText(0, QString((*iter)->getName().c_str()));
|
---|
| 128 | molItem->setText(1, QString::number((*iter)->getAtomCount()));
|
---|
| 129 | molItem->setText(2, QString((*iter)->getFormula().toString().c_str()));
|
---|
[41815a3] | 130 | const int index = (*iter)->getId();
|
---|
[79b59b] | 131 | molItem->setData(0, Qt::UserRole, QVariant(index));
|
---|
[b14efe] | 132 | molItem->setSelected(World::getInstance().isSelected(*iter));
|
---|
[79b59b] | 133 |
|
---|
| 134 |
|
---|
| 135 | // increase group occurrence
|
---|
| 136 | int count = groupItem->text(3).toInt() + 1;
|
---|
| 137 | groupItem->setText(3, QString::number(count));
|
---|
[b47bfc] | 138 | }
|
---|
[a39006] | 139 | dirty = false;
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | void QtWorldView::paintEvent(QPaintEvent * event)
|
---|
| 143 | {
|
---|
| 144 | if (dirty)
|
---|
| 145 | refill();
|
---|
| 146 | QTreeWidget::paintEvent(event);
|
---|
[b47bfc] | 147 | }
|
---|
| 148 |
|
---|
[4cf323d] | 149 | void QtWorldView::subjectKilled(Observable *publisher) {
|
---|
[b47bfc] | 150 | }
|
---|
| 151 |
|
---|
[79b59b] | 152 | void QtWorldView::moleculeChanged() {
|
---|
| 153 | /*int idx = verticalHeaderItem(row)->data(Qt::UserRole).toInt();
|
---|
[b47bfc] | 154 | molecule *mol = molecules->ReturnIndex(idx);
|
---|
| 155 | string cellValue = item(row,NAME)->text().toStdString();
|
---|
| 156 | if(mol->getName() != cellValue && cellValue !="") {
|
---|
| 157 | mol->setName(cellValue);
|
---|
| 158 | }
|
---|
| 159 | else if(cellValue==""){
|
---|
| 160 | item(row,NAME)->setText(QString(mol->getName().c_str()));
|
---|
[79b59b] | 161 | }*/
|
---|
[b47bfc] | 162 | }
|
---|
| 163 |
|
---|
[41815a3] | 164 | void QtWorldView::rowsSelected(const QItemSelection & selected, const QItemSelection & deselected){
|
---|
[b14efe] | 165 |
|
---|
[99e8ea] | 166 | if (clearing)
|
---|
| 167 | return;
|
---|
[41815a3] | 168 | if (selecting)
|
---|
| 169 | return;
|
---|
| 170 | selecting = true;
|
---|
| 171 |
|
---|
| 172 | // Select all molecules which belong to newly selected rows.
|
---|
| 173 | QModelIndex index;
|
---|
| 174 | QModelIndexList items = selected.indexes();
|
---|
| 175 | foreach (index, items)
|
---|
| 176 | if (index.column() == 0){
|
---|
| 177 | int mol_id = model()->data(index, Qt::UserRole).toInt();
|
---|
| 178 | if (mol_id < 0)
|
---|
| 179 | continue;
|
---|
| 180 | //std::cout << "select molecule" << std::endl;
|
---|
| 181 | MoleCuilder::SelectionMoleculeById(mol_id);
|
---|
| 182 | }
|
---|
[99e8ea] | 183 |
|
---|
[41815a3] | 184 | // Unselect all molecules which belong to newly unselected rows.
|
---|
| 185 | items = deselected.indexes();
|
---|
| 186 | foreach (index, items)
|
---|
| 187 | if (index.column() == 0){
|
---|
| 188 | int mol_id = model()->data(index, Qt::UserRole).toInt();
|
---|
| 189 | if (mol_id < 0)
|
---|
| 190 | continue;
|
---|
| 191 | //std::cout << "unselect molecule" << std::endl;
|
---|
| 192 | MoleCuilder::SelectionNotMoleculeById(mol_id);
|
---|
[b47bfc] | 193 | }
|
---|
[41815a3] | 194 |
|
---|
| 195 | selecting = false;
|
---|
[b47bfc] | 196 | }
|
---|