KDEUI
kreplacedialog.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "kreplacedialog.h"
00022 #include "kfinddialog_p.h"
00023
00024 #include <QtGui/QCheckBox>
00025 #include <QtGui/QGroupBox>
00026 #include <QtGui/QLabel>
00027 #include <QtGui/QLayout>
00028 #include <QtGui/QLineEdit>
00029 #include <QtCore/QRegExp>
00030 #include <khistorycombobox.h>
00031 #include <klocale.h>
00032 #include <kmessagebox.h>
00033 #include <kdebug.h>
00034
00040 class KReplaceDialogPrivate
00041 {
00042 public:
00043 KReplaceDialogPrivate(KReplaceDialog *q)
00044 : q(q)
00045 , initialShowDone(false)
00046 , replaceExtension (0)
00047 {
00048 }
00049
00050 void _k_slotOk();
00051
00052 KReplaceDialog *q;
00053 QStringList replaceStrings;
00054 bool initialShowDone;
00055 QWidget *replaceExtension;
00056 };
00057
00058 KReplaceDialog::KReplaceDialog(QWidget *parent, long options, const QStringList &findStrings,
00059 const QStringList &replaceStrings, bool hasSelection)
00060 : KFindDialog(parent, options, findStrings, hasSelection, true ),
00061 d(new KReplaceDialogPrivate(this))
00062 {
00063 d->replaceStrings = replaceStrings;
00064 }
00065
00066 KReplaceDialog::~KReplaceDialog()
00067 {
00068 delete d;
00069 }
00070
00071 void KReplaceDialog::showEvent( QShowEvent *e )
00072 {
00073 if ( !d->initialShowDone )
00074 {
00075 d->initialShowDone = true;
00076
00077 if (!d->replaceStrings.isEmpty())
00078 {
00079 setReplacementHistory(d->replaceStrings);
00080 KFindDialog::d->replace->lineEdit()->setText( d->replaceStrings[0] );
00081 }
00082 }
00083
00084 KFindDialog::showEvent(e);
00085 }
00086
00087 long KReplaceDialog::options() const
00088 {
00089 long options = 0;
00090
00091 options = KFindDialog::options();
00092 if (KFindDialog::d->promptOnReplace->isChecked())
00093 options |= PromptOnReplace;
00094 if (KFindDialog::d->backRef->isChecked())
00095 options |= BackReference;
00096 return options;
00097 }
00098
00099 QWidget *KReplaceDialog::replaceExtension() const
00100 {
00101 if (!d->replaceExtension)
00102 {
00103 d->replaceExtension = new QWidget(KFindDialog::d->replaceGrp);
00104 KFindDialog::d->replaceLayout->addWidget(d->replaceExtension, 3, 0, 1, 2);
00105 }
00106
00107 return d->replaceExtension;
00108 }
00109
00110 QString KReplaceDialog::replacement() const
00111 {
00112 return KFindDialog::d->replace->currentText();
00113 }
00114
00115 QStringList KReplaceDialog::replacementHistory() const
00116 {
00117 QStringList lst = KFindDialog::d->replace->historyItems();
00118
00119 if ( KFindDialog::d->replace->lineEdit()->text().isEmpty() )
00120 lst.prepend( QString() );
00121 return lst;
00122 }
00123
00124 void KReplaceDialog::setOptions(long options)
00125 {
00126 KFindDialog::setOptions(options);
00127 KFindDialog::d->promptOnReplace->setChecked(options & PromptOnReplace);
00128 KFindDialog::d->backRef->setChecked(options & BackReference);
00129 }
00130
00131 void KReplaceDialog::setReplacementHistory(const QStringList &strings)
00132 {
00133 if (strings.count() > 0)
00134 KFindDialog::d->replace->setHistoryItems(strings, true);
00135 else
00136 KFindDialog::d->replace->clearHistory();
00137 }
00138
00139 void KReplaceDialogPrivate::_k_slotOk()
00140 {
00141
00142 if ( q->KFindDialog::d->regExp->isChecked() && q->KFindDialog::d->backRef->isChecked() )
00143 {
00144 QRegExp r ( q->pattern() );
00145 int caps = r.numCaptures();
00146 QRegExp check(QString("((?:\\\\)+)(\\d+)"));
00147 int p = 0;
00148 QString rep = q->replacement();
00149 while ( (p = check.indexIn( rep, p ) ) > -1 )
00150 {
00151 if ( check.cap(1).length()%2 && check.cap(2).toInt() > caps )
00152 {
00153 KMessageBox::information( q, i18n(
00154 "Your replacement string is referencing a capture greater than '\\%1', ", caps ) +
00155 ( caps ?
00156 i18np("but your pattern only defines 1 capture.",
00157 "but your pattern only defines %1 captures.", caps ) :
00158 i18n("but your pattern defines no captures.") ) +
00159 i18n("\nPlease correct.") );
00160 return;
00161 }
00162 p += check.matchedLength();
00163 }
00164
00165 }
00166
00167 q->KFindDialog::d->_k_slotOk();
00168 q->KFindDialog::d->replace->addToHistory(q->replacement());
00169 }
00170
00171
00172 #include "kreplacedialog.moc"