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