1 | /*
|
---|
2 | * Project: MoleCuilder
|
---|
3 | * Description: creates and alters molecular systems
|
---|
4 | * Copyright (C) 2015 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 | * QtMoleculeItem.cpp
|
---|
25 | *
|
---|
26 | * Created on: Jan 17, 2015
|
---|
27 | * Author: heber
|
---|
28 | */
|
---|
29 |
|
---|
30 | // include config.h
|
---|
31 | #ifdef HAVE_CONFIG_H
|
---|
32 | #include <config.h>
|
---|
33 | #endif
|
---|
34 |
|
---|
35 | #include "QtMoleculeItem.hpp"
|
---|
36 |
|
---|
37 | #include <QModelIndex>
|
---|
38 |
|
---|
39 | #include "UIElements/Views/Qt4/MoleculeList/QtMoleculeList.hpp"
|
---|
40 |
|
---|
41 | #include "CodePatterns/MemDebug.hpp"
|
---|
42 |
|
---|
43 | #include "CodePatterns/Assert.hpp"
|
---|
44 | #include "CodePatterns/toString.hpp"
|
---|
45 |
|
---|
46 | #include "CodePatterns/Observer/Notification.hpp"
|
---|
47 |
|
---|
48 | #include <algorithm>
|
---|
49 |
|
---|
50 | #include <boost/thread/locks.hpp>
|
---|
51 |
|
---|
52 | #include "Descriptors/MoleculeIdDescriptor.hpp"
|
---|
53 | #include "molecule.hpp"
|
---|
54 | #include "World.hpp"
|
---|
55 |
|
---|
56 | QtMoleculeItem::QtMoleculeItem(
|
---|
57 | const moleculeId_t _molid,
|
---|
58 | const channellist_t &_channellist,
|
---|
59 | const enum MoveTypes _movetype,
|
---|
60 | const emitDirtyState_t _emitDirtyState,
|
---|
61 | const emitSubjectKilledState_t _emitSubjectKilledState) :
|
---|
62 | Observer("QtMoleculeItem"),
|
---|
63 | molid(_molid),
|
---|
64 | movetype(_movetype),
|
---|
65 | channellist(_channellist),
|
---|
66 | owner(NULL),
|
---|
67 | dirty(true),
|
---|
68 | emitDirtyState(_emitDirtyState),
|
---|
69 | emitSubjectKilledState(_emitSubjectKilledState)
|
---|
70 | {
|
---|
71 | // BUG: We cannot sign on here as then and again we catch "pure virtual function called"
|
---|
72 | // this makes no sense as both the Observer part (which also has a recievedNotification())
|
---|
73 | // nor QtMoleculeItem's vtable have been fully created.
|
---|
74 | // signOnToMolecule();
|
---|
75 | setFlags(flags() | Qt::ItemIsSelectable);
|
---|
76 | }
|
---|
77 |
|
---|
78 | void QtMoleculeItem::signOnToMolecule()
|
---|
79 | {
|
---|
80 | // count the number of threads entering the function
|
---|
81 | std::cout << "QtMoleculeItem::signOnToMolecule() called for " << this << std::endl;
|
---|
82 | const molecule * const mol = const_cast<const World &>(World::getInstance()).
|
---|
83 | getMolecule(MoleculeById(molid));
|
---|
84 | if (mol != NULL) {
|
---|
85 | owner_mutex.lock();
|
---|
86 | if (owner == NULL) {
|
---|
87 | owner_mutex.unlock();
|
---|
88 | const Observable *owner_copy = static_cast<const Observable *>(mol);
|
---|
89 | for (channellist_t::const_iterator channeliter = channellist.begin();
|
---|
90 | channeliter != channellist.end(); ++channeliter)
|
---|
91 | owner_copy->signOn(this, *channeliter);
|
---|
92 | owner_mutex.lock();
|
---|
93 | owner = owner_copy;
|
---|
94 | owner_mutex.unlock();
|
---|
95 | std::cout << "QtMoleculeItem " << this << " has signed on to " << owner << std::endl;
|
---|
96 | } else
|
---|
97 | owner_mutex.unlock();
|
---|
98 | }
|
---|
99 | }
|
---|
100 |
|
---|
101 | void QtMoleculeItem::signOffFromMolecule()
|
---|
102 | {
|
---|
103 | // prevent via owner that another thread enters the function again
|
---|
104 | // (re-entrancy)
|
---|
105 | const Observable *owner_copy = NULL;
|
---|
106 | {
|
---|
107 | boost::recursive_mutex::scoped_lock lock(owner_mutex);
|
---|
108 | if (owner != NULL) {
|
---|
109 | owner_copy = owner;
|
---|
110 | owner = NULL;
|
---|
111 | }
|
---|
112 | }
|
---|
113 | std::cout << "QtMoleculeItem::signOffFromMolecule() called for " << this << std::endl;
|
---|
114 | if (owner_copy != NULL) {
|
---|
115 | for (channellist_t::const_iterator channeliter = channellist.begin();
|
---|
116 | channeliter != channellist.end(); ++channeliter)
|
---|
117 | owner_copy->signOff(this, *channeliter);
|
---|
118 | std::cout << "QtMoleculeItem " << this << " has signed off from " << owner_copy << std::endl;
|
---|
119 | }
|
---|
120 | }
|
---|
121 |
|
---|
122 | QtMoleculeItem::~QtMoleculeItem()
|
---|
123 | {
|
---|
124 | // BUG: We cannot sign off here as then and again we catch "pure virtual function called"
|
---|
125 | // this makes no sense as neither the Observer part (which also has a recievedNotification())
|
---|
126 | // nor QtMoleculeItem's vtable have yet been destroyed.
|
---|
127 | // signOffFromMolecule();
|
---|
128 | }
|
---|
129 |
|
---|
130 | void QtMoleculeItem::updateState()
|
---|
131 | {
|
---|
132 | if (dirty) {
|
---|
133 | internal_updateState();
|
---|
134 | dirty = false;
|
---|
135 | }
|
---|
136 | }
|
---|
137 |
|
---|
138 | void QtMoleculeItem::update(Observable *publisher)
|
---|
139 | {}
|
---|
140 |
|
---|
141 | void QtMoleculeItem::recieveNotification(Observable *publisher, Notification_ptr notification)
|
---|
142 | {
|
---|
143 | if (dynamic_cast<molecule *>(publisher) != NULL) {
|
---|
144 | if (notification->getChannelNo() == molecule::AboutToBeRemoved) {
|
---|
145 | signOffFromMolecule();
|
---|
146 | // prevent any remaining updates from accessing the molecule
|
---|
147 | //molid = -1;
|
---|
148 | dirty = false;
|
---|
149 | } else {
|
---|
150 | channellist_t::const_iterator iter =
|
---|
151 | std::find(channellist.begin(), channellist.end(), notification->getChannelNo());
|
---|
152 | if (iter != channellist.end()) {
|
---|
153 | dirty = true;
|
---|
154 | emitDirtyState(getMoleculeId(), getType(), movetype);
|
---|
155 | } else {
|
---|
156 | ASSERT(0,
|
---|
157 | "QtMoleculeItem::recieveNotification() - received notification to channel "
|
---|
158 | +toString(notification->getChannelNo())+" we are not subscribed to.");
|
---|
159 | }
|
---|
160 | }
|
---|
161 | }
|
---|
162 | }
|
---|
163 |
|
---|
164 | void QtMoleculeItem::subjectKilled(Observable *publisher)
|
---|
165 | {
|
---|
166 | signOffFromMolecule();
|
---|
167 | dirty = false;
|
---|
168 | emitSubjectKilledState(getMoleculeId());
|
---|
169 | }
|
---|
170 |
|
---|
171 | const molecule * const QtMoleculeItem::getMolecule() const
|
---|
172 | {
|
---|
173 | const molecule * const mol = const_cast<const World &>(World::getInstance()).getMolecule(MoleculeById(molid));
|
---|
174 | // ASSERT (mol != NULL,
|
---|
175 | // "QtMoleculeItem::getMolecule() - mol with "+toString(molid)+" is gone.");
|
---|
176 | return mol;
|
---|
177 | }
|
---|