[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"
|
---|
[4aed01] | 36 | #include "Views/Qt4/QtNumericalItem.hpp"
|
---|
[1e1098] | 37 |
|
---|
| 38 | #include <QAbstractItemView>
|
---|
[a8f5d94] | 39 | #include <QtGui/QTreeWidget>
|
---|
[b677ab] | 40 | #include <QtGui/QTabWidget>
|
---|
[3b1798] | 41 | #include <Qt/qsettings.h>
|
---|
[0ac3e3] | 42 | #include <Qt/qsplitter.h>
|
---|
| 43 | #include <Qt/qboxlayout.h>
|
---|
[1e1098] | 44 |
|
---|
| 45 | #include "CodePatterns/MemDebug.hpp"
|
---|
| 46 |
|
---|
[4aed01] | 47 | #include <iterator>
|
---|
| 48 | #include <iostream>
|
---|
| 49 |
|
---|
| 50 | #include <boost/assign.hpp>
|
---|
| 51 |
|
---|
[b677ab] | 52 | #include "CodePatterns/Log.hpp"
|
---|
| 53 |
|
---|
[1e1098] | 54 | #include "Fragmentation/Homology/HomologyContainer.hpp"
|
---|
[b677ab] | 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"
|
---|
[f0964c] | 60 | #include "Potentials/PotentialRegistry.hpp"
|
---|
[b677ab] | 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
|
---|
[1e1098] | 68 | #include "World.hpp"
|
---|
| 69 |
|
---|
| 70 | using namespace std;
|
---|
| 71 |
|
---|
[4aed01] | 72 | using namespace boost::assign;
|
---|
| 73 |
|
---|
[1e1098] | 74 | const int QtHomologyList::COLUMNCOUNT = COLUMNTYPES_MAX;
|
---|
| 75 | const char *QtHomologyList::COLUMNNAMES[QtHomologyList::COLUMNCOUNT]={"Number","Nodes","Edges","Occurrence"};
|
---|
| 76 |
|
---|
| 77 | QtHomologyList::QtHomologyList(QWidget * _parent) :
|
---|
[a8f5d94] | 78 | QWidget(_parent),
|
---|
[f0964c] | 79 | Observer("QtHomologyList"),
|
---|
| 80 | potentialregistry_enabled(false)
|
---|
[1e1098] | 81 | {
|
---|
[3b1798] | 82 | QHBoxLayout* layout = new QHBoxLayout(this);
|
---|
[a8f5d94] | 83 | QSplitter *splitter = new QSplitter (Qt::Horizontal, this );
|
---|
[0ac3e3] | 84 | layout->addWidget(splitter);
|
---|
| 85 |
|
---|
| 86 | // tree widget
|
---|
[a8f5d94] | 87 | treewidget = new QTreeWidget (splitter);
|
---|
[0ac3e3] | 88 | treewidget->setSelectionMode( QTreeWidget::SingleSelection );
|
---|
| 89 | treewidget->setColumnCount(COLUMNCOUNT);
|
---|
[562b0a] | 90 | treewidget->setSortingEnabled(true);
|
---|
[3b1798] | 91 | //treewidget->setSizePolicy( QSizePolicy::Minimum, sizePolicy().verticalPolicy() );
|
---|
[0ac3e3] | 92 | QStringList header;
|
---|
| 93 | for(int i=0; i<COLUMNCOUNT;++i)
|
---|
| 94 | header << COLUMNNAMES[i];
|
---|
| 95 | treewidget->setHeaderLabels(header);
|
---|
[562b0a] | 96 | treewidget->sortByColumn(0);
|
---|
[a8f5d94] | 97 | splitter->addWidget(treewidget);
|
---|
[b677ab] | 98 |
|
---|
[0ac3e3] | 99 | // plot widget
|
---|
[b677ab] | 100 | #ifdef HAVE_QWT
|
---|
| 101 | widget = new QSeisPlotPage ("energy", splitter);
|
---|
[3b1798] | 102 | //widget->setSizePolicy( QSizePolicy::MinimumExpanding, sizePolicy().verticalPolicy() );
|
---|
[b677ab] | 103 | QSeisPageRegistry::getInstance().registerInstance(widget);
|
---|
| 104 | #else
|
---|
| 105 | widget = new QWidget(splitter);
|
---|
| 106 | #endif
|
---|
[a8f5d94] | 107 | splitter->addWidget(widget);
|
---|
[1e1098] | 108 |
|
---|
[3b1798] | 109 | dirty = true;
|
---|
[52bb5a] | 110 | clearing = false;
|
---|
[3b1798] | 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();
|
---|
[52bb5a] | 117 |
|
---|
[1e1098] | 118 |
|
---|
| 119 | HomologyContainer &homologies = World::getInstance().getHomologies();
|
---|
| 120 | homologies.signOn(this);
|
---|
[f0964c] | 121 | PotentialRegistry::getInstance().signOn(this);
|
---|
| 122 | potentialregistry_enabled = true;
|
---|
[1e1098] | 123 |
|
---|
[b677ab] | 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 |
|
---|
[a8f5d94] | 131 | connect(treewidget,SIGNAL(itemSelectionChanged()),this,SLOT(rowSelected()));
|
---|
[1e1098] | 132 | connect(this,SIGNAL(changed()),this,SLOT(update()));
|
---|
[9d4f964] | 133 | connect(this,SIGNAL(needsRefill()),this,SLOT(refill()), Qt::QueuedConnection);
|
---|
| 134 |
|
---|
| 135 | emit needsRefill();
|
---|
[1e1098] | 136 | }
|
---|
| 137 |
|
---|
| 138 | QtHomologyList::~QtHomologyList()
|
---|
| 139 | {
|
---|
[3b1798] | 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 |
|
---|
[1e1098] | 147 | HomologyContainer &homologies = World::getInstance().getHomologies();
|
---|
| 148 | homologies.signOff(this);
|
---|
[f0964c] | 149 | if (potentialregistry_enabled)
|
---|
| 150 | PotentialRegistry::getInstance().signOff(this);
|
---|
[1e1098] | 151 | }
|
---|
| 152 |
|
---|
| 153 | void QtHomologyList::update(Observable *publisher) {
|
---|
| 154 |
|
---|
| 155 | dirty = true;
|
---|
| 156 |
|
---|
| 157 | // force an update from Qt...
|
---|
[b677ab] | 158 | // clearing = true;
|
---|
| 159 | // treewidget->clear();
|
---|
| 160 | // clearing = false;
|
---|
| 161 | emit changed(); //doesn't work!?!
|
---|
[1e1098] | 162 | }
|
---|
| 163 |
|
---|
| 164 | void QtHomologyList::refill()
|
---|
| 165 | {
|
---|
[52bb5a] | 166 | clearing = true;
|
---|
[1e1098] | 167 | const HomologyContainer &homologies = World::getInstance().getHomologies();
|
---|
| 168 |
|
---|
| 169 | // clear everything
|
---|
| 170 | HomologySelection.clear();
|
---|
[a8f5d94] | 171 | treewidget->clear();
|
---|
[b677ab] | 172 | #ifdef HAVE_QWT
|
---|
| 173 | QSeisCurveRegistry::getInstance().resetRegistry();
|
---|
| 174 | #endif
|
---|
[1e1098] | 175 |
|
---|
| 176 | size_t count = 0;
|
---|
[b677ab] | 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);
|
---|
[1e1098] | 181 | const HomologyGraph &graph = occurences.first->first;
|
---|
| 182 | const size_t times = std::distance(occurences.first, occurences.second);
|
---|
| 183 |
|
---|
[b677ab] | 184 | // create item
|
---|
[4aed01] | 185 | std::vector<int> numerical_columns;
|
---|
| 186 | numerical_columns += OCCURRENCE;
|
---|
| 187 | QTreeWidgetItem *treeItem = new QtNumericalItem(NUMBER, numerical_columns, treewidget);
|
---|
[1e1098] | 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());
|
---|
[b677ab] | 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
|
---|
[e1fe7e] | 216 | const TrainingData::InputVector_t &inputs = data.getAllArguments();
|
---|
[b677ab] | 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();
|
---|
[9bdcc9] | 230 | const FunctionModel::filter_t filter = potential.getSpecificFilter();
|
---|
[b677ab] | 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();
|
---|
[9bdcc9] | 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)
|
---|
[b677ab] | 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 | 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
|
---|
[1e1098] | 288 | }
|
---|
| 289 | dirty = false;
|
---|
[52bb5a] | 290 | clearing = false;
|
---|
[1e1098] | 291 | }
|
---|
| 292 |
|
---|
| 293 | void QtHomologyList::paintEvent(QPaintEvent * event)
|
---|
| 294 | {
|
---|
| 295 | if (dirty)
|
---|
| 296 | refill();
|
---|
[a8f5d94] | 297 | // treewidget->paintEvent(event);
|
---|
[1e1098] | 298 | }
|
---|
| 299 |
|
---|
| 300 | void QtHomologyList::subjectKilled(Observable *publisher)
|
---|
| 301 | {
|
---|
| 302 | // as a new instance should always already be present ... just sign on
|
---|
[f0964c] | 303 | if (static_cast<PotentialRegistry *>(publisher) == PotentialRegistry::getPointer()) {
|
---|
| 304 | potentialregistry_enabled = false;
|
---|
| 305 | } else {
|
---|
| 306 | // its HomologyContainer
|
---|
| 307 | }
|
---|
[1e1098] | 308 | }
|
---|
| 309 |
|
---|
| 310 |
|
---|
| 311 | void QtHomologyList::rowSelected()
|
---|
| 312 | {
|
---|
| 313 | //std::cout << "rowSelected\n";
|
---|
[a8f5d94] | 314 | for (int i=0;i<treewidget->topLevelItemCount();i++){
|
---|
| 315 | QTreeWidgetItem *item = treewidget->topLevelItem(i);
|
---|
[1e1098] | 316 | bool newSelection = item->isSelected();
|
---|
| 317 | if (newSelection != HomologySelection[i]){
|
---|
[b677ab] | 318 | // TODO: Add selected curve to QTabWidget
|
---|
[1e1098] | 319 | }
|
---|
| 320 | }
|
---|
| 321 | }
|
---|
| 322 |
|
---|