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 | * QtElementList.cpp
|
---|
25 | *
|
---|
26 | * Created on: Mar 6, 2012
|
---|
27 | * Author: ankele
|
---|
28 | */
|
---|
29 |
|
---|
30 | // include config.h
|
---|
31 | #ifdef HAVE_CONFIG_H
|
---|
32 | #include <config.h>
|
---|
33 | #endif
|
---|
34 |
|
---|
35 | #include "Views/Qt4/ElementList/QtElementList.hpp"
|
---|
36 | #include "UIElements/Qt4/InstanceBoard/QtObservedInstanceBoard.hpp"
|
---|
37 |
|
---|
38 | #include <iostream>
|
---|
39 |
|
---|
40 | #include "CodePatterns/MemDebug.hpp"
|
---|
41 |
|
---|
42 | #include "Atom/atom.hpp"
|
---|
43 | #include "Atom/AtomObserver.hpp"
|
---|
44 |
|
---|
45 | #include "Element/element.hpp"
|
---|
46 | #include "Element/periodentafel.hpp"
|
---|
47 | #include "Descriptors/AtomTypeDescriptor.hpp"
|
---|
48 | #include <QAbstractItemView>
|
---|
49 | #include "Actions/SelectionAction/Atoms/AtomByElementAction.hpp"
|
---|
50 | #include "Actions/SelectionAction/Atoms/NotAtomByElementAction.hpp"
|
---|
51 |
|
---|
52 | using namespace std;
|
---|
53 |
|
---|
54 | const int QtElementList::COLUMNCOUNT = COLUMNTYPES_MAX;
|
---|
55 | const char *QtElementList::COLUMNNAMES[QtElementList::COLUMNCOUNT]={"Number","Name","Symbol","Mass","Occurrence"};
|
---|
56 |
|
---|
57 | QtElementList::QtElementList(QtObservedInstanceBoard *_board, QWidget * _parent) :
|
---|
58 | QTreeWidget (_parent),
|
---|
59 | Observer("QtElementList"),
|
---|
60 | board(_board)
|
---|
61 | {
|
---|
62 | setColumnCount(COLUMNCOUNT);
|
---|
63 | setSelectionMode(QAbstractItemView::MultiSelection);
|
---|
64 |
|
---|
65 | QStringList header;
|
---|
66 | for(int i=0; i<COLUMNCOUNT;++i)
|
---|
67 | header << COLUMNNAMES[i];
|
---|
68 | setHeaderLabels(header);
|
---|
69 |
|
---|
70 | {
|
---|
71 | periodentafel *&periode = World::getInstance().getPeriode();
|
---|
72 |
|
---|
73 | elementSelection.clear();
|
---|
74 |
|
---|
75 | int i;
|
---|
76 |
|
---|
77 | clear();
|
---|
78 | periodentafel::const_iterator iter;
|
---|
79 | for(iter = periode->begin(),i=0;
|
---|
80 | iter != periode->end();
|
---|
81 | ++i,++iter) {
|
---|
82 | const element *e = iter->second;
|
---|
83 | int count = 0;
|
---|
84 | count = const_cast<const World &>(World::getInstance()).
|
---|
85 | getAllAtoms(AtomByType(e)).size();
|
---|
86 |
|
---|
87 | QTreeWidgetItem *treeItem = new QTreeWidgetItem(this);
|
---|
88 | treeItem->setText(NUMBER, QString::number(e->getAtomicNumber()));
|
---|
89 | treeItem->setText(NAME, QString(e->getName().c_str()));
|
---|
90 | treeItem->setText(SYMBOL, QString(e->getSymbol().c_str()));
|
---|
91 | treeItem->setText(MASS, QString::number(e->getMass()));
|
---|
92 | setOccurrence(*treeItem, count);
|
---|
93 | elementSelection.push_back(treeItem->isSelected());
|
---|
94 | // insertTopLevelItem(e->getAtomicNumber()-1, treeItem);
|
---|
95 | }
|
---|
96 | }
|
---|
97 | dirty = true;
|
---|
98 |
|
---|
99 | AtomObserver::getInstance().signOn(this, atom::ElementChanged);
|
---|
100 |
|
---|
101 | connect(this,SIGNAL(itemSelectionChanged()),this,SLOT(rowSelected()));
|
---|
102 | connect(this,SIGNAL(needsRefill(const atomId_t)),this,SLOT(refill(const atomId_t)), Qt::QueuedConnection);
|
---|
103 | // connect(this,SIGNAL(changed()),this,SLOT(update()));
|
---|
104 |
|
---|
105 | }
|
---|
106 |
|
---|
107 | QtElementList::~QtElementList()
|
---|
108 | {
|
---|
109 | AtomObserver::getInstance().signOff(this, atom::ElementChanged);
|
---|
110 | }
|
---|
111 |
|
---|
112 | void QtElementList::update(Observable *publisher)
|
---|
113 | {
|
---|
114 | ASSERT(0, "QtElementList::update() - is not enlisted to any general update.");
|
---|
115 | }
|
---|
116 |
|
---|
117 |
|
---|
118 | void QtElementList::recieveNotification(Observable *publisher, Notification_ptr notification)
|
---|
119 | {
|
---|
120 | if (notification->getChannelNo() == atom::ElementChanged) {
|
---|
121 | dirty = true;
|
---|
122 |
|
---|
123 | emit needsRefill(dynamic_cast<atom *>(publisher)->getId());
|
---|
124 | } else
|
---|
125 | ASSERT(0, "QtElementList::recieveNotification() - we are not enlisted to any other instance's channel.");
|
---|
126 | }
|
---|
127 |
|
---|
128 | void QtElementList::updateElement(const QtObservedAtom::ptr &_atom)
|
---|
129 | {
|
---|
130 | const atomicNumber_t newelement = _atom->getAtomElement();
|
---|
131 | atomicNumber_t oldelement = -1;
|
---|
132 | const atomId_t atomid = _atom->getAtomIndex();
|
---|
133 | QTreeWidgetItem *newtreeItem = topLevelItem(newelement-1);
|
---|
134 | ASSERT( newtreeItem != NULL,
|
---|
135 | "QtElementList::updateElement() - new element item not present.");
|
---|
136 | // if (newtreeItem == NULL) {
|
---|
137 | // // add new item
|
---|
138 | // const element& e = _atom.getElement();
|
---|
139 | // newtreeItem = new QTreeWidgetItem();
|
---|
140 | // newtreeItem->setText(NUMBER, QString::number(e.getAtomicNumber()));
|
---|
141 | // newtreeItem->setText(NAME, QString(e.getName().c_str()));
|
---|
142 | // newtreeItem->setText(SYMBOL, QString(e.getSymbol().c_str()));
|
---|
143 | // newtreeItem->setText(MASS, QString::number(e.getMass()));
|
---|
144 | // setOccurrence(*newtreeItem, 0);
|
---|
145 | // elementSelection.push_back(newtreeItem->isSelected());
|
---|
146 | // insertTopLevelItem(newelement-1, newtreeItem);
|
---|
147 | // }
|
---|
148 | AtomElementMap_t::iterator iter = AtomElementMap.find(atomid);
|
---|
149 | if (iter == AtomElementMap.end()) {
|
---|
150 | AtomElementMap.insert( std::make_pair(atomid, -1));
|
---|
151 | iter = AtomElementMap.find(atomid);
|
---|
152 | } else
|
---|
153 | oldelement = iter->second;
|
---|
154 | iter->second = newelement;
|
---|
155 |
|
---|
156 | // reduce old occurence
|
---|
157 | if (oldelement != (atomicNumber_t)-1) {
|
---|
158 | QTreeWidgetItem *oldtreeItem = topLevelItem(oldelement-1);
|
---|
159 | ASSERT( oldtreeItem != NULL,
|
---|
160 | "QtElementList::updateElement() - old element item not present.");
|
---|
161 | const int count_old = oldtreeItem->text(OCCURRENCE).toInt();
|
---|
162 | setOccurrence(*oldtreeItem, count_old-1);
|
---|
163 | }
|
---|
164 | // increase new occurence
|
---|
165 | const int count_new = newtreeItem->text(OCCURRENCE).toInt();
|
---|
166 | setOccurrence(*newtreeItem, count_new+1);
|
---|
167 | }
|
---|
168 |
|
---|
169 | void QtElementList::setOccurrence(QTreeWidgetItem &_item, const int count)
|
---|
170 | {
|
---|
171 | ASSERT (count >= 0,
|
---|
172 | "QtElementList::setOccurrence() - count for an elment < 0.");
|
---|
173 |
|
---|
174 | if (count > 0) {
|
---|
175 | _item.setText(OCCURRENCE, QString::number(count));
|
---|
176 | if (_item.isDisabled())
|
---|
177 | _item.setDisabled(false);
|
---|
178 | } else {
|
---|
179 | _item.setText(OCCURRENCE, "none");
|
---|
180 | _item.setDisabled(true);
|
---|
181 | }
|
---|
182 | }
|
---|
183 |
|
---|
184 | void QtElementList::refill(const atomId_t _atomid)
|
---|
185 | {
|
---|
186 | refill_mutex.lock();
|
---|
187 |
|
---|
188 | const QtObservedAtom::ptr walker = board->getObservedAtom(_atomid);
|
---|
189 | if (walker)
|
---|
190 | updateElement(walker);
|
---|
191 | else {
|
---|
192 | AtomElementMap_t::iterator iter = AtomElementMap.find(_atomid);
|
---|
193 | if (iter != AtomElementMap.end()) {
|
---|
194 | QTreeWidgetItem *oldtreeItem = topLevelItem(iter->second-1);
|
---|
195 | const int count_old = oldtreeItem->text(OCCURRENCE).toInt();
|
---|
196 | setOccurrence(*oldtreeItem, count_old-1);
|
---|
197 |
|
---|
198 | AtomElementMap.erase(iter);
|
---|
199 | }
|
---|
200 | }
|
---|
201 |
|
---|
202 | refill_mutex.unlock();
|
---|
203 | }
|
---|
204 |
|
---|
205 | void QtElementList::paintEvent(QPaintEvent * event)
|
---|
206 | {
|
---|
207 | // if (dirty)
|
---|
208 | // refill();
|
---|
209 | QTreeWidget::paintEvent(event);
|
---|
210 | }
|
---|
211 |
|
---|
212 | void QtElementList::subjectKilled(Observable *publisher) {
|
---|
213 | }
|
---|
214 |
|
---|
215 |
|
---|
216 | void QtElementList::rowSelected()
|
---|
217 | {
|
---|
218 | //std::cout << "rowSelected\n";
|
---|
219 | periodentafel *&periode = World::getInstance().getPeriode();
|
---|
220 |
|
---|
221 | for (int i=0;i<topLevelItemCount();i++){
|
---|
222 | QTreeWidgetItem *item = topLevelItem(i);
|
---|
223 | bool newSelection = item->isSelected();
|
---|
224 | if (newSelection != elementSelection[i]){
|
---|
225 | elementSelection[i] = newSelection;
|
---|
226 |
|
---|
227 | int atomicNumber = item->text(NUMBER).toInt();
|
---|
228 | const element *e = periode->FindElement(atomicNumber);
|
---|
229 | if (newSelection)
|
---|
230 | MoleCuilder::SelectionAtomByElement(e);
|
---|
231 | else
|
---|
232 | MoleCuilder::SelectionNotAtomByElement(e);
|
---|
233 | }
|
---|
234 | }
|
---|
235 | }
|
---|
236 |
|
---|