[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,
|
---|
[8b59dd] | 67 | boost::bind(&QtObservedInstanceBoard::moleculecountsubjectKilled, this, _1))
|
---|
[68418e] | 68 | {
|
---|
[04c3a3] | 69 | qRegisterMetaType<QtObservedAtom::ptr>("QtObservedAtom::ptr");
|
---|
| 70 | qRegisterMetaType<QtObservedMolecule::ptr>("QtObservedMolecule::ptr");
|
---|
| 71 |
|
---|
[15c8a9] | 72 | // be first (besides ObservedValues to know about new insertions)
|
---|
| 73 | World::getInstance().signOn(this, World::AtomInserted, GlobalObservableInfo::PriorityLevel(int(-10)));
|
---|
| 74 | World::getInstance().signOn(this, World::MoleculeInserted, GlobalObservableInfo::PriorityLevel(int(-10)));
|
---|
| 75 | WorldSignedOn = true;
|
---|
| 76 | }
|
---|
| 77 |
|
---|
[2f7988] | 78 | QtObservedInstanceBoard::~QtObservedInstanceBoard()
|
---|
[15c8a9] | 79 | {
|
---|
| 80 | if (WorldSignedOn) {
|
---|
| 81 | World::getInstance().signOff(this, World::AtomInserted);
|
---|
| 82 | World::getInstance().signOff(this, World::MoleculeInserted);
|
---|
| 83 | }
|
---|
| 84 | // sign off from all remaining molecules and atoms
|
---|
| 85 | for (SignedOn_t::iterator iter = AtomSignedOn.begin(); !AtomSignedOn.empty();
|
---|
| 86 | iter = AtomSignedOn.begin()) {
|
---|
| 87 | (*iter)->signOff(this, atom::IndexChanged);
|
---|
| 88 | AtomSignedOn.erase(iter);
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | for (SignedOn_t::iterator iter = MoleculeSignedOn.begin(); !MoleculeSignedOn.empty();
|
---|
| 92 | iter = MoleculeSignedOn.begin()) {
|
---|
[5c9604] | 93 | (*iter)->signOff(this, molecule::IndexChanged);
|
---|
| 94 | MoleculeSignedOn.erase(iter);
|
---|
[15c8a9] | 95 | }
|
---|
| 96 | }
|
---|
| 97 |
|
---|
[2f7988] | 98 | void QtObservedInstanceBoard::update(Observable *publisher)
|
---|
[15c8a9] | 99 | {
|
---|
| 100 | ASSERT(0,
|
---|
[2f7988] | 101 | "QtObservedInstanceBoard::update() - we are not signed on to general updates.");
|
---|
[15c8a9] | 102 | }
|
---|
| 103 |
|
---|
[2f7988] | 104 | void QtObservedInstanceBoard::subjectKilled(Observable *publisher)
|
---|
[15c8a9] | 105 | {
|
---|
| 106 | SignedOn_t::iterator iter = AtomSignedOn.find(publisher);
|
---|
| 107 | if ( iter != AtomSignedOn.end()) {
|
---|
| 108 | LOG(3, "DEBUG: InstanceBoard got subjectKilled() from atom " << publisher);
|
---|
| 109 | AtomSignedOn.erase(iter);
|
---|
| 110 | } else {
|
---|
| 111 | iter = MoleculeSignedOn.find(publisher);
|
---|
| 112 | if ( iter != MoleculeSignedOn.end()) {
|
---|
| 113 | LOG(3, "DEBUG: InstanceBoard got subjectKilled() from molecule " << publisher);
|
---|
| 114 | MoleculeSignedOn.erase(iter);
|
---|
| 115 | } else {
|
---|
| 116 | ASSERT(0,
|
---|
| 117 | "QtObservedInstanceBoard::subjectKilled() - could not find signedOn for atom/molecule "+toString(publisher));
|
---|
| 118 | }
|
---|
| 119 | }
|
---|
| 120 | }
|
---|
| 121 |
|
---|
[2f7988] | 122 | void QtObservedInstanceBoard::recieveNotification(Observable *publisher, Notification_ptr notification)
|
---|
[15c8a9] | 123 | {
|
---|
| 124 | if (static_cast<World *>(publisher) == World::getPointer()) {
|
---|
| 125 | switch (notification->getChannelNo()) {
|
---|
| 126 | case World::MoleculeInserted:
|
---|
| 127 | {
|
---|
| 128 | const moleculeId_t _id = const_cast<const World &>(World::getInstance()).lastChangedMolId();
|
---|
| 129 | #ifdef LOG_OBSERVER
|
---|
| 130 | observerLog().addMessage() << "++ Observer " << observerLog().getName(static_cast<Observer *>(this)) << " received notification that molecule "+toString(_id)+" has been inserted.";
|
---|
| 131 | #endif
|
---|
| 132 | LOG(3, "DEBUG: InformationBoard got moleculeInserted signal for molecule " << _id);
|
---|
| 133 | const molecule * const _molecule = const_cast<const World &>(World::getInstance()).
|
---|
| 134 | getMolecule(MoleculeById(_id));
|
---|
| 135 | if (_molecule != NULL) {
|
---|
[62a0ee] | 136 | LOG(3, "DEBUG: InformationBoard initializes QtObservedMolecule for " << _id);
|
---|
| 137 | QtObservedMolecule::ptr observedmolecule(
|
---|
| 138 | new QtObservedMolecule(
|
---|
| 139 | _id,
|
---|
| 140 | _molecule,
|
---|
| 141 | *this));
|
---|
[0af22d] | 142 | moleculeObservedValues.insert(_id, observedmolecule);
|
---|
[15c8a9] | 143 | // we need to check for index changes
|
---|
| 144 | LOG(3, "DEBUG: InformationBoard signOn()s to molecule " << _id);
|
---|
| 145 | _molecule->signOn(this, molecule::IndexChanged);
|
---|
| 146 | MoleculeSignedOn.insert( static_cast<Observable *>(const_cast<molecule *>(_molecule)) );
|
---|
| 147 |
|
---|
[1b07b1] | 148 | emit moleculeInserted(observedmolecule);
|
---|
[15c8a9] | 149 | } else {
|
---|
[2f7988] | 150 | ELOG(1, "QtObservedInstanceBoard got MoleculeInserted for unknown molecule id " << _id);
|
---|
[15c8a9] | 151 | }
|
---|
| 152 | break;
|
---|
| 153 | }
|
---|
| 154 | case World::AtomInserted:
|
---|
| 155 | {
|
---|
| 156 | const atomId_t _id = const_cast<const World &>(World::getInstance()).lastChangedAtomId();
|
---|
| 157 | #ifdef LOG_OBSERVER
|
---|
| 158 | observerLog().addMessage() << "++ Observer " << observerLog().getName(static_cast<Observer *>(this)) << " received notification that atom "+toString(_id)+" has been inserted.";
|
---|
| 159 | #endif
|
---|
| 160 | LOG(3, "DEBUG: InformationBoard got atomInserted signal for atom " << _id);
|
---|
| 161 | const atom * const _atom = const_cast<const World &>(World::getInstance()).
|
---|
| 162 | getAtom(AtomById(_id));
|
---|
| 163 | if (_atom!= NULL) {
|
---|
[62a0ee] | 164 | LOG(3, "DEBUG: InformationBoard initializes QtObservedAtom for " << _id);
|
---|
| 165 | QtObservedAtom::ptr observedatom(
|
---|
| 166 | new QtObservedAtom(_id, _atom, *this));
|
---|
[0af22d] | 167 | atomObservedValues.insert(_id, observedatom);
|
---|
[15c8a9] | 168 | // we need to check for index changes
|
---|
| 169 | LOG(3, "DEBUG: InformationBoard signOn()s to atom " << _id);
|
---|
| 170 | _atom->signOn(this, atom::IndexChanged);
|
---|
| 171 | AtomSignedOn.insert( static_cast<Observable *>(const_cast<atom *>(_atom)) );
|
---|
[1b07b1] | 172 | emit atomInserted(observedatom);
|
---|
[15c8a9] | 173 | } else {
|
---|
[2f7988] | 174 | ELOG(1, "QtObservedInstanceBoard got AtomInserted for unknown atom id " << _id);
|
---|
[15c8a9] | 175 | }
|
---|
| 176 | break;
|
---|
| 177 | }
|
---|
| 178 | default:
|
---|
[2f7988] | 179 | ASSERT(0, "QtObservedInstanceBoard::recieveNotification() - we cannot get here for World.");
|
---|
[15c8a9] | 180 | break;
|
---|
| 181 | }
|
---|
| 182 | } else if (dynamic_cast<molecule *>(publisher) != NULL) {
|
---|
| 183 | const moleculeId_t molid = const_cast<const World &>(World::getInstance()).lastChangedMolId();
|
---|
| 184 | switch (notification->getChannelNo()) {
|
---|
| 185 | case molecule::IndexChanged:
|
---|
| 186 | {
|
---|
| 187 | // molecule has changed its index
|
---|
| 188 | const moleculeId_t newmoleculeId = dynamic_cast<molecule *>(publisher)->getId();
|
---|
| 189 | LOG(3, "DEBUG: InformationBoard got IndexChanged from molecule " << molid << " to " << newmoleculeId);
|
---|
[41e287] | 190 | #ifndef NDEBUG
|
---|
| 191 | bool status =
|
---|
| 192 | #endif
|
---|
| 193 | moleculeObservedValues.changeIdentifier(molid, newmoleculeId);
|
---|
| 194 | ASSERT( status,
|
---|
| 195 | "QtObservedInstanceBoard::recieveNotification() - cannot change molecule's id "
|
---|
| 196 | +toString(molid)+" "+toString(newmoleculeId)+" in moleculeObservedValues.");
|
---|
[15c8a9] | 197 | // no need update SignedOn, ref does not change
|
---|
| 198 | emit moleculeIndexChanged(molid, newmoleculeId);
|
---|
| 199 | break;
|
---|
| 200 | }
|
---|
| 201 | default:
|
---|
[2f7988] | 202 | ASSERT(0, "QtObservedInstanceBoard::recieveNotification() - we cannot get here.");
|
---|
[15c8a9] | 203 | break;
|
---|
| 204 | }
|
---|
| 205 | } else if (dynamic_cast<atom *>(publisher) != NULL) {
|
---|
| 206 | const atomId_t oldatomId = const_cast<const World &>(World::getInstance()).lastChangedAtomId();
|
---|
| 207 | switch (notification->getChannelNo()) {
|
---|
| 208 | case AtomObservable::IndexChanged:
|
---|
| 209 | {
|
---|
| 210 | const atomId_t newatomId = dynamic_cast<atom *>(publisher)->getId();
|
---|
| 211 | LOG(3, "DEBUG: InformationBoard got IndexChanged from atom " << oldatomId << " to " << newatomId);
|
---|
[6d4925] | 212 | #ifndef NDEBUG
|
---|
| 213 | bool status =
|
---|
| 214 | #endif
|
---|
[41e287] | 215 | atomObservedValues.changeIdentifier(oldatomId, newatomId);
|
---|
[6d4925] | 216 | ASSERT( status,
|
---|
| 217 | "QtObservedInstanceBoard::recieveNotification() - cannot change atom's id "
|
---|
| 218 | +toString(oldatomId)+" "+toString(newatomId)+" in atomObservedValues.");
|
---|
[15c8a9] | 219 | // no need update SignedOn, ref does not change
|
---|
| 220 | emit atomIndexChanged(oldatomId, newatomId);
|
---|
| 221 | break;
|
---|
| 222 | }
|
---|
| 223 | default:
|
---|
[2f7988] | 224 | ASSERT(0, "QtObservedInstanceBoard::recieveNotification() - we cannot get here.");
|
---|
[15c8a9] | 225 | break;
|
---|
| 226 | }
|
---|
| 227 | } else {
|
---|
[2f7988] | 228 | ASSERT(0, "QtObservedInstanceBoard::recieveNotification() - notification from unknown source.");
|
---|
[15c8a9] | 229 | }
|
---|
| 230 | }
|
---|
| 231 |
|
---|
[2f7988] | 232 | void QtObservedInstanceBoard::atomcountsubjectKilled(const atomId_t _atomid)
|
---|
[15c8a9] | 233 | {
|
---|
[8b59dd] | 234 | LOG(3, "DEBUG: InstanceBoard emits atomRemoved for " << _atomid);
|
---|
| 235 | emit atomRemoved(_atomid);
|
---|
[15c8a9] | 236 | }
|
---|
| 237 |
|
---|
[2f7988] | 238 | void QtObservedInstanceBoard::moleculecountsubjectKilled(const moleculeId_t _molid)
|
---|
[15c8a9] | 239 | {
|
---|
[8b59dd] | 240 | LOG(3, "DEBUG: InstanceBoard emits moleculeRemoved for " << _molid);
|
---|
| 241 | emit moleculeRemoved(_molid);
|
---|
[15c8a9] | 242 | }
|
---|
| 243 |
|
---|
[41e287] | 244 | QtObservedAtom::ptr QtObservedInstanceBoard::getObservedAtom(const atomId_t _id)
|
---|
[15c8a9] | 245 | {
|
---|
[41e287] | 246 | return atomObservedValues.get(_id);
|
---|
[15c8a9] | 247 | }
|
---|
| 248 |
|
---|
[41e287] | 249 | QtObservedMolecule::ptr QtObservedInstanceBoard::getObservedMolecule(const moleculeId_t _id)
|
---|
[15c8a9] | 250 | {
|
---|
[41e287] | 251 | return moleculeObservedValues.get(_id);
|
---|
[15c8a9] | 252 | }
|
---|
| 253 |
|
---|
[68418e] | 254 | void QtObservedInstanceBoard::markObservedAtomAsConnected(const atomId_t _id)
|
---|
[15c8a9] | 255 | {
|
---|
[68418e] | 256 | atomObservedValues.markObservedValuesAsConnected(_id);
|
---|
[15c8a9] | 257 | }
|
---|
| 258 |
|
---|
[68418e] | 259 | void QtObservedInstanceBoard::markObservedAtomAsDisconnected(const atomId_t _id)
|
---|
[15c8a9] | 260 | {
|
---|
[68418e] | 261 | atomObservedValues.markObservedValuesAsDisconnected(_id);
|
---|
[15c8a9] | 262 | }
|
---|
[98c35c] | 263 |
|
---|
[4e6ffe] | 264 | void QtObservedInstanceBoard::markObservedAtomForErase(const atomId_t _id)
|
---|
| 265 | {
|
---|
| 266 | atomObservedValues.eraseObservedValues(_id);
|
---|
| 267 | }
|
---|
| 268 |
|
---|
[68418e] | 269 | void QtObservedInstanceBoard::markObservedMoleculeAsConnected(const moleculeId_t _id)
|
---|
[98c35c] | 270 | {
|
---|
[68418e] | 271 | moleculeObservedValues.markObservedValuesAsConnected(_id);
|
---|
[98c35c] | 272 | }
|
---|
| 273 |
|
---|
[68418e] | 274 | void QtObservedInstanceBoard::markObservedMoleculeAsDisconnected(const moleculeId_t _id)
|
---|
[98c35c] | 275 | {
|
---|
[68418e] | 276 | moleculeObservedValues.markObservedValuesAsDisconnected(_id);
|
---|
[98c35c] | 277 | }
|
---|
[4e6ffe] | 278 |
|
---|
| 279 | void QtObservedInstanceBoard::markObservedMoleculeForErase(const moleculeId_t _id)
|
---|
| 280 | {
|
---|
| 281 | moleculeObservedValues.eraseObservedValues(_id);
|
---|
| 282 | }
|
---|