[f62e60] | 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 |
|
---|
[0f368b] | 37 | #include <QtCore/QMetaType>
|
---|
| 38 |
|
---|
[f62e60] | 39 | #include "UIElements/Views/Qt4/MoleculeList/QtMoleculeList.hpp"
|
---|
[fcdf05] | 40 | #include "UIElements/Views/Qt4/MoleculeList/QtMoleculeItem.hpp"
|
---|
[f62e60] | 41 |
|
---|
| 42 | #include "CodePatterns/MemDebug.hpp"
|
---|
| 43 |
|
---|
| 44 | #include "CodePatterns/Observer/Notification.hpp"
|
---|
| 45 |
|
---|
[d2dbb5d] | 46 | #include "Actions/SelectionAction/Molecules/MoleculeByIdAction.hpp"
|
---|
| 47 | #include "Actions/SelectionAction/Molecules/NotMoleculeByIdAction.hpp"
|
---|
[0f368b] | 48 | #include "MoleculeObserver.hpp"
|
---|
| 49 | #include "molecule.hpp"
|
---|
[f62e60] | 50 | #include "World.hpp"
|
---|
| 51 |
|
---|
| 52 | QtMoleculeListView::QtMoleculeListView(QWidget * _parent) :
|
---|
[d2dbb5d] | 53 | QTreeView(_parent),
|
---|
| 54 | Observer("QtMoleculeListView"),
|
---|
| 55 | selecting(false)
|
---|
[f62e60] | 56 | {
|
---|
| 57 | setSelectionMode(QAbstractItemView::MultiSelection);
|
---|
[0f368b] | 58 |
|
---|
| 59 | qRegisterMetaType<QItemSelection>("QItemSelection");
|
---|
| 60 |
|
---|
| 61 | MoleculeObserver::getInstance().signOn(this, molecule::SelectionChanged);
|
---|
[f62e60] | 62 | }
|
---|
| 63 |
|
---|
| 64 | QtMoleculeListView::~QtMoleculeListView()
|
---|
[0f368b] | 65 | {
|
---|
| 66 | MoleculeObserver::getInstance().signOff(this, molecule::SelectionChanged);
|
---|
| 67 | }
|
---|
[f62e60] | 68 |
|
---|
| 69 | void QtMoleculeListView::setModel(QtMoleculeList *_moleculelist)
|
---|
| 70 | {
|
---|
| 71 | QTreeView::setModel(_moleculelist);
|
---|
[d2dbb5d] | 72 | // clicking a molecule means calling SelectionAction
|
---|
[f62e60] | 73 | connect(
|
---|
| 74 | selectionModel(), SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
|
---|
[9a4880] | 75 | this, SLOT(rowsSelected(const QItemSelection &, const QItemSelection &)), Qt::DirectConnection);
|
---|
[f62e60] | 76 | }
|
---|
| 77 |
|
---|
| 78 | void QtMoleculeListView::update(Observable *publisher)
|
---|
| 79 | {}
|
---|
| 80 |
|
---|
[d2dbb5d] | 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.");
|
---|
[fcdf05] | 87 | return_index = parent_index.child(_index.row(), QtMoleculeItem::OCCURRENCE);
|
---|
[d2dbb5d] | 88 | // return_index =
|
---|
| 89 | // model()->invisibleRootItem()->child(
|
---|
| 90 | // _index.row(),
|
---|
[fcdf05] | 91 | // QtMoleculeItem::OCCURRENCE)->index();
|
---|
[d2dbb5d] | 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))) {
|
---|
[69b434] | 112 | const moleculeId_t mol_id = moleculelist->IndexToMoleculeId(index);
|
---|
[1259df] | 113 | const molecule * const mol = const_cast<const World &>(World::getInstance()).
|
---|
| 114 | getMolecule(MoleculeById(mol_id));
|
---|
[d2dbb5d] | 115 | // check for invalid molecule
|
---|
| 116 | if (mol_id < 0)
|
---|
| 117 | continue;
|
---|
| 118 | // means we are looking at deselection because of removal (in World)
|
---|
[69b434] | 119 | if (mol == NULL)
|
---|
[d2dbb5d] | 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)
|
---|
[69b434] | 135 | if ((index.column() == 0) && (!selectionModel()->isSelected(index))) {
|
---|
| 136 | const moleculeId_t mol_id = moleculelist->IndexToMoleculeId(index);
|
---|
[1259df] | 137 | const molecule * const mol = const_cast<const World &>(World::getInstance()).
|
---|
| 138 | getMolecule(MoleculeById(mol_id));
|
---|
[d2dbb5d] | 139 | // check for invalid molecule
|
---|
| 140 | if (mol_id < 0)
|
---|
| 141 | continue;
|
---|
| 142 | // means we are looking at deselection because of removal (in World)
|
---|
[69b434] | 143 | if (mol == NULL)
|
---|
[d2dbb5d] | 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 |
|
---|
[015f8c] | 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());
|
---|
[a39d72] | 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 | }
|
---|
[015f8c] | 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());
|
---|
[a39d72] | 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 | }
|
---|
[015f8c] | 196 |
|
---|
| 197 | selecting = false;
|
---|
| 198 | }
|
---|
| 199 |
|
---|
| 200 |
|
---|
[f62e60] | 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
|
---|
[97445f] | 208 | const std::vector<const molecule *> selectedMolecules =
|
---|
[f62e60] | 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;
|
---|
[97445f] | 215 | for (std::vector<const molecule *>::const_iterator iter = selectedMolecules.begin();
|
---|
[d2dbb5d] | 216 | iter != selectedMolecules.end(); ++iter) {
|
---|
[fcdf05] | 217 | if (moleculelist->isMoleculeItemPresent((*iter)->getId())) {
|
---|
| 218 | QtMoleculeItem *item = moleculelist->MoleculeIdToItem((*iter)->getId());
|
---|
[ec6abc] | 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 | }
|
---|
[f62e60] | 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())
|
---|
[d2dbb5d] | 232 | deselected.select(mol_index, setIndexToLastColumn(mol_index));
|
---|
[f62e60] | 233 | }
|
---|
| 234 | }
|
---|
[d2dbb5d] | 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;
|
---|
[f62e60] | 241 | break;
|
---|
| 242 | }
|
---|
| 243 | default:
|
---|
[0f368b] | 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 "
|
---|
[f62e60] | 261 | +toString(notification->getChannelNo()));
|
---|
| 262 | break;
|
---|
| 263 | }
|
---|
[0f368b] | 264 | } else {
|
---|
| 265 | ASSERT(0,
|
---|
| 266 | "QtMoleculeListView::recieveNotification() - cannot get here, unknown publisher "+toString(publisher));
|
---|
[f62e60] | 267 | }
|
---|
| 268 | }
|
---|
| 269 |
|
---|
| 270 | void QtMoleculeListView::subjectKilled(Observable *publisher)
|
---|
| 271 | {}
|
---|