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 |
|
---|
50 | QtMoleculeListView::QtMoleculeListView(QWidget * _parent) :
|
---|
51 | QTreeView(_parent),
|
---|
52 | Observer("QtMoleculeListView"),
|
---|
53 | selecting(false)
|
---|
54 | {
|
---|
55 | setSelectionMode(QAbstractItemView::MultiSelection);
|
---|
56 | }
|
---|
57 |
|
---|
58 | QtMoleculeListView::~QtMoleculeListView()
|
---|
59 | {}
|
---|
60 |
|
---|
61 | void 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 |
|
---|
71 | void 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 |
|
---|
79 | void QtMoleculeListView::update(Observable *publisher)
|
---|
80 | {}
|
---|
81 |
|
---|
82 | QModelIndex 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 |
|
---|
96 | void 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 |
|
---|
157 | void 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 | if (moleculelist->isMoleculeItemPresent(_id)) {
|
---|
166 | QModelIndex index = moleculelist->MoleculeIdToIndex(_id);
|
---|
167 | // ASSERT( !selectionModel()->isSelected(index),
|
---|
168 | // "QtMoleculeListView::MoleculeSelected() - row to molecule "
|
---|
169 | // +toString(_id)+" is already selected.");
|
---|
170 |
|
---|
171 | // select the full row
|
---|
172 | expand(index);
|
---|
173 | selectionModel()->select(index, QItemSelectionModel::Select | QItemSelectionModel::Rows);
|
---|
174 | }
|
---|
175 |
|
---|
176 | selecting = false;
|
---|
177 | }
|
---|
178 |
|
---|
179 | void QtMoleculeListView::MoleculeUnselected(const moleculeId_t _id)
|
---|
180 | {
|
---|
181 | if (selecting)
|
---|
182 | return;
|
---|
183 |
|
---|
184 | selecting = true;
|
---|
185 |
|
---|
186 | const QtMoleculeList *moleculelist = dynamic_cast<const QtMoleculeList *>(model());
|
---|
187 | if (moleculelist->isMoleculeItemPresent(_id)) {
|
---|
188 | QModelIndex index = moleculelist->MoleculeIdToIndex(_id);
|
---|
189 | // ASSERT( selectionModel()->isSelected(index),
|
---|
190 | // "QtMoleculeListView::MoleculeSelected() - row to molecule "
|
---|
191 | // +toString(_id)+" is already unselected.");
|
---|
192 |
|
---|
193 | // unselect the full row
|
---|
194 | expand(index);
|
---|
195 | selectionModel()->select(index, QItemSelectionModel::Deselect | QItemSelectionModel::Rows);
|
---|
196 | }
|
---|
197 |
|
---|
198 | selecting = false;
|
---|
199 | }
|
---|
200 |
|
---|
201 |
|
---|
202 | void QtMoleculeListView::recieveNotification(Observable *publisher, Notification_ptr notification)
|
---|
203 | {
|
---|
204 | if (dynamic_cast<World *>(publisher) != NULL) {
|
---|
205 | switch (notification->getChannelNo()) {
|
---|
206 | case World::SelectionChanged:
|
---|
207 | {
|
---|
208 | // obtain molecule selection from World and go through our selection step by step
|
---|
209 | const std::vector<const molecule *> selectedMolecules =
|
---|
210 | const_cast<const World &>(World::getInstance()).getSelectedMolecules();
|
---|
211 | QItemSelection currently_selected = selectionModel()->selection();
|
---|
212 | QtMoleculeList *moleculelist = static_cast<QtMoleculeList *>(model());
|
---|
213 | QItemSelection selected;
|
---|
214 | QItemSelection deselected;
|
---|
215 | std::set<QModelIndex> already_selected_indices;
|
---|
216 | for (std::vector<const molecule *>::const_iterator iter = selectedMolecules.begin();
|
---|
217 | iter != selectedMolecules.end(); ++iter) {
|
---|
218 | if (moleculelist->isMoleculeItemPresent((*iter)->getId())) {
|
---|
219 | QtMoleculeItem *item = moleculelist->MoleculeIdToItem((*iter)->getId());
|
---|
220 | QModelIndex mol_index = item->index();
|
---|
221 | if (!currently_selected.contains(mol_index))
|
---|
222 | selected.select(mol_index, setIndexToLastColumn(mol_index));
|
---|
223 | else
|
---|
224 | already_selected_indices.insert(mol_index);
|
---|
225 | }
|
---|
226 | }
|
---|
227 | {
|
---|
228 | QModelIndex mol_index;
|
---|
229 | foreach(mol_index, currently_selected.indexes()) {
|
---|
230 | std::set<QModelIndex>::const_iterator iter =
|
---|
231 | already_selected_indices.find(mol_index);
|
---|
232 | if (iter == already_selected_indices.end())
|
---|
233 | deselected.select(mol_index, setIndexToLastColumn(mol_index));
|
---|
234 | }
|
---|
235 | }
|
---|
236 | selecting = true;
|
---|
237 | if (!selected.indexes().empty())
|
---|
238 | selectionModel()->select(selected, QItemSelectionModel::Select);
|
---|
239 | if (!deselected.indexes().empty())
|
---|
240 | selectionModel()->select(deselected, QItemSelectionModel::Deselect);
|
---|
241 | selecting = false;
|
---|
242 | break;
|
---|
243 | }
|
---|
244 | default:
|
---|
245 | ASSERT(0, "QtMoleculeListView::recieveNotification() - cannot get here, not subscribed to channel "
|
---|
246 | +toString(notification->getChannelNo()));
|
---|
247 | break;
|
---|
248 | }
|
---|
249 | }
|
---|
250 | }
|
---|
251 |
|
---|
252 | void QtMoleculeListView::subjectKilled(Observable *publisher)
|
---|
253 | {}
|
---|