diff --git a/src/library/libraryfilterwidget.cpp b/src/library/libraryfilterwidget.cpp index 190710425..b99fb4a2a 100644 --- a/src/library/libraryfilterwidget.cpp +++ b/src/library/libraryfilterwidget.cpp @@ -126,6 +126,11 @@ LibraryFilterWidget::~LibraryFilterWidget() { delete ui_; } +void LibraryFilterWidget::FocusOnFilter(QKeyEvent *event) { + ui_->filter->setFocus(Qt::OtherFocusReason); + QApplication::sendEvent(ui_->filter, event); +} + void LibraryFilterWidget::SetLibraryModel(LibraryModel *model) { if (model_) { disconnect(model_, 0, this, 0); @@ -223,6 +228,11 @@ void LibraryFilterWidget::keyReleaseEvent(QKeyEvent* e) { emit DownPressed(); e->accept(); break; + + case Qt::Key_Escape: + ui_->filter->LineEditInterface::clear(); + e->accept(); + break; } QWidget::keyReleaseEvent(e); diff --git a/src/library/libraryfilterwidget.h b/src/library/libraryfilterwidget.h index b0cb7738d..9b83a8645 100644 --- a/src/library/libraryfilterwidget.h +++ b/src/library/libraryfilterwidget.h @@ -55,6 +55,7 @@ class LibraryFilterWidget : public QWidget { public slots: void SetQueryMode(QueryOptions::QueryMode view); + void FocusOnFilter(QKeyEvent* e); signals: void UpPressed(); diff --git a/src/library/libraryview.cpp b/src/library/libraryview.cpp index 516f6710d..677beeb82 100644 --- a/src/library/libraryview.cpp +++ b/src/library/libraryview.cpp @@ -485,18 +485,6 @@ void LibraryView::CopyToDevice() { organise_dialog_->show(); } -void LibraryView::keyPressEvent(QKeyEvent* e) { - switch (e->key()) { - case Qt::Key_Enter: - case Qt::Key_Return: - if (currentIndex().isValid()) - emit doubleClicked(currentIndex()); - break; - } - - QTreeView::keyPressEvent(e); -} - void LibraryView::DeleteFinished(const SongList& songs_with_errors) { if (songs_with_errors.isEmpty()) return; @@ -506,16 +494,6 @@ void LibraryView::DeleteFinished(const SongList& songs_with_errors) { // It deletes itself when the user closes it } -void LibraryView::UpAndFocus() { - setCurrentIndex(moveCursor(QAbstractItemView::MoveUp, Qt::NoModifier)); - setFocus(); -} - -void LibraryView::DownAndFocus() { - setCurrentIndex(moveCursor(QAbstractItemView::MoveDown, Qt::NoModifier)); - setFocus(); -} - void LibraryView::FilterReturnPressed() { if (!currentIndex().isValid()) { // Pick the first thing that isn't a divider diff --git a/src/library/libraryview.h b/src/library/libraryview.h index 15328ed93..9a1909f76 100644 --- a/src/library/libraryview.h +++ b/src/library/libraryview.h @@ -79,8 +79,6 @@ class LibraryView : public AutoExpandingTreeView { void TotalSongCountUpdated(int count); void ReloadSettings(); - void UpAndFocus(); - void DownAndFocus(); void FilterReturnPressed(); signals: @@ -91,7 +89,6 @@ class LibraryView : public AutoExpandingTreeView { void paintEvent(QPaintEvent* event); void mouseReleaseEvent(QMouseEvent* e); void contextMenuEvent(QContextMenuEvent* e); - void keyPressEvent(QKeyEvent* e); private slots: void Load(); diff --git a/src/library/libraryviewcontainer.cpp b/src/library/libraryviewcontainer.cpp index 4427e034d..bd2392efe 100644 --- a/src/library/libraryviewcontainer.cpp +++ b/src/library/libraryviewcontainer.cpp @@ -27,6 +27,7 @@ LibraryViewContainer::LibraryViewContainer(QWidget* parent) connect(filter(), SIGNAL(UpPressed()), view(), SLOT(UpAndFocus())); connect(filter(), SIGNAL(DownPressed()), view(), SLOT(DownAndFocus())); connect(filter(), SIGNAL(ReturnPressed()), view(), SLOT(FilterReturnPressed())); + connect(view(), SIGNAL(FocusOnFilterSignal(QKeyEvent*)), filter(), SLOT(FocusOnFilter(QKeyEvent*))); } LibraryViewContainer::~LibraryViewContainer() { diff --git a/src/radio/icecastfilterwidget.cpp b/src/radio/icecastfilterwidget.cpp index f94d17a67..c7d9e9ce6 100644 --- a/src/radio/icecastfilterwidget.cpp +++ b/src/radio/icecastfilterwidget.cpp @@ -71,6 +71,11 @@ IcecastFilterWidget::~IcecastFilterWidget() { delete ui_; } +void IcecastFilterWidget::FocusOnFilter(QKeyEvent *event) { + ui_->filter->setFocus(Qt::OtherFocusReason); + QApplication::sendEvent(ui_->filter, event); +} + void IcecastFilterWidget::SetIcecastModel(IcecastModel* model) { model_ = model; connect(filter_->widget(), SIGNAL(textChanged(QString)), diff --git a/src/radio/icecastfilterwidget.h b/src/radio/icecastfilterwidget.h index cd87d402c..f3f5bc4ed 100644 --- a/src/radio/icecastfilterwidget.h +++ b/src/radio/icecastfilterwidget.h @@ -39,6 +39,9 @@ public: void SetIcecastModel(IcecastModel* model); +public slots: + void FocusOnFilter(QKeyEvent* e); + private slots: void SortModeChanged(int mode); diff --git a/src/radio/radioviewcontainer.cpp b/src/radio/radioviewcontainer.cpp index bef0f0eea..7c60f42bf 100644 --- a/src/radio/radioviewcontainer.cpp +++ b/src/radio/radioviewcontainer.cpp @@ -21,8 +21,9 @@ #include "ui_radioviewcontainer.h" #include "core/mergedproxymodel.h" -#include +#include #include +#include const int RadioViewContainer::kAnimationDuration = 500; @@ -37,6 +38,7 @@ RadioViewContainer::RadioViewContainer(QWidget *parent) connect(ui_->tree, SIGNAL(collapsed(QModelIndex)), SLOT(Collapsed(QModelIndex))); connect(ui_->tree, SIGNAL(expanded(QModelIndex)), SLOT(Expanded(QModelIndex))); + connect(ui_->tree, SIGNAL(FocusOnFilterSignal(QKeyEvent*)), SLOT(FocusOnFilter(QKeyEvent*))); } RadioViewContainer::~RadioViewContainer() { @@ -113,6 +115,19 @@ void RadioViewContainer::SetHeaderVisible(QWidget* header, bool visible) { d.animation_->start(); } +void RadioViewContainer::FocusOnFilter(QKeyEvent* event) { + // Beware: magic + + if (current_header_) { + int slot = current_header_->metaObject()->indexOfSlot( + QMetaObject::normalizedSignature("FocusOnFilter(QKeyEvent*)")); + if (slot != -1) { + current_header_->metaObject()->method(slot).invoke( + current_header_, Q_ARG(QKeyEvent*, event)); + } + } +} + void RadioViewContainer::SetHeaderHeight(int height) { QTimeLine* animation = qobject_cast(sender()); QWidget* header = NULL; diff --git a/src/radio/radioviewcontainer.h b/src/radio/radioviewcontainer.h index 3c76640eb..45be3a381 100644 --- a/src/radio/radioviewcontainer.h +++ b/src/radio/radioviewcontainer.h @@ -49,6 +49,8 @@ class RadioViewContainer : public QWidget { void CurrentIndexChanged(const QModelIndex& index); void SetHeaderHeight(int height); + void FocusOnFilter(QKeyEvent* event); + private: void ServiceChanged(const QModelIndex& index); void SetHeaderVisible(QWidget* header, bool visible); diff --git a/src/widgets/autoexpandingtreeview.cpp b/src/widgets/autoexpandingtreeview.cpp index efbe91b67..325bfd9da 100644 --- a/src/widgets/autoexpandingtreeview.cpp +++ b/src/widgets/autoexpandingtreeview.cpp @@ -112,3 +112,32 @@ void AutoExpandingTreeView::mousePressEvent(QMouseEvent* event) { emit AddToPlaylistSignal(data); } } + +void AutoExpandingTreeView::keyPressEvent(QKeyEvent* e) { + switch (e->key()) { + case Qt::Key_Enter: + case Qt::Key_Return: + if (currentIndex().isValid()) + emit doubleClicked(currentIndex()); + e->accept(); + break; + + case Qt::Key_Backspace: + case Qt::Key_Escape: + emit FocusOnFilterSignal(e); + e->accept(); + break; + } + + QTreeView::keyPressEvent(e); +} + +void AutoExpandingTreeView::UpAndFocus() { + setCurrentIndex(moveCursor(QAbstractItemView::MoveUp, Qt::NoModifier)); + setFocus(); +} + +void AutoExpandingTreeView::DownAndFocus() { + setCurrentIndex(moveCursor(QAbstractItemView::MoveDown, Qt::NoModifier)); + setFocus(); +} diff --git a/src/widgets/autoexpandingtreeview.h b/src/widgets/autoexpandingtreeview.h index 8cd0c6879..c6696b7f3 100644 --- a/src/widgets/autoexpandingtreeview.h +++ b/src/widgets/autoexpandingtreeview.h @@ -33,9 +33,12 @@ public: public slots: void RecursivelyExpand(const QModelIndex &index); + void UpAndFocus(); + void DownAndFocus(); signals: void AddToPlaylistSignal(QMimeData* data); + void FocusOnFilterSignal(QKeyEvent* event); protected: // QAbstractItemView @@ -43,6 +46,7 @@ protected: // QWidget void mousePressEvent(QMouseEvent* event); + void keyPressEvent(QKeyEvent* event); virtual bool CanRecursivelyExpand(const QModelIndex& index) const { return true; }