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

mainwindow.cpp 13.6 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, 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 Universität München
//      Boltzmannstr. 3, 85748 Garching b. München, Germany
schultezub's avatar
schultezub committed
10
// For a full list of authors and contributors, please refer to the file "AUTHORS.txt".
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// 
// The licensing of this softare is not yet resolved. Until then, redistribution in source or
// binary forms outside the CAMP chair is not permitted, unless explicitly stated in legal form.
// However, the names of the original authors and the above copyright notice must retain in its
// original state in any case.
// 
// Legal disclaimer provided by the BSD license:
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 
// AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
// 
// ================================================================================================

30
31
32
#include "mainwindow.h"

#include "tgt/assert.h"
schultezub's avatar
schultezub committed
33
#include "application/campvisapplication.h"
34
#include "application/gui/datacontainerinspectorwidget.h"
35
#include "application/gui/datacontainerinspectorcanvas.h"
36
#include "application/gui/mdi/mdidockablewindow.h"
37
#include "application/gui/qtdatahandle.h"
38
#include "core/datastructures/datacontainer.h"
39
40
#include "core/pipeline/abstractpipeline.h"
#include "core/pipeline/abstractprocessor.h"
41
42
#include "core/tools/stringutils.h"
#include "modules/pipelinefactory.h"
43

44
#include <QScrollBar>
Christian Schulte zu Berge's avatar
Christian Schulte zu Berge committed
45

46

schultezub's avatar
schultezub committed
47
namespace campvis {
48

49
    MainWindow::MainWindow(CampVisApplication* application)
50
51
        : QMainWindow()
        , _application(application)
52
        , _mdiArea(0)
53
54
55
        , _containerWidget(0)
        , _cbPipelineFactory(0)
        , _btnPipelineFactory(0)
56
        , _pipelineWidget(0)
57
58
        , _propCollectionWidget(0)
        , _dcInspectorWidget(0)
59
        , _dcInspectorDock(0)
60
61
62
63
        , _btnExecute(0)
        , _btnShowDataContainerInspector(0)
        , _selectedPipeline(0)
        , _selectedProcessor(0)
64
        , _selectedDataContainer(0)
65
        , _logViewer(0)
66
67
    {
        tgtAssert(_application != 0, "Application must not be 0.");
68
        ui.setupUi(this);
69
70
71
72
        setup();
    }

    MainWindow::~MainWindow() {
schultezub's avatar
schultezub committed
73
        _application->s_PipelinesChanged.disconnect(this);
74
        _application->s_DataContainersChanged.disconnect(this);
75
        delete _dcInspectorWidget;
76
77
78
    }

    void MainWindow::setup() {
79
80
        qRegisterMetaType<QtDataHandle>("QtDataHandle");

81
        setCorner(Qt::BottomLeftCorner, Qt::LeftDockWidgetArea);
82
        setCorner(Qt::TopLeftCorner, Qt::LeftDockWidgetArea);
83
        setCorner(Qt::BottomRightCorner, Qt::RightDockWidgetArea);
84
        setCorner(Qt::TopRightCorner, Qt::RightDockWidgetArea);
85

86
        setTabPosition(Qt::TopDockWidgetArea, QTabWidget::North);
87

88
        _mdiArea = new MdiDockArea();
89
90
91
        _mdiArea->tileSubWindows();
        setCentralWidget(_mdiArea);

92
93
94
95
96
97
98
99
100
101
102
103
        _containerWidget = new QWidget(this);
        QGridLayout* _cwLayout = new QGridLayout(_containerWidget);

        _cbPipelineFactory = new QComboBox(_containerWidget);
        std::vector<std::string> registeredPipelines = PipelineFactory::getRef().getRegisteredPipelines();
        for (std::vector<std::string>::const_iterator it = registeredPipelines.begin(); it != registeredPipelines.end(); ++it)
            _cbPipelineFactory->addItem(QString::fromStdString(*it));
        _cwLayout->addWidget(_cbPipelineFactory, 0, 0);

        _btnPipelineFactory = new QPushButton("Add Pipeline", _containerWidget);
        _cwLayout->addWidget(_btnPipelineFactory, 0, 1);

104
        _pipelineWidget = new PipelineTreeWidget(this);
105
106
107
108
109
        _containerWidget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum);
        _cwLayout->addWidget(_pipelineWidget, 1, 0, 1, 2);

        _containerWidget->setLayout(_cwLayout);
        ui.pipelineTreeDock->setWidget(_containerWidget);
110

111
        _pipelinePropertiesScrollArea = new QScrollArea(this);
112
113
114
115
        _pipelinePropertiesScrollArea->setWidgetResizable(true);
        _pipelinePropertiesScrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
        _pipelinePropertiesScrollArea->setFrameStyle(QScrollArea::NoFrame);

116
        _pipelinePropertiesWidget = new QWidget(_pipelinePropertiesScrollArea);
117
118
119
        _pipelinePropertiesWidget->installEventFilter(this);
        _pipelinePropertiesScrollArea->setWidget(_pipelinePropertiesWidget);
        ui.pipelinePropertiesDock->setWidget(_pipelinePropertiesScrollArea);
120

121
        QVBoxLayout* rightLayout = new QVBoxLayout(_pipelinePropertiesWidget);
122
        rightLayout->setSpacing(4);
123
        _pipelinePropertiesWidget->setLayout(rightLayout);
124

125
        _btnExecute = new QPushButton("Execute Selected Pipeline/Processor", _pipelinePropertiesWidget);
126
127
        rightLayout->addWidget(_btnExecute);

128
        _btnShowDataContainerInspector = new QPushButton("Inspect DataContainer of Selected Pipeline", _pipelinePropertiesWidget);
129
        rightLayout->addWidget(_btnShowDataContainerInspector);
130

131
        _propCollectionWidget = new PropertyCollectionWidget(this);
132
        rightLayout->addWidget(_propCollectionWidget);
133
        rightLayout->addStretch();
134

135
        _logViewer = new LogViewerWidget(this);
136
        ui.logViewerDock->setWidget(_logViewer);
137

138
        _dcInspectorWidget = new DataContainerInspectorWidget();
139
        this->populateMainMenu();
140
141

        connect(
142
143
            this, SIGNAL(updatePipelineWidget(const std::vector<DataContainer*>&, const std::vector<AbstractPipeline*>&)), 
            _pipelineWidget, SLOT(update(const std::vector<DataContainer*>&, const std::vector<AbstractPipeline*>&)));
144
145
146
147
        connect(
            _pipelineWidget, SIGNAL(clicked(const QModelIndex&)), 
            this, SLOT(onPipelineWidgetItemClicked(const QModelIndex&)));
        connect(
148
149
            this, SIGNAL(updatePropCollectionWidget(HasPropertyCollection*, DataContainer*)),
            _propCollectionWidget, SLOT(updatePropCollection(HasPropertyCollection*, DataContainer*)));
150
151
152
153
154
155
        connect(
            _btnExecute, SIGNAL(clicked()), 
            this, SLOT(onBtnExecuteClicked()));
        connect(
            _btnShowDataContainerInspector, SIGNAL(clicked()), 
            this, SLOT(onBtnShowDataContainerInspectorClicked()));
156
157
158
159
        connect(
            _btnPipelineFactory, SIGNAL(clicked()), 
            this, SLOT(onBtnPipelineFactoryClicked()));

160
        _application->s_PipelinesChanged.connect(this, &MainWindow::onPipelinesChanged);
161
        _application->s_DataContainersChanged.connect(this, &MainWindow::onDataContainersChanged);
162
163
    }

164
    void MainWindow::populateMainMenu() {
165
        // Populate the file menu
166
167
168
169
        QMenuBar* menuBar = this->menuBar();
        QMenu* fileMenu = menuBar->addMenu(tr("&File"));
        fileMenu->addAction(tr("&Quit"), qApp, SLOT(closeAllWindows()), QKeySequence(Qt::CTRL + Qt::Key_Q));

170
171
172
173
174
175
176
177
178
179
        // Populate the visualizations menu
        QMenu* visualizationsMenu = _mdiArea->menu();
        visualizationsMenu->setTitle(tr("&Visualizations"));
        menuBar->addMenu(visualizationsMenu);

        // Populate the tools menu
        QMenu* toolsMenu = menuBar->addMenu(tr("&Tools"));
        toolsMenu->addAction(ui.pipelineTreeDock->toggleViewAction());
        toolsMenu->addAction(ui.pipelinePropertiesDock->toggleViewAction());
        toolsMenu->addAction(ui.logViewerDock->toggleViewAction());
180
181
    }

182
183
184
185
186
187
188
189
190
    bool MainWindow::eventFilter(QObject* watched, QEvent* event) {
        if (watched == _pipelinePropertiesWidget && event->type() == QEvent::Resize) {
            _pipelinePropertiesScrollArea->setMinimumWidth(_pipelinePropertiesWidget->minimumSizeHint().width() +
                                                           _pipelinePropertiesScrollArea->verticalScrollBar()->width());
        }

        return false;
    }

191
    void MainWindow::onPipelinesChanged() {
192
193
194
195
196
        emit updatePipelineWidget(_application->_dataContainers, _application->_pipelines);
    }

    void MainWindow::onDataContainersChanged() {
        emit updatePipelineWidget(_application->_dataContainers, _application->_pipelines);
197
198
    }

199
200
201
202
    void MainWindow::onPipelineWidgetItemClicked(const QModelIndex& index) {
        if (index.isValid()) {
            // Yak, this is so ugly - another reason why GUI programming sucks...
            QVariant item = index.data(Qt::UserRole);
203
204
            if (item.isValid()) {
                HasPropertyCollection* ptr = static_cast<HasPropertyCollection*>(item.value<void*>());
205

206
                if (AbstractPipeline* pipeline = dynamic_cast<AbstractPipeline*>(ptr)) {
207
                    _selectedPipeline = pipeline;
208
                    _selectedProcessor = 0;
209
                    _selectedDataContainer = &pipeline->getDataContainer();
210
211
212
213
214
215
216
217
                }
                else if (AbstractProcessor* processor = dynamic_cast<AbstractProcessor*>(ptr)) {
                    _selectedProcessor = processor;

                    QVariant parentItem = index.parent().data(Qt::UserRole);
                    HasPropertyCollection* pptr = static_cast<HasPropertyCollection*>(parentItem.value<void*>());
                    if (AbstractPipeline* pipeline = dynamic_cast<AbstractPipeline*>(pptr)) {
                        _selectedPipeline = pipeline;
218
                        _selectedDataContainer = &pipeline->getDataContainer();
219
                    }
220
221
                }

222
223
224
225
                emit updatePropCollectionWidget(ptr, &_selectedPipeline->getDataContainer());
            }
            else {
                emit updatePropCollectionWidget(0, 0);
226
                _selectedDataContainer = 0;
227
            }
228
229
        }
        else {
230
            emit updatePropCollectionWidget(0, 0);
231
            _selectedDataContainer = 0;
232
233
234
        }
    }

235
    QSize MainWindow::sizeHint() const {
236
        return QSize(1000, 600);
237
238
    }

239
240
241
242
    void MainWindow::onBtnExecuteClicked() {
        if (_selectedProcessor != 0 && _selectedPipeline != 0) {
            // this is not as trivial as it seems:
            // We need the pipeline, probably an OpenGL context...
243
            _selectedProcessor->invalidate(AbstractProcessor::INVALID_RESULT);
244
245
        }
        else if (_selectedPipeline != 0) {
246
247
248
            for (std::vector<AbstractProcessor*>::const_iterator it = _selectedPipeline->getProcessors().begin(); it != _selectedPipeline->getProcessors().end(); ++it) {
                (*it)->invalidate(AbstractProcessor::INVALID_RESULT);
            }
249
250
251
252
253
        }
    }

    void MainWindow::onBtnShowDataContainerInspectorClicked() {
        if (_selectedPipeline != 0) {
254
255
256
257
258
259
260
261
            if (_dcInspectorDock == 0) {
                _dcInspectorDock = dockPrimaryWidget("Data Container inspector", _dcInspectorWidget);
            } else {
                // Activate the dock's tab
                _dcInspectorDock->setVisible(true);
                _dcInspectorDock->raise();
            }

262
263
264
265
            _dcInspectorWidget->setDataContainer(&(_selectedPipeline->getDataContainer()));
        }
    }

266
    void MainWindow::init() {
267
268
        if (_dcInspectorWidget != 0)
            _dcInspectorWidget->init();
269

270
        _logViewer->init();
271
272
273
    }

    void MainWindow::deinit() {
274
275
        if (_dcInspectorWidget != 0)
            _dcInspectorWidget->deinit();
276

277
        _logViewer->deinit();
278
279
    }

280
    void MainWindow::addVisualizationPipelineWidget(const std::string& name, QWidget* canvas) {
281
        MdiDockableWindow* dockableWindow = _mdiArea->addWidget(canvas);
282
        const QString& windowTitle = QString::fromStdString(name);
283
284
        dockableWindow->setWindowTitle(windowTitle);
        dockableWindow->show();
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
    }

    QDockWidget* MainWindow::dockPrimaryWidget(const std::string& name, QWidget* widget) {
        QDockWidget* dockWidget = new QDockWidget(QString::fromStdString(name));
        dockWidget->setWidget(widget);
        dockWidget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding);

        if (_primaryDocks.empty()) {
            addDockWidget(Qt::TopDockWidgetArea, dockWidget);
        } else {
            tabifyDockWidget(_primaryDocks.back(), dockWidget);
            // Activate the dock's tab
            dockWidget->setVisible(true);
            dockWidget->raise();
        }

        _primaryDocks.push_back(dockWidget);
        return dockWidget;
303
304
    }

305
306
307
308
309
310
311
312
313
314
    void MainWindow::onBtnPipelineFactoryClicked() {
        std::string name = this->_cbPipelineFactory->currentText().toStdString();
        DataContainer* dc = _selectedDataContainer;
        if (dc == 0) {
            dc = _application->createAndAddDataContainer("DataContainer #" + StringUtils::toString(_application->_dataContainers.size() + 1));
        }
        AbstractPipeline* p = PipelineFactory::getRef().createPipeline(name, dc);
        _application->addPipeline(name, p);
    }

315
}