File: speed_dial.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 (463 lines) | stat: -rw-r--r-- 15,802 bytes parent folder | download | duplicates (2)
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/************************************************************************
 *
 * Copyright (C) 2023-2024 IRCAD France
 *
 * 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 "speed_dial.hpp"

#include <core/spy_log.hpp>

#include <QEvent>
#include <QLayout>
#include <QParallelAnimationGroup>
#include <QPropertyAnimation>

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

speed_dial::speed_dial(enum direction _direction, int _spacing, int _animation_duration, QWidget* _parent) :
    QPushButton(_parent),
    m_direction(_direction),
    m_spacing(_spacing),
    m_animation_duration(_animation_duration),
    m_animation_group(new QParallelAnimationGroup)
{
    setEnabled(false);
    setCheckable(true);
    m_actions_container->setObjectName(objectName() + "Actions");
    infect(_parent);
    QObject::connect(this, &QPushButton::clicked, this, &speed_dial::toggle_fold);
    QObject::connect(
        m_animation_group,
        &QAnimationGroup::finished,
        [this]
        {
            if(m_folded)
            {
                m_actions_container->hide();
            }
        });
    QObject::connect(
        this,
        &QObject::objectNameChanged,
        [this](QString)
        {
            m_actions_container->setObjectName(objectName() + "Actions");
        });
}

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

void speed_dial::set_direction(enum direction _direction)
{
    m_direction = _direction;
}

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

enum speed_dial::direction speed_dial::direction() const
{
    return m_direction;
}

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

void speed_dial::set_spacing(int _spacing)
{
    m_spacing = _spacing;
}

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

int speed_dial::spacing() const
{
    return m_spacing;
}

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

void speed_dial::set_animation_duration(int _animation_duration)
{
    // It seems there is a bug when animationDuration=1; QAbstractAnimation::finished doesn't trigger, and as such the
    // buttons don't hide when folded.
    if(_animation_duration == 1)
    {
        m_animation_duration = 0;
    }
    else
    {
        m_animation_duration = _animation_duration;
    }
}

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

int speed_dial::animation_duration() const
{
    return m_animation_duration;
}

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

void speed_dial::fold()
{
    if(m_folded)
    {
        return;
    }

    m_folded = true;
    setChecked(false);
    if(m_actions_container == nullptr)
    {
        return;
    }

    m_animation_group->setDirection(QAbstractAnimation::Backward);
    m_animation_group->start();
}

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

void speed_dial::unfold()
{
    if(!m_folded)
    {
        return;
    }

    if(m_actions.empty())
    {
        return;
    }

    m_folded = false;
    setChecked(true);
    m_animation_group->setDirection(QAbstractAnimation::Forward);
    m_actions_container->show();
    m_animation_group->start();
}

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

void speed_dial::toggle_fold()
{
    if(m_folded)
    {
        unfold();
    }
    else
    {
        fold();
    }
}

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

bool speed_dial::is_folded() const
{
    return m_folded;
}

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

void speed_dial::update_actions(std::vector<QWidget*> _actions)
{
    m_actions = std::move(_actions);
    setEnabled(!m_actions.empty());

    // Update the action container
    for(QObject* obj : m_actions_container->children())
    {
        if(auto* widget = qobject_cast<QWidget*>(obj);
           widget != nullptr && std::ranges::find(m_actions, obj) == m_actions.end())
        {
            widget->hide();
            widget->setParent(nullptr);
        }
    }

    for(QWidget* w : m_actions)
    {
        w->setParent(m_actions_container);
        w->show();
    }

    // Update the animations
    int current_time = m_animation_group->currentTime(); // Current time get resetted when calling
                                                         // QAnimationGroup::clear
    m_animation_group->clear();
    auto* container_animation = new QPropertyAnimation(m_actions_container, "geometry");
    container_animation->setDuration(m_animation_duration);
    container_animation->setCurrentTime(current_time);
    m_animation_group->addAnimation(container_animation);
    for(QWidget* w : m_actions)
    {
        auto* animation = new QPropertyAnimation(w, "pos");
        animation->setDuration(m_animation_duration);
        animation->setCurrentTime(current_time);
        m_animation_group->addAnimation(animation);
    }

    // Ensure the actual size of the widgets are computed; this is needed by computePositions
    std::ranges::for_each(m_actions, &QWidget::adjustSize);
    if(parentWidget() != nullptr)
    {
        update_actions_positions();
    }
}

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

const std::vector<QWidget*>& speed_dial::actions() const
{
    return m_actions;
}

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

bool speed_dial::eventFilter(QObject* _o, QEvent* _e)
{
    QWidget* w = qobject_cast<QWidget*>(_o);
    if(w == nullptr)
    {
        return false;
    }

    // The positions of the actions must be updated if:
    // - The size of one of the actions changes
    // - One of the ancestors of the speed dial is resized of moves
    if((std::ranges::find(m_actions, w) != m_actions.end() && _e->type() == QEvent::Resize)
       || (_o->findChildren<speed_dial*>(objectName()).contains(this)
           && (_e->type() == QEvent::Resize || _e->type() == QEvent::Move)))
    {
        update_actions_positions();
    }

    return false;
}

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

bool speed_dial::event(QEvent* _e)
{
    if(_e->type() == QEvent::ParentChange)
    {
        infect(parent());
        m_actions_container->setParent(window());
    }

    return QPushButton::event(_e);
}

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

void speed_dial::moveEvent(QMoveEvent* _e)
{
    QPushButton::moveEvent(_e);
    update_actions_positions();
}

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

void speed_dial::resizeEvent(QResizeEvent* _e)
{
    QPushButton::resizeEvent(_e);
    update_actions_positions();
    bool is_in_an_overlay = false;
    for(QWidget* child = this ; !is_in_an_overlay && child->parentWidget() != nullptr ; child = child->parentWidget())
    {
        QLayout* parent_layout = child->parentWidget()->layout();
        is_in_an_overlay = (parent_layout == nullptr || parent_layout->indexOf(child) == -1);
    }

    if(is_in_an_overlay)
    {
        setIconSize(size());
    }
}

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

void speed_dial::infect(QObject* _parent)
{
    if(_parent == nullptr)
    {
        return;
    }

    _parent->installEventFilter(this);
    infect(_parent->parent());
}

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

void speed_dial::update_actions_positions()
{
    if(m_actions_container == nullptr || m_animation_group == nullptr)
    {
        return;
    }

    // Either one of the actions was resized or one of the ancestor was resized or moved. Either way, the position and
    // size of the container is now invalid and must be changed.
    SIGHT_ASSERT(
        "There must be as much animations as there are actions (+ 1 animation because of the container)",
        static_cast<std::size_t>(m_animation_group->animationCount()) == m_actions.size() + 1
    );
    // computePositions will give us the positions and sizes of the container and the positions of the actions. They are
    // provided as a pair, where the first element is the initial value and the second element is the final value.
    auto [containerRect, actionsPositions] = compute_positions();
    // In the animation group, there must be precisely as much as animations as action, plus the animation of the
    // container, which is the first element. See updateActions, where these animations are created.
    auto* container_animation = qobject_cast<QPropertyAnimation*>(m_animation_group->animationAt(0));
    SIGHT_ASSERT(
        "All animations inside the animation group must be QPropertyAnimation",
        container_animation != nullptr
    );
    container_animation->setStartValue(containerRect.first);
    container_animation->setEndValue(containerRect.second);
    m_actions_container->setGeometry(container_animation->currentValue().toRect());
    for(std::size_t i = 0 ; i < actionsPositions.size() ; i++)
    {
        auto* animation = qobject_cast<QPropertyAnimation*>(m_animation_group->animationAt(static_cast<int>(i) + 1));
        SIGHT_ASSERT("All animations inside the animation group must be QPropertyAnimation", animation != nullptr);
        animation->setStartValue(actionsPositions[i].first);
        animation->setEndValue(actionsPositions[i].second);
        m_actions[i]->move(animation->currentValue().toPoint());
    }
}

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

speed_dial::animations speed_dial::compute_positions() const
{
    std::pair<QRect, QRect> container_rect;
    QPoint speed_dial_position_on_window = mapTo(window(), parentWidget()->pos());
    QPoint container_position            = speed_dial_position_on_window;
    QSize container_final_size;
    // Firstly, we must compute what will be the initial container position and the  final container size. If the
    // direction is vertical (either up or down), the width is determined by the "fattest" action, and the height is the
    // sum of the height of all the actions including the spacing, and if the direction is horizontal, the width is the
    // sum of the width of all the actions including the spacing, and the height is the height of the biggest action.
    // In order for the container to be centered on the speed dial, its position is adapted. The container won't however
    // exceed the window.
    if(m_direction == direction::up || m_direction == direction::down)
    {
        int fattest_widget_width = 0;
        int total_height         = static_cast<int>(m_actions.size() - 1) * m_spacing;
        for(const QWidget* w : m_actions)
        {
            fattest_widget_width = std::max(fattest_widget_width, w->width());
            total_height        += w->height();
        }

        container_final_size.setWidth(fattest_widget_width);
        container_final_size.setHeight(total_height);
        container_position.setX(
            std::clamp(
                speed_dial_position_on_window.x() + (width() - fattest_widget_width) / 2,
                0,
                window()->width() - fattest_widget_width
            )
        );
    }
    else
    {
        int biggest_widget_height = 0;
        int total_width           = static_cast<int>(m_actions.size() - 1) * m_spacing;
        for(const QWidget* w : m_actions)
        {
            biggest_widget_height = std::max(biggest_widget_height, w->height());
            total_width          += w->width();
        }

        container_final_size.setWidth(total_width);
        container_final_size.setHeight(biggest_widget_height);
        container_position.setY(
            std::clamp(
                speed_dial_position_on_window.y() + (height() - biggest_widget_height) / 2,
                0,
                window()->height() - biggest_widget_height
            )
        );
    }

    container_rect.first = QRect(container_position, size());
    // Then, we compute the final position of the container. In the initial position, the container overlaps with the
    // speed dial, while in its final position, it must be aside. The spacing must be applied between the speed dial and
    // the container (it will effectively be the spacing between the speed dial and the first button). Since in Qt the
    // position of a widget is determined by its top left point, there are two main cases: we are either in the
    // "positive" world (down and right), where we must add the width/height of the speed dial to its position, or in
    // the "negative" world (up and left), where we must substract the final width/height of the container to its
    // position.
    QPoint delta;
    if(m_direction == direction::up)
    {
        delta = QPoint(0, -container_final_size.height() - m_spacing);
    }
    else if(m_direction == direction::left)
    {
        delta = QPoint(-container_final_size.width() - m_spacing, 0);
    }
    else if(m_direction == direction::down)
    {
        delta = QPoint(0, height() + m_spacing);
    }
    else
    {
        delta = QPoint(width() + m_spacing, 0);
    }

    container_rect.second = QRect(container_position + delta, container_final_size);
    // Finally, we compute the position of the actions within the container. The actions must be centered in the
    // container, and an offset must be calculated to place them evenly within the container. If the direction is
    // vertical (up or down), x is calculated so that the action is in the center of the container, which is the width
    // of the container minus its width. Its y (offset) will be the cumulated height of the previous widgets including
    // spacing. If the direction is horizontal (left or right), y is the height of the container minus its height, and
    // its x (offset) is the cumulated width of the previous widgets including spacing.
    std::vector<std::pair<QPoint, QPoint> > actions_positions;
    int cumulated_size = 0;
    for(int i = 0 ; const QWidget * w : m_actions)
    {
        std::pair<QPoint, QPoint> action_positions;
        int offset = cumulated_size + i * m_spacing;
        if(m_direction == direction::up || m_direction == direction::down)
        {
            int x = container_final_size.width() - w->width();
            action_positions.first  = QPoint(x, 0);
            action_positions.second = QPoint(x, offset);
        }
        else
        {
            int y = container_final_size.height() - w->height();
            action_positions.first  = QPoint(0, y);
            action_positions.second = QPoint(offset, y);
        }

        actions_positions.push_back(action_positions);

        cumulated_size += m_direction == direction::up || m_direction == direction::down ? w->height() : w->width();
        i++;
    }

    return {container_rect, actions_positions};
}

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