source: src/UIElements/Views/Qt4/MoleculeList/QtMoleculeListView.cpp@ 52c5d4

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 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 52c5d4 was fcdf05, checked in by Frederik Heber <heber@…>, 10 years ago

Fixing QtMoleculeList with list containing only formulas and ids.

  • Property mode set to 100644
File size: 7.0 KB
Line 
1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
4 * Copyright (C) 2015 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 * QtMoleculeListView.cpp
25 *
26 * Created on: Jan 17, 2015
27 * Author: heber
28 */
29
30// include config.h
31#ifdef HAVE_CONFIG_H
32#include <config.h>
33#endif
34
35#include "QtMoleculeListView.hpp"
36
37#include "UIElements/Views/Qt4/MoleculeList/QtMoleculeList.hpp"
38#include "UIElements/Views/Qt4/MoleculeList/QtMoleculeItem.hpp"
39
40#include "CodePatterns/MemDebug.hpp"
41
42#include "CodePatterns/Observer/Notification.hpp"
43
44#include "Actions/SelectionAction/Molecules/MoleculeByIdAction.hpp"
45#include "Actions/SelectionAction/Molecules/NotMoleculeByIdAction.hpp"
46
47#include "World.hpp"
48
49QtMoleculeListView::QtMoleculeListView(QWidget * _parent) :
50 QTreeView(_parent),
51 Observer("QtMoleculeListView"),
52 selecting(false)
53{
54 setSelectionMode(QAbstractItemView::MultiSelection);
55
56 World::getInstance().signOn(this, World::SelectionChanged);
57}
58
59QtMoleculeListView::~QtMoleculeListView()
60{
61 World::getInstance().signOff(this, World::SelectionChanged);
62}
63
64void QtMoleculeListView::setModel(QtMoleculeList *_moleculelist)
65{
66 QTreeView::setModel(_moleculelist);
67 // clicking a molecule means calling SelectionAction
68 connect(
69 selectionModel(), SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
70 this, SLOT(rowsSelected(const QItemSelection &, const QItemSelection &)), Qt::DirectConnection);
71}
72
73void QtMoleculeListView::update(Observable *publisher)
74{}
75
76QModelIndex QtMoleculeListView::setIndexToLastColumn(const QModelIndex &_index) const
77{
78 QModelIndex return_index;
79 QModelIndex parent_index = _index.parent();
80 ASSERT (parent_index.isValid(),
81 "QtMoleculeListView::setIndexToLastColumn() - _index has no valid parent.");
82 return_index = parent_index.child(_index.row(), QtMoleculeItem::OCCURRENCE);
83// return_index =
84// model()->invisibleRootItem()->child(
85// _index.row(),
86// QtMoleculeItem::OCCURRENCE)->index();
87 return return_index;
88}
89
90void QtMoleculeListView::rowsSelected(
91 const QItemSelection& selected, const QItemSelection& deselected)
92{
93 if (selecting)
94 return;
95
96 selecting = true;
97
98 // Select all molecules which belong to newly selected rows.
99 QtMoleculeList *moleculelist = dynamic_cast<QtMoleculeList *>(model());
100 QModelIndex index;
101 {
102 QModelIndexList items = selected.indexes();
103 molids_t ids;
104 ids.reserve(items.size());
105 foreach (index, items)
106 if ((index.column() == 0) && (selectionModel()->isSelected(index))) {
107 const moleculeId_t mol_id = moleculelist->IndexToMoleculeId(index);
108 const molecule * const mol = const_cast<const World &>(World::getInstance()).
109 getMolecule(MoleculeById(mol_id));
110 // check for invalid molecule
111 if (mol_id < 0)
112 continue;
113 // means we are looking at deselection because of removal (in World)
114 if (mol == NULL)
115 continue;
116 if (!World::getInstance().isSelected(mol))
117 ids.push_back(mol_id);
118 //std::cout << "select molecule" << std::endl;
119 }
120 if (!ids.empty())
121 MoleCuilder::SelectionMoleculeById(ids);
122 }
123
124 // Unselect all molecules which belong to newly unselected rows.
125 {
126 QModelIndexList items = deselected.indexes();
127 molids_t ids;
128 ids.reserve(items.size());
129 foreach (index, items)
130 if ((index.column() == 0) && (!selectionModel()->isSelected(index))) {
131 const moleculeId_t mol_id = moleculelist->IndexToMoleculeId(index);
132 const molecule * const mol = const_cast<const World &>(World::getInstance()).
133 getMolecule(MoleculeById(mol_id));
134 // check for invalid molecule
135 if (mol_id < 0)
136 continue;
137 // means we are looking at deselection because of removal (in World)
138 if (mol == NULL)
139 continue;
140 if (World::getInstance().isSelected(mol))
141 ids.push_back(mol_id);
142 //std::cout << "unselect molecule" << std::endl;
143 }
144 if (!ids.empty())
145 MoleCuilder::SelectionNotMoleculeById(ids);
146 }
147
148 selecting = false;
149}
150
151void QtMoleculeListView::recieveNotification(Observable *publisher, Notification_ptr notification)
152{
153 if (dynamic_cast<World *>(publisher) != NULL) {
154 switch (notification->getChannelNo()) {
155 case World::SelectionChanged:
156 {
157 // obtain molecule selection from World and go through our selection step by step
158 const std::vector<const molecule *> selectedMolecules =
159 const_cast<const World &>(World::getInstance()).getSelectedMolecules();
160 QItemSelection currently_selected = selectionModel()->selection();
161 QtMoleculeList *moleculelist = static_cast<QtMoleculeList *>(model());
162 QItemSelection selected;
163 QItemSelection deselected;
164 std::set<QModelIndex> already_selected_indices;
165 for (std::vector<const molecule *>::const_iterator iter = selectedMolecules.begin();
166 iter != selectedMolecules.end(); ++iter) {
167 if (moleculelist->isMoleculeItemPresent((*iter)->getId())) {
168 QtMoleculeItem *item = moleculelist->MoleculeIdToItem((*iter)->getId());
169 QModelIndex mol_index = item->index();
170 if (!currently_selected.contains(mol_index))
171 selected.select(mol_index, setIndexToLastColumn(mol_index));
172 else
173 already_selected_indices.insert(mol_index);
174 }
175 }
176 {
177 QModelIndex mol_index;
178 foreach(mol_index, currently_selected.indexes()) {
179 std::set<QModelIndex>::const_iterator iter =
180 already_selected_indices.find(mol_index);
181 if (iter == already_selected_indices.end())
182 deselected.select(mol_index, setIndexToLastColumn(mol_index));
183 }
184 }
185 selecting = true;
186 if (!selected.indexes().empty())
187 selectionModel()->select(selected, QItemSelectionModel::Select);
188 if (!deselected.indexes().empty())
189 selectionModel()->select(deselected, QItemSelectionModel::Deselect);
190 selecting = false;
191 break;
192 }
193 default:
194 ASSERT(0, "QtMoleculeListView::recieveNotification() - cannot get here, not subscribed to channel "
195 +toString(notification->getChannelNo()));
196 break;
197 }
198 }
199}
200
201void QtMoleculeListView::subjectKilled(Observable *publisher)
202{}
Note: See TracBrowser for help on using the repository browser.