KTextAddons

textfindreplacewidget.cpp
1/*
2 SPDX-FileCopyrightText: 2013-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "textfindreplacewidget.h"
8#include <KStatefulBrush>
9
10#include <KColorScheme>
11#include <KLocalizedString>
12#include <QPushButton>
13
14#include <QHBoxLayout>
15#include <QLabel>
16#include <QLineEdit>
17#include <QMenu>
18#include <QRegularExpression>
19
20using namespace Qt::Literals::StringLiterals;
21using namespace TextCustomEditor;
22
23TextReplaceWidget::TextReplaceWidget(QWidget *parent)
24 : QWidget(parent)
25 , mReplace(new QLineEdit(this))
26 , mReplaceBtn(new QPushButton(i18nc("@action:button", "Replace"), this))
27 , mReplaceAllBtn(new QPushButton(i18nc("@action:button", "Replace All"), this))
28{
29 auto lay = new QHBoxLayout(this);
30 auto label = new QLabel(i18nc("Replace text", "Replace:"), this);
31 lay->addWidget(label);
32 const int marg1 = lay->contentsMargins().left();
33 const int marg2 = lay->contentsMargins().right();
34 lay->setContentsMargins(marg1, 0, marg2, 0);
35
36 mReplace->setClearButtonEnabled(true);
37 lay->addWidget(mReplace);
38
39 mReplace->setObjectName(QStringLiteral("mReplace"));
40 mReplaceBtn->setObjectName(QStringLiteral("mReplaceBtn"));
41 mReplaceAllBtn->setObjectName(QStringLiteral("mReplaceAllBtn"));
42
43 connect(mReplaceBtn, &QPushButton::clicked, this, &TextReplaceWidget::replaceText);
44 lay->addWidget(mReplaceBtn);
45
46 connect(mReplaceAllBtn, &QPushButton::clicked, this, &TextReplaceWidget::replaceAllText);
47 lay->addWidget(mReplaceAllBtn);
48}
49
50TextReplaceWidget::~TextReplaceWidget() = default;
51
52QLineEdit *TextReplaceWidget::replaceLineEdit() const
53{
54 return mReplace;
55}
56
57void TextReplaceWidget::slotSearchStringEmpty(bool isEmpty)
58{
59 mReplaceBtn->setDisabled(isEmpty);
60 mReplaceAllBtn->setDisabled(isEmpty);
61}
62
63TextFindWidget::TextFindWidget(QWidget *parent)
64 : QWidget(parent)
65 , mSearch(new QLineEdit(this))
66 , mFindPrevBtn(new QPushButton(QIcon::fromTheme(QStringLiteral("go-up-search")), i18nc("Find and go to the previous search match", "Previous"), this))
67 , mFindNextBtn(new QPushButton(QIcon::fromTheme(QStringLiteral("go-down-search")), i18nc("Find and go to the next search match", "Next"), this))
68{
69 auto lay = new QHBoxLayout(this);
70 const int marg1 = lay->contentsMargins().left();
71 const int marg2 = lay->contentsMargins().right();
72 lay->setContentsMargins(marg1, 0, marg2, 0);
73 auto label = new QLabel(i18nc("Find text", "F&ind:"), this);
74 lay->addWidget(label);
75
76 mSearch->setToolTip(i18nc("@info:tooltip", "Text to search for"));
77 mSearch->setClearButtonEnabled(true);
78 mSearch->setObjectName(QStringLiteral("mSearch"));
79 label->setBuddy(mSearch);
80 lay->addWidget(mSearch);
81
82 mFindNextBtn->setToolTip(i18nc("@info:tooltip", "Jump to next match"));
83 lay->addWidget(mFindNextBtn);
84 mFindNextBtn->setEnabled(false);
85 mFindNextBtn->setObjectName(QStringLiteral("mFindNextBtn"));
86
87 mFindPrevBtn->setToolTip(i18nc("@info:tooltip", "Jump to previous match"));
88 lay->addWidget(mFindPrevBtn);
89 mFindPrevBtn->setEnabled(false);
90 mFindPrevBtn->setObjectName(QStringLiteral("mFindPrevBtn"));
91
92 auto optionsBtn = new QPushButton(this);
93 optionsBtn->setText(i18n("Options"));
94 optionsBtn->setToolTip(i18nc("@info:tooltip", "Modify search behavior"));
95 optionsBtn->setObjectName(QStringLiteral("optionsBtn"));
96 auto optionsMenu = new QMenu(optionsBtn);
97 mCaseSensitiveAct = optionsMenu->addAction(i18n("Case sensitive"));
98 mCaseSensitiveAct->setCheckable(true);
99
100 mWholeWordAct = optionsMenu->addAction(i18n("Whole word"));
101 mWholeWordAct->setCheckable(true);
102
103 mRegularExpressionAct = optionsMenu->addAction(i18n("Regular Expression"));
104 mRegularExpressionAct->setCheckable(true);
105
106 mRespectDiacriticAct = optionsMenu->addAction(i18n("Respect Diacritic and Accents"));
107 mRespectDiacriticAct->setCheckable(true);
108 mRespectDiacriticAct->setChecked(true);
109
110 optionsBtn->setMenu(optionsMenu);
111 lay->addWidget(optionsBtn);
112
113 connect(mFindNextBtn, &QPushButton::clicked, this, &TextFindWidget::findNext);
114 connect(mFindPrevBtn, &QPushButton::clicked, this, &TextFindWidget::findPrev);
115 connect(mCaseSensitiveAct, &QAction::toggled, this, &TextFindWidget::updateSearchOptions);
116 connect(mWholeWordAct, &QAction::toggled, this, &TextFindWidget::updateSearchOptions);
117 connect(mRespectDiacriticAct, &QAction::toggled, this, &TextFindWidget::updateSearchOptions);
118 connect(mRegularExpressionAct, &QAction::toggled, this, &TextFindWidget::slotRegularExpressionChanged);
119 connect(mSearch, &QLineEdit::textChanged, this, &TextFindWidget::slotAutoSearch);
120 connect(mSearch, &QLineEdit::returnPressed, this, &TextFindWidget::findNext);
121}
122
123TextFindWidget::~TextFindWidget() = default;
124
125void TextFindWidget::slotRegularExpressionChanged(bool b)
126{
127 mRespectDiacriticAct->setEnabled(!b);
128 Q_EMIT updateSearchOptions();
129}
130
131void TextFindWidget::setFoundMatch(bool match)
132{
133#ifndef QT_NO_STYLE_STYLESHEET
135
136 if (!mSearch->text().isEmpty()) {
137 KColorScheme::BackgroundRole bgColorScheme;
138
139 if (match) {
140 bgColorScheme = KColorScheme::PositiveBackground;
141 } else {
142 bgColorScheme = KColorScheme::NegativeBackground;
143 }
144
145 KStatefulBrush bgBrush(KColorScheme::View, bgColorScheme);
146
147 styleSheet = QStringLiteral("QLineEdit{ background-color:%1 }").arg(bgBrush.brush(mSearch->palette()).color().name());
148 }
149
150 mSearch->setStyleSheet(styleSheet);
151#endif
152}
153
154void TextFindWidget::slotAutoSearch(const QString &str)
155{
156 const bool isNotEmpty = (!str.isEmpty());
157 mFindPrevBtn->setEnabled(isNotEmpty);
158 mFindNextBtn->setEnabled(isNotEmpty);
159 Q_EMIT searchStringEmpty(!isNotEmpty);
160 Q_EMIT autoSearch(str);
161 if (str.isEmpty()) {
162 Q_EMIT clearSearch();
163 }
164}
165
166QLineEdit *TextFindWidget::searchLineEdit() const
167{
168 return mSearch;
169}
170
171bool TextFindWidget::isRegularExpression() const
172{
173 return mRegularExpressionAct->isChecked();
174}
175
176QString TextFindWidget::searchText() const
177{
178 return mSearch->text();
179}
180
181QRegularExpression TextFindWidget::searchRegularExpression() const
182{
184 if (!mCaseSensitiveAct->isChecked()) {
186 }
187 QString searchTextString = mSearch->text();
188 if (mWholeWordAct->isChecked()) {
189 searchTextString = "\\b"_L1 + searchTextString + "\\b"_L1;
190 }
191 reg.setPattern(searchTextString);
192 return reg;
193}
194
195TextEditFindBarBase::FindFlags TextFindWidget::searchOptions() const
196{
198 if (mCaseSensitiveAct->isChecked()) {
199 opt |= TextEditFindBarBase::FindCaseSensitively;
200 }
201 if (mWholeWordAct->isChecked()) {
202 opt |= TextEditFindBarBase::FindWholeWords;
203 }
204 if (mRespectDiacriticAct->isChecked()) {
205 opt |= TextEditFindBarBase::FindRespectDiacritics;
206 }
207 return opt;
208}
209
210#include "moc_textfindreplacewidget.cpp"
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
QString label(StandardShortcut id)
void clicked(bool checked)
bool isChecked() const const
void setEnabled(bool)
void toggled(bool checked)
void returnPressed()
void textChanged(const QString &text)
Q_EMITQ_EMIT
void setPattern(const QString &pattern)
void setPatternOptions(PatternOptions options)
bool isEmpty() const const
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
void setEnabled(bool)
void setDisabled(bool disable)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:29:59 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.