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