KDevelop API Documentation

editors/qeditor/koReplace.cpp

Go to the documentation of this file.
00001 /* 00002 Copyright (C) 2001, S.R.Haque <srhaque@iee.org>. 00003 This file is part of the KDE project 00004 00005 This library is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Library General Public 00007 License as published by the Free Software Foundation; either 00008 version 2 of the License, or (at your option) any later version. 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., 59 Temple Place - Suite 330, 00018 Boston, MA 02111-1307, USA. 00019 */ 00020 00021 #include <qcheckbox.h> 00022 #include <qlabel.h> 00023 #include <qregexp.h> 00024 #include <kapplication.h> 00025 #include <kdebug.h> 00026 #include <kcombobox.h> 00027 #include <klocale.h> 00028 #include "koReplace.h" 00029 #include <kmessagebox.h> 00030 00031 KoReplaceDialog::KoReplaceDialog(QWidget *parent, const char *name, long options, const QStringList &findStrings, const QStringList &replaceStrings, bool hasSelection) : 00032 KoFindDialog(parent, name, true) 00033 { 00034 init(true, findStrings, hasSelection); 00035 setOptions(options); 00036 setReplacementHistory(replaceStrings); 00037 } 00038 00039 KoReplaceDialog::~KoReplaceDialog() 00040 { 00041 } 00042 00043 long KoReplaceDialog::options() const 00044 { 00045 long options = 0; 00046 00047 options = KoFindDialog::options(); 00048 if (m_promptOnReplace->isChecked()) 00049 options |= PromptOnReplace; 00050 if (m_backRef->isChecked()) 00051 options |= BackReference; 00052 return options; 00053 } 00054 00055 QWidget *KoReplaceDialog::replaceExtension() 00056 { 00057 return m_replaceExtension; 00058 } 00059 00060 QString KoReplaceDialog::replacement() const 00061 { 00062 return m_replace->currentText(); 00063 } 00064 00065 QStringList KoReplaceDialog::replacementHistory() const 00066 { 00067 return m_find->historyItems(); 00068 } 00069 00070 void KoReplaceDialog::setOptions(long options) 00071 { 00072 KoFindDialog::setOptions(options); 00073 m_promptOnReplace->setChecked(options & PromptOnReplace); 00074 m_backRef->setChecked(options & BackReference); 00075 } 00076 00077 void KoReplaceDialog::setReplacementHistory(const QStringList &strings) 00078 { 00079 if (strings.count() > 0) 00080 m_replace->setHistoryItems(strings, true); 00081 else 00082 m_replace->clearHistory(); 00083 } 00084 00085 void KoReplaceDialog::slotOk() 00086 { 00087 KoFindDialog::slotOk(); 00088 m_replace->addToHistory(replacement()); 00089 } 00090 00091 // Create the dialog. 00092 KoReplace::KoReplace(const QString &pattern, const QString &replacement, long options, QWidget *parent) : 00093 KDialogBase(parent, __FILE__, false, // non-modal! 00094 i18n("Replace"), 00095 User3 | User2 | User1 | Close, 00096 User3, 00097 false, 00098 i18n("&All"), i18n("&Skip"), KStdGuiItem::yes()) 00099 { 00100 setMainWidget( new QLabel( i18n("Replace '%1' with '%2'?").arg(pattern).arg(replacement), this ) ); 00101 m_cancelled = false; 00102 m_options = options; 00103 m_parent = parent; 00104 m_replacements = 0; 00105 if (m_options & KoReplaceDialog::RegularExpression) 00106 m_regExp = new QRegExp(pattern, m_options & KoReplaceDialog::CaseSensitive); 00107 else 00108 m_pattern = pattern; 00109 m_replacement = replacement; 00110 resize(minimumSize()); 00111 } 00112 00113 KoReplace::~KoReplace() 00114 { 00115 if (!m_replacements && !m_cancelled) 00116 KMessageBox::information(m_parent, i18n("No text was replaced.")); 00117 } 00118 00119 void KoReplace::slotClose() 00120 { 00121 m_cancelled = true; 00122 kapp->exit_loop(); 00123 } 00124 00125 void KoReplace::abort() 00126 { 00127 slotClose(); 00128 } 00129 00130 bool KoReplace::replace(QString &text, const QRect &expose) 00131 { 00132 if (m_options & KoFindDialog::FindBackwards) 00133 { 00134 m_index = text.length(); 00135 } 00136 else 00137 { 00138 m_index = 0; 00139 } 00140 m_text = text; 00141 m_expose = expose; 00142 do 00143 { 00144 // Find the next match. 00145 if (m_options & KoReplaceDialog::RegularExpression) 00146 m_index = KoFind::find(m_text, *m_regExp, m_index, m_options, &m_matchedLength); 00147 else 00148 m_index = KoFind::find(m_text, m_pattern, m_index, m_options, &m_matchedLength); 00149 if (m_index != -1) 00150 { 00151 if (m_options & KoReplaceDialog::PromptOnReplace) 00152 { 00153 // Tell the world about the match we found, in case someone wants to 00154 // highlight it. 00155 if ( validateMatch( m_text, m_index, m_matchedLength )) 00156 { 00157 emit highlight(m_text, m_index, m_matchedLength, m_expose); 00158 show(); 00159 kapp->enter_loop(); 00160 } 00161 else 00162 m_index = m_index+m_matchedLength; 00163 } 00164 else 00165 { 00166 doReplace(); 00167 } 00168 } 00169 } 00170 while ((m_index != -1) && !m_cancelled); 00171 text = m_text; 00172 00173 // Should the user continue? 00174 return !m_cancelled; 00175 } 00176 00177 int KoReplace::replace(QString &text, const QString &pattern, const QString &replacement, int index, long options, int *replacedLength) 00178 { 00179 int matchedLength; 00180 00181 index = KoFind::find(text, pattern, index, options, &matchedLength); 00182 if (index != -1) 00183 { 00184 *replacedLength = replace(text, replacement, index, matchedLength); 00185 if (options & KoReplaceDialog::FindBackwards) 00186 index--; 00187 else 00188 index += *replacedLength; 00189 } 00190 return index; 00191 } 00192 00193 int KoReplace::replace(QString &text, const QRegExp &pattern, const QString &replacement, int index, long options, int *replacedLength) 00194 { 00195 int matchedLength; 00196 00197 index = KoFind::find(text, pattern, index, options, &matchedLength); 00198 if (index != -1) 00199 { 00200 *replacedLength = replace(text, replacement, index, matchedLength); 00201 if (options & KoReplaceDialog::FindBackwards) 00202 index--; 00203 else 00204 index += *replacedLength; 00205 } 00206 return index; 00207 } 00208 00209 int KoReplace::replace(QString &text, const QString &replacement, int index, int length) 00210 { 00211 // TBD: implement backreferences. 00212 text.replace(index, length, replacement); 00213 return replacement.length(); 00214 } 00215 00216 // All. 00217 void KoReplace::slotUser1() 00218 { 00219 int replacedLength; 00220 00221 replacedLength = KoReplace::replace(m_text, m_replacement, m_index, m_matchedLength); 00222 00223 // Tell the world about the replacement we made, in case someone wants to 00224 // highlight it. 00225 emit replace(m_text, m_index, replacedLength,m_matchedLength , m_expose); 00226 m_replacements++; 00227 if (m_options & KoReplaceDialog::FindBackwards) 00228 m_index--; 00229 else 00230 m_index += replacedLength; 00231 m_options &= ~KoReplaceDialog::PromptOnReplace; 00232 kapp->exit_loop(); 00233 } 00234 00235 // Skip. 00236 void KoReplace::slotUser2() 00237 { 00238 if (m_options & KoReplaceDialog::FindBackwards) m_index--; 00239 else m_index++; 00240 kapp->exit_loop(); 00241 } 00242 00243 // Yes. 00244 void KoReplace::slotUser3() 00245 { 00246 doReplace(); 00247 kapp->exit_loop(); 00248 } 00249 00250 void KoReplace::doReplace() 00251 { 00252 int replacedLength; 00253 00254 replacedLength = KoReplace::replace(m_text, m_replacement, m_index, m_matchedLength); 00255 00256 // Tell the world about the replacement we made, in case someone wants to 00257 // highlight it. 00258 emit replace(m_text, m_index, replacedLength,m_matchedLength, m_expose); 00259 m_replacements++; 00260 if (m_options & KoReplaceDialog::FindBackwards) 00261 m_index--; 00262 else 00263 m_index += replacedLength; 00264 } 00265 00266 #include "koReplace.moc"
KDE Logo
This file is part of the documentation for KDevelop Version 3.0.4.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Oct 6 17:38:55 2004 by doxygen 1.3.7 written by Dimitri van Heesch, © 1997-2003