| 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 | * | 
|---|
| 26 | *  Created on: Jun 24, 2013 | 
|---|
| 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 | #include "Views/Qt4/QtNumericalItem.hpp" | 
|---|
| 37 |  | 
|---|
| 38 | #include <QAbstractItemView> | 
|---|
| 39 | #include <QtGui/QTreeWidget> | 
|---|
| 40 | #include <QtGui/QTabWidget> | 
|---|
| 41 | #include <Qt/qsettings.h> | 
|---|
| 42 | #include <Qt/qsplitter.h> | 
|---|
| 43 | #include <Qt/qboxlayout.h> | 
|---|
| 44 |  | 
|---|
| 45 | //#include "CodePatterns/MemDebug.hpp" | 
|---|
| 46 |  | 
|---|
| 47 | #include <iterator> | 
|---|
| 48 | #include <iostream> | 
|---|
| 49 |  | 
|---|
| 50 | #include <boost/assign.hpp> | 
|---|
| 51 |  | 
|---|
| 52 | #include "CodePatterns/Log.hpp" | 
|---|
| 53 |  | 
|---|
| 54 | #include "Fragmentation/Homology/HomologyContainer.hpp" | 
|---|
| 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" | 
|---|
| 60 | #include "Potentials/PotentialRegistry.hpp" | 
|---|
| 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 | 
|---|
| 68 | #include "World.hpp" | 
|---|
| 69 |  | 
|---|
| 70 | using namespace std; | 
|---|
| 71 |  | 
|---|
| 72 | using namespace boost::assign; | 
|---|
| 73 |  | 
|---|
| 74 | const int QtHomologyList::COLUMNCOUNT = COLUMNTYPES_MAX; | 
|---|
| 75 | const char *QtHomologyList::COLUMNNAMES[QtHomologyList::COLUMNCOUNT]={"Number","Nodes","Edges","Occurrence"}; | 
|---|
| 76 |  | 
|---|
| 77 | QtHomologyList::QtHomologyList(QWidget * _parent) : | 
|---|
| 78 | QWidget(_parent), | 
|---|
| 79 | Observer("QtHomologyList"), | 
|---|
| 80 | homologycontainer_enabled(false), | 
|---|
| 81 | potentialregistry_enabled(false) | 
|---|
| 82 | { | 
|---|
| 83 | QHBoxLayout* layout = new QHBoxLayout(this); | 
|---|
| 84 | QSplitter *splitter = new QSplitter (Qt::Horizontal, this ); | 
|---|
| 85 | layout->addWidget(splitter); | 
|---|
| 86 |  | 
|---|
| 87 | // tree widget | 
|---|
| 88 | treewidget = new QTreeWidget (splitter); | 
|---|
| 89 | treewidget->setSelectionMode( QTreeWidget::SingleSelection ); | 
|---|
| 90 | treewidget->setColumnCount(COLUMNCOUNT); | 
|---|
| 91 | treewidget->setSortingEnabled(true); | 
|---|
| 92 | //treewidget->setSizePolicy( QSizePolicy::Minimum, sizePolicy().verticalPolicy() ); | 
|---|
| 93 | QStringList header; | 
|---|
| 94 | for(int i=0; i<COLUMNCOUNT;++i) | 
|---|
| 95 | header << COLUMNNAMES[i]; | 
|---|
| 96 | treewidget->setHeaderLabels(header); | 
|---|
| 97 | treewidget->sortByColumn(0); | 
|---|
| 98 | splitter->addWidget(treewidget); | 
|---|
| 99 |  | 
|---|
| 100 | // plot widget | 
|---|
| 101 | #ifdef HAVE_QWT | 
|---|
| 102 | widget = new QSeisPlotPage ("energy", splitter); | 
|---|
| 103 | //widget->setSizePolicy( QSizePolicy::MinimumExpanding, sizePolicy().verticalPolicy() ); | 
|---|
| 104 | QSeisPageRegistry::getInstance().registerInstance(widget); | 
|---|
| 105 | #else | 
|---|
| 106 | widget = new QWidget(splitter); | 
|---|
| 107 | #endif | 
|---|
| 108 | splitter->addWidget(widget); | 
|---|
| 109 |  | 
|---|
| 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(); | 
|---|
| 117 |  | 
|---|
| 118 |  | 
|---|
| 119 | HomologyContainer &homologies = World::getInstance().getHomologies(); | 
|---|
| 120 | homologies.signOn(this); | 
|---|
| 121 | homologycontainer_enabled = true; | 
|---|
| 122 | PotentialRegistry::getInstance().signOn(this); | 
|---|
| 123 | potentialregistry_enabled = true; | 
|---|
| 124 |  | 
|---|
| 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 |  | 
|---|
| 132 | connect(treewidget,SIGNAL(itemSelectionChanged()),this,SLOT(rowSelected())); | 
|---|
| 133 | connect(this,SIGNAL(changed()),this,SLOT(update())); | 
|---|
| 134 | connect(this,SIGNAL(needsRefill()),this,SLOT(refill()), Qt::QueuedConnection); | 
|---|
| 135 |  | 
|---|
| 136 | emit needsRefill(); | 
|---|
| 137 | } | 
|---|
| 138 |  | 
|---|
| 139 | QtHomologyList::~QtHomologyList() | 
|---|
| 140 | { | 
|---|
| 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 |  | 
|---|
| 148 | if (homologycontainer_enabled) { | 
|---|
| 149 | HomologyContainer &homologies = World::getInstance().getHomologies(); | 
|---|
| 150 | homologies.signOff(this); | 
|---|
| 151 | } | 
|---|
| 152 | if (potentialregistry_enabled) | 
|---|
| 153 | PotentialRegistry::getInstance().signOff(this); | 
|---|
| 154 | } | 
|---|
| 155 |  | 
|---|
| 156 | void QtHomologyList::update(Observable *publisher) | 
|---|
| 157 | { | 
|---|
| 158 | dirty = true; | 
|---|
| 159 |  | 
|---|
| 160 | // force an update from Qt... | 
|---|
| 161 | //  treewidget->clear(); | 
|---|
| 162 | emit changed(); //doesn't work!?! | 
|---|
| 163 | } | 
|---|
| 164 |  | 
|---|
| 165 | void QtHomologyList::refill() | 
|---|
| 166 | { | 
|---|
| 167 | boost::recursive_mutex::scoped_lock lock(refill_mutex); | 
|---|
| 168 |  | 
|---|
| 169 | const HomologyContainer &homologies = World::getInstance().getHomologies(); | 
|---|
| 170 |  | 
|---|
| 171 | // clear everything | 
|---|
| 172 | HomologySelection.clear(); | 
|---|
| 173 | treewidget->clear(); | 
|---|
| 174 | #ifdef HAVE_QWT | 
|---|
| 175 | QSeisCurveRegistry::getInstance().resetRegistry(); | 
|---|
| 176 | #endif | 
|---|
| 177 |  | 
|---|
| 178 | size_t count = 0; | 
|---|
| 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); | 
|---|
| 183 | const HomologyGraph &graph = occurences.first->first; | 
|---|
| 184 | const size_t times = std::distance(occurences.first, occurences.second); | 
|---|
| 185 |  | 
|---|
| 186 | // create item | 
|---|
| 187 | std::vector<int> numerical_columns; | 
|---|
| 188 | numerical_columns += OCCURRENCE; | 
|---|
| 189 | QTreeWidgetItem *treeItem = new QtNumericalItem(NUMBER, numerical_columns, treewidget); | 
|---|
| 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()); | 
|---|
| 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 | 
|---|
| 218 | const TrainingData::InputVector_t &inputs = data.getAllArguments(); | 
|---|
| 219 | const TrainingData::OutputVector_t &outputs = data.getTrainingOutputs(); | 
|---|
| 220 | std::vector<double> xvalues; | 
|---|
| 221 | std::vector<double> yvalues; | 
|---|
| 222 | for (TrainingData::OutputVector_t::const_iterator outputiter = outputs.begin(); | 
|---|
| 223 | outputiter != outputs.end(); ++outputiter) | 
|---|
| 224 | yvalues.push_back((*outputiter)[0]); | 
|---|
| 225 |  | 
|---|
| 226 | // go through each potential | 
|---|
| 227 | for (CompoundPotential::models_t::const_iterator potiter = compound->begin(); | 
|---|
| 228 | potiter != compound->end(); | 
|---|
| 229 | ++potiter) { | 
|---|
| 230 | const EmpiricalPotential &potential = dynamic_cast<const EmpiricalPotential &>(**potiter); | 
|---|
| 231 | const std::string potentialname = potential.getName(); | 
|---|
| 232 | const FunctionModel::filter_t filter = potential.getSpecificFilter(); | 
|---|
| 233 | Coordinator::ptr coordinator = potential.getCoordinator(); | 
|---|
| 234 | // then we need to sample the potential | 
|---|
| 235 | HomologyGraph dummy; | 
|---|
| 236 | xvalues.clear(); | 
|---|
| 237 | for (TrainingData::InputVector_t::const_iterator inputiter = inputs.begin(); | 
|---|
| 238 | inputiter != inputs.end(); ++inputiter) { | 
|---|
| 239 | const FunctionModel::list_of_arguments_t specificargs = filter(dummy, *inputiter); | 
|---|
| 240 | double average = 0.; | 
|---|
| 241 | for (FunctionModel::list_of_arguments_t::const_iterator argiter = specificargs.begin(); | 
|---|
| 242 | argiter != specificargs.end(); ++argiter) { | 
|---|
| 243 | const FunctionModel::arguments_t args = *argiter; | 
|---|
| 244 | average += (*coordinator)(args); | 
|---|
| 245 | } | 
|---|
| 246 | if (specificargs.size() > 1) { | 
|---|
| 247 | const size_t index =  xvalues.size(); | 
|---|
| 248 | xvalues.push_back(average/(double)specificargs.size()); | 
|---|
| 249 | yvalues[index] *= 1./(double)specificargs.size(); | 
|---|
| 250 | } | 
|---|
| 251 | } | 
|---|
| 252 |  | 
|---|
| 253 | // We need to sort the xvalues (and associated yvalues also) | 
|---|
| 254 | std::vector<double>::const_iterator xiter = xvalues.begin(); | 
|---|
| 255 | std::vector<double>::const_iterator yiter = yvalues.begin(); | 
|---|
| 256 | std::map<double, std::set<double> > sorted_xy; | 
|---|
| 257 | for (;xiter != xvalues.end(); ++xiter, ++yiter) { | 
|---|
| 258 | std::set<double> yset; | 
|---|
| 259 | yset.insert(*yiter); | 
|---|
| 260 | std::pair<std::map<double, std::set<double> >::iterator, bool> inserter = | 
|---|
| 261 | sorted_xy.insert(std::make_pair(*xiter, yset)); | 
|---|
| 262 | if (!inserter.second) | 
|---|
| 263 | inserter.first->second.insert(*yiter); | 
|---|
| 264 | } | 
|---|
| 265 | xvalues.clear(); | 
|---|
| 266 | yvalues.clear(); | 
|---|
| 267 | for (std::map<double, std::set<double> >::const_iterator iter = sorted_xy.begin(); | 
|---|
| 268 | iter != sorted_xy.end(); ++iter) { | 
|---|
| 269 | for (std::set<double>::const_iterator valueiter = iter->second.begin(); | 
|---|
| 270 | valueiter != iter->second.end(); | 
|---|
| 271 | ++valueiter) { | 
|---|
| 272 | xvalues.push_back(iter->first); | 
|---|
| 273 | yvalues.push_back(*valueiter); | 
|---|
| 274 | } | 
|---|
| 275 | } | 
|---|
| 276 |  | 
|---|
| 277 | if ((!xvalues.empty()) && (!yvalues.empty())) { | 
|---|
| 278 | QSeisData data(xvalues, yvalues, QString(potentialname.c_str())); | 
|---|
| 279 | // couple to QSeisPlotCurve and register the curve | 
|---|
| 280 | QSeisPlotCurve *curve = new QSeisPlotCurve(QString(potentialname.c_str()), "energy"); | 
|---|
| 281 | curve->updateCurve(&data); | 
|---|
| 282 | if (!QSeisCurveRegistry::getInstance().isPresentByName(curve->getName())) | 
|---|
| 283 | QSeisCurveRegistry::getInstance().registerInstance(curve); | 
|---|
| 284 | else | 
|---|
| 285 | delete curve; | 
|---|
| 286 | // couple to QSeisPlotPage | 
|---|
| 287 | widget->addCurve(potentialname); | 
|---|
| 288 | } | 
|---|
| 289 | } | 
|---|
| 290 | } | 
|---|
| 291 | #endif | 
|---|
| 292 | } | 
|---|
| 293 | dirty = false; | 
|---|
| 294 | } | 
|---|
| 295 |  | 
|---|
| 296 | void QtHomologyList::paintEvent(QPaintEvent * event) | 
|---|
| 297 | { | 
|---|
| 298 | boost::recursive_mutex::scoped_lock lock(refill_mutex); | 
|---|
| 299 |  | 
|---|
| 300 | if (dirty) | 
|---|
| 301 | refill(); | 
|---|
| 302 | //  treewidget->paintEvent(event); | 
|---|
| 303 | } | 
|---|
| 304 |  | 
|---|
| 305 | void QtHomologyList::subjectKilled(Observable *publisher) | 
|---|
| 306 | { | 
|---|
| 307 | // as a new instance should always already be present ... just sign on | 
|---|
| 308 | if (static_cast<PotentialRegistry *>(publisher) == PotentialRegistry::getPointer()) { | 
|---|
| 309 | potentialregistry_enabled = false; | 
|---|
| 310 | } else if (static_cast<HomologyContainer *>(publisher) == &World::getInstance().getHomologies()) { | 
|---|
| 311 | homologycontainer_enabled = false; | 
|---|
| 312 | } | 
|---|
| 313 | } | 
|---|
| 314 |  | 
|---|
| 315 |  | 
|---|
| 316 | void QtHomologyList::rowSelected() | 
|---|
| 317 | { | 
|---|
| 318 | boost::recursive_mutex::scoped_lock lock(refill_mutex); | 
|---|
| 319 |  | 
|---|
| 320 | //std::cout << "rowSelected\n"; | 
|---|
| 321 | for (int i=0;i<treewidget->topLevelItemCount();i++){ | 
|---|
| 322 | QTreeWidgetItem *item = treewidget->topLevelItem(i); | 
|---|
| 323 | bool newSelection = item->isSelected(); | 
|---|
| 324 | if (newSelection != HomologySelection[i]){ | 
|---|
| 325 | // TODO: Add selected curve to QTabWidget | 
|---|
| 326 | } | 
|---|
| 327 | } | 
|---|
| 328 | } | 
|---|
| 329 |  | 
|---|