[145498] | 1 | /*
|
---|
| 2 | * Project: MoleCuilder
|
---|
| 3 | * Description: creates and alters molecular systems
|
---|
| 4 | * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
|
---|
[5aaa43] | 5 | * Copyright (C) 2013 Frederik Heber. All rights reserved.
|
---|
[145498] | 6 | *
|
---|
| 7 | *
|
---|
| 8 | * This file is part of MoleCuilder.
|
---|
| 9 | *
|
---|
| 10 | * MoleCuilder is free software: you can redistribute it and/or modify
|
---|
| 11 | * it under the terms of the GNU General Public License as published by
|
---|
| 12 | * the Free Software Foundation, either version 2 of the License, or
|
---|
| 13 | * (at your option) any later version.
|
---|
| 14 | *
|
---|
| 15 | * MoleCuilder is distributed in the hope that it will be useful,
|
---|
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 18 | * GNU General Public License for more details.
|
---|
| 19 | *
|
---|
| 20 | * You should have received a copy of the GNU General Public License
|
---|
| 21 | * along with MoleCuilder. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 22 | */
|
---|
| 23 |
|
---|
| 24 | /*
|
---|
| 25 | * ShapeRegistry.cpp
|
---|
| 26 | *
|
---|
| 27 | * Created on: Sep 13, 2012
|
---|
| 28 | * Author: ankele
|
---|
| 29 | */
|
---|
| 30 |
|
---|
| 31 | // include config.h
|
---|
| 32 | #ifdef HAVE_CONFIG_H
|
---|
| 33 | #include <config.h>
|
---|
| 34 | #endif
|
---|
| 35 |
|
---|
| 36 | #include "CodePatterns/MemDebug.hpp"
|
---|
| 37 |
|
---|
| 38 | #include "ShapeRegistry.hpp"
|
---|
| 39 |
|
---|
| 40 | #include "CodePatterns/Singleton_impl.hpp"
|
---|
| 41 | #include "CodePatterns/Registry_impl.hpp"
|
---|
[8f6a27] | 42 | #include "CodePatterns/Observer/Channels.hpp"
|
---|
[145498] | 43 |
|
---|
[1c9588] | 44 | #include "ShapeFactory.hpp"
|
---|
| 45 |
|
---|
[8f6a27] | 46 | ShapeRegistry::ShapeRegistry() :
|
---|
| 47 | Observable::Observable("ShapeRegistry")
|
---|
[145498] | 48 | {
|
---|
[8f6a27] | 49 | Channels *OurChannel = new Channels;
|
---|
[574d377] | 50 | Observable::insertNotificationChannel( std::make_pair(static_cast<Observable *>(this), OurChannel) );
|
---|
[8f6a27] | 51 | // add instance for each notification type
|
---|
| 52 | for (size_t type = 0; type < NotificationType_MAX; ++type)
|
---|
| 53 | OurChannel->addChannel(type);
|
---|
[85c36d] | 54 |
|
---|
| 55 | _lastchanged = NULL;
|
---|
[145498] | 56 | }
|
---|
| 57 |
|
---|
[8f12de] | 58 | void ShapeRegistry::selectShape(const std::string& name)
|
---|
| 59 | {
|
---|
| 60 | OBSERVE;
|
---|
| 61 | NOTIFY(SelectionChanged);
|
---|
| 62 | ASSERT(isPresentByName(name),"Shape selected that was not in the registry");
|
---|
[85c36d] | 63 | selectedShapes[name] = getByName(name);
|
---|
[8f12de] | 64 | }
|
---|
| 65 |
|
---|
| 66 | void ShapeRegistry::selectShape(Shape* s)
|
---|
| 67 | {
|
---|
| 68 | ASSERT(s,"Invalid pointer in selection of shape");
|
---|
| 69 | selectShape(s->getName());
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | void ShapeRegistry::unselectShape(const std::string& name)
|
---|
| 73 | {
|
---|
| 74 | OBSERVE;
|
---|
| 75 | NOTIFY(SelectionChanged);
|
---|
| 76 | ASSERT(isPresentByName(name),"Shape unselected that was not in the registry");
|
---|
| 77 | selectedShapes.erase(name);
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | void ShapeRegistry::unselectShape(Shape* s)
|
---|
| 81 | {
|
---|
| 82 | ASSERT(s,"Invalid pointer in unselection of shape");
|
---|
| 83 | unselectShape(s->getName());
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | void ShapeRegistry::selectAllShapes()
|
---|
| 87 | {
|
---|
| 88 | OBSERVE;
|
---|
| 89 | NOTIFY(SelectionChanged);
|
---|
| 90 | const_iterator iter;
|
---|
| 91 | for (iter = getBeginIter(); iter != getEndIter(); iter ++)
|
---|
| 92 | if (!isSelected(iter->first))
|
---|
| 93 | selectedShapes[iter->first] = iter->second;
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | void ShapeRegistry::unselectAllShapes()
|
---|
| 97 | {
|
---|
| 98 | OBSERVE;
|
---|
| 99 | NOTIFY(SelectionChanged);
|
---|
| 100 | selectedShapes.clear();
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | void ShapeRegistry::invertShapeSelection()
|
---|
| 104 | {
|
---|
| 105 | OBSERVE;
|
---|
| 106 | NOTIFY(SelectionChanged);
|
---|
| 107 | std::map<std::string, Shape*> inverted;
|
---|
| 108 | const_iterator iter;
|
---|
| 109 | for (iter = getBeginIter(); iter != getEndIter(); iter ++)
|
---|
| 110 | if (!isSelected(iter->first))
|
---|
| 111 | inverted[iter->first] = iter->second;
|
---|
| 112 | selectedShapes = inverted;
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | bool ShapeRegistry::isSelected(const std::string& name) const
|
---|
| 116 | {
|
---|
| 117 | return selectedShapes.find(name) != selectedShapes.end();
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | bool ShapeRegistry::isSelected(Shape* s) const
|
---|
| 121 | {
|
---|
| 122 | ASSERT(s,"Invalid pointer in selection query of shape");
|
---|
| 123 | return isSelected(s->getName());
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | int ShapeRegistry::countSelectedShapes() const
|
---|
| 127 | {
|
---|
| 128 | size_t count = 0;
|
---|
| 129 | for (const_iterator iter = selectedShapes.begin(); iter != selectedShapes.end(); ++iter)
|
---|
| 130 | count++;
|
---|
| 131 | return count;
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | std::vector<Shape*> ShapeRegistry::getSelectedShapes() const
|
---|
| 135 | {
|
---|
| 136 | std::vector<Shape*> returnShapes;
|
---|
| 137 | returnShapes.resize(countSelectedShapes());
|
---|
| 138 | int count = 0;
|
---|
| 139 | for (const_iterator iter = selectedShapes.begin(); iter != selectedShapes.end(); ++iter)
|
---|
| 140 | returnShapes[count++] = iter->second;
|
---|
| 141 | return returnShapes;
|
---|
| 142 | }
|
---|
| 143 |
|
---|
[145498] | 144 | ShapeRegistry::~ShapeRegistry()
|
---|
| 145 | {
|
---|
| 146 | cleanup();
|
---|
| 147 | }
|
---|
| 148 |
|
---|
[87ec81] | 149 | std::string ShapeRegistry::getDefaultNameForShape(const std::string &baseName) const
|
---|
[1c9588] | 150 | {
|
---|
| 151 | int n = 1;
|
---|
| 152 | while(true){
|
---|
| 153 | std::string name = baseName + toString(n);
|
---|
| 154 | if (!isPresentByName(name))
|
---|
| 155 | return name;
|
---|
| 156 | n ++;
|
---|
| 157 | }
|
---|
| 158 | return "";
|
---|
| 159 | }
|
---|
| 160 |
|
---|
[145498] | 161 | void ShapeRegistry::addShape(Shape s)
|
---|
| 162 | {
|
---|
[8f6a27] | 163 | OBSERVE;
|
---|
[145498] | 164 | Shape *instance = new Shape(s);
|
---|
| 165 | registerInstance(instance);
|
---|
[85c36d] | 166 | _lastchanged = instance;
|
---|
[8f6a27] | 167 | NOTIFY(ShapeInserted);
|
---|
[145498] | 168 | }
|
---|
| 169 |
|
---|
[1c9588] | 170 | void ShapeRegistry::removeShape(const std::string &name)
|
---|
| 171 | {
|
---|
[70e9eb] | 172 | selectedShapes.erase(name);
|
---|
[1c9588] | 173 | Shape *instance = getByName(name);
|
---|
[85c36d] | 174 | {
|
---|
| 175 | OBSERVE;
|
---|
| 176 | _lastchanged = instance;
|
---|
| 177 | NOTIFY(ShapeRemoved);
|
---|
| 178 | }
|
---|
[1c9588] | 179 | unregisterInstance(instance);
|
---|
| 180 | delete(instance);
|
---|
| 181 | }
|
---|
| 182 |
|
---|
[145498] | 183 | CONSTRUCT_SINGLETON(ShapeRegistry)
|
---|
| 184 | CONSTRUCT_REGISTRY(Shape)
|
---|