| 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 <QtCore/QMetaType> | 
|---|
| 38 |  | 
|---|
| 39 | #include "UIElements/Views/Qt4/MoleculeList/QtMoleculeList.hpp" | 
|---|
| 40 | #include "UIElements/Views/Qt4/MoleculeList/QtMoleculeItem.hpp" | 
|---|
| 41 |  | 
|---|
| 42 | //#include "CodePatterns/MemDebug.hpp" | 
|---|
| 43 |  | 
|---|
| 44 | #include "Actions/SelectionAction/Molecules/MoleculeByIdAction.hpp" | 
|---|
| 45 | #include "Actions/SelectionAction/Molecules/NotMoleculeByIdAction.hpp" | 
|---|
| 46 | #include "molecule.hpp" | 
|---|
| 47 | #include "World.hpp" | 
|---|
| 48 |  | 
|---|
| 49 | QtMoleculeListView::QtMoleculeListView(QWidget * _parent) : | 
|---|
| 50 | QTreeView(_parent), | 
|---|
| 51 | selecting(false) | 
|---|
| 52 | { | 
|---|
| 53 | setSelectionMode(QAbstractItemView::MultiSelection); | 
|---|
| 54 |  | 
|---|
| 55 | qRegisterMetaType<QItemSelection>("QItemSelection"); | 
|---|
| 56 | } | 
|---|
| 57 |  | 
|---|
| 58 | QtMoleculeListView::~QtMoleculeListView() | 
|---|
| 59 | { | 
|---|
| 60 | } | 
|---|
| 61 |  | 
|---|
| 62 | void QtMoleculeListView::setModel(QtMoleculeList *_moleculelist) | 
|---|
| 63 | { | 
|---|
| 64 | QTreeView::setModel(_moleculelist); | 
|---|
| 65 | // clicking a molecule means calling SelectionAction | 
|---|
| 66 | connect( | 
|---|
| 67 | selectionModel(), SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)), | 
|---|
| 68 | this, SLOT(rowsSelected(const QItemSelection &, const QItemSelection &)), Qt::DirectConnection); | 
|---|
| 69 | connect(_moleculelist, SIGNAL(MayStartSelections()), | 
|---|
| 70 | this, SLOT(activateSelections()), Qt::DirectConnection); | 
|---|
| 71 | connect(_moleculelist, SIGNAL(MayNotStartSelections()), | 
|---|
| 72 | this, SLOT(deactivateSelections()), Qt::DirectConnection); | 
|---|
| 73 |  | 
|---|
| 74 | connect(&_moleculelist->observer, SIGNAL(SelectionChanged(QtObservedMolecule::ptr)), | 
|---|
| 75 | this, SLOT(selectionChanged(QtObservedMolecule::ptr))); | 
|---|
| 76 | } | 
|---|
| 77 |  | 
|---|
| 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."); | 
|---|
| 84 | return_index = parent_index.child(_index.row(), QtMoleculeItem::OCCURRENCE); | 
|---|
| 85 | //    return_index = | 
|---|
| 86 | //        model()->invisibleRootItem()->child( | 
|---|
| 87 | //            _index.row(), | 
|---|
| 88 | //            QtMoleculeItem::OCCURRENCE)->index(); | 
|---|
| 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))) { | 
|---|
| 109 | const moleculeId_t mol_id = moleculelist->observer.getIdtoIndex( | 
|---|
| 110 | moleculelist->IndexToMoleculeId(index)); | 
|---|
| 111 | const molecule * const mol = const_cast<const World &>(World::getInstance()). | 
|---|
| 112 | getMolecule(MoleculeById(mol_id)); | 
|---|
| 113 | // check for invalid molecule | 
|---|
| 114 | if (mol_id < 0) | 
|---|
| 115 | continue; | 
|---|
| 116 | // means we are looking at deselection because of removal (in World) | 
|---|
| 117 | if (mol == NULL) | 
|---|
| 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) | 
|---|
| 133 | if ((index.column() == 0) && (!selectionModel()->isSelected(index))) { | 
|---|
| 134 | const moleculeId_t mol_id = moleculelist->observer.getIdtoIndex( | 
|---|
| 135 | moleculelist->IndexToMoleculeId(index)); | 
|---|
| 136 | // check for invalid molecule | 
|---|
| 137 | if (mol_id == (moleculeId_t)-1) | 
|---|
| 138 | continue; | 
|---|
| 139 | const molecule * const mol = const_cast<const World &>(World::getInstance()). | 
|---|
| 140 | getMolecule(MoleculeById(mol_id)); | 
|---|
| 141 | // means we are looking at deselection because of removal (in World) | 
|---|
| 142 | if (mol == NULL) | 
|---|
| 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 |  | 
|---|
| 155 | void QtMoleculeListView::activateSelections() | 
|---|
| 156 | { | 
|---|
| 157 | selecting = false; | 
|---|
| 158 | } | 
|---|
| 159 |  | 
|---|
| 160 | void QtMoleculeListView::deactivateSelections() | 
|---|
| 161 | { | 
|---|
| 162 | selecting = true; | 
|---|
| 163 | } | 
|---|
| 164 |  | 
|---|
| 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) | 
|---|
| 174 | { | 
|---|
| 175 | if (selecting) | 
|---|
| 176 | return; | 
|---|
| 177 |  | 
|---|
| 178 | selecting = true; | 
|---|
| 179 |  | 
|---|
| 180 | const QtMoleculeList *moleculelist = dynamic_cast<const QtMoleculeList *>(model()); | 
|---|
| 181 | if (moleculelist->isMoleculeItemPresent(_id)) { | 
|---|
| 182 | QModelIndex index = moleculelist->MoleculeIdToIndex(_id); | 
|---|
| 183 | //    ASSERT( !selectionModel()->isSelected(index), | 
|---|
| 184 | //        "QtMoleculeListView::MoleculeSelected() - row to molecule " | 
|---|
| 185 | //        +toString(mol->getMolIndex())+" is already selected."); | 
|---|
| 186 |  | 
|---|
| 187 | // select the full row | 
|---|
| 188 | expand(index); | 
|---|
| 189 | selectionModel()->select(index, QItemSelectionModel::Select | QItemSelectionModel::Rows); | 
|---|
| 190 | } | 
|---|
| 191 |  | 
|---|
| 192 | selecting = false; | 
|---|
| 193 | } | 
|---|
| 194 |  | 
|---|
| 195 | void QtMoleculeListView::MoleculeUnselected(ObservedValue_Index_t _id) | 
|---|
| 196 | { | 
|---|
| 197 | if (selecting) | 
|---|
| 198 | return; | 
|---|
| 199 |  | 
|---|
| 200 | selecting = true; | 
|---|
| 201 |  | 
|---|
| 202 | const QtMoleculeList *moleculelist = dynamic_cast<const QtMoleculeList *>(model()); | 
|---|
| 203 | if (moleculelist->isMoleculeItemPresent(_id)) { | 
|---|
| 204 | QModelIndex index = moleculelist->MoleculeIdToIndex(_id); | 
|---|
| 205 | //    ASSERT( selectionModel()->isSelected(index), | 
|---|
| 206 | //        "QtMoleculeListView::MoleculeSelected() - row to molecule " | 
|---|
| 207 | //        +toString(mol->getMolIndex())+" is already unselected."); | 
|---|
| 208 |  | 
|---|
| 209 | // unselect the full row | 
|---|
| 210 | expand(index); | 
|---|
| 211 | selectionModel()->select(index, QItemSelectionModel::Deselect | QItemSelectionModel::Rows); | 
|---|
| 212 | } | 
|---|
| 213 |  | 
|---|
| 214 | selecting = false; | 
|---|
| 215 | } | 
|---|