From 179992bbde9b16a1a1bd0c0ea6f61027cd7789f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dag=20Henning=20Liodden=20S=C3=B8rb=C3=B8?= Date: Fri, 10 Nov 2017 00:22:42 +0100 Subject: [PATCH] Add search to template chooser (see #183336) --- mscore/newwizard.cpp | 14 +++++++++++- mscore/scoreBrowser.cpp | 49 +++++++++++++++++++++++++++++++++++++++-- mscore/scoreBrowser.h | 2 ++ 3 files changed, 62 insertions(+), 3 deletions(-) diff --git a/mscore/newwizard.cpp b/mscore/newwizard.cpp index e38084b650..f116972095 100644 --- a/mscore/newwizard.cpp +++ b/mscore/newwizard.cpp @@ -287,12 +287,24 @@ NewWizardPage4::NewWizardPage4(QWidget* parent) templateFileBrowser->setScores(fil); templateFileBrowser->setSizePolicy(QSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored)); - QLayout* layout = new QVBoxLayout; + QVBoxLayout* layout = new QVBoxLayout; + QHBoxLayout* searchLayout = new QHBoxLayout; + QLineEdit* search = new QLineEdit; + search->setPlaceholderText(tr("Search")); + search->setClearButtonEnabled(true); + search->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed); + searchLayout->addSpacerItem(new QSpacerItem(0, 0, QSizePolicy::MinimumExpanding, QSizePolicy::Maximum)); + searchLayout->addWidget(search); + + layout->addLayout(searchLayout); layout->addWidget(templateFileBrowser); setLayout(layout); connect(templateFileBrowser, SIGNAL(scoreSelected(const QString&)), SLOT(templateChanged(const QString&))); connect(templateFileBrowser, SIGNAL(scoreActivated(const QString&)), SLOT(fileAccepted(const QString&))); + connect(search, &QLineEdit::textChanged, [this] (const QString& searchString) { + this->templateFileBrowser->filter(searchString); + }); } //--------------------------------------------------------- diff --git a/mscore/scoreBrowser.cpp b/mscore/scoreBrowser.cpp index 9c00ae3780..b7d1415f23 100644 --- a/mscore/scoreBrowser.cpp +++ b/mscore/scoreBrowser.cpp @@ -24,7 +24,12 @@ namespace Ms { QSize ScoreListWidget::sizeHint() const { int cols = (width()-SPACE) / (CELLW + SPACE); - int n = count(); + int n = 0; + for (int i = 0; i < count(); ++i) { + if (!item(i)->isHidden()) + n++; + } + int rows = 1; if (cols > 0) rows = (n+cols-1) / cols; @@ -57,6 +62,9 @@ ScoreBrowser::ScoreBrowser(QWidget* parent) scoreList->setLayout(new QVBoxLayout); scoreList->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred); scoreList->layout()->setMargin(0); + _noMatchedScoresLabel = new QLabel(tr("There are no templates matching the current search.")); + _noMatchedScoresLabel->setHidden(true); + scoreList->layout()->addWidget(_noMatchedScoresLabel); connect(preview, SIGNAL(doubleClicked(QString)), SIGNAL(scoreActivated(QString))); if (!_showPreview) preview->setVisible(false); @@ -263,7 +271,44 @@ void ScoreBrowser::selectLast() ScoreItem* item = static_cast(w->item(w->count()-1)); w->setCurrentItem(item); preview->setScore(item->info()); - } +} + +//--------------------------------------------------------- +// filter +// filter which scores are visible based on searchString +//--------------------------------------------------------- +void ScoreBrowser::filter(const QString &searchString) +{ + int numCategoriesWithMathingScores = 0; + + for (ScoreListWidget* list : scoreLists) { + int numMatchedScores = 0; + + for (int i = 0; i < list->count(); ++i) { + ScoreItem* score = static_cast(list->item(i)); + QString title = score->text(); + bool isMatch = title.contains(searchString, Qt::CaseInsensitive); + + score->setHidden(!isMatch); + + if (isMatch) + numMatchedScores++; + } + + int listindex = scoreList->layout()->indexOf(list); + QLayoutItem *label = scoreList->layout()->itemAt(listindex - 1); + if (label && label->widget()) { + label->widget()->setHidden(numMatchedScores == 0); + } + + list->setHidden(numMatchedScores == 0); + if (numMatchedScores > 0) { + numCategoriesWithMathingScores++; + } + } + + _noMatchedScoresLabel->setHidden(numCategoriesWithMathingScores > 0); +} //--------------------------------------------------------- // scoreChanged diff --git a/mscore/scoreBrowser.h b/mscore/scoreBrowser.h index f836c241c3..eef100edcb 100644 --- a/mscore/scoreBrowser.h +++ b/mscore/scoreBrowser.h @@ -55,6 +55,7 @@ class ScoreBrowser : public QWidget, public Ui::ScoreBrowser // - single click action bool _boldTitle { false }; // score title are displayed in bold bool _showCustomCategory { false };// show a custom category for files + QLabel* _noMatchedScoresLabel; // displayed when no scores are matching the search ScoreListWidget* createScoreList(); ScoreItem* genScoreItem(const QFileInfo&, ScoreListWidget*); @@ -76,6 +77,7 @@ class ScoreBrowser : public QWidget, public Ui::ScoreBrowser void selectLast(); void setBoldTitle(bool bold) { _boldTitle = bold; } void setShowCustomCategory(bool showCustomCategory) { _showCustomCategory = showCustomCategory; } + void filter(const QString&); }; }