1 | /*
|
---|
2 | * Project: MoleCuilder
|
---|
3 | * Description: creates and alters molecular systems
|
---|
4 | * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
|
---|
5 | * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
|
---|
6 | */
|
---|
7 |
|
---|
8 | /*
|
---|
9 | * QtWorldView.cpp
|
---|
10 | *
|
---|
11 | * Created on: Jan 21, 2010
|
---|
12 | * Author: crueger
|
---|
13 | */
|
---|
14 |
|
---|
15 | // include config.h
|
---|
16 | #ifdef HAVE_CONFIG_H
|
---|
17 | #include <config.h>
|
---|
18 | #endif
|
---|
19 |
|
---|
20 | #include "Views/Qt4/QtWorldView.hpp"
|
---|
21 |
|
---|
22 | #include <QMetaMethod>
|
---|
23 |
|
---|
24 | #include <iostream>
|
---|
25 |
|
---|
26 | #include "CodePatterns/MemDebug.hpp"
|
---|
27 |
|
---|
28 | #include "Atom/atom.hpp"
|
---|
29 | #include "Formula.hpp"
|
---|
30 | #include "molecule.hpp"
|
---|
31 | #include "MoleculeListClass.hpp"
|
---|
32 | #include "Actions/SelectionAction/Molecules/MoleculeByIdAction.hpp"
|
---|
33 | #include "Actions/SelectionAction/Molecules/NotMoleculeByIdAction.hpp"
|
---|
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
|
---|
41 | const int QtWorldView::COLUMNCOUNT = COLUMNTYPES_MAX;
|
---|
42 | const char *QtWorldView::COLUMNNAMES[QtWorldView::COLUMNCOUNT]={"Name","Atoms","Formula","Occurrence"/*,"Size"*/};
|
---|
43 |
|
---|
44 | QtWorldView::QtWorldView(QWidget * _parent) :
|
---|
45 | QTreeWidget (_parent),
|
---|
46 | Observer("QtWorldView")
|
---|
47 | {
|
---|
48 | setColumnCount(COLUMNCOUNT);
|
---|
49 | setSelectionMode(QAbstractItemView::MultiSelection);
|
---|
50 |
|
---|
51 | QStringList header;
|
---|
52 | for(int i=0; i<COLUMNCOUNT;++i)
|
---|
53 | header << COLUMNNAMES[i];
|
---|
54 | setHeaderLabels(header);
|
---|
55 |
|
---|
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 |
|
---|
64 |
|
---|
65 | dirty = true;
|
---|
66 | clearing = false;
|
---|
67 | selecting = false;
|
---|
68 | refill();
|
---|
69 |
|
---|
70 | //connect(this,SIGNAL(cellChanged(int,int)),this,SLOT(moleculeChanged(int,int)));
|
---|
71 | connect(selectionModel(),SIGNAL(selectionChanged(QItemSelection, QItemSelection)),this,SLOT(rowsSelected(QItemSelection, QItemSelection)));
|
---|
72 |
|
---|
73 | }
|
---|
74 |
|
---|
75 | QtWorldView::~QtWorldView()
|
---|
76 | {
|
---|
77 | World::getInstance().signOff(this);//, World::MoleculeInserted);
|
---|
78 | //World::getInstance().signOff(this, World::MoleculeRemoved);
|
---|
79 | }
|
---|
80 |
|
---|
81 | void QtWorldView::update(Observable *publisher) {
|
---|
82 |
|
---|
83 | if (selecting)
|
---|
84 | return;
|
---|
85 |
|
---|
86 | dirty = true;
|
---|
87 |
|
---|
88 | // force an update from Qt...
|
---|
89 | clearing = true;
|
---|
90 | clear();
|
---|
91 | clearing = false;
|
---|
92 | }
|
---|
93 |
|
---|
94 | void QtWorldView::refill() {
|
---|
95 | const std::vector<molecule*> &molecules = World::getInstance().getAllMolecules();
|
---|
96 |
|
---|
97 | clear();
|
---|
98 |
|
---|
99 | // list of (unique) formulas in the world
|
---|
100 | std::vector<Formula> formula;
|
---|
101 |
|
---|
102 | for (std::vector<molecule*>::const_iterator iter = molecules.begin();
|
---|
103 | iter != molecules.end();
|
---|
104 | iter++) {
|
---|
105 |
|
---|
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");
|
---|
122 | groupItem->setData(0, Qt::UserRole, QVariant(-1));
|
---|
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()));
|
---|
130 | const int index = (*iter)->getId();
|
---|
131 | molItem->setData(0, Qt::UserRole, QVariant(index));
|
---|
132 | molItem->setSelected(World::getInstance().isSelected(*iter));
|
---|
133 |
|
---|
134 |
|
---|
135 | // increase group occurrence
|
---|
136 | int count = groupItem->text(3).toInt() + 1;
|
---|
137 | groupItem->setText(3, QString::number(count));
|
---|
138 | }
|
---|
139 | dirty = false;
|
---|
140 | }
|
---|
141 |
|
---|
142 | void QtWorldView::paintEvent(QPaintEvent * event)
|
---|
143 | {
|
---|
144 | if (dirty)
|
---|
145 | refill();
|
---|
146 | QTreeWidget::paintEvent(event);
|
---|
147 | }
|
---|
148 |
|
---|
149 | void QtWorldView::subjectKilled(Observable *publisher) {
|
---|
150 | }
|
---|
151 |
|
---|
152 | void QtWorldView::moleculeChanged() {
|
---|
153 | /*int idx = verticalHeaderItem(row)->data(Qt::UserRole).toInt();
|
---|
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()));
|
---|
161 | }*/
|
---|
162 | }
|
---|
163 |
|
---|
164 | void QtWorldView::rowsSelected(const QItemSelection & selected, const QItemSelection & deselected){
|
---|
165 |
|
---|
166 | if (clearing)
|
---|
167 | return;
|
---|
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 | }
|
---|
183 |
|
---|
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);
|
---|
193 | }
|
---|
194 |
|
---|
195 | selecting = false;
|
---|
196 | }
|
---|