source: src/UIElements/Views/Qt4/QtHomologyList.cpp@ df3f2f

Action_Thermostats Add_AtomRandomPerturbation Add_FitFragmentPartialChargesAction Add_RotateAroundBondAction Add_SelectAtomByNameAction Added_ParseSaveFragmentResults AddingActions_SaveParseParticleParameters Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_ParticleName_to_Atom Adding_StructOpt_integration_tests AtomFragments Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.5.4 Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator CombiningParticlePotentialParsing Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_BoundInBox_CenterInBox_MoleculeActions Fix_ChargeSampling_PBC Fix_ChronosMutex Fix_FitPartialCharges Fix_FitPotential_needs_atomicnumbers Fix_ForceAnnealing Fix_IndependentFragmentGrids Fix_ParseParticles Fix_ParseParticles_split_forward_backward_Actions Fix_PopActions Fix_QtFragmentList_sorted_selection Fix_Restrictedkeyset_FragmentMolecule Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns Fix_fitting_potentials Fixes ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion FragmentAction_writes_AtomFragments FragmentMolecule_checks_bonddegrees GeometryObjects Gui_Fixes Gui_displays_atomic_force_velocity ImplicitCharges IndependentFragmentGrids IndependentFragmentGrids_IndividualZeroInstances IndependentFragmentGrids_IntegrationTest IndependentFragmentGrids_Sole_NN_Calculation JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix MoreRobust_FragmentAutomation ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PdbParser_setsAtomName PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks Rewrite_FitPartialCharges RotateToPrincipalAxisSystem_UndoRedo SaturateAtoms_findBestMatching SaturateAtoms_singleDegree StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_LinearAlgebra Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg Switchable_LogView ThirdParty_MPQC_rebuilt_buildsystem TrajectoryDependenant_MaxOrder TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps TremoloParser_setsAtomName Ubuntu_1604_changes stable
Last change on this file since df3f2f was 9d4f964, checked in by Frederik Heber <heber@…>, 9 years ago

FIX: QtHomologyList::refill() is now called via signal/slot.

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