KDEUI
kreplacedialog.cpp
Go to the documentation of this file.
00001 /* 00002 Copyright (C) 2001, S.R.Haque <srhaque@iee.org>. 00003 Copyright (C) 2002, David Faure <david@mandrakesoft.com> 00004 This file is part of the KDE project 00005 00006 This library is free software; you can redistribute it and/or 00007 modify it under the terms of the GNU Library General Public 00008 License version 2, as published by the Free Software Foundation. 00009 00010 This library is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 Library General Public License for more details. 00014 00015 You should have received a copy of the GNU Library General Public License 00016 along with this library; see the file COPYING.LIB. If not, write to 00017 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00018 Boston, MA 02110-1301, USA. 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 /*create replace dialog*/), 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; // only once 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 // historyItems() doesn't tell us about the case of replacing with an empty string 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 // If regex and backrefs are enabled, do a sanity check. 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; // abort OKing 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 // kate: space-indent on; indent-width 4; replace-tabs on; 00172 #include "kreplacedialog.moc"
KDE 4.6 API Reference