[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 |
|
---|
[9eb71b3] | 45 | //#include "CodePatterns/MemDebug.hpp"
|
---|
[1e1098] | 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;
|
---|
| 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();
|
---|
[52bb5a] | 116 |
|
---|
[1e1098] | 117 |
|
---|
| 118 | HomologyContainer &homologies = World::getInstance().getHomologies();
|
---|
| 119 | homologies.signOn(this);
|
---|
[f0964c] | 120 | PotentialRegistry::getInstance().signOn(this);
|
---|
| 121 | potentialregistry_enabled = true;
|
---|
[1e1098] | 122 |
|
---|
[b677ab] | 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 |
|
---|
[a8f5d94] | 130 | connect(treewidget,SIGNAL(itemSelectionChanged()),this,SLOT(rowSelected()));
|
---|
[1e1098] | 131 | connect(this,SIGNAL(changed()),this,SLOT(update()));
|
---|
[9d4f964] | 132 | connect(this,SIGNAL(needsRefill()),this,SLOT(refill()), Qt::QueuedConnection);
|
---|
| 133 |
|
---|
| 134 | emit needsRefill();
|
---|
[1e1098] | 135 | }
|
---|
| 136 |
|
---|
| 137 | QtHomologyList::~QtHomologyList()
|
---|
| 138 | {
|
---|
[3b1798] | 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 |
|
---|
[1e1098] | 146 | HomologyContainer &homologies = World::getInstance().getHomologies();
|
---|
| 147 | homologies.signOff(this);
|
---|
[f0964c] | 148 | if (potentialregistry_enabled)
|
---|
| 149 | PotentialRegistry::getInstance().signOff(this);
|
---|
[1e1098] | 150 | }
|
---|
| 151 |
|
---|
[7516f6] | 152 | void QtHomologyList::update(Observable *publisher)
|
---|
| 153 | {
|
---|
[1e1098] | 154 | dirty = true;
|
---|
| 155 |
|
---|
| 156 | // force an update from Qt...
|
---|
[b677ab] | 157 | // treewidget->clear();
|
---|
| 158 | emit changed(); //doesn't work!?!
|
---|
[1e1098] | 159 | }
|
---|
| 160 |
|
---|
| 161 | void QtHomologyList::refill()
|
---|
| 162 | {
|
---|
[7516f6] | 163 | boost::recursive_mutex::scoped_lock lock(refill_mutex);
|
---|
| 164 |
|
---|
[1e1098] | 165 | const HomologyContainer &homologies = World::getInstance().getHomologies();
|
---|
| 166 |
|
---|
| 167 | // clear everything
|
---|
| 168 | HomologySelection.clear();
|
---|
[a8f5d94] | 169 | treewidget->clear();
|
---|
[b677ab] | 170 | #ifdef HAVE_QWT
|
---|
| 171 | QSeisCurveRegistry::getInstance().resetRegistry();
|
---|
| 172 | #endif
|
---|
[1e1098] | 173 |
|
---|
| 174 | size_t count = 0;
|
---|
[b677ab] | 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);
|
---|
[1e1098] | 179 | const HomologyGraph &graph = occurences.first->first;
|
---|
| 180 | const size_t times = std::distance(occurences.first, occurences.second);
|
---|
| 181 |
|
---|
[b677ab] | 182 | // create item
|
---|
[4aed01] | 183 | std::vector<int> numerical_columns;
|
---|
| 184 | numerical_columns += OCCURRENCE;
|
---|
| 185 | QTreeWidgetItem *treeItem = new QtNumericalItem(NUMBER, numerical_columns, treewidget);
|
---|
[1e1098] | 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());
|
---|
[b677ab] | 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
|
---|
[e1fe7e] | 214 | const TrainingData::InputVector_t &inputs = data.getAllArguments();
|
---|
[b677ab] | 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();
|
---|
[9bdcc9] | 228 | const FunctionModel::filter_t filter = potential.getSpecificFilter();
|
---|
[b677ab] | 229 | Coordinator::ptr coordinator = potential.getCoordinator();
|
---|
| 230 | // then we need to sample the potential
|
---|
[e60558] | 231 | HomologyGraph dummy;
|
---|
[b677ab] | 232 | xvalues.clear();
|
---|
| 233 | for (TrainingData::InputVector_t::const_iterator inputiter = inputs.begin();
|
---|
[9bdcc9] | 234 | inputiter != inputs.end(); ++inputiter) {
|
---|
[e60558] | 235 | const FunctionModel::list_of_arguments_t specificargs = filter(dummy, *inputiter);
|
---|
[9bdcc9] | 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)
|
---|
[b677ab] | 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 |
|
---|
[d88397] | 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 | }
|
---|
[b677ab] | 285 | }
|
---|
| 286 | }
|
---|
| 287 | #endif
|
---|
[1e1098] | 288 | }
|
---|
| 289 | dirty = false;
|
---|
| 290 | }
|
---|
| 291 |
|
---|
| 292 | void QtHomologyList::paintEvent(QPaintEvent * event)
|
---|
| 293 | {
|
---|
[7516f6] | 294 | boost::recursive_mutex::scoped_lock lock(refill_mutex);
|
---|
| 295 |
|
---|
[1e1098] | 296 | if (dirty)
|
---|
| 297 | refill();
|
---|
[a8f5d94] | 298 | // treewidget->paintEvent(event);
|
---|
[1e1098] | 299 | }
|
---|
| 300 |
|
---|
| 301 | void QtHomologyList::subjectKilled(Observable *publisher)
|
---|
| 302 | {
|
---|
| 303 | // as a new instance should always already be present ... just sign on
|
---|
[f0964c] | 304 | if (static_cast<PotentialRegistry *>(publisher) == PotentialRegistry::getPointer()) {
|
---|
| 305 | potentialregistry_enabled = false;
|
---|
| 306 | } else {
|
---|
| 307 | // its HomologyContainer
|
---|
| 308 | }
|
---|
[1e1098] | 309 | }
|
---|
| 310 |
|
---|
| 311 |
|
---|
| 312 | void QtHomologyList::rowSelected()
|
---|
| 313 | {
|
---|
[7516f6] | 314 | boost::recursive_mutex::scoped_lock lock(refill_mutex);
|
---|
| 315 |
|
---|
[1e1098] | 316 | //std::cout << "rowSelected\n";
|
---|
[a8f5d94] | 317 | for (int i=0;i<treewidget->topLevelItemCount();i++){
|
---|
| 318 | QTreeWidgetItem *item = treewidget->topLevelItem(i);
|
---|
[1e1098] | 319 | bool newSelection = item->isSelected();
|
---|
| 320 | if (newSelection != HomologySelection[i]){
|
---|
[b677ab] | 321 | // TODO: Add selected curve to QTabWidget
|
---|
[1e1098] | 322 | }
|
---|
| 323 | }
|
---|
| 324 | }
|
---|
| 325 |
|
---|