21.10.2021, 9:00 - 11:00: Due to updates GitLab may be unavailable for some minutes between 09:00 and 11:00.

propertycollection.cpp 5.63 KB
Newer Older
1
2
// ================================================================================================
// 
schultezub's avatar
schultezub committed
3
// This file is part of the CAMPVis Software Framework.
4
// 
5
// If not explicitly stated otherwise: Copyright (C) 2012-2014, all rights reserved,
schultezub's avatar
schultezub committed
6
//      Christian Schulte zu Berge <christian.szb@in.tum.de>
7
//      Chair for Computer Aided Medical Procedures
8
9
//      Technische Universitaet Muenchen
//      Boltzmannstr. 3, 85748 Garching b. Muenchen, Germany
10
// 
schultezub's avatar
schultezub committed
11
// For a full list of authors and contributors, please refer to the file "AUTHORS.txt".
12
// 
13
14
15
16
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file 
// except in compliance with the License. You may obtain a copy of the License at
// 
// http://www.apache.org/licenses/LICENSE-2.0
17
// 
18
19
20
21
// Unless required by applicable law or agreed to in writing, software distributed under the 
// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
// either express or implied. See the License for the specific language governing permissions 
// and limitations under the License.
22
23
24
// 
// ================================================================================================

schultezub's avatar
schultezub committed
25
#include "propertycollection.h"
26

27
#include "core/properties/abstractproperty.h"
28
29
#include "core/properties/metaproperty.h"
#include "core/tools/stringutils.h"
schultezub's avatar
schultezub committed
30

schultezub's avatar
schultezub committed
31
namespace campvis {
schultezub's avatar
schultezub committed
32
33
    HasPropertyCollection::HasPropertyCollection() {
    }
schultezub's avatar
schultezub committed
34

schultezub's avatar
schultezub committed
35
    HasPropertyCollection::~HasPropertyCollection() {
schultezub's avatar
schultezub committed
36
37
    }

38
39
    void HasPropertyCollection::addProperty(AbstractProperty& prop) {
        PropertyCollection::iterator it = findProperty(prop.getName());
schultezub's avatar
schultezub committed
40
        if (it != _properties.end()) {
schultezub's avatar
schultezub committed
41
            (*it)->s_changed.disconnect(this);
42
            s_propertyRemoved.emitSignal(*it);
43
            *it = &prop;
schultezub's avatar
schultezub committed
44
45
        }
        else {
46
            _properties.push_back(&prop);
schultezub's avatar
schultezub committed
47
        }
48
        prop.s_changed.connect(this, &HasPropertyCollection::onPropertyChanged);
49
        s_propertyAdded.emitSignal(&prop);
schultezub's avatar
schultezub committed
50
51
    }

52
53
    void HasPropertyCollection::removeProperty(AbstractProperty& prop) {
        PropertyCollection::iterator it = findProperty(prop.getName());
schultezub's avatar
schultezub committed
54
55
        if (it != _properties.end()) {
            (*it)->s_changed.disconnect(this);
schultezub's avatar
schultezub committed
56
            _properties.erase(it);
57
            s_propertyRemoved.emitSignal(&prop);
schultezub's avatar
schultezub committed
58
        }
schultezub's avatar
schultezub committed
59
60
    }

schultezub's avatar
schultezub committed
61
62
    AbstractProperty* HasPropertyCollection::getProperty(const std::string& name) const {
        PropertyCollection::const_iterator it = findProperty(name);
schultezub's avatar
schultezub committed
63
64
        if (it != _properties.end())
            return *it;
65

schultezub's avatar
schultezub committed
66
67
68
        return 0;
    }

69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
    AbstractProperty* HasPropertyCollection::getNestedProperty(const std::string& name) const {
        // try to find nested property (use :: as delimiter)
        std::vector<std::string> levels = StringUtils::split(name, "::");
        AbstractProperty* toReturn = getProperty(levels[0]);
        size_t currentLevel = 1;
        while (toReturn != 0 && currentLevel < levels.size()) {
            if (MetaProperty* tester = dynamic_cast<MetaProperty*>(toReturn)) {
            	toReturn = tester->getProperty(levels[currentLevel]);
                ++currentLevel;
            }
            else {
                toReturn = 0;
            }
        }

        return toReturn;
    }

schultezub's avatar
schultezub committed
87
88
89
90
91
    const PropertyCollection& HasPropertyCollection::getProperties() const {
        return _properties;
    }

    PropertyCollection& HasPropertyCollection::getProperties() {
schultezub's avatar
schultezub committed
92
93
94
        return _properties;
    }

schultezub's avatar
schultezub committed
95
    PropertyCollection::iterator HasPropertyCollection::findProperty(const std::string& name) {
schultezub's avatar
schultezub committed
96
        // using std::find would be more elegant, but also need more LOC...
schultezub's avatar
schultezub committed
97
        for (PropertyCollection::iterator it = _properties.begin(); it != _properties.end(); ++it) {
schultezub's avatar
schultezub committed
98
99
100
101
102
103
            if ((*it)->getName() == name)
                return it;
        }
        return _properties.end();
    }

schultezub's avatar
schultezub committed
104
    PropertyCollection::const_iterator HasPropertyCollection::findProperty(const std::string& name) const {
schultezub's avatar
schultezub committed
105
        // using std::find would be more elegant, but also need more LOC...
schultezub's avatar
schultezub committed
106
        for (PropertyCollection::const_iterator it = _properties.begin(); it != _properties.end(); ++it) {
schultezub's avatar
schultezub committed
107
108
109
110
111
112
            if ((*it)->getName() == name)
                return it;
        }
        return _properties.end();
    }

schultezub's avatar
schultezub committed
113
114
    void HasPropertyCollection::lockAllProperties() {
        for (PropertyCollection::iterator it = _properties.begin(); it != _properties.end(); ++it) {
schultezub's avatar
schultezub committed
115
116
117
118
            (*it)->lock();
        }
    }

schultezub's avatar
schultezub committed
119
120
    void HasPropertyCollection::unlockAllProperties() {
        for (PropertyCollection::iterator it = _properties.begin(); it != _properties.end(); ++it) {
schultezub's avatar
schultezub committed
121
122
123
124
            (*it)->unlock();
        }
    }

schultezub's avatar
schultezub committed
125
126
127
128
129
130
131
132
133
    void HasPropertyCollection::initAllProperties() {
        for (PropertyCollection::iterator it = _properties.begin(); it != _properties.end(); ++it) {
            (*it)->init();
        }
    }

    void HasPropertyCollection::deinitAllProperties() {
        for (PropertyCollection::iterator it = _properties.begin(); it != _properties.end(); ++it) {
            (*it)->deinit();
134
            (*it)->s_changed.disconnect(this);
schultezub's avatar
schultezub committed
135
136
137
        }
    }

138
139
    void HasPropertyCollection::onPropertyChanged(const AbstractProperty* /*prop*/) {
        // nothing to do here, method is just provided as convenience for child classes.
140
141
    }

142
143
144
    void HasPropertyCollection::clearProperties() {
        for (auto it = _properties.begin(); it != _properties.end(); ++it) {
            (*it)->s_changed.disconnect(this);
145
            s_propertyRemoved.triggerSignal(*it);
146
147
148
149
150
        }

        _properties.clear();
    }

151
}