[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"
|
---|
[f62e60] | 39 |
|
---|
| 40 | #include "CodePatterns/MemDebug.hpp"
|
---|
| 41 |
|
---|
| 42 | #include "CodePatterns/Observer/Notification.hpp"
|
---|
| 43 |
|
---|
[d2dbb5d] | 44 | #include "Actions/SelectionAction/Molecules/MoleculeByIdAction.hpp"
|
---|
| 45 | #include "Actions/SelectionAction/Molecules/NotMoleculeByIdAction.hpp"
|
---|
| 46 |
|
---|
[f62e60] | 47 | #include "World.hpp"
|
---|
| 48 |
|
---|
| 49 | QtMoleculeListView::QtMoleculeListView(QWidget * _parent) :
|
---|
[d2dbb5d] | 50 | QTreeView(_parent),
|
---|
| 51 | Observer("QtMoleculeListView"),
|
---|
| 52 | selecting(false)
|
---|
[f62e60] | 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);
|
---|
[d2dbb5d] | 67 | // clicking a molecule means calling SelectionAction
|
---|
[f62e60] | 68 | connect(
|
---|
| 69 | selectionModel(), SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
|
---|
[9a4880] | 70 | this, SLOT(rowsSelected(const QItemSelection &, const QItemSelection &)), Qt::DirectConnection);
|
---|
[f62e60] | 71 | }
|
---|
| 72 |
|
---|
| 73 | void QtMoleculeListView::update(Observable *publisher)
|
---|
| 74 | {}
|
---|
| 75 |
|
---|
[d2dbb5d] | 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.");
|
---|
[fcdf05] | 82 | return_index = parent_index.child(_index.row(), QtMoleculeItem::OCCURRENCE);
|
---|
[d2dbb5d] | 83 | // return_index =
|
---|
| 84 | // model()->invisibleRootItem()->child(
|
---|
| 85 | // _index.row(),
|
---|
[fcdf05] | 86 | // QtMoleculeItem::OCCURRENCE)->index();
|
---|
[d2dbb5d] | 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))) {
|
---|
[69b434] | 107 | const moleculeId_t mol_id = moleculelist->IndexToMoleculeId(index);
|
---|
[1259df] | 108 | const molecule * const mol = const_cast<const World &>(World::getInstance()).
|
---|
| 109 | getMolecule(MoleculeById(mol_id));
|
---|
[d2dbb5d] | 110 | // check for invalid molecule
|
---|
| 111 | if (mol_id < 0)
|
---|
| 112 | continue;
|
---|
| 113 | // means we are looking at deselection because of removal (in World)
|
---|
[69b434] | 114 | if (mol == NULL)
|
---|
[d2dbb5d] | 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)
|
---|
[69b434] | 130 | if ((index.column() == 0) && (!selectionModel()->isSelected(index))) {
|
---|
| 131 | const moleculeId_t mol_id = moleculelist->IndexToMoleculeId(index);
|
---|
[1259df] | 132 | const molecule * const mol = const_cast<const World &>(World::getInstance()).
|
---|
| 133 | getMolecule(MoleculeById(mol_id));
|
---|
[d2dbb5d] | 134 | // check for invalid molecule
|
---|
| 135 | if (mol_id < 0)
|
---|
| 136 | continue;
|
---|
| 137 | // means we are looking at deselection because of removal (in World)
|
---|
[69b434] | 138 | if (mol == NULL)
|
---|
[d2dbb5d] | 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 |
|
---|
[f62e60] | 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
|
---|
[97445f] | 158 | const std::vector<const molecule *> selectedMolecules =
|
---|
[f62e60] | 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;
|
---|
[97445f] | 165 | for (std::vector<const molecule *>::const_iterator iter = selectedMolecules.begin();
|
---|
[d2dbb5d] | 166 | iter != selectedMolecules.end(); ++iter) {
|
---|
[fcdf05] | 167 | if (moleculelist->isMoleculeItemPresent((*iter)->getId())) {
|
---|
| 168 | QtMoleculeItem *item = moleculelist->MoleculeIdToItem((*iter)->getId());
|
---|
[ec6abc] | 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 | }
|
---|
[f62e60] | 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())
|
---|
[d2dbb5d] | 182 | deselected.select(mol_index, setIndexToLastColumn(mol_index));
|
---|
[f62e60] | 183 | }
|
---|
| 184 | }
|
---|
[d2dbb5d] | 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;
|
---|
[f62e60] | 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 | {}
|
---|