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