[15c8a9] | 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 | /*
|
---|
[2f7988] | 24 | * QtObservedInstanceBoard.cpp
|
---|
[15c8a9] | 25 | *
|
---|
| 26 | * Created on: Oct 17, 2015
|
---|
| 27 | * Author: heber
|
---|
| 28 | */
|
---|
| 29 |
|
---|
| 30 |
|
---|
| 31 | // include config.h
|
---|
| 32 | #ifdef HAVE_CONFIG_H
|
---|
| 33 | #include <config.h>
|
---|
| 34 | #endif
|
---|
| 35 |
|
---|
[2f7988] | 36 | #include "QtObservedInstanceBoard.hpp"
|
---|
[15c8a9] | 37 |
|
---|
[04c3a3] | 38 | #include <QtCore/QMetaType>
|
---|
| 39 |
|
---|
[65c323] | 40 | #include "UIElements/Qt4/InstanceBoard/QtObservedAtom.hpp"
|
---|
[494478] | 41 | #include "UIElements/Qt4/InstanceBoard/QtObservedMolecule.hpp"
|
---|
[15c8a9] | 42 |
|
---|
| 43 | #include "CodePatterns/MemDebug.hpp"
|
---|
| 44 |
|
---|
[68418e] | 45 | #include <boost/bind.hpp>
|
---|
| 46 |
|
---|
[15c8a9] | 47 | #include "CodePatterns/Log.hpp"
|
---|
| 48 |
|
---|
| 49 | #include "Atom/atom.hpp"
|
---|
| 50 | #include "Descriptors/AtomIdDescriptor.hpp"
|
---|
| 51 | #include "Descriptors/MoleculeIdDescriptor.hpp"
|
---|
| 52 | #include "molecule.hpp"
|
---|
[41e287] | 53 | #include "UIElements/Qt4/InstanceBoard/ObservedValuesContainer_impl.hpp"
|
---|
[15c8a9] | 54 | #include "World.hpp"
|
---|
| 55 |
|
---|
[2f7988] | 56 | QtObservedInstanceBoard::QtObservedInstanceBoard(QWidget * _parent) :
|
---|
[15c8a9] | 57 | QWidget(_parent),
|
---|
[2f7988] | 58 | Observer("QtObservedInstanceBoard"),
|
---|
[15c8a9] | 59 | WorldSignedOn(false),
|
---|
[68418e] | 60 | atomObservedValues(
|
---|
| 61 | "atom",
|
---|
| 62 | *this,
|
---|
| 63 | boost::bind(&QtObservedInstanceBoard::atomcountsubjectKilled, this, _1)),
|
---|
| 64 | moleculeObservedValues(
|
---|
| 65 | "molecule",
|
---|
| 66 | *this,
|
---|
| 67 | boost::bind(&QtObservedInstanceBoard::moleculecountsubjectKilled, this, _1)),
|
---|
| 68 | lastremovedatom((atomId_t)-1),
|
---|
| 69 | lastremovedmolecule((moleculeId_t)-1)
|
---|
| 70 | {
|
---|
[04c3a3] | 71 | qRegisterMetaType<QtObservedAtom::ptr>("QtObservedAtom::ptr");
|
---|
| 72 | qRegisterMetaType<QtObservedMolecule::ptr>("QtObservedMolecule::ptr");
|
---|
| 73 |
|
---|
[15c8a9] | 74 | // be first (besides ObservedValues to know about new insertions)
|
---|
| 75 | World::getInstance().signOn(this, World::AtomInserted, GlobalObservableInfo::PriorityLevel(int(-10)));
|
---|
| 76 | World::getInstance().signOn(this, World::AtomRemoved, GlobalObservableInfo::PriorityLevel(int(-10)));
|
---|
| 77 | World::getInstance().signOn(this, World::MoleculeInserted, GlobalObservableInfo::PriorityLevel(int(-10)));
|
---|
| 78 | World::getInstance().signOn(this, World::MoleculeRemoved, GlobalObservableInfo::PriorityLevel(int(-10)));
|
---|
| 79 | WorldSignedOn = true;
|
---|
| 80 | }
|
---|
| 81 |
|
---|
[2f7988] | 82 | QtObservedInstanceBoard::~QtObservedInstanceBoard()
|
---|
[15c8a9] | 83 | {
|
---|
| 84 | if (WorldSignedOn) {
|
---|
| 85 | World::getInstance().signOff(this, World::AtomInserted);
|
---|
| 86 | World::getInstance().signOff(this, World::AtomRemoved);
|
---|
| 87 | World::getInstance().signOff(this, World::MoleculeInserted);
|
---|
| 88 | World::getInstance().signOff(this, World::MoleculeRemoved);
|
---|
| 89 | }
|
---|
| 90 | // sign off from all remaining molecules and atoms
|
---|
| 91 | for (SignedOn_t::iterator iter = AtomSignedOn.begin(); !AtomSignedOn.empty();
|
---|
| 92 | iter = AtomSignedOn.begin()) {
|
---|
| 93 | (*iter)->signOff(this, atom::IndexChanged);
|
---|
| 94 | AtomSignedOn.erase(iter);
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | for (SignedOn_t::iterator iter = MoleculeSignedOn.begin(); !MoleculeSignedOn.empty();
|
---|
| 98 | iter = MoleculeSignedOn.begin()) {
|
---|
[62a0ee] | 99 | Observable * const _observable = *iter;
|
---|
| 100 | _observable->signOff(this, molecule::IndexChanged);
|
---|
| 101 | // remove all three remaining instances in multiset
|
---|
| 102 | for (SignedOn_t::iterator sameiter = MoleculeSignedOn.find(_observable);
|
---|
| 103 | sameiter != MoleculeSignedOn.end();
|
---|
| 104 | sameiter = MoleculeSignedOn.find(_observable))
|
---|
| 105 | MoleculeSignedOn.erase(*sameiter); //erase all instances
|
---|
[15c8a9] | 106 | }
|
---|
| 107 | }
|
---|
| 108 |
|
---|
[2f7988] | 109 | void QtObservedInstanceBoard::update(Observable *publisher)
|
---|
[15c8a9] | 110 | {
|
---|
| 111 | ASSERT(0,
|
---|
[2f7988] | 112 | "QtObservedInstanceBoard::update() - we are not signed on to general updates.");
|
---|
[15c8a9] | 113 | }
|
---|
| 114 |
|
---|
[2f7988] | 115 | void QtObservedInstanceBoard::subjectKilled(Observable *publisher)
|
---|
[15c8a9] | 116 | {
|
---|
| 117 | SignedOn_t::iterator iter = AtomSignedOn.find(publisher);
|
---|
| 118 | if ( iter != AtomSignedOn.end()) {
|
---|
| 119 | LOG(3, "DEBUG: InstanceBoard got subjectKilled() from atom " << publisher);
|
---|
| 120 | AtomSignedOn.erase(iter);
|
---|
| 121 | } else {
|
---|
| 122 | iter = MoleculeSignedOn.find(publisher);
|
---|
| 123 | if ( iter != MoleculeSignedOn.end()) {
|
---|
| 124 | LOG(3, "DEBUG: InstanceBoard got subjectKilled() from molecule " << publisher);
|
---|
| 125 | MoleculeSignedOn.erase(iter);
|
---|
| 126 | } else {
|
---|
| 127 | ASSERT(0,
|
---|
| 128 | "QtObservedInstanceBoard::subjectKilled() - could not find signedOn for atom/molecule "+toString(publisher));
|
---|
| 129 | }
|
---|
| 130 | }
|
---|
| 131 | }
|
---|
| 132 |
|
---|
[2f7988] | 133 | void QtObservedInstanceBoard::recieveNotification(Observable *publisher, Notification_ptr notification)
|
---|
[15c8a9] | 134 | {
|
---|
| 135 | if (static_cast<World *>(publisher) == World::getPointer()) {
|
---|
| 136 | switch (notification->getChannelNo()) {
|
---|
| 137 | case World::MoleculeInserted:
|
---|
| 138 | {
|
---|
| 139 | const moleculeId_t _id = const_cast<const World &>(World::getInstance()).lastChangedMolId();
|
---|
| 140 | #ifdef LOG_OBSERVER
|
---|
| 141 | observerLog().addMessage() << "++ Observer " << observerLog().getName(static_cast<Observer *>(this)) << " received notification that molecule "+toString(_id)+" has been inserted.";
|
---|
| 142 | #endif
|
---|
| 143 | LOG(3, "DEBUG: InformationBoard got moleculeInserted signal for molecule " << _id);
|
---|
| 144 | const molecule * const _molecule = const_cast<const World &>(World::getInstance()).
|
---|
| 145 | getMolecule(MoleculeById(_id));
|
---|
| 146 | if (_molecule != NULL) {
|
---|
[62a0ee] | 147 | LOG(3, "DEBUG: InformationBoard initializes QtObservedMolecule for " << _id);
|
---|
| 148 | QtObservedMolecule::ptr observedmolecule(
|
---|
| 149 | new QtObservedMolecule(
|
---|
| 150 | _id,
|
---|
| 151 | _molecule,
|
---|
| 152 | *this));
|
---|
[0af22d] | 153 | moleculeObservedValues.insert(_id, observedmolecule);
|
---|
[15c8a9] | 154 | // we need to check for index changes
|
---|
| 155 | LOG(3, "DEBUG: InformationBoard signOn()s to molecule " << _id);
|
---|
| 156 | _molecule->signOn(this, molecule::IndexChanged);
|
---|
| 157 | MoleculeSignedOn.insert( static_cast<Observable *>(const_cast<molecule *>(_molecule)) );
|
---|
| 158 |
|
---|
[1b07b1] | 159 | emit moleculeInserted(observedmolecule);
|
---|
[15c8a9] | 160 | } else {
|
---|
[2f7988] | 161 | ELOG(1, "QtObservedInstanceBoard got MoleculeInserted for unknown molecule id " << _id);
|
---|
[15c8a9] | 162 | }
|
---|
| 163 | break;
|
---|
| 164 | }
|
---|
| 165 | case World::MoleculeRemoved:
|
---|
| 166 | {
|
---|
| 167 | const moleculeId_t _id = const_cast<const World &>(World::getInstance()).lastChangedMolId();
|
---|
| 168 | LOG(3, "DEBUG: InformationBoard got MoleculeRemoved signal for molecule " << _id);
|
---|
| 169 | // note down such that ObservedValues are simply dropped
|
---|
| 170 | lastremovedmolecule = _id;
|
---|
| 171 | break;
|
---|
| 172 | }
|
---|
| 173 | case World::AtomInserted:
|
---|
| 174 | {
|
---|
| 175 | const atomId_t _id = const_cast<const World &>(World::getInstance()).lastChangedAtomId();
|
---|
| 176 | #ifdef LOG_OBSERVER
|
---|
| 177 | observerLog().addMessage() << "++ Observer " << observerLog().getName(static_cast<Observer *>(this)) << " received notification that atom "+toString(_id)+" has been inserted.";
|
---|
| 178 | #endif
|
---|
| 179 | LOG(3, "DEBUG: InformationBoard got atomInserted signal for atom " << _id);
|
---|
| 180 | const atom * const _atom = const_cast<const World &>(World::getInstance()).
|
---|
| 181 | getAtom(AtomById(_id));
|
---|
| 182 | if (_atom!= NULL) {
|
---|
[62a0ee] | 183 | LOG(3, "DEBUG: InformationBoard initializes QtObservedAtom for " << _id);
|
---|
| 184 | QtObservedAtom::ptr observedatom(
|
---|
| 185 | new QtObservedAtom(_id, _atom, *this));
|
---|
[0af22d] | 186 | atomObservedValues.insert(_id, observedatom);
|
---|
[15c8a9] | 187 | // we need to check for index changes
|
---|
| 188 | LOG(3, "DEBUG: InformationBoard signOn()s to atom " << _id);
|
---|
| 189 | _atom->signOn(this, atom::IndexChanged);
|
---|
| 190 | AtomSignedOn.insert( static_cast<Observable *>(const_cast<atom *>(_atom)) );
|
---|
[1b07b1] | 191 | emit atomInserted(observedatom);
|
---|
[15c8a9] | 192 | } else {
|
---|
[2f7988] | 193 | ELOG(1, "QtObservedInstanceBoard got AtomInserted for unknown atom id " << _id);
|
---|
[15c8a9] | 194 | }
|
---|
| 195 | break;
|
---|
| 196 | }
|
---|
| 197 | case World::AtomRemoved:
|
---|
| 198 | {
|
---|
| 199 | const atomId_t _id = const_cast<const World &>(World::getInstance()).lastChangedAtomId();
|
---|
| 200 | LOG(3, "DEBUG: InformationBoard got AtomRemoved signal for atom " << _id);
|
---|
| 201 | // note down such that ObservedValues are simply dropped
|
---|
| 202 | lastremovedatom = _id;
|
---|
| 203 | break;
|
---|
| 204 | }
|
---|
| 205 | default:
|
---|
[2f7988] | 206 | ASSERT(0, "QtObservedInstanceBoard::recieveNotification() - we cannot get here for World.");
|
---|
[15c8a9] | 207 | break;
|
---|
| 208 | }
|
---|
| 209 | } else if (dynamic_cast<molecule *>(publisher) != NULL) {
|
---|
| 210 | const moleculeId_t molid = const_cast<const World &>(World::getInstance()).lastChangedMolId();
|
---|
| 211 | switch (notification->getChannelNo()) {
|
---|
| 212 | case molecule::IndexChanged:
|
---|
| 213 | {
|
---|
| 214 | // molecule has changed its index
|
---|
| 215 | const moleculeId_t newmoleculeId = dynamic_cast<molecule *>(publisher)->getId();
|
---|
| 216 | LOG(3, "DEBUG: InformationBoard got IndexChanged from molecule " << molid << " to " << newmoleculeId);
|
---|
[41e287] | 217 | #ifndef NDEBUG
|
---|
| 218 | bool status =
|
---|
| 219 | #endif
|
---|
| 220 | moleculeObservedValues.changeIdentifier(molid, newmoleculeId);
|
---|
| 221 | ASSERT( status,
|
---|
| 222 | "QtObservedInstanceBoard::recieveNotification() - cannot change molecule's id "
|
---|
| 223 | +toString(molid)+" "+toString(newmoleculeId)+" in moleculeObservedValues.");
|
---|
[15c8a9] | 224 | // no need update SignedOn, ref does not change
|
---|
| 225 | emit moleculeIndexChanged(molid, newmoleculeId);
|
---|
| 226 | break;
|
---|
| 227 | }
|
---|
| 228 | default:
|
---|
[2f7988] | 229 | ASSERT(0, "QtObservedInstanceBoard::recieveNotification() - we cannot get here.");
|
---|
[15c8a9] | 230 | break;
|
---|
| 231 | }
|
---|
| 232 | } else if (dynamic_cast<atom *>(publisher) != NULL) {
|
---|
| 233 | const atomId_t oldatomId = const_cast<const World &>(World::getInstance()).lastChangedAtomId();
|
---|
| 234 | switch (notification->getChannelNo()) {
|
---|
| 235 | case AtomObservable::IndexChanged:
|
---|
| 236 | {
|
---|
| 237 | const atomId_t newatomId = dynamic_cast<atom *>(publisher)->getId();
|
---|
| 238 | LOG(3, "DEBUG: InformationBoard got IndexChanged from atom " << oldatomId << " to " << newatomId);
|
---|
[6d4925] | 239 | #ifndef NDEBUG
|
---|
| 240 | bool status =
|
---|
| 241 | #endif
|
---|
[41e287] | 242 | atomObservedValues.changeIdentifier(oldatomId, newatomId);
|
---|
[6d4925] | 243 | ASSERT( status,
|
---|
| 244 | "QtObservedInstanceBoard::recieveNotification() - cannot change atom's id "
|
---|
| 245 | +toString(oldatomId)+" "+toString(newatomId)+" in atomObservedValues.");
|
---|
[15c8a9] | 246 | // no need update SignedOn, ref does not change
|
---|
| 247 | emit atomIndexChanged(oldatomId, newatomId);
|
---|
| 248 | break;
|
---|
| 249 | }
|
---|
| 250 | default:
|
---|
[2f7988] | 251 | ASSERT(0, "QtObservedInstanceBoard::recieveNotification() - we cannot get here.");
|
---|
[15c8a9] | 252 | break;
|
---|
| 253 | }
|
---|
| 254 | } else {
|
---|
[2f7988] | 255 | ASSERT(0, "QtObservedInstanceBoard::recieveNotification() - notification from unknown source.");
|
---|
[15c8a9] | 256 | }
|
---|
| 257 | }
|
---|
| 258 |
|
---|
[2f7988] | 259 | void QtObservedInstanceBoard::atomcountsubjectKilled(const atomId_t _atomid)
|
---|
[15c8a9] | 260 | {
|
---|
[1b07b1] | 261 | if ((_atomid == lastremovedatom)) {
|
---|
[68418e] | 262 | LOG(3, "DEBUG: InstanceBoard emits atomRemoved for " << _atomid);
|
---|
[1b07b1] | 263 | emit atomRemoved(lastremovedatom);
|
---|
[68418e] | 264 | } else
|
---|
[90821d] | 265 | ELOG(2, "QtObservedInstanceBoard::atomcountsubjectKilled() - id " << _atomid
|
---|
[1b07b1] | 266 | << " not fitting with " << lastremovedatom);
|
---|
[15c8a9] | 267 | }
|
---|
| 268 |
|
---|
[2f7988] | 269 | void QtObservedInstanceBoard::moleculecountsubjectKilled(const moleculeId_t _molid)
|
---|
[15c8a9] | 270 | {
|
---|
[68418e] | 271 | if (lastremovedmolecule == _molid) {
|
---|
| 272 | LOG(3, "DEBUG: InstanceBoard emits moleculeRemoved for " << _molid);
|
---|
| 273 | emit moleculeRemoved(_molid);
|
---|
| 274 | } else
|
---|
| 275 | ELOG(2, "QtObservedInstanceBoard::moleculecountsubjectKilled() - id " << _molid
|
---|
| 276 | << " not fitting with " << lastremovedmolecule);
|
---|
[15c8a9] | 277 | }
|
---|
| 278 |
|
---|
[41e287] | 279 | QtObservedAtom::ptr QtObservedInstanceBoard::getObservedAtom(const atomId_t _id)
|
---|
[15c8a9] | 280 | {
|
---|
[41e287] | 281 | return atomObservedValues.get(_id);
|
---|
[15c8a9] | 282 | }
|
---|
| 283 |
|
---|
[41e287] | 284 | QtObservedMolecule::ptr QtObservedInstanceBoard::getObservedMolecule(const moleculeId_t _id)
|
---|
[15c8a9] | 285 | {
|
---|
[41e287] | 286 | return moleculeObservedValues.get(_id);
|
---|
[15c8a9] | 287 | }
|
---|
| 288 |
|
---|
[68418e] | 289 | void QtObservedInstanceBoard::markObservedAtomAsConnected(const atomId_t _id)
|
---|
[15c8a9] | 290 | {
|
---|
[68418e] | 291 | atomObservedValues.markObservedValuesAsConnected(_id);
|
---|
[15c8a9] | 292 | }
|
---|
| 293 |
|
---|
[68418e] | 294 | void QtObservedInstanceBoard::markObservedAtomAsDisconnected(const atomId_t _id)
|
---|
[15c8a9] | 295 | {
|
---|
[68418e] | 296 | atomObservedValues.markObservedValuesAsDisconnected(_id);
|
---|
[15c8a9] | 297 | }
|
---|
[98c35c] | 298 |
|
---|
[4e6ffe] | 299 | void QtObservedInstanceBoard::markObservedAtomForErase(const atomId_t _id)
|
---|
| 300 | {
|
---|
| 301 | atomObservedValues.eraseObservedValues(_id);
|
---|
| 302 | }
|
---|
| 303 |
|
---|
[68418e] | 304 | void QtObservedInstanceBoard::markObservedMoleculeAsConnected(const moleculeId_t _id)
|
---|
[98c35c] | 305 | {
|
---|
[68418e] | 306 | moleculeObservedValues.markObservedValuesAsConnected(_id);
|
---|
[98c35c] | 307 | }
|
---|
| 308 |
|
---|
[68418e] | 309 | void QtObservedInstanceBoard::markObservedMoleculeAsDisconnected(const moleculeId_t _id)
|
---|
[98c35c] | 310 | {
|
---|
[68418e] | 311 | moleculeObservedValues.markObservedValuesAsDisconnected(_id);
|
---|
[98c35c] | 312 | }
|
---|
[4e6ffe] | 313 |
|
---|
| 314 | void QtObservedInstanceBoard::markObservedMoleculeForErase(const moleculeId_t _id)
|
---|
| 315 | {
|
---|
| 316 | moleculeObservedValues.eraseObservedValues(_id);
|
---|
| 317 | }
|
---|