| 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 | * QtFragmentList.cpp | 
|---|
| 25 | * | 
|---|
| 26 | *  Created on: Sep 03, 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/QtFragmentList.hpp" | 
|---|
| 36 |  | 
|---|
| 37 | #include <iterator> | 
|---|
| 38 | #include <iostream> | 
|---|
| 39 |  | 
|---|
| 40 | #include <QAbstractItemView> | 
|---|
| 41 |  | 
|---|
| 42 | #include "CodePatterns/MemDebug.hpp" | 
|---|
| 43 |  | 
|---|
| 44 | #include "CodePatterns/Log.hpp" | 
|---|
| 45 |  | 
|---|
| 46 | #include "Descriptors/AtomIdDescriptor.hpp" | 
|---|
| 47 | #include "Formula.hpp" | 
|---|
| 48 | #include "Fragmentation/KeySetsContainer.hpp" | 
|---|
| 49 | #include "Fragmentation/Summation/IndexSetContainer.hpp" | 
|---|
| 50 | #include "Fragmentation/Summation/Containers/FragmentationResultContainer.hpp" | 
|---|
| 51 | #include "Fragmentation/Summation/Containers/MPQCData.hpp" | 
|---|
| 52 | #include "World.hpp" | 
|---|
| 53 |  | 
|---|
| 54 | using namespace std; | 
|---|
| 55 |  | 
|---|
| 56 | const int QtFragmentList::COLUMNCOUNT = COLUMNTYPES_MAX; | 
|---|
| 57 | const char *QtFragmentList::COLUMNNAMES[QtFragmentList::COLUMNCOUNT]={"Number","KeySet","Formula","Energy value","Energy contribution"}; | 
|---|
| 58 |  | 
|---|
| 59 | QtFragmentList::QtFragmentList(QWidget * _parent) : | 
|---|
| 60 | QTreeWidget (_parent), | 
|---|
| 61 | Observer("QtFragmentList") | 
|---|
| 62 | { | 
|---|
| 63 | setColumnCount(COLUMNCOUNT); | 
|---|
| 64 | QStringList header; | 
|---|
| 65 | for(int i=0; i<COLUMNCOUNT;++i) | 
|---|
| 66 | header << COLUMNNAMES[i]; | 
|---|
| 67 | setHeaderLabels(header); | 
|---|
| 68 |  | 
|---|
| 69 | setSelectionMode( MultiSelection ); | 
|---|
| 70 |  | 
|---|
| 71 | dirty = true; | 
|---|
| 72 | clearing = false; | 
|---|
| 73 |  | 
|---|
| 74 | refill(); | 
|---|
| 75 |  | 
|---|
| 76 | FragmentationResultContainer &fragments = | 
|---|
| 77 | FragmentationResultContainer::getInstance(); | 
|---|
| 78 | fragments.signOn(this); | 
|---|
| 79 |  | 
|---|
| 80 | connect(this,SIGNAL(itemSelectionChanged()),this,SLOT(rowSelected())); | 
|---|
| 81 | connect(this,SIGNAL(changed()),this,SLOT(update())); | 
|---|
| 82 | } | 
|---|
| 83 |  | 
|---|
| 84 | QtFragmentList::~QtFragmentList() | 
|---|
| 85 | { | 
|---|
| 86 | FragmentationResultContainer &fragments = | 
|---|
| 87 | FragmentationResultContainer::getInstance(); | 
|---|
| 88 | fragments.signOff(this); | 
|---|
| 89 | } | 
|---|
| 90 |  | 
|---|
| 91 | void QtFragmentList::update(Observable *publisher) { | 
|---|
| 92 |  | 
|---|
| 93 | dirty = true; | 
|---|
| 94 |  | 
|---|
| 95 | // force an update from Qt... | 
|---|
| 96 | clearing = true; | 
|---|
| 97 | clear(); | 
|---|
| 98 | clearing = false; | 
|---|
| 99 | emit changed(); // doesn't work!?! | 
|---|
| 100 | } | 
|---|
| 101 |  | 
|---|
| 102 | void QtFragmentList::refill() | 
|---|
| 103 | { | 
|---|
| 104 | clearing = true; | 
|---|
| 105 | FragmentationResultContainer &fragments = | 
|---|
| 106 | FragmentationResultContainer::getInstance(); | 
|---|
| 107 |  | 
|---|
| 108 | // clear everything | 
|---|
| 109 | FragmentSelection.clear(); | 
|---|
| 110 | indexsets.clear(); | 
|---|
| 111 | clear(); | 
|---|
| 112 |  | 
|---|
| 113 | size_t count = 0; | 
|---|
| 114 | const IndexSetContainer keysets(fragments.getKeySets()); | 
|---|
| 115 | const FragmentationResultContainer::shortrangedata_t &shortrange = fragments.getShortRangeResults(); | 
|---|
| 116 | const FragmentationShortRangeResults::summedshortrange_t &summedshortrange = fragments.getShortRangeSummedResults(); | 
|---|
| 117 | IndexSetContainer::Container_t::const_iterator keyiter = keysets.getContainer().begin(); | 
|---|
| 118 | FragmentationResultContainer::shortrangedata_t::const_iterator resultiter = shortrange.begin(); | 
|---|
| 119 | for (;(keyiter != keysets.getContainer().end()) && (resultiter != shortrange.end()); | 
|---|
| 120 | ++keyiter,++resultiter,++count) { | 
|---|
| 121 |  | 
|---|
| 122 | QTreeWidgetItem *treeItem = new QTreeWidgetItem(this); | 
|---|
| 123 | treeItem->setText(NUMBER, QString::number(count)); | 
|---|
| 124 | { | 
|---|
| 125 | std::stringstream output; | 
|---|
| 126 | output << **keyiter; | 
|---|
| 127 | treeItem->setText(KEYSET, QString(output.str().c_str())); | 
|---|
| 128 | } | 
|---|
| 129 | { | 
|---|
| 130 | std::stringstream output; | 
|---|
| 131 | Formula formula; | 
|---|
| 132 | for (IndexSet::const_iterator indexiter = (*keyiter)->begin(); | 
|---|
| 133 | indexiter != (*keyiter)->end(); | 
|---|
| 134 | ++indexiter) { | 
|---|
| 135 | const atom *_atom = World::getInstance().getAtom(AtomById(*indexiter)); | 
|---|
| 136 | if (_atom != NULL) | 
|---|
| 137 | formula.addElements(_atom->getElementNo(), 1); | 
|---|
| 138 | else | 
|---|
| 139 | ELOG(2, "QtFragmentList::refill() - Could not find id " << *indexiter << " for formula."); | 
|---|
| 140 | } | 
|---|
| 141 | output << formula.toString(); | 
|---|
| 142 | treeItem->setText(FORMULA, QString(output.str().c_str())); | 
|---|
| 143 | } | 
|---|
| 144 | if (summedshortrange.empty()) { | 
|---|
| 145 | { | 
|---|
| 146 | std::stringstream output; | 
|---|
| 147 | output << resultiter->second.energies.total; | 
|---|
| 148 | treeItem->setText(ENERGYVALUE, QString(output.str().c_str())); | 
|---|
| 149 | } | 
|---|
| 150 | { | 
|---|
| 151 | std::stringstream output; | 
|---|
| 152 | output << ""; | 
|---|
| 153 | treeItem->setText(ENERGYCONTRIBUTION, QString(output.str().c_str())); | 
|---|
| 154 | } | 
|---|
| 155 | } else { | 
|---|
| 156 | FragmentationShortRangeResults::summedshortrange_t::const_iterator sumresiter = | 
|---|
| 157 | summedshortrange.find(*keyiter); | 
|---|
| 158 | ASSERT( sumresiter != summedshortrange.end(), | 
|---|
| 159 | "QtFragmentList::refill() - "+toString(*keyiter)+" not present in summed results."); | 
|---|
| 160 | { | 
|---|
| 161 | std::stringstream output; | 
|---|
| 162 | output << sumresiter->second.first.energies.total; | 
|---|
| 163 | treeItem->setText(ENERGYVALUE, QString(output.str().c_str())); | 
|---|
| 164 | } | 
|---|
| 165 | { | 
|---|
| 166 | std::stringstream output; | 
|---|
| 167 | output << sumresiter->second.second.energies.total; | 
|---|
| 168 | treeItem->setText(ENERGYCONTRIBUTION, QString(output.str().c_str())); | 
|---|
| 169 | } | 
|---|
| 170 | } | 
|---|
| 171 | FragmentSelection.push_back(treeItem->isSelected()); | 
|---|
| 172 | indexsets.push_back( **keyiter ); | 
|---|
| 173 | } | 
|---|
| 174 | dirty = false; | 
|---|
| 175 | clearing = false; | 
|---|
| 176 | } | 
|---|
| 177 |  | 
|---|
| 178 | void QtFragmentList::paintEvent(QPaintEvent * event) | 
|---|
| 179 | { | 
|---|
| 180 | if (dirty) | 
|---|
| 181 | refill(); | 
|---|
| 182 | QTreeWidget::paintEvent(event); | 
|---|
| 183 | } | 
|---|
| 184 |  | 
|---|
| 185 | void QtFragmentList::subjectKilled(Observable *publisher) | 
|---|
| 186 | { | 
|---|
| 187 | // as a new instance should always already be present ... just sign on | 
|---|
| 188 | FragmentationResultContainer &fragments = | 
|---|
| 189 | FragmentationResultContainer::getInstance(); | 
|---|
| 190 | fragments.signOn(this); | 
|---|
| 191 | } | 
|---|
| 192 |  | 
|---|
| 193 |  | 
|---|
| 194 | void QtFragmentList::rowSelected() | 
|---|
| 195 | { | 
|---|
| 196 | //std::cout << "rowSelected\n"; | 
|---|
| 197 | // unselect | 
|---|
| 198 | for (int i=0;i<topLevelItemCount();++i) { | 
|---|
| 199 | QTreeWidgetItem *item = topLevelItem(i); | 
|---|
| 200 | bool newSelection = item->isSelected(); | 
|---|
| 201 | if (newSelection != FragmentSelection[i]){ | 
|---|
| 202 | // get the keyset and select the specific atoms | 
|---|
| 203 | if (!newSelection) | 
|---|
| 204 | for (IndexSet::const_iterator iter = indexsets[i].begin(); | 
|---|
| 205 | iter != indexsets[i].end(); | 
|---|
| 206 | ++iter) | 
|---|
| 207 | World::getInstance().unselectAtom(*iter); | 
|---|
| 208 | } | 
|---|
| 209 | } | 
|---|
| 210 | // select all | 
|---|
| 211 | for (int i=0;i<topLevelItemCount();++i) { | 
|---|
| 212 | QTreeWidgetItem *item = topLevelItem(i); | 
|---|
| 213 | bool newSelection = item->isSelected(); | 
|---|
| 214 | if (newSelection != FragmentSelection[i]){ | 
|---|
| 215 | FragmentSelection[i] = newSelection; | 
|---|
| 216 | // get the keyset and select the specific atoms | 
|---|
| 217 | if (newSelection) | 
|---|
| 218 | for (IndexSet::const_iterator iter = indexsets[i].begin(); | 
|---|
| 219 | iter != indexsets[i].end(); | 
|---|
| 220 | ++iter) | 
|---|
| 221 | World::getInstance().selectAtom(*iter); | 
|---|
| 222 | } | 
|---|
| 223 | } | 
|---|
| 224 | } | 
|---|
| 225 |  | 
|---|