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