[b4bd0e] | 1 | /*
|
---|
| 2 | * Project: MoleCuilder
|
---|
| 3 | * Description: creates and alters molecular systems
|
---|
| 4 | * Copyright (C) 2016 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 | * QtObservedBond.cpp
|
---|
| 25 | *
|
---|
| 26 | * Created on: Mar 03, 2016
|
---|
| 27 | * Author: heber
|
---|
| 28 | */
|
---|
| 29 |
|
---|
| 30 |
|
---|
| 31 | // include config.h
|
---|
| 32 | #ifdef HAVE_CONFIG_H
|
---|
| 33 | #include <config.h>
|
---|
| 34 | #endif
|
---|
| 35 |
|
---|
| 36 | #include "QtObservedBond.hpp"
|
---|
| 37 |
|
---|
| 38 | #include <QtCore/QMetaType>
|
---|
| 39 |
|
---|
| 40 | #include "UIElements/Qt4/InstanceBoard/QtObservedInstanceBoard.hpp"
|
---|
| 41 |
|
---|
| 42 | #include "CodePatterns/MemDebug.hpp"
|
---|
| 43 |
|
---|
| 44 | #include <boost/assign.hpp>
|
---|
| 45 |
|
---|
| 46 | #include "Atom/atom.hpp"
|
---|
| 47 | #include "Bond/bond.hpp"
|
---|
| 48 | #include "World.hpp"
|
---|
| 49 |
|
---|
| 50 | #include "UIElements/Qt4/InstanceBoard/ObservedValue_wCallback.hpp"
|
---|
| 51 |
|
---|
| 52 | using namespace boost::assign;
|
---|
| 53 |
|
---|
| 54 | template <class T>
|
---|
| 55 | Observable * const getObservable(const T * _ptr)
|
---|
| 56 | {
|
---|
| 57 | return static_cast<Observable * const>(
|
---|
| 58 | const_cast<T * const>(_ptr));
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | static QtObservedBond::ObservableCount_t initAllsignedOnChannels(const bond::ptr _bond)
|
---|
| 62 | {
|
---|
| 63 | const QtObservedBond::ObservableCount_t returnlist =
|
---|
| 64 | boost::assign::list_of< QtObservedBond::ObservableCount_t::value_type >
|
---|
[273c8a] | 65 | ( getObservable(_bond.get()), 1);
|
---|
[b4bd0e] | 66 | return returnlist;
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | // static entities
|
---|
| 70 | const Observable::channels_t
|
---|
| 71 | QtObservedBond::BondDegreeChannels(1, BondObservable::DegreeChanged);
|
---|
| 72 |
|
---|
| 73 |
|
---|
| 74 | QtObservedBond::QtObservedBond(
|
---|
| 75 | const bondId_t _id,
|
---|
| 76 | const bond::ptr _bond,
|
---|
[4112691] | 77 | const QtObservedAtom::ptr &_leftatom,
|
---|
| 78 | const QtObservedAtom::ptr &_rightatom,
|
---|
[b4bd0e] | 79 | QtObservedInstanceBoard &_board,
|
---|
| 80 | QWidget * _parent) :
|
---|
| 81 | QWidget(_parent),
|
---|
| 82 | Observer("QtObservedBond"),
|
---|
| 83 | AllsignedOnChannels(initAllsignedOnChannels(_bond)),
|
---|
| 84 | bondowner(NULL),
|
---|
| 85 | oldbondId(_id),
|
---|
[4112691] | 86 | leftatom(_leftatom),
|
---|
| 87 | rightatom(_rightatom),
|
---|
[08a7ecc] | 88 | index(static_cast<const ObservedValue_Index_t>(const_cast<const QtObservedBond * const>(this))),
|
---|
[b4bd0e] | 89 | board(_board),
|
---|
| 90 | BoardIsGone(false),
|
---|
| 91 | ObservedValues(QtObservedBond::MAX_ObservedTypes)
|
---|
| 92 | {
|
---|
| 93 | qRegisterMetaType<bondId_t>("bondId_t");
|
---|
| 94 |
|
---|
| 95 | typedef boost::function<ObservableCount_t::mapped_type& (const ObservableCount_t::key_type&)> map_accessor_t;
|
---|
| 96 | const map_accessor_t accessor =
|
---|
| 97 | boost::bind<ObservableCount_t::mapped_type&>(
|
---|
| 98 | &ObservableCount_t::at,
|
---|
| 99 | boost::ref(subjectKilledCount), _1);
|
---|
| 100 |
|
---|
[d48a16] | 101 | const boost::function<void ()> bondSubjectKilled(
|
---|
[b4bd0e] | 102 | boost::bind(&QtObservedBond::countValuesSubjectKilled,
|
---|
| 103 | boost::ref(*this),
|
---|
| 104 | boost::bind(&QtObservedBond::getIndex, boost::cref(*this)),
|
---|
| 105 | boost::bind(accessor,
|
---|
| 106 | getObservable(_bond.get()))));
|
---|
[4112691] | 107 | initObservedValues( ObservedValues, _id, _bond, bondSubjectKilled);
|
---|
[b4bd0e] | 108 |
|
---|
| 109 | // activating Observer is done by ObservedValueContainer when it's inserted
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | QtObservedBond::~QtObservedBond()
|
---|
| 113 | {
|
---|
| 114 | boost::any_cast<ObservedValue_wCallback<int, ObservedValue_Index_t> *>(ObservedValues[BondDegree])->noteCallBackIsGone();
|
---|
| 115 |
|
---|
| 116 | deactivateObserver();
|
---|
| 117 |
|
---|
| 118 | destroyObservedValues(ObservedValues);
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | #ifdef HAVE_INLINE
|
---|
| 122 | inline
|
---|
| 123 | #endif
|
---|
| 124 | int QtObservedBond::updateDegree(const bond &_bond)
|
---|
| 125 | {
|
---|
| 126 | return _bond.getDegree();
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | void QtObservedBond::update(Observable *publisher)
|
---|
| 130 | {
|
---|
| 131 | ASSERT(0, "QtObservedBond::update() - we are not signed on for global updates.");
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | void QtObservedBond::subjectKilled(Observable *publisher)
|
---|
| 135 | {
|
---|
| 136 | ++(signedOffChannels[publisher]);
|
---|
| 137 |
|
---|
| 138 | checkForRemoval(getIndex());
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | void QtObservedBond::countValuesSubjectKilled(
|
---|
| 142 | ObservedValue_Index_t _id,
|
---|
| 143 | unsigned int &_counter)
|
---|
| 144 | {
|
---|
| 145 | ASSERT( _id == getIndex(),
|
---|
| 146 | "QtObservedBond::countValuesSubjectKilled() - bond "+toString(getIndex())
|
---|
| 147 | +" received countValuesSubjectKilled for bond id "+toString(_id)+".");
|
---|
| 148 |
|
---|
| 149 | ++_counter;
|
---|
| 150 |
|
---|
| 151 | checkForRemoval(_id);
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | void QtObservedBond::checkForRemoval(ObservedValue_Index_t _id)
|
---|
| 155 | {
|
---|
| 156 | if (bondowner != NULL) {
|
---|
| 157 | // only bond needs to be destroyed to signal removal
|
---|
| 158 | const ObservableCount_t::const_iterator subjectkillediter =
|
---|
| 159 | subjectKilledCount.find(const_cast<Observable *>(bondowner));
|
---|
| 160 | const ObservableCount_t::const_iterator allsignediter =
|
---|
| 161 | AllsignedOnChannels.find(const_cast<Observable *>(bondowner));
|
---|
| 162 | const ObservableCount_t::const_iterator signedoffiter =
|
---|
| 163 | signedOffChannels.find(const_cast<Observable *>(bondowner));
|
---|
| 164 | ASSERT( (subjectkillediter != subjectKilledCount.end())
|
---|
| 165 | && (allsignediter != AllsignedOnChannels.end())
|
---|
| 166 | && (signedoffiter != signedOffChannels.end()),
|
---|
| 167 | "QtObservedBond::checkForRemoval() - something is wrong here.");
|
---|
| 168 | if ((signedoffiter->second == allsignediter->second)
|
---|
| 169 | && (subjectkillediter->second == allsignediter->second)) {
|
---|
| 170 | // remove owner: no more signOff needed
|
---|
| 171 | bondowner = NULL;
|
---|
| 172 |
|
---|
| 173 | emit bondRemoved();
|
---|
| 174 |
|
---|
| 175 | if (!BoardIsGone) {
|
---|
| 176 | board.markObservedBondAsDisconnected(_id);
|
---|
| 177 | board.markObservedBondForErase(_id);
|
---|
| 178 | }
|
---|
| 179 | }
|
---|
| 180 | }
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | void QtObservedBond::recieveNotification(Observable *publisher, Notification_ptr notification)
|
---|
| 184 | {
|
---|
| 185 | // ObservedValues have been updated before, hence convert updates to Qt's signals
|
---|
[4112691] | 186 | if (publisher == bondowner) {
|
---|
[b4bd0e] | 187 | switch (notification->getChannelNo()) {
|
---|
| 188 | case BondObservable::DegreeChanged:
|
---|
| 189 | emit degreeChanged();
|
---|
| 190 | break;
|
---|
| 191 | default:
|
---|
| 192 | ASSERT(0, "QtObservedBond::recieveNotification() - we are not signed on to channel "
|
---|
| 193 | +toString(notification->getChannelNo())+" of the bond "
|
---|
| 194 | +toString(getBondIndex())+".");
|
---|
| 195 | break;
|
---|
| 196 | }
|
---|
| 197 | } else
|
---|
| 198 | ASSERT(0,
|
---|
| 199 | "QtObservedBond::recieveNotification() - received signal from unknown source.");
|
---|
| 200 | }
|
---|
| 201 |
|
---|
| 202 | static QtObservedBond::ObservableCount_t::mapped_type getObservableCountValue(
|
---|
| 203 | const QtObservedBond::ObservableCount_t &_map,
|
---|
| 204 | const Observable *_obs)
|
---|
| 205 | {
|
---|
| 206 | Observable * const obs_const = const_cast<Observable * const>(_obs);
|
---|
| 207 | QtObservedBond::ObservableCount_t::const_iterator iter = _map.find(obs_const);
|
---|
| 208 | ASSERT( iter != _map.end(),
|
---|
| 209 | "getObservableCount_tValue");
|
---|
| 210 | return iter->second;
|
---|
| 211 | }
|
---|
| 212 |
|
---|
| 213 | static void assignObservableCountValue(
|
---|
| 214 | QtObservedBond::ObservableCount_t &_map,
|
---|
| 215 | const Observable *_obs,
|
---|
| 216 | const QtObservedBond::ObservableCount_t::mapped_type &_value)
|
---|
| 217 | {
|
---|
| 218 | Observable * const obs_const = const_cast<Observable * const>(_obs);
|
---|
| 219 | QtObservedBond::ObservableCount_t::iterator iter = _map.find(obs_const);
|
---|
| 220 | ASSERT( iter != _map.end(),
|
---|
| 221 | "getObservableCount_tValue");
|
---|
| 222 | iter->second = _value;
|
---|
| 223 | }
|
---|
| 224 |
|
---|
| 225 | void QtObservedBond::activateObserver()
|
---|
| 226 | {
|
---|
| 227 | signedOffChannels.clear();
|
---|
| 228 | subjectKilledCount.clear();
|
---|
| 229 |
|
---|
[4112691] | 230 | atom * leftatomref = getAtom(getLeftAtomIndex());
|
---|
| 231 | atom * rightatomref = getAtom(getRightAtomIndex());
|
---|
| 232 | bond::ptr bondref = leftatomref->getBond(rightatomref);
|
---|
| 233 | if (bondref != NULL) {
|
---|
| 234 | // bond
|
---|
| 235 | {
|
---|
| 236 | bondowner = static_cast<const Observable *>(bondref.get());
|
---|
| 237 | bondowner->signOn(this, BondObservable::DegreeChanged);
|
---|
| 238 | subjectKilledCount.insert( std::make_pair(const_cast<Observable * const>(bondowner), 0));
|
---|
| 239 | signedOffChannels.insert( std::make_pair(const_cast<Observable * const>(bondowner), 0));
|
---|
[b4bd0e] | 240 | }
|
---|
| 241 |
|
---|
[4112691] | 242 | // and mark as connected
|
---|
| 243 | if (!BoardIsGone)
|
---|
| 244 | board.markObservedBondAsConnected(getIndex());
|
---|
| 245 | } else {
|
---|
| 246 | subjectKilledCount.insert( std::make_pair(const_cast<Observable * const>(bondowner), 1));
|
---|
| 247 | assignObservableCountValue(signedOffChannels, bondowner,
|
---|
| 248 | getObservableCountValue(AllsignedOnChannels, bondowner));
|
---|
[b4bd0e] | 249 | }
|
---|
[4112691] | 250 |
|
---|
| 251 | // pass thru to signals from both atoms
|
---|
[522c45] | 252 | connect( leftatom.get(), SIGNAL(indexChanged()), this, SIGNAL(leftAtomIndexChanged()));
|
---|
[4112691] | 253 | connect( leftatom.get(), SIGNAL(elementChanged()), this, SIGNAL(leftAtomElementChanged()));
|
---|
| 254 | connect( leftatom.get(), SIGNAL(positionChanged()), this, SIGNAL(leftAtomPositionChanged()));
|
---|
[7f185f9] | 255 | connect( leftatom.get(), SIGNAL(moleculeChanged()), this, SIGNAL(leftmoleculeChanged()));
|
---|
[522c45] | 256 | connect( rightatom.get(), SIGNAL(indexChanged()), this, SIGNAL(rightAtomIndexChanged()));
|
---|
[4112691] | 257 | connect( rightatom.get(), SIGNAL(elementChanged()), this, SIGNAL(rightAtomElementChanged()));
|
---|
| 258 | connect( rightatom.get(), SIGNAL(positionChanged()), this, SIGNAL(rightAtomPositionChanged()));
|
---|
[7f185f9] | 259 | connect( rightatom.get(), SIGNAL(moleculeChanged()), this, SIGNAL(rightmoleculeChanged()));
|
---|
[b4bd0e] | 260 | }
|
---|
| 261 |
|
---|
| 262 | void QtObservedBond::deactivateObserver()
|
---|
| 263 | {
|
---|
| 264 | if (bondowner != NULL) {
|
---|
| 265 | const ObservableCount_t::iterator subjectkilledbonditer =
|
---|
| 266 | subjectKilledCount.find(const_cast<Observable *>(bondowner));
|
---|
| 267 | ASSERT( (subjectkilledbonditer != subjectKilledCount.end()),
|
---|
| 268 | "QtObservedBond::deactivateObserver() - no entry in subjectKilledCount for bond"
|
---|
| 269 | +toString(bondowner)+","
|
---|
| 270 | +" has activateObserver() been called?");
|
---|
| 271 | if (subjectkilledbonditer->second == 0)
|
---|
| 272 | bondowner->signOff(this, BondObservable::DegreeChanged);
|
---|
| 273 | subjectkilledbonditer->second = 1;
|
---|
| 274 | bondowner = NULL;
|
---|
| 275 | signedOffChannels.clear();
|
---|
| 276 | signedOffChannels.insert(AllsignedOnChannels.begin(), AllsignedOnChannels.end());
|
---|
| 277 |
|
---|
| 278 | if (!BoardIsGone)
|
---|
| 279 | board.markObservedBondAsDisconnected(getIndex());
|
---|
| 280 | }
|
---|
| 281 | }
|
---|
| 282 |
|
---|
| 283 | const atom * const QtObservedBond::getAtomConst(const atomId_t _id)
|
---|
| 284 | {
|
---|
| 285 | const atom * const _atom = const_cast<const World &>(World::getInstance()).
|
---|
| 286 | getAtom(AtomById(_id));
|
---|
| 287 | return _atom;
|
---|
| 288 | }
|
---|
| 289 |
|
---|
| 290 | atom * const QtObservedBond::getAtom(const atomId_t _id)
|
---|
| 291 | {
|
---|
| 292 | atom * const _atom = World::getInstance().getAtom(AtomById(_id));
|
---|
| 293 | return _atom;
|
---|
| 294 | }
|
---|
| 295 |
|
---|
| 296 | void QtObservedBond::initObservedValues(
|
---|
| 297 | ObservedValues_t &_ObservedValues,
|
---|
| 298 | const bondId_t _id,
|
---|
| 299 | const bond::ptr _bondref,
|
---|
[4112691] | 300 | const boost::function<void()> &_bondsubjectKilled)
|
---|
[b4bd0e] | 301 | {
|
---|
| 302 | // fill ObservedValues: index first
|
---|
| 303 | const boost::function<ObservedValue_Index_t ()> BondIndexGetter =
|
---|
| 304 | boost::bind(&QtObservedBond::getIndex,
|
---|
| 305 | boost::cref(*this));
|
---|
| 306 |
|
---|
| 307 | // fill ObservedValues: then all the other that need index
|
---|
| 308 | const boost::function<int ()> BondDegreeUpdater(
|
---|
| 309 | boost::bind(&QtObservedBond::updateDegree, boost::cref(*_bondref)));
|
---|
| 310 |
|
---|
| 311 | _ObservedValues[BondDegree] = new ObservedValue_wCallback<int, ObservedValue_Index_t>(
|
---|
| 312 | _bondref.get(),
|
---|
| 313 | BondDegreeUpdater,
|
---|
| 314 | "BondDegree_bond"+toString(_id),
|
---|
| 315 | BondDegreeUpdater(),
|
---|
| 316 | BondDegreeChannels,
|
---|
[d48a16] | 317 | _bondsubjectKilled);
|
---|
[b4bd0e] | 318 | }
|
---|
| 319 |
|
---|
| 320 | void QtObservedBond::destroyObservedValues(
|
---|
| 321 | std::vector<boost::any> &_ObservedValues)
|
---|
| 322 | {
|
---|
| 323 | delete boost::any_cast<ObservedValue_wCallback<int, ObservedValue_Index_t> *>(_ObservedValues[BondDegree]);
|
---|
| 324 | _ObservedValues.clear();
|
---|
| 325 | }
|
---|
| 326 |
|
---|
| 327 | ObservedValue_Index_t QtObservedBond::getIndex() const
|
---|
| 328 | {
|
---|
[205389] | 329 | ASSERT( index != NULL,
|
---|
[b4bd0e] | 330 | "QtObservedBond::getIndex() - index is NULL");
|
---|
[205389] | 331 | return index;
|
---|
[b4bd0e] | 332 | }
|
---|
| 333 |
|
---|
| 334 | const QtObservedBond::bondId_t QtObservedBond::getBondIndex() const
|
---|
| 335 | {
|
---|
| 336 | return QtObservedBond::bondId_t(getLeftAtomIndex(), getRightAtomIndex());
|
---|
| 337 | }
|
---|
| 338 |
|
---|
| 339 | const int& QtObservedBond::getBondDegree() const
|
---|
| 340 | {
|
---|
| 341 | return boost::any_cast<ObservedValue_wCallback<int, ObservedValue_Index_t> *>(ObservedValues[BondDegree])->get();
|
---|
| 342 | }
|
---|
| 343 |
|
---|
| 344 | const atomId_t& QtObservedBond::getLeftAtomIndex() const
|
---|
| 345 | {
|
---|
[4112691] | 346 | return leftatom->getAtomIndex();
|
---|
[b4bd0e] | 347 | }
|
---|
| 348 |
|
---|
| 349 | const atomicNumber_t& QtObservedBond::getLeftAtomElement() const
|
---|
| 350 | {
|
---|
[4112691] | 351 | return leftatom->getAtomElement();
|
---|
[b4bd0e] | 352 | }
|
---|
| 353 |
|
---|
| 354 | const Vector& QtObservedBond::getLeftAtomPosition() const
|
---|
| 355 | {
|
---|
[4112691] | 356 | return leftatom->getAtomPosition();
|
---|
[b4bd0e] | 357 | }
|
---|
| 358 |
|
---|
[7f185f9] | 359 | const moleculeId_t QtObservedBond::getLeftMoleculeIndex() const
|
---|
| 360 | {
|
---|
[273c8a] | 361 | if (leftatom->getMoleculeRef() != NULL)
|
---|
| 362 | return leftatom->getMoleculeRef()->getMolIndex();
|
---|
| 363 | else
|
---|
| 364 | return (moleculeId_t)-1;
|
---|
[7f185f9] | 365 | }
|
---|
| 366 |
|
---|
[b4bd0e] | 367 | const atomId_t& QtObservedBond::getRightAtomIndex() const
|
---|
| 368 | {
|
---|
[4112691] | 369 | return rightatom->getAtomIndex();
|
---|
[b4bd0e] | 370 | }
|
---|
| 371 |
|
---|
| 372 | const atomicNumber_t& QtObservedBond::getRightAtomElement() const
|
---|
| 373 | {
|
---|
[4112691] | 374 | return rightatom->getAtomElement();
|
---|
[b4bd0e] | 375 | }
|
---|
| 376 |
|
---|
| 377 | const Vector& QtObservedBond::getRightAtomPosition() const
|
---|
| 378 | {
|
---|
[4112691] | 379 | return rightatom->getAtomPosition();
|
---|
[b4bd0e] | 380 | }
|
---|
| 381 |
|
---|
[7f185f9] | 382 | const moleculeId_t QtObservedBond::getRightMoleculeIndex() const
|
---|
[b4bd0e] | 383 | {
|
---|
[273c8a] | 384 | if (rightatom->getMoleculeRef() != NULL)
|
---|
| 385 | return rightatom->getMoleculeRef()->getMolIndex();
|
---|
| 386 | else
|
---|
| 387 | return (moleculeId_t)-1;
|
---|
[b4bd0e] | 388 | }
|
---|