Skip to content

Commit

Permalink
Merge pull request #564 from Murmele/implementCaseInsensitivForTreevi…
Browse files Browse the repository at this point in the history
…ewSearch

Implement CaseInsensitive
  • Loading branch information
Murmele authored May 25, 2023
2 parents 8a0dd90 + f26c103 commit db545a2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
18 changes: 13 additions & 5 deletions src/ui/TreeWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,12 @@ TreeWidget::TreeWidget(const git::Repository &repo, QWidget *parent)
connect(mSearch, &QLineEdit::textChanged, this, &TreeWidget::search);
mcbRegex = new QCheckBox(tr("Regex"), this);
connect(mcbRegex, &QCheckBox::clicked, this, &TreeWidget::search);
mcbCaseSensitive = new QCheckBox(tr("Case Sensitive"), this);
connect(mcbCaseSensitive, &QCheckBox::clicked, this, &TreeWidget::search);
QHBoxLayout *l = new QHBoxLayout();
l->addWidget(mLabelSearch);
l->addWidget(mSearch);
l->addWidget(mcbCaseSensitive);
l->addWidget(mcbRegex);

mSearchResults = new QListWidget(this);
Expand Down Expand Up @@ -170,25 +173,30 @@ void TreeWidget::search() {
return;

bool regex = mcbRegex->isChecked();
bool caseSensitive = mcbCaseSensitive->isChecked();

QRegularExpression re(pattern, QRegularExpression::CaseInsensitiveOption);
QRegularExpression re(
pattern, caseSensitive ? QRegularExpression::NoPatternOption
: QRegularExpression::CaseInsensitiveOption);
mSuppressIndexChange = true;
mSearchResults->clear();
searchFiles(re, regex);
searchFiles(re, regex, caseSensitive);
mSuppressIndexChange = false;
}

void TreeWidget::searchFiles(const QRegularExpression &re, bool regex,
const QModelIndex &parent) {
bool caseSensitive, const QModelIndex &parent) {
for (int row = 0; row < mModel->rowCount(parent); row++) {
const auto index = mModel->index(row, 0, QModelIndex(parent));
if (mModel->rowCount(index) > 0) {
// folder
searchFiles(re, regex, index);
searchFiles(re, regex, caseSensitive, index);
} else {
// file
const QString name = mModel->data(index, Qt::EditRole).toString();
if ((!regex && name.contains(re.pattern())) ||
if ((!regex &&
name.contains(re.pattern(), caseSensitive ? Qt::CaseSensitive
: Qt::CaseInsensitive)) ||
(regex && re.match(name).hasMatch())) {
QListWidgetItem *item = new QListWidgetItem(name, mSearchResults);
item->setData(Qt::UserRole, index);
Expand Down
3 changes: 2 additions & 1 deletion src/ui/TreeWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,15 @@ private slots:

private:
void edit(const QModelIndex &index);
void searchFiles(const QRegularExpression &re, bool regex,
void searchFiles(const QRegularExpression &re, bool regex, bool caseSensitive,
const QModelIndex &parent = QModelIndex());
void loadEditorContent(const QModelIndex &index);

void selectFile(const QString &name);

QLabel *mLabelSearch;
QCheckBox *mcbRegex;
QCheckBox *mcbCaseSensitive;
QLineEdit *mSearch;
QListWidget *mSearchResults;
ColumnView *mView;
Expand Down

0 comments on commit db545a2

Please sign in to comment.