source: src/UIElements/Views/Qt4/MoleculeList/QtMoleculeListView.cpp@ 015f8c

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

QtMainWindow uses QtSelectionChangedAgent.

  • QtMoleculeListView and GLWorldView receive its signals. The latter passes it on to GLWorldScene.
  • QtMoleculeList has new MoleculeIdToIndex to allow QtMoleculeListView to (un)select specific rows on a given index.
  • GLWorldScene passes atom and molecule selection to GLMoleculeObject_molecule.
  • GLMoleculeObject_molecule passes atom selection to respective GLMoleculeObject_atom.
  • Property mode set to 100644
File size: 8.4 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#include "UIElements/Views/Qt4/QtSelectionChangedAgent.hpp"
40
41#include "CodePatterns/MemDebug.hpp"
42
43#include "CodePatterns/Observer/Notification.hpp"
44
45#include "Actions/SelectionAction/Molecules/MoleculeByIdAction.hpp"
46#include "Actions/SelectionAction/Molecules/NotMoleculeByIdAction.hpp"
47
48#include "World.hpp"
49
50QtMoleculeListView::QtMoleculeListView(QWidget * _parent) :
51 QTreeView(_parent),
52 Observer("QtMoleculeListView"),
53 selecting(false)
54{
55 setSelectionMode(QAbstractItemView::MultiSelection);
56}
57
58QtMoleculeListView::~QtMoleculeListView()
59{}
60
61void QtMoleculeListView::setModel(QtMoleculeList *_moleculelist)
62{
63 QTreeView::setModel(_moleculelist);
64 // clicking a molecule means calling SelectionAction
65 connect(
66 selectionModel(), SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
67 this, SLOT(rowsSelected(const QItemSelection &, const QItemSelection &)), Qt::DirectConnection);
68}
69
70
71void QtMoleculeListView::setSelectionChangedAgent(QtSelectionChangedAgent *agent)
72{
73 connect(agent, SIGNAL(moleculeSelected(const moleculeId_t)),
74 this, SLOT(MoleculeSelected(const moleculeId_t)));
75 connect(agent, SIGNAL(moleculeUnselected(const moleculeId_t)),
76 this, SLOT(MoleculeUnselected(const moleculeId_t)));
77}
78
79void QtMoleculeListView::update(Observable *publisher)
80{}
81
82QModelIndex QtMoleculeListView::setIndexToLastColumn(const QModelIndex &_index) const
83{
84 QModelIndex return_index;
85 QModelIndex parent_index = _index.parent();
86 ASSERT (parent_index.isValid(),
87 "QtMoleculeListView::setIndexToLastColumn() - _index has no valid parent.");
88 return_index = parent_index.child(_index.row(), QtMoleculeItem::OCCURRENCE);
89// return_index =
90// model()->invisibleRootItem()->child(
91// _index.row(),
92// QtMoleculeItem::OCCURRENCE)->index();
93 return return_index;
94}
95
96void QtMoleculeListView::rowsSelected(
97 const QItemSelection& selected, const QItemSelection& deselected)
98{
99 if (selecting)
100 return;
101
102 selecting = true;
103
104 // Select all molecules which belong to newly selected rows.
105 QtMoleculeList *moleculelist = dynamic_cast<QtMoleculeList *>(model());
106 QModelIndex index;
107 {
108 QModelIndexList items = selected.indexes();
109 molids_t ids;
110 ids.reserve(items.size());
111 foreach (index, items)
112 if ((index.column() == 0) && (selectionModel()->isSelected(index))) {
113 const moleculeId_t mol_id = moleculelist->IndexToMoleculeId(index);
114 const molecule * const mol = const_cast<const World &>(World::getInstance()).
115 getMolecule(MoleculeById(mol_id));
116 // check for invalid molecule
117 if (mol_id < 0)
118 continue;
119 // means we are looking at deselection because of removal (in World)
120 if (mol == NULL)
121 continue;
122 if (!World::getInstance().isSelected(mol))
123 ids.push_back(mol_id);
124 //std::cout << "select molecule" << std::endl;
125 }
126 if (!ids.empty())
127 MoleCuilder::SelectionMoleculeById(ids);
128 }
129
130 // Unselect all molecules which belong to newly unselected rows.
131 {
132 QModelIndexList items = deselected.indexes();
133 molids_t ids;
134 ids.reserve(items.size());
135 foreach (index, items)
136 if ((index.column() == 0) && (!selectionModel()->isSelected(index))) {
137 const moleculeId_t mol_id = moleculelist->IndexToMoleculeId(index);
138 const molecule * const mol = const_cast<const World &>(World::getInstance()).
139 getMolecule(MoleculeById(mol_id));
140 // check for invalid molecule
141 if (mol_id < 0)
142 continue;
143 // means we are looking at deselection because of removal (in World)
144 if (mol == NULL)
145 continue;
146 if (World::getInstance().isSelected(mol))
147 ids.push_back(mol_id);
148 //std::cout << "unselect molecule" << std::endl;
149 }
150 if (!ids.empty())
151 MoleCuilder::SelectionNotMoleculeById(ids);
152 }
153
154 selecting = false;
155}
156
157void QtMoleculeListView::MoleculeSelected(const moleculeId_t _id)
158{
159 if (selecting)
160 return;
161
162 selecting = true;
163
164 const QtMoleculeList *moleculelist = dynamic_cast<const QtMoleculeList *>(model());
165 QModelIndex index = moleculelist->MoleculeIdToIndex(_id);
166 ASSERT( !selectionModel()->isSelected(index),
167 "QtMoleculeListView::MoleculeSelected() - row to molecule "
168 +toString(_id)+" is already selected.");
169
170 // select the full row
171 expand(index);
172 selectionModel()->select(index, QItemSelectionModel::Select | QItemSelectionModel::Rows);
173
174 selecting = false;
175}
176
177void QtMoleculeListView::MoleculeUnselected(const moleculeId_t _id)
178{
179 if (selecting)
180 return;
181
182 selecting = true;
183
184 const QtMoleculeList *moleculelist = dynamic_cast<const QtMoleculeList *>(model());
185 QModelIndex index = moleculelist->MoleculeIdToIndex(_id);
186 ASSERT( selectionModel()->isSelected(index),
187 "QtMoleculeListView::MoleculeSelected() - row to molecule "
188 +toString(_id)+" is already unselected.");
189
190 // unselect the full row
191 expand(index);
192 selectionModel()->select(index, QItemSelectionModel::Deselect | QItemSelectionModel::Rows);
193
194 selecting = false;
195}
196
197
198void QtMoleculeListView::recieveNotification(Observable *publisher, Notification_ptr notification)
199{
200 if (dynamic_cast<World *>(publisher) != NULL) {
201 switch (notification->getChannelNo()) {
202 case World::SelectionChanged:
203 {
204 // obtain molecule selection from World and go through our selection step by step
205 const std::vector<const molecule *> selectedMolecules =
206 const_cast<const World &>(World::getInstance()).getSelectedMolecules();
207 QItemSelection currently_selected = selectionModel()->selection();
208 QtMoleculeList *moleculelist = static_cast<QtMoleculeList *>(model());
209 QItemSelection selected;
210 QItemSelection deselected;
211 std::set<QModelIndex> already_selected_indices;
212 for (std::vector<const molecule *>::const_iterator iter = selectedMolecules.begin();
213 iter != selectedMolecules.end(); ++iter) {
214 if (moleculelist->isMoleculeItemPresent((*iter)->getId())) {
215 QtMoleculeItem *item = moleculelist->MoleculeIdToItem((*iter)->getId());
216 QModelIndex mol_index = item->index();
217 if (!currently_selected.contains(mol_index))
218 selected.select(mol_index, setIndexToLastColumn(mol_index));
219 else
220 already_selected_indices.insert(mol_index);
221 }
222 }
223 {
224 QModelIndex mol_index;
225 foreach(mol_index, currently_selected.indexes()) {
226 std::set<QModelIndex>::const_iterator iter =
227 already_selected_indices.find(mol_index);
228 if (iter == already_selected_indices.end())
229 deselected.select(mol_index, setIndexToLastColumn(mol_index));
230 }
231 }
232 selecting = true;
233 if (!selected.indexes().empty())
234 selectionModel()->select(selected, QItemSelectionModel::Select);
235 if (!deselected.indexes().empty())
236 selectionModel()->select(deselected, QItemSelectionModel::Deselect);
237 selecting = false;
238 break;
239 }
240 default:
241 ASSERT(0, "QtMoleculeListView::recieveNotification() - cannot get here, not subscribed to channel "
242 +toString(notification->getChannelNo()));
243 break;
244 }
245 }
246}
247
248void QtMoleculeListView::subjectKilled(Observable *publisher)
249{}
Note: See TracBrowser for help on using the repository browser.