You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
161 lines
4.7 KiB
161 lines
4.7 KiB
15 years ago
|
/*
|
||
|
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"
|
||
|
|
||
15 years ago
|
#include <tqcheckbox.h>
|
||
|
#include <tqgroupbox.h>
|
||
|
#include <tqlabel.h>
|
||
13 years ago
|
#include <tqlayout.h>
|
||
15 years ago
|
#include <tqregexp.h>
|
||
15 years ago
|
#include <kcombobox.h>
|
||
|
#include <klocale.h>
|
||
|
#include <kmessagebox.h>
|
||
|
#include <kdebug.h>
|
||
|
|
||
|
/**
|
||
|
* we need to insert the strings after the dialog is set
|
||
13 years ago
|
* up, otherwise TQComboBox will deliver an aweful big sizeHint
|
||
14 years ago
|
* for long replacement texts.
|
||
15 years ago
|
*/
|
||
|
class KReplaceDialog::KReplaceDialogPrivate {
|
||
|
public:
|
||
|
KReplaceDialogPrivate() : m_initialShowDone(false) {}
|
||
14 years ago
|
TQStringList replaceStrings;
|
||
15 years ago
|
bool m_initialShowDone;
|
||
|
};
|
||
|
|
||
14 years ago
|
KReplaceDialog::KReplaceDialog(TQWidget *parent, const char *name, long options, const TQStringList &findStrings, const TQStringList &replaceStrings, bool hasSelection) :
|
||
15 years ago
|
KFindDialog(parent, name, true)
|
||
|
{
|
||
|
d = new KReplaceDialogPrivate;
|
||
14 years ago
|
d->replaceStrings = replaceStrings;
|
||
|
init(true, findStrings, hasSelection);
|
||
15 years ago
|
setOptions(options);
|
||
|
}
|
||
|
|
||
|
KReplaceDialog::~KReplaceDialog()
|
||
|
{
|
||
|
delete d;
|
||
|
}
|
||
|
|
||
15 years ago
|
void KReplaceDialog::showEvent( TQShowEvent *e )
|
||
15 years ago
|
{
|
||
|
if ( !d->m_initialShowDone )
|
||
|
{
|
||
|
d->m_initialShowDone = true; // only once
|
||
|
|
||
14 years ago
|
if (!d->replaceStrings.isEmpty())
|
||
15 years ago
|
{
|
||
14 years ago
|
setReplacementHistory(d->replaceStrings);
|
||
|
m_replace->lineEdit()->setText( d->replaceStrings[0] );
|
||
15 years ago
|
}
|
||
|
}
|
||
|
|
||
|
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;
|
||
|
}
|
||
|
|
||
14 years ago
|
TQWidget *KReplaceDialog::replaceExtension()
|
||
15 years ago
|
{
|
||
14 years ago
|
if (!m_replaceExtension)
|
||
15 years ago
|
{
|
||
14 years ago
|
m_replaceExtension = new TQWidget(m_replaceGrp);
|
||
|
m_replaceLayout->addMultiCellWidget(m_replaceExtension, 3, 3, 0, 1);
|
||
15 years ago
|
}
|
||
|
|
||
14 years ago
|
return m_replaceExtension;
|
||
15 years ago
|
}
|
||
|
|
||
14 years ago
|
TQString KReplaceDialog::replacement() const
|
||
15 years ago
|
{
|
||
14 years ago
|
return m_replace->currentText();
|
||
15 years ago
|
}
|
||
|
|
||
14 years ago
|
TQStringList KReplaceDialog::replacementHistory() const
|
||
15 years ago
|
{
|
||
14 years ago
|
TQStringList lst = m_replace->historyItems();
|
||
15 years ago
|
// historyItems() doesn't tell us about the case of replacing with an empty string
|
||
14 years ago
|
if ( m_replace->lineEdit()->text().isEmpty() )
|
||
15 years ago
|
lst.prepend( TQString::null );
|
||
15 years ago
|
return lst;
|
||
|
}
|
||
|
|
||
|
void KReplaceDialog::setOptions(long options)
|
||
|
{
|
||
|
KFindDialog::setOptions(options);
|
||
|
m_promptOnReplace->setChecked(options & PromptOnReplace);
|
||
|
m_backRef->setChecked(options & BackReference);
|
||
|
}
|
||
|
|
||
15 years ago
|
void KReplaceDialog::setReplacementHistory(const TQStringList &strings)
|
||
15 years ago
|
{
|
||
|
if (strings.count() > 0)
|
||
14 years ago
|
m_replace->setHistoryItems(strings, true);
|
||
15 years ago
|
else
|
||
14 years ago
|
m_replace->clearHistory();
|
||
15 years ago
|
}
|
||
|
|
||
|
void KReplaceDialog::slotOk()
|
||
|
{
|
||
|
// If regex and backrefs are enabled, do a sanity check.
|
||
|
if ( m_regExp->isChecked() && m_backRef->isChecked() )
|
||
|
{
|
||
15 years ago
|
TQRegExp r ( pattern() );
|
||
15 years ago
|
int caps = r.numCaptures();
|
||
15 years ago
|
TQRegExp check(TQString("((?:\\\\)+)(\\d+)"));
|
||
15 years ago
|
int p = 0;
|
||
14 years ago
|
TQString rep = replacement();
|
||
15 years ago
|
while ( (p = check.search( rep, p ) ) > -1 )
|
||
|
{
|
||
|
if ( check.cap(1).length()%2 && check.cap(2).toInt() > caps )
|
||
|
{
|
||
|
KMessageBox::information( this, i18n(
|
||
14 years ago
|
"Your replacement string is referencing a capture greater than '\\%1', ").arg( caps ) +
|
||
15 years ago
|
( 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();
|
||
14 years ago
|
m_replace->addToHistory(replacement());
|
||
15 years ago
|
}
|
||
|
|
||
14 years ago
|
// kate: space-indent on; indent-width 4; replace-tabs on;
|
||
15 years ago
|
#include "kreplacedialog.moc"
|