[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 |
|
---|
[9eb71b3] | 42 | //#include "CodePatterns/MemDebug.hpp"
|
---|
[f62e60] | 43 |
|
---|
[d2dbb5d] | 44 | #include "Actions/SelectionAction/Molecules/MoleculeByIdAction.hpp"
|
---|
| 45 | #include "Actions/SelectionAction/Molecules/NotMoleculeByIdAction.hpp"
|
---|
[0f368b] | 46 | #include "molecule.hpp"
|
---|
[f62e60] | 47 | #include "World.hpp"
|
---|
| 48 |
|
---|
| 49 | QtMoleculeListView::QtMoleculeListView(QWidget * _parent) :
|
---|
[d2dbb5d] | 50 | QTreeView(_parent),
|
---|
| 51 | selecting(false)
|
---|
[f62e60] | 52 | {
|
---|
| 53 | setSelectionMode(QAbstractItemView::MultiSelection);
|
---|
[0f368b] | 54 |
|
---|
| 55 | qRegisterMetaType<QItemSelection>("QItemSelection");
|
---|
[f62e60] | 56 | }
|
---|
| 57 |
|
---|
| 58 | QtMoleculeListView::~QtMoleculeListView()
|
---|
[0f368b] | 59 | {
|
---|
| 60 | }
|
---|
[f62e60] | 61 |
|
---|
| 62 | void QtMoleculeListView::setModel(QtMoleculeList *_moleculelist)
|
---|
| 63 | {
|
---|
| 64 | QTreeView::setModel(_moleculelist);
|
---|
[d2dbb5d] | 65 | // clicking a molecule means calling SelectionAction
|
---|
[f62e60] | 66 | connect(
|
---|
| 67 | selectionModel(), SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
|
---|
[9a4880] | 68 | this, SLOT(rowsSelected(const QItemSelection &, const QItemSelection &)), Qt::DirectConnection);
|
---|
[b2eeaf] | 69 | connect(_moleculelist, SIGNAL(MayStartSelections()),
|
---|
| 70 | this, SLOT(activateSelections()), Qt::DirectConnection);
|
---|
| 71 | connect(_moleculelist, SIGNAL(MayNotStartSelections()),
|
---|
| 72 | this, SLOT(deactivateSelections()), Qt::DirectConnection);
|
---|
[f62e60] | 73 |
|
---|
[3054f4a] | 74 | connect(&_moleculelist->observer, SIGNAL(SelectionChanged(QtObservedMolecule::ptr)),
|
---|
| 75 | this, SLOT(selectionChanged(QtObservedMolecule::ptr)));
|
---|
| 76 | }
|
---|
[f62e60] | 77 |
|
---|
[d2dbb5d] | 78 | QModelIndex QtMoleculeListView::setIndexToLastColumn(const QModelIndex &_index) const
|
---|
| 79 | {
|
---|
| 80 | QModelIndex return_index;
|
---|
| 81 | QModelIndex parent_index = _index.parent();
|
---|
| 82 | ASSERT (parent_index.isValid(),
|
---|
| 83 | "QtMoleculeListView::setIndexToLastColumn() - _index has no valid parent.");
|
---|
[fcdf05] | 84 | return_index = parent_index.child(_index.row(), QtMoleculeItem::OCCURRENCE);
|
---|
[d2dbb5d] | 85 | // return_index =
|
---|
| 86 | // model()->invisibleRootItem()->child(
|
---|
| 87 | // _index.row(),
|
---|
[fcdf05] | 88 | // QtMoleculeItem::OCCURRENCE)->index();
|
---|
[d2dbb5d] | 89 | return return_index;
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | void QtMoleculeListView::rowsSelected(
|
---|
| 93 | const QItemSelection& selected, const QItemSelection& deselected)
|
---|
| 94 | {
|
---|
| 95 | if (selecting)
|
---|
| 96 | return;
|
---|
| 97 |
|
---|
| 98 | selecting = true;
|
---|
| 99 |
|
---|
| 100 | // Select all molecules which belong to newly selected rows.
|
---|
| 101 | QtMoleculeList *moleculelist = dynamic_cast<QtMoleculeList *>(model());
|
---|
| 102 | QModelIndex index;
|
---|
| 103 | {
|
---|
| 104 | QModelIndexList items = selected.indexes();
|
---|
| 105 | molids_t ids;
|
---|
| 106 | ids.reserve(items.size());
|
---|
| 107 | foreach (index, items)
|
---|
| 108 | if ((index.column() == 0) && (selectionModel()->isSelected(index))) {
|
---|
[3054f4a] | 109 | const moleculeId_t mol_id = moleculelist->observer.getIdtoIndex(
|
---|
| 110 | moleculelist->IndexToMoleculeId(index));
|
---|
[1259df] | 111 | const molecule * const mol = const_cast<const World &>(World::getInstance()).
|
---|
| 112 | getMolecule(MoleculeById(mol_id));
|
---|
[d2dbb5d] | 113 | // check for invalid molecule
|
---|
| 114 | if (mol_id < 0)
|
---|
| 115 | continue;
|
---|
| 116 | // means we are looking at deselection because of removal (in World)
|
---|
[69b434] | 117 | if (mol == NULL)
|
---|
[d2dbb5d] | 118 | continue;
|
---|
| 119 | if (!World::getInstance().isSelected(mol))
|
---|
| 120 | ids.push_back(mol_id);
|
---|
| 121 | //std::cout << "select molecule" << std::endl;
|
---|
| 122 | }
|
---|
| 123 | if (!ids.empty())
|
---|
| 124 | MoleCuilder::SelectionMoleculeById(ids);
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | // Unselect all molecules which belong to newly unselected rows.
|
---|
| 128 | {
|
---|
| 129 | QModelIndexList items = deselected.indexes();
|
---|
| 130 | molids_t ids;
|
---|
| 131 | ids.reserve(items.size());
|
---|
| 132 | foreach (index, items)
|
---|
[69b434] | 133 | if ((index.column() == 0) && (!selectionModel()->isSelected(index))) {
|
---|
[3054f4a] | 134 | const moleculeId_t mol_id = moleculelist->observer.getIdtoIndex(
|
---|
| 135 | moleculelist->IndexToMoleculeId(index));
|
---|
[d2dbb5d] | 136 | // check for invalid molecule
|
---|
[abb115] | 137 | if (mol_id == (moleculeId_t)-1)
|
---|
[d2dbb5d] | 138 | continue;
|
---|
[abb115] | 139 | const molecule * const mol = const_cast<const World &>(World::getInstance()).
|
---|
| 140 | getMolecule(MoleculeById(mol_id));
|
---|
[d2dbb5d] | 141 | // means we are looking at deselection because of removal (in World)
|
---|
[69b434] | 142 | if (mol == NULL)
|
---|
[d2dbb5d] | 143 | continue;
|
---|
| 144 | if (World::getInstance().isSelected(mol))
|
---|
| 145 | ids.push_back(mol_id);
|
---|
| 146 | //std::cout << "unselect molecule" << std::endl;
|
---|
| 147 | }
|
---|
| 148 | if (!ids.empty())
|
---|
| 149 | MoleCuilder::SelectionNotMoleculeById(ids);
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 | selecting = false;
|
---|
| 153 | }
|
---|
| 154 |
|
---|
[b2eeaf] | 155 | void QtMoleculeListView::activateSelections()
|
---|
| 156 | {
|
---|
| 157 | selecting = false;
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | void QtMoleculeListView::deactivateSelections()
|
---|
| 161 | {
|
---|
| 162 | selecting = true;
|
---|
| 163 | }
|
---|
| 164 |
|
---|
[3054f4a] | 165 | void QtMoleculeListView::selectionChanged(const QtObservedMolecule::ptr _molecule)
|
---|
| 166 | {
|
---|
| 167 | if (_molecule->getMolSelected())
|
---|
| 168 | MoleculeSelected(_molecule->getIndex());
|
---|
| 169 | else
|
---|
| 170 | MoleculeUnselected(_molecule->getIndex());
|
---|
| 171 | }
|
---|
| 172 |
|
---|
| 173 | void QtMoleculeListView::MoleculeSelected(ObservedValue_Index_t _id)
|
---|
[015f8c] | 174 | {
|
---|
| 175 | if (selecting)
|
---|
| 176 | return;
|
---|
| 177 |
|
---|
| 178 | selecting = true;
|
---|
| 179 |
|
---|
| 180 | const QtMoleculeList *moleculelist = dynamic_cast<const QtMoleculeList *>(model());
|
---|
[a39d72] | 181 | if (moleculelist->isMoleculeItemPresent(_id)) {
|
---|
| 182 | QModelIndex index = moleculelist->MoleculeIdToIndex(_id);
|
---|
| 183 | // ASSERT( !selectionModel()->isSelected(index),
|
---|
| 184 | // "QtMoleculeListView::MoleculeSelected() - row to molecule "
|
---|
[3054f4a] | 185 | // +toString(mol->getMolIndex())+" is already selected.");
|
---|
[a39d72] | 186 |
|
---|
| 187 | // select the full row
|
---|
| 188 | expand(index);
|
---|
| 189 | selectionModel()->select(index, QItemSelectionModel::Select | QItemSelectionModel::Rows);
|
---|
| 190 | }
|
---|
[015f8c] | 191 |
|
---|
| 192 | selecting = false;
|
---|
| 193 | }
|
---|
| 194 |
|
---|
[3054f4a] | 195 | void QtMoleculeListView::MoleculeUnselected(ObservedValue_Index_t _id)
|
---|
[015f8c] | 196 | {
|
---|
| 197 | if (selecting)
|
---|
| 198 | return;
|
---|
| 199 |
|
---|
| 200 | selecting = true;
|
---|
| 201 |
|
---|
| 202 | const QtMoleculeList *moleculelist = dynamic_cast<const QtMoleculeList *>(model());
|
---|
[a39d72] | 203 | if (moleculelist->isMoleculeItemPresent(_id)) {
|
---|
| 204 | QModelIndex index = moleculelist->MoleculeIdToIndex(_id);
|
---|
| 205 | // ASSERT( selectionModel()->isSelected(index),
|
---|
| 206 | // "QtMoleculeListView::MoleculeSelected() - row to molecule "
|
---|
[3054f4a] | 207 | // +toString(mol->getMolIndex())+" is already unselected.");
|
---|
[a39d72] | 208 |
|
---|
| 209 | // unselect the full row
|
---|
| 210 | expand(index);
|
---|
| 211 | selectionModel()->select(index, QItemSelectionModel::Deselect | QItemSelectionModel::Rows);
|
---|
| 212 | }
|
---|
[015f8c] | 213 |
|
---|
| 214 | selecting = false;
|
---|
| 215 | }
|
---|