[bed759] | 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 | * QtMoleculeItem.cpp
|
---|
| 25 | *
|
---|
| 26 | * Created on: Jan 17, 2015
|
---|
| 27 | * Author: heber
|
---|
| 28 | */
|
---|
| 29 |
|
---|
| 30 | // include config.h
|
---|
| 31 | #ifdef HAVE_CONFIG_H
|
---|
| 32 | #include <config.h>
|
---|
| 33 | #endif
|
---|
| 34 |
|
---|
| 35 | #include "QtMoleculeItem.hpp"
|
---|
| 36 |
|
---|
| 37 | #include <QModelIndex>
|
---|
| 38 |
|
---|
| 39 | #include "UIElements/Views/Qt4/MoleculeList/QtMoleculeList.hpp"
|
---|
| 40 |
|
---|
| 41 | #include "CodePatterns/MemDebug.hpp"
|
---|
| 42 |
|
---|
[69b434] | 43 | #include "CodePatterns/Assert.hpp"
|
---|
| 44 | #include "CodePatterns/toString.hpp"
|
---|
| 45 |
|
---|
[bed759] | 46 | #include "CodePatterns/Observer/Notification.hpp"
|
---|
| 47 |
|
---|
[6d1e0a] | 48 | #include <algorithm>
|
---|
[bed759] | 49 |
|
---|
[cc2976] | 50 | #include <boost/thread/locks.hpp>
|
---|
| 51 |
|
---|
[69b434] | 52 | #include "Descriptors/MoleculeIdDescriptor.hpp"
|
---|
[6d1e0a] | 53 | #include "molecule.hpp"
|
---|
[69b434] | 54 | #include "World.hpp"
|
---|
[bed759] | 55 |
|
---|
[6d1e0a] | 56 | QtMoleculeItem::QtMoleculeItem(
|
---|
[69b434] | 57 | const moleculeId_t _molid,
|
---|
[6d1e0a] | 58 | const channellist_t &_channellist,
|
---|
| 59 | const enum MoveTypes _movetype,
|
---|
[cc2976] | 60 | const emitDirtyState_t _emitDirtyState,
|
---|
| 61 | const emitSubjectKilledState_t _emitSubjectKilledState) :
|
---|
[6d1e0a] | 62 | Observer("QtMoleculeItem"),
|
---|
[69b434] | 63 | molid(_molid),
|
---|
[6d1e0a] | 64 | movetype(_movetype),
|
---|
| 65 | channellist(_channellist),
|
---|
[3e52834] | 66 | owner(NULL),
|
---|
[6d1e0a] | 67 | dirty(true),
|
---|
[cc2976] | 68 | emitDirtyState(_emitDirtyState),
|
---|
| 69 | emitSubjectKilledState(_emitSubjectKilledState)
|
---|
[bed759] | 70 | {
|
---|
[b9680b] | 71 | // BUG: We cannot sign on here as then and again we catch "pure virtual function called"
|
---|
| 72 | // this makes no sense as both the Observer part (which also has a recievedNotification())
|
---|
| 73 | // nor QtMoleculeItem's vtable have been fully created.
|
---|
| 74 | // signOnToMolecule();
|
---|
[6d1e0a] | 75 | setFlags(flags() | Qt::ItemIsSelectable);
|
---|
[bed759] | 76 | }
|
---|
| 77 |
|
---|
[6d1e0a] | 78 | void QtMoleculeItem::signOnToMolecule()
|
---|
[bed759] | 79 | {
|
---|
[cc2976] | 80 | // count the number of threads entering the function
|
---|
| 81 | std::cout << "QtMoleculeItem::signOnToMolecule() called for " << this << std::endl;
|
---|
[63fb7a] | 82 | const molecule * const mol = const_cast<const World &>(World::getInstance()).
|
---|
| 83 | getMolecule(MoleculeById(molid));
|
---|
[69b434] | 84 | if (mol != NULL) {
|
---|
[cc2976] | 85 | owner_mutex.lock();
|
---|
| 86 | if (owner == NULL) {
|
---|
| 87 | owner_mutex.unlock();
|
---|
| 88 | const Observable *owner_copy = static_cast<const Observable *>(mol);
|
---|
| 89 | for (channellist_t::const_iterator channeliter = channellist.begin();
|
---|
| 90 | channeliter != channellist.end(); ++channeliter)
|
---|
| 91 | owner_copy->signOn(this, *channeliter);
|
---|
| 92 | owner_mutex.lock();
|
---|
| 93 | owner = owner_copy;
|
---|
| 94 | owner_mutex.unlock();
|
---|
| 95 | std::cout << "QtMoleculeItem " << this << " has signed on to " << owner << std::endl;
|
---|
| 96 | } else
|
---|
| 97 | owner_mutex.unlock();
|
---|
[69b434] | 98 | }
|
---|
[bed759] | 99 | }
|
---|
| 100 |
|
---|
[6d1e0a] | 101 | void QtMoleculeItem::signOffFromMolecule()
|
---|
| 102 | {
|
---|
[cc2976] | 103 | // prevent via owner that another thread enters the function again
|
---|
| 104 | // (re-entrancy)
|
---|
[47f0e4] | 105 | const Observable *owner_copy;
|
---|
[cc2976] | 106 | {
|
---|
| 107 | boost::recursive_mutex::scoped_lock lock(owner_mutex);
|
---|
| 108 | if (owner != NULL) {
|
---|
| 109 | owner_copy = owner;
|
---|
| 110 | owner = NULL;
|
---|
| 111 | }
|
---|
| 112 | }
|
---|
| 113 | std::cout << "QtMoleculeItem::signOffFromMolecule() called for " << this << std::endl;
|
---|
| 114 | if (owner_copy != NULL) {
|
---|
[3e52834] | 115 | for (channellist_t::const_iterator channeliter = channellist.begin();
|
---|
| 116 | channeliter != channellist.end(); ++channeliter)
|
---|
[cc2976] | 117 | owner_copy->signOff(this, *channeliter);
|
---|
| 118 | std::cout << "QtMoleculeItem " << this << " has signed off from " << owner_copy << std::endl;
|
---|
[47f0e4] | 119 | owner_copy = NULL;
|
---|
[69b434] | 120 | }
|
---|
[6d1e0a] | 121 | }
|
---|
| 122 |
|
---|
[bed759] | 123 | QtMoleculeItem::~QtMoleculeItem()
|
---|
| 124 | {
|
---|
[b9680b] | 125 | // BUG: We cannot sign off here as then and again we catch "pure virtual function called"
|
---|
| 126 | // this makes no sense as neither the Observer part (which also has a recievedNotification())
|
---|
| 127 | // nor QtMoleculeItem's vtable have yet been destroyed.
|
---|
[47f0e4] | 128 | signOffFromMolecule();
|
---|
[bed759] | 129 | }
|
---|
| 130 |
|
---|
[6d1e0a] | 131 | void QtMoleculeItem::updateState()
|
---|
| 132 | {
|
---|
| 133 | if (dirty) {
|
---|
| 134 | internal_updateState();
|
---|
| 135 | dirty = false;
|
---|
| 136 | }
|
---|
| 137 | }
|
---|
[bed759] | 138 |
|
---|
| 139 | void QtMoleculeItem::update(Observable *publisher)
|
---|
| 140 | {}
|
---|
| 141 |
|
---|
| 142 | void QtMoleculeItem::recieveNotification(Observable *publisher, Notification_ptr notification)
|
---|
| 143 | {
|
---|
| 144 | if (dynamic_cast<molecule *>(publisher) != NULL) {
|
---|
[6d1e0a] | 145 | if (notification->getChannelNo() == molecule::AboutToBeRemoved) {
|
---|
| 146 | signOffFromMolecule();
|
---|
| 147 | // prevent any remaining updates from accessing the molecule
|
---|
[69b434] | 148 | //molid = -1;
|
---|
[6d1e0a] | 149 | dirty = false;
|
---|
| 150 | } else {
|
---|
| 151 | channellist_t::const_iterator iter =
|
---|
| 152 | std::find(channellist.begin(), channellist.end(), notification->getChannelNo());
|
---|
| 153 | if (iter != channellist.end()) {
|
---|
| 154 | dirty = true;
|
---|
[fcdf05] | 155 | emitDirtyState(getMoleculeId(), getType(), movetype);
|
---|
[6d1e0a] | 156 | } else {
|
---|
| 157 | ASSERT(0,
|
---|
| 158 | "QtMoleculeItem::recieveNotification() - received notification to channel "
|
---|
| 159 | +toString(notification->getChannelNo())+" we are not subscribed to.");
|
---|
[bed759] | 160 | }
|
---|
| 161 | }
|
---|
| 162 | }
|
---|
| 163 | }
|
---|
| 164 |
|
---|
| 165 | void QtMoleculeItem::subjectKilled(Observable *publisher)
|
---|
| 166 | {
|
---|
[47f0e4] | 167 | // signOffFromMolecule();
|
---|
[53c1ff] | 168 | dirty = false;
|
---|
[cc2976] | 169 | emitSubjectKilledState(getMoleculeId());
|
---|
[bed759] | 170 | }
|
---|
[69b434] | 171 |
|
---|
| 172 | const molecule * const QtMoleculeItem::getMolecule() const
|
---|
| 173 | {
|
---|
[63fb7a] | 174 | const molecule * const mol = const_cast<const World &>(World::getInstance()).getMolecule(MoleculeById(molid));
|
---|
[7d0ddb] | 175 | // ASSERT (mol != NULL,
|
---|
| 176 | // "QtMoleculeItem::getMolecule() - mol with "+toString(molid)+" is gone.");
|
---|
[69b434] | 177 | return mol;
|
---|
| 178 | }
|
---|