00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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 bool KoReplace::validateMatch( const QString & , int , int )
00044 {
00045 return true;
00046 }
00047
00048 long KoReplaceDialog::options() const
00049 {
00050 long options = 0;
00051
00052 options = KoFindDialog::options();
00053 if (m_promptOnReplace->isChecked())
00054 options |= PromptOnReplace;
00055 if (m_backRef->isChecked())
00056 options |= BackReference;
00057 return options;
00058 }
00059
00060 QWidget *KoReplaceDialog::replaceExtension()
00061 {
00062 return m_replaceExtension;
00063 }
00064
00065 QString KoReplaceDialog::replacement() const
00066 {
00067 return m_replace->currentText();
00068 }
00069
00070 QStringList KoReplaceDialog::replacementHistory() const
00071 {
00072 return m_find->historyItems();
00073 }
00074
00075 void KoReplaceDialog::setOptions(long options)
00076 {
00077 KoFindDialog::setOptions(options);
00078 m_promptOnReplace->setChecked(options & PromptOnReplace);
00079 m_backRef->setChecked(options & BackReference);
00080 }
00081
00082 void KoReplaceDialog::setReplacementHistory(const QStringList &strings)
00083 {
00084 if (strings.count() > 0)
00085 m_replace->setHistoryItems(strings, true);
00086 else
00087 m_replace->clearHistory();
00088 }
00089
00090 void KoReplaceDialog::slotOk()
00091 {
00092 KoFindDialog::slotOk();
00093 m_replace->addToHistory(replacement());
00094 }
00095
00096
00097 KoReplace::KoReplace(const QString &pattern, const QString &replacement, long options, QWidget *parent) :
00098 KDialogBase(parent, __FILE__, false,
00099 i18n("Replace"),
00100 User3 | User2 | User1 | Close,
00101 User3,
00102 false,
00103 i18n("&All"), i18n("&Skip"), KStdGuiItem::yes())
00104 {
00105 setMainWidget( new QLabel( i18n("Replace '%1' with '%2'?").arg(pattern).arg(replacement), this ) );
00106 m_cancelled = false;
00107 m_options = options;
00108 m_parent = parent;
00109 m_replacements = 0;
00110 if (m_options & KoReplaceDialog::RegularExpression)
00111 m_regExp = new QRegExp(pattern, m_options & KoReplaceDialog::CaseSensitive);
00112 else
00113 m_pattern = pattern;
00114 m_replacement = replacement;
00115 resize(minimumSize());
00116 }
00117
00118 KoReplace::~KoReplace()
00119 {
00120 if (!m_replacements && !m_cancelled)
00121 KMessageBox::information(m_parent, i18n("No text was replaced."));
00122 }
00123
00124 void KoReplace::slotClose()
00125 {
00126 m_cancelled = true;
00127 kapp->exit_loop();
00128 }
00129
00130 void KoReplace::abort()
00131 {
00132 slotClose();
00133 }
00134
00135 bool KoReplace::replace(QString &text, const QRect &expose)
00136 {
00137 if (m_options & KoFindDialog::FindBackwards)
00138 {
00139 m_index = text.length();
00140 }
00141 else
00142 {
00143 m_index = 0;
00144 }
00145 m_text = text;
00146 m_expose = expose;
00147 do
00148 {
00149
00150 if (m_options & KoReplaceDialog::RegularExpression)
00151 m_index = KoFind::find(m_text, *m_regExp, m_index, m_options, &m_matchedLength);
00152 else
00153 m_index = KoFind::find(m_text, m_pattern, m_index, m_options, &m_matchedLength);
00154 if (m_index != -1)
00155 {
00156 if (m_options & KoReplaceDialog::PromptOnReplace)
00157 {
00158
00159
00160 if ( validateMatch( m_text, m_index, m_matchedLength ))
00161 {
00162 emit highlight(m_text, m_index, m_matchedLength, m_expose);
00163 show();
00164 kapp->enter_loop();
00165 }
00166 else
00167 m_index = m_index+m_matchedLength;
00168 }
00169 else
00170 {
00171 doReplace();
00172 }
00173 }
00174 }
00175 while ((m_index != -1) && !m_cancelled);
00176 text = m_text;
00177
00178
00179 return !m_cancelled;
00180 }
00181
00182 int KoReplace::replace(QString &text, const QString &pattern, const QString &replacement, int index, long options, int *replacedLength)
00183 {
00184 int matchedLength;
00185
00186 index = KoFind::find(text, pattern, index, options, &matchedLength);
00187 if (index != -1)
00188 {
00189 *replacedLength = replace(text, replacement, index, matchedLength);
00190 if (options & KoReplaceDialog::FindBackwards)
00191 index--;
00192 else
00193 index += *replacedLength;
00194 }
00195 return index;
00196 }
00197
00198 int KoReplace::replace(QString &text, const QRegExp &pattern, const QString &replacement, int index, long options, int *replacedLength)
00199 {
00200 int matchedLength;
00201
00202 index = KoFind::find(text, pattern, index, options, &matchedLength);
00203 if (index != -1)
00204 {
00205 *replacedLength = replace(text, replacement, index, matchedLength);
00206 if (options & KoReplaceDialog::FindBackwards)
00207 index--;
00208 else
00209 index += *replacedLength;
00210 }
00211 return index;
00212 }
00213
00214 int KoReplace::replace(QString &text, const QString &replacement, int index, int length)
00215 {
00216
00217 text.replace(index, length, replacement);
00218 return replacement.length();
00219 }
00220
00221
00222 void KoReplace::slotUser1()
00223 {
00224 int replacedLength;
00225
00226 replacedLength = KoReplace::replace(m_text, m_replacement, m_index, m_matchedLength);
00227
00228
00229
00230 emit replace(m_text, m_index, replacedLength,m_matchedLength , m_expose);
00231 m_replacements++;
00232 if (m_options & KoReplaceDialog::FindBackwards)
00233 m_index--;
00234 else
00235 m_index += replacedLength;
00236 m_options &= ~KoReplaceDialog::PromptOnReplace;
00237 kapp->exit_loop();
00238 }
00239
00240
00241 void KoReplace::slotUser2()
00242 {
00243 if (m_options & KoReplaceDialog::FindBackwards) m_index--;
00244 else m_index++;
00245 kapp->exit_loop();
00246 }
00247
00248
00249 void KoReplace::slotUser3()
00250 {
00251 doReplace();
00252 kapp->exit_loop();
00253 }
00254
00255 void KoReplace::doReplace()
00256 {
00257 int replacedLength;
00258
00259 replacedLength = KoReplace::replace(m_text, m_replacement, m_index, m_matchedLength);
00260
00261
00262
00263 emit replace(m_text, m_index, replacedLength,m_matchedLength, m_expose);
00264 m_replacements++;
00265 if (m_options & KoReplaceDialog::FindBackwards)
00266 m_index--;
00267 else
00268 m_index += replacedLength;
00269 }
00270
00271 #include "koReplace.moc"