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