2014-11-13 01:04:59 +02:00
|
|
|
/*
|
|
|
|
Copyright (C) 2001, S.R.Haque <srhaque@iee.org>.
|
|
|
|
Copyright (C) 2002, David Faure <david@mandrakesoft.com>
|
|
|
|
This file is part of the KDE project
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Library General Public
|
|
|
|
License version 2, as published by the Free Software Foundation.
|
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Library General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Library General Public License
|
|
|
|
along with this library; see the file COPYING.LIB. If not, write to
|
|
|
|
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
|
|
Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "kreplacedialog.h"
|
|
|
|
#include "kfinddialog_p.h"
|
|
|
|
|
|
|
|
#include <QtGui/QCheckBox>
|
|
|
|
#include <QtGui/QGroupBox>
|
|
|
|
#include <QtGui/QLabel>
|
|
|
|
#include <QtGui/QLayout>
|
|
|
|
#include <QtGui/QLineEdit>
|
|
|
|
#include <QtCore/QRegExp>
|
|
|
|
#include <khistorycombobox.h>
|
|
|
|
#include <klocale.h>
|
|
|
|
#include <kmessagebox.h>
|
|
|
|
#include <kdebug.h>
|
|
|
|
|
|
|
|
/**
|
|
|
|
* we need to insert the strings after the dialog is set
|
|
|
|
* up, otherwise QComboBox will deliver an aweful big sizeHint
|
|
|
|
* for long replacement texts.
|
|
|
|
*/
|
|
|
|
class KReplaceDialogPrivate
|
|
|
|
{
|
2023-06-29 05:37:22 +03:00
|
|
|
public:
|
2014-11-13 01:04:59 +02:00
|
|
|
KReplaceDialogPrivate(KReplaceDialog *q)
|
2023-06-29 05:37:22 +03:00
|
|
|
: q(q),
|
|
|
|
initialShowDone(false),
|
|
|
|
replaceExtension(nullptr)
|
2014-11-13 01:04:59 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void _k_slotOk();
|
|
|
|
|
|
|
|
KReplaceDialog *q;
|
|
|
|
QStringList replaceStrings;
|
|
|
|
bool initialShowDone;
|
|
|
|
QWidget *replaceExtension;
|
|
|
|
};
|
|
|
|
|
|
|
|
KReplaceDialog::KReplaceDialog(QWidget *parent, long options, const QStringList &findStrings,
|
|
|
|
const QStringList &replaceStrings, bool hasSelection)
|
|
|
|
: KFindDialog(parent, options, findStrings, hasSelection, true /*create replace dialog*/),
|
|
|
|
d(new KReplaceDialogPrivate(this))
|
|
|
|
{
|
|
|
|
d->replaceStrings = replaceStrings;
|
|
|
|
}
|
|
|
|
|
|
|
|
KReplaceDialog::~KReplaceDialog()
|
|
|
|
{
|
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
2023-06-29 05:37:22 +03:00
|
|
|
void KReplaceDialog::showEvent(QShowEvent *e )
|
2014-11-13 01:04:59 +02:00
|
|
|
{
|
2023-06-29 05:37:22 +03:00
|
|
|
if (!d->initialShowDone) {
|
2014-11-13 01:04:59 +02:00
|
|
|
d->initialShowDone = true; // only once
|
|
|
|
|
2023-06-29 05:37:22 +03:00
|
|
|
if (!d->replaceStrings.isEmpty()) {
|
2014-11-13 01:04:59 +02:00
|
|
|
setReplacementHistory(d->replaceStrings);
|
2023-06-29 05:37:22 +03:00
|
|
|
KFindDialog::d->replace->lineEdit()->setText(d->replaceStrings[0]);
|
2014-11-13 01:04:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
KFindDialog::showEvent(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
long KReplaceDialog::options() const
|
|
|
|
{
|
|
|
|
long options = 0;
|
|
|
|
options = KFindDialog::options();
|
2023-06-29 05:37:22 +03:00
|
|
|
if (KFindDialog::d->promptOnReplace->isChecked()) {
|
2014-11-13 01:04:59 +02:00
|
|
|
options |= PromptOnReplace;
|
2023-06-29 05:37:22 +03:00
|
|
|
}
|
|
|
|
if (KFindDialog::d->backRef->isChecked()) {
|
2014-11-13 01:04:59 +02:00
|
|
|
options |= BackReference;
|
2023-06-29 05:37:22 +03:00
|
|
|
}
|
2014-11-13 01:04:59 +02:00
|
|
|
return options;
|
|
|
|
}
|
|
|
|
|
|
|
|
QWidget *KReplaceDialog::replaceExtension() const
|
|
|
|
{
|
2023-06-29 05:37:22 +03:00
|
|
|
if (!d->replaceExtension) {
|
|
|
|
d->replaceExtension = new QWidget(KFindDialog::d->replaceGrp);
|
|
|
|
KFindDialog::d->replaceLayout->addWidget(d->replaceExtension, 3, 0, 1, 2);
|
2014-11-13 01:04:59 +02:00
|
|
|
}
|
|
|
|
return d->replaceExtension;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString KReplaceDialog::replacement() const
|
|
|
|
{
|
|
|
|
return KFindDialog::d->replace->currentText();
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList KReplaceDialog::replacementHistory() const
|
|
|
|
{
|
|
|
|
QStringList lst = KFindDialog::d->replace->historyItems();
|
|
|
|
// historyItems() doesn't tell us about the case of replacing with an empty string
|
2023-06-29 05:37:22 +03:00
|
|
|
if (KFindDialog::d->replace->lineEdit()->text().isEmpty()) {
|
|
|
|
lst.prepend(QString());
|
|
|
|
}
|
2014-11-13 01:04:59 +02:00
|
|
|
return lst;
|
|
|
|
}
|
|
|
|
|
|
|
|
void KReplaceDialog::setOptions(long options)
|
|
|
|
{
|
|
|
|
KFindDialog::setOptions(options);
|
|
|
|
KFindDialog::d->promptOnReplace->setChecked(options & PromptOnReplace);
|
|
|
|
KFindDialog::d->backRef->setChecked(options & BackReference);
|
|
|
|
}
|
|
|
|
|
|
|
|
void KReplaceDialog::setReplacementHistory(const QStringList &strings)
|
|
|
|
{
|
2023-06-29 05:37:22 +03:00
|
|
|
if (strings.count() > 0) {
|
2014-11-13 01:04:59 +02:00
|
|
|
KFindDialog::d->replace->setHistoryItems(strings, true);
|
2023-06-29 05:37:22 +03:00
|
|
|
} else {
|
2014-11-13 01:04:59 +02:00
|
|
|
KFindDialog::d->replace->clearHistory();
|
2023-06-29 05:37:22 +03:00
|
|
|
}
|
2014-11-13 01:04:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void KReplaceDialogPrivate::_k_slotOk()
|
|
|
|
{
|
|
|
|
// If regex and backrefs are enabled, do a sanity check.
|
2023-06-29 05:37:22 +03:00
|
|
|
if (q->KFindDialog::d->regExp->isChecked() && q->KFindDialog::d->backRef->isChecked()) {
|
|
|
|
QRegExp r(q->pattern());
|
2015-10-27 18:47:05 +02:00
|
|
|
int caps = r.captureCount();
|
2014-11-13 01:04:59 +02:00
|
|
|
QRegExp check(QString("((?:\\\\)+)(\\d+)"));
|
|
|
|
int p = 0;
|
|
|
|
QString rep = q->replacement();
|
2023-06-29 05:37:22 +03:00
|
|
|
while ((p = check.indexIn(rep, p)) > -1) {
|
|
|
|
if (check.cap(1).length() % 2 && check.cap(2).toInt() > caps) {
|
|
|
|
KMessageBox::information(
|
|
|
|
q, i18n(
|
|
|
|
"Your replacement string is referencing a capture greater than '\\%1', ", caps) +
|
|
|
|
(
|
|
|
|
caps ?
|
|
|
|
i18np("but your pattern only defines 1 capture.", "but your pattern only defines %1 captures.", caps) :
|
|
|
|
i18n("but your pattern defines no captures.")
|
|
|
|
) + i18n("\nPlease correct.")
|
|
|
|
);
|
|
|
|
return; // abort
|
2014-11-13 01:04:59 +02:00
|
|
|
}
|
|
|
|
p += check.matchedLength();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
q->KFindDialog::d->_k_slotOk();
|
|
|
|
q->KFindDialog::d->replace->addToHistory(q->replacement());
|
|
|
|
}
|
|
|
|
|
2015-02-27 07:40:26 +00:00
|
|
|
#include "moc_kreplacedialog.cpp"
|