KTextWidgets

kreplacedialog.cpp
1/*
2 This file is part of the KDE project
3 SPDX-FileCopyrightText: 2001 S.R. Haque <srhaque@iee.org>.
4 SPDX-FileCopyrightText: 2002 David Faure <david@mandrakesoft.com>
5
6 SPDX-License-Identifier: LGPL-2.0-only
7*/
8
9#include "kreplacedialog.h"
10#include "kfinddialog_p.h"
11
12#include <QCheckBox>
13#include <QGridLayout>
14#include <QGroupBox>
15#include <QLineEdit>
16#include <QRegularExpression>
17
18#include <KHistoryComboBox>
19#include <KLocalizedString>
20#include <KMessageBox>
21
22/**
23 * we need to insert the strings after the dialog is set
24 * up, otherwise QComboBox will deliver an awful big sizeHint
25 * for long replacement texts.
26 */
27class KReplaceDialogPrivate : public KFindDialogPrivate
28{
29 Q_DECLARE_PUBLIC(KReplaceDialog)
30
31public:
32 explicit KReplaceDialogPrivate(KReplaceDialog *qq)
33 : KFindDialogPrivate(qq)
34 {
35 }
36
37 void slotOk();
38
39 QStringList replaceStrings;
40 mutable QWidget *replaceExtension = nullptr;
41 bool initialShowDone = false;
42};
43
44KReplaceDialog::KReplaceDialog(QWidget *parent, long options, const QStringList &findStrings, const QStringList &replaceStrings, bool hasSelection)
45 : KFindDialog(*new KReplaceDialogPrivate(this), parent, options, findStrings, hasSelection, true /*create replace dialog*/)
46{
48
49 d->replaceStrings = replaceStrings;
50}
51
53
54void KReplaceDialog::showEvent(QShowEvent *e)
55{
57
58 if (!d->initialShowDone) {
59 d->initialShowDone = true; // only once
60
61 if (!d->replaceStrings.isEmpty()) {
62 setReplacementHistory(d->replaceStrings);
63 d->replace->lineEdit()->setText(d->replaceStrings[0]);
64 }
65 }
66
67 KFindDialog::showEvent(e);
68}
69
71{
72 Q_D(const KReplaceDialog);
73
74 long options = 0;
75
77 if (d->promptOnReplace->isChecked()) {
79 }
80 if (d->backRef->isChecked()) {
81 options |= BackReference;
82 }
83 return options;
84}
85
87{
88 Q_D(const KReplaceDialog);
89
90 if (!d->replaceExtension) {
91 d->replaceExtension = new QWidget(d->replaceGrp);
92 d->replaceLayout->addWidget(d->replaceExtension, 3, 0, 1, 2);
93 }
94
95 return d->replaceExtension;
96}
97
99{
100 Q_D(const KReplaceDialog);
101
102 return d->replace->currentText();
103}
104
106{
107 Q_D(const KReplaceDialog);
108
109 QStringList lst = d->replace->historyItems();
110 // historyItems() doesn't tell us about the case of replacing with an empty string
111 if (d->replace->lineEdit()->text().isEmpty()) {
112 lst.prepend(QString());
113 }
114 return lst;
115}
116
118{
120
122 d->promptOnReplace->setChecked(options & PromptOnReplace);
123 d->backRef->setChecked(options & BackReference);
124}
125
127{
129
130 if (!strings.isEmpty()) {
131 d->replace->setHistoryItems(strings, true);
132 } else {
133 d->replace->clearHistory();
134 }
135}
136
137void KReplaceDialogPrivate::slotOk()
138{
139 Q_Q(KReplaceDialog);
140
141 // If regex and backrefs are enabled, do a sanity check.
142 if (regExp->isChecked() && backRef->isChecked()) {
144 const int caps = re.captureCount();
145
146 const QString rep = q->replacement();
147 const static QRegularExpression check(QStringLiteral("((?:\\\\)+)(\\d+)"));
148 auto iter = check.globalMatch(rep);
149 while (iter.hasNext()) {
150 const QRegularExpressionMatch match = iter.next();
151 if ((match.captured(1).size() % 2) && match.captured(2).toInt() > caps) {
153 i18n("Your replacement string is referencing a capture greater than '\\%1', ", caps)
154 + (caps ? i18np("but your pattern only defines 1 capture.", "but your pattern only defines %1 captures.", caps)
155 : i18n("but your pattern defines no captures."))
156 + i18n("\nPlease correct."));
157 return; // abort OKing
158 }
159 }
160 }
161
162 slotOk();
163 replace->addToHistory(q->replacement());
164}
165
166#include "moc_kreplacedialog.cpp"
A generic "find" dialog.
Definition kfinddialog.h:66
long options() const
Returns the state of the options.
void setOptions(long options)
Set the options which are checked.
A generic "replace" dialog.
QWidget * replaceExtension() const
Returns an empty widget which the user may fill with additional UI elements as required.
~KReplaceDialog() override
Destructor.
QStringList replacementHistory() const
Returns the list of history items.
long options() const
Returns the state of the options.
void setReplacementHistory(const QStringList &history)
Provide the list of strings to be displayed as the history of replacement strings.
void setOptions(long options)
Set the options which are enabled.
KReplaceDialog(QWidget *parent=nullptr, long options=0, const QStringList &findStrings=QStringList(), const QStringList &replaceStrings=QStringList(), bool hasSelection=true)
Construct a replace dialog.read-only or rather select-only combo box with a parent object and a name.
QString replacement() const
Returns the replacement string.
@ PromptOnReplace
Should the user be prompted before the replace operation?
QString i18np(const char *singular, const char *plural, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
void information(QWidget *parent, const QString &text, const QString &title=QString(), const QString &dontShowAgainName=QString(), Options options=Notify)
const QList< QKeySequence > & replace()
bool isEmpty() const const
void prepend(parameter_type value)
void replace(qsizetype i, parameter_type value)
QWidget(QWidget *parent, Qt::WindowFlags f)
Q_D(Todo)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:14:15 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.