File indexing completed on 2025-01-19 04:23:30

0001 /*
0002     Copyright 2013 Christian Surlykke
0003 
0004     This program is free software; you can redistribute it and/or modify
0005     it under the terms of the GNU General Public License as published by
0006     the Free Software Foundation; either version 2 of the License, or
0007     (at your option) any later version.
0008 
0009     This program is distributed in the hope that it will be useful,
0010     but WITHOUT ANY WARRANTY; without even the implied warranty of
0011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0012     GNU General Public License for more details.
0013 
0014     You should have received a copy of the GNU General Public License
0015     along with this program; if not, write to the Free Software
0016     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
0017     02110-1301  USA.
0018 */
0019 #include <QMenu>
0020 #include <QAction>
0021 #include <QRegExp>
0022 #include <QDebug>
0023 
0024 #include "SearchBar.h"
0025 
0026 SearchBar::SearchBar(QWidget *parent) : QWidget(parent)
0027 {
0028     widget.setupUi(this);
0029     setAutoFillBackground(true); // make it always opaque, especially inside translucent windows
0030     connect(widget.closeButton, SIGNAL(clicked()), this, SLOT(hide()));
0031     connect(widget.searchTextEdit, SIGNAL(textChanged(QString)), this, SIGNAL(searchCriteriaChanged()));
0032     connect(widget.findPreviousButton, SIGNAL(clicked()), this, SIGNAL(findPrevious()));
0033     connect(widget.findNextButton, SIGNAL(clicked()), this, SIGNAL(findNext()));
0034 
0035     connect(this, SIGNAL(searchCriteriaChanged()), this, SLOT(clearBackgroundColor()));
0036 
0037     QMenu *optionsMenu = new QMenu(widget.optionsButton);
0038     widget.optionsButton->setMenu(optionsMenu);
0039 
0040     m_matchCaseMenuEntry = optionsMenu->addAction(tr("Match case"));
0041     m_matchCaseMenuEntry->setCheckable(true);
0042     m_matchCaseMenuEntry->setChecked(true);
0043     connect(m_matchCaseMenuEntry, SIGNAL(toggled(bool)), this, SIGNAL(searchCriteriaChanged()));
0044 
0045 
0046     m_useRegularExpressionMenuEntry = optionsMenu->addAction(tr("Regular expression"));
0047     m_useRegularExpressionMenuEntry->setCheckable(true);
0048     connect(m_useRegularExpressionMenuEntry, SIGNAL(toggled(bool)), this, SIGNAL(searchCriteriaChanged()));
0049 
0050     m_highlightMatchesMenuEntry = optionsMenu->addAction(tr("Highlight all matches"));
0051     m_highlightMatchesMenuEntry->setCheckable(true);
0052     m_highlightMatchesMenuEntry->setChecked(true);
0053     connect(m_highlightMatchesMenuEntry, SIGNAL(toggled(bool)), this, SIGNAL(highlightMatchesChanged(bool)));
0054 }
0055 
0056 SearchBar::~SearchBar() {
0057 }
0058 
0059 QString SearchBar::searchText()
0060 {
0061     return widget.searchTextEdit->text();
0062 }
0063 
0064 
0065 bool SearchBar::useRegularExpression()
0066 {
0067     return m_useRegularExpressionMenuEntry->isChecked();
0068 }
0069 
0070 bool SearchBar::matchCase()
0071 {
0072     return m_matchCaseMenuEntry->isChecked();
0073 }
0074 
0075 bool SearchBar::highlightAllMatches()
0076 {
0077     return m_highlightMatchesMenuEntry->isChecked();
0078 }
0079 
0080 void SearchBar::show()
0081 {
0082     QWidget::show();
0083     widget.searchTextEdit->setFocus();
0084 }
0085 
0086 void SearchBar::noMatchFound()
0087 {
0088     QPalette palette;
0089     palette.setColor(widget.searchTextEdit->backgroundRole(), QColor(255, 128, 128));
0090     widget.searchTextEdit->setPalette(palette);
0091 }
0092 
0093 
0094 void SearchBar::keyReleaseEvent(QKeyEvent* keyEvent)
0095 {
0096     if (keyEvent->key() == Qt::Key_Return || keyEvent->key() == Qt::Key_Enter)
0097     {
0098         if (keyEvent->modifiers() == Qt::ShiftModifier)
0099         {
0100             findPrevious();
0101         }
0102         else
0103         {
0104             findNext();
0105         }
0106     }
0107     else if (keyEvent->key() == Qt::Key_Escape)
0108     {
0109         hide();
0110     }
0111 }
0112 
0113 void SearchBar::clearBackgroundColor()
0114 {
0115     QPalette p;
0116     p.setColor(QPalette::Base, Qt::white);
0117     widget.searchTextEdit->setPalette(p);
0118 
0119 }