From 3a4257c51fa37e031b21befae3ea4591abef498a Mon Sep 17 00:00:00 2001 From: reionwong Date: Tue, 28 Sep 2021 12:00:19 +0800 Subject: [PATCH] Fix keyboard search --- helper/shortcut.cpp | 3 ++- helper/shortcut.h | 1 + model/foldermodel.cpp | 58 +++++++++++++++++-------------------------- model/foldermodel.h | 5 +--- qml/Desktop/Main.qml | 3 +++ qml/FolderPage.qml | 3 +++ 6 files changed, 33 insertions(+), 40 deletions(-) diff --git a/helper/shortcut.cpp b/helper/shortcut.cpp index 2200c72..764957d 100644 --- a/helper/shortcut.cpp +++ b/helper/shortcut.cpp @@ -70,7 +70,8 @@ bool ShortCut::eventFilter(QObject *obj, QEvent *e) emit refresh(); } else if (keyEvent->key() >= Qt::Key_A && keyEvent->key() <= Qt::Key_Z) { // Handle select - KeyboardSearchManager::self()->addKeys(keyEvent->text()); + // KeyboardSearchManager::self()->addKeys(keyEvent->text()); + emit keyPressed(keyEvent->text()); keyEvent->ignore(); } } diff --git a/helper/shortcut.h b/helper/shortcut.h index 490b724..25b2713 100644 --- a/helper/shortcut.h +++ b/helper/shortcut.h @@ -44,6 +44,7 @@ signals: void selectAll(); void backspace(); void deleteFile(); + void keyPressed(const QString &text); protected: bool eventFilter(QObject *obj, QEvent *e) override; diff --git a/model/foldermodel.cpp b/model/foldermodel.cpp index 7facc0e..3b05de1 100644 --- a/model/foldermodel.cpp +++ b/model/foldermodel.cpp @@ -90,7 +90,6 @@ FolderModel::FolderModel(QObject *parent) , m_viewAdapter(nullptr) , m_mimeAppManager(MimeAppManager::self()) , m_sizeJob(nullptr) - , m_keyboardSearchManager(KeyboardSearchManager::self()) { QSettings settings("cutefishos", qApp->applicationName()); m_showHiddenFiles = settings.value("showHiddenFiles", false).toBool(); @@ -120,9 +119,6 @@ FolderModel::FolderModel(QObject *parent) connect(this, SIGNAL(rowsInserted(QModelIndex, int, int)), SIGNAL(countChanged())); connect(this, SIGNAL(rowsRemoved(QModelIndex, int, int)), SIGNAL(countChanged())); connect(this, SIGNAL(modelReset()), SIGNAL(countChanged())); - - connect(m_keyboardSearchManager, &KeyboardSearchManager::searchTextChanged, - this, &FolderModel::keyboardSearchChanged); } FolderModel::~FolderModel() @@ -1164,6 +1160,29 @@ void FolderModel::updateSelectedItemsSize() { } +void FolderModel::keyboardSearch(const QString &text) +{ + if (rowCount() == 0) + return; + + int index; + int currentIndex = -1; + + if (m_selectionModel->hasSelection()) { + currentIndex = m_selectionModel->selectedIndexes().first().row(); + } + + index = indexForKeyboardSearch(text, (currentIndex + 1) % rowCount()); + + if (index < 0 || currentIndex == index) + return; + + if (index >= 0) { + clearSelection(); + setSelected(index); + } +} + void FolderModel::restoreFromTrash() { if (!m_selectionModel->hasSelection()) @@ -1298,37 +1317,6 @@ void FolderModel::dragSelectedInternal(int x, int y) } } -void FolderModel::keyboardSearchChanged(const QString &text, bool searchFromNextItem) -{ - Q_UNUSED(searchFromNextItem); - - if (rowCount() == 0) - return; - - int index; - int currentIndex = -1; - - if (m_selectionModel->hasSelection()) { - currentIndex = m_selectionModel->selectedIndexes().first().row(); - } - -// if (searchFromNextItem) { -// index = indexForKeyboardSearch(text, (currentIndex + 1) % rowCount()); -// } else { -// index = indexForKeyboardSearch(text, 0); -// } - - index = indexForKeyboardSearch(text, (currentIndex + 1) % rowCount()); - - if (index < 0 || currentIndex == index) - return; - - if (index >= 0) { - clearSelection(); - setSelected(index); - } -} - bool FolderModel::isSupportThumbnails(const QString &mimeType) const { const QStringList supportsMimetypes = {"image/bmp", "image/png", "image/gif", "image/jpeg", "image/web", diff --git a/model/foldermodel.h b/model/foldermodel.h index b82fed4..2584c47 100644 --- a/model/foldermodel.h +++ b/model/foldermodel.h @@ -26,7 +26,6 @@ #define FOLDERMODEL_H #include "../widgets/itemviewadapter.h" -#include "../helper/keyboardsearchmanager.h" #include "../helper/pathhistory.h" #include "../mimetype/mimeappmanager.h" @@ -207,6 +206,7 @@ public: Q_INVOKABLE void openDeleteDialog(); Q_INVOKABLE void updateSelectedItemsSize(); + Q_INVOKABLE void keyboardSearch(const QString &text); void restoreFromTrash(); @@ -242,7 +242,6 @@ signals: private slots: void selectionChanged(const QItemSelection &selected, const QItemSelection &deselected); void dragSelectedInternal(int x, int y); - void keyboardSearchChanged(const QString &text, bool searchFromNextItem); private: void invalidateIfComplete(); @@ -299,8 +298,6 @@ private: MimeAppManager *m_mimeAppManager; CFileSizeJob *m_sizeJob; - - KeyboardSearchManager *m_keyboardSearchManager; }; #endif // FOLDERMODEL_H diff --git a/qml/Desktop/Main.qml b/qml/Desktop/Main.qml index 50b894d..b5ad0e2 100644 --- a/qml/Desktop/Main.qml +++ b/qml/Desktop/Main.qml @@ -134,6 +134,9 @@ Item { onDeleteFile: { dirModel.keyDeletePress() } + onKeyPressed: { + dirModel.keyboardSearch(text) + } } Component { diff --git a/qml/FolderPage.qml b/qml/FolderPage.qml index 0a398d5..279fc2c 100644 --- a/qml/FolderPage.qml +++ b/qml/FolderPage.qml @@ -394,6 +394,9 @@ Item { onRefresh: { dirModel.refresh() } + onKeyPressed: { + dirModel.keyboardSearch(text) + } } function openUrl(url) {