| 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 | * QtObservedAtom.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 "QtObservedAtom.hpp"
|
|---|
| 37 |
|
|---|
| 38 | #include "UIElements/Qt4/InstanceBoard/QtObservedInstanceBoard.hpp"
|
|---|
| 39 |
|
|---|
| 40 | #include "CodePatterns/MemDebug.hpp"
|
|---|
| 41 |
|
|---|
| 42 | #include <boost/assign.hpp>
|
|---|
| 43 |
|
|---|
| 44 | #include "Atom/atom.hpp"
|
|---|
| 45 | #include "Bond/bond.hpp"
|
|---|
| 46 | #include "Descriptors/AtomIdDescriptor.hpp"
|
|---|
| 47 | #include "Element/element.hpp"
|
|---|
| 48 | #include "World.hpp"
|
|---|
| 49 |
|
|---|
| 50 | #include "UIElements/Views/Qt4/Qt3D/ObservedValue_wCallback.hpp"
|
|---|
| 51 |
|
|---|
| 52 | using namespace boost::assign;
|
|---|
| 53 |
|
|---|
| 54 | static const Observable::channels_t getAtomBondsChannels()
|
|---|
| 55 | {
|
|---|
| 56 | Observable::channels_t channels;
|
|---|
| 57 | channels += AtomObservable::BondsAdded, AtomObservable::BondsRemoved;
|
|---|
| 58 | return channels;
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | static const Observable::channels_t getAllObservedChannels()
|
|---|
| 62 | {
|
|---|
| 63 | Observable::channels_t channels;
|
|---|
| 64 | channels +=
|
|---|
| 65 | AtomObservable::IndexChanged,
|
|---|
| 66 | AtomObservable::PositionChanged,
|
|---|
| 67 | AtomObservable::ElementChanged,
|
|---|
| 68 | AtomObservable::BondsAdded,
|
|---|
| 69 | AtomObservable::BondsRemoved;
|
|---|
| 70 | return channels;
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | // static entities
|
|---|
| 74 | const Observable::channels_t
|
|---|
| 75 | QtObservedAtom::AtomIndexChannels(1, AtomObservable::IndexChanged);
|
|---|
| 76 | const Observable::channels_t
|
|---|
| 77 | QtObservedAtom::AtomPositionChannels(1, AtomObservable::PositionChanged);
|
|---|
| 78 | const Observable::channels_t
|
|---|
| 79 | QtObservedAtom::AtomElementChannels(1, AtomObservable::ElementChanged);
|
|---|
| 80 | const Observable::channels_t
|
|---|
| 81 | QtObservedAtom::AtomBondsChannels(getAtomBondsChannels());
|
|---|
| 82 |
|
|---|
| 83 | QtObservedAtom::QtObservedAtom(
|
|---|
| 84 | const ObservedValues_t &_ObservedValues,
|
|---|
| 85 | QtObservedInstanceBoard &_board,
|
|---|
| 86 | QWidget * _parent) :
|
|---|
| 87 | QWidget(_parent),
|
|---|
| 88 | Observer("QtObservedAtom"),
|
|---|
| 89 | subjectKilledCount(0),
|
|---|
| 90 | AllsignedOnChannels(getAllObservedChannels().size()),
|
|---|
| 91 | signedOffChannels(0),
|
|---|
| 92 | owner(NULL),
|
|---|
| 93 | board(_board),
|
|---|
| 94 | ObservedValues(_ObservedValues)
|
|---|
| 95 | {
|
|---|
| 96 | activateObserver();
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | QtObservedAtom::~QtObservedAtom()
|
|---|
| 100 | {
|
|---|
| 101 | deactivateObserver();
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 | const atom * const QtObservedAtom::getAtomConst(const atomId_t _id)
|
|---|
| 105 | {
|
|---|
| 106 | const atom * const _atom = const_cast<const World &>(World::getInstance()).
|
|---|
| 107 | getAtom(AtomById(_id));
|
|---|
| 108 | return _atom;
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 | atom * const QtObservedAtom::getAtom(const atomId_t _id)
|
|---|
| 112 | {
|
|---|
| 113 | atom * const _atom = World::getInstance().getAtom(AtomById(_id));
|
|---|
| 114 | return _atom;
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | atomId_t QtObservedAtom::updateIndex()
|
|---|
| 118 | {
|
|---|
| 119 | return const_cast<const World &>(World::getInstance()).lastChangedAtomId();
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | Vector QtObservedAtom::updatePosition(
|
|---|
| 123 | const boost::function<const atomId_t ()> &_getAtomIndex)
|
|---|
| 124 | {
|
|---|
| 125 | const atom * const _atom = getAtomConst(_getAtomIndex());
|
|---|
| 126 | if (_atom != NULL) {
|
|---|
| 127 | return _atom->getPosition();
|
|---|
| 128 | } else {
|
|---|
| 129 | return zeroVec;
|
|---|
| 130 | }
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | atomicNumber_t QtObservedAtom::updateElement(
|
|---|
| 134 | const boost::function<const atomId_t ()> &_getAtomIndex)
|
|---|
| 135 | {
|
|---|
| 136 | const atom * const _atom = getAtomConst(_getAtomIndex());
|
|---|
| 137 | if (_atom != NULL) {
|
|---|
| 138 | return _atom->getElementNo();
|
|---|
| 139 | } else {
|
|---|
| 140 | return (atomicNumber_t)-1;
|
|---|
| 141 | }
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | QtObservedAtom::ListOfBonds_t QtObservedAtom::updateBonds(
|
|---|
| 145 | const boost::function<const atomId_t ()> &_getAtomIndex)
|
|---|
| 146 | {
|
|---|
| 147 | ListOfBonds_t ListOfBonds;
|
|---|
| 148 | const atom * const _atom = getAtomConst(_getAtomIndex());
|
|---|
| 149 | if (_atom != NULL) {
|
|---|
| 150 | // make sure bonds is up-to-date
|
|---|
| 151 | const BondList ListBonds = _atom->getListOfBonds();
|
|---|
| 152 | for (BondList::const_iterator iter = ListBonds.begin();
|
|---|
| 153 | iter != ListBonds.end();
|
|---|
| 154 | ++iter)
|
|---|
| 155 | ListOfBonds.insert( ListOfBonds.end(), std::make_pair(
|
|---|
| 156 | (*iter)->leftatom->getId(),
|
|---|
| 157 | (*iter)->rightatom->getId()) );
|
|---|
| 158 | }
|
|---|
| 159 | return ListOfBonds;
|
|---|
| 160 | }
|
|---|
| 161 |
|
|---|
| 162 | void QtObservedAtom::update(Observable *publisher)
|
|---|
| 163 | {
|
|---|
| 164 | ASSERT(0, "QtObservedAtom::update() - we are not signed on for global updates.");
|
|---|
| 165 | }
|
|---|
| 166 |
|
|---|
| 167 | void QtObservedAtom::subjectKilled(Observable *publisher)
|
|---|
| 168 | {
|
|---|
| 169 | ++signedOffChannels;
|
|---|
| 170 |
|
|---|
| 171 | if (signedOffChannels == AllsignedOnChannels) {
|
|---|
| 172 | // remove owner: no more signOff needed
|
|---|
| 173 | owner = NULL;
|
|---|
| 174 |
|
|---|
| 175 | board.atomcountsubjectKilled(getAtomIndex());
|
|---|
| 176 | }
|
|---|
| 177 | }
|
|---|
| 178 |
|
|---|
| 179 | void QtObservedAtom::recieveNotification(Observable *publisher, Notification_ptr notification)
|
|---|
| 180 | {
|
|---|
| 181 | // ObservedValues have been updated before, hence convert updates to Qt's signals
|
|---|
| 182 | switch (notification->getChannelNo()) {
|
|---|
| 183 | case AtomObservable::IndexChanged:
|
|---|
| 184 | emit indexChanged();
|
|---|
| 185 | break;
|
|---|
| 186 | case AtomObservable::PositionChanged:
|
|---|
| 187 | emit positionChanged();
|
|---|
| 188 | break;
|
|---|
| 189 | case AtomObservable::ElementChanged:
|
|---|
| 190 | emit elementChanged();
|
|---|
| 191 | break;
|
|---|
| 192 | case AtomObservable::BondsAdded:
|
|---|
| 193 | case AtomObservable::BondsRemoved:
|
|---|
| 194 | emit bondsChanged();
|
|---|
| 195 | break;
|
|---|
| 196 | default:
|
|---|
| 197 | ASSERT(0, "QtObservedAtom::recieveNotification() - we are not signed on to channel "
|
|---|
| 198 | +toString(notification->getChannelNo())+" of the atom.");
|
|---|
| 199 | break;
|
|---|
| 200 | }
|
|---|
| 201 | }
|
|---|
| 202 |
|
|---|
| 203 | void QtObservedAtom::activateObserver()
|
|---|
| 204 | {
|
|---|
| 205 | atom * atomref = getAtom(getAtomIndex());
|
|---|
| 206 | if (atomref != NULL) {
|
|---|
| 207 | Observable::channels_t channels = getAllObservedChannels();
|
|---|
| 208 | owner = static_cast<const Observable *>(atomref);
|
|---|
| 209 | for (Observable::channels_t::const_iterator iter = channels.begin();
|
|---|
| 210 | iter != channels.end(); ++iter)
|
|---|
| 211 | owner->signOn(this, *iter);
|
|---|
| 212 | } else
|
|---|
| 213 | signedOffChannels = getAllObservedChannels().size();
|
|---|
| 214 | }
|
|---|
| 215 |
|
|---|
| 216 | void QtObservedAtom::deactivateObserver()
|
|---|
| 217 | {
|
|---|
| 218 | // sign Off
|
|---|
| 219 | if (owner != NULL) {
|
|---|
| 220 | Observable::channels_t channels = getAllObservedChannels();
|
|---|
| 221 | for (Observable::channels_t::const_iterator iter = channels.begin();
|
|---|
| 222 | iter != channels.end(); ++iter)
|
|---|
| 223 | owner->signOff(this, *iter);
|
|---|
| 224 | owner = NULL;
|
|---|
| 225 | signedOffChannels = AllsignedOnChannels;
|
|---|
| 226 | }
|
|---|
| 227 | }
|
|---|
| 228 |
|
|---|
| 229 | void QtObservedAtom::initObservedValues(
|
|---|
| 230 | ObservedValues_t &_ObservedValues,
|
|---|
| 231 | const atomId_t _id,
|
|---|
| 232 | const atom * const _atomref,
|
|---|
| 233 | const boost::function<void(const atomId_t)> &_subjectKilled)
|
|---|
| 234 | {
|
|---|
| 235 | /* This is an old note from when the code was still part of cstor's initializer body.
|
|---|
| 236 | * TODO: Probably does not apply anymore but has not yet been tested.
|
|---|
| 237 | *
|
|---|
| 238 | * We must not use boost::cref(this) as "this" has not been properly constructed and seemingly
|
|---|
| 239 | * boost::cref tries to do some magic to grasp the inheritance hierarchy which fails because
|
|---|
| 240 | * the class has not been fully constructed yet. "This" itself seems to be working fine.
|
|---|
| 241 | */
|
|---|
| 242 |
|
|---|
| 243 | ASSERT( _ObservedValues.size() == MAX_ObservedTypes,
|
|---|
| 244 | "QtObservedAtom::initObservedValues() - given ObservedValues has not correct size.");
|
|---|
| 245 |
|
|---|
| 246 | // fill ObservedValues: index first
|
|---|
| 247 | const boost::function<atomId_t ()> AtomIndexUpdater(
|
|---|
| 248 | boost::bind(&QtObservedAtom::updateIndex));
|
|---|
| 249 |
|
|---|
| 250 | ObservedValue_wCallback<atomId_t> * const IndexObservable =
|
|---|
| 251 | new ObservedValue_wCallback<atomId_t>(
|
|---|
| 252 | _atomref,
|
|---|
| 253 | boost::bind(&QtObservedAtom::updateIndex),
|
|---|
| 254 | "AtomIndex_"+toString(_id),
|
|---|
| 255 | _id,
|
|---|
| 256 | AtomIndexChannels,
|
|---|
| 257 | _subjectKilled);
|
|---|
| 258 | _ObservedValues[AtomIndex] = IndexObservable;
|
|---|
| 259 |
|
|---|
| 260 | const boost::function<const atomId_t ()> AtomIndexGetter =
|
|---|
| 261 | boost::bind(&ObservedValue_wCallback<atomId_t>::get,
|
|---|
| 262 | IndexObservable);
|
|---|
| 263 |
|
|---|
| 264 | // fill ObservedValues: then all the other that need index
|
|---|
| 265 | const boost::function<Vector ()> AtomPositionUpdater(
|
|---|
| 266 | boost::bind(&QtObservedAtom::updatePosition, AtomIndexGetter));
|
|---|
| 267 | const boost::function<atomicNumber_t ()> AtomElementUpdater(
|
|---|
| 268 | boost::bind(&QtObservedAtom::updateElement, AtomIndexGetter));
|
|---|
| 269 | const boost::function<ListOfBonds_t ()> AtomBondsUpdater(
|
|---|
| 270 | boost::bind(&QtObservedAtom::updateBonds, AtomIndexGetter));
|
|---|
| 271 |
|
|---|
| 272 | _ObservedValues[AtomPosition] = new ObservedValue_wCallback<Vector, atomId_t>(
|
|---|
| 273 | _atomref,
|
|---|
| 274 | AtomPositionUpdater,
|
|---|
| 275 | "AtomPosition_"+toString(_id),
|
|---|
| 276 | AtomPositionUpdater(),
|
|---|
| 277 | AtomPositionChannels,
|
|---|
| 278 | _subjectKilled,
|
|---|
| 279 | AtomIndexGetter);
|
|---|
| 280 | _ObservedValues[AtomElement] = new ObservedValue_wCallback<atomicNumber_t, atomId_t>(
|
|---|
| 281 | _atomref,
|
|---|
| 282 | AtomElementUpdater,
|
|---|
| 283 | "AtomElement"+toString(_id),
|
|---|
| 284 | AtomElementUpdater(),
|
|---|
| 285 | AtomElementChannels,
|
|---|
| 286 | _subjectKilled,
|
|---|
| 287 | AtomIndexGetter);
|
|---|
| 288 | _ObservedValues[AtomBonds] = new ObservedValue_wCallback<ListOfBonds_t, atomId_t>(
|
|---|
| 289 | _atomref,
|
|---|
| 290 | AtomBondsUpdater,
|
|---|
| 291 | "AtomBonds_"+toString(_id),
|
|---|
| 292 | AtomBondsUpdater(),
|
|---|
| 293 | AtomBondsChannels,
|
|---|
| 294 | _subjectKilled,
|
|---|
| 295 | AtomIndexGetter);
|
|---|
| 296 | }
|
|---|
| 297 |
|
|---|
| 298 | void QtObservedAtom::destroyObservedValues(
|
|---|
| 299 | std::vector<boost::any> &_ObservedValues)
|
|---|
| 300 | {
|
|---|
| 301 | delete boost::any_cast<ObservedValue_wCallback<atomId_t> *>(_ObservedValues[AtomIndex]);
|
|---|
| 302 | delete boost::any_cast<ObservedValue_wCallback<Vector, atomId_t> *>(_ObservedValues[AtomPosition]);
|
|---|
| 303 | delete boost::any_cast<ObservedValue_wCallback<atomicNumber_t, atomId_t> *>(_ObservedValues[AtomElement]);
|
|---|
| 304 | delete boost::any_cast<ObservedValue_wCallback<ListOfBonds_t, atomId_t> *>(_ObservedValues[AtomBonds]);
|
|---|
| 305 | _ObservedValues.clear();
|
|---|
| 306 | }
|
|---|
| 307 |
|
|---|
| 308 | atomId_t QtObservedAtom::getAtomIndex() const
|
|---|
| 309 | {
|
|---|
| 310 | return boost::any_cast<ObservedValue_wCallback<atomId_t> *>(ObservedValues[AtomIndex])->get();
|
|---|
| 311 | }
|
|---|
| 312 |
|
|---|
| 313 | Vector QtObservedAtom::getAtomPosition() const
|
|---|
| 314 | {
|
|---|
| 315 | return boost::any_cast<ObservedValue_wCallback<Vector, atomId_t> *>(ObservedValues[AtomPosition])->get();
|
|---|
| 316 | }
|
|---|
| 317 |
|
|---|
| 318 | atomicNumber_t QtObservedAtom::getAtomElement() const
|
|---|
| 319 | {
|
|---|
| 320 | return boost::any_cast<ObservedValue_wCallback<atomicNumber_t, atomId_t> *>(ObservedValues[AtomElement])->get();
|
|---|
| 321 | }
|
|---|
| 322 |
|
|---|
| 323 | QtObservedAtom::ListOfBonds_t QtObservedAtom::getAtomBonds() const
|
|---|
| 324 | {
|
|---|
| 325 | return boost::any_cast<ObservedValue_wCallback<ListOfBonds_t, atomId_t> *>(ObservedValues[AtomBonds])->get();
|
|---|
| 326 | }
|
|---|