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 <iostream>
|
---|
23 |
|
---|
24 | #include "CodePatterns/MemDebug.hpp"
|
---|
25 |
|
---|
26 | #include "Atom/atom.hpp"
|
---|
27 | #include "Formula.hpp"
|
---|
28 | #include "molecule.hpp"
|
---|
29 | #include "MoleculeListClass.hpp"
|
---|
30 | #include "Actions/SelectionAction/Molecules/MoleculeByIdAction.hpp"
|
---|
31 | #include "Actions/SelectionAction/Molecules/NotMoleculeByIdAction.hpp"
|
---|
32 |
|
---|
33 | using namespace std;
|
---|
34 |
|
---|
35 | // maybe this should go with the definition of molecules
|
---|
36 |
|
---|
37 | // some attributes need to be easier to find for molecules
|
---|
38 | // these attributes are skiped so far
|
---|
39 | const int QtWorldView::COLUMNCOUNT = COLUMNTYPES_MAX;
|
---|
40 | const char *QtWorldView::COLUMNNAMES[QtWorldView::COLUMNCOUNT]={"Name","Atoms","Formula","Occurrence"/*,"Size"*/};
|
---|
41 |
|
---|
42 | QtWorldView::QtWorldView(QWidget * _parent) :
|
---|
43 | QTreeWidget (_parent),
|
---|
44 | Observer("QtWorldView")
|
---|
45 | {
|
---|
46 | setColumnCount(COLUMNCOUNT);
|
---|
47 | setSelectionMode(QAbstractItemView::MultiSelection);
|
---|
48 |
|
---|
49 | QStringList header;
|
---|
50 | for(int i=0; i<COLUMNCOUNT;++i)
|
---|
51 | header << COLUMNNAMES[i];
|
---|
52 | setHeaderLabels(header);
|
---|
53 |
|
---|
54 | molecules = World::getInstance().getMolecules();
|
---|
55 | molecules->signOn(this);
|
---|
56 |
|
---|
57 | dirty = true;
|
---|
58 | clearing = false;
|
---|
59 | refill();
|
---|
60 |
|
---|
61 | //connect(this,SIGNAL(cellChanged(int,int)),this,SLOT(moleculeChanged(int,int)));
|
---|
62 | connect(this,SIGNAL(itemSelectionChanged()),this,SLOT(rowSelected()));
|
---|
63 |
|
---|
64 | }
|
---|
65 |
|
---|
66 | QtWorldView::~QtWorldView()
|
---|
67 | {
|
---|
68 | molecules->signOff(this);
|
---|
69 | }
|
---|
70 |
|
---|
71 | void QtWorldView::update(Observable *publisher) {
|
---|
72 |
|
---|
73 | dirty = true;
|
---|
74 |
|
---|
75 | // force an update from Qt...
|
---|
76 | clearing = true;
|
---|
77 | clear();
|
---|
78 | clearing = false;
|
---|
79 | }
|
---|
80 |
|
---|
81 | void QtWorldView::refill() {
|
---|
82 | int numMolecules = molecules->ListOfMolecules.size();
|
---|
83 | clear();
|
---|
84 | molSelection.resize(numMolecules);
|
---|
85 |
|
---|
86 | // list of (unique) formulas in the world
|
---|
87 | std::vector<Formula> formula;
|
---|
88 |
|
---|
89 | int i;
|
---|
90 | MoleculeList::iterator iter;
|
---|
91 | for(iter = molecules->ListOfMolecules.begin(),i=0;
|
---|
92 | iter != molecules->ListOfMolecules.end();
|
---|
93 | ++i,++iter) {
|
---|
94 |
|
---|
95 | // find group if already in list
|
---|
96 | QTreeWidgetItem *groupItem = NULL;
|
---|
97 | for (unsigned int j=0;j<formula.size();j++)
|
---|
98 | if ((*iter)->getFormula() == formula[j]){
|
---|
99 | groupItem = topLevelItem(j);
|
---|
100 | break;
|
---|
101 | }
|
---|
102 |
|
---|
103 | // new molecule type -> create new group
|
---|
104 | if (!groupItem){
|
---|
105 | formula.push_back((*iter)->getFormula());
|
---|
106 | groupItem = new QTreeWidgetItem(this);
|
---|
107 | groupItem->setText(0, QString((*iter)->getName().c_str()));
|
---|
108 | groupItem->setText(1, QString::number((*iter)->getAtomCount()));
|
---|
109 | groupItem->setText(2, QString((*iter)->getFormula().toString().c_str()));
|
---|
110 | groupItem->setText(3, "0");
|
---|
111 | }
|
---|
112 |
|
---|
113 | // add molecule
|
---|
114 | QTreeWidgetItem *molItem = new QTreeWidgetItem(groupItem);
|
---|
115 | molItem->setText(0, QString((*iter)->getName().c_str()));
|
---|
116 | molItem->setText(1, QString::number((*iter)->getAtomCount()));
|
---|
117 | molItem->setText(2, QString((*iter)->getFormula().toString().c_str()));
|
---|
118 | const int index = (*iter)->IndexNr;
|
---|
119 | molItem->setData(0, Qt::UserRole, QVariant(index));
|
---|
120 | molItem->setSelected(World::getInstance().isSelected(*iter));
|
---|
121 |
|
---|
122 |
|
---|
123 | // increase group occurrence
|
---|
124 | int count = groupItem->text(3).toInt() + 1;
|
---|
125 | groupItem->setText(3, QString::number(count));
|
---|
126 |
|
---|
127 | //molSelection[i]=nameWidget->isSelected();
|
---|
128 | }
|
---|
129 | dirty = false;
|
---|
130 | }
|
---|
131 |
|
---|
132 | void QtWorldView::paintEvent(QPaintEvent * event)
|
---|
133 | {
|
---|
134 | if (dirty)
|
---|
135 | refill();
|
---|
136 | QTreeWidget::paintEvent(event);
|
---|
137 | }
|
---|
138 |
|
---|
139 | void QtWorldView::subjectKilled(Observable *publisher) {
|
---|
140 | }
|
---|
141 |
|
---|
142 | void QtWorldView::moleculeChanged() {
|
---|
143 | /*int idx = verticalHeaderItem(row)->data(Qt::UserRole).toInt();
|
---|
144 | molecule *mol = molecules->ReturnIndex(idx);
|
---|
145 | string cellValue = item(row,NAME)->text().toStdString();
|
---|
146 | if(mol->getName() != cellValue && cellValue !="") {
|
---|
147 | mol->setName(cellValue);
|
---|
148 | }
|
---|
149 | else if(cellValue==""){
|
---|
150 | item(row,NAME)->setText(QString(mol->getName().c_str()));
|
---|
151 | }*/
|
---|
152 | }
|
---|
153 |
|
---|
154 |
|
---|
155 | void QtWorldView::rowSelected(){
|
---|
156 |
|
---|
157 | if (clearing)
|
---|
158 | return;
|
---|
159 |
|
---|
160 | // lookup all molecules in the treeWidget
|
---|
161 | for (int i=0;i<topLevelItemCount();i++){
|
---|
162 | QTreeWidgetItem *top = topLevelItem(i);
|
---|
163 | for (int j=0;j<top->childCount();j++){
|
---|
164 |
|
---|
165 | // molecules are 1 level below the top
|
---|
166 | QTreeWidgetItem *molItem = top->child(j);
|
---|
167 |
|
---|
168 | // molecule index stored as user data
|
---|
169 | int index = molItem->data(0, Qt::UserRole).toInt();
|
---|
170 | molecule *mol = molecules->ReturnIndex(index);
|
---|
171 | ASSERT(mol, "QtWorldView::rowSelected()");
|
---|
172 |
|
---|
173 | // selection changed by user?
|
---|
174 | bool molSelectedWorld = World::getInstance().isSelected(mol);
|
---|
175 | bool molSelectedList = molItem->isSelected();
|
---|
176 | //std::cout << molSelectedWorld << " " << molSelectedList << std::endl;
|
---|
177 |
|
---|
178 | if (molSelectedWorld != molSelectedList){
|
---|
179 |
|
---|
180 | // apply new selection state
|
---|
181 | if (molSelectedList){
|
---|
182 | //std::cout << "select molecule" << std::endl;
|
---|
183 | MoleCuilder::SelectionMoleculeById(mol->getId());
|
---|
184 | }else{
|
---|
185 | //std::cout << "unselect molecule" << std::endl;
|
---|
186 | MoleCuilder::SelectionNotMoleculeById(mol->getId());
|
---|
187 | }
|
---|
188 | }
|
---|
189 | }
|
---|
190 | }
|
---|
191 | }
|
---|