source: src/Shapes/ShapeRegistry.cpp@ 4464ef

Action_Thermostats Add_AtomRandomPerturbation Add_RotateAroundBondAction Add_SelectAtomByNameAction Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_StructOpt_integration_tests Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_ChronosMutex Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion GeometryObjects Gui_displays_atomic_force_velocity IndependentFragmentGrids_IntegrationTest JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks RotateToPrincipalAxisSystem_UndoRedo StoppableMakroAction Subpackage_levmar Subpackage_vmg ThirdParty_MPQC_rebuilt_buildsystem TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps Ubuntu_1604_changes stable
Last change on this file since 4464ef was 574d377, checked in by Frederik Heber <heber@…>, 9 years ago

Using static Observable::..NotificationsChannel() functions.

  • Property mode set to 100644
File size: 4.5 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")
48{
49 Channels *OurChannel = new Channels;
50 Observable::insertNotificationChannel( std::make_pair(static_cast<Observable *>(this), OurChannel) );
51 // add instance for each notification type
52 for (size_t type = 0; type < NotificationType_MAX; ++type)
53 OurChannel->addChannel(type);
54
55 _lastchanged = NULL;
56}
57
58void ShapeRegistry::selectShape(const std::string& name)
59{
60 OBSERVE;
61 NOTIFY(SelectionChanged);
62 ASSERT(isPresentByName(name),"Shape selected that was not in the registry");
63 selectedShapes[name] = getByName(name);
64}
65
66void ShapeRegistry::selectShape(Shape* s)
67{
68 ASSERT(s,"Invalid pointer in selection of shape");
69 selectShape(s->getName());
70}
71
72void 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
80void ShapeRegistry::unselectShape(Shape* s)
81{
82 ASSERT(s,"Invalid pointer in unselection of shape");
83 unselectShape(s->getName());
84}
85
86void 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
96void ShapeRegistry::unselectAllShapes()
97{
98 OBSERVE;
99 NOTIFY(SelectionChanged);
100 selectedShapes.clear();
101}
102
103void 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
115bool ShapeRegistry::isSelected(const std::string& name) const
116{
117 return selectedShapes.find(name) != selectedShapes.end();
118}
119
120bool ShapeRegistry::isSelected(Shape* s) const
121{
122 ASSERT(s,"Invalid pointer in selection query of shape");
123 return isSelected(s->getName());
124}
125
126int 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
134std::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
144ShapeRegistry::~ShapeRegistry()
145{
146 cleanup();
147}
148
149std::string ShapeRegistry::getDefaultNameForShape(const std::string &baseName) const
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
161void ShapeRegistry::addShape(Shape s)
162{
163 OBSERVE;
164 Shape *instance = new Shape(s);
165 registerInstance(instance);
166 _lastchanged = instance;
167 NOTIFY(ShapeInserted);
168}
169
170void ShapeRegistry::removeShape(const std::string &name)
171{
172 selectedShapes.erase(name);
173 Shape *instance = getByName(name);
174 {
175 OBSERVE;
176 _lastchanged = instance;
177 NOTIFY(ShapeRemoved);
178 }
179 unregisterInstance(instance);
180 delete(instance);
181}
182
183CONSTRUCT_SINGLETON(ShapeRegistry)
184CONSTRUCT_REGISTRY(Shape)
Note: See TracBrowser for help on using the repository browser.