File indexing completed on 2024-04-14 05:45:37

0001 /*
0002     SPDX-FileCopyrightText: 2017, 2020 Friedrich W. H. Kossebau <kossebau@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-or-later
0005 */
0006 
0007 #include "searchtoolbar.hpp"
0008 
0009 // part
0010 #include "ui_searchtoolbar.h"
0011 // Qt
0012 #include <QTextBrowser>
0013 
0014 
0015 SearchToolBar::SearchToolBar(QTextBrowser* markdownView, QWidget* parent)
0016     : QWidget(parent)
0017     , m_ui(new Ui::SearchToolBar)
0018     , m_markdownView(markdownView)
0019 {
0020     m_ui->setupUi(this);
0021 
0022     connect(m_ui->hideButton, &QToolButton::clicked,
0023             this, &SearchToolBar::hide);
0024     connect(m_ui->searchTextEdit, &QLineEdit::textEdited,
0025             this, &SearchToolBar::searchIncrementally);
0026     connect(m_ui->matchCaseCheckButton, &QAbstractButton::toggled,
0027             this, &SearchToolBar::searchIncrementally);
0028     connect(m_ui->searchTextEdit, &QLineEdit::returnPressed,
0029             this, &SearchToolBar::searchNext);
0030     connect(m_ui->nextButton, &QToolButton::clicked,
0031             this, &SearchToolBar::searchNext);
0032     connect(m_ui->previousButton, &QToolButton::clicked,
0033             this, &SearchToolBar::searchPrevious);
0034     // TODO: disable next/previous buttons if no (more) search hits, color coding in text field
0035 }
0036 
0037 SearchToolBar::~SearchToolBar() = default;
0038 
0039 void SearchToolBar::searchNext()
0040 {
0041     const QString text = m_ui->searchTextEdit->text();
0042     if (text.isEmpty()) {
0043         startSearch();
0044         return;
0045     }
0046 
0047     QTextDocument::FindFlags findFlags = {};
0048     if (m_ui->matchCaseCheckButton->isChecked()) {
0049         findFlags |= QTextDocument::FindCaseSensitively;
0050     }
0051 
0052     m_markdownView->find(text, findFlags);
0053 
0054 }
0055 
0056 void SearchToolBar::searchPrevious()
0057 {
0058     const QString text = m_ui->searchTextEdit->text();
0059     if (text.isEmpty()) {
0060         startSearch();
0061         return;
0062     }
0063 
0064     QTextDocument::FindFlags findFlags = {QTextDocument::FindBackward};
0065     if (m_ui->matchCaseCheckButton->isChecked()) {
0066         findFlags |= QTextDocument::FindCaseSensitively;
0067     }
0068 
0069     m_markdownView->find(text, findFlags);
0070 }
0071 
0072 void SearchToolBar::startSearch()
0073 {
0074     show();
0075     m_ui->searchTextEdit->selectAll();
0076     m_ui->searchTextEdit->setFocus();
0077 }
0078 
0079 void SearchToolBar::searchIncrementally()
0080 {
0081     QTextDocument::FindFlags findFlags = {};
0082 
0083     if (m_ui->matchCaseCheckButton->isChecked()) {
0084         findFlags |= QTextDocument::FindCaseSensitively;
0085     }
0086 
0087     // calling with changed text with added or removed chars at end will result in current
0088     // selection kept, if also matching new text
0089     // behaviour on changed case sensitivity though is advancing to next match even if current
0090     // would be still matching. as there is no control about currently shown match, nothing
0091     // we can do about it. thankfully case sensitivity does not happen too often, so should
0092     // not be too grave UX
0093     m_markdownView->find(m_ui->searchTextEdit->text(), findFlags);
0094 }
0095 
0096 void SearchToolBar::hideEvent(QHideEvent* event)
0097 {
0098     // finish search
0099     // passing emptry string to reset search
0100     m_markdownView->find(QString());
0101 
0102     QWidget::hideEvent(event);
0103 }
0104 
0105 #include "moc_searchtoolbar.cpp"