| 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 | {
|
|---|
| 58 | setColumnCount(COLUMNCOUNT);
|
|---|
| 59 | setSelectionMode(QAbstractItemView::SingleSelection);
|
|---|
| 60 | setSortingEnabled(true);
|
|---|
| 61 |
|
|---|
| 62 | QStringList header;
|
|---|
| 63 | for(int i=0; i<COLUMNCOUNT;++i)
|
|---|
| 64 | header << COLUMNNAMES[i];
|
|---|
| 65 | setHeaderLabels(header);
|
|---|
| 66 | sortByColumn(0);
|
|---|
| 67 |
|
|---|
| 68 | setStyleSheet("QTreeView::item:hover{background-color:#999966;}");
|
|---|
| 69 |
|
|---|
| 70 | refill(NULL);
|
|---|
| 71 |
|
|---|
| 72 | GeometryRegistry::getInstance().signOn(this);
|
|---|
| 73 | GeometryRegistry::getInstance().signOn(this, GeometryRegistry::GeometryInserted);
|
|---|
| 74 | GeometryRegistry::getInstance().signOn(this, GeometryRegistry::GeometryRemoved);
|
|---|
| 75 |
|
|---|
| 76 | QCoreApplication::instance()->installEventFilter(this);
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | QtGeometryList::~QtGeometryList()
|
|---|
| 80 | {
|
|---|
| 81 | GeometryRegistry::getInstance().signOff(this);
|
|---|
| 82 | GeometryRegistry::getInstance().signOff(this, GeometryRegistry::GeometryInserted);
|
|---|
| 83 | GeometryRegistry::getInstance().signOff(this, GeometryRegistry::GeometryRemoved);
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | void QtGeometryList::update(Observable *publisher) {}
|
|---|
| 87 |
|
|---|
| 88 | void QtGeometryList::recieveNotification(Observable *publisher, Notification_ptr notification)
|
|---|
| 89 | {
|
|---|
| 90 | if (static_cast<GeometryRegistry*>(publisher) == GeometryRegistry::getPointer()) {
|
|---|
| 91 | switch (notification->getChannelNo()) {
|
|---|
| 92 | case GeometryRegistry::GeometryInserted:
|
|---|
| 93 | {
|
|---|
| 94 | refill(NULL);
|
|---|
| 95 | break;
|
|---|
| 96 | }
|
|---|
| 97 | case GeometryRegistry::GeometryRemoved:
|
|---|
| 98 | {
|
|---|
| 99 | refill(GeometryRegistry::getInstance().lastChanged());
|
|---|
| 100 | break;
|
|---|
| 101 | }
|
|---|
| 102 | default:
|
|---|
| 103 | ASSERT(0, "QtGeometryList::recieveNotification() - we cannot get here.");
|
|---|
| 104 | break;
|
|---|
| 105 | }
|
|---|
| 106 | }
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | void QtGeometryList::refill(::GeometryObject *ignore)
|
|---|
| 110 | {
|
|---|
| 111 | clear();
|
|---|
| 112 |
|
|---|
| 113 | GeometryRegistry ® = GeometryRegistry::getInstance();
|
|---|
| 114 |
|
|---|
| 115 | GeometryRegistry::const_iterator iter;
|
|---|
| 116 | for (iter = reg.getBeginIter(); iter != reg.getEndIter(); iter ++){
|
|---|
| 117 | ::GeometryObject *v = iter->second;
|
|---|
| 118 | if (v == ignore)
|
|---|
| 119 | continue;
|
|---|
| 120 |
|
|---|
| 121 | QTreeWidgetItem *treeItem = new QTreeWidgetItem(this);
|
|---|
| 122 | treeItem->setText(NAME, QString(v->getName().c_str()));
|
|---|
| 123 | treeItem->setText(VECTOR, QString(toString(v->getVector()).c_str()));
|
|---|
| 124 | }
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | #if 0
|
|---|
| 128 | void QtGeometryList::paintEvent(QPaintEvent * event)
|
|---|
| 129 | {
|
|---|
| 130 | /*if (dirty)
|
|---|
| 131 | refill(NULL);*/
|
|---|
| 132 | QtGeometryList::paintEvent(event);
|
|---|
| 133 | }
|
|---|
| 134 | #endif
|
|---|
| 135 |
|
|---|
| 136 | void QtGeometryList::subjectKilled(Observable *publisher) {
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 | bool QtGeometryList::eventFilter(QObject* object, QEvent* event)
|
|---|
| 140 | {
|
|---|
| 141 | if(event->type() == QEvent::MouseMove)
|
|---|
| 142 | {
|
|---|
| 143 | mouseMoveFunction(static_cast<QMouseEvent*>(event));
|
|---|
| 144 | }
|
|---|
| 145 | // Repeat for other mouse events
|
|---|
| 146 |
|
|---|
| 147 | // 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
|
|---|
| 148 | return false;
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 | void QtGeometryList::mouseMoveFunction(QMouseEvent * event)
|
|---|
| 152 | {
|
|---|
| 153 | if (event->type() == QEvent::MouseMove) {
|
|---|
| 154 | QTreeWidgetItem* current = itemAt(event->pos());
|
|---|
| 155 | if (current == NULL)
|
|---|
| 156 | return;
|
|---|
| 157 | // reset all tooltips
|
|---|
| 158 | bool NoneSelected = true;
|
|---|
| 159 | for (int i=0;i<topLevelItemCount();i++){
|
|---|
| 160 | QTreeWidgetItem *item = topLevelItem(i);
|
|---|
| 161 | item->setToolTip(NAME, tr(""));
|
|---|
| 162 | item->setToolTip(VECTOR, tr(""));
|
|---|
| 163 | NoneSelected &= !item->isSelected();
|
|---|
| 164 | }
|
|---|
| 165 | const std::string name = current->text(NAME).toStdString();
|
|---|
| 166 | GeometryObject *v = GeometryRegistry::getInstance().getByName(name);
|
|---|
| 167 | const Vector currentVec = v->getVector();
|
|---|
| 168 | if ((NoneSelected) || (current->isSelected())) {
|
|---|
| 169 | // show norm
|
|---|
| 170 | std::string tooltiptext("Length:");
|
|---|
| 171 | tooltiptext += toString<double>(currentVec.Norm());
|
|---|
| 172 | tooltiptext += std::string(" Angström");
|
|---|
| 173 | current->setToolTip(NAME, trUtf8(tooltiptext.c_str()));
|
|---|
| 174 | current->setToolTip(VECTOR, trUtf8(tooltiptext.c_str()));
|
|---|
| 175 | } else {
|
|---|
| 176 | for (int i=0;i<topLevelItemCount();i++){
|
|---|
| 177 | QTreeWidgetItem *item = topLevelItem(i);
|
|---|
| 178 | if (item->isSelected()) {
|
|---|
| 179 | // show angle
|
|---|
| 180 | const std::string othername = item->text(NAME).toStdString();
|
|---|
| 181 | GeometryObject *otherv = GeometryRegistry::getInstance().getByName(othername);
|
|---|
| 182 | const Vector otherVec = otherv->getVector();
|
|---|
| 183 | std::string tooltiptext("Angle:");
|
|---|
| 184 | tooltiptext += toString<double>(currentVec.Angle(otherVec)*180/M_PI);
|
|---|
| 185 | tooltiptext += std::string(" deg");
|
|---|
| 186 | current->setToolTip(NAME, trUtf8(tooltiptext.c_str()));
|
|---|
| 187 | current->setToolTip(VECTOR, trUtf8(tooltiptext.c_str()));
|
|---|
| 188 | break;
|
|---|
| 189 | }
|
|---|
| 190 | }
|
|---|
| 191 | }
|
|---|
| 192 | }
|
|---|
| 193 | }
|
|---|