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

propertycollectionwidget.cpp 5.47 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
// 
// ================================================================================================

25
26
#include "propertycollectionwidget.h"

schultezub's avatar
schultezub committed
27
28
#include "application/gui/properties/abstractpropertywidget.h"
#include "application/gui/properties/propertywidgetfactory.h"
29
#include "core/datastructures/datacontainer.h"
30
31
32
33
34
#include "core/properties/abstractproperty.h"
#include "core/properties/propertycollection.h"

#include <QPushButton>

schultezub's avatar
schultezub committed
35
namespace campvis {
36
37
38
39
40


    PropertyCollectionWidget::PropertyCollectionWidget(QWidget* parent /*= 0*/)
        : QWidget(parent)
        , _propCollection(0)
41
        , _dataContainer(0)
42
43
44
45
46
47
        , _layout(0)
    {
        setupWidget();
    }

    PropertyCollectionWidget::~PropertyCollectionWidget() {
48
        clearWidgetMap();
49
50
    }

51
    void PropertyCollectionWidget::updatePropCollection(HasPropertyCollection* propertyCollection, DataContainer* dc) {
52
        // remove and delete all widgets of the previous PropertyCollection
53
        clearWidgetMap();
54
        
55
56
57
        _propCollection = propertyCollection;
        _dataContainer = dc;

58
        // create widgets for the new PropertyCollection
schultezub's avatar
schultezub committed
59
60
        if (propertyCollection != 0) {
            for (std::vector<AbstractProperty*>::const_iterator it = propertyCollection->getProperties().begin(); it != propertyCollection->getProperties().end(); ++it) {
61
62
63
64
65
                addProperty(*it);
            } 

            propertyCollection->s_propertyAdded.connect(this, &PropertyCollectionWidget::onPropCollectionPropAdded);
            propertyCollection->s_propertyRemoved.connect(this, &PropertyCollectionWidget::onPropCollectionPropRemoved);
66
67
68
69
70
        }
    }

    void PropertyCollectionWidget::setupWidget() {
        _layout = new QVBoxLayout();
71
        _layout->setSpacing(8);
72
        _layout->setMargin(0);
73
        setLayout(_layout);
74
        connect(this, SIGNAL(s_widgetVisibilityChanged(QWidget*, bool)), this, SLOT(onWidgetVisibilityChanged(QWidget*, bool)));
75
76
        connect(this, SIGNAL(propertyAdded(AbstractProperty*)), this, SLOT(addProperty(AbstractProperty*)));
        connect(this, SIGNAL(propertyRemoved(std::map<AbstractProperty*, QWidget*>::iterator)), this, SLOT(removeProperty(std::map<AbstractProperty*, QWidget*>::iterator)));
77
78
    }

79
80
    void PropertyCollectionWidget::clearWidgetMap() {
        for (std::map<AbstractProperty*, QWidget*>::iterator it = _widgetMap.begin(); it != _widgetMap.end(); ++it) {
81
            removeProperty(it);
82
83
84
        }

        _widgetMap.clear();
85
86
87
88
89

        if (_propCollection != 0) {
            _propCollection->s_propertyAdded.disconnect(this);
            _propCollection->s_propertyRemoved.disconnect(this);
        }
90
91
92
93
94
95
    }

    void PropertyCollectionWidget::onPropertyVisibilityChanged(const AbstractProperty* prop) {
        // const_cast does not harm anything.
        std::map<AbstractProperty*, QWidget*>::iterator item = _widgetMap.find(const_cast<AbstractProperty*>(prop));
        if (item != _widgetMap.end())
96
97
98
99
100
            emit s_widgetVisibilityChanged(item->second, item->first->isVisible());
    }

    void PropertyCollectionWidget::onWidgetVisibilityChanged(QWidget* widget, bool visibility) {
        widget->setVisible(visibility);
101
102
    }

103
104
105
106
107
108
109
110
111
112
113
    void PropertyCollectionWidget::onPropCollectionPropAdded(AbstractProperty* prop) {
        emit propertyAdded(prop);
    }

    void PropertyCollectionWidget::onPropCollectionPropRemoved(AbstractProperty* prop) {
        std::map<AbstractProperty*, QWidget*>::iterator it = _widgetMap.find(prop);
        if (it != _widgetMap.end())
            emit propertyRemoved(it);
    }

    void PropertyCollectionWidget::addProperty(AbstractProperty* prop) {
114
        QWidget* propWidget = PropertyWidgetFactory::getRef().createWidget(prop, _dataContainer);
115
116
117
118
119
        if (propWidget == 0)
            propWidget = new QPushButton(QString::fromStdString(prop->getTitle()));

        _widgetMap.insert(std::make_pair(prop, propWidget));
        _layout->addWidget(propWidget);
120

121
        prop->s_visibilityChanged.connect(this, &PropertyCollectionWidget::onPropertyVisibilityChanged);
122
        prop->s_visibilityChanged(prop);
123
124
125
126
127
128
129
130
    }

    void PropertyCollectionWidget::removeProperty(std::map<AbstractProperty*, QWidget*>::iterator it) {
        it->first->s_visibilityChanged.disconnect(this);
        _layout->removeWidget(it->second);
        delete it->second;
    }

131
}