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 |
|
---|
49 | QtMoleculeListView::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 |
|
---|
59 | QtMoleculeListView::~QtMoleculeListView()
|
---|
60 | {
|
---|
61 | World::getInstance().signOff(this, World::SelectionChanged);
|
---|
62 | }
|
---|
63 |
|
---|
64 | void 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 |
|
---|
73 | void QtMoleculeListView::update(Observable *publisher)
|
---|
74 | {}
|
---|
75 |
|
---|
76 | QModelIndex 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 |
|
---|
90 | void 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 |
|
---|
151 | void 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 |
|
---|
201 | void QtMoleculeListView::subjectKilled(Observable *publisher)
|
---|
202 | {}
|
---|