Skip to content

Commit

Permalink
Merge pull request #4369 from ronso0/focus-library-directly
Browse files Browse the repository at this point in the history
Focus library directly
  • Loading branch information
Holzhaus authored Oct 19, 2021
2 parents 070a475 + 94f324b commit 45e8068
Show file tree
Hide file tree
Showing 30 changed files with 199 additions and 28 deletions.
4 changes: 4 additions & 0 deletions src/library/autodj/dlgautodj.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -388,3 +388,7 @@ void DlgAutoDJ::shiftTabKeypress() {
QKeyEvent{QEvent::KeyPress, Qt::Key_Tab, Qt::ShiftModifier};
QApplication::sendEvent(this, &backwardFocusKeyEvent);
}

void DlgAutoDJ::setFocus() {
m_pTrackTableView->setFocus();
}
1 change: 1 addition & 0 deletions src/library/autodj/dlgautodj.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class DlgAutoDJ : public QWidget, public Ui::DlgAutoDJ, public LibraryView {

void onShow() override;
bool hasFocus() const override;
void setFocus() override;
void onSearch(const QString& text) override;
void loadSelectedTrack() override;
void loadSelectedTrackToGroup(const QString& group, bool play) override;
Expand Down
1 change: 1 addition & 0 deletions src/library/browse/browsefeature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ void BrowseFeature::bindLibraryWidget(WLibrary* libraryWidget,
WLibraryTextBrowser* edit = new WLibraryTextBrowser(libraryWidget);
edit->setHtml(getRootViewHtml());
libraryWidget->registerView("BROWSEHOME", edit);
m_pLibrary->bindFeatureRootView(edit);
}

void BrowseFeature::bindSidebarWidget(WLibrarySidebar* pSidebarWidget) {
Expand Down
4 changes: 4 additions & 0 deletions src/library/dlganalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ bool DlgAnalysis::hasFocus() const {
return m_pAnalysisLibraryTableView->hasFocus();
}

void DlgAnalysis::setFocus() {
m_pAnalysisLibraryTableView->setFocus();
}

void DlgAnalysis::onSearch(const QString& text) {
m_pAnalysisLibraryTableModel->search(text);
}
Expand Down
1 change: 1 addition & 0 deletions src/library/dlganalysis.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class DlgAnalysis : public QWidget, public Ui::DlgAnalysis, public virtual Libra
void onSearch(const QString& text) override;
void onShow() override;
bool hasFocus() const override;
void setFocus() override;
void loadSelectedTrack() override;
void loadSelectedTrackToGroup(const QString& group, bool play) override;
void slotAddToAutoDJBottom() override;
Expand Down
4 changes: 4 additions & 0 deletions src/library/dlghidden.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,7 @@ void DlgHidden::selectionChanged(const QItemSelection &selected,
bool DlgHidden::hasFocus() const {
return m_pTrackTableView->hasFocus();
}

void DlgHidden::setFocus() {
m_pTrackTableView->setFocus();
}
1 change: 1 addition & 0 deletions src/library/dlghidden.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class DlgHidden : public QWidget, public Ui::DlgHidden, public LibraryView {

void onShow() override;
bool hasFocus() const override;
void setFocus() override;
void onSearch(const QString& text) override;
QString currentSearch();

Expand Down
4 changes: 4 additions & 0 deletions src/library/dlgmissing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,7 @@ void DlgMissing::selectionChanged(const QItemSelection &selected,
bool DlgMissing::hasFocus() const {
return m_pTrackTableView->hasFocus();
}

void DlgMissing::setFocus() {
m_pTrackTableView->setFocus();
}
1 change: 1 addition & 0 deletions src/library/dlgmissing.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class DlgMissing : public QWidget, public Ui::DlgMissing, public LibraryView {

void onShow() override;
bool hasFocus() const override;
void setFocus() override;
void onSearch(const QString& text) override;
QString currentSearch();

Expand Down
21 changes: 21 additions & 0 deletions src/library/library.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#include "util/sandbox.h"
#include "widget/wlibrary.h"
#include "widget/wlibrarysidebar.h"
#include "widget/wlibrarytextbrowser.h"
#include "widget/wsearchlineedit.h"
#include "widget/wtracktableview.h"

Expand Down Expand Up @@ -288,6 +289,10 @@ void Library::bindSearchboxWidget(WSearchLineEdit* pSearchboxWidget) {
&WSearchLineEdit::slotSetFont);
emit setTrackTableFont(m_trackTableFont);
m_pLibraryControl->bindSearchboxWidget(pSearchboxWidget);
connect(pSearchboxWidget,
&WSearchLineEdit::searchbarFocusChange,
m_pLibraryControl,
&LibraryControl::setLibraryFocus);
}

void Library::bindSidebarWidget(WLibrarySidebar* pSidebarWidget) {
Expand Down Expand Up @@ -318,6 +323,11 @@ void Library::bindSidebarWidget(WLibrarySidebar* pSidebarWidget) {
m_pSidebarModel,
&SidebarModel::rightClicked);

connect(pSidebarWidget,
&WLibrarySidebar::sidebarFocusChange,
m_pLibraryControl,
&LibraryControl::setLibraryFocus);

pSidebarWidget->slotSetFont(m_trackTableFont);
connect(this,
&Library::setTrackTableFont,
Expand Down Expand Up @@ -375,6 +385,10 @@ void Library::bindLibraryWidget(
&WTrackTableView::setSelectedClick);

m_pLibraryControl->bindLibraryWidget(pLibraryWidget, pKeyboard);
connect(pTrackTableView,
&WTrackTableView::trackTableFocusChange,
m_pLibraryControl,
&LibraryControl::setLibraryFocus);

for (const auto& feature : qAsConst(m_features)) {
feature->bindLibraryWidget(pLibraryWidget, pKeyboard);
Expand All @@ -387,6 +401,13 @@ void Library::bindLibraryWidget(
emit setSelectedClick(m_editMetadataSelectedClick);
}

void Library::bindFeatureRootView(WLibraryTextBrowser* pTextBrowser) {
connect(pTextBrowser,
&WLibraryTextBrowser::textBrowserFocusChange,
m_pLibraryControl,
&LibraryControl::setLibraryFocus);
}

void Library::addFeature(LibraryFeature* feature) {
VERIFY_OR_DEBUG_ASSERT(feature) {
return;
Expand Down
2 changes: 2 additions & 0 deletions src/library/library.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class TrackModel;
class WSearchLineEdit;
class WLibrarySidebar;
class WLibrary;
class WLibraryTextBrowser;

#ifdef __ENGINEPRIME__
namespace mixxx {
Expand Down Expand Up @@ -69,6 +70,7 @@ class Library: public QObject {
void bindSidebarWidget(WLibrarySidebar* sidebarWidget);
void bindLibraryWidget(WLibrary* libraryWidget,
KeyboardEventFilter* pKeyboard);
void bindFeatureRootView(WLibraryTextBrowser* pTextBrowser);

void addFeature(LibraryFeature* feature);

Expand Down
9 changes: 9 additions & 0 deletions src/library/library_decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,12 @@ enum class LibraryRemovalType {
HideTracks,
PurgeTracks
};

enum class FocusWidget {
None,
Searchbar,
Sidebar,
TracksTable, // or a feature root view (WLibraryTextBrowser)
Count // used for setting the number of PushButton states of
// m_pLibraryFocusedWidgetCO in librarycontrol.cpp
};
84 changes: 61 additions & 23 deletions src/library/librarycontrol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ LibraryControl::LibraryControl(Library* pLibrary)
m_numDecks("[Master]", "num_decks", this),
m_numSamplers("[Master]", "num_samplers", this),
m_numPreviewDecks("[Master]", "num_preview_decks", this) {
qRegisterMetaType<FocusWidget>("FocusWidget");

slotNumDecksChanged(m_numDecks.get());
slotNumSamplersChanged(m_numSamplers.get());
Expand Down Expand Up @@ -121,7 +122,8 @@ LibraryControl::LibraryControl(Library* pLibrary)
this,
&LibraryControl::slotMoveHorizontal);

// Control to navigate between widgets (tab/shit+tab button)
// Controls to navigate between widgets
// Relative focus controls (tab/shift+tab button)
m_pMoveFocusForward = std::make_unique<ControlPushButton>(ConfigKey("[Library]", "MoveFocusForward"));
m_pMoveFocusBackward = std::make_unique<ControlPushButton>(ConfigKey("[Library]", "MoveFocusBackward"));
m_pMoveFocus = std::make_unique<ControlEncoder>(ConfigKey("[Library]", "MoveFocus"), false);
Expand All @@ -137,6 +139,25 @@ LibraryControl::LibraryControl(Library* pLibrary)
&ControlEncoder::valueChanged,
this,
&LibraryControl::slotMoveFocus);
// Direct focus control, read/write
m_pLibraryFocusedWidgetCO = std::make_unique<ControlPushButton>(
ConfigKey("[Library]", "focused_widget"));
m_pLibraryFocusedWidgetCO->setStates(static_cast<int>(FocusWidget::Count));
m_pLibraryFocusedWidgetCO->connectValueChangeRequest(
this,
[this](double value) {
// Focus can not be removed from a widget just moved to another one.
// Thus, to keep the CO and QApplication::focusWidget() in sync we
// have to prevent scripts or GUI buttons setting the CO to 'None'.
// It's only set to 'None' internally when one of the library widgets
// receives a FocusOutEvent(), e.g. when the focus is moved to another
// widget, or when the main window loses focus.
const int valueInt = static_cast<int>(value);
if (valueInt != static_cast<int>(FocusWidget::None) &&
valueInt < static_cast<int>(FocusWidget::Count)) {
setLibraryFocus(static_cast<FocusWidget>(valueInt));
}
});

// Control to "goto" the currently selected item in focused widget (context dependent)
m_pGoToItem = std::make_unique<ControlPushButton>(ConfigKey("[Library]", "GoToItem"));
Expand Down Expand Up @@ -355,7 +376,6 @@ void LibraryControl::slotNumSamplersChanged(double v) {
}
}


void LibraryControl::slotNumPreviewDecksChanged(double v) {
int iNumPreviewDecks = static_cast<int>(v);

Expand Down Expand Up @@ -406,8 +426,6 @@ void LibraryControl::bindSearchboxWidget(WSearchLineEdit* pSearchbox) {
&LibraryControl::searchboxWidgetDeleted);
}



void LibraryControl::libraryWidgetDeleted() {
m_pLibraryWidget = nullptr;
}
Expand Down Expand Up @@ -612,11 +630,11 @@ void LibraryControl::emitKeyEvent(QKeyEvent&& event) {
if (!keyIsTab && !m_pSidebarWidget->hasFocus()
&& !m_pLibraryWidget->getActiveView()->hasFocus()) {
if (keyIsUpDown && !m_pSearchbox->hasFocus()) {
setLibraryFocus();
setLibraryFocus(FocusWidget::TracksTable);
}
}
if (keyIsTab && !QApplication::focusWidget()){
setLibraryFocus();
setLibraryFocus(FocusWidget::TracksTable);
}

// Send the event pointer to the currently focused widget
Expand All @@ -628,24 +646,44 @@ void LibraryControl::emitKeyEvent(QKeyEvent&& event) {
}
}

void LibraryControl::setLibraryFocus() {
// TODO: Set the focus of the library panel directly instead of sending tab from sidebar
VERIFY_OR_DEBUG_ASSERT(m_pSidebarWidget) {
void LibraryControl::setLibraryFocus(FocusWidget newFocusWidget) {
// ignore no-op
if (static_cast<double>(newFocusWidget) == m_pLibraryFocusedWidgetCO->get()) {
return;
}
// Try to focus the sidebar.
m_pSidebarWidget->setFocus();

// This may have failed, for example when a Cover window still has focus,
// so make sure the sidebar is focused or we'll crash.
if (!m_pSidebarWidget->hasFocus()) {
return;
bool confirmed = false;
switch (newFocusWidget) {
case FocusWidget::Searchbar:
VERIFY_OR_DEBUG_ASSERT(m_pSearchbox) {
return;
}
m_pSearchbox->setFocus();
confirmed = m_pSearchbox->hasFocus();
break;
case FocusWidget::Sidebar:
VERIFY_OR_DEBUG_ASSERT(m_pSidebarWidget) {
return;
}
m_pSidebarWidget->setFocus();
confirmed = m_pSidebarWidget->hasFocus();
break;
case FocusWidget::TracksTable:
VERIFY_OR_DEBUG_ASSERT(m_pLibraryWidget) {
return;
}
m_pLibraryWidget->getActiveView()->setFocus();
confirmed = m_pLibraryWidget->getActiveView()->hasFocus();
break;
case FocusWidget::None:
confirmed = true;
break;
default:
DEBUG_ASSERT(!"Invalid focus widget change request");
break;
}
if (confirmed) {
m_pLibraryFocusedWidgetCO->setAndConfirm(static_cast<double>(newFocusWidget));
}
// Send Tab to move focus to the Tracks table.
// Obviously only works as desired if the skin widgets are arranged
// accordingly.
QKeyEvent event(QEvent::KeyPress, Qt::Key_Tab, Qt::NoModifier);
QApplication::sendEvent(m_pSidebarWidget, &event);
}

void LibraryControl::slotSelectSidebarItem(double v) {
Expand Down Expand Up @@ -707,7 +745,7 @@ void LibraryControl::slotGoToItem(double v) {
// expanding those root items via controllers is considered dispensable
// because the subfeatures' actions can't be accessed by controllers anyway.
if (m_pSidebarWidget->isLeafNodeSelected()) {
setLibraryFocus();
setLibraryFocus(FocusWidget::TracksTable);
return;
} else {
// Otherwise toggle the sidebar item expanded state
Expand All @@ -724,7 +762,7 @@ void LibraryControl::slotGoToItem(double v) {

// If searchbox has focus jump to the tracks table
if (m_pSearchbox->hasFocus()) {
return setLibraryFocus();
return setLibraryFocus(FocusWidget::TracksTable);
}

// Clear the search if the searchbox has focus
Expand Down
6 changes: 4 additions & 2 deletions src/library/librarycontrol.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "control/controlencoder.h"
#include "control/controlproxy.h"
#include "library/library_decl.h"
#include "util/memory.h"

class ControlObject;
Expand Down Expand Up @@ -43,6 +44,8 @@ class LibraryControl : public QObject {
void bindLibraryWidget(WLibrary* pLibrary, KeyboardEventFilter* pKeyboard);
void bindSidebarWidget(WLibrarySidebar* pLibrarySidebar);
void bindSearchboxWidget(WSearchLineEdit* pSearchbox);
// Give the keyboard focus to one of the library widgets
void setLibraryFocus(FocusWidget newFocusWidget);

signals:
void clearSearchIfClearButtonHasFocus();
Expand Down Expand Up @@ -103,8 +106,6 @@ class LibraryControl : public QObject {

// Simulate pressing a key on the keyboard
void emitKeyEvent(QKeyEvent&& event);
// Give the keyboard focus to the main library pane
void setLibraryFocus();

// Controls to navigate vertically within currently focused widget (up/down buttons)
std::unique_ptr<ControlPushButton> m_pMoveUp;
Expand All @@ -125,6 +126,7 @@ class LibraryControl : public QObject {
std::unique_ptr<ControlPushButton> m_pMoveFocusForward;
std::unique_ptr<ControlPushButton> m_pMoveFocusBackward;
std::unique_ptr<ControlEncoder> m_pMoveFocus;
std::unique_ptr<ControlPushButton> m_pLibraryFocusedWidgetCO;

// Control to choose the currently selected item in focused widget (double click)
std::unique_ptr<ControlObject> m_pGoToItem;
Expand Down
2 changes: 2 additions & 0 deletions src/library/libraryview.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class LibraryView {

virtual void onShow() = 0;
virtual bool hasFocus() const = 0;
virtual void setFocus() {
}
/// Reimplement if LibraryView should be able to search
virtual void onSearch(const QString& text) {Q_UNUSED(text);}

Expand Down
4 changes: 4 additions & 0 deletions src/library/recording/dlgrecording.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ bool DlgRecording::hasFocus() const {
return m_pTrackTableView->hasFocus();
}

void DlgRecording::setFocus() {
m_pTrackTableView->setFocus();
}

void DlgRecording::refreshBrowseModel() {
m_browseModel.setPath(mixxx::FileAccess(mixxx::FileInfo(m_recordingDir)));
}
Expand Down
1 change: 1 addition & 0 deletions src/library/recording/dlgrecording.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class DlgRecording : public QWidget, public Ui::DlgRecording, public virtual Lib
void onSearch(const QString& text) override;
void onShow() override;
bool hasFocus() const override;
void setFocus() override;
void loadSelectedTrack() override;
void slotAddToAutoDJBottom() override;
void slotAddToAutoDJTop() override;
Expand Down
1 change: 1 addition & 0 deletions src/library/rekordbox/rekordboxfeature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1421,6 +1421,7 @@ void RekordboxFeature::bindLibraryWidget(WLibrary* libraryWidget,
edit->setOpenLinks(false);
connect(edit, &WLibraryTextBrowser::anchorClicked, this, &RekordboxFeature::htmlLinkClicked);
libraryWidget->registerView("REKORDBOXHOME", edit);
m_pLibrary->bindFeatureRootView(edit);
}

void RekordboxFeature::htmlLinkClicked(const QUrl& link) {
Expand Down
1 change: 1 addition & 0 deletions src/library/serato/seratofeature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -937,6 +937,7 @@ void SeratoFeature::bindLibraryWidget(WLibrary* libraryWidget,
edit->setOpenLinks(false);
connect(edit, &WLibraryTextBrowser::anchorClicked, this, &SeratoFeature::htmlLinkClicked);
libraryWidget->registerView("SERATOHOME", edit);
m_pLibrary->bindFeatureRootView(edit);
}

void SeratoFeature::htmlLinkClicked(const QUrl& link) {
Expand Down
1 change: 1 addition & 0 deletions src/library/trackset/baseplaylistfeature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,7 @@ void BasePlaylistFeature::bindLibraryWidget(WLibrary* libraryWidget,
this,
&BasePlaylistFeature::htmlLinkClicked);
libraryWidget->registerView(m_rootViewName, edit);
m_pLibrary->bindFeatureRootView(edit);
}

void BasePlaylistFeature::bindSidebarWidget(WLibrarySidebar* pSidebarWidget) {
Expand Down
Loading

0 comments on commit 45e8068

Please sign in to comment.