workflowcontrollerwidget.cpp 7.48 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// ================================================================================================
// 
// This file is part of the CAMPVis Software Framework.
// 
// If not explicitly stated otherwise: Copyright (C) 2012-2014, all rights reserved,
//      Christian Schulte zu Berge <christian.szb@in.tum.de>
//      Chair for Computer Aided Medical Procedures
//      Technische Universitaet Muenchen
//      Boltzmannstr. 3, 85748 Garching b. Muenchen, Germany
// 
// For a full list of authors and contributors, please refer to the file "AUTHORS.txt".
// 
// 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
// 
// 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.
// 
// ================================================================================================

#include "workflowcontrollerwidget.h"

#include "cgt/assert.h"
28
#include "application/campvisapplication.h"
29
#include "application/tools/qtjobprocessor.h"
30
31
32
33
34
35
36
37
38
39
40
41
42

#include <QLabel>
#include <QGridLayout>
#include <QPushButton>
#include <QScrollArea>
#include <QSignalMapper>
#include <QVBoxLayout>

namespace campvis {

    const std::string WorkflowControllerWidget::loggerCat_ = "CAMPVis.application.WorkflowControllerWidget";


43
    WorkflowControllerWidget::WorkflowControllerWidget(CampVisApplication* application, QWidget* parent /*= nullptr*/)
44
        : QWidget(parent)
45
        , _application(application)
46
        , _workflow(nullptr)
47
        , _isBackWardsStep(true)
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
        , _signalMapper(nullptr)
        , _lblWorkflowStage(nullptr)
        , _propertyCollectionWidget(nullptr)
        , _btnPrevStage(nullptr)
        , _layoutNextStages(nullptr)
    {
        setupGUI();
    }

    WorkflowControllerWidget::~WorkflowControllerWidget() {
        clearNextStagesLayout();
    }

    void WorkflowControllerWidget::init() {
        if (_workflow != nullptr)
            _workflow->init();
    }

    void WorkflowControllerWidget::deinit() {
        if (_workflow != nullptr)
            _workflow->deinit();
    }

    void WorkflowControllerWidget::setWorkflow(AbstractWorkflow* workflow) {
        if (_workflow != nullptr) {
            _workflow->s_stageChanged.disconnect(this);
            _workflow->s_stageAvailabilityChanged.disconnect(this);
        }

        _workflow = workflow;
        _stageHistory.clear();

        QtJobProc.enqueueJob([&]() {
            if (_workflow != nullptr) {
                // FIXME: Taking the first of the DataContainers here is not really beautiful!
83
                _propertyCollectionWidget->updatePropCollection(_workflow, _workflow->getDataContainer());
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160

                _workflow->s_stageChanged.connect(this, &WorkflowControllerWidget::onStageChanged);
                _workflow->s_stageAvailabilityChanged.connect(this, &WorkflowControllerWidget::onStageAvailabilityChanged);
            }
            else {
                _propertyCollectionWidget->updatePropCollection(nullptr, nullptr);
                _lblWorkflowStage->setText(tr("No Workflow Loaded."));
            }
        });
    }

    void WorkflowControllerWidget::onPrevButtonClicked() {
        if (!_stageHistory.empty()) {
            int previousStage = _stageHistory.back();
            _stageHistory.pop_back();
            _isBackWardsStep = true;
            _workflow->setCurrentStage(previousStage);
        }
    }

    void WorkflowControllerWidget::onStageSelectionButtonClicked(int id) {
        _workflow->setCurrentStage(id);
    }

    void WorkflowControllerWidget::setupGUI() {
        QGridLayout* layout = new QGridLayout(this);

        _lblWorkflowStage = new QLabel(tr("Current Stage: n/a"), this);
        layout->addWidget(_lblWorkflowStage, 0, 0, 1, 2);

        QScrollArea* sa = new QScrollArea(this);
        sa->setWidgetResizable(true);
        sa->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
        sa->setFrameStyle(QScrollArea::NoFrame);

        _propertyCollectionWidget = new PropertyCollectionWidget(this);
        sa->setWidget(_propertyCollectionWidget);
        layout->addWidget(sa, 1, 0, 1, 2);

        _btnPrevStage = new QPushButton("<< n/a");
        _btnPrevStage->setEnabled(false);
        layout->addWidget(_btnPrevStage, 2, 0, 1, 1);

        _layoutNextStages = new QVBoxLayout();
        layout->addLayout(_layoutNextStages, 2, 1, 1, 1);

        _signalMapper = new QSignalMapper(this);
        populateNextStagesLayout();

        connect(_btnPrevStage, SIGNAL(clicked()), this, SLOT(onPrevButtonClicked()));
        connect(_signalMapper, SIGNAL(mapped(int)), this, SLOT(onStageSelectionButtonClicked(int)));
    }

    void WorkflowControllerWidget::clearNextStagesLayout() {
        for (auto it = _nextButtons.begin(); it != _nextButtons.end(); ++it) {
            _layoutNextStages->removeWidget(it->first);
            delete it->first;
        }
        _nextButtons.clear();
    }

    void WorkflowControllerWidget::populateNextStagesLayout() {
        // first, remove all buttons from the layout
        clearNextStagesLayout();

        // now, populate with buttons for the current stage
        if (_workflow != nullptr) {
            const AbstractWorkflow::Stage& s = _workflow->getCurrentStage();
            for (auto it = s._possibleTransitions.begin(); it != s._possibleTransitions.end(); ++it) {
                QPushButton* theButton = new QPushButton(QString::fromStdString((*it)->_title + " >>"));
                _layoutNextStages->addWidget(theButton);
                _nextButtons.push_back(std::make_pair(theButton, (*it)->_id));

                _signalMapper->setMapping(theButton, (*it)->_id);
                connect(theButton, SIGNAL(clicked()), _signalMapper, SLOT(map()));
            }

161
162
163
164
165
166
167
168
169
            if (_stageHistory.empty()) {
                _btnPrevStage->setEnabled(false);
                _btnPrevStage->setText(tr("<< n/a"));
            }
            else {
                _btnPrevStage->setEnabled(true);
                _btnPrevStage->setText(tr("<< ") + QString::fromStdString(_workflow->getStage(_stageHistory.back())._title));
            }

170
171
172
173
174
175
176
177
178
            onStageAvailabilityChanged();
        }
    }

    void WorkflowControllerWidget::onStageChanged(int previousStage, int currentStage) {
        if (!_isBackWardsStep)
            _stageHistory.push_back(previousStage);
        _isBackWardsStep = false;

179
        QtJobProc.enqueueJob([&]() {
180
181
182
183
            populateNextStagesLayout();

            const AbstractWorkflow::Stage& s = _workflow->getCurrentStage();
            _lblWorkflowStage->setText(QString::fromStdString(s._title));
184
185
186
187

            for (auto it = s._pipelineCanvasVisibilities.begin(); it != s._pipelineCanvasVisibilities.end(); ++it) {
                _application->setPipelineVisibility(it->first, it->second);
            }
188
189
190
191
192
        });

    }

    void WorkflowControllerWidget::onStageAvailabilityChanged() {
193
194
195
196
197
198
199
200
201
202
        QtJobProc.enqueueJob([&]() {
            for (auto it = _nextButtons.begin(); it != _nextButtons.end(); ++it) {
                int theStage = it->second;
                it->first->setEnabled(_workflow->isStageAvailable(theStage));
            }
        });
    }

    QSize WorkflowControllerWidget::sizeHint() const {
        return QSize(300, 400);
203
204
205
    }

}