source: src/UIElements/Views/Qt4/MoleculeList/QtMoleculeListView.cpp@ 3054f4a

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 3054f4a was 3054f4a, checked in by Frederik Heber <heber@…>, 9 years ago

QtObserved...Observed now get index on ...Removed() signal.

  • Property mode set to 100644
File size: 6.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 <QtCore/QMetaType>
38
39#include "UIElements/Views/Qt4/MoleculeList/QtMoleculeList.hpp"
40#include "UIElements/Views/Qt4/MoleculeList/QtMoleculeItem.hpp"
41
42#include "CodePatterns/MemDebug.hpp"
43
44#include "Actions/SelectionAction/Molecules/MoleculeByIdAction.hpp"
45#include "Actions/SelectionAction/Molecules/NotMoleculeByIdAction.hpp"
46#include "molecule.hpp"
47#include "World.hpp"
48
49QtMoleculeListView::QtMoleculeListView(QWidget * _parent) :
50 QTreeView(_parent),
51 selecting(false)
52{
53 setSelectionMode(QAbstractItemView::MultiSelection);
54
55 qRegisterMetaType<QItemSelection>("QItemSelection");
56}
57
58QtMoleculeListView::~QtMoleculeListView()
59{
60}
61
62void QtMoleculeListView::setModel(QtMoleculeList *_moleculelist)
63{
64 QTreeView::setModel(_moleculelist);
65 // clicking a molecule means calling SelectionAction
66 connect(
67 selectionModel(), SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
68 this, SLOT(rowsSelected(const QItemSelection &, const QItemSelection &)), Qt::DirectConnection);
69
70 connect(&_moleculelist->observer, SIGNAL(SelectionChanged(QtObservedMolecule::ptr)),
71 this, SLOT(selectionChanged(QtObservedMolecule::ptr)));
72}
73
74QModelIndex QtMoleculeListView::setIndexToLastColumn(const QModelIndex &_index) const
75{
76 QModelIndex return_index;
77 QModelIndex parent_index = _index.parent();
78 ASSERT (parent_index.isValid(),
79 "QtMoleculeListView::setIndexToLastColumn() - _index has no valid parent.");
80 return_index = parent_index.child(_index.row(), QtMoleculeItem::OCCURRENCE);
81// return_index =
82// model()->invisibleRootItem()->child(
83// _index.row(),
84// QtMoleculeItem::OCCURRENCE)->index();
85 return return_index;
86}
87
88void QtMoleculeListView::rowsSelected(
89 const QItemSelection& selected, const QItemSelection& deselected)
90{
91 if (selecting)
92 return;
93
94 selecting = true;
95
96 // Select all molecules which belong to newly selected rows.
97 QtMoleculeList *moleculelist = dynamic_cast<QtMoleculeList *>(model());
98 QModelIndex index;
99 {
100 QModelIndexList items = selected.indexes();
101 molids_t ids;
102 ids.reserve(items.size());
103 foreach (index, items)
104 if ((index.column() == 0) && (selectionModel()->isSelected(index))) {
105 const moleculeId_t mol_id = moleculelist->observer.getIdtoIndex(
106 moleculelist->IndexToMoleculeId(index));
107 const molecule * const mol = const_cast<const World &>(World::getInstance()).
108 getMolecule(MoleculeById(mol_id));
109 // check for invalid molecule
110 if (mol_id < 0)
111 continue;
112 // means we are looking at deselection because of removal (in World)
113 if (mol == NULL)
114 continue;
115 if (!World::getInstance().isSelected(mol))
116 ids.push_back(mol_id);
117 //std::cout << "select molecule" << std::endl;
118 }
119 if (!ids.empty())
120 MoleCuilder::SelectionMoleculeById(ids);
121 }
122
123 // Unselect all molecules which belong to newly unselected rows.
124 {
125 QModelIndexList items = deselected.indexes();
126 molids_t ids;
127 ids.reserve(items.size());
128 foreach (index, items)
129 if ((index.column() == 0) && (!selectionModel()->isSelected(index))) {
130 const moleculeId_t mol_id = moleculelist->observer.getIdtoIndex(
131 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::selectionChanged(const QtObservedMolecule::ptr _molecule)
152{
153 if (_molecule->getMolSelected())
154 MoleculeSelected(_molecule->getIndex());
155 else
156 MoleculeUnselected(_molecule->getIndex());
157}
158
159void QtMoleculeListView::MoleculeSelected(ObservedValue_Index_t _id)
160{
161 if (selecting)
162 return;
163
164 selecting = true;
165
166 const QtMoleculeList *moleculelist = dynamic_cast<const QtMoleculeList *>(model());
167 if (moleculelist->isMoleculeItemPresent(_id)) {
168 QModelIndex index = moleculelist->MoleculeIdToIndex(_id);
169// ASSERT( !selectionModel()->isSelected(index),
170// "QtMoleculeListView::MoleculeSelected() - row to molecule "
171// +toString(mol->getMolIndex())+" is already selected.");
172
173 // select the full row
174 expand(index);
175 selectionModel()->select(index, QItemSelectionModel::Select | QItemSelectionModel::Rows);
176 }
177
178 selecting = false;
179}
180
181void QtMoleculeListView::MoleculeUnselected(ObservedValue_Index_t _id)
182{
183 if (selecting)
184 return;
185
186 selecting = true;
187
188 const QtMoleculeList *moleculelist = dynamic_cast<const QtMoleculeList *>(model());
189 if (moleculelist->isMoleculeItemPresent(_id)) {
190 QModelIndex index = moleculelist->MoleculeIdToIndex(_id);
191// ASSERT( selectionModel()->isSelected(index),
192// "QtMoleculeListView::MoleculeSelected() - row to molecule "
193// +toString(mol->getMolIndex())+" is already unselected.");
194
195 // unselect the full row
196 expand(index);
197 selectionModel()->select(index, QItemSelectionModel::Deselect | QItemSelectionModel::Rows);
198 }
199
200 selecting = false;
201}
Note: See TracBrowser for help on using the repository browser.