| [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 |  | 
|---|
| [454b555] | 38 | #include <QtGui/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"), | 
|---|
| [3c9ac3] | 80 | homologycontainer_enabled(false), | 
|---|
| [f0964c] | 81 | potentialregistry_enabled(false) | 
|---|
| [1e1098] | 82 | { | 
|---|
| [3b1798] | 83 | QHBoxLayout* layout = new QHBoxLayout(this); | 
|---|
| [a8f5d94] | 84 | QSplitter *splitter = new QSplitter (Qt::Horizontal, this ); | 
|---|
| [0ac3e3] | 85 | layout->addWidget(splitter); | 
|---|
|  | 86 |  | 
|---|
|  | 87 | // tree widget | 
|---|
| [a8f5d94] | 88 | treewidget = new QTreeWidget (splitter); | 
|---|
| [0ac3e3] | 89 | treewidget->setSelectionMode( QTreeWidget::SingleSelection ); | 
|---|
|  | 90 | treewidget->setColumnCount(COLUMNCOUNT); | 
|---|
| [562b0a] | 91 | treewidget->setSortingEnabled(true); | 
|---|
| [3b1798] | 92 | //treewidget->setSizePolicy( QSizePolicy::Minimum, sizePolicy().verticalPolicy() ); | 
|---|
| [0ac3e3] | 93 | QStringList header; | 
|---|
|  | 94 | for(int i=0; i<COLUMNCOUNT;++i) | 
|---|
|  | 95 | header << COLUMNNAMES[i]; | 
|---|
|  | 96 | treewidget->setHeaderLabels(header); | 
|---|
| [562b0a] | 97 | treewidget->sortByColumn(0); | 
|---|
| [a8f5d94] | 98 | splitter->addWidget(treewidget); | 
|---|
| [b677ab] | 99 |  | 
|---|
| [0ac3e3] | 100 | // plot widget | 
|---|
| [b677ab] | 101 | #ifdef HAVE_QWT | 
|---|
|  | 102 | widget = new QSeisPlotPage ("energy", splitter); | 
|---|
| [3b1798] | 103 | //widget->setSizePolicy( QSizePolicy::MinimumExpanding, sizePolicy().verticalPolicy() ); | 
|---|
| [b677ab] | 104 | QSeisPageRegistry::getInstance().registerInstance(widget); | 
|---|
|  | 105 | #else | 
|---|
|  | 106 | widget = new QWidget(splitter); | 
|---|
|  | 107 | #endif | 
|---|
| [a8f5d94] | 108 | splitter->addWidget(widget); | 
|---|
| [1e1098] | 109 |  | 
|---|
| [3b1798] | 110 | dirty = true; | 
|---|
|  | 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); | 
|---|
| [3c9ac3] | 121 | homologycontainer_enabled = true; | 
|---|
| [f0964c] | 122 | PotentialRegistry::getInstance().signOn(this); | 
|---|
|  | 123 | potentialregistry_enabled = true; | 
|---|
| [1e1098] | 124 |  | 
|---|
| [b677ab] | 125 | #ifdef HAVE_QWT | 
|---|
|  | 126 | //connect the PlotCurveRegistry directly to the PlotPage registry | 
|---|
|  | 127 | connect(QSeisCurveRegistry::getPointer(), SIGNAL(curveAdded(std::string, QString)), QSeisPageRegistry::getPointer(), SLOT(addCurve(std::string, QString))); | 
|---|
|  | 128 | connect(QSeisCurveRegistry::getPointer(), SIGNAL(curveRemoved(std::string, QString)), QSeisPageRegistry::getPointer(), SLOT(removeCurve(std::string, QString))); | 
|---|
|  | 129 | connect(QSeisCurveRegistry::getPointer(), SIGNAL(curveChanged(std::string, QString)), QSeisPageRegistry::getPointer(), SLOT(updateCurve(std::string, QString))); | 
|---|
|  | 130 | #endif | 
|---|
|  | 131 |  | 
|---|
| [a8f5d94] | 132 | connect(treewidget,SIGNAL(itemSelectionChanged()),this,SLOT(rowSelected())); | 
|---|
| [1e1098] | 133 | connect(this,SIGNAL(changed()),this,SLOT(update())); | 
|---|
| [9d4f964] | 134 | connect(this,SIGNAL(needsRefill()),this,SLOT(refill()), Qt::QueuedConnection); | 
|---|
|  | 135 |  | 
|---|
|  | 136 | emit needsRefill(); | 
|---|
| [1e1098] | 137 | } | 
|---|
|  | 138 |  | 
|---|
|  | 139 | QtHomologyList::~QtHomologyList() | 
|---|
|  | 140 | { | 
|---|
| [3b1798] | 141 | QSettings settings; | 
|---|
|  | 142 | settings.beginGroup("QtHomologyList"); | 
|---|
|  | 143 | settings.setValue("treewidget_size", treewidget->size()); | 
|---|
|  | 144 | settings.setValue("plotwidget_size", widget->size()); | 
|---|
|  | 145 | settings.endGroup(); | 
|---|
|  | 146 |  | 
|---|
|  | 147 |  | 
|---|
| [3c9ac3] | 148 | if (homologycontainer_enabled) { | 
|---|
|  | 149 | HomologyContainer &homologies = World::getInstance().getHomologies(); | 
|---|
|  | 150 | homologies.signOff(this); | 
|---|
|  | 151 | } | 
|---|
| [f0964c] | 152 | if (potentialregistry_enabled) | 
|---|
|  | 153 | PotentialRegistry::getInstance().signOff(this); | 
|---|
| [1e1098] | 154 | } | 
|---|
|  | 155 |  | 
|---|
| [7516f6] | 156 | void QtHomologyList::update(Observable *publisher) | 
|---|
|  | 157 | { | 
|---|
| [1e1098] | 158 | dirty = true; | 
|---|
|  | 159 |  | 
|---|
|  | 160 | // force an update from Qt... | 
|---|
| [b677ab] | 161 | //  treewidget->clear(); | 
|---|
|  | 162 | emit changed(); //doesn't work!?! | 
|---|
| [1e1098] | 163 | } | 
|---|
|  | 164 |  | 
|---|
|  | 165 | void QtHomologyList::refill() | 
|---|
|  | 166 | { | 
|---|
| [7516f6] | 167 | boost::recursive_mutex::scoped_lock lock(refill_mutex); | 
|---|
|  | 168 |  | 
|---|
| [1e1098] | 169 | const HomologyContainer &homologies = World::getInstance().getHomologies(); | 
|---|
|  | 170 |  | 
|---|
|  | 171 | // clear everything | 
|---|
|  | 172 | HomologySelection.clear(); | 
|---|
| [a8f5d94] | 173 | treewidget->clear(); | 
|---|
| [b677ab] | 174 | #ifdef HAVE_QWT | 
|---|
|  | 175 | QSeisCurveRegistry::getInstance().resetRegistry(); | 
|---|
|  | 176 | #endif | 
|---|
| [1e1098] | 177 |  | 
|---|
|  | 178 | size_t count = 0; | 
|---|
| [b677ab] | 179 | for (HomologyContainer::const_key_iterator homologyiter = homologies.key_begin(); | 
|---|
|  | 180 | homologyiter != homologies.key_end(); | 
|---|
|  | 181 | homologyiter = homologies.getNextKey(homologyiter), ++count) { | 
|---|
|  | 182 | HomologyContainer::range_t occurences = homologies.getHomologousGraphs(*homologyiter); | 
|---|
| [1e1098] | 183 | const HomologyGraph &graph = occurences.first->first; | 
|---|
|  | 184 | const size_t times = std::distance(occurences.first, occurences.second); | 
|---|
|  | 185 |  | 
|---|
| [b677ab] | 186 | // create item | 
|---|
| [4aed01] | 187 | std::vector<int> numerical_columns; | 
|---|
|  | 188 | numerical_columns += OCCURRENCE; | 
|---|
|  | 189 | QTreeWidgetItem *treeItem = new QtNumericalItem(NUMBER, numerical_columns, treewidget); | 
|---|
| [1e1098] | 190 | treeItem->setText(NUMBER, QString::number(count)); | 
|---|
|  | 191 | { | 
|---|
|  | 192 | std::stringstream output; | 
|---|
|  | 193 | graph.printNodes(output); | 
|---|
|  | 194 | treeItem->setText(NODES, QString(output.str().c_str())); | 
|---|
|  | 195 | } | 
|---|
|  | 196 | { | 
|---|
|  | 197 | std::stringstream output; | 
|---|
|  | 198 | graph.printEdges(output); | 
|---|
|  | 199 | treeItem->setText(EDGES, QString(output.str().c_str())); | 
|---|
|  | 200 | } | 
|---|
|  | 201 | if (times > 0) { | 
|---|
|  | 202 | treeItem->setText(OCCURRENCE, QString::number(times)); | 
|---|
|  | 203 | } else { | 
|---|
|  | 204 | treeItem->setText(OCCURRENCE, "none"); | 
|---|
|  | 205 | treeItem->setDisabled(true); | 
|---|
|  | 206 | } | 
|---|
|  | 207 | HomologySelection.push_back(treeItem->isSelected()); | 
|---|
| [b677ab] | 208 |  | 
|---|
|  | 209 | #ifdef HAVE_QWT | 
|---|
|  | 210 | // create associated curve in plot | 
|---|
|  | 211 | CompoundPotential *compound = new CompoundPotential(graph); | 
|---|
|  | 212 | ASSERT( compound != NULL, | 
|---|
|  | 213 | "QtHomologyList::refill() - compound is NULL."); | 
|---|
|  | 214 | TrainingData data(compound->getSpecificFilter()); | 
|---|
|  | 215 | data(homologies.getHomologousGraphs(graph)); | 
|---|
|  | 216 | if (!data.getTrainingInputs().empty()) { | 
|---|
|  | 217 | // generate QSeisData | 
|---|
| [e1fe7e] | 218 | const TrainingData::InputVector_t &inputs = data.getAllArguments(); | 
|---|
| [b677ab] | 219 | const TrainingData::OutputVector_t &outputs = data.getTrainingOutputs(); | 
|---|
|  | 220 | std::vector<double> xvalues; | 
|---|
|  | 221 | std::vector<double> yvalues; | 
|---|
|  | 222 |  | 
|---|
|  | 223 | // go through each potential | 
|---|
|  | 224 | for (CompoundPotential::models_t::const_iterator potiter = compound->begin(); | 
|---|
|  | 225 | potiter != compound->end(); | 
|---|
|  | 226 | ++potiter) { | 
|---|
|  | 227 | const EmpiricalPotential &potential = dynamic_cast<const EmpiricalPotential &>(**potiter); | 
|---|
|  | 228 | const std::string potentialname = potential.getName(); | 
|---|
| [9bdcc9] | 229 | const FunctionModel::filter_t filter = potential.getSpecificFilter(); | 
|---|
| [b677ab] | 230 | Coordinator::ptr coordinator = potential.getCoordinator(); | 
|---|
|  | 231 | // then we need to sample the potential | 
|---|
| [e60558] | 232 | HomologyGraph dummy; | 
|---|
| [476781] | 233 |  | 
|---|
| [b677ab] | 234 | xvalues.clear(); | 
|---|
| [476781] | 235 | yvalues.clear(); | 
|---|
|  | 236 | for (TrainingData::OutputVector_t::const_iterator outputiter = outputs.begin(); | 
|---|
|  | 237 | outputiter != outputs.end(); ++outputiter) | 
|---|
|  | 238 | yvalues.push_back((*outputiter)[0]); | 
|---|
| [b677ab] | 239 | for (TrainingData::InputVector_t::const_iterator inputiter = inputs.begin(); | 
|---|
| [9bdcc9] | 240 | inputiter != inputs.end(); ++inputiter) { | 
|---|
| [e60558] | 241 | const FunctionModel::list_of_arguments_t specificargs = filter(dummy, *inputiter); | 
|---|
| [9bdcc9] | 242 | double average = 0.; | 
|---|
|  | 243 | for (FunctionModel::list_of_arguments_t::const_iterator argiter = specificargs.begin(); | 
|---|
|  | 244 | argiter != specificargs.end(); ++argiter) { | 
|---|
|  | 245 | const FunctionModel::arguments_t args = *argiter; | 
|---|
|  | 246 | average += (*coordinator)(args); | 
|---|
|  | 247 | } | 
|---|
| [476781] | 248 | const size_t index =  xvalues.size(); | 
|---|
| [9bdcc9] | 249 | if (specificargs.size() > 1) { | 
|---|
|  | 250 | xvalues.push_back(average/(double)specificargs.size()); | 
|---|
| [476781] | 251 | } else if (specificargs.size() == 1) { | 
|---|
|  | 252 | xvalues.push_back(average); | 
|---|
|  | 253 | } else { | 
|---|
|  | 254 | xvalues.push_back(index/0.1); | 
|---|
| [9bdcc9] | 255 | } | 
|---|
|  | 256 | } | 
|---|
|  | 257 |  | 
|---|
|  | 258 | // We need to sort the xvalues (and associated yvalues also) | 
|---|
| [b677ab] | 259 | std::vector<double>::const_iterator xiter = xvalues.begin(); | 
|---|
|  | 260 | std::vector<double>::const_iterator yiter = yvalues.begin(); | 
|---|
|  | 261 | std::map<double, std::set<double> > sorted_xy; | 
|---|
|  | 262 | for (;xiter != xvalues.end(); ++xiter, ++yiter) { | 
|---|
|  | 263 | std::set<double> yset; | 
|---|
|  | 264 | yset.insert(*yiter); | 
|---|
|  | 265 | std::pair<std::map<double, std::set<double> >::iterator, bool> inserter = | 
|---|
|  | 266 | sorted_xy.insert(std::make_pair(*xiter, yset)); | 
|---|
|  | 267 | if (!inserter.second) | 
|---|
|  | 268 | inserter.first->second.insert(*yiter); | 
|---|
|  | 269 | } | 
|---|
|  | 270 | xvalues.clear(); | 
|---|
|  | 271 | yvalues.clear(); | 
|---|
|  | 272 | for (std::map<double, std::set<double> >::const_iterator iter = sorted_xy.begin(); | 
|---|
|  | 273 | iter != sorted_xy.end(); ++iter) { | 
|---|
|  | 274 | for (std::set<double>::const_iterator valueiter = iter->second.begin(); | 
|---|
|  | 275 | valueiter != iter->second.end(); | 
|---|
|  | 276 | ++valueiter) { | 
|---|
|  | 277 | xvalues.push_back(iter->first); | 
|---|
|  | 278 | yvalues.push_back(*valueiter); | 
|---|
|  | 279 | } | 
|---|
|  | 280 | } | 
|---|
|  | 281 |  | 
|---|
| [d88397] | 282 | if ((!xvalues.empty()) && (!yvalues.empty())) { | 
|---|
|  | 283 | QSeisData data(xvalues, yvalues, QString(potentialname.c_str())); | 
|---|
|  | 284 | // couple to QSeisPlotCurve and register the curve | 
|---|
|  | 285 | QSeisPlotCurve *curve = new QSeisPlotCurve(QString(potentialname.c_str()), "energy"); | 
|---|
|  | 286 | curve->updateCurve(&data); | 
|---|
|  | 287 | if (!QSeisCurveRegistry::getInstance().isPresentByName(curve->getName())) | 
|---|
|  | 288 | QSeisCurveRegistry::getInstance().registerInstance(curve); | 
|---|
|  | 289 | else | 
|---|
|  | 290 | delete curve; | 
|---|
|  | 291 | } | 
|---|
| [b677ab] | 292 | } | 
|---|
|  | 293 | } | 
|---|
|  | 294 | #endif | 
|---|
| [1e1098] | 295 | } | 
|---|
|  | 296 | dirty = false; | 
|---|
|  | 297 | } | 
|---|
|  | 298 |  | 
|---|
|  | 299 | void QtHomologyList::paintEvent(QPaintEvent * event) | 
|---|
|  | 300 | { | 
|---|
| [7516f6] | 301 | boost::recursive_mutex::scoped_lock lock(refill_mutex); | 
|---|
|  | 302 |  | 
|---|
| [1e1098] | 303 | if (dirty) | 
|---|
|  | 304 | refill(); | 
|---|
| [a8f5d94] | 305 | //  treewidget->paintEvent(event); | 
|---|
| [1e1098] | 306 | } | 
|---|
|  | 307 |  | 
|---|
|  | 308 | void QtHomologyList::subjectKilled(Observable *publisher) | 
|---|
|  | 309 | { | 
|---|
|  | 310 | // as a new instance should always already be present ... just sign on | 
|---|
| [f0964c] | 311 | if (static_cast<PotentialRegistry *>(publisher) == PotentialRegistry::getPointer()) { | 
|---|
|  | 312 | potentialregistry_enabled = false; | 
|---|
| [3c9ac3] | 313 | } else if (static_cast<HomologyContainer *>(publisher) == &World::getInstance().getHomologies()) { | 
|---|
|  | 314 | homologycontainer_enabled = false; | 
|---|
| [f0964c] | 315 | } | 
|---|
| [1e1098] | 316 | } | 
|---|
|  | 317 |  | 
|---|
|  | 318 |  | 
|---|
|  | 319 | void QtHomologyList::rowSelected() | 
|---|
|  | 320 | { | 
|---|
| [7516f6] | 321 | boost::recursive_mutex::scoped_lock lock(refill_mutex); | 
|---|
|  | 322 |  | 
|---|
| [1e1098] | 323 | //std::cout << "rowSelected\n"; | 
|---|
| [a8f5d94] | 324 | for (int i=0;i<treewidget->topLevelItemCount();i++){ | 
|---|
|  | 325 | QTreeWidgetItem *item = treewidget->topLevelItem(i); | 
|---|
| [1e1098] | 326 | bool newSelection = item->isSelected(); | 
|---|
|  | 327 | if (newSelection != HomologySelection[i]){ | 
|---|
| [b677ab] | 328 | // TODO: Add selected curve to QTabWidget | 
|---|
| [1e1098] | 329 | } | 
|---|
|  | 330 | } | 
|---|
|  | 331 | } | 
|---|
|  | 332 |  | 
|---|