1+ /*
2+ * Bittorrent Client using Qt and libtorrent.
3+ * Copyright (C) 2026 FTA7700
4+ *
5+ * This program is free software; you can redistribute it and/or
6+ * modify it under the terms of the GNU General Public License
7+ * as published by the Free Software Foundation; either version 2
8+ * of the License, or (at your option) any later version.
9+ *
10+ * This program is distributed in the hope that it will be useful,
11+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+ * GNU General Public License for more details.
14+ *
15+ * You should have received a copy of the GNU General Public License
16+ * along with this program; if not, write to the Free Software
17+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18+ *
19+ * In addition, as a special exception, the copyright holders give permission to
20+ * link this program with the OpenSSL project's "OpenSSL" library (or with
21+ * modified versions of it that use the same license as the "OpenSSL" library),
22+ * and distribute the linked executables. You must obey the GNU General Public
23+ * License in all respects for all of the code used other than "OpenSSL". If you
24+ * modify file(s), you may extend this exception to your version of the file(s),
25+ * but you are not obligated to do so. If you do not wish to do so, delete this
26+ * exception statement from your version.
27+ */
28+
129#include " customizabletoolbar.h"
230
331#include < QAction>
@@ -40,9 +68,8 @@ void CustomizableToolBar::actionEvent(QActionEvent *event)
4068 QToolBar::actionEvent (event);
4169 if (event->type () == QEvent::ActionAdded)
4270 {
43- QWidget *w = widgetForAction (event->action ());
44- if (w)
45- w->installEventFilter (this );
71+ if (QWidget *widget = widgetForAction (event->action ()))
72+ widget->installEventFilter (this );
4673 }
4774}
4875
@@ -55,79 +82,91 @@ bool CustomizableToolBar::eventFilter(QObject *watched, QEvent *event)
5582 if (!widget)
5683 return QToolBar::eventFilter (watched, event);
5784
85+ auto *mouseEvent = static_cast <QMouseEvent *>(event);
86+
5887 switch (event->type ())
5988 {
6089 case QEvent::MouseButtonPress:
61- {
62- m_dragJustFinished = false ;
63-
64- auto *me = static_cast <QMouseEvent *>(event);
65- if (me->button () != Qt::LeftButton)
66- break ;
90+ if (handleMousePress (widget, mouseEvent))
91+ return true ;
92+ break ;
6793
68- QAction *action = nullptr ;
69- for (QAction *a : asConst (actions ()))
70- {
71- if (widgetForAction (a) == widget)
72- {
73- action = a;
74- break ;
75- }
76- }
94+ case QEvent::MouseMove:
95+ if (handleMouseMove (mouseEvent))
96+ return true ;
97+ break ;
7798
78- if (!action || m_lockedActions.contains (action) || action->isSeparator ())
79- break ;
99+ case QEvent::MouseButtonRelease:
100+ if (handleMouseRelease (mouseEvent))
101+ return true ;
102+ break ;
80103
81- m_dragAction = action;
82- m_dragWidget = widget;
83- m_dragStartPos = me->globalPosition ().toPoint ();
84- m_dragOffsetX = me->position ().toPoint ().x ();
104+ default :
85105 break ;
86106 }
87107
88- case QEvent::MouseMove:
89- {
90- if (!m_dragAction)
91- break ;
92-
93- auto *me = static_cast <QMouseEvent *>(event);
94- const QPoint globalPos = me->globalPosition ().toPoint ();
108+ return QToolBar::eventFilter (watched, event);
109+ }
95110
96- if (!m_dragging)
97- {
98- if ((globalPos - m_dragStartPos).manhattanLength () < QApplication::startDragDistance ())
99- break ;
100- startDrag (m_dragWidget, globalPos);
101- }
111+ bool CustomizableToolBar::handleMousePress (QWidget *widget, QMouseEvent *mouseEvent)
112+ {
113+ m_dragJustFinished = false ;
102114
103- if (m_dragging)
104- return true ;
105- break ;
106- }
115+ if (mouseEvent->button () != Qt::LeftButton)
116+ return false ;
107117
108- case QEvent::MouseButtonRelease:
118+ QAction *action = nullptr ;
119+ for (QAction *a : asConst (actions ()))
109120 {
110- if (m_dragJustFinished)
111- {
112- m_dragJustFinished = false ;
113- return true ;
114- }
115- if (m_dragging)
121+ if (widgetForAction (a) == widget)
116122 {
117- auto *me = static_cast <QMouseEvent *>(event);
118- endDrag (me->globalPosition ().toPoint ());
119- return true ;
123+ action = a;
124+ break ;
120125 }
121- if (m_dragAction)
122- m_dragAction = nullptr ;
123- break ;
124126 }
125127
126- default :
127- break ;
128+ if (!action || m_lockedActions.contains (action) || action->isSeparator ())
129+ return false ;
130+
131+ m_dragAction = action;
132+ m_dragWidget = widget;
133+ m_dragStartPos = mouseEvent->globalPosition ().toPoint ();
134+ m_dragOffsetX = mouseEvent->position ().toPoint ().x ();
135+ return false ;
136+ }
137+
138+ bool CustomizableToolBar::handleMouseMove (QMouseEvent *mouseEvent)
139+ {
140+ if (!m_dragAction)
141+ return false ;
142+
143+ const QPoint globalPos = mouseEvent->globalPosition ().toPoint ();
144+
145+ if (!m_dragging)
146+ {
147+ if ((globalPos - m_dragStartPos).manhattanLength () < QApplication::startDragDistance ())
148+ return false ;
149+ startDrag (m_dragWidget, globalPos);
128150 }
129151
130- return QToolBar::eventFilter (watched, event);
152+ return m_dragging;
153+ }
154+
155+ bool CustomizableToolBar::handleMouseRelease (QMouseEvent *mouseEvent)
156+ {
157+ if (m_dragJustFinished)
158+ {
159+ m_dragJustFinished = false ;
160+ return true ;
161+ }
162+ if (m_dragging)
163+ {
164+ endDrag (mouseEvent->globalPosition ().toPoint ());
165+ return true ;
166+ }
167+ if (m_dragAction)
168+ m_dragAction = nullptr ;
169+ return false ;
131170}
132171
133172void CustomizableToolBar::onDragTimer ()
@@ -194,8 +233,11 @@ void CustomizableToolBar::updateDrag(const QPoint &globalPos)
194233 const int bi = findBoundaryIndex ();
195234 const QList<QAction *> acts = actions ();
196235
197- if (!acts.isEmpty () && (acts.first () == m_dragAction) && !movingLeft && (m_lastSwapX >= 0 ) && (qAbs (dragFloatLeft - m_lastSwapX) < 20 ))
236+ if (!acts.isEmpty () && (acts.first () == m_dragAction) && !movingLeft
237+ && (m_lastSwapX >= 0 ) && (qAbs (dragFloatLeft - m_lastSwapX) < 20 ))
238+ {
198239 return ;
240+ }
199241 QAction *newInsertBefore = (bi >= 0 ) ? acts[bi] : nullptr ;
200242 for (int i = 0 ; i < bi; ++i)
201243 {
@@ -243,9 +285,8 @@ void CustomizableToolBar::updateDrag(const QPoint &globalPos)
243285 }
244286}
245287
246- void CustomizableToolBar::endDrag (const QPoint &globalPos )
288+ void CustomizableToolBar::endDrag (const QPoint &)
247289{
248- Q_UNUSED (globalPos)
249290
250291 if (m_dragTimer)
251292 {
0 commit comments