1 | /*
|
---|
2 | * Project: MoleCuilder
|
---|
3 | * Description: creates and alters molecular systems
|
---|
4 | * Copyright (C) 2013 Frederik Heber. 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 | * QtHomologyList.cpp
|
---|
25 | *
|
---|
26 | * Created on: Jun 24, 2013
|
---|
27 | * Author: heber
|
---|
28 | */
|
---|
29 |
|
---|
30 | // include config.h
|
---|
31 | #ifdef HAVE_CONFIG_H
|
---|
32 | #include <config.h>
|
---|
33 | #endif
|
---|
34 |
|
---|
35 | #include "Views/Qt4/QtHomologyList.hpp"
|
---|
36 |
|
---|
37 | #include <iterator>
|
---|
38 | #include <iostream>
|
---|
39 |
|
---|
40 | #include <QAbstractItemView>
|
---|
41 | #include <QtGui/QTreeWidget>
|
---|
42 | #include <QtGui/QTabWidget>
|
---|
43 | #include <Qt/qsettings.h>
|
---|
44 | #include <Qt/qsplitter.h>
|
---|
45 | #include <Qt/qboxlayout.h>
|
---|
46 |
|
---|
47 | #include "CodePatterns/MemDebug.hpp"
|
---|
48 |
|
---|
49 | #include "CodePatterns/Log.hpp"
|
---|
50 |
|
---|
51 | #include "Fragmentation/Homology/HomologyContainer.hpp"
|
---|
52 | #include "FunctionApproximation/FunctionModel.hpp"
|
---|
53 | #include "FunctionApproximation/TrainingData.hpp"
|
---|
54 | #include "Potentials/CompoundPotential.hpp"
|
---|
55 | #include "Potentials/EmpiricalPotential.hpp"
|
---|
56 | #include "Potentials/InternalCoordinates/Coordinator.hpp"
|
---|
57 | #include "Potentials/PotentialRegistry.hpp"
|
---|
58 | #ifdef HAVE_QWT
|
---|
59 | #include "UIElements/Views/Qt4/Plotting/QSeisData.hpp"
|
---|
60 | #include "UIElements/Views/Qt4/Plotting/QSeisPageRegistry.hpp"
|
---|
61 | #include "UIElements/Views/Qt4/Plotting/QSeisPlotCurve.hpp"
|
---|
62 | #include "UIElements/Views/Qt4/Plotting/QSeisPlotPage.hpp"
|
---|
63 | #include "UIElements/Views/Qt4/Plotting/QSeisCurveRegistry.hpp"
|
---|
64 | #endif
|
---|
65 | #include "World.hpp"
|
---|
66 |
|
---|
67 | using namespace std;
|
---|
68 |
|
---|
69 | const int QtHomologyList::COLUMNCOUNT = COLUMNTYPES_MAX;
|
---|
70 | const char *QtHomologyList::COLUMNNAMES[QtHomologyList::COLUMNCOUNT]={"Number","Nodes","Edges","Occurrence"};
|
---|
71 |
|
---|
72 | QtHomologyList::QtHomologyList(QWidget * _parent) :
|
---|
73 | QWidget(_parent),
|
---|
74 | Observer("QtHomologyList"),
|
---|
75 | potentialregistry_enabled(false)
|
---|
76 | {
|
---|
77 | QHBoxLayout* layout = new QHBoxLayout(this);
|
---|
78 | QSplitter *splitter = new QSplitter (Qt::Horizontal, this );
|
---|
79 | layout->addWidget(splitter);
|
---|
80 |
|
---|
81 | // tree widget
|
---|
82 | treewidget = new QTreeWidget (splitter);
|
---|
83 | treewidget->setSelectionMode( QTreeWidget::SingleSelection );
|
---|
84 | treewidget->setColumnCount(COLUMNCOUNT);
|
---|
85 | //treewidget->setSizePolicy( QSizePolicy::Minimum, sizePolicy().verticalPolicy() );
|
---|
86 | QStringList header;
|
---|
87 | for(int i=0; i<COLUMNCOUNT;++i)
|
---|
88 | header << COLUMNNAMES[i];
|
---|
89 | treewidget->setHeaderLabels(header);
|
---|
90 | splitter->addWidget(treewidget);
|
---|
91 |
|
---|
92 | // plot widget
|
---|
93 | #ifdef HAVE_QWT
|
---|
94 | widget = new QSeisPlotPage ("energy", splitter);
|
---|
95 | //widget->setSizePolicy( QSizePolicy::MinimumExpanding, sizePolicy().verticalPolicy() );
|
---|
96 | QSeisPageRegistry::getInstance().registerInstance(widget);
|
---|
97 | #else
|
---|
98 | widget = new QWidget(splitter);
|
---|
99 | #endif
|
---|
100 | splitter->addWidget(widget);
|
---|
101 |
|
---|
102 | dirty = true;
|
---|
103 | clearing = false;
|
---|
104 |
|
---|
105 | QSettings settings;
|
---|
106 | settings.beginGroup("QtHomologyList");
|
---|
107 | treewidget->resize(settings.value("treewidget_size", QSize(width()/2, 20)).toSize());
|
---|
108 | widget->resize(settings.value("plotwidget_size", QSize(width()/2, 20)).toSize());
|
---|
109 | settings.endGroup();
|
---|
110 |
|
---|
111 | refill();
|
---|
112 |
|
---|
113 | HomologyContainer &homologies = World::getInstance().getHomologies();
|
---|
114 | homologies.signOn(this);
|
---|
115 | PotentialRegistry::getInstance().signOn(this);
|
---|
116 | potentialregistry_enabled = true;
|
---|
117 |
|
---|
118 | #ifdef HAVE_QWT
|
---|
119 | //connect the PlotCurveRegistry directly to the PlotPage registry
|
---|
120 | connect(QSeisCurveRegistry::getPointer(), SIGNAL(curveAdded(std::string, QString)), QSeisPageRegistry::getPointer(), SLOT(addCurve(std::string, QString)));
|
---|
121 | connect(QSeisCurveRegistry::getPointer(), SIGNAL(curveRemoved(std::string, QString)), QSeisPageRegistry::getPointer(), SLOT(removeCurve(std::string, QString)));
|
---|
122 | connect(QSeisCurveRegistry::getPointer(), SIGNAL(curveChanged(std::string, QString)), QSeisPageRegistry::getPointer(), SLOT(updateCurve(std::string, QString)));
|
---|
123 | #endif
|
---|
124 |
|
---|
125 | connect(treewidget,SIGNAL(itemSelectionChanged()),this,SLOT(rowSelected()));
|
---|
126 | connect(this,SIGNAL(changed()),this,SLOT(update()));
|
---|
127 | }
|
---|
128 |
|
---|
129 | QtHomologyList::~QtHomologyList()
|
---|
130 | {
|
---|
131 | QSettings settings;
|
---|
132 | settings.beginGroup("QtHomologyList");
|
---|
133 | settings.setValue("treewidget_size", treewidget->size());
|
---|
134 | settings.setValue("plotwidget_size", widget->size());
|
---|
135 | settings.endGroup();
|
---|
136 |
|
---|
137 |
|
---|
138 | HomologyContainer &homologies = World::getInstance().getHomologies();
|
---|
139 | homologies.signOff(this);
|
---|
140 | if (potentialregistry_enabled)
|
---|
141 | PotentialRegistry::getInstance().signOff(this);
|
---|
142 | }
|
---|
143 |
|
---|
144 | void QtHomologyList::update(Observable *publisher) {
|
---|
145 |
|
---|
146 | dirty = true;
|
---|
147 |
|
---|
148 | // force an update from Qt...
|
---|
149 | // clearing = true;
|
---|
150 | // treewidget->clear();
|
---|
151 | // clearing = false;
|
---|
152 | emit changed(); //doesn't work!?!
|
---|
153 | }
|
---|
154 |
|
---|
155 | void QtHomologyList::refill()
|
---|
156 | {
|
---|
157 | clearing = true;
|
---|
158 | const HomologyContainer &homologies = World::getInstance().getHomologies();
|
---|
159 |
|
---|
160 | // clear everything
|
---|
161 | HomologySelection.clear();
|
---|
162 | treewidget->clear();
|
---|
163 | #ifdef HAVE_QWT
|
---|
164 | QSeisCurveRegistry::getInstance().resetRegistry();
|
---|
165 | #endif
|
---|
166 |
|
---|
167 | size_t count = 0;
|
---|
168 | for (HomologyContainer::const_key_iterator homologyiter = homologies.key_begin();
|
---|
169 | homologyiter != homologies.key_end();
|
---|
170 | homologyiter = homologies.getNextKey(homologyiter), ++count) {
|
---|
171 | HomologyContainer::range_t occurences = homologies.getHomologousGraphs(*homologyiter);
|
---|
172 | const HomologyGraph &graph = occurences.first->first;
|
---|
173 | const size_t times = std::distance(occurences.first, occurences.second);
|
---|
174 |
|
---|
175 | // create item
|
---|
176 | QTreeWidgetItem *treeItem = new QTreeWidgetItem(treewidget);
|
---|
177 | treeItem->setText(NUMBER, QString::number(count));
|
---|
178 | {
|
---|
179 | std::stringstream output;
|
---|
180 | graph.printNodes(output);
|
---|
181 | treeItem->setText(NODES, QString(output.str().c_str()));
|
---|
182 | }
|
---|
183 | {
|
---|
184 | std::stringstream output;
|
---|
185 | graph.printEdges(output);
|
---|
186 | treeItem->setText(EDGES, QString(output.str().c_str()));
|
---|
187 | }
|
---|
188 | if (times > 0) {
|
---|
189 | treeItem->setText(OCCURRENCE, QString::number(times));
|
---|
190 | } else {
|
---|
191 | treeItem->setText(OCCURRENCE, "none");
|
---|
192 | treeItem->setDisabled(true);
|
---|
193 | }
|
---|
194 | HomologySelection.push_back(treeItem->isSelected());
|
---|
195 |
|
---|
196 | #ifdef HAVE_QWT
|
---|
197 | // create associated curve in plot
|
---|
198 | CompoundPotential *compound = new CompoundPotential(graph);
|
---|
199 | ASSERT( compound != NULL,
|
---|
200 | "QtHomologyList::refill() - compound is NULL.");
|
---|
201 | TrainingData data(compound->getSpecificFilter());
|
---|
202 | data(homologies.getHomologousGraphs(graph));
|
---|
203 | if (!data.getTrainingInputs().empty()) {
|
---|
204 | // generate QSeisData
|
---|
205 | const TrainingData::InputVector_t &inputs = data.getAllArguments();
|
---|
206 | const TrainingData::OutputVector_t &outputs = data.getTrainingOutputs();
|
---|
207 | std::vector<double> xvalues;
|
---|
208 | std::vector<double> yvalues;
|
---|
209 | for (TrainingData::OutputVector_t::const_iterator outputiter = outputs.begin();
|
---|
210 | outputiter != outputs.end(); ++outputiter)
|
---|
211 | yvalues.push_back((*outputiter)[0]);
|
---|
212 |
|
---|
213 | // go through each potential
|
---|
214 | for (CompoundPotential::models_t::const_iterator potiter = compound->begin();
|
---|
215 | potiter != compound->end();
|
---|
216 | ++potiter) {
|
---|
217 | const EmpiricalPotential &potential = dynamic_cast<const EmpiricalPotential &>(**potiter);
|
---|
218 | const std::string potentialname = potential.getName();
|
---|
219 | const FunctionModel::filter_t filter = potential.getSpecificFilter();
|
---|
220 | Coordinator::ptr coordinator = potential.getCoordinator();
|
---|
221 | // then we need to sample the potential
|
---|
222 | xvalues.clear();
|
---|
223 | for (TrainingData::InputVector_t::const_iterator inputiter = inputs.begin();
|
---|
224 | inputiter != inputs.end(); ++inputiter) {
|
---|
225 | const FunctionModel::list_of_arguments_t specificargs = filter(*inputiter);
|
---|
226 | double average = 0.;
|
---|
227 | for (FunctionModel::list_of_arguments_t::const_iterator argiter = specificargs.begin();
|
---|
228 | argiter != specificargs.end(); ++argiter) {
|
---|
229 | const FunctionModel::arguments_t args = *argiter;
|
---|
230 | average += (*coordinator)(args);
|
---|
231 | }
|
---|
232 | if (specificargs.size() > 1) {
|
---|
233 | const size_t index = xvalues.size();
|
---|
234 | xvalues.push_back(average/(double)specificargs.size());
|
---|
235 | yvalues[index] *= 1./(double)specificargs.size();
|
---|
236 | }
|
---|
237 | }
|
---|
238 |
|
---|
239 | // We need to sort the xvalues (and associated yvalues also)
|
---|
240 | std::vector<double>::const_iterator xiter = xvalues.begin();
|
---|
241 | std::vector<double>::const_iterator yiter = yvalues.begin();
|
---|
242 | std::map<double, std::set<double> > sorted_xy;
|
---|
243 | for (;xiter != xvalues.end(); ++xiter, ++yiter) {
|
---|
244 | std::set<double> yset;
|
---|
245 | yset.insert(*yiter);
|
---|
246 | std::pair<std::map<double, std::set<double> >::iterator, bool> inserter =
|
---|
247 | sorted_xy.insert(std::make_pair(*xiter, yset));
|
---|
248 | if (!inserter.second)
|
---|
249 | inserter.first->second.insert(*yiter);
|
---|
250 | }
|
---|
251 | xvalues.clear();
|
---|
252 | yvalues.clear();
|
---|
253 | for (std::map<double, std::set<double> >::const_iterator iter = sorted_xy.begin();
|
---|
254 | iter != sorted_xy.end(); ++iter) {
|
---|
255 | for (std::set<double>::const_iterator valueiter = iter->second.begin();
|
---|
256 | valueiter != iter->second.end();
|
---|
257 | ++valueiter) {
|
---|
258 | xvalues.push_back(iter->first);
|
---|
259 | yvalues.push_back(*valueiter);
|
---|
260 | }
|
---|
261 | }
|
---|
262 |
|
---|
263 | QSeisData data(xvalues, yvalues, QString(potentialname.c_str()));
|
---|
264 | // couple to QSeisPlotCurve and register the curve
|
---|
265 | QSeisPlotCurve *curve = new QSeisPlotCurve(QString(potentialname.c_str()), "energy");
|
---|
266 | curve->updateCurve(&data);
|
---|
267 | if (!QSeisCurveRegistry::getInstance().isPresentByName(curve->getName()))
|
---|
268 | QSeisCurveRegistry::getInstance().registerInstance(curve);
|
---|
269 | else
|
---|
270 | delete curve;
|
---|
271 | // couple to QSeisPlotPage
|
---|
272 | widget->addCurve(potentialname);
|
---|
273 |
|
---|
274 | }
|
---|
275 | }
|
---|
276 | #endif
|
---|
277 | }
|
---|
278 | dirty = false;
|
---|
279 | clearing = false;
|
---|
280 | }
|
---|
281 |
|
---|
282 | void QtHomologyList::paintEvent(QPaintEvent * event)
|
---|
283 | {
|
---|
284 | if (dirty)
|
---|
285 | refill();
|
---|
286 | // treewidget->paintEvent(event);
|
---|
287 | }
|
---|
288 |
|
---|
289 | void QtHomologyList::subjectKilled(Observable *publisher)
|
---|
290 | {
|
---|
291 | // as a new instance should always already be present ... just sign on
|
---|
292 | if (static_cast<PotentialRegistry *>(publisher) == PotentialRegistry::getPointer()) {
|
---|
293 | potentialregistry_enabled = false;
|
---|
294 | } else {
|
---|
295 | // its HomologyContainer
|
---|
296 | }
|
---|
297 | }
|
---|
298 |
|
---|
299 |
|
---|
300 | void QtHomologyList::rowSelected()
|
---|
301 | {
|
---|
302 | //std::cout << "rowSelected\n";
|
---|
303 | for (int i=0;i<treewidget->topLevelItemCount();i++){
|
---|
304 | QTreeWidgetItem *item = treewidget->topLevelItem(i);
|
---|
305 | bool newSelection = item->isSelected();
|
---|
306 | if (newSelection != HomologySelection[i]){
|
---|
307 | // TODO: Add selected curve to QTabWidget
|
---|
308 | }
|
---|
309 | }
|
---|
310 | }
|
---|
311 |
|
---|