| 1 | /*
 | 
|---|
| 2 |  * Project: MoleCuilder
 | 
|---|
| 3 |  * Description: creates and alters molecular systems
 | 
|---|
| 4 |  * Copyright (C)  2017 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 |  * QtGeometryList.cpp
 | 
|---|
| 25 |  *
 | 
|---|
| 26 |  *  Created on: Mar 25, 2017
 | 
|---|
| 27 |  *      Author: heber
 | 
|---|
| 28 |  */
 | 
|---|
| 29 | 
 | 
|---|
| 30 | // include config.h
 | 
|---|
| 31 | #ifdef HAVE_CONFIG_H
 | 
|---|
| 32 | #include <config.h>
 | 
|---|
| 33 | #endif
 | 
|---|
| 34 | 
 | 
|---|
| 35 | #include "Views/Qt4/QtGeometryList.hpp"
 | 
|---|
| 36 | 
 | 
|---|
| 37 | #include <iostream>
 | 
|---|
| 38 | 
 | 
|---|
| 39 | //#include "CodePatterns/MemDebug.hpp"
 | 
|---|
| 40 | 
 | 
|---|
| 41 | #include "CodePatterns/Observer/Notification.hpp"
 | 
|---|
| 42 | #include "CodePatterns/toString.hpp"
 | 
|---|
| 43 | 
 | 
|---|
| 44 | #include "Geometry/GeometryRegistry.hpp"
 | 
|---|
| 45 | 
 | 
|---|
| 46 | #include <QAbstractItemView>
 | 
|---|
| 47 | #include <QCoreApplication>
 | 
|---|
| 48 | 
 | 
|---|
| 49 | using namespace std;
 | 
|---|
| 50 | 
 | 
|---|
| 51 | const int QtGeometryList::COLUMNCOUNT = COLUMNTYPES_MAX;
 | 
|---|
| 52 | const char *QtGeometryList::COLUMNNAMES[QtGeometryList::COLUMNCOUNT]={"Name","Vector"};
 | 
|---|
| 53 | 
 | 
|---|
| 54 | QtGeometryList::QtGeometryList(QWidget * _parent) :
 | 
|---|
| 55 |     QTreeWidget (_parent),
 | 
|---|
| 56 |     Observer("QtGeometryList"),
 | 
|---|
| 57 |     geometryregistry_enabled(false)
 | 
|---|
| 58 | {
 | 
|---|
| 59 |   setColumnCount(COLUMNCOUNT);
 | 
|---|
| 60 |   setSelectionMode(QAbstractItemView::SingleSelection);
 | 
|---|
| 61 |   setSortingEnabled(true);
 | 
|---|
| 62 | 
 | 
|---|
| 63 |   QStringList header;
 | 
|---|
| 64 |         for(int i=0; i<COLUMNCOUNT;++i)
 | 
|---|
| 65 |           header << COLUMNNAMES[i];
 | 
|---|
| 66 |         setHeaderLabels(header);
 | 
|---|
| 67 |   sortByColumn(0);
 | 
|---|
| 68 | 
 | 
|---|
| 69 |   setStyleSheet("QTreeView::item:hover{background-color:#999966;}");
 | 
|---|
| 70 | 
 | 
|---|
| 71 |         refill(NULL);
 | 
|---|
| 72 | 
 | 
|---|
| 73 |         GeometryRegistry::getInstance().signOn(this);
 | 
|---|
| 74 |         GeometryRegistry::getInstance().signOn(this, GeometryRegistry::GeometryInserted);
 | 
|---|
| 75 |         GeometryRegistry::getInstance().signOn(this, GeometryRegistry::GeometryRemoved);
 | 
|---|
| 76 |         geometryregistry_enabled = true;
 | 
|---|
| 77 | 
 | 
|---|
| 78 |         QCoreApplication::instance()->installEventFilter(this);
 | 
|---|
| 79 | }
 | 
|---|
| 80 | 
 | 
|---|
| 81 | QtGeometryList::~QtGeometryList()
 | 
|---|
| 82 | {
 | 
|---|
| 83 |   if (geometryregistry_enabled) {
 | 
|---|
| 84 |     GeometryRegistry::getInstance().signOff(this);
 | 
|---|
| 85 |     GeometryRegistry::getInstance().signOff(this, GeometryRegistry::GeometryInserted);
 | 
|---|
| 86 |     GeometryRegistry::getInstance().signOff(this, GeometryRegistry::GeometryRemoved);
 | 
|---|
| 87 |   }
 | 
|---|
| 88 | }
 | 
|---|
| 89 | 
 | 
|---|
| 90 | void QtGeometryList::subjectKilled(Observable *publisher)
 | 
|---|
| 91 | {
 | 
|---|
| 92 |   // as a new instance should always already be present ... just sign on
 | 
|---|
| 93 |   if (static_cast<GeometryRegistry *>(publisher) == GeometryRegistry::getPointer()) {
 | 
|---|
| 94 |     geometryregistry_enabled = false;
 | 
|---|
| 95 |   }
 | 
|---|
| 96 | }
 | 
|---|
| 97 | 
 | 
|---|
| 98 | void QtGeometryList::update(Observable *publisher) {}
 | 
|---|
| 99 | 
 | 
|---|
| 100 | void QtGeometryList::recieveNotification(Observable *publisher, Notification_ptr notification)
 | 
|---|
| 101 | {
 | 
|---|
| 102 |   if (static_cast<GeometryRegistry*>(publisher) == GeometryRegistry::getPointer()) {
 | 
|---|
| 103 |     switch (notification->getChannelNo()) {
 | 
|---|
| 104 |       case GeometryRegistry::GeometryInserted:
 | 
|---|
| 105 |       {
 | 
|---|
| 106 |         refill(NULL);
 | 
|---|
| 107 |         break;
 | 
|---|
| 108 |       }
 | 
|---|
| 109 |       case GeometryRegistry::GeometryRemoved:
 | 
|---|
| 110 |       {
 | 
|---|
| 111 |         refill(GeometryRegistry::getInstance().lastChanged());
 | 
|---|
| 112 |         break;
 | 
|---|
| 113 |       }
 | 
|---|
| 114 |       default:
 | 
|---|
| 115 |         ASSERT(0, "QtGeometryList::recieveNotification() - we cannot get here.");
 | 
|---|
| 116 |         break;
 | 
|---|
| 117 |     }
 | 
|---|
| 118 |   }
 | 
|---|
| 119 | }
 | 
|---|
| 120 | 
 | 
|---|
| 121 | void QtGeometryList::refill(::GeometryObject *ignore)
 | 
|---|
| 122 | {
 | 
|---|
| 123 |   boost::recursive_mutex::scoped_lock lock(refill_mutex);
 | 
|---|
| 124 | 
 | 
|---|
| 125 |   clear();
 | 
|---|
| 126 | 
 | 
|---|
| 127 |   GeometryRegistry ® = GeometryRegistry::getInstance();
 | 
|---|
| 128 | 
 | 
|---|
| 129 |   GeometryRegistry::const_iterator iter;
 | 
|---|
| 130 |   for (iter = reg.getBeginIter(); iter != reg.getEndIter(); iter ++){
 | 
|---|
| 131 |     ::GeometryObject *v = iter->second;
 | 
|---|
| 132 |     if (v == ignore)
 | 
|---|
| 133 |       continue;
 | 
|---|
| 134 | 
 | 
|---|
| 135 |     QTreeWidgetItem *treeItem = new QTreeWidgetItem(this);
 | 
|---|
| 136 |     treeItem->setText(NAME, QString(v->getName().c_str()));
 | 
|---|
| 137 |     treeItem->setText(VECTOR, QString(toString(v->getVector()).c_str()));
 | 
|---|
| 138 |   }
 | 
|---|
| 139 | }
 | 
|---|
| 140 | 
 | 
|---|
| 141 | void QtGeometryList::paintEvent(QPaintEvent * event)
 | 
|---|
| 142 | {
 | 
|---|
| 143 |   boost::recursive_mutex::scoped_lock lock(refill_mutex);
 | 
|---|
| 144 |   /*if (dirty)
 | 
|---|
| 145 |     refill(NULL);*/
 | 
|---|
| 146 |   QTreeWidget::paintEvent(event);
 | 
|---|
| 147 | }
 | 
|---|
| 148 | 
 | 
|---|
| 149 | bool QtGeometryList::eventFilter(QObject* object, QEvent* event)
 | 
|---|
| 150 | {
 | 
|---|
| 151 |     if(event->type() == QEvent::MouseMove)
 | 
|---|
| 152 |     {
 | 
|---|
| 153 |          mouseMoveFunction(static_cast<QMouseEvent*>(event));
 | 
|---|
| 154 |     }
 | 
|---|
| 155 |     // Repeat for other mouse events
 | 
|---|
| 156 | 
 | 
|---|
| 157 |    // returning true blocks event returning false doesnt so if you want to block all mouse events except what your handling then return true inside each of your ifs
 | 
|---|
| 158 |    return false;
 | 
|---|
| 159 | }
 | 
|---|
| 160 | 
 | 
|---|
| 161 | void QtGeometryList::mouseMoveFunction(QMouseEvent * event)
 | 
|---|
| 162 | {
 | 
|---|
| 163 |   boost::recursive_mutex::scoped_lock lock(refill_mutex);
 | 
|---|
| 164 | 
 | 
|---|
| 165 |   if (event->type() == QEvent::MouseMove) {
 | 
|---|
| 166 |     QTreeWidgetItem* current = itemAt(event->pos());
 | 
|---|
| 167 |     if (current == NULL)
 | 
|---|
| 168 |       return;
 | 
|---|
| 169 |     // reset all tooltips
 | 
|---|
| 170 |     bool NoneSelected = true;
 | 
|---|
| 171 |     for (int i=0;i<topLevelItemCount();i++){
 | 
|---|
| 172 |       QTreeWidgetItem *item = topLevelItem(i);
 | 
|---|
| 173 |       item->setToolTip(NAME, tr(""));
 | 
|---|
| 174 |       item->setToolTip(VECTOR, tr(""));
 | 
|---|
| 175 |       NoneSelected &= !item->isSelected();
 | 
|---|
| 176 |     }
 | 
|---|
| 177 |     const std::string name = current->text(NAME).toStdString();
 | 
|---|
| 178 |     GeometryObject *v = GeometryRegistry::getInstance().getByName(name);
 | 
|---|
| 179 |     const Vector currentVec = v->getVector();
 | 
|---|
| 180 |     if ((NoneSelected) || (current->isSelected())) {
 | 
|---|
| 181 |       // show norm
 | 
|---|
| 182 |       std::string tooltiptext("Length:");
 | 
|---|
| 183 |       tooltiptext += toString<double>(currentVec.Norm());
 | 
|---|
| 184 |       tooltiptext += std::string(" Angström");
 | 
|---|
| 185 |       current->setToolTip(NAME, trUtf8(tooltiptext.c_str()));
 | 
|---|
| 186 |       current->setToolTip(VECTOR, trUtf8(tooltiptext.c_str()));
 | 
|---|
| 187 |     } else {
 | 
|---|
| 188 |       for (int i=0;i<topLevelItemCount();i++){
 | 
|---|
| 189 |         QTreeWidgetItem *item = topLevelItem(i);
 | 
|---|
| 190 |         if (item->isSelected()) {
 | 
|---|
| 191 |           // show angle
 | 
|---|
| 192 |           const std::string othername = item->text(NAME).toStdString();
 | 
|---|
| 193 |           GeometryObject *otherv = GeometryRegistry::getInstance().getByName(othername);
 | 
|---|
| 194 |           const Vector otherVec = otherv->getVector();
 | 
|---|
| 195 |           std::string tooltiptext("Angle:");
 | 
|---|
| 196 |           tooltiptext += toString<double>(currentVec.Angle(otherVec)*180/M_PI);
 | 
|---|
| 197 |           tooltiptext += std::string(" deg");
 | 
|---|
| 198 |           current->setToolTip(NAME, trUtf8(tooltiptext.c_str()));
 | 
|---|
| 199 |           current->setToolTip(VECTOR, trUtf8(tooltiptext.c_str()));
 | 
|---|
| 200 |           break;
 | 
|---|
| 201 |         }
 | 
|---|
| 202 |       }
 | 
|---|
| 203 |     }
 | 
|---|
| 204 |   }
 | 
|---|
| 205 | }
 | 
|---|