| 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 |  | 
|---|
| 39 | //#include "CodePatterns/MemDebug.hpp" | 
|---|
| 40 |  | 
|---|
| 41 | #include "CodePatterns/Observer/Notification.hpp" | 
|---|
| 42 |  | 
|---|
| 43 | #include "Shapes/ShapeFactory.hpp" | 
|---|
| 44 | #include "Shapes/ShapeRegistry.hpp" | 
|---|
| 45 |  | 
|---|
| 46 | #include <QAbstractItemView> | 
|---|
| 47 | #include "Actions/SelectionAction/Shapes/ShapeByNameAction.hpp" | 
|---|
| 48 | #include "Actions/SelectionAction/Shapes/NotShapeByNameAction.hpp" | 
|---|
| 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), | 
|---|
| 57 | Observer("QtShapeList"), | 
|---|
| 58 | shaperegistry_enabled(false) | 
|---|
| 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 |  | 
|---|
| 68 | selecting = false; | 
|---|
| 69 |  | 
|---|
| 70 | refill(NULL); | 
|---|
| 71 |  | 
|---|
| 72 | ShapeRegistry::getInstance().signOn(this); | 
|---|
| 73 | ShapeRegistry::getInstance().signOn(this, ShapeRegistry::ShapeInserted); | 
|---|
| 74 | ShapeRegistry::getInstance().signOn(this, ShapeRegistry::ShapeRemoved); | 
|---|
| 75 | ShapeRegistry::getInstance().signOn(this, ShapeRegistry::SelectionChanged); | 
|---|
| 76 | shaperegistry_enabled = true; | 
|---|
| 77 |  | 
|---|
| 78 | connect(this,SIGNAL(itemSelectionChanged()),this,SLOT(rowSelected())); | 
|---|
| 79 | } | 
|---|
| 80 |  | 
|---|
| 81 | QtShapeList::~QtShapeList() | 
|---|
| 82 | { | 
|---|
| 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 | } | 
|---|
| 89 | } | 
|---|
| 90 |  | 
|---|
| 91 | void QtShapeList::update(Observable *publisher) {} | 
|---|
| 92 |  | 
|---|
| 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 | } | 
|---|
| 116 | } | 
|---|
| 117 | } | 
|---|
| 118 |  | 
|---|
| 119 | void QtShapeList::refill(::Shape *ignore) | 
|---|
| 120 | { | 
|---|
| 121 | boost::recursive_mutex::scoped_lock lock(refill_mutex); | 
|---|
| 122 |  | 
|---|
| 123 | clear(); | 
|---|
| 124 | shapeSelection.clear(); | 
|---|
| 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; | 
|---|
| 132 | if (s == ignore) | 
|---|
| 133 | continue; | 
|---|
| 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())); | 
|---|
| 140 | treeItem->setSelected(reg.isSelected(s)); | 
|---|
| 141 |  | 
|---|
| 142 | shapeSelection.push_back(reg.isSelected(s)); | 
|---|
| 143 | } | 
|---|
| 144 | } | 
|---|
| 145 |  | 
|---|
| 146 | void QtShapeList::paintEvent(QPaintEvent * event) | 
|---|
| 147 | { | 
|---|
| 148 | boost::recursive_mutex::scoped_lock lock(refill_mutex); | 
|---|
| 149 | /*if (dirty) | 
|---|
| 150 | refill();*/ | 
|---|
| 151 | QTreeWidget::paintEvent(event); | 
|---|
| 152 | } | 
|---|
| 153 |  | 
|---|
| 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 | } | 
|---|
| 160 | } | 
|---|
| 161 |  | 
|---|
| 162 | void QtShapeList::rowSelected() | 
|---|
| 163 | { | 
|---|
| 164 | boost::recursive_mutex::scoped_lock lock(refill_mutex); | 
|---|
| 165 |  | 
|---|
| 166 | if (selecting) | 
|---|
| 167 | return; | 
|---|
| 168 | //std::cout << "rowSelected\n"; | 
|---|
| 169 | selecting = true; | 
|---|
| 170 |  | 
|---|
| 171 | for (int i=0;i<topLevelItemCount();i++){ | 
|---|
| 172 | QTreeWidgetItem *item = topLevelItem(i); | 
|---|
| 173 | bool newSelection = item->isSelected(); | 
|---|
| 174 | if (newSelection != shapeSelection[i]){ | 
|---|
| 175 | shapeSelection[i] = newSelection; | 
|---|
| 176 |  | 
|---|
| 177 | std::string shapeName = item->text(NAME).toStdString(); | 
|---|
| 178 | if (newSelection) | 
|---|
| 179 | MoleCuilder::SelectionShapeByName(shapeName); | 
|---|
| 180 | else | 
|---|
| 181 | MoleCuilder::SelectionNotShapeByName(shapeName); | 
|---|
| 182 | } | 
|---|
| 183 | } | 
|---|
| 184 | selecting = false; | 
|---|
| 185 | } | 
|---|
| 186 |  | 
|---|