File: slide_bar.cpp

package info (click to toggle)
sight 25.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 43,108 kB
  • sloc: cpp: 306,170; xml: 18,037; ansic: 9,960; python: 1,379; sh: 144; makefile: 33
file content (345 lines) | stat: -rw-r--r-- 9,994 bytes parent folder | download
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
28
29
30
31
32
33
34
35
36
37
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/************************************************************************
 *
 * Copyright (C) 2016-2023 IRCAD France
 * Copyright (C) 2016-2020 IHU Strasbourg
 *
 * This file is part of Sight.
 *
 * Sight is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Sight is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with Sight. If not, see <https://www.gnu.org/licenses/>.
 *
 ***********************************************************************/

#include "ui/qt/widget/slide_bar.hpp"

#include "ui/qt/qt_main_frame.hpp"

#include <core/spy_log.hpp>

#include <QParallelAnimationGroup>
#include <QPropertyAnimation>
#include <QWindow>

namespace sight::ui::qt::widget
{

//-----------------------------------------------------------------------------

slide_bar::slide_bar(
    QWidget* _parent,
    h_alignment _h_align,
    v_alignment _v_align,
    int _width,
    bool _percent_width,
    int _height,
    bool _percent_height,
    int _h_offset,
    bool _percent_h_offset,
    int _v_offset,
    bool _percent_v_offset,
    double _opacity,
    bool _animatable,
    animatable_alignment _animatable_alignment
) :
    QWidget(_parent),
    m_h_alignment(_h_align),
    m_v_alignment(_v_align),
    m_width(_width),
    m_percent_width(_percent_width),
    m_height(_height),
    m_percent_height(_percent_height),
    m_h_offset(_h_offset),
    m_percent_h_offset(_percent_h_offset),
    m_v_offset(_v_offset),
    m_percent_v_offset(_percent_v_offset),
    m_opacity(_opacity),
    m_animatable(_animatable),
    m_animatable_alignment(_animatable_alignment)
{
    this->init();
}

//-----------------------------------------------------------------------------

void slide_bar::init()
{
    this->setObjectName("slide_bar");

    // Set the widget position
    this->update_position();

    // window flags to have a frameless dialog that can be displayed over an openGL widget
    this->setWindowFlags(
        Qt::Tool
        | Qt::FramelessWindowHint
        | Qt::NoDropShadowWindowHint
    );

    // Set the transparent background
    this->setAttribute(Qt::WA_TranslucentBackground);

    // Listen parent and activeWindow 'Resize' and 'Move' event to update widget position
    // (just listening parent event is not enough because the Move event is only send on activeWindow).
    this->parent()->installEventFilter(this);

    // Find the real root (when using opengl/ogre, there are intermediate "QWindow")
    QWidget* ancestor = this->parentWidget();
    while(ancestor->parentWidget() != nullptr)
    {
        ancestor = ancestor->parentWidget();
    }

    ancestor = ancestor->topLevelWidget();
    ancestor->installEventFilter(this);
}

//-----------------------------------------------------------------------------

slide_bar::~slide_bar()
= default;

//-----------------------------------------------------------------------------

void slide_bar::update_position()
{
    // Computes the size.
    int width  = this->parentWidget()->width();
    int height = this->parentWidget()->height();

    if(!m_percent_width)
    {
        if(m_width > -1)
        {
            width = std::min(width, m_width);
        }
    }
    else
    {
        width = std::min(width, static_cast<int>(m_width / 100. * width));
    }

    if(!m_percent_height)
    {
        if(m_height > -1)
        {
            height = std::min(height, m_height);
        }
    }
    else
    {
        height = std::min(height, static_cast<int>(m_height / 100. * height));
    }

    this->setFixedWidth(width);
    this->setFixedHeight(height);

    // Computes the offset.
    int h_offset = 0;
    int v_offset = 0;

    if(!m_percent_h_offset)
    {
        h_offset = m_h_offset;
    }
    else
    {
        h_offset = static_cast<int>(m_h_offset / 100. * this->parentWidget()->width());
    }

    if(!m_percent_v_offset)
    {
        v_offset = m_v_offset;
    }
    else
    {
        v_offset = static_cast<int>(m_v_offset / 100. * this->parentWidget()->height());
    }

    // Compute the shown and hidden position.
    if(m_h_alignment == left && m_v_alignment == top)
    {
        QPoint pos = this->parentWidget()->rect().topLeft();
        pos = this->parentWidget()->mapToGlobal(pos);

        m_shown_position = QRect(pos.x() + h_offset, pos.y() + v_offset, width, height);
    }
    else if(m_h_alignment == right && m_v_alignment == top)
    {
        QPoint pos = this->parentWidget()->rect().topRight();
        pos = this->parentWidget()->mapToGlobal(pos);

        m_shown_position = QRect(pos.x() - width - h_offset, pos.y() + v_offset, width, height);
    }
    else if(m_h_alignment == left && m_v_alignment == bottom)
    {
        QPoint pos = this->parentWidget()->rect().bottomLeft();
        pos = this->parentWidget()->mapToGlobal(pos);

        m_shown_position = QRect(pos.x() + h_offset, pos.y() - height - v_offset, width, height);
    }
    else if(m_h_alignment == right && m_v_alignment == bottom)
    {
        QPoint pos = this->parentWidget()->rect().bottomRight();
        pos = this->parentWidget()->mapToGlobal(pos);

        m_shown_position = QRect(pos.x() - width - h_offset, pos.y() - height - v_offset, width, height);
    }

    switch(m_animatable_alignment)
    {
        case top_animation:
        {
            QPoint pos = this->parentWidget()->rect().topLeft();
            pos               = this->parentWidget()->mapToGlobal(pos);
            m_hidden_position = QRect(m_shown_position.x(), pos.y() - height + v_offset, width, 0);
            break;
        }

        case bottom_animation:
        {
            QPoint pos = this->parentWidget()->rect().bottomLeft();
            pos               = this->parentWidget()->mapToGlobal(pos);
            m_hidden_position = QRect(m_shown_position.x(), pos.y() + v_offset, width, 0);
            break;
        }

        case left_animation:
        {
            QPoint pos = this->parentWidget()->rect().topLeft();
            pos               = this->parentWidget()->mapToGlobal(pos);
            m_hidden_position = QRect(pos.x() - width + h_offset, m_shown_position.y(), 0, height);
            break;
        }

        case right_animation:
        {
            QPoint pos = this->parentWidget()->rect().bottomRight();
            pos               = this->parentWidget()->mapToGlobal(pos);
            m_hidden_position = QRect(pos.x() + h_offset, m_shown_position.y(), 0, height);
            break;
        }
    }

    if(!m_animatable || m_is_shown)
    {
        this->setGeometry(m_shown_position);
    }
    else
    {
        this->setGeometry(m_hidden_position);
    }
}

//-----------------------------------------------------------------------------

void slide_bar::setVisible(bool _visible)
{
    this->slide(_visible);
}

//-----------------------------------------------------------------------------

void slide_bar::force_hide()
{
    this->QWidget::setVisible(false);
}

//-----------------------------------------------------------------------------

void slide_bar::force_show()
{
    this->QWidget::setVisible(true);
}

//-----------------------------------------------------------------------------

void slide_bar::slide(bool _visible)
{
    if(m_animatable)
    {
        // Show the widget with the previous opacity. It must be hidden after the slide(false) because if opacity == 0,
        // the widget is still clickable.
        this->force_show();
        this->setWindowOpacity(m_is_shown ? m_opacity : 0);

        // Set animation to slide the widget and update the opacity
        auto* animations = new QParallelAnimationGroup();

        // slide animation
        auto* geom_animation = new QPropertyAnimation(this, "geometry");
        geom_animation->setDuration(500);
        geom_animation->setEasingCurve(QEasingCurve::InBack);
        geom_animation->setStartValue(this->geometry());

        if(_visible)
        {
            geom_animation->setEndValue(m_shown_position);
        }
        else
        {
            geom_animation->setEndValue(m_hidden_position);

            // hide the widget when the animation is finished (if opacity == 0, widget is still clickable)
            QObject::connect(animations, &QAbstractAnimation::finished, this, &slide_bar::force_hide);
        }

        // opacity animation
        auto* opacity_animation = new QPropertyAnimation(this, "windowOpacity");
        opacity_animation->setDuration(500);
        opacity_animation->setEndValue(_visible ? m_opacity : 0);

        animations->addAnimation(geom_animation);
        animations->addAnimation(opacity_animation);

        animations->start(QPropertyAnimation::DeleteWhenStopped);
    }
    else if(_visible)
    {
        this->force_show();
    }
    else
    {
        this->force_hide();
    }

    m_is_shown = _visible;
}

//-----------------------------------------------------------------------------

bool slide_bar::eventFilter(QObject* _obj, QEvent* _event)
{
    // Update the widget position when the parent is moved or resized
    if(_event->type() == QEvent::Resize
       || _event->type() == QEvent::Move)
    {
        this->update_position();
    }
    else if(_event->type() == QEvent::Show)
    {
        if(m_is_shown)
        {
            this->force_show();
        }
    }
    else if(_event->type() == QEvent::Hide)
    {
        this->force_hide();
    }

    return QObject::eventFilter(_obj, _event);
}

//-----------------------------------------------------------------------------

} // namespace sight::ui::qt::widget