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