kreplacedialog.cpp
/*
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 <qcheckbox.h>
#include <qgroupbox.h>
#include <qlabel.h>
#include <qlayout.h>
#include <qregexp.h>
#include <kcombobox.h>
#include <klocale.h>
#include <kmessagebox.h>
#include <kdebug.h>
class KReplaceDialog::KReplaceDialogPrivate {
public:
KReplaceDialogPrivate() : m_initialShowDone(false) {}
QStringList replaceStrings;
bool m_initialShowDone;
};
KReplaceDialog::KReplaceDialog(QWidget *parent, const char *name, long options, const QStringList &findStrings, const QStringList &replaceStrings, bool hasSelection) :
KFindDialog(parent, name, true)
{
d = new KReplaceDialogPrivate;
d->replaceStrings = replaceStrings;
init(true, findStrings, hasSelection);
setOptions(options);
}
KReplaceDialog::~KReplaceDialog()
{
delete d;
}
void KReplaceDialog::showEvent( QShowEvent *e )
{
if ( !d->m_initialShowDone )
{
d->m_initialShowDone = true; // only once
if (!d->replaceStrings.isEmpty())
{
setReplacementHistory(d->replaceStrings);
m_replace->lineEdit()->setText( d->replaceStrings[0] );
}
}
KFindDialog::showEvent(e);
}
long KReplaceDialog::options() const
{
long options = 0;
options = KFindDialog::options();
if (m_promptOnReplace->isChecked())
options |= PromptOnReplace;
if (m_backRef->isChecked())
options |= BackReference;
return options;
}
QWidget *KReplaceDialog::replaceExtension()
{
if (!m_replaceExtension)
{
m_replaceExtension = new QWidget(m_replaceGrp);
m_replaceLayout->addMultiCellWidget(m_replaceExtension, 3, 3, 0, 1);
}
return m_replaceExtension;
}
QString KReplaceDialog::replacement() const
{
return m_replace->currentText();
}
QStringList KReplaceDialog::replacementHistory() const
{
QStringList lst = m_replace->historyItems();
// historyItems() doesn't tell us about the case of replacing with an empty string
if ( m_replace->lineEdit()->text().isEmpty() )
lst.prepend( QString::null );
return lst;
}
void KReplaceDialog::setOptions(long options)
{
KFindDialog::setOptions(options);
m_promptOnReplace->setChecked(options & PromptOnReplace);
m_backRef->setChecked(options & BackReference);
}
void KReplaceDialog::setReplacementHistory(const QStringList &strings)
{
if (strings.count() > 0)
m_replace->setHistoryItems(strings, true);
else
m_replace->clearHistory();
}
void KReplaceDialog::slotOk()
{
// If regex and backrefs are enabled, do a sanity check.
if ( m_regExp->isChecked() && m_backRef->isChecked() )
{
QRegExp r ( pattern() );
int caps = r.numCaptures();
QRegExp check(QString("((?:\\\\)+)(\\d+)"));
int p = 0;
QString rep = replacement();
while ( (p = check.search( rep, p ) ) > -1 )
{
if ( check.cap(1).length()%2 && check.cap(2).toInt() > caps )
{
KMessageBox::information( this, i18n(
"Your replacement string is referencing a capture greater than '\\%1', ").arg( caps ) +
( caps ?
i18n("but your pattern only defines 1 capture.",
"but your pattern only defines %n captures.", caps ) :
i18n("but your pattern defines no captures.") ) +
i18n("\nPlease correct.") );
return; // abort OKing
}
p += check.matchedLength();
}
}
KFindDialog::slotOk();
m_replace->addToHistory(replacement());
}
// kate: space-indent on; indent-width 4; replace-tabs on;
#include "kreplacedialog.moc"
KFindDialog
A generic "find" dialog.
Definition: kfinddialog.h:74
KReplaceDialog::replaceExtension
QWidget * replaceExtension()
Returns an empty widget which the user may fill with additional UI elements as required.
Definition: kreplacedialog.cpp:87
KReplaceDialog::replacementHistory
QStringList replacementHistory() const
Returns the list of history items.
Definition: kreplacedialog.cpp:103
KReplaceDialog::KReplaceDialog
KReplaceDialog(QWidget *parent=0, const char *name=0, 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...
Definition: kreplacedialog.cpp:45
KReplaceDialog::options
long options() const
Returns the state of the options.
Definition: kreplacedialog.cpp:75
klocale.h
KFindDialog::options
long options() const
Returns the state of the options.
Definition: kfinddialog.cpp:302
KFindDialog::pattern
QString pattern() const
Returns the pattern to find.
Definition: kfinddialog.cpp:321
KReplaceDialog::replacement
QString replacement() const
Returns the replacement string.
Definition: kreplacedialog.cpp:98
KReplaceDialog::PromptOnReplace
Should the user be prompted before the replace operation?
Definition: kreplacedialog.h:71
KReplaceDialog::~KReplaceDialog
virtual ~KReplaceDialog()
Destructor.
Definition: kreplacedialog.cpp:54
KReplaceDialog::setReplacementHistory
void setReplacementHistory(const QStringList &history)
Provide the list of strings to be displayed as the history of replacement strings.
Definition: kreplacedialog.cpp:119
KMessageBox::information
static void information(QWidget *parent, const QString &text, const QString &caption=QString::null, const QString &dontShowAgainName=QString::null, int options=Notify)
KFindDialog::setOptions
void setOptions(long options)
Set the options which are checked.
Definition: kfinddialog.cpp:410
KReplaceDialog::setOptions
void setOptions(long options)
Set the options which are enabled.
Definition: kreplacedialog.cpp:112

KDE Home | KDE Accessibility Home | Description of Access Keys