| 1 | /* | 
|---|
| 2 | * Project: MoleCuilder | 
|---|
| 3 | * Description: creates and alters molecular systems | 
|---|
| 4 | * Copyright (C)  2025 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 | * QtPotentialList.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/QtPotentialList.hpp" | 
|---|
| 36 | #include "Views/Qt4/QtNumericalItem.hpp" | 
|---|
| 37 |  | 
|---|
| 38 | #include <QtGui/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 <iostream> | 
|---|
| 48 |  | 
|---|
| 49 | #include "CodePatterns/Log.hpp" | 
|---|
| 50 |  | 
|---|
| 51 | #include "Potentials/EmpiricalPotential.hpp" | 
|---|
| 52 | #include "Potentials/PotentialRegistry.hpp" | 
|---|
| 53 |  | 
|---|
| 54 | using namespace std; | 
|---|
| 55 |  | 
|---|
| 56 | const int QtPotentialList::COLUMNCOUNT = COLUMNTYPES_MAX; | 
|---|
| 57 | const char *QtPotentialList::COLUMNNAMES[QtPotentialList::COLUMNCOUNT]={"Number","Name","Charges","Parameters"}; | 
|---|
| 58 |  | 
|---|
| 59 | QtPotentialList::QtPotentialList(QWidget * _parent) : | 
|---|
| 60 | QWidget(_parent), | 
|---|
| 61 | Observer("QtPotentialList"), | 
|---|
| 62 | potentialregistry_enabled(false) | 
|---|
| 63 | { | 
|---|
| 64 | QHBoxLayout* layout = new QHBoxLayout(this); | 
|---|
| 65 | QSplitter *splitter = new QSplitter (Qt::Horizontal, this ); | 
|---|
| 66 | layout->addWidget(splitter); | 
|---|
| 67 |  | 
|---|
| 68 | // tree widget | 
|---|
| 69 | treewidget = new QTreeWidget (splitter); | 
|---|
| 70 | treewidget->setSelectionMode( QTreeWidget::SingleSelection ); | 
|---|
| 71 | treewidget->setColumnCount(COLUMNCOUNT); | 
|---|
| 72 | treewidget->setSortingEnabled(true); | 
|---|
| 73 | //treewidget->setSizePolicy( QSizePolicy::Minimum, sizePolicy().verticalPolicy() ); | 
|---|
| 74 | QStringList header; | 
|---|
| 75 | for(int i=0; i<COLUMNCOUNT;++i) | 
|---|
| 76 | header << COLUMNNAMES[i]; | 
|---|
| 77 | treewidget->setHeaderLabels(header); | 
|---|
| 78 | treewidget->sortByColumn(0); | 
|---|
| 79 | splitter->addWidget(treewidget); | 
|---|
| 80 |  | 
|---|
| 81 | dirty = true; | 
|---|
| 82 |  | 
|---|
| 83 | QSettings settings; | 
|---|
| 84 | settings.beginGroup("QtPotentialList"); | 
|---|
| 85 | treewidget->resize(settings.value("treewidget_size", QSize(width()/2, 20)).toSize()); | 
|---|
| 86 | settings.endGroup(); | 
|---|
| 87 |  | 
|---|
| 88 | PotentialRegistry::getInstance().signOn(this); | 
|---|
| 89 | potentialregistry_enabled = true; | 
|---|
| 90 |  | 
|---|
| 91 | connect(this,SIGNAL(changed()),this,SLOT(update())); | 
|---|
| 92 | connect(this,SIGNAL(needsRefill()),this,SLOT(refill()), Qt::QueuedConnection); | 
|---|
| 93 |  | 
|---|
| 94 | emit needsRefill(); | 
|---|
| 95 | } | 
|---|
| 96 |  | 
|---|
| 97 | QtPotentialList::~QtPotentialList() | 
|---|
| 98 | { | 
|---|
| 99 | QSettings settings; | 
|---|
| 100 | settings.beginGroup("QtPotentialList"); | 
|---|
| 101 | settings.setValue("treewidget_size", treewidget->size()); | 
|---|
| 102 | settings.endGroup(); | 
|---|
| 103 |  | 
|---|
| 104 |  | 
|---|
| 105 | if (potentialregistry_enabled) | 
|---|
| 106 | PotentialRegistry::getInstance().signOff(this); | 
|---|
| 107 | } | 
|---|
| 108 |  | 
|---|
| 109 | void QtPotentialList::update(Observable *publisher) | 
|---|
| 110 | { | 
|---|
| 111 | dirty = true; | 
|---|
| 112 |  | 
|---|
| 113 | // force an update from Qt... | 
|---|
| 114 | //  treewidget->clear(); | 
|---|
| 115 | emit changed(); //doesn't work!?! | 
|---|
| 116 | } | 
|---|
| 117 |  | 
|---|
| 118 | void QtPotentialList::refill() | 
|---|
| 119 | { | 
|---|
| 120 | boost::recursive_mutex::scoped_lock lock(refill_mutex); | 
|---|
| 121 |  | 
|---|
| 122 | const PotentialRegistry ®istry = PotentialRegistry::getConstInstance(); | 
|---|
| 123 |  | 
|---|
| 124 | // clear everything | 
|---|
| 125 | treewidget->clear(); | 
|---|
| 126 |  | 
|---|
| 127 | size_t count = 0; | 
|---|
| 128 | for (PotentialRegistry::const_iterator potentialiter = registry.getBeginIter(); | 
|---|
| 129 | potentialiter != registry.getEndIter(); ++potentialiter) { | 
|---|
| 130 | const EmpiricalPotential * const potential = potentialiter->second; | 
|---|
| 131 |  | 
|---|
| 132 | // create item | 
|---|
| 133 | QTreeWidgetItem *treeItem = new QTreeWidgetItem(treewidget); | 
|---|
| 134 | treeItem->setText(NUMBER, QString::number(count)); | 
|---|
| 135 | { | 
|---|
| 136 | std::stringstream output; | 
|---|
| 137 | output << potential->getName(); | 
|---|
| 138 | treeItem->setText(NAME, QString(output.str().c_str())); | 
|---|
| 139 | } | 
|---|
| 140 | { | 
|---|
| 141 | std::stringstream output; | 
|---|
| 142 | output << potential->getParticleTypes(); | 
|---|
| 143 | treeItem->setText(CHARGES, QString(output.str().c_str())); | 
|---|
| 144 | } | 
|---|
| 145 | { | 
|---|
| 146 | std::stringstream output; | 
|---|
| 147 | output << potential->getParameters(); | 
|---|
| 148 | treeItem->setText(PARAMETERS, QString(output.str().c_str())); | 
|---|
| 149 | } | 
|---|
| 150 | } | 
|---|
| 151 | dirty = false; | 
|---|
| 152 | } | 
|---|
| 153 |  | 
|---|
| 154 | void QtPotentialList::paintEvent(QPaintEvent * event) | 
|---|
| 155 | { | 
|---|
| 156 | boost::recursive_mutex::scoped_lock lock(refill_mutex); | 
|---|
| 157 |  | 
|---|
| 158 | if (dirty) | 
|---|
| 159 | refill(); | 
|---|
| 160 | //  treewidget->paintEvent(event); | 
|---|
| 161 | } | 
|---|
| 162 |  | 
|---|
| 163 | void QtPotentialList::subjectKilled(Observable *publisher) | 
|---|
| 164 | { | 
|---|
| 165 | // as a new instance should always already be present ... just sign on | 
|---|
| 166 | if (static_cast<PotentialRegistry *>(publisher) == PotentialRegistry::getPointer()) { | 
|---|
| 167 | potentialregistry_enabled = false; | 
|---|
| 168 | } | 
|---|
| 169 | } | 
|---|