-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Add Toolbar customization #24309
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
FTA7700
wants to merge
25
commits into
qbittorrent:master
Choose a base branch
from
FTA7700:feature/toolbar-customization
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add Toolbar customization #24309
Changes from 19 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
a26b7f4
Add toolbar customization: separator, show/hide buttons, lock
FTA7700 4b87e1f
Add drag-to-reorder functionality to CustomizableToolBar
FTA7700 8ff49ca
Fix file encoding and line endings to UTF-8/LF
FTA7700 f8fc283
Fix drag reorder for edge separators
FTA7700 3076b78
Fix separator swap threshold for rightward drag
FTA7700 5d1ec50
Handle queue actions in toolbar customization: dynamic show/hide, sub…
FTA7700 72473e4
Fix menu bar preservation and drag shuffle for hidden buttons
FTA7700 7b2ba19
Fix menu bar preservation and uniform drag shuffle threshold
FTA7700 47662d5
Style fixes: asConst, const bool, include order, drag threshold
FTA7700 8890914
Style: parenthesize comparisons in boolean chains
FTA7700 b11d4b8
Drag: reduce swap threshold for sooner response
FTA7700 d3dc11b
Test #2: Drag feel improvements — smoother separator crossing and no …
FTA7700 2dc63d0
Style: add missing asConst() and parenthesize comparisons
FTA7700 d3308c9
Style: revert redundant asConst(); fix separator 1-tick asymmetry
FTA7700 f5c8f86
Test #3: correct movingLeft detection on first drag tick for symmetri…
FTA7700 5c3249d
Merge remote-tracking branch 'upstream/master' into feature/toolbar-c…
FTA7700 285d593
Merge remote-tracking branch 'upstream/master' into feature/toolbar-c…
FTA7700 bcc6bed
Fix: remove duplicate managePlugins() left over from previous merge
FTA7700 876e32e
Add Reset Toolbar Buttons option to empty-space context menu
FTA7700 39d173d
Address review feedback: destructor, access, naming, GPL headers, met…
FTA7700 3c65914
[Style]: Add braces to multi-line if conditions, fix header path, rev…
FTA7700 7d44010
Retrigger CI
FTA7700 7249294
Apply style fixes from glassez's review round
FTA7700 5938a68
Encapsulate toolbar state in CustomizableToolBar
FTA7700 9f43254
Hide disabled toolbar actions automatically
FTA7700 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -69,6 +69,7 @@ add_library(qbt_gui STATIC | |
| log/logfiltermodel.h | ||
| log/loglistview.h | ||
| log/logmodel.h | ||
| customizabletoolbar.h | ||
| mainwindow.h | ||
| optionsdialog.h | ||
| powermanagement/inhibitor.h | ||
|
|
@@ -175,6 +176,7 @@ add_library(qbt_gui STATIC | |
| log/logfiltermodel.cpp | ||
| log/loglistview.cpp | ||
| log/logmodel.cpp | ||
| customizabletoolbar.cpp | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto. |
||
| mainwindow.cpp | ||
| optionsdialog.cpp | ||
| powermanagement/inhibitor.cpp | ||
|
|
@@ -261,7 +263,7 @@ add_library(qbt_gui STATIC | |
| target_sources(qbt_gui INTERFACE about.qrc) | ||
|
|
||
| # UI headers will be generated in ${CMAKE_CURRENT_BINARY_DIR} | ||
| target_include_directories(qbt_gui PRIVATE ${CMAKE_CURRENT_BINARY_DIR}) | ||
| target_include_directories(qbt_gui PRIVATE ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR}) | ||
|
glassez marked this conversation as resolved.
Outdated
|
||
|
|
||
| target_link_libraries(qbt_gui | ||
| PRIVATE | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,297 @@ | ||||||
| #include "customizabletoolbar.h" | ||||||
|
|
||||||
| #include <QAction> | ||||||
| #include <QActionEvent> | ||||||
| #include <QApplication> | ||||||
| #include <QCursor> | ||||||
| #include <QGraphicsOpacityEffect> | ||||||
| #include <QLabel> | ||||||
| #include <QMouseEvent> | ||||||
| #include <QPixmap> | ||||||
| #include <QTimer> | ||||||
|
|
||||||
| #include "base/global.h" | ||||||
|
|
||||||
| CustomizableToolBar::CustomizableToolBar(QWidget *parent) | ||||||
| : QToolBar(parent) | ||||||
| { | ||||||
| setMouseTracking(true); | ||||||
| } | ||||||
|
|
||||||
| CustomizableToolBar::CustomizableToolBar(const QString &title, QWidget *parent) | ||||||
| : QToolBar(title, parent) | ||||||
| { | ||||||
| setMouseTracking(true); | ||||||
| } | ||||||
|
|
||||||
| void CustomizableToolBar::lockAction(QAction *action) | ||||||
| { | ||||||
| if (action && !m_lockedActions.contains(action)) | ||||||
| m_lockedActions.append(action); | ||||||
| } | ||||||
|
|
||||||
| void CustomizableToolBar::setLocked(const bool locked) | ||||||
| { | ||||||
| m_locked = locked; | ||||||
| } | ||||||
|
|
||||||
| void CustomizableToolBar::actionEvent(QActionEvent *event) | ||||||
| { | ||||||
| QToolBar::actionEvent(event); | ||||||
| if (event->type() == QEvent::ActionAdded) | ||||||
| { | ||||||
| QWidget *w = widgetForAction(event->action()); | ||||||
| if (w) | ||||||
| w->installEventFilter(this); | ||||||
|
glassez marked this conversation as resolved.
Outdated
|
||||||
| } | ||||||
| } | ||||||
|
|
||||||
| bool CustomizableToolBar::eventFilter(QObject *watched, QEvent *event) | ||||||
|
glassez marked this conversation as resolved.
|
||||||
| { | ||||||
| if (m_locked) | ||||||
| return QToolBar::eventFilter(watched, event); | ||||||
|
|
||||||
| auto *widget = qobject_cast<QWidget *>(watched); | ||||||
| if (!widget) | ||||||
| return QToolBar::eventFilter(watched, event); | ||||||
|
|
||||||
| switch (event->type()) | ||||||
| { | ||||||
| case QEvent::MouseButtonPress: | ||||||
| { | ||||||
| m_dragJustFinished = false; | ||||||
|
|
||||||
| auto *me = static_cast<QMouseEvent *>(event); | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you not use such shorthands?
Suggested change
|
||||||
| if (me->button() != Qt::LeftButton) | ||||||
| break; | ||||||
|
|
||||||
| QAction *action = nullptr; | ||||||
| for (QAction *a : asConst(actions())) | ||||||
| { | ||||||
| if (widgetForAction(a) == widget) | ||||||
| { | ||||||
| action = a; | ||||||
| break; | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| if (!action || m_lockedActions.contains(action) || action->isSeparator()) | ||||||
| break; | ||||||
|
|
||||||
| m_dragAction = action; | ||||||
| m_dragWidget = widget; | ||||||
| m_dragStartPos = me->globalPosition().toPoint(); | ||||||
| m_dragOffsetX = me->position().toPoint().x(); | ||||||
| break; | ||||||
| } | ||||||
|
|
||||||
| case QEvent::MouseMove: | ||||||
| { | ||||||
| if (!m_dragAction) | ||||||
| break; | ||||||
|
|
||||||
| auto *me = static_cast<QMouseEvent *>(event); | ||||||
| const QPoint globalPos = me->globalPosition().toPoint(); | ||||||
|
|
||||||
| if (!m_dragging) | ||||||
| { | ||||||
| if ((globalPos - m_dragStartPos).manhattanLength() < QApplication::startDragDistance()) | ||||||
| break; | ||||||
| startDrag(m_dragWidget, globalPos); | ||||||
| } | ||||||
|
|
||||||
| if (m_dragging) | ||||||
| return true; | ||||||
| break; | ||||||
| } | ||||||
|
|
||||||
| case QEvent::MouseButtonRelease: | ||||||
| { | ||||||
| if (m_dragJustFinished) | ||||||
| { | ||||||
| m_dragJustFinished = false; | ||||||
| return true; | ||||||
| } | ||||||
| if (m_dragging) | ||||||
| { | ||||||
| auto *me = static_cast<QMouseEvent *>(event); | ||||||
| endDrag(me->globalPosition().toPoint()); | ||||||
| return true; | ||||||
| } | ||||||
| if (m_dragAction) | ||||||
| m_dragAction = nullptr; | ||||||
| break; | ||||||
| } | ||||||
|
|
||||||
| default: | ||||||
| break; | ||||||
| } | ||||||
|
|
||||||
| return QToolBar::eventFilter(watched, event); | ||||||
| } | ||||||
|
|
||||||
| void CustomizableToolBar::onDragTimer() | ||||||
| { | ||||||
| if (!m_dragging) | ||||||
| return; | ||||||
|
|
||||||
| updateDrag(QCursor::pos()); | ||||||
|
|
||||||
| if (!(QApplication::mouseButtons() & Qt::LeftButton)) | ||||||
| endDrag(QCursor::pos()); | ||||||
| } | ||||||
|
|
||||||
| void CustomizableToolBar::startDrag(QWidget *widget, const QPoint &globalPos) | ||||||
| { | ||||||
| m_dragging = true; | ||||||
|
|
||||||
| // Snapshot BEFORE making transparent | ||||||
| const QPixmap snap = widget->grab(); | ||||||
|
|
||||||
| // Make drag widget invisible but keep it in layout, avoiding reflow | ||||||
| auto *effect = new QGraphicsOpacityEffect(widget); | ||||||
| effect->setOpacity(0.0); | ||||||
| widget->setGraphicsEffect(effect); | ||||||
|
|
||||||
| // Float label follows cursor globally | ||||||
| m_floatLabel = new QLabel(nullptr, Qt::ToolTip | Qt::FramelessWindowHint | Qt::NoDropShadowWindowHint); | ||||||
| m_floatLabel->setAttribute(Qt::WA_TransparentForMouseEvents); | ||||||
| m_floatLabel->setAttribute(Qt::WA_ShowWithoutActivating); | ||||||
| m_floatLabel->setPixmap(snap); | ||||||
| m_floatLabel->setFixedSize(snap.size()); | ||||||
| m_floatLabel->show(); | ||||||
|
|
||||||
| m_dragTimer = new QTimer(this); | ||||||
| m_dragTimer->setInterval(16); | ||||||
| connect(m_dragTimer, &QTimer::timeout, this, &CustomizableToolBar::onDragTimer); | ||||||
| m_dragTimer->start(); | ||||||
|
|
||||||
| updateDrag(globalPos); | ||||||
| } | ||||||
|
|
||||||
| void CustomizableToolBar::updateDrag(const QPoint &globalPos) | ||||||
| { | ||||||
| if (!m_floatLabel) | ||||||
| return; | ||||||
|
|
||||||
| const int fw = m_floatLabel->width(); | ||||||
| const int fh = m_floatLabel->height(); | ||||||
| const QPoint toolbarTopLeft = mapToGlobal(QPoint(0, 0)); | ||||||
| const int boundaryGX = getBoundaryGlobalX(); | ||||||
|
|
||||||
| const int clampedX = qBound(toolbarTopLeft.x(), globalPos.x() - m_dragOffsetX, boundaryGX - fw + 20); | ||||||
| const int clampedY = toolbarTopLeft.y() + (height() - fh) / 2; | ||||||
| m_floatLabel->move(clampedX, clampedY); | ||||||
|
|
||||||
| const int dragFloatLeft = mapFromGlobal(QPoint(clampedX, 0)).x(); | ||||||
| const int dragFloatRight = mapFromGlobal(QPoint(clampedX + fw, 0)).x(); | ||||||
| const int dragFloatCentre = mapFromGlobal(QPoint(clampedX + fw / 2, 0)).x(); | ||||||
| const bool atLeftWall = (clampedX <= toolbarTopLeft.x()); | ||||||
| const bool movingLeft = atLeftWall || (dragFloatCentre < m_lastFloatCentreX) | ||||||
| || ((m_lastFloatCentreX == 0) && (globalPos.x() < m_dragStartPos.x())); | ||||||
| m_lastFloatCentreX = dragFloatCentre; | ||||||
|
|
||||||
| const int bi = findBoundaryIndex(); | ||||||
| const QList<QAction *> acts = actions(); | ||||||
|
|
||||||
| if (!acts.isEmpty() && (acts.first() == m_dragAction) && !movingLeft && (m_lastSwapX >= 0) && (qAbs(dragFloatLeft - m_lastSwapX) < 20)) | ||||||
| return; | ||||||
|
glassez marked this conversation as resolved.
Outdated
|
||||||
| QAction *newInsertBefore = (bi >= 0) ? acts[bi] : nullptr; | ||||||
| for (int i = 0; i < bi; ++i) | ||||||
| { | ||||||
| QAction *a = acts[i]; | ||||||
| if (a == m_dragAction) | ||||||
| continue; | ||||||
| QWidget *w = widgetForAction(a); | ||||||
| if (!w && !a->isSeparator()) | ||||||
| continue; | ||||||
|
|
||||||
| const int threshold = w ? (a->isSeparator() ? (movingLeft ? w->x() + w->width() + fw / 2 : w->x() - fw / 2) : w->x() + w->width() / 2) : 0; | ||||||
|
|
||||||
| const int compareX = a->isSeparator() ? dragFloatCentre : (movingLeft ? dragFloatLeft : dragFloatRight); | ||||||
| if (compareX < threshold) | ||||||
| { | ||||||
| newInsertBefore = a; | ||||||
| break; | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| const bool targetIsSeparator = newInsertBefore && newInsertBefore->isSeparator(); | ||||||
| if (!targetIsSeparator && (m_lastSwapX >= 0) && (qAbs(dragFloatLeft - m_lastSwapX) < 14)) | ||||||
| return; | ||||||
| // Only swap when target slot changes, one atomic reorder per crossing | ||||||
| if (newInsertBefore == m_gapTarget) | ||||||
| return; | ||||||
|
|
||||||
| m_gapTarget = newInsertBefore; | ||||||
| m_lastSwapX = dragFloatLeft; | ||||||
|
|
||||||
| // Briefly remove opacity effect during reorder, reapply to new widget | ||||||
| if (m_dragWidget) | ||||||
| m_dragWidget->setGraphicsEffect(nullptr); | ||||||
|
|
||||||
| removeAction(m_dragAction); | ||||||
| insertAction(m_gapTarget, m_dragAction); | ||||||
|
|
||||||
| QWidget *newWidget = widgetForAction(m_dragAction); | ||||||
| if (newWidget) | ||||||
| { | ||||||
| m_dragWidget = newWidget; | ||||||
| auto *effect = new QGraphicsOpacityEffect(newWidget); | ||||||
| effect->setOpacity(0.0); | ||||||
| newWidget->setGraphicsEffect(effect); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| void CustomizableToolBar::endDrag(const QPoint &globalPos) | ||||||
| { | ||||||
| Q_UNUSED(globalPos) | ||||||
|
|
||||||
|
glassez marked this conversation as resolved.
Outdated
|
||||||
| if (m_dragTimer) | ||||||
| { | ||||||
| m_dragTimer->stop(); | ||||||
| delete m_dragTimer; | ||||||
| m_dragTimer = nullptr; | ||||||
| } | ||||||
|
|
||||||
| delete m_floatLabel; | ||||||
| m_floatLabel = nullptr; | ||||||
|
|
||||||
| // Remove opacity effect, restore widget appearance | ||||||
| if (m_dragWidget) | ||||||
| m_dragWidget->setGraphicsEffect(nullptr); | ||||||
|
|
||||||
| // Reorder already committed live in updateDrag, just save state | ||||||
| emit actionOrderChanged(); | ||||||
|
|
||||||
| m_gapTarget = nullptr; | ||||||
| m_lastSwapX = -1; | ||||||
| m_lastFloatCentreX = 0; | ||||||
| m_dragAction = nullptr; | ||||||
| m_dragWidget = nullptr; | ||||||
| m_dragJustFinished = true; | ||||||
| m_dragging = false; | ||||||
| } | ||||||
|
|
||||||
| int CustomizableToolBar::findBoundaryIndex() const | ||||||
| { | ||||||
| const QList<QAction *> acts = actions(); | ||||||
| for (int i = 0; i < acts.size(); ++i) | ||||||
| { | ||||||
| if (m_lockedActions.contains(acts[i])) | ||||||
| return i; | ||||||
| } | ||||||
| return -1; | ||||||
| } | ||||||
|
|
||||||
| int CustomizableToolBar::getBoundaryGlobalX() const | ||||||
| { | ||||||
| const int bi = findBoundaryIndex(); | ||||||
| if (bi >= 0) | ||||||
| { | ||||||
| QWidget *bw = widgetForAction(actions()[bi]); | ||||||
| if (bw) | ||||||
| return mapToGlobal(QPoint(bw->x(), 0)).x(); | ||||||
| } | ||||||
| return mapToGlobal(QPoint(width(), 0)).x(); | ||||||
| } | ||||||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Incorrect order.