From e1d6fccb4d666e12b244f91ed8b7fa9d17685b1b Mon Sep 17 00:00:00 2001 From: Nicolau Leal Werneck Date: Sat, 13 Apr 2024 11:56:04 +0200 Subject: [PATCH] Add track move commands to controls library. Sending key event to library widget. Co-authored-by: ronso0 --- src/library/librarycontrol.cpp | 70 +++++++++++++++++++++++++++++++--- src/library/librarycontrol.h | 8 ++++ 2 files changed, 72 insertions(+), 6 deletions(-) diff --git a/src/library/librarycontrol.cpp b/src/library/librarycontrol.cpp index 4c1d0d15289..3d5e9cac1fa 100644 --- a/src/library/librarycontrol.cpp +++ b/src/library/librarycontrol.cpp @@ -167,6 +167,30 @@ LibraryControl::LibraryControl(Library* pLibrary) &LibraryControl::slotMoveFocus); } + // Controls to move tracks on playlist + // Track move controls (emulate Alt+up/down button press) + m_pMoveTrackUp = std::make_unique(ConfigKey("[Library]", "MoveTrackUp")); + m_pMoveTrackDown = std::make_unique( + ConfigKey("[Library]", "MoveTrackDown")); + m_pMoveTrack = std::make_unique(ConfigKey("[Library]", "MoveTrack"), false); +#ifdef MIXXX_USE_QML + if (!CmdlineArgs::Instance().isQml()) +#endif + { + connect(m_pMoveTrackUp.get(), + &ControlPushButton::valueChanged, + this, + &LibraryControl::slotMoveTrackUp); + connect(m_pMoveTrackDown.get(), + &ControlPushButton::valueChanged, + this, + &LibraryControl::slotMoveTrackDown); + connect(m_pMoveTrack.get(), + &ControlEncoder::valueChanged, + this, + &LibraryControl::slotMoveTrack); + } + // Direct focus control, read/write m_pFocusedWidgetCO = std::make_unique( ConfigKey("[Library]", "focused_widget")); @@ -219,7 +243,7 @@ LibraryControl::LibraryControl(Library* pLibrary) } // Auto DJ controls - m_pAutoDjAddTop = std::make_unique(ConfigKey("[Library]","AutoDjAddTop")); + m_pAutoDjAddTop = std::make_unique(ConfigKey("[Library]", "AutoDjAddTop")); m_pAutoDjAddTop->addAlias(ConfigKey( QStringLiteral("[Playlist]"), QStringLiteral("AutoDjAddTop"))); #ifdef MIXXX_USE_QML @@ -232,7 +256,8 @@ LibraryControl::LibraryControl(Library* pLibrary) &LibraryControl::slotAutoDjAddTop); } - m_pAutoDjAddBottom = std::make_unique(ConfigKey("[Library]","AutoDjAddBottom")); + m_pAutoDjAddBottom = std::make_unique( + ConfigKey("[Library]", "AutoDjAddBottom")); m_pAutoDjAddBottom->addAlias(ConfigKey( QStringLiteral("[Playlist]"), QStringLiteral("AutoDjAddBottom"))); #ifdef MIXXX_USE_QML @@ -419,7 +444,8 @@ LibraryControl::LibraryControl(Library* pLibrary) &LibraryControl::slotSelectPrevTrack); // Ignoring no-ops is important since this is for +/- tickers. - m_pSelectTrack = std::make_unique(ConfigKey("[Playlist]","SelectTrackKnob"), false); + m_pSelectTrack = std::make_unique( + ConfigKey("[Playlist]", "SelectTrackKnob"), false); connect(m_pSelectTrack.get(), &ControlObject::valueChanged, this, @@ -450,7 +476,8 @@ LibraryControl::LibraryControl(Library* pLibrary) this, &LibraryControl::slotToggleSelectedSidebarItem); - m_pLoadSelectedIntoFirstStopped = std::make_unique(ConfigKey("[Playlist]","LoadSelectedIntoFirstStopped")); + m_pLoadSelectedIntoFirstStopped = std::make_unique( + ConfigKey("[Playlist]", "LoadSelectedIntoFirstStopped")); connect(m_pLoadSelectedIntoFirstStopped.get(), &ControlPushButton::valueChanged, this, @@ -734,7 +761,7 @@ void LibraryControl::slotScrollDown(double v) { } void LibraryControl::slotScrollVertical(double v) { - const auto key = (v < 0) ? Qt::Key_PageUp: Qt::Key_PageDown; + const auto key = (v < 0) ? Qt::Key_PageUp : Qt::Key_PageDown; const auto times = static_cast(std::abs(v)); emitKeyEvent(QKeyEvent{QEvent::KeyPress, key, Qt::NoModifier, QString(), false, times}); } @@ -752,7 +779,7 @@ void LibraryControl::slotMoveRight(double v) { } void LibraryControl::slotMoveHorizontal(double v) { - const auto key = (v < 0) ? Qt::Key_Left: Qt::Key_Right; + const auto key = (v < 0) ? Qt::Key_Left : Qt::Key_Right; const auto times = static_cast(std::abs(v)); emitKeyEvent(QKeyEvent{QEvent::KeyPress, key, Qt::NoModifier, QString(), false, times}); } @@ -781,6 +808,37 @@ void LibraryControl::slotMoveFocus(double v) { QEvent::KeyPress, key, Qt::NoModifier, QString(), false, times}); } +void LibraryControl::slotMoveTrackUp(double v) { + if (v > 0) { + slotMoveTrack(-1); + } +} + +void LibraryControl::slotMoveTrackDown(double v) { + if (v > 0) { + slotMoveTrack(1); + } +} + +/// Move a selected track up or down a playlist by emulating Alt + Up/Down keypresses +void LibraryControl::slotMoveTrack(double v) { + if (!m_pLibraryWidget) { + return; + } + + auto* pTrackTableview = m_pLibraryWidget->getCurrentTrackTableView(); + if (!pTrackTableview) { + // no track table view is currently visible + return; + } + + const auto key = (v < 0) ? Qt::Key_Up : Qt::Key_Down; + const auto times = static_cast(std::abs(v)); + QKeyEvent pEvent = QKeyEvent{ + QEvent::KeyPress, key, Qt::AltModifier, QString(), false, times}; + QApplication::sendEvent(pTrackTableview, &pEvent); +} + void LibraryControl::emitKeyEvent(QKeyEvent&& event) { if (!QApplication::focusWindow()) { qInfo() << "No Mixxx window, popup or menu has focus." diff --git a/src/library/librarycontrol.h b/src/library/librarycontrol.h index ddd4ef0ae9e..65e21013ea4 100644 --- a/src/library/librarycontrol.h +++ b/src/library/librarycontrol.h @@ -79,6 +79,9 @@ class LibraryControl : public QObject { void slotMoveFocusForward(double); void slotMoveFocusBackward(double); void slotMoveFocus(double); + void slotMoveTrackUp(double); + void slotMoveTrackDown(double); + void slotMoveTrack(double); void slotGoToItem(double v); void slotTrackColorPrev(double v); @@ -140,6 +143,11 @@ class LibraryControl : public QObject { std::unique_ptr m_pRefocusPrevWidgetCO; FocusWidget m_prevFocusedWidget; + // Controls to move tracks (alt+up/down buttons) + std::unique_ptr m_pMoveTrackUp; + std::unique_ptr m_pMoveTrackDown; + std::unique_ptr m_pMoveTrack; + // Control to choose the currently selected item in focused widget (double click) std::unique_ptr m_pGoToItem;