| 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 | #include "Views/Qt4/QtNumericalItem.hpp" | 
|---|
| 37 |  | 
|---|
| 38 | #include <QAbstractItemView> | 
|---|
| 39 |  | 
|---|
| 40 | #include <iterator> | 
|---|
| 41 | #include <iostream> | 
|---|
| 42 |  | 
|---|
| 43 | #include <boost/lexical_cast.hpp> | 
|---|
| 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 "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 |  | 
|---|
| 64 | using namespace boost::assign; | 
|---|
| 65 |  | 
|---|
| 66 | const int QtFragmentList::COLUMNCOUNT = COLUMNTYPES_MAX; | 
|---|
| 67 | const char *QtFragmentList::COLUMNNAMES[QtFragmentList::COLUMNCOUNT]={"Number","KeySet","Formula","Energy value","Energy contribution"}; | 
|---|
| 68 |  | 
|---|
| 69 | QtFragmentList::QtFragmentList(QWidget * _parent) : | 
|---|
| 70 | QTreeWidget (_parent), | 
|---|
| 71 | Observer("QtFragmentList"), | 
|---|
| 72 | dirty(true), | 
|---|
| 73 | fragmentresultcontainer_enabled(false) | 
|---|
| 74 | { | 
|---|
| 75 | setColumnCount(COLUMNCOUNT); | 
|---|
| 76 | setSortingEnabled(true); | 
|---|
| 77 | QStringList header; | 
|---|
| 78 | for(int i=0; i<COLUMNCOUNT;++i) | 
|---|
| 79 | header << COLUMNNAMES[i]; | 
|---|
| 80 | setHeaderLabels(header); | 
|---|
| 81 | sortByColumn(0, Qt::AscendingOrder); | 
|---|
| 82 |  | 
|---|
| 83 | setSelectionMode( MultiSelection ); | 
|---|
| 84 |  | 
|---|
| 85 | refill(); | 
|---|
| 86 |  | 
|---|
| 87 | FragmentationResultContainer &fragments = | 
|---|
| 88 | FragmentationResultContainer::getInstance(); | 
|---|
| 89 | fragments.signOn(this); | 
|---|
| 90 | fragmentresultcontainer_enabled = true; | 
|---|
| 91 |  | 
|---|
| 92 | connect(this,SIGNAL(itemSelectionChanged()),this,SLOT(rowSelected())); | 
|---|
| 93 | connect(this,SIGNAL(changed()),this,SLOT(update())); | 
|---|
| 94 | } | 
|---|
| 95 |  | 
|---|
| 96 | QtFragmentList::~QtFragmentList() | 
|---|
| 97 | { | 
|---|
| 98 | if (fragmentresultcontainer_enabled) { | 
|---|
| 99 | FragmentationResultContainer &fragments = | 
|---|
| 100 | FragmentationResultContainer::getInstance(); | 
|---|
| 101 | fragments.signOff(this); | 
|---|
| 102 | } | 
|---|
| 103 | } | 
|---|
| 104 |  | 
|---|
| 105 | void QtFragmentList::update(Observable *publisher) { | 
|---|
| 106 | boost::unique_lock<boost::mutex> lock(flag_mutex); | 
|---|
| 107 | dirty = true; | 
|---|
| 108 | } | 
|---|
| 109 |  | 
|---|
| 110 | void QtFragmentList::refill() | 
|---|
| 111 | { | 
|---|
| 112 | boost::unique_lock<boost::mutex> lock(flag_mutex); | 
|---|
| 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(); | 
|---|
| 124 | const FragmentationShortRangeResults::summedshortrange_t &summedshortrange = fragments.getShortRangeSummedResults(); | 
|---|
| 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 |  | 
|---|
| 130 | std::vector<int> numerical_columns; | 
|---|
| 131 | numerical_columns += ENERGYVALUE, ENERGYCONTRIBUTION; | 
|---|
| 132 | QTreeWidgetItem *treeItem = new QtNumericalItem(NUMBER, numerical_columns, this); | 
|---|
| 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) { | 
|---|
| 145 | const atom * const _atom = const_cast<const World &>(World::getInstance()). | 
|---|
| 146 | getAtom(AtomById(*indexiter)); | 
|---|
| 147 | if (_atom != NULL) | 
|---|
| 148 | formula.addElements(_atom->getElementNo(), 1); | 
|---|
| 149 | else | 
|---|
| 150 | ELOG(2, "QtFragmentList::refill() - Could not find id " << *indexiter << " for formula."); | 
|---|
| 151 | } | 
|---|
| 152 | output << formula.toString(); | 
|---|
| 153 | treeItem->setText(FORMULA, QString(output.str().c_str())); | 
|---|
| 154 | } | 
|---|
| 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 | } | 
|---|
| 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 | 
|---|
| 198 | if (static_cast<FragmentationResultContainer *>(publisher) == FragmentationResultContainer::getPointer()) { | 
|---|
| 199 | fragmentresultcontainer_enabled = false; | 
|---|
| 200 | } | 
|---|
| 201 | } | 
|---|
| 202 |  | 
|---|
| 203 | void QtFragmentList::rowSelected() | 
|---|
| 204 | { | 
|---|
| 205 | //std::cout << "rowSelected\n"; | 
|---|
| 206 | // unselect | 
|---|
| 207 | for (int i=0;i<topLevelItemCount();++i) { | 
|---|
| 208 | QTreeWidgetItem *item = topLevelItem(i); | 
|---|
| 209 | bool newSelection = item->isSelected(); | 
|---|
| 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); | 
|---|
| 213 | // get the keyset and select the specific atoms | 
|---|
| 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(); | 
|---|
| 218 | ++iter) | 
|---|
| 219 | World::getInstance().unselectAtom(*iter); | 
|---|
| 220 | } | 
|---|
| 221 | } | 
|---|
| 222 | } | 
|---|
| 223 | // select all | 
|---|
| 224 | for (int i=0;i<topLevelItemCount();++i) { | 
|---|
| 225 | QTreeWidgetItem *item = topLevelItem(i); | 
|---|
| 226 | bool newSelection = item->isSelected(); | 
|---|
| 227 | const int itemno = boost::lexical_cast<int>(item->text(NUMBER).toStdString()); | 
|---|
| 228 | if (newSelection != FragmentSelection[itemno]){ | 
|---|
| 229 | FragmentSelection[itemno] = newSelection; | 
|---|
| 230 | // get the keyset and select the specific atoms | 
|---|
| 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(); | 
|---|
| 235 | ++iter) | 
|---|
| 236 | World::getInstance().selectAtom(*iter); | 
|---|
| 237 | } | 
|---|
| 238 | } | 
|---|
| 239 | } | 
|---|
| 240 | } | 
|---|
| 241 |  | 
|---|