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

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

#include <QMenu>
#include <QSignalMapper>
35
36
37

namespace campvis {

38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
    MdiDockArea::MdiDockArea(QWidget* parent /*= 0*/)
        : QMdiArea(parent)
        , _menu(0)
        , _signalMapper(0)
    {
        _signalMapper = new QSignalMapper(this);
        this->setTabsClosable(true);
        this->setTabsMovable(true);
        this->setDocumentMode(true);

        // Menu setup
        _menu = new QMenu(tr("&Visualizations"));
        QActionGroup* displayStyleActions = new QActionGroup(this);

        QAction* displayTiledAction = displayStyleActions->addAction(tr("Display tiled"));
        displayTiledAction->setCheckable(true);
        displayTiledAction->setChecked(true);
        _menu->addAction(displayTiledAction);
        this->connect(displayTiledAction, SIGNAL(triggered()), SLOT(switchToTiledDisplay()));

        QAction* displayTabbedAction = displayStyleActions->addAction(tr("Display tabbed"));
        displayTabbedAction->setCheckable(true);
        _menu->addAction(displayTabbedAction);
        this->connect(displayTabbedAction, SIGNAL(triggered()), SLOT(switchToTabbedDisplay()));

        _menu->addSeparator();
    }

66
    MdiDockedWindow* MdiDockArea::addSubWindow(QWidget* widget, Qt::WindowFlags windowFlags /*= 0*/) {
67
        MdiDockedWindow* dockedWindow = new MdiDockedWindow(this, windowFlags);
68

69
        dockedWindow->setWidget(widget);
70
        this->addDockedWindow(dockedWindow);
71

72
73
74
75
76
77
78
79
80
81
82
83
        // Calling tileSubWindows() in TabbedView mode breaks the tabbed display
        if (this->viewMode() == QMdiArea::SubWindowView)
            this->tileSubWindows();

        QAction* visibilityAction = _menu->addAction(widget->windowTitle());
        visibilityAction->setCheckable(true);
        visibilityAction->setChecked(true);
        visibilityAction->setData(QVariant::fromValue(dynamic_cast<QWidget*>(widget)));

        connect(visibilityAction, SIGNAL(triggered()), _signalMapper, SLOT(map()));
        _signalMapper->setMapping(visibilityAction, visibilityAction);
        connect(_signalMapper, SIGNAL(mapped(QObject*)), SLOT(toggleSubWindowVisibility(QObject*)));
84

85
        return dockedWindow;
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
    QMenu* MdiDockArea::menu() {
        return _menu;
    }

    void MdiDockArea::addDockedWindow(MdiDockedWindow* dockedWindow) {
        QMdiArea::addSubWindow(dockedWindow);
        dockedWindow->show();

        connect(dockedWindow, SIGNAL(s_positionChanged(MdiDockedWindow*, const QPoint&)),
                SLOT(trackMdiSubWindowsPosition(MdiDockedWindow*, const QPoint&)));
    }

    void MdiDockArea::toggleSubWindowVisibility(QObject* actionObject) {
        /*
         * This static_cast is safe as toggleSubWindowVisibility() is a private slot to which only
         * MdiDockArea creates connections, always passing a QAction instance.
         */
        QAction* visibilityAction = static_cast<QAction*>(actionObject);
        QWidget* windowWidget = visibilityAction->data().value<QWidget*>()->parentWidget();

        if (MdiDockedWindow* dockedWindow = dynamic_cast<MdiDockedWindow*>(windowWidget)) {
            if (visibilityAction->isChecked())
                this->addDockedWindow(dockedWindow);
            else
                this->removeSubWindow(dockedWindow);

            // Calling tileSubWindows() in TabbedView mode breaks the tabbed display
            if (this->viewMode() == QMdiArea::SubWindowView)
                this->tileSubWindows();
        } else if (MdiFloatingWindow* floatingWindow = dynamic_cast<MdiFloatingWindow*>(windowWidget)) {
            if (visibilityAction->isChecked())
                floatingWindow->show();
            else
                floatingWindow->hide();
        }
        else
            tgtAssert(false, "Widget's parent is of unsupported type.");
    }

    void MdiDockArea::switchToTiledDisplay() {
        this->setViewMode(QMdiArea::SubWindowView);
        this->tileSubWindows();
    }

    void MdiDockArea::switchToTabbedDisplay() {
        this->setViewMode(QMdiArea::TabbedView);
    }

136
137
    void MdiDockArea::trackFloatingWindowsPosition(MdiFloatingWindow* floatingWindow, const QPoint& newPos) {
        const QRect& widgetGeometry = floatingWindow->frameGeometry();
138
139
140
        const QRect& mdiAreaRect = this->contentsRect();
        const QRect mdiAreaGeometry(this->mapToGlobal(mdiAreaRect.topLeft()),
                                    this->mapToGlobal(mdiAreaRect.bottomRight()));
141
142
        const QRect& intersection = widgetGeometry & mdiAreaGeometry;

143
        // Dock the floating window's widget if at least 60% of it is over the MDI area
144
145
        if (widgetGeometry.width() * widgetGeometry.height() * 3 <
                intersection.width() * intersection.height() * 5) {
146
            floatingWindow->stopWindowDrag();
147
            floatingWindow->hide();
148

149
            QWidget* widget = floatingWindow->widget();
150
151
            MdiDockedWindow* dockedWindow = new MdiDockedWindow();
            dockedWindow->setWidget(widget);
152
            dockedWindow->setWindowTitle(floatingWindow->windowTitle());
153
154

            this->addDockedWindow(dockedWindow);
155
            widget->show();
156
            floatingWindow->deleteLater();
157
            dockedWindow->forceWindowDrag();
158
159
160
        }
    }

161
    void MdiDockArea::trackMdiSubWindowsPosition(MdiDockedWindow *dockedWindow, const QPoint& /*newPos*/) {
162
        const QRect& subWindowGeometry = dockedWindow->frameGeometry();
163
        const QRect& mdiAreaGeometry = contentsRect();
164
165
        const QRect& intersection = subWindowGeometry & mdiAreaGeometry;

166
        // Detach the docked window's widget if at least 60% of it has left the MDI area
167
168
        if (subWindowGeometry.width() * subWindowGeometry.height() * 2 >
                intersection.width() * intersection.height() * 5) {
169
170
171
172
            QWidget* widget = dockedWindow->widget();
            dockedWindow->stopWindowDrag();
            dockedWindow->setWidget(0);
            removeSubWindow(dockedWindow);
173

174
            MdiFloatingWindow* floatingWindow = new MdiFloatingWindow(widget);
175
            floatingWindow->setWindowTitle(dockedWindow->windowTitle());
176
177
178
            floatingWindow->forceWindowDrag();
            connect(floatingWindow, SIGNAL(s_positionChanged(MdiFloatingWindow*, const QPoint&)),
                    this, SLOT(trackFloatingWindowsPosition(MdiFloatingWindow*, const QPoint&)));
179
180
181
182

            dockedWindow->deleteLater();
            tileSubWindows();
            floatingWindow->show();
183
        }
184
185
    }

186
}