source: src/Shapes/ShapeRegistry.cpp@ c8cb0d

Candidate_v1.7.1 stable
Last change on this file since c8cb0d was c8cb0d, checked in by Frederik Heber <frederik.heber@…>, 7 days ago

Streamlines channel creation in Observables.

  • CodePatterns is now version 1.3.4.
  • we no longer need to add the channels manually in the cstor of a class that derives from Observable. Instead, we just need to pass the maximum number of channels (as they are typically enumerated anyway) and they are generated and added.
  • added mutex protection when inserting.
  • adjusted class Relay to forward similar convenience cstors.
  • adjusted all call sites in molecuilder.
  • Property mode set to 100644
File size: 4.3 KB
Line 
1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
4 * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
5 * Copyright (C) 2013 Frederik Heber. All rights reserved.
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"
42#include "CodePatterns/Observer/Channels.hpp"
43
44#include "ShapeFactory.hpp"
45
46ShapeRegistry::ShapeRegistry() :
47 Observable::Observable("ShapeRegistry", NotificationType_MAX)
48{
49 _lastchanged = NULL;
50}
51
52void ShapeRegistry::selectShape(const std::string& name)
53{
54 OBSERVE;
55 NOTIFY(SelectionChanged);
56 ASSERT(isPresentByName(name),"Shape selected that was not in the registry");
57 selectedShapes[name] = getByName(name);
58}
59
60void ShapeRegistry::selectShape(Shape* s)
61{
62 ASSERT(s,"Invalid pointer in selection of shape");
63 selectShape(s->getName());
64}
65
66void ShapeRegistry::unselectShape(const std::string& name)
67{
68 OBSERVE;
69 NOTIFY(SelectionChanged);
70 ASSERT(isPresentByName(name),"Shape unselected that was not in the registry");
71 selectedShapes.erase(name);
72}
73
74void ShapeRegistry::unselectShape(Shape* s)
75{
76 ASSERT(s,"Invalid pointer in unselection of shape");
77 unselectShape(s->getName());
78}
79
80void ShapeRegistry::selectAllShapes()
81{
82 OBSERVE;
83 NOTIFY(SelectionChanged);
84 const_iterator iter;
85 for (iter = getBeginIter(); iter != getEndIter(); iter ++)
86 if (!isSelected(iter->first))
87 selectedShapes[iter->first] = iter->second;
88}
89
90void ShapeRegistry::unselectAllShapes()
91{
92 OBSERVE;
93 NOTIFY(SelectionChanged);
94 selectedShapes.clear();
95}
96
97void ShapeRegistry::invertShapeSelection()
98{
99 OBSERVE;
100 NOTIFY(SelectionChanged);
101 std::map<std::string, Shape*> inverted;
102 const_iterator iter;
103 for (iter = getBeginIter(); iter != getEndIter(); iter ++)
104 if (!isSelected(iter->first))
105 inverted[iter->first] = iter->second;
106 selectedShapes = inverted;
107}
108
109bool ShapeRegistry::isSelected(const std::string& name) const
110{
111 return selectedShapes.find(name) != selectedShapes.end();
112}
113
114bool ShapeRegistry::isSelected(Shape* s) const
115{
116 ASSERT(s,"Invalid pointer in selection query of shape");
117 return isSelected(s->getName());
118}
119
120int ShapeRegistry::countSelectedShapes() const
121{
122 size_t count = 0;
123 for (const_iterator iter = selectedShapes.begin(); iter != selectedShapes.end(); ++iter)
124 count++;
125 return count;
126}
127
128std::vector<Shape*> ShapeRegistry::getSelectedShapes() const
129{
130 std::vector<Shape*> returnShapes;
131 returnShapes.resize(countSelectedShapes());
132 int count = 0;
133 for (const_iterator iter = selectedShapes.begin(); iter != selectedShapes.end(); ++iter)
134 returnShapes[count++] = iter->second;
135 return returnShapes;
136}
137
138ShapeRegistry::~ShapeRegistry()
139{
140 cleanup();
141}
142
143std::string ShapeRegistry::getDefaultNameForShape(const std::string &baseName) const
144{
145 int n = 1;
146 while(true){
147 std::string name = baseName + toString(n);
148 if (!isPresentByName(name))
149 return name;
150 n ++;
151 }
152 return "";
153}
154
155void ShapeRegistry::addShape(Shape s)
156{
157 OBSERVE;
158 Shape *instance = new Shape(s);
159 registerInstance(instance);
160 _lastchanged = instance;
161 NOTIFY(ShapeInserted);
162}
163
164void ShapeRegistry::removeShape(const std::string &name)
165{
166 selectedShapes.erase(name);
167 Shape *instance = getByName(name);
168 {
169 OBSERVE;
170 _lastchanged = instance;
171 NOTIFY(ShapeRemoved);
172 }
173 unregisterInstance(instance);
174 delete(instance);
175}
176
177CONSTRUCT_SINGLETON(ShapeRegistry)
178CONSTRUCT_REGISTRY(Shape)
Note: See TracBrowser for help on using the repository browser.