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