source: src/UIElements/Views/Qt4/QtFragmentList.cpp@ 85056e

Gui_Fixes
Last change on this file since 85056e was 85056e, checked in by Frederik Heber <heber@…>, 9 years ago

FragmentList and HomologyList can be sorted by clicking column title.

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