File: WidgetMoverButton.cpp

package info (click to toggle)
bornagain 23.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 103,936 kB
  • sloc: cpp: 423,131; python: 40,997; javascript: 11,167; awk: 630; sh: 318; ruby: 173; xml: 130; makefile: 51; ansic: 24
file content (173 lines) | stat: -rw-r--r-- 6,616 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
//  ************************************************************************************************
//
//  BornAgain: simulate and fit reflection and scattering
//
//! @file      GUI/View/Widget/WidgetMoverButton.cpp
//! @brief     Implements class WidgetMoverButton.
//!
//! @homepage  http://www.bornagainproject.org
//! @license   GNU General Public License v3 or higher (see COPYING)
//! @copyright Forschungszentrum Jülich GmbH 2021
//! @authors   Scientific Computing Group at MLZ (see CITATION, AUTHORS)
//
//  ************************************************************************************************

#include "GUI/View/Widget/WidgetMoverButton.h"
#include "Base/Util/Assert.h"
#include <QLayout>
#include <QMouseEvent>
#include <QScrollBar>

WidgetMoverButton::WidgetMoverButton(QWidget* parent, QWidget* widgetToMove, int ignoreOnTop)
    : QToolButton(parent)
    , m_widget_to_move(widgetToMove)
    , m_drop_above_this_widget(nullptr)
    , m_ignore_on_top(ignoreOnTop)
    , m_scroll_area(nullptr)
{
    setIcon(QIcon(":/images/move_up_down.svg"));
    m_drag_scroll_timer.setSingleShot(false);
}

void WidgetMoverButton::mousePressEvent(QMouseEvent* event)
{
    if (m_scroll_area == nullptr) {
        QWidget* p = parentWidget();
        do {
            m_scroll_area = dynamic_cast<QScrollArea*>(p);
            p = p->parentWidget();
        } while (p != nullptr && m_scroll_area == nullptr);
    }

    ASSERT(m_scroll_area);
    m_global_mouse_downY = event->globalY();
    m_hot_spot =
        event->globalPos()
        - m_widget_to_move->parentWidget()->mapToGlobal(m_widget_to_move->geometry().topLeft());
    m_pressed = true;
}

void WidgetMoverButton::mouseReleaseEvent(QMouseEvent*)
{
    if (!m_pressed)
        return;

    qDeleteAll(m_animations.values());
    m_animations.clear();

    m_drag_scroll_timer.stop();
    m_pressed = false;
    m_started = false;
    if (m_layout_to_deactivate != nullptr) {
        m_layout_to_deactivate->setEnabled(true);
        m_layout_to_deactivate->update();
    }
    emit finishedMoving(m_widget_to_move, m_drop_above_this_widget);
}

void WidgetMoverButton::mouseMoveEvent(QMouseEvent* event)
{
    if (!m_pressed)
        return;

    int dy = event->globalY() - m_global_mouse_downY;
    if (abs(dy) > 5 && !m_started) {
        m_started = true;

        m_layout_to_deactivate = m_widget_to_move->parentWidget()->layout();

        m_y_of_first_relevant_widget =
            m_layout_to_deactivate->itemAt(m_ignore_on_top)->geometry().top();
        m_layout_to_deactivate->setEnabled(false);
        m_original_widgetY = m_widget_to_move->y();
        m_widget_to_move->raise();
        emit startingToMove();
        m_widget_to_move->move(m_widget_to_move->x() + 5, m_widget_to_move->y());
        m_widget_to_move->resize(m_widget_to_move->width() - 10, m_widget_to_move->height());
    }

    if (m_started) {
        // -- move the dragged widget to the new position
        QPoint newPos =
            m_widget_to_move->parentWidget()->mapFromGlobal(event->globalPos()) - m_hot_spot;
        m_widget_to_move->move(m_widget_to_move->x(), newPos.y());

        // -- move other widgets to make room
        m_drop_above_this_widget = nullptr;
        int yTopOfLayoutItem = m_y_of_first_relevant_widget;

        for (int i = m_ignore_on_top; i < m_layout_to_deactivate->count(); ++i) {
            auto* layoutItem = m_layout_to_deactivate->itemAt(i);
            if (layoutItem->widget() == m_widget_to_move)
                continue;

            const bool movedWidgetIsAboveThisLayoutItem =
                m_widget_to_move->y() < yTopOfLayoutItem + layoutItem->geometry().height() / 2;
            if (movedWidgetIsAboveThisLayoutItem && layoutItem->widget() != nullptr
                && m_drop_above_this_widget == nullptr)
                m_drop_above_this_widget = layoutItem->widget();

            QRect r = layoutItem->geometry();
            if (movedWidgetIsAboveThisLayoutItem)
                r.moveTop(yTopOfLayoutItem + m_widget_to_move->height()
                          + m_layout_to_deactivate->spacing());
            else
                r.moveTop(yTopOfLayoutItem);

            if (r != layoutItem->geometry()) {
                QWidget* w = layoutItem->widget();
                if (w == nullptr)
                    layoutItem->setGeometry(r);
                else if (!m_animations.contains(w)) {
                    auto* animation = new QPropertyAnimation(w, "geometry");
                    animation->setDuration(100);
                    animation->setEasingCurve(QEasingCurve::OutQuad);
                    animation->setStartValue(w->geometry());
                    animation->setEndValue(r);
                    animation->start();
                    m_animations[w] = animation;
                } else {
                    auto* animation = m_animations[w];
                    if (animation->endValue() != r) {
                        animation->stop();
                        animation->setStartValue(w->geometry());
                        animation->setEndValue(r);
                        animation->start();
                    }
                }
            }

            yTopOfLayoutItem += r.height() + m_layout_to_deactivate->spacing();
        }

        // -- check whether scrolling is necessary
        QPoint parPos = m_scroll_area->mapFromGlobal(mapToGlobal(event->pos()));
        if (parPos.y() < 20) {
            m_drag_scroll_timer.setInterval(10);
            m_drag_scroll_timer.disconnect();
            connect(&m_drag_scroll_timer, &QTimer::timeout, [this] { scrollParent(true); });
            m_drag_scroll_timer.start();
        } else if (parPos.y() > m_scroll_area->height() - 20) {
            m_drag_scroll_timer.setInterval(10);
            m_drag_scroll_timer.disconnect();
            connect(&m_drag_scroll_timer, &QTimer::timeout, [this] { scrollParent(false); });
            m_drag_scroll_timer.start();
        } else
            m_drag_scroll_timer.stop();
    }
}

void WidgetMoverButton::scrollParent(bool up)
{
    auto* scrollbar = m_scroll_area->verticalScrollBar();
    if (!scrollbar->isVisible())
        return;

    const int oldScrollValue = scrollbar->value();
    scrollbar->setValue(oldScrollValue + (up ? -5 : 5));

    // move the dragged widget to the new position to avoid jitter effects
    // use the real change of value, not +/-5: scrolling may have been unsuccessful
    m_widget_to_move->move(m_widget_to_move->x(),
                           m_widget_to_move->y() + scrollbar->value() - oldScrollValue);
}