| [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>
 | 
|---|
| [3b1798] | 43 | #include <Qt/qsettings.h>
 | 
|---|
| [0ac3e3] | 44 | #include <Qt/qsplitter.h>
 | 
|---|
 | 45 | #include <Qt/qboxlayout.h>
 | 
|---|
| [1e1098] | 46 | 
 | 
|---|
 | 47 | #include "CodePatterns/MemDebug.hpp"
 | 
|---|
 | 48 | 
 | 
|---|
| [b677ab] | 49 | #include "CodePatterns/Log.hpp"
 | 
|---|
 | 50 | 
 | 
|---|
| [1e1098] | 51 | #include "Fragmentation/Homology/HomologyContainer.hpp"
 | 
|---|
| [b677ab] | 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"
 | 
|---|
| [f0964c] | 57 | #include "Potentials/PotentialRegistry.hpp"
 | 
|---|
| [b677ab] | 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
 | 
|---|
| [1e1098] | 65 | #include "World.hpp"
 | 
|---|
 | 66 | 
 | 
|---|
 | 67 | using namespace std;
 | 
|---|
 | 68 | 
 | 
|---|
 | 69 | const int QtHomologyList::COLUMNCOUNT = COLUMNTYPES_MAX;
 | 
|---|
 | 70 | const char *QtHomologyList::COLUMNNAMES[QtHomologyList::COLUMNCOUNT]={"Number","Nodes","Edges","Occurrence"};
 | 
|---|
 | 71 | 
 | 
|---|
 | 72 | QtHomologyList::QtHomologyList(QWidget * _parent) :
 | 
|---|
| [a8f5d94] | 73 |     QWidget(_parent),
 | 
|---|
| [f0964c] | 74 |     Observer("QtHomologyList"),
 | 
|---|
 | 75 |     potentialregistry_enabled(false)
 | 
|---|
| [1e1098] | 76 | {
 | 
|---|
| [3b1798] | 77 |   QHBoxLayout* layout = new QHBoxLayout(this);
 | 
|---|
| [a8f5d94] | 78 |   QSplitter *splitter = new QSplitter (Qt::Horizontal, this );
 | 
|---|
| [0ac3e3] | 79 |   layout->addWidget(splitter);
 | 
|---|
 | 80 | 
 | 
|---|
 | 81 |   // tree widget
 | 
|---|
| [a8f5d94] | 82 |   treewidget = new QTreeWidget (splitter);
 | 
|---|
| [0ac3e3] | 83 |   treewidget->setSelectionMode( QTreeWidget::SingleSelection );
 | 
|---|
 | 84 |   treewidget->setColumnCount(COLUMNCOUNT);
 | 
|---|
| [3b1798] | 85 |   //treewidget->setSizePolicy( QSizePolicy::Minimum, sizePolicy().verticalPolicy() );
 | 
|---|
| [0ac3e3] | 86 |   QStringList header;
 | 
|---|
 | 87 |   for(int i=0; i<COLUMNCOUNT;++i)
 | 
|---|
 | 88 |     header << COLUMNNAMES[i];
 | 
|---|
 | 89 |   treewidget->setHeaderLabels(header);
 | 
|---|
| [a8f5d94] | 90 |   splitter->addWidget(treewidget);
 | 
|---|
| [b677ab] | 91 | 
 | 
|---|
| [0ac3e3] | 92 |   // plot widget
 | 
|---|
| [b677ab] | 93 | #ifdef HAVE_QWT
 | 
|---|
 | 94 |   widget = new QSeisPlotPage ("energy", splitter);
 | 
|---|
| [3b1798] | 95 |   //widget->setSizePolicy( QSizePolicy::MinimumExpanding, sizePolicy().verticalPolicy() );
 | 
|---|
| [b677ab] | 96 |   QSeisPageRegistry::getInstance().registerInstance(widget);
 | 
|---|
 | 97 | #else
 | 
|---|
 | 98 |   widget = new QWidget(splitter);
 | 
|---|
 | 99 | #endif
 | 
|---|
| [a8f5d94] | 100 |   splitter->addWidget(widget);
 | 
|---|
| [1e1098] | 101 | 
 | 
|---|
| [3b1798] | 102 |   dirty = true;
 | 
|---|
| [52bb5a] | 103 |   clearing = false;
 | 
|---|
| [3b1798] | 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();
 | 
|---|
| [52bb5a] | 110 | 
 | 
|---|
| [1e1098] | 111 | 
 | 
|---|
 | 112 |         HomologyContainer &homologies = World::getInstance().getHomologies();
 | 
|---|
 | 113 |         homologies.signOn(this);
 | 
|---|
| [f0964c] | 114 |         PotentialRegistry::getInstance().signOn(this);
 | 
|---|
 | 115 |         potentialregistry_enabled = true;
 | 
|---|
| [1e1098] | 116 | 
 | 
|---|
| [b677ab] | 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 | 
 | 
|---|
| [a8f5d94] | 124 |   connect(treewidget,SIGNAL(itemSelectionChanged()),this,SLOT(rowSelected()));
 | 
|---|
| [1e1098] | 125 |   connect(this,SIGNAL(changed()),this,SLOT(update()));
 | 
|---|
| [9d4f964] | 126 |   connect(this,SIGNAL(needsRefill()),this,SLOT(refill()), Qt::QueuedConnection);
 | 
|---|
 | 127 | 
 | 
|---|
 | 128 |   emit needsRefill();
 | 
|---|
| [1e1098] | 129 | }
 | 
|---|
 | 130 | 
 | 
|---|
 | 131 | QtHomologyList::~QtHomologyList()
 | 
|---|
 | 132 | {
 | 
|---|
| [3b1798] | 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 | 
 | 
|---|
| [1e1098] | 140 |   HomologyContainer &homologies = World::getInstance().getHomologies();
 | 
|---|
 | 141 |   homologies.signOff(this);
 | 
|---|
| [f0964c] | 142 |   if (potentialregistry_enabled)
 | 
|---|
 | 143 |     PotentialRegistry::getInstance().signOff(this);
 | 
|---|
| [1e1098] | 144 | }
 | 
|---|
 | 145 | 
 | 
|---|
 | 146 | void QtHomologyList::update(Observable *publisher) {
 | 
|---|
 | 147 | 
 | 
|---|
 | 148 |   dirty = true;
 | 
|---|
 | 149 | 
 | 
|---|
 | 150 |   // force an update from Qt...
 | 
|---|
| [b677ab] | 151 | //  clearing = true;
 | 
|---|
 | 152 | //  treewidget->clear();
 | 
|---|
 | 153 | //  clearing = false;
 | 
|---|
 | 154 |   emit changed(); //doesn't work!?!
 | 
|---|
| [1e1098] | 155 | }
 | 
|---|
 | 156 | 
 | 
|---|
 | 157 | void QtHomologyList::refill()
 | 
|---|
 | 158 | {
 | 
|---|
| [52bb5a] | 159 |   clearing = true;
 | 
|---|
| [1e1098] | 160 |   const HomologyContainer &homologies = World::getInstance().getHomologies();
 | 
|---|
 | 161 | 
 | 
|---|
 | 162 |   // clear everything
 | 
|---|
 | 163 |   HomologySelection.clear();
 | 
|---|
| [a8f5d94] | 164 |   treewidget->clear();
 | 
|---|
| [b677ab] | 165 | #ifdef HAVE_QWT
 | 
|---|
 | 166 |   QSeisCurveRegistry::getInstance().resetRegistry();
 | 
|---|
 | 167 | #endif
 | 
|---|
| [1e1098] | 168 | 
 | 
|---|
 | 169 |   size_t count = 0;
 | 
|---|
| [b677ab] | 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);
 | 
|---|
| [1e1098] | 174 |     const HomologyGraph &graph = occurences.first->first;
 | 
|---|
 | 175 |     const size_t times = std::distance(occurences.first, occurences.second);
 | 
|---|
 | 176 | 
 | 
|---|
| [b677ab] | 177 |     // create item
 | 
|---|
| [a8f5d94] | 178 |     QTreeWidgetItem *treeItem = new QTreeWidgetItem(treewidget);
 | 
|---|
| [1e1098] | 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());
 | 
|---|
| [b677ab] | 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
 | 
|---|
| [e1fe7e] | 207 |       const TrainingData::InputVector_t &inputs = data.getAllArguments();
 | 
|---|
| [b677ab] | 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();
 | 
|---|
| [9bdcc9] | 221 |         const FunctionModel::filter_t filter = potential.getSpecificFilter();
 | 
|---|
| [b677ab] | 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();
 | 
|---|
| [9bdcc9] | 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)
 | 
|---|
| [b677ab] | 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
 | 
|---|
| [1e1098] | 279 |   }
 | 
|---|
 | 280 |   dirty = false;
 | 
|---|
| [52bb5a] | 281 |   clearing = false;
 | 
|---|
| [1e1098] | 282 | }
 | 
|---|
 | 283 | 
 | 
|---|
 | 284 | void QtHomologyList::paintEvent(QPaintEvent * event)
 | 
|---|
 | 285 | {
 | 
|---|
 | 286 |   if (dirty)
 | 
|---|
 | 287 |     refill();
 | 
|---|
| [a8f5d94] | 288 | //  treewidget->paintEvent(event);
 | 
|---|
| [1e1098] | 289 | }
 | 
|---|
 | 290 | 
 | 
|---|
 | 291 | void QtHomologyList::subjectKilled(Observable *publisher)
 | 
|---|
 | 292 | {
 | 
|---|
 | 293 |   // as a new instance should always already be present ... just sign on
 | 
|---|
| [f0964c] | 294 |   if (static_cast<PotentialRegistry *>(publisher) == PotentialRegistry::getPointer()) {
 | 
|---|
 | 295 |     potentialregistry_enabled = false;
 | 
|---|
 | 296 |   } else {
 | 
|---|
 | 297 |     // its HomologyContainer
 | 
|---|
 | 298 |   }
 | 
|---|
| [1e1098] | 299 | }
 | 
|---|
 | 300 | 
 | 
|---|
 | 301 | 
 | 
|---|
 | 302 | void QtHomologyList::rowSelected()
 | 
|---|
 | 303 | {
 | 
|---|
 | 304 |   //std::cout << "rowSelected\n";
 | 
|---|
| [a8f5d94] | 305 |   for (int i=0;i<treewidget->topLevelItemCount();i++){
 | 
|---|
 | 306 |     QTreeWidgetItem *item = treewidget->topLevelItem(i);
 | 
|---|
| [1e1098] | 307 |     bool newSelection = item->isSelected();
 | 
|---|
 | 308 |     if (newSelection != HomologySelection[i]){
 | 
|---|
| [b677ab] | 309 |       // TODO: Add selected curve to QTabWidget
 | 
|---|
| [1e1098] | 310 |     }
 | 
|---|
 | 311 |   }
 | 
|---|
 | 312 | }
 | 
|---|
 | 313 | 
 | 
|---|