/* * Project: MoleCuilder * Description: creates and alters molecular systems * Copyright (C) 2015 Frederik Heber. All rights reserved. * * * This file is part of MoleCuilder. * * MoleCuilder is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 2 of the License, or * (at your option) any later version. * * MoleCuilder is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with MoleCuilder. If not, see . */ /* * QtMoleculeListView.cpp * * Created on: Jan 17, 2015 * Author: heber */ // include config.h #ifdef HAVE_CONFIG_H #include #endif #include "QtMoleculeListView.hpp" #include "UIElements/Views/Qt4/MoleculeList/QtMoleculeList.hpp" #include "CodePatterns/MemDebug.hpp" #include "CodePatterns/Observer/Notification.hpp" #include "World.hpp" QtMoleculeListView::QtMoleculeListView(QWidget * _parent) : QTreeView(_parent), Observer("QtMoleculeListView") { setSelectionMode(QAbstractItemView::MultiSelection); World::getInstance().signOn(this, World::SelectionChanged); } QtMoleculeListView::~QtMoleculeListView() { World::getInstance().signOff(this, World::SelectionChanged); } void QtMoleculeListView::setModel(QtMoleculeList *_moleculelist) { QTreeView::setModel(_moleculelist); connect( selectionModel(), SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)), _moleculelist, SLOT(rowsSelected(const QItemSelection &, const QItemSelection &))); } void QtMoleculeListView::update(Observable *publisher) {} void QtMoleculeListView::recieveNotification(Observable *publisher, Notification_ptr notification) { if (dynamic_cast(publisher) != NULL) { switch (notification->getChannelNo()) { case World::SelectionChanged: { // obtain molecule selection from World and go through our selection step by step const std::vector selectedMolecules = const_cast(World::getInstance()).getSelectedMolecules(); QItemSelection currently_selected = selectionModel()->selection(); QtMoleculeList *moleculelist = static_cast(model()); QItemSelection selected; QItemSelection deselected; std::set already_selected_indices; for (std::vector::const_iterator iter = selectedMolecules.begin(); iter != selectedMolecules.begin(); ++iter) { QModelIndex mol_index = moleculelist->MoleculeToItem(*iter)->index(); if (!currently_selected.contains(mol_index)) selected.select(mol_index, mol_index); else already_selected_indices.insert(mol_index); } { QModelIndex mol_index; foreach(mol_index, currently_selected.indexes()) { std::set::const_iterator iter = already_selected_indices.find(mol_index); if (iter == already_selected_indices.end()) deselected.select(mol_index, mol_index); } } selectionChanged(selected, deselected); break; } default: ASSERT(0, "QtMoleculeListView::recieveNotification() - cannot get here, not subscribed to channel " +toString(notification->getChannelNo())); break; } } } void QtMoleculeListView::subjectKilled(Observable *publisher) {}