From cda217281dc6991d10f2b567321180f1cfb37f5f Mon Sep 17 00:00:00 2001 From: Christian Surlykke Date: Wed, 23 Jan 2013 08:39:22 +0100 Subject: [PATCH] Search code cleanup --- lib/SearchBar.cpp | 79 ++++++++++++++++++--------------------------- lib/SearchBar.h | 16 ++++----- lib/qtermwidget.cpp | 28 +++++++++++++--- lib/qtermwidget.h | 5 ++- 4 files changed, 68 insertions(+), 60 deletions(-) diff --git a/lib/SearchBar.cpp b/lib/SearchBar.cpp index 12ceb70..21e7325 100644 --- a/lib/SearchBar.cpp +++ b/lib/SearchBar.cpp @@ -23,15 +23,15 @@ #include "SearchBar.h" -SearchBar::SearchBar(QWidget *parent) : - QWidget(parent), - m_regExp("") +SearchBar::SearchBar(QWidget *parent) : QWidget(parent) { widget.setupUi(this); connect(widget.closeButton, SIGNAL(clicked()), this, SLOT(hide())); - connect(widget.searchTextEdit, SIGNAL(textChanged(QString)), this, SLOT(searchTextChanged())); - connect(widget.findPreviousButton, SIGNAL(clicked()), this, SLOT(findPrevious())); - connect(widget.findNextButton, SIGNAL(clicked()), this, SLOT(findNext())); + connect(widget.searchTextEdit, SIGNAL(textChanged(QString)), this, SIGNAL(searchCriteriaChanged())); + connect(widget.findPreviousButton, SIGNAL(clicked()), this, SIGNAL(findPrevious())); + connect(widget.findNextButton, SIGNAL(clicked()), this, SIGNAL(findNext())); + + connect(this, SIGNAL(searchCriteriaChanged()), this, SLOT(clearBackgroundColor())); QMenu *optionsMenu = new QMenu(widget.optionsButton); widget.optionsButton->setMenu(optionsMenu); @@ -39,34 +39,49 @@ SearchBar::SearchBar(QWidget *parent) : m_matchCaseMenuEntry = optionsMenu->addAction(tr("Match case")); m_matchCaseMenuEntry->setCheckable(true); m_matchCaseMenuEntry->setChecked(true); + connect(m_matchCaseMenuEntry, SIGNAL(toggled(bool)), this, SIGNAL(searchCriteriaChanged())); + m_useRegularExpressionMenuEntry = optionsMenu->addAction(tr("Regular expression")); m_useRegularExpressionMenuEntry->setCheckable(true); + connect(m_useRegularExpressionMenuEntry, SIGNAL(toggled(bool)), this, SIGNAL(searchCriteriaChanged())); m_highlightMatchesMenuEntry = optionsMenu->addAction(tr("Higlight all matches")); m_highlightMatchesMenuEntry->setCheckable(true); m_highlightMatchesMenuEntry->setChecked(true); + connect(m_highlightMatchesMenuEntry, SIGNAL(toggled(bool)), this, SIGNAL(highlightMatchesChanged(bool))); } SearchBar::~SearchBar() { } +QString SearchBar::searchText() +{ + return widget.searchTextEdit->text(); +} + + +bool SearchBar::useRegularExpression() +{ + return m_useRegularExpressionMenuEntry->isChecked(); +} + +bool SearchBar::matchCase() +{ + return m_matchCaseMenuEntry->isChecked(); +} + +bool SearchBar::highlightAllMatches() +{ + return m_highlightMatchesMenuEntry->isChecked(); +} + void SearchBar::show() { QWidget::show(); widget.searchTextEdit->setFocus(); } -void SearchBar::hide() -{ - QWidget::hide(); -} - -void SearchBar::toggleShown() -{ - isHidden() ? show() : hide(); -} - void SearchBar::noMatchFound() { QPalette palette; @@ -74,24 +89,6 @@ void SearchBar::noMatchFound() widget.searchTextEdit->setPalette(palette); } -void SearchBar::searchTextChanged() -{ - m_regExp = QRegExp(widget.searchTextEdit->text()); - - if (! m_useRegularExpressionMenuEntry->isChecked()) { - m_regExp.setPatternSyntax(QRegExp::FixedString); - } - if (! m_matchCaseMenuEntry->isChecked()) { - m_regExp.setCaseSensitivity(Qt::CaseInsensitive); - } - - clearBackgroundColor(); - - if (!m_regExp.isEmpty()) - { - emit search(m_regExp, true, false); - } -} void SearchBar::keyReleaseEvent(QKeyEvent* keyEvent) { @@ -108,22 +105,10 @@ void SearchBar::keyReleaseEvent(QKeyEvent* keyEvent) } else if (keyEvent->key() == Qt::Key_Escape) { - toggleShown(); + hide(); } } -void SearchBar::findNext() -{ - clearBackgroundColor(); - emit search(m_regExp, true, true); -} - -void SearchBar::findPrevious() -{ - clearBackgroundColor(); - emit search(m_regExp, false, false); -} - void SearchBar::clearBackgroundColor() { QPalette p; diff --git a/lib/SearchBar.h b/lib/SearchBar.h index 3f0de59..3eb751f 100644 --- a/lib/SearchBar.h +++ b/lib/SearchBar.h @@ -30,27 +30,27 @@ public: SearchBar(QWidget* parent = 0); virtual ~SearchBar(); virtual void show(); - virtual void hide(); + QString searchText(); + bool useRegularExpression(); + bool matchCase(); + bool highlightAllMatches(); public slots: - void toggleShown(); void noMatchFound(); signals: - void search(QRegExp regexp, bool forwards, bool skip); + void searchCriteriaChanged(); + void highlightMatchesChanged(bool highlightMatches); + void findNext(); + void findPrevious(); protected: virtual void keyReleaseEvent(QKeyEvent* keyEvent); private slots: - void searchTextChanged(); - void findNext(); - void findPrevious(); void clearBackgroundColor(); private: - QRegExp m_regExp; - Ui::SearchBar widget; QAction *m_matchCaseMenuEntry; QAction *m_useRegularExpressionMenuEntry; diff --git a/lib/qtermwidget.cpp b/lib/qtermwidget.cpp index 5368373..5f0570e 100644 --- a/lib/qtermwidget.cpp +++ b/lib/qtermwidget.cpp @@ -126,7 +126,22 @@ void QTermWidget::selectionChanged(bool textSelected) emit copyAvailable(textSelected); } -void QTermWidget::search(QRegExp regexp, bool forwards, bool next) +void QTermWidget::find() +{ + search(true, false); +} + +void QTermWidget::findNext() +{ + search(true, true); +} + +void QTermWidget::findPrevious() +{ + search(false, false); +} + +void QTermWidget::search(bool forwards, bool next) { int startColumn, startLine; @@ -143,9 +158,12 @@ void QTermWidget::search(QRegExp regexp, bool forwards, bool next) qDebug() << "current selection starts at: " << startColumn << startLine; qDebug() << "current cursor position: " << m_impl->m_terminalDisplay->screenWindow()->cursorPosition(); + QRegExp regExp(m_searchBar->searchText()); + regExp.setPatternSyntax(m_searchBar->useRegularExpression() ? QRegExp::RegExp : QRegExp::FixedString); + regExp.setCaseSensitivity(m_searchBar->matchCase() ? Qt::CaseSensitive : Qt::CaseInsensitive); HistorySearch *historySearch = - new HistorySearch(m_impl->m_session->emulation(), regexp, forwards, startColumn, startLine, this); + new HistorySearch(m_impl->m_session->emulation(), regExp, forwards, startColumn, startLine, this); connect(historySearch, SIGNAL(matchFound(int, int, int, int)), this, SLOT(matchFound(int, int, int, int))); connect(historySearch, SIGNAL(noMatchFound()), this, SLOT(noMatchFound())); connect(historySearch, SIGNAL(noMatchFound()), m_searchBar, SLOT(noMatchFound())); @@ -221,7 +239,9 @@ void QTermWidget::init(int startnow) m_searchBar = new SearchBar(this); m_searchBar->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Maximum); - connect(m_searchBar, SIGNAL(search(QRegExp, bool, bool)), this, SLOT(search(QRegExp, bool, bool))); + connect(m_searchBar, SIGNAL(searchCriteriaChanged()), this, SLOT(find())); + connect(m_searchBar, SIGNAL(findNext()), this, SLOT(findNext())); + connect(m_searchBar, SIGNAL(findPrevious()), this, SLOT(findPrevious())); m_layout->addWidget(m_searchBar); m_searchBar->hide(); @@ -450,7 +470,7 @@ QString QTermWidget::keyBindings() void QTermWidget::toggleShowSearchBar() { - m_searchBar->toggleShown(); + m_searchBar->isHidden() ? m_searchBar->show() : m_searchBar->hide(); } bool QTermWidget::flowControlEnabled(void) diff --git a/lib/qtermwidget.h b/lib/qtermwidget.h index dea6155..51a2352 100644 --- a/lib/qtermwidget.h +++ b/lib/qtermwidget.h @@ -160,11 +160,14 @@ protected slots: void selectionChanged(bool textSelected); private slots: - void search(QRegExp, bool forwards, bool skip); + void find(); + void findNext(); + void findPrevious(); void matchFound(int startColumn, int startLine, int endColumn, int endLine); void noMatchFound(); private: + void search(bool forwards, bool next); void setZoom(int step); void init(int startnow); TermWidgetImpl * m_impl;