| 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 | * QtObservedMolecule.cpp | 
|---|
| 25 | * | 
|---|
| 26 | *  Created on: Oct 28, 2015 | 
|---|
| 27 | *      Author: heber | 
|---|
| 28 | */ | 
|---|
| 29 |  | 
|---|
| 30 |  | 
|---|
| 31 | // include config.h | 
|---|
| 32 | #ifdef HAVE_CONFIG_H | 
|---|
| 33 | #include <config.h> | 
|---|
| 34 | #endif | 
|---|
| 35 |  | 
|---|
| 36 | #include "QtObservedMolecule.hpp" | 
|---|
| 37 |  | 
|---|
| 38 | #include "UIElements/Qt4/InstanceBoard/QtObservedInstanceBoard.hpp" | 
|---|
| 39 |  | 
|---|
| 40 | #include "CodePatterns/MemDebug.hpp" | 
|---|
| 41 | #include "CodePatterns/Assert.hpp" | 
|---|
| 42 | #include "CodePatterns/Log.hpp" | 
|---|
| 43 |  | 
|---|
| 44 | #include <boost/assign.hpp> | 
|---|
| 45 | #include <boost/bind.hpp> | 
|---|
| 46 |  | 
|---|
| 47 | #include "UIElements/Qt4/InstanceBoard/ObservedValue_wCallback.hpp" | 
|---|
| 48 | #include "UIElements/Qt4/InstanceBoard/ObservedValue_UpdateAtoms.hpp" | 
|---|
| 49 |  | 
|---|
| 50 | #include "Atom/atom.hpp" | 
|---|
| 51 | #include "Descriptors/MoleculeIdDescriptor.hpp" | 
|---|
| 52 | #include "World.hpp" | 
|---|
| 53 |  | 
|---|
| 54 | using namespace boost::assign; | 
|---|
| 55 |  | 
|---|
| 56 | static const Observable::channels_t getAllObservedChannels() | 
|---|
| 57 | { | 
|---|
| 58 | Observable::channels_t channels; | 
|---|
| 59 | channels += | 
|---|
| 60 | molecule::AtomInserted, | 
|---|
| 61 | molecule::AtomRemoved, | 
|---|
| 62 | molecule::IndexChanged, | 
|---|
| 63 | molecule::MoleculeNameChanged, | 
|---|
| 64 | molecule::BoundingBoxChanged; | 
|---|
| 65 | return channels; | 
|---|
| 66 | } | 
|---|
| 67 |  | 
|---|
| 68 | // static instances | 
|---|
| 69 | const Observable::channels_t QtObservedMolecule::BoundingBoxChannels(1, molecule::BoundingBoxChanged); | 
|---|
| 70 | const Observable::channels_t QtObservedMolecule::IndexChannels(1, molecule::IndexChanged); | 
|---|
| 71 | const Observable::channels_t QtObservedMolecule::NameChannels(1, molecule::MoleculeNameChanged); | 
|---|
| 72 |  | 
|---|
| 73 | QtObservedMolecule::QtObservedMolecule( | 
|---|
| 74 | const ObservedValues_t &_ObservedValues, | 
|---|
| 75 | QtObservedInstanceBoard &_board, | 
|---|
| 76 | QWidget * _parent) : | 
|---|
| 77 | QWidget(_parent), | 
|---|
| 78 | Observer("QtObservedMolecule"), | 
|---|
| 79 | subjectKilledCount(0), | 
|---|
| 80 | AllsignedOnChannels(getAllObservedChannels().size()), | 
|---|
| 81 | signedOffChannels(0), | 
|---|
| 82 | owner(NULL), | 
|---|
| 83 | board(_board), | 
|---|
| 84 | ObservedValues(_ObservedValues) | 
|---|
| 85 | { | 
|---|
| 86 | // activating Observer is done by ObservedValueContainer when it's prepared | 
|---|
| 87 | } | 
|---|
| 88 |  | 
|---|
| 89 | QtObservedMolecule::~QtObservedMolecule() | 
|---|
| 90 | { | 
|---|
| 91 | deactivateObserver(); | 
|---|
| 92 | } | 
|---|
| 93 |  | 
|---|
| 94 | void QtObservedMolecule::deactivateObserver() | 
|---|
| 95 | { | 
|---|
| 96 | if (owner != NULL) { | 
|---|
| 97 | Observable::channels_t channels = getAllObservedChannels(); | 
|---|
| 98 | for (Observable::channels_t::const_iterator iter = channels.begin(); | 
|---|
| 99 | iter != channels.end(); ++iter) | 
|---|
| 100 | owner->signOff(this, *iter); | 
|---|
| 101 | owner = NULL; | 
|---|
| 102 | signedOffChannels = AllsignedOnChannels; | 
|---|
| 103 | board.markObservedMoleculeAsDisconnected(getMolIndex()); | 
|---|
| 104 | } | 
|---|
| 105 | } | 
|---|
| 106 |  | 
|---|
| 107 | void QtObservedMolecule::activateObserver() | 
|---|
| 108 | { | 
|---|
| 109 | // sign on as observer (obtain non-const instance before) | 
|---|
| 110 | const molecule * const _molecule = getMolecule(getMolIndex()); | 
|---|
| 111 | if (_molecule != NULL) { | 
|---|
| 112 | Observable::channels_t channels = getAllObservedChannels(); | 
|---|
| 113 | owner = static_cast<const Observable *>(_molecule); | 
|---|
| 114 | for (Observable::channels_t::const_iterator iter = channels.begin(); | 
|---|
| 115 | iter != channels.end(); ++iter) | 
|---|
| 116 | owner->signOn(this, *iter); | 
|---|
| 117 | } else | 
|---|
| 118 | signedOffChannels = AllsignedOnChannels; | 
|---|
| 119 | } | 
|---|
| 120 |  | 
|---|
| 121 | void QtObservedMolecule::update(Observable *publisher) | 
|---|
| 122 | { | 
|---|
| 123 | ASSERT(0, | 
|---|
| 124 | "QtObservedMolecule::update() - general update from unexpected source."); | 
|---|
| 125 | } | 
|---|
| 126 |  | 
|---|
| 127 | void QtObservedMolecule::subjectKilled(Observable *publisher) | 
|---|
| 128 | { | 
|---|
| 129 | ++signedOffChannels; | 
|---|
| 130 |  | 
|---|
| 131 | if (signedOffChannels == AllsignedOnChannels) { | 
|---|
| 132 | // remove owner: no more signOff needed | 
|---|
| 133 | owner = NULL; | 
|---|
| 134 |  | 
|---|
| 135 | board.markObservedMoleculeAsDisconnected(getMolIndex()); | 
|---|
| 136 | } | 
|---|
| 137 | } | 
|---|
| 138 |  | 
|---|
| 139 | void QtObservedMolecule::recieveNotification(Observable *publisher, Notification_ptr notification) | 
|---|
| 140 | { | 
|---|
| 141 | const molecule * const _molecule = getMolecule(getMolIndex()); | 
|---|
| 142 | // when molecule is NULL we will soon get destroyed anyway | 
|---|
| 143 | if (_molecule == NULL) | 
|---|
| 144 | return; | 
|---|
| 145 | if (publisher == dynamic_cast<const Observable*>(_molecule)){ | 
|---|
| 146 | // notification from atom | 
|---|
| 147 | #ifdef LOG_OBSERVER | 
|---|
| 148 | observerLog().addMessage() << "++ Update of Observer "<< observerLog().getName(static_cast<Observer *>(this)) | 
|---|
| 149 | << " received notification from molecule " << getMolIndex() << " for channel " | 
|---|
| 150 | << notification->getChannelNo() << "."; | 
|---|
| 151 | #endif | 
|---|
| 152 | switch (notification->getChannelNo()) { | 
|---|
| 153 | case molecule::AtomInserted: | 
|---|
| 154 | { | 
|---|
| 155 | const atomId_t _id = _molecule->lastChangedAtomId(); | 
|---|
| 156 | emit atomInserted(_id); | 
|---|
| 157 | break; | 
|---|
| 158 | } | 
|---|
| 159 | case molecule::AtomRemoved: | 
|---|
| 160 | { | 
|---|
| 161 | const atomId_t _id = _molecule->lastChangedAtomId(); | 
|---|
| 162 | emit atomRemoved(_id); | 
|---|
| 163 | break; | 
|---|
| 164 | } | 
|---|
| 165 | case molecule::BoundingBoxChanged: | 
|---|
| 166 | { | 
|---|
| 167 | #ifdef LOG_OBSERVER | 
|---|
| 168 | observerLog().addMessage() << "++ Observer " << observerLog().getName(static_cast<Observer *>(this)) << " received notification that bounding box has changed."; | 
|---|
| 169 | #endif | 
|---|
| 170 | emit tesselationhullChanged(); | 
|---|
| 171 | emit boundingboxChanged(); | 
|---|
| 172 | break; | 
|---|
| 173 | } | 
|---|
| 174 | case molecule::IndexChanged: | 
|---|
| 175 | { | 
|---|
| 176 | #ifdef LOG_OBSERVER | 
|---|
| 177 | const atomId_t _id = _molecule->lastChangedAtomId(); | 
|---|
| 178 | observerLog().addMessage() << "++ Observer " << observerLog().getName(static_cast<Observer *>(this)) << " received notification that atom "+toString(_id)+"'s index has changed."; | 
|---|
| 179 | #endif | 
|---|
| 180 | emit indexChanged(); | 
|---|
| 181 | break; | 
|---|
| 182 | } | 
|---|
| 183 | case molecule::MoleculeNameChanged: | 
|---|
| 184 | { | 
|---|
| 185 | #ifdef LOG_OBSERVER | 
|---|
| 186 | observerLog().addMessage() << "++ Observer " << observerLog().getName(static_cast<Observer *>(this)) << " received notification that name has changed."; | 
|---|
| 187 | #endif | 
|---|
| 188 | emit nameChanged(); | 
|---|
| 189 | break; | 
|---|
| 190 | } | 
|---|
| 191 | default: | 
|---|
| 192 | break; | 
|---|
| 193 | } | 
|---|
| 194 | } | 
|---|
| 195 | } | 
|---|
| 196 |  | 
|---|
| 197 | const molecule * const QtObservedMolecule::getMolecule(const moleculeId_t _id) | 
|---|
| 198 | { | 
|---|
| 199 | const molecule * const mol = const_cast<const World &>(World::getInstance()). | 
|---|
| 200 | getMolecule(MoleculeById(_id)); | 
|---|
| 201 | return mol; | 
|---|
| 202 | } | 
|---|
| 203 |  | 
|---|
| 204 | static molecule::BoundingBoxInfo initBoundingBox() | 
|---|
| 205 | { | 
|---|
| 206 | molecule::BoundingBoxInfo info; | 
|---|
| 207 | info.position = zeroVec; | 
|---|
| 208 | info.radius = 0.; | 
|---|
| 209 | return info; | 
|---|
| 210 | } | 
|---|
| 211 |  | 
|---|
| 212 | void QtObservedMolecule::initObservedValues( | 
|---|
| 213 | ObservedValues_t &_ObservedValues, | 
|---|
| 214 | const moleculeId_t _molid, | 
|---|
| 215 | const molecule * const _molref, | 
|---|
| 216 | const boost::function<void(const moleculeId_t)> &_subjectKilled) | 
|---|
| 217 | { | 
|---|
| 218 | /* This is an old note from when the code was still part of cstor's initializer body. | 
|---|
| 219 | * TODO: Probably does not apply anymore but has not yet been tested. | 
|---|
| 220 | * | 
|---|
| 221 | * We must not use boost::cref(this) as "this" has not been properly constructed and seemingly | 
|---|
| 222 | * boost::cref tries to do some magic to grasp the inheritance hierarchy which fails because | 
|---|
| 223 | * the class has not been fully constructed yet. "This" itself seems to be working fine. | 
|---|
| 224 | */ | 
|---|
| 225 |  | 
|---|
| 226 | ASSERT( _ObservedValues.size() == MAX_ObservedTypes, | 
|---|
| 227 | "QtObservedMolecule::initObservedValues() - given ObservedValues has not correct size."); | 
|---|
| 228 |  | 
|---|
| 229 | // fill ObservedValues: index first | 
|---|
| 230 | const boost::function<moleculeId_t ()> MolIndexUpdater( | 
|---|
| 231 | boost::bind(&QtObservedMolecule::updateIndex)); | 
|---|
| 232 |  | 
|---|
| 233 | ObservedValue_wCallback<moleculeId_t> * const IndexObservable = | 
|---|
| 234 | new ObservedValue_wCallback<moleculeId_t>( | 
|---|
| 235 | _molref, | 
|---|
| 236 | MolIndexUpdater, | 
|---|
| 237 | "MoleculeIndex_"+toString(_molid), | 
|---|
| 238 | _molid, | 
|---|
| 239 | IndexChannels, | 
|---|
| 240 | _subjectKilled); | 
|---|
| 241 | _ObservedValues[MolIndex] = IndexObservable; | 
|---|
| 242 |  | 
|---|
| 243 | const boost::function<const moleculeId_t ()> MolIndexGetter = | 
|---|
| 244 | boost::bind(&ObservedValue_wCallback<moleculeId_t>::get, | 
|---|
| 245 | IndexObservable); | 
|---|
| 246 |  | 
|---|
| 247 | // fill ObservedValues: then all the other that need index | 
|---|
| 248 | const boost::function<std::string ()> MolNameUpdater( | 
|---|
| 249 | boost::bind(&QtObservedMolecule::updateName, MolIndexGetter)); | 
|---|
| 250 | const boost::function<molecule::BoundingBoxInfo ()> BoundingBoxUpdater( | 
|---|
| 251 | boost::bind(&QtObservedMolecule::updateBoundingBox, MolIndexGetter)); | 
|---|
| 252 |  | 
|---|
| 253 | _ObservedValues[MolName] = new ObservedValue_wCallback<std::string, moleculeId_t>( | 
|---|
| 254 | _molref, | 
|---|
| 255 | MolNameUpdater, | 
|---|
| 256 | "MoleculeName_"+toString(_molid), | 
|---|
| 257 | MolNameUpdater(), | 
|---|
| 258 | NameChannels, | 
|---|
| 259 | _subjectKilled, | 
|---|
| 260 | MolIndexGetter); | 
|---|
| 261 | _ObservedValues[BoundingBox] = new ObservedValue_wCallback<molecule::BoundingBoxInfo, moleculeId_t>( | 
|---|
| 262 | _molref, | 
|---|
| 263 | BoundingBoxUpdater, | 
|---|
| 264 | "MoleculeBoundingBox_"+toString(_molid), | 
|---|
| 265 | initBoundingBox(), | 
|---|
| 266 | BoundingBoxChannels, | 
|---|
| 267 | _subjectKilled, | 
|---|
| 268 | MolIndexGetter); | 
|---|
| 269 | } | 
|---|
| 270 |  | 
|---|
| 271 | void QtObservedMolecule::destroyObservedValues( | 
|---|
| 272 | std::vector<boost::any> &_ObservedValues) | 
|---|
| 273 | { | 
|---|
| 274 | delete boost::any_cast<ObservedValue_wCallback<moleculeId_t> *>(_ObservedValues[MolIndex]); | 
|---|
| 275 | delete boost::any_cast<ObservedValue_wCallback<std::string, moleculeId_t> *>(_ObservedValues[MolName]); | 
|---|
| 276 | delete boost::any_cast<ObservedValue_wCallback<molecule::BoundingBoxInfo, moleculeId_t> *>(_ObservedValues[BoundingBox]); | 
|---|
| 277 | _ObservedValues.clear(); | 
|---|
| 278 | } | 
|---|
| 279 |  | 
|---|
| 280 | molecule::BoundingBoxInfo QtObservedMolecule::updateBoundingBox( | 
|---|
| 281 | const boost::function<const moleculeId_t ()> &_getMolIndex) | 
|---|
| 282 | { | 
|---|
| 283 | const molecule * const mol = getMolecule(_getMolIndex()); | 
|---|
| 284 | if (mol != NULL) | 
|---|
| 285 | return mol->getBoundingBox(); | 
|---|
| 286 | else | 
|---|
| 287 | return molecule::BoundingBoxInfo(); | 
|---|
| 288 | } | 
|---|
| 289 |  | 
|---|
| 290 | moleculeId_t QtObservedMolecule::updateIndex() | 
|---|
| 291 | { | 
|---|
| 292 | return const_cast<const World &>(World::getInstance()).lastChangedMolId(); | 
|---|
| 293 | } | 
|---|
| 294 |  | 
|---|
| 295 | std::string QtObservedMolecule::updateName( | 
|---|
| 296 | const boost::function<const moleculeId_t ()> &_getMolIndex) | 
|---|
| 297 | { | 
|---|
| 298 | const molecule * const mol = getMolecule(_getMolIndex()); | 
|---|
| 299 | if (mol != NULL) | 
|---|
| 300 | return mol->getName(); | 
|---|
| 301 | else | 
|---|
| 302 | return std::string(""); | 
|---|
| 303 | } | 
|---|
| 304 |  | 
|---|
| 305 | moleculeId_t QtObservedMolecule::getMolIndex() const | 
|---|
| 306 | { | 
|---|
| 307 | return boost::any_cast<ObservedValue_wCallback<moleculeId_t> *>(ObservedValues[MolIndex])->get(); | 
|---|
| 308 | } | 
|---|
| 309 |  | 
|---|
| 310 | std::string QtObservedMolecule::getMolName() const | 
|---|
| 311 | { | 
|---|
| 312 | return boost::any_cast<ObservedValue_wCallback<std::string, moleculeId_t> *>(ObservedValues[MolName])->get(); | 
|---|
| 313 | } | 
|---|
| 314 |  | 
|---|
| 315 | molecule::BoundingBoxInfo QtObservedMolecule::getBoundingBox() const | 
|---|
| 316 | { | 
|---|
| 317 | return boost::any_cast<ObservedValue_wCallback<molecule::BoundingBoxInfo, moleculeId_t> *>(ObservedValues[BoundingBox])->get(); | 
|---|
| 318 | } | 
|---|