[4458b0] | 1 | /*
|
---|
| 2 | * Project: MoleCuilder
|
---|
| 3 | * Description: creates and alters molecular systems
|
---|
| 4 | * Copyright (C) 2010-2012 University of Bonn. 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 | * QtShapeList.cpp
|
---|
| 25 | *
|
---|
| 26 | * Created on: Sep 14, 2012
|
---|
| 27 | * Author: ankele
|
---|
| 28 | */
|
---|
| 29 |
|
---|
| 30 | // include config.h
|
---|
| 31 | #ifdef HAVE_CONFIG_H
|
---|
| 32 | #include <config.h>
|
---|
| 33 | #endif
|
---|
| 34 |
|
---|
| 35 | #include "Views/Qt4/QtShapeList.hpp"
|
---|
| 36 |
|
---|
| 37 | #include <iostream>
|
---|
| 38 |
|
---|
[9eb71b3] | 39 | //#include "CodePatterns/MemDebug.hpp"
|
---|
[4458b0] | 40 |
|
---|
[85c36d] | 41 | #include "CodePatterns/Observer/Notification.hpp"
|
---|
| 42 |
|
---|
[4458b0] | 43 | #include "Shapes/ShapeFactory.hpp"
|
---|
| 44 | #include "Shapes/ShapeRegistry.hpp"
|
---|
| 45 |
|
---|
| 46 | #include <QAbstractItemView>
|
---|
[a0d5ef] | 47 | #include "Actions/SelectionAction/Shapes/ShapeByNameAction.hpp"
|
---|
| 48 | #include "Actions/SelectionAction/Shapes/NotShapeByNameAction.hpp"
|
---|
[4458b0] | 49 |
|
---|
| 50 | using namespace std;
|
---|
| 51 |
|
---|
| 52 | const int QtShapeList::COLUMNCOUNT = COLUMNTYPES_MAX;
|
---|
| 53 | const char *QtShapeList::COLUMNNAMES[QtShapeList::COLUMNCOUNT]={"Name","Type","Center","Radius"};
|
---|
| 54 |
|
---|
| 55 | QtShapeList::QtShapeList(QWidget * _parent) :
|
---|
| 56 | QTreeWidget (_parent),
|
---|
[3c9ac3] | 57 | Observer("QtShapeList"),
|
---|
| 58 | shaperegistry_enabled(false)
|
---|
[4458b0] | 59 | {
|
---|
| 60 | setColumnCount(COLUMNCOUNT);
|
---|
| 61 | setSelectionMode(QAbstractItemView::MultiSelection);
|
---|
| 62 |
|
---|
| 63 | QStringList header;
|
---|
| 64 | for(int i=0; i<COLUMNCOUNT;++i)
|
---|
| 65 | header << COLUMNNAMES[i];
|
---|
| 66 | setHeaderLabels(header);
|
---|
| 67 |
|
---|
[a0d5ef] | 68 | selecting = false;
|
---|
| 69 |
|
---|
[85c36d] | 70 | refill(NULL);
|
---|
[4458b0] | 71 |
|
---|
| 72 | ShapeRegistry::getInstance().signOn(this);
|
---|
[85c36d] | 73 | ShapeRegistry::getInstance().signOn(this, ShapeRegistry::ShapeInserted);
|
---|
| 74 | ShapeRegistry::getInstance().signOn(this, ShapeRegistry::ShapeRemoved);
|
---|
| 75 | ShapeRegistry::getInstance().signOn(this, ShapeRegistry::SelectionChanged);
|
---|
[3c9ac3] | 76 | shaperegistry_enabled = true;
|
---|
[4458b0] | 77 |
|
---|
[a0d5ef] | 78 | connect(this,SIGNAL(itemSelectionChanged()),this,SLOT(rowSelected()));
|
---|
[4458b0] | 79 | }
|
---|
| 80 |
|
---|
| 81 | QtShapeList::~QtShapeList()
|
---|
| 82 | {
|
---|
[3c9ac3] | 83 | if (shaperegistry_enabled) {
|
---|
| 84 | ShapeRegistry::getInstance().signOff(this);
|
---|
| 85 | ShapeRegistry::getInstance().signOff(this, ShapeRegistry::ShapeInserted);
|
---|
| 86 | ShapeRegistry::getInstance().signOff(this, ShapeRegistry::ShapeRemoved);
|
---|
| 87 | ShapeRegistry::getInstance().signOff(this, ShapeRegistry::SelectionChanged);
|
---|
| 88 | }
|
---|
[4458b0] | 89 | }
|
---|
| 90 |
|
---|
[85c36d] | 91 | void QtShapeList::update(Observable *publisher) {}
|
---|
[4458b0] | 92 |
|
---|
[85c36d] | 93 | void QtShapeList::recieveNotification(Observable *publisher, Notification_ptr notification)
|
---|
| 94 | {
|
---|
| 95 | if (static_cast<ShapeRegistry*>(publisher) == ShapeRegistry::getPointer()) {
|
---|
| 96 | switch (notification->getChannelNo()) {
|
---|
| 97 | case ShapeRegistry::ShapeInserted:
|
---|
| 98 | {
|
---|
| 99 | refill(NULL);
|
---|
| 100 | break;
|
---|
| 101 | }
|
---|
| 102 | case ShapeRegistry::ShapeRemoved:
|
---|
| 103 | {
|
---|
| 104 | refill(ShapeRegistry::getInstance().lastChanged());
|
---|
| 105 | break;
|
---|
| 106 | }
|
---|
| 107 | case ShapeRegistry::SelectionChanged:
|
---|
| 108 | {
|
---|
| 109 | refill(NULL);
|
---|
| 110 | break;
|
---|
| 111 | }
|
---|
| 112 | default:
|
---|
| 113 | ASSERT(0, "QtShapeList::recieveNotification() - we cannot get here.");
|
---|
| 114 | break;
|
---|
| 115 | }
|
---|
[a9161d] | 116 | }
|
---|
[4458b0] | 117 | }
|
---|
| 118 |
|
---|
[7516f6] | 119 | void QtShapeList::refill(::Shape *ignore)
|
---|
| 120 | {
|
---|
| 121 | boost::recursive_mutex::scoped_lock lock(refill_mutex);
|
---|
[4458b0] | 122 |
|
---|
| 123 | clear();
|
---|
[a0d5ef] | 124 | shapeSelection.clear();
|
---|
[4458b0] | 125 |
|
---|
| 126 | ShapeRegistry ® = ShapeRegistry::getInstance();
|
---|
| 127 | ShapeFactory &factory = ShapeFactory::getInstance();
|
---|
| 128 |
|
---|
| 129 | ShapeRegistry::const_iterator iter;
|
---|
| 130 | for (iter = reg.getBeginIter(); iter != reg.getEndIter(); iter ++){
|
---|
| 131 | ::Shape *s = iter->second;
|
---|
[85c36d] | 132 | if (s == ignore)
|
---|
| 133 | continue;
|
---|
[4458b0] | 134 |
|
---|
| 135 | QTreeWidgetItem *treeItem = new QTreeWidgetItem(this);
|
---|
| 136 | treeItem->setText(NAME, QString(s->getName().c_str()));
|
---|
| 137 | treeItem->setText(TYPE, QString(factory.getShapeName(s->getType()).c_str()));
|
---|
| 138 | treeItem->setText(CENTER, QString(toString(s->getCenter()).c_str()));
|
---|
| 139 | treeItem->setText(RADIUS, QString::number(s->getRadius()));
|
---|
[43c79a] | 140 | treeItem->setSelected(reg.isSelected(s));
|
---|
[a0d5ef] | 141 |
|
---|
| 142 | shapeSelection.push_back(reg.isSelected(s));
|
---|
[4458b0] | 143 | }
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | void QtShapeList::paintEvent(QPaintEvent * event)
|
---|
| 147 | {
|
---|
[7516f6] | 148 | boost::recursive_mutex::scoped_lock lock(refill_mutex);
|
---|
[4458b0] | 149 | /*if (dirty)
|
---|
| 150 | refill();*/
|
---|
[7516f6] | 151 | QTreeWidget::paintEvent(event);
|
---|
[4458b0] | 152 | }
|
---|
| 153 |
|
---|
[3c9ac3] | 154 | void QtShapeList::subjectKilled(Observable *publisher)
|
---|
| 155 | {
|
---|
| 156 | // as a new instance should always already be present ... just sign on
|
---|
| 157 | if (static_cast<ShapeRegistry *>(publisher) == ShapeRegistry::getPointer()) {
|
---|
| 158 | shaperegistry_enabled = false;
|
---|
| 159 | }
|
---|
[4458b0] | 160 | }
|
---|
| 161 |
|
---|
| 162 | void QtShapeList::rowSelected()
|
---|
| 163 | {
|
---|
[7516f6] | 164 | boost::recursive_mutex::scoped_lock lock(refill_mutex);
|
---|
| 165 |
|
---|
[a0d5ef] | 166 | if (selecting)
|
---|
| 167 | return;
|
---|
[4458b0] | 168 | //std::cout << "rowSelected\n";
|
---|
[a0d5ef] | 169 | selecting = true;
|
---|
[4458b0] | 170 |
|
---|
| 171 | for (int i=0;i<topLevelItemCount();i++){
|
---|
| 172 | QTreeWidgetItem *item = topLevelItem(i);
|
---|
| 173 | bool newSelection = item->isSelected();
|
---|
[a0d5ef] | 174 | if (newSelection != shapeSelection[i]){
|
---|
| 175 | shapeSelection[i] = newSelection;
|
---|
[4458b0] | 176 |
|
---|
[a0d5ef] | 177 | std::string shapeName = item->text(NAME).toStdString();
|
---|
[4458b0] | 178 | if (newSelection)
|
---|
[a0d5ef] | 179 | MoleCuilder::SelectionShapeByName(shapeName);
|
---|
[4458b0] | 180 | else
|
---|
[a0d5ef] | 181 | MoleCuilder::SelectionNotShapeByName(shapeName);
|
---|
[4458b0] | 182 | }
|
---|
[a0d5ef] | 183 | }
|
---|
| 184 | selecting = false;
|
---|
[4458b0] | 185 | }
|
---|
| 186 |
|
---|