From ddcb197a6ba0c61bdedb05466536ece9c02351f6 Mon Sep 17 00:00:00 2001 From: birkeh Date: Wed, 20 Jun 2018 16:19:28 +0200 Subject: [PATCH] added search function --- cmainwindow.cpp | 181 ++++++++++++++++++++++++++++++++++++++++++++++-- cmainwindow.h | 17 ++++- cmainwindow.ui | 10 ++- 3 files changed, 200 insertions(+), 8 deletions(-) diff --git a/cmainwindow.cpp b/cmainwindow.cpp index 2d4c02b..f190368 100644 --- a/cmainwindow.cpp +++ b/cmainwindow.cpp @@ -39,6 +39,8 @@ #include +#include + static bool serieSort(cSerie* s1, cSerie* s2) { @@ -76,7 +78,10 @@ cMainWindow::cMainWindow(QWidget *parent) : m_lpUpdateThread(0), m_lpPicturesThread(0), m_bProcessing(false), - m_lpShortcut(0) + m_lpShortcutAdd(0), + m_lpShortcutFind(0), + m_szFind(""), + m_szFindMovie("") { m_timer.start(); @@ -158,8 +163,14 @@ cMainWindow::cMainWindow(QWidget *parent) : connect(ui->m_lpSeriesList1->verticalScrollBar(), &QScrollBar::valueChanged, this, &cMainWindow::scrollbarValueChanged1); connect(ui->m_lpSeriesList2->verticalScrollBar(), &QScrollBar::valueChanged, this, &cMainWindow::scrollbarValueChanged2); - m_lpShortcut = new QShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_A), this, SLOT(onActionAddGlobal())); - m_lpShortcut->setAutoRepeat(false); + m_lpShortcutAdd = new QShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_A), this, SLOT(onActionAddGlobal())); + m_lpShortcutAdd->setAutoRepeat(false); + + m_lpShortcutFind = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_F), this, SLOT(onActionFindGlobal())); + m_lpShortcutFind->setAutoRepeat(false); + + m_lpShortcutFindAgain = new QShortcut(QKeySequence(Qt::Key_F3), this, SLOT(onActionFindAgainGlobal())); + m_lpShortcutFindAgain->setAutoRepeat(false); applySeriesFilter(); applyMoviesFilter(); @@ -167,8 +178,12 @@ cMainWindow::cMainWindow(QWidget *parent) : cMainWindow::~cMainWindow() { - if(m_lpShortcut) - delete m_lpShortcut; + if(m_lpShortcutAdd) + delete m_lpShortcutAdd; + if(m_lpShortcutFind) + delete m_lpShortcutFind; + if(m_lpShortcutFindAgain) + delete m_lpShortcutFindAgain; delete ui; } @@ -2146,3 +2161,159 @@ bool cMainWindow::checkState(const Qt::CheckState& state, bool bDesiredState) return(false); } + +void cMainWindow::onActionFindGlobal() +{ + if(ui->m_lpMainTab->currentIndex() == 0) + onActionFind(); + else + onActionMovieFind(); +} + +void cMainWindow::onActionFindAgainGlobal() +{ + if(ui->m_lpMainTab->currentIndex() == 0) + onActionFindAgain(); + else + onActionMovieFindAgain(); +} + +void cMainWindow::onActionFind() +{ + bool ok; + QString szText = QInputDialog::getText(this, tr("Find"), tr("Find:"), QLineEdit::Normal, m_szFind, &ok); + + if(ok && !szText.isEmpty()) + { + m_szFind = szText; + find(); + } +} + +void cMainWindow::onActionFindAgain() +{ + if(m_szFind.isEmpty()) + onActionFind(); + else + find(); +} + +void cMainWindow::onActionMovieFind() +{ + bool ok; + QString szText = QInputDialog::getText(this, tr("Find"), tr("Find:"), QLineEdit::Normal, m_szFindMovie, &ok); + + if(ok && !szText.isEmpty()) + { + m_szFindMovie = szText; + findMovie(); + } +} + +void cMainWindow::onActionMovieFindAgain() +{ + if(m_szFindMovie.isEmpty()) + onActionMovieFind(); + else + findMovie(); +} + +void cMainWindow::find() +{ + if(m_szFind.isEmpty()) + return; + + if(!m_lpSeriesListModel->rowCount()) + return; + + QModelIndex index = ui->m_lpSeriesList1->currentIndex(); + qint32 iSelected = -1; + + if(index.isValid()) + iSelected = index.row(); + + iSelected++; + if(iSelected == m_lpSeriesListModel->rowCount()) + iSelected = 0; + + qint32 iFirst = iSelected; + + for(int x = iSelected;x < m_lpSeriesListModel->rowCount();x++) + { + QStandardItem* lpItem = m_lpSeriesListModel->item(x, 1); + if(lpItem->text().contains(m_szFind, Qt::CaseInsensitive)) + { + ui->m_lpSeriesList1->selectionModel()->setCurrentIndex(lpItem->index(), QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows); + ui->m_lpSeriesList1->scrollTo(lpItem->index()); + ui->m_lpSeriesList1->setFocus(); + return; + } + } + + if(iFirst) + { + for(int x = 0;x < iFirst;x++) + { + QStandardItem* lpItem = m_lpSeriesListModel->item(x, 1); + if(lpItem->text().contains(m_szFind, Qt::CaseInsensitive)) + { + ui->m_lpSeriesList1->selectionModel()->setCurrentIndex(lpItem->index(), QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows); + ui->m_lpSeriesList1->scrollTo(lpItem->index()); + ui->m_lpSeriesList1->setFocus(); + return; + } + } + } + + QMessageBox::information(this, "Find", "Nothing found."); +} + +void cMainWindow::findMovie() +{ + if(m_szFindMovie.isEmpty()) + return; + + if(!m_lpMoviesListModel->rowCount()) + return; + + QModelIndex index = ui->m_lpMoviesList->currentIndex(); + qint32 iSelected = -1; + + if(index.isValid()) + iSelected = index.row(); + + iSelected++; + if(iSelected == m_lpMoviesListModel->rowCount()) + iSelected = 0; + + qint32 iFirst = iSelected; + + for(int x = iSelected;x < m_lpMoviesListModel->rowCount();x++) + { + QStandardItem* lpItem = m_lpMoviesListModel->item(x, 0); + if(lpItem->text().contains(m_szFindMovie, Qt::CaseInsensitive)) + { + ui->m_lpMoviesList->selectionModel()->setCurrentIndex(lpItem->index(), QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows); + ui->m_lpMoviesList->scrollTo(lpItem->index()); + ui->m_lpMoviesList->setFocus(); + return; + } + } + + if(iFirst) + { + for(int x = 0;x < iFirst;x++) + { + QStandardItem* lpItem = m_lpMoviesListModel->item(x, 0); + if(lpItem->text().contains(m_szFindMovie, Qt::CaseInsensitive)) + { + ui->m_lpMoviesList->selectionModel()->setCurrentIndex(lpItem->index(), QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows); + ui->m_lpMoviesList->scrollTo(lpItem->index()); + ui->m_lpMoviesList->setFocus(); + return; + } + } + } + + QMessageBox::information(this, "Find", "Nothing found."); +} diff --git a/cmainwindow.h b/cmainwindow.h index 1ba2844..f8d6aa6 100644 --- a/cmainwindow.h +++ b/cmainwindow.h @@ -59,6 +59,8 @@ private slots: void onActionCopyDownload(); void onActionGotoIMDB(); void onActionLoadPictures(); + void onActionFind(); + void onActionFindAgain(); void onActionGotoAllDownload(); void onActionGotoAllDownloadOpen(); @@ -70,10 +72,13 @@ private slots: void onActionMovieEdit(); void onActionMovieGotoIMDB(); void onActionMovieLoadPictures(); - + void onActionMovieFind(); + void onActionMovieFindAgain(); void onActionExport(); void onActionAddGlobal(); + void onActionFindGlobal(); + void onActionFindAgainGlobal(); void updateMessage(const QString& szMessage, const qint32 &iProgress); void updateAppendMessage(const QString& szMessage); @@ -121,7 +126,12 @@ private: bool m_bProcessing; - QShortcut* m_lpShortcut; + QShortcut* m_lpShortcutAdd; + QShortcut* m_lpShortcutFind; + QShortcut* m_lpShortcutFindAgain; + + QString m_szFind; + QString m_szFindMovie; void initDB(); void loadDB(); @@ -151,6 +161,9 @@ private: void applyMoviesFilter(QStandardItem* lpParent); bool checkState(const Qt::CheckState &state, bool bDesiredState); + + void find(); + void findMovie(); protected: void closeEvent(QCloseEvent *event); }; diff --git a/cmainwindow.ui b/cmainwindow.ui index 9635063..a22985e 100644 --- a/cmainwindow.ui +++ b/cmainwindow.ui @@ -18,7 +18,7 @@ - 1 + 0 @@ -401,6 +401,14 @@ + + + &find... + + + Ctrl+F + +