source: src/UIElements/Views/Qt4/MoleculeList/QtMoleculeList.cpp@ f62e60

Action_Thermostats Add_AtomRandomPerturbation Add_FitFragmentPartialChargesAction Add_RotateAroundBondAction Add_SelectAtomByNameAction Added_ParseSaveFragmentResults AddingActions_SaveParseParticleParameters Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_ParticleName_to_Atom Adding_StructOpt_integration_tests AtomFragments Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.5.4 Candidate_v1.6.0 Candidate_v1.6.1 Candidate_v1.7.0 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator CombiningParticlePotentialParsing Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_BoundInBox_CenterInBox_MoleculeActions Fix_ChargeSampling_PBC Fix_ChronosMutex Fix_FitPartialCharges Fix_FitPotential_needs_atomicnumbers Fix_ForceAnnealing Fix_IndependentFragmentGrids Fix_ParseParticles Fix_ParseParticles_split_forward_backward_Actions Fix_PopActions Fix_QtFragmentList_sorted_selection Fix_Restrictedkeyset_FragmentMolecule Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns Fix_fitting_potentials Fixes ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion FragmentAction_writes_AtomFragments FragmentMolecule_checks_bonddegrees GeometryObjects Gui_Fixes Gui_displays_atomic_force_velocity ImplicitCharges IndependentFragmentGrids IndependentFragmentGrids_IndividualZeroInstances IndependentFragmentGrids_IntegrationTest IndependentFragmentGrids_Sole_NN_Calculation JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix MoreRobust_FragmentAutomation ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PdbParser_setsAtomName PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks Rewrite_FitPartialCharges RotateToPrincipalAxisSystem_UndoRedo SaturateAtoms_findBestMatching SaturateAtoms_singleDegree StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_LinearAlgebra Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg Switchable_LogView ThirdParty_MPQC_rebuilt_buildsystem TrajectoryDependenant_MaxOrder TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps TremoloParser_setsAtomName Ubuntu_1604_changes stable
Last change on this file since f62e60 was f62e60, checked in by Frederik Heber <heber@…>, 11 years ago

Moved QtMoleculeList files into own subfolder.

  • added QtMoleculeListView.
  • we are going to need a lot of classes for this to work properly.
  • Property mode set to 100644
File size: 12.9 KB
RevLine 
[bcf653]1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
[0aa122]4 * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
[94d5ac6]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/>.
[bcf653]21 */
22
[b47bfc]23/*
[0eb7bf3]24 * QtMoleculeList.cpp
[b47bfc]25 *
26 * Created on: Jan 21, 2010
27 * Author: crueger
28 */
29
[bf3817]30// include config.h
31#ifdef HAVE_CONFIG_H
32#include <config.h>
33#endif
[bbbad5]34
[f62e60]35#include "QtMoleculeList.hpp"
[b47bfc]36
[41815a3]37#include <QMetaMethod>
38
[b47bfc]39#include <iostream>
40
[ad011c]41#include "CodePatterns/MemDebug.hpp"
[bbbad5]42
[6770fa]43#include "CodePatterns/Observer/Notification.hpp"
44
[6f0841]45#include "Atom/atom.hpp"
[26cf178]46#include "Formula.hpp"
[b47bfc]47#include "molecule.hpp"
[42127c]48#include "MoleculeListClass.hpp"
[b14efe]49#include "Actions/SelectionAction/Molecules/MoleculeByIdAction.hpp"
50#include "Actions/SelectionAction/Molecules/NotMoleculeByIdAction.hpp"
[b47bfc]51
52using namespace std;
53
54// maybe this should go with the definition of molecules
55
56// some attributes need to be easier to find for molecules
[6cab535]57// these attributes are skipped so far
[0eb7bf3]58const int QtMoleculeList::COLUMNCOUNT = COLUMNTYPES_MAX;
[739ee9]59const char *QtMoleculeList::COLUMNNAMES[QtMoleculeList::COLUMNCOUNT]={"Name","Visibility", "Atoms","Formula","Occurrence"/*,"Size"*/};
[b47bfc]60
[8ccf3b]61QtMoleculeList::QtMoleculeList() :
[0eb7bf3]62 Observer("QtMoleculeList")
[b47bfc]63{
64 setColumnCount(COLUMNCOUNT);
[8ccf3b]65// setSelectionMode(QAbstractItemView::MultiSelection);
[b47bfc]66
[3eb91c]67 World::getInstance().signOn(this, World::MoleculeInserted);
68 World::getInstance().signOn(this, World::MoleculeRemoved);
[41815a3]69
[a39006]70 dirty = true;
[99e8ea]71 clearing = false;
[41815a3]72 selecting = false;
[6770fa]73 changing = false;
[3eb91c]74 ChangingChildrensVisibility = false;
[a39006]75 refill();
[b47bfc]76
[8ccf3b]77// qRegisterMetaType<QItemSelection>("QItemSelection");
[79b59b]78 //connect(this,SIGNAL(cellChanged(int,int)),this,SLOT(moleculeChanged(int,int)));
[8ccf3b]79// connect(selectionModel(),SIGNAL(selectionChanged(QItemSelection, QItemSelection)),this,SLOT(rowsSelected(QItemSelection, QItemSelection)));
80// connect(this, SIGNAL(itemChanged(QStandardItem*, int)), this, SLOT(visibilityChanged(QStandardItem*, int)));
[b47bfc]81}
82
[0eb7bf3]83QtMoleculeList::~QtMoleculeList()
[b47bfc]84{
[3eb91c]85 World::getInstance().signOff(this, World::MoleculeInserted);
86 World::getInstance().signOff(this, World::MoleculeRemoved);
[b47bfc]87}
88
[1c3390]89QVariant QtMoleculeList::headerData(int section, Qt::Orientation orientation, int role) const
90{
91 if (role == Qt::DisplayRole) {
92 if (orientation == Qt::Horizontal) {
93 if (section < COLUMNTYPES_MAX)
94 return QString(COLUMNNAMES[section]);
95 }
96 }
97 return QVariant();
98}
99
[0eb7bf3]100void QtMoleculeList::update(Observable *publisher) {
[3eb91c]101 ASSERT(0,
102 "QtMoleculeList::update() - we did not sign up for any global updates.");
103}
104
[8ccf3b]105QStandardItem * QtMoleculeList::MoleculeToItem(const molecule * const _mol)
106{
107 MoleculeItemBiMap_t::left_const_iterator iter =
108 MoleculeItemBiMap.left.find(_mol);
109 ASSERT( iter != MoleculeItemBiMap.left.end(),
110 "QtMoleculeList - could not find molecule "+_mol->getName()+" in my list.");
111 return iter->second;
112}
113
[6770fa]114void QtMoleculeList::recieveNotification(Observable *publisher, Notification_ptr notification)
115{
116 if (dynamic_cast<World *>(publisher) != NULL) {
117 switch (notification->getChannelNo()) {
118 case World::MoleculeInserted:
119 {
120 const molecule * const mol = World::getInstance().lastChanged<molecule>();
[8ccf3b]121 addItem( mol );
[6770fa]122 break;
123 }
124 case World::MoleculeRemoved:
125 {
126 const molecule * const mol = World::getInstance().lastChanged<molecule>();
[8ccf3b]127 removeItem( mol );
[6770fa]128 break;
129 }
130 default:
131 ASSERT(0, "QtMoleculeList::recieveNotification() - cannot get here, not subscribed to channel "
132 +toString(notification->getChannelNo()));
133 break;
134 }
135 }
[a39006]136
[41815a3]137 if (selecting)
138 return;
139
[a39006]140 dirty = true;
141}
142
[6770fa]143void QtMoleculeList::addGroupItem(
[8ccf3b]144 QStandardItem *&mainitem,
[6770fa]145 const std::string &_molecule_formula)
146{
[8ccf3b]147 QList<QStandardItem *> groupItems;
148 // fill item list
149 mainitem = new QStandardItem(QString("default"));
150 mainitem->setFlags(mainitem->flags() ^ Qt::ItemIsSelectable);
151 formula.insert( std::make_pair(_molecule_formula, mainitem) );
152 groupItems << mainitem;
153 QStandardItem *visitem = new QStandardItem;
154 visitem->setCheckState(Qt::Unchecked);
155 visitem->setFlags(mainitem->flags() | Qt::ItemIsUserCheckable);
156 groupItems << visitem;
157 groupItems << new QStandardItem(QString::number(0));
158 groupItems << new QStandardItem(QString(""));
159 groupItems << new QStandardItem(QString::number(0));
160 invisibleRootItem()->appendRow(groupItems);
161// _groupItem->setData(0, Qt::UserRole, QVariant(-1));
[6770fa]162}
163
164void QtMoleculeList::addMoleculeItem(
[8ccf3b]165 QStandardItem *_groupitem,
[6770fa]166 const molecule *_mol,
167 const std::string &_molecule_formula)
168{
[8ccf3b]169 QList<QStandardItem *> molItems;
170 QStandardItem *mainitem = new QStandardItem(QString(_mol->getName().c_str()));
171 mainitem->setFlags(mainitem->flags() | Qt::ItemIsSelectable);
172// mainitem->setSelected(World::getInstance().isSelected(_mol));
173 MoleculeItemBiMap.left.insert( std::make_pair(_mol, mainitem) );
174 molItems << mainitem;
175 QStandardItem *visitem = new QStandardItem();
176 visitem->setCheckState(Qt::Unchecked);
177 visitem->setFlags(mainitem->flags() | Qt::ItemIsUserCheckable);
178 molItems << visitem;
179 molItems << new QStandardItem(QString::number(_mol->getAtomCount()));
180 molItems << new QStandardItem(QString(_molecule_formula.c_str()));
181 molItems << new QStandardItem(QString::number(0));
182// const int index = _mol->getId();
183// molItem->setData(0, Qt::UserRole, QVariant(index));
184 _groupitem->appendRow(molItems);
[6770fa]185}
186
187void QtMoleculeList::addItem(const molecule *_mol)
188{
189 if (changing)
190 return;
191 changing = true;
192 // find group if already in list
[8ccf3b]193 QStandardItem *groupItem = NULL;
[6770fa]194
195 const std::string &molecule_formula = _mol->getFormula().toString();
196 FormulaTreeItemMap_t::const_iterator formulaiter =
197 formula.find(molecule_formula);
198
199 // new molecule type -> create new group
200 if (formulaiter == formula.end()){
201 // insert new formula entry into visibility
202#ifndef NDEBUG
203 std::pair< FormulaVisibilityCountMap_t::iterator, bool> visibilityinserter =
204#endif
205 FormulaVisibilityCountMap.insert(
206 std::make_pair( molecule_formula, (unsigned int)0) );
207 ASSERT( visibilityinserter.second,
208 "QtMoleculeList::refill() - molecule with formula "
209 +molecule_formula+" already in FormulaVisibilityCountMap.");
210
211 // create item and place into Map with formula as key
212 addGroupItem(groupItem, molecule_formula);
213 } else {
214 groupItem = formulaiter->second;
215 }
216 ASSERT( groupItem != NULL,
217 "QtMoleculeList::addItem() - item with id "+toString(_mol->getId())
218 +" has not parent?");
219
220 // add molecule
221 addMoleculeItem(groupItem, _mol, molecule_formula);
222
223 // increase group occurrence
[8ccf3b]224 const int index = groupItem->index().row();
225 QStandardItem *occ_item = invisibleRootItem()->child(index, OCCURRENCE);
226 int count = occ_item->text().toInt() + 1;
227 occ_item->setText(QString::number(count));
[6770fa]228
229 changing = false;
230}
231
232void QtMoleculeList::removeItem(const molecule *_mol)
233{
234 if (changing)
235 return;
236 changing = true;
237
238 dirty = true;
239 // TODO: We have to get some Model into the Table in order to "find" items right
240 // away
241// QTreeWidgetItem *molItem = itemFromIndex(mol->getId());
242// delete molItem1;
243
244 changing = false;
245}
[3eb91c]246
[0eb7bf3]247void QtMoleculeList::refill() {
[6cab535]248 clearing = true;
[41815a3]249 const std::vector<molecule*> &molecules = World::getInstance().getAllMolecules();
250
[79b59b]251 clear();
[3eb91c]252 formula.clear();
253 FormulaVisibilityCountMap.clear();
[8ccf3b]254 MoleculeItemBiMap.clear();
[79b59b]255
[41815a3]256 for (std::vector<molecule*>::const_iterator iter = molecules.begin();
257 iter != molecules.end();
258 iter++) {
[b47bfc]259
[6770fa]260 addItem(*iter);
[b47bfc]261 }
[a39006]262 dirty = false;
[6cab535]263 clearing = false;
[a39006]264}
265
[8ccf3b]266/*
[0eb7bf3]267void QtMoleculeList::paintEvent(QPaintEvent * event)
[a39006]268{
269 if (dirty)
270 refill();
271 QTreeWidget::paintEvent(event);
[b47bfc]272}
[8ccf3b]273*/
[b47bfc]274
[0eb7bf3]275void QtMoleculeList::subjectKilled(Observable *publisher) {
[b47bfc]276}
277
[8ccf3b]278/*
279void QtMoleculeList::visibilityChanged(QStandardItem* item, int column)
[739ee9]280{
[6770fa]281 if ((!changing) && (!clearing) && (!ChangingChildrensVisibility))
[3eb91c]282 if (column == VISIBILITY) {
283 const moleculeId_t molid = item->data(0, Qt::UserRole).toInt();
284 const bool visible = item->checkState(VISIBILITY);
285 if (molid != (unsigned int)-1) { // molecule item
[6770fa]286 World::MoleculeConstIterator moliter =
287 const_cast<const World &>(World::getInstance()).getMoleculeIter(MoleculeById(molid));
288 const molecule * const _molecule = *moliter;
[3eb91c]289 ASSERT( _molecule != NULL,
290 "QtMoleculeList::visibilityChanged() - molecule with id "
291 +toString(molid)+" is not known to World.");
292 const std::string &molecule_formula = _molecule->getFormula().toString();
293 ASSERT( FormulaVisibilityCountMap.count(molecule_formula) != 0,
294 "QtMoleculeList::visibilityChanged() - molecule with formula " +molecule_formula
295 +" is not present in FormulaVisibilityCountMap.");
296
297 // get parent
298 QTreeWidgetItem *groupItem = item->parent();
299 ASSERT( groupItem != NULL,
300 "QtMoleculeList::visibilityChanged() - item with id "+toString(molid)
301 +" has not parent?");
302 // check whether we have to set the group item
303
304 ChangingChildrensVisibility = true;
305 if (visible) {
306 ++(FormulaVisibilityCountMap[molecule_formula]);
307 // compare with occurence/total number of molecules
308 if (FormulaVisibilityCountMap[molecule_formula] ==
309 (unsigned int)(groupItem->text(OCCURRENCE).toInt()))
310 groupItem->setCheckState(VISIBILITY, Qt::Checked);
311 } else {
312 --(FormulaVisibilityCountMap[molecule_formula]);
313 // none selected anymore?
314 if (FormulaVisibilityCountMap[molecule_formula] == 0)
315 groupItem->setCheckState(VISIBILITY, Qt::Unchecked);
316 }
317 ChangingChildrensVisibility = false;
318
319 emit moleculesVisibilityChanged(molid, visible);
320
321 } else { // group item
322
323 // go through all children, but don't enter for groupItem once more
324 ChangingChildrensVisibility = true;
325 for (int i=0;i<item->childCount();++i) {
326 QTreeWidgetItem *molItem = item->child(i);
327 const moleculeId_t molid = molItem->data(0, Qt::UserRole).toInt();
328 ASSERT( molid != (unsigned int)-1,
329 "QtMoleculeList::visibilityChanged() - to child with index"
330 +toString(i)+" there is no molecule?");
331 molItem->setCheckState(VISIBILITY, visible ? Qt::Checked : Qt::Unchecked);
332
333 // emit signal
334 emit moleculesVisibilityChanged(molid, visible);
335 }
336 // set current number of visible children
337 const std::string molecule_formula =
338 item->text(FORMULA).toStdString();
339 FormulaVisibilityCountMap[molecule_formula] =
340 visible ? item->text(OCCURRENCE).toInt() : 0;
341
342 ChangingChildrensVisibility = false;
343 }
344 }
[739ee9]345}
[8ccf3b]346*/
[739ee9]347
[0eb7bf3]348void QtMoleculeList::moleculeChanged() {
[79b59b]349 /*int idx = verticalHeaderItem(row)->data(Qt::UserRole).toInt();
[b47bfc]350 molecule *mol = molecules->ReturnIndex(idx);
351 string cellValue = item(row,NAME)->text().toStdString();
352 if(mol->getName() != cellValue && cellValue !="") {
353 mol->setName(cellValue);
354 }
355 else if(cellValue==""){
356 item(row,NAME)->setText(QString(mol->getName().c_str()));
[79b59b]357 }*/
[b47bfc]358}
359
[8ccf3b]360/*
[0eb7bf3]361void QtMoleculeList::rowsSelected(const QItemSelection & selected, const QItemSelection & deselected){
[b14efe]362
[99e8ea]363 if (clearing)
364 return;
[41815a3]365 if (selecting)
366 return;
367 selecting = true;
368
369 // Select all molecules which belong to newly selected rows.
370 QModelIndex index;
[d7cad1]371 {
372 QModelIndexList items = selected.indexes();
373 molids_t ids;
374 ids.reserve(items.size());
375 foreach (index, items)
376 if (index.column() == 0){
377 int mol_id = model()->data(index, Qt::UserRole).toInt();
378 if (mol_id < 0)
379 continue;
380 ids.push_back(mol_id);
381 //std::cout << "select molecule" << std::endl;
382 }
383 MoleCuilder::SelectionMoleculeById(ids);
384 }
[99e8ea]385
[41815a3]386 // Unselect all molecules which belong to newly unselected rows.
[d7cad1]387 {
388 QModelIndexList items = deselected.indexes();
389 molids_t ids;
390 ids.reserve(items.size());
391 foreach (index, items)
392 if (index.column() == 0){
393 int mol_id = model()->data(index, Qt::UserRole).toInt();
394 if (mol_id < 0)
395 continue;
396 //std::cout << "unselect molecule" << std::endl;
397 ids.push_back(mol_id);
398 }
399 MoleCuilder::SelectionNotMoleculeById(ids);
400 }
[41815a3]401
402 selecting = false;
[b47bfc]403}
[8ccf3b]404
405*/
Note: See TracBrowser for help on using the repository browser.