kreplacedialog.cpp
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 00023 #include <tqcheckbox.h> 00024 #include <tqgroupbox.h> 00025 #include <tqlabel.h> 00026 #include <tqlayout.h> 00027 #include <tqregexp.h> 00028 #include <kcombobox.h> 00029 #include <klocale.h> 00030 #include <kmessagebox.h> 00031 #include <kdebug.h> 00032 00038 class KReplaceDialog::KReplaceDialogPrivate { 00039 public: 00040 KReplaceDialogPrivate() : m_initialShowDone(false) {} 00041 TQStringList replaceStrings; 00042 bool m_initialShowDone; 00043 }; 00044 00045 KReplaceDialog::KReplaceDialog(TQWidget *parent, const char *name, long options, const TQStringList &findStrings, const TQStringList &replaceStrings, bool hasSelection) : 00046 KFindDialog(parent, name, true) 00047 { 00048 d = new KReplaceDialogPrivate; 00049 d->replaceStrings = replaceStrings; 00050 init(true, findStrings, hasSelection); 00051 setOptions(options); 00052 } 00053 00054 KReplaceDialog::~KReplaceDialog() 00055 { 00056 delete d; 00057 } 00058 00059 void KReplaceDialog::showEvent( TQShowEvent *e ) 00060 { 00061 if ( !d->m_initialShowDone ) 00062 { 00063 d->m_initialShowDone = true; // only once 00064 00065 if (!d->replaceStrings.isEmpty()) 00066 { 00067 setReplacementHistory(d->replaceStrings); 00068 m_replace->lineEdit()->setText( d->replaceStrings[0] ); 00069 } 00070 } 00071 00072 KFindDialog::showEvent(e); 00073 } 00074 00075 long KReplaceDialog::options() const 00076 { 00077 long options = 0; 00078 00079 options = KFindDialog::options(); 00080 if (m_promptOnReplace->isChecked()) 00081 options |= PromptOnReplace; 00082 if (m_backRef->isChecked()) 00083 options |= BackReference; 00084 return options; 00085 } 00086 00087 TQWidget *KReplaceDialog::replaceExtension() 00088 { 00089 if (!m_replaceExtension) 00090 { 00091 m_replaceExtension = new TQWidget(m_replaceGrp); 00092 m_replaceLayout->addMultiCellWidget(m_replaceExtension, 3, 3, 0, 1); 00093 } 00094 00095 return m_replaceExtension; 00096 } 00097 00098 TQString KReplaceDialog::replacement() const 00099 { 00100 return m_replace->currentText(); 00101 } 00102 00103 TQStringList KReplaceDialog::replacementHistory() const 00104 { 00105 TQStringList lst = m_replace->historyItems(); 00106 // historyItems() doesn't tell us about the case of replacing with an empty string 00107 if ( m_replace->lineEdit()->text().isEmpty() ) 00108 lst.prepend( TQString::null ); 00109 return lst; 00110 } 00111 00112 void KReplaceDialog::setOptions(long options) 00113 { 00114 KFindDialog::setOptions(options); 00115 m_promptOnReplace->setChecked(options & PromptOnReplace); 00116 m_backRef->setChecked(options & BackReference); 00117 } 00118 00119 void KReplaceDialog::setReplacementHistory(const TQStringList &strings) 00120 { 00121 if (strings.count() > 0) 00122 m_replace->setHistoryItems(strings, true); 00123 else 00124 m_replace->clearHistory(); 00125 } 00126 00127 void KReplaceDialog::slotOk() 00128 { 00129 // If regex and backrefs are enabled, do a sanity check. 00130 if ( m_regExp->isChecked() && m_backRef->isChecked() ) 00131 { 00132 TQRegExp r ( pattern() ); 00133 int caps = r.numCaptures(); 00134 TQRegExp check(TQString("((?:\\\\)+)(\\d+)")); 00135 int p = 0; 00136 TQString rep = replacement(); 00137 while ( (p = check.search( rep, p ) ) > -1 ) 00138 { 00139 if ( check.cap(1).length()%2 && check.cap(2).toInt() > caps ) 00140 { 00141 KMessageBox::information( this, i18n( 00142 "Your replacement string is referencing a capture greater than '\\%1', ").arg( caps ) + 00143 ( caps ? 00144 i18n("but your pattern only defines 1 capture.", 00145 "but your pattern only defines %n captures.", caps ) : 00146 i18n("but your pattern defines no captures.") ) + 00147 i18n("\nPlease correct.") ); 00148 return; // abort OKing 00149 } 00150 p += check.matchedLength(); 00151 } 00152 00153 } 00154 00155 KFindDialog::slotOk(); 00156 m_replace->addToHistory(replacement()); 00157 } 00158 00159 // kate: space-indent on; indent-width 4; replace-tabs on; 00160 #include "kreplacedialog.moc"