source: src/UIElements/Views/Qt4/QtGeometryList.cpp@ 4c6f0d

Action_Thermostats Adding_MD_integration_tests Adding_StructOpt_integration_tests AutomationFragmentation_failures Candidate_v1.6.1 ChemicalSpaceEvaluator Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Exclude_Hydrogens_annealWithBondGraph Fix_Verbose_Codepatterns ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion Gui_displays_atomic_force_velocity JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool PythonUI_with_named_parameters Recreated_GuiChecks StoppableMakroAction TremoloParser_IncreasedPrecision
Last change on this file since 4c6f0d was 7516f6, checked in by Frederik Heber <frederik.heber@…>, 8 years ago

FIX: several Qt...Lists required mutex to control refill triggered by observable update.

  • Property mode set to 100644
File size: 6.0 KB
Line 
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
49using namespace std;
50
51const int QtGeometryList::COLUMNCOUNT = COLUMNTYPES_MAX;
52const char *QtGeometryList::COLUMNNAMES[QtGeometryList::COLUMNCOUNT]={"Name","Vector"};
53
54QtGeometryList::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
79QtGeometryList::~QtGeometryList()
80{
81 GeometryRegistry::getInstance().signOff(this);
82 GeometryRegistry::getInstance().signOff(this, GeometryRegistry::GeometryInserted);
83 GeometryRegistry::getInstance().signOff(this, GeometryRegistry::GeometryRemoved);
84}
85
86void QtGeometryList::update(Observable *publisher) {}
87
88void 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
109void QtGeometryList::refill(::GeometryObject *ignore)
110{
111 boost::recursive_mutex::scoped_lock lock(refill_mutex);
112
113 clear();
114
115 GeometryRegistry &reg = GeometryRegistry::getInstance();
116
117 GeometryRegistry::const_iterator iter;
118 for (iter = reg.getBeginIter(); iter != reg.getEndIter(); iter ++){
119 ::GeometryObject *v = iter->second;
120 if (v == ignore)
121 continue;
122
123 QTreeWidgetItem *treeItem = new QTreeWidgetItem(this);
124 treeItem->setText(NAME, QString(v->getName().c_str()));
125 treeItem->setText(VECTOR, QString(toString(v->getVector()).c_str()));
126 }
127}
128
129void QtGeometryList::paintEvent(QPaintEvent * event)
130{
131 boost::recursive_mutex::scoped_lock lock(refill_mutex);
132 /*if (dirty)
133 refill(NULL);*/
134 QTreeWidget::paintEvent(event);
135}
136
137void QtGeometryList::subjectKilled(Observable *publisher) {
138}
139
140bool QtGeometryList::eventFilter(QObject* object, QEvent* event)
141{
142 if(event->type() == QEvent::MouseMove)
143 {
144 mouseMoveFunction(static_cast<QMouseEvent*>(event));
145 }
146 // Repeat for other mouse events
147
148 // 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
149 return false;
150}
151
152void QtGeometryList::mouseMoveFunction(QMouseEvent * event)
153{
154 boost::recursive_mutex::scoped_lock lock(refill_mutex);
155
156 if (event->type() == QEvent::MouseMove) {
157 QTreeWidgetItem* current = itemAt(event->pos());
158 if (current == NULL)
159 return;
160 // reset all tooltips
161 bool NoneSelected = true;
162 for (int i=0;i<topLevelItemCount();i++){
163 QTreeWidgetItem *item = topLevelItem(i);
164 item->setToolTip(NAME, tr(""));
165 item->setToolTip(VECTOR, tr(""));
166 NoneSelected &= !item->isSelected();
167 }
168 const std::string name = current->text(NAME).toStdString();
169 GeometryObject *v = GeometryRegistry::getInstance().getByName(name);
170 const Vector currentVec = v->getVector();
171 if ((NoneSelected) || (current->isSelected())) {
172 // show norm
173 std::string tooltiptext("Length:");
174 tooltiptext += toString<double>(currentVec.Norm());
175 tooltiptext += std::string(" Angström");
176 current->setToolTip(NAME, trUtf8(tooltiptext.c_str()));
177 current->setToolTip(VECTOR, trUtf8(tooltiptext.c_str()));
178 } else {
179 for (int i=0;i<topLevelItemCount();i++){
180 QTreeWidgetItem *item = topLevelItem(i);
181 if (item->isSelected()) {
182 // show angle
183 const std::string othername = item->text(NAME).toStdString();
184 GeometryObject *otherv = GeometryRegistry::getInstance().getByName(othername);
185 const Vector otherVec = otherv->getVector();
186 std::string tooltiptext("Angle:");
187 tooltiptext += toString<double>(currentVec.Angle(otherVec)*180/M_PI);
188 tooltiptext += std::string(" deg");
189 current->setToolTip(NAME, trUtf8(tooltiptext.c_str()));
190 current->setToolTip(VECTOR, trUtf8(tooltiptext.c_str()));
191 break;
192 }
193 }
194 }
195 }
196}
Note: See TracBrowser for help on using the repository browser.