File: searchtoolbar.cpp

package info (click to toggle)
markdownpart 25.04.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 396 kB
  • sloc: cpp: 632; xml: 164; makefile: 3; sh: 2
file content (113 lines) | stat: -rw-r--r-- 3,376 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
    SPDX-FileCopyrightText: 2017, 2020 Friedrich W. H. Kossebau <kossebau@kde.org>

    SPDX-License-Identifier: LGPL-2.1-or-later
*/

#include "searchtoolbar.hpp"

// part
#include "ui_searchtoolbar.h"
// Qt
#include <QTextBrowser>
#include <QTextCursor>


SearchToolBar::SearchToolBar(QTextBrowser* markdownView, QWidget* parent)
    : QWidget(parent)
    , m_ui(new Ui::SearchToolBar)
    , m_markdownView(markdownView)
{
    m_ui->setupUi(this);

    connect(m_ui->hideButton, &QToolButton::clicked,
            this, &SearchToolBar::hide);
    connect(m_ui->searchTextEdit, &QLineEdit::textEdited,
            this, &SearchToolBar::searchIncrementally);
    connect(m_ui->matchCaseCheckButton, &QAbstractButton::toggled,
            this, &SearchToolBar::searchIncrementally);
    connect(m_ui->searchTextEdit, &QLineEdit::returnPressed,
            this, &SearchToolBar::searchNext);
    connect(m_ui->nextButton, &QToolButton::clicked,
            this, &SearchToolBar::searchNext);
    connect(m_ui->previousButton, &QToolButton::clicked,
            this, &SearchToolBar::searchPrevious);
    // TODO: disable next/previous buttons if no (more) search hits, color coding in text field
}

SearchToolBar::~SearchToolBar() = default;

void SearchToolBar::searchNext()
{
    const QString text = m_ui->searchTextEdit->text();
    if (text.isEmpty()) {
        startSearch();
        return;
    }

    QTextDocument::FindFlags findFlags = {};
    if (m_ui->matchCaseCheckButton->isChecked()) {
        findFlags |= QTextDocument::FindCaseSensitively;
    }

    m_markdownView->find(text, findFlags);

}

void SearchToolBar::searchPrevious()
{
    const QString text = m_ui->searchTextEdit->text();
    if (text.isEmpty()) {
        startSearch();
        return;
    }

    QTextDocument::FindFlags findFlags = {QTextDocument::FindBackward};
    if (m_ui->matchCaseCheckButton->isChecked()) {
        findFlags |= QTextDocument::FindCaseSensitively;
    }

    m_markdownView->find(text, findFlags);
}

void SearchToolBar::startSearch()
{
    // if there is seleted text, assume the user wants to search that selected text
    // otherwise reuse any old term
    const QString selectedText = m_markdownView->textCursor().selectedText();
    if (!selectedText.isEmpty()) {
        m_ui->searchTextEdit->setText(selectedText);
    }

    show();
    m_ui->searchTextEdit->selectAll();
    m_ui->searchTextEdit->setFocus();
}

void SearchToolBar::searchIncrementally()
{
    QTextDocument::FindFlags findFlags = {};

    if (m_ui->matchCaseCheckButton->isChecked()) {
        findFlags |= QTextDocument::FindCaseSensitively;
    }

    // calling with changed text with added or removed chars at end will result in current
    // selection kept, if also matching new text
    // behaviour on changed case sensitivity though is advancing to next match even if current
    // would be still matching. as there is no control about currently shown match, nothing
    // we can do about it. thankfully case sensitivity does not happen too often, so should
    // not be too grave UX
    m_markdownView->find(m_ui->searchTextEdit->text(), findFlags);
}

void SearchToolBar::hideEvent(QHideEvent* event)
{
    // finish search
    // passing emptry string to reset search
    m_markdownView->find(QString());

    QWidget::hideEvent(event);
}

#include "moc_searchtoolbar.cpp"