source: src/UIElements/Views/Qt4/QtHomologyList.cpp@ 4c6f0d

Action_Thermostats Adding_MD_integration_tests Adding_StructOpt_integration_tests AutomationFragmentation_failures Candidate_v1.6.1 ChemicalSpaceEvaluator Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Exclude_Hydrogens_annealWithBondGraph Fix_Verbose_Codepatterns ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion Gui_displays_atomic_force_velocity JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool PythonUI_with_named_parameters Recreated_GuiChecks StoppableMakroAction TremoloParser_IncreasedPrecision
Last change on this file since 4c6f0d was 7516f6, checked in by Frederik Heber <frederik.heber@…>, 8 years ago

FIX: several Qt...Lists required mutex to control refill triggered by observable update.

  • Property mode set to 100644
File size: 11.3 KB
Line 
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
70using namespace std;
71
72using namespace boost::assign;
73
74const int QtHomologyList::COLUMNCOUNT = COLUMNTYPES_MAX;
75const char *QtHomologyList::COLUMNNAMES[QtHomologyList::COLUMNCOUNT]={"Number","Nodes","Edges","Occurrence"};
76
77QtHomologyList::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
111 QSettings settings;
112 settings.beginGroup("QtHomologyList");
113 treewidget->resize(settings.value("treewidget_size", QSize(width()/2, 20)).toSize());
114 widget->resize(settings.value("plotwidget_size", QSize(width()/2, 20)).toSize());
115 settings.endGroup();
116
117
118 HomologyContainer &homologies = World::getInstance().getHomologies();
119 homologies.signOn(this);
120 PotentialRegistry::getInstance().signOn(this);
121 potentialregistry_enabled = true;
122
123#ifdef HAVE_QWT
124 //connect the PlotCurveRegistry directly to the PlotPage registry
125 connect(QSeisCurveRegistry::getPointer(), SIGNAL(curveAdded(std::string, QString)), QSeisPageRegistry::getPointer(), SLOT(addCurve(std::string, QString)));
126 connect(QSeisCurveRegistry::getPointer(), SIGNAL(curveRemoved(std::string, QString)), QSeisPageRegistry::getPointer(), SLOT(removeCurve(std::string, QString)));
127 connect(QSeisCurveRegistry::getPointer(), SIGNAL(curveChanged(std::string, QString)), QSeisPageRegistry::getPointer(), SLOT(updateCurve(std::string, QString)));
128#endif
129
130 connect(treewidget,SIGNAL(itemSelectionChanged()),this,SLOT(rowSelected()));
131 connect(this,SIGNAL(changed()),this,SLOT(update()));
132 connect(this,SIGNAL(needsRefill()),this,SLOT(refill()), Qt::QueuedConnection);
133
134 emit needsRefill();
135}
136
137QtHomologyList::~QtHomologyList()
138{
139 QSettings settings;
140 settings.beginGroup("QtHomologyList");
141 settings.setValue("treewidget_size", treewidget->size());
142 settings.setValue("plotwidget_size", widget->size());
143 settings.endGroup();
144
145
146 HomologyContainer &homologies = World::getInstance().getHomologies();
147 homologies.signOff(this);
148 if (potentialregistry_enabled)
149 PotentialRegistry::getInstance().signOff(this);
150}
151
152void QtHomologyList::update(Observable *publisher)
153{
154 dirty = true;
155
156 // force an update from Qt...
157// treewidget->clear();
158 emit changed(); //doesn't work!?!
159}
160
161void QtHomologyList::refill()
162{
163 boost::recursive_mutex::scoped_lock lock(refill_mutex);
164
165 const HomologyContainer &homologies = World::getInstance().getHomologies();
166
167 // clear everything
168 HomologySelection.clear();
169 treewidget->clear();
170#ifdef HAVE_QWT
171 QSeisCurveRegistry::getInstance().resetRegistry();
172#endif
173
174 size_t count = 0;
175 for (HomologyContainer::const_key_iterator homologyiter = homologies.key_begin();
176 homologyiter != homologies.key_end();
177 homologyiter = homologies.getNextKey(homologyiter), ++count) {
178 HomologyContainer::range_t occurences = homologies.getHomologousGraphs(*homologyiter);
179 const HomologyGraph &graph = occurences.first->first;
180 const size_t times = std::distance(occurences.first, occurences.second);
181
182 // create item
183 std::vector<int> numerical_columns;
184 numerical_columns += OCCURRENCE;
185 QTreeWidgetItem *treeItem = new QtNumericalItem(NUMBER, numerical_columns, treewidget);
186 treeItem->setText(NUMBER, QString::number(count));
187 {
188 std::stringstream output;
189 graph.printNodes(output);
190 treeItem->setText(NODES, QString(output.str().c_str()));
191 }
192 {
193 std::stringstream output;
194 graph.printEdges(output);
195 treeItem->setText(EDGES, QString(output.str().c_str()));
196 }
197 if (times > 0) {
198 treeItem->setText(OCCURRENCE, QString::number(times));
199 } else {
200 treeItem->setText(OCCURRENCE, "none");
201 treeItem->setDisabled(true);
202 }
203 HomologySelection.push_back(treeItem->isSelected());
204
205#ifdef HAVE_QWT
206 // create associated curve in plot
207 CompoundPotential *compound = new CompoundPotential(graph);
208 ASSERT( compound != NULL,
209 "QtHomologyList::refill() - compound is NULL.");
210 TrainingData data(compound->getSpecificFilter());
211 data(homologies.getHomologousGraphs(graph));
212 if (!data.getTrainingInputs().empty()) {
213 // generate QSeisData
214 const TrainingData::InputVector_t &inputs = data.getAllArguments();
215 const TrainingData::OutputVector_t &outputs = data.getTrainingOutputs();
216 std::vector<double> xvalues;
217 std::vector<double> yvalues;
218 for (TrainingData::OutputVector_t::const_iterator outputiter = outputs.begin();
219 outputiter != outputs.end(); ++outputiter)
220 yvalues.push_back((*outputiter)[0]);
221
222 // go through each potential
223 for (CompoundPotential::models_t::const_iterator potiter = compound->begin();
224 potiter != compound->end();
225 ++potiter) {
226 const EmpiricalPotential &potential = dynamic_cast<const EmpiricalPotential &>(**potiter);
227 const std::string potentialname = potential.getName();
228 const FunctionModel::filter_t filter = potential.getSpecificFilter();
229 Coordinator::ptr coordinator = potential.getCoordinator();
230 // then we need to sample the potential
231 HomologyGraph dummy;
232 xvalues.clear();
233 for (TrainingData::InputVector_t::const_iterator inputiter = inputs.begin();
234 inputiter != inputs.end(); ++inputiter) {
235 const FunctionModel::list_of_arguments_t specificargs = filter(dummy, *inputiter);
236 double average = 0.;
237 for (FunctionModel::list_of_arguments_t::const_iterator argiter = specificargs.begin();
238 argiter != specificargs.end(); ++argiter) {
239 const FunctionModel::arguments_t args = *argiter;
240 average += (*coordinator)(args);
241 }
242 if (specificargs.size() > 1) {
243 const size_t index = xvalues.size();
244 xvalues.push_back(average/(double)specificargs.size());
245 yvalues[index] *= 1./(double)specificargs.size();
246 }
247 }
248
249 // We need to sort the xvalues (and associated yvalues also)
250 std::vector<double>::const_iterator xiter = xvalues.begin();
251 std::vector<double>::const_iterator yiter = yvalues.begin();
252 std::map<double, std::set<double> > sorted_xy;
253 for (;xiter != xvalues.end(); ++xiter, ++yiter) {
254 std::set<double> yset;
255 yset.insert(*yiter);
256 std::pair<std::map<double, std::set<double> >::iterator, bool> inserter =
257 sorted_xy.insert(std::make_pair(*xiter, yset));
258 if (!inserter.second)
259 inserter.first->second.insert(*yiter);
260 }
261 xvalues.clear();
262 yvalues.clear();
263 for (std::map<double, std::set<double> >::const_iterator iter = sorted_xy.begin();
264 iter != sorted_xy.end(); ++iter) {
265 for (std::set<double>::const_iterator valueiter = iter->second.begin();
266 valueiter != iter->second.end();
267 ++valueiter) {
268 xvalues.push_back(iter->first);
269 yvalues.push_back(*valueiter);
270 }
271 }
272
273 if ((!xvalues.empty()) && (!yvalues.empty())) {
274 QSeisData data(xvalues, yvalues, QString(potentialname.c_str()));
275 // couple to QSeisPlotCurve and register the curve
276 QSeisPlotCurve *curve = new QSeisPlotCurve(QString(potentialname.c_str()), "energy");
277 curve->updateCurve(&data);
278 if (!QSeisCurveRegistry::getInstance().isPresentByName(curve->getName()))
279 QSeisCurveRegistry::getInstance().registerInstance(curve);
280 else
281 delete curve;
282 // couple to QSeisPlotPage
283 widget->addCurve(potentialname);
284 }
285 }
286 }
287#endif
288 }
289 dirty = false;
290}
291
292void QtHomologyList::paintEvent(QPaintEvent * event)
293{
294 boost::recursive_mutex::scoped_lock lock(refill_mutex);
295
296 if (dirty)
297 refill();
298// treewidget->paintEvent(event);
299}
300
301void 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
312void QtHomologyList::rowSelected()
313{
314 boost::recursive_mutex::scoped_lock lock(refill_mutex);
315
316 //std::cout << "rowSelected\n";
317 for (int i=0;i<treewidget->topLevelItemCount();i++){
318 QTreeWidgetItem *item = treewidget->topLevelItem(i);
319 bool newSelection = item->isSelected();
320 if (newSelection != HomologySelection[i]){
321 // TODO: Add selected curve to QTabWidget
322 }
323 }
324}
325
Note: See TracBrowser for help on using the repository browser.