Add search to template chooser (see #183336)

This commit is contained in:
Dag Henning Liodden Sørbø 2017-11-10 00:22:42 +01:00
parent 528c1318b9
commit 179992bbde
3 changed files with 62 additions and 3 deletions

View file

@ -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);
});
}
//---------------------------------------------------------

View file

@ -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<ScoreItem*>(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<ScoreItem*>(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

View file

@ -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&);
};
}