| 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" | 
|---|
| 38 | #include "UIElements/Views/Qt4/MoleculeList/QtMoleculeItemFactory.hpp" | 
|---|
| 39 |  | 
|---|
| 40 | #include "CodePatterns/MemDebug.hpp" | 
|---|
| 41 |  | 
|---|
| 42 | #include "CodePatterns/Observer/Notification.hpp" | 
|---|
| 43 |  | 
|---|
| 44 | #include "Actions/SelectionAction/Molecules/MoleculeByIdAction.hpp" | 
|---|
| 45 | #include "Actions/SelectionAction/Molecules/NotMoleculeByIdAction.hpp" | 
|---|
| 46 |  | 
|---|
| 47 | #include "World.hpp" | 
|---|
| 48 |  | 
|---|
| 49 | QtMoleculeListView::QtMoleculeListView(QWidget * _parent) : | 
|---|
| 50 | QTreeView(_parent), | 
|---|
| 51 | Observer("QtMoleculeListView"), | 
|---|
| 52 | selecting(false) | 
|---|
| 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); | 
|---|
| 67 | // clicking a molecule means calling SelectionAction | 
|---|
| 68 | connect( | 
|---|
| 69 | selectionModel(), SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)), | 
|---|
| 70 | this, SLOT(rowsSelected(const QItemSelection &, const QItemSelection &))); | 
|---|
| 71 | } | 
|---|
| 72 |  | 
|---|
| 73 | void QtMoleculeListView::update(Observable *publisher) | 
|---|
| 74 | {} | 
|---|
| 75 |  | 
|---|
| 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."); | 
|---|
| 82 | return_index = parent_index.child(_index.row(), QtMoleculeItemFactory::OCCURRENCE); | 
|---|
| 83 | //    return_index = | 
|---|
| 84 | //        model()->invisibleRootItem()->child( | 
|---|
| 85 | //            _index.row(), | 
|---|
| 86 | //            QtMoleculeItemFactory::OCCURRENCE)->index(); | 
|---|
| 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))) { | 
|---|
| 107 | const molecule *mol = moleculelist->IndexToMolecule(index); | 
|---|
| 108 | if (mol == NULL) // means we are looking at deselection because of removal | 
|---|
| 109 | continue; | 
|---|
| 110 | int mol_id = mol->getId(); | 
|---|
| 111 | // check for invalid molecule | 
|---|
| 112 | if (mol_id < 0) | 
|---|
| 113 | continue; | 
|---|
| 114 | // means we are looking at deselection because of removal (in World) | 
|---|
| 115 | if (World::getInstance().getMolecule(MoleculeById(mol_id)) != mol) | 
|---|
| 116 | continue; | 
|---|
| 117 | if (!World::getInstance().isSelected(mol)) | 
|---|
| 118 | ids.push_back(mol_id); | 
|---|
| 119 | //std::cout << "select molecule" << std::endl; | 
|---|
| 120 | } | 
|---|
| 121 | if (!ids.empty()) | 
|---|
| 122 | MoleCuilder::SelectionMoleculeById(ids); | 
|---|
| 123 | } | 
|---|
| 124 |  | 
|---|
| 125 | // Unselect all molecules which belong to newly unselected rows. | 
|---|
| 126 | { | 
|---|
| 127 | QModelIndexList items = deselected.indexes(); | 
|---|
| 128 | molids_t ids; | 
|---|
| 129 | ids.reserve(items.size()); | 
|---|
| 130 | foreach (index, items) | 
|---|
| 131 | if ((index.column() == 0) && (!selectionModel()->isSelected(index))) { | 
|---|
| 132 | const molecule *mol = moleculelist->IndexToMolecule(index); | 
|---|
| 133 | if (mol == NULL) // means we are looking at deselection because of removal | 
|---|
| 134 | continue; | 
|---|
| 135 | int mol_id = mol->getId(); | 
|---|
| 136 | // check for invalid molecule | 
|---|
| 137 | if (mol_id < 0) | 
|---|
| 138 | continue; | 
|---|
| 139 | // means we are looking at deselection because of removal (in World) | 
|---|
| 140 | if (World::getInstance().getMolecule(MoleculeById(mol_id)) != mol) | 
|---|
| 141 | continue; | 
|---|
| 142 | if (World::getInstance().isSelected(mol)) | 
|---|
| 143 | ids.push_back(mol_id); | 
|---|
| 144 | //std::cout << "unselect molecule" << std::endl; | 
|---|
| 145 | } | 
|---|
| 146 | if (!ids.empty()) | 
|---|
| 147 | MoleCuilder::SelectionNotMoleculeById(ids); | 
|---|
| 148 | } | 
|---|
| 149 |  | 
|---|
| 150 | selecting = false; | 
|---|
| 151 | } | 
|---|
| 152 |  | 
|---|
| 153 | void QtMoleculeListView::recieveNotification(Observable *publisher, Notification_ptr notification) | 
|---|
| 154 | { | 
|---|
| 155 | if (dynamic_cast<World *>(publisher) != NULL) { | 
|---|
| 156 | switch (notification->getChannelNo()) { | 
|---|
| 157 | case World::SelectionChanged: | 
|---|
| 158 | { | 
|---|
| 159 | // obtain molecule selection from World and go through our selection step by step | 
|---|
| 160 | const std::vector<molecule *> selectedMolecules = | 
|---|
| 161 | const_cast<const World &>(World::getInstance()).getSelectedMolecules(); | 
|---|
| 162 | QItemSelection currently_selected = selectionModel()->selection(); | 
|---|
| 163 | QtMoleculeList *moleculelist = static_cast<QtMoleculeList *>(model()); | 
|---|
| 164 | QItemSelection selected; | 
|---|
| 165 | QItemSelection deselected; | 
|---|
| 166 | std::set<QModelIndex> already_selected_indices; | 
|---|
| 167 | for (std::vector<molecule *>::const_iterator iter = selectedMolecules.begin(); | 
|---|
| 168 | iter != selectedMolecules.end(); ++iter) { | 
|---|
| 169 | QModelIndex mol_index = moleculelist->MoleculeToItem(*iter)->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 | } | 
|---|
| 175 | { | 
|---|
| 176 | QModelIndex mol_index; | 
|---|
| 177 | foreach(mol_index, currently_selected.indexes()) { | 
|---|
| 178 | std::set<QModelIndex>::const_iterator iter = | 
|---|
| 179 | already_selected_indices.find(mol_index); | 
|---|
| 180 | if (iter == already_selected_indices.end()) | 
|---|
| 181 | deselected.select(mol_index, setIndexToLastColumn(mol_index)); | 
|---|
| 182 | } | 
|---|
| 183 | } | 
|---|
| 184 | selecting = true; | 
|---|
| 185 | if (!selected.indexes().empty()) | 
|---|
| 186 | selectionModel()->select(selected, QItemSelectionModel::Select); | 
|---|
| 187 | if (!deselected.indexes().empty()) | 
|---|
| 188 | selectionModel()->select(deselected, QItemSelectionModel::Deselect); | 
|---|
| 189 | selecting = false; | 
|---|
| 190 | break; | 
|---|
| 191 | } | 
|---|
| 192 | default: | 
|---|
| 193 | ASSERT(0, "QtMoleculeListView::recieveNotification() - cannot get here, not subscribed to channel " | 
|---|
| 194 | +toString(notification->getChannelNo())); | 
|---|
| 195 | break; | 
|---|
| 196 | } | 
|---|
| 197 | } | 
|---|
| 198 | } | 
|---|
| 199 |  | 
|---|
| 200 | void QtMoleculeListView::subjectKilled(Observable *publisher) | 
|---|
| 201 | {} | 
|---|