[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 |
|
---|
| 37 | #include "UIElements/Views/Qt4/MoleculeList/QtMoleculeList.hpp"
|
---|
[fcdf05] | 38 | #include "UIElements/Views/Qt4/MoleculeList/QtMoleculeItem.hpp"
|
---|
[015f8c] | 39 | #include "UIElements/Views/Qt4/QtSelectionChangedAgent.hpp"
|
---|
[f62e60] | 40 |
|
---|
| 41 | #include "CodePatterns/MemDebug.hpp"
|
---|
| 42 |
|
---|
| 43 | #include "CodePatterns/Observer/Notification.hpp"
|
---|
| 44 |
|
---|
[d2dbb5d] | 45 | #include "Actions/SelectionAction/Molecules/MoleculeByIdAction.hpp"
|
---|
| 46 | #include "Actions/SelectionAction/Molecules/NotMoleculeByIdAction.hpp"
|
---|
| 47 |
|
---|
[f62e60] | 48 | #include "World.hpp"
|
---|
| 49 |
|
---|
| 50 | QtMoleculeListView::QtMoleculeListView(QWidget * _parent) :
|
---|
[d2dbb5d] | 51 | QTreeView(_parent),
|
---|
| 52 | Observer("QtMoleculeListView"),
|
---|
| 53 | selecting(false)
|
---|
[f62e60] | 54 | {
|
---|
| 55 | setSelectionMode(QAbstractItemView::MultiSelection);
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | QtMoleculeListView::~QtMoleculeListView()
|
---|
[015f8c] | 59 | {}
|
---|
[f62e60] | 60 |
|
---|
| 61 | void QtMoleculeListView::setModel(QtMoleculeList *_moleculelist)
|
---|
| 62 | {
|
---|
| 63 | QTreeView::setModel(_moleculelist);
|
---|
[d2dbb5d] | 64 | // clicking a molecule means calling SelectionAction
|
---|
[f62e60] | 65 | connect(
|
---|
| 66 | selectionModel(), SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
|
---|
[9a4880] | 67 | this, SLOT(rowsSelected(const QItemSelection &, const QItemSelection &)), Qt::DirectConnection);
|
---|
[f62e60] | 68 | }
|
---|
| 69 |
|
---|
[015f8c] | 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 |
|
---|
[f62e60] | 79 | void QtMoleculeListView::update(Observable *publisher)
|
---|
| 80 | {}
|
---|
| 81 |
|
---|
[d2dbb5d] | 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.");
|
---|
[fcdf05] | 88 | return_index = parent_index.child(_index.row(), QtMoleculeItem::OCCURRENCE);
|
---|
[d2dbb5d] | 89 | // return_index =
|
---|
| 90 | // model()->invisibleRootItem()->child(
|
---|
| 91 | // _index.row(),
|
---|
[fcdf05] | 92 | // QtMoleculeItem::OCCURRENCE)->index();
|
---|
[d2dbb5d] | 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))) {
|
---|
[69b434] | 113 | const moleculeId_t mol_id = moleculelist->IndexToMoleculeId(index);
|
---|
[1259df] | 114 | const molecule * const mol = const_cast<const World &>(World::getInstance()).
|
---|
| 115 | getMolecule(MoleculeById(mol_id));
|
---|
[d2dbb5d] | 116 | // check for invalid molecule
|
---|
| 117 | if (mol_id < 0)
|
---|
| 118 | continue;
|
---|
| 119 | // means we are looking at deselection because of removal (in World)
|
---|
[69b434] | 120 | if (mol == NULL)
|
---|
[d2dbb5d] | 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)
|
---|
[69b434] | 136 | if ((index.column() == 0) && (!selectionModel()->isSelected(index))) {
|
---|
| 137 | const moleculeId_t mol_id = moleculelist->IndexToMoleculeId(index);
|
---|
[1259df] | 138 | const molecule * const mol = const_cast<const World &>(World::getInstance()).
|
---|
| 139 | getMolecule(MoleculeById(mol_id));
|
---|
[d2dbb5d] | 140 | // check for invalid molecule
|
---|
| 141 | if (mol_id < 0)
|
---|
| 142 | continue;
|
---|
| 143 | // means we are looking at deselection because of removal (in World)
|
---|
[69b434] | 144 | if (mol == NULL)
|
---|
[d2dbb5d] | 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 |
|
---|
[015f8c] | 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());
|
---|
[a39d72] | 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 | }
|
---|
[015f8c] | 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());
|
---|
[a39d72] | 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 | }
|
---|
[015f8c] | 197 |
|
---|
| 198 | selecting = false;
|
---|
| 199 | }
|
---|
| 200 |
|
---|
| 201 |
|
---|
[f62e60] | 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
|
---|
[97445f] | 209 | const std::vector<const molecule *> selectedMolecules =
|
---|
[f62e60] | 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;
|
---|
[97445f] | 216 | for (std::vector<const molecule *>::const_iterator iter = selectedMolecules.begin();
|
---|
[d2dbb5d] | 217 | iter != selectedMolecules.end(); ++iter) {
|
---|
[fcdf05] | 218 | if (moleculelist->isMoleculeItemPresent((*iter)->getId())) {
|
---|
| 219 | QtMoleculeItem *item = moleculelist->MoleculeIdToItem((*iter)->getId());
|
---|
[ec6abc] | 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 | }
|
---|
[f62e60] | 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())
|
---|
[d2dbb5d] | 233 | deselected.select(mol_index, setIndexToLastColumn(mol_index));
|
---|
[f62e60] | 234 | }
|
---|
| 235 | }
|
---|
[d2dbb5d] | 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;
|
---|
[f62e60] | 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 | {}
|
---|