kreplacedialog.cpp
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
00023 #include <qcheckbox.h>
00024 #include <qgroupbox.h>
00025 #include <qlabel.h>
00026 #include <qlayout.h>
00027 #include <qregexp.h>
00028 #include <kcombobox.h>
00029 #include <klocale.h>
00030 #include <kmessagebox.h>
00031
00037 class KReplaceDialog::KReplaceDialogPrivate {
00038 public:
00039 KReplaceDialogPrivate() : m_initialShowDone(false) {}
00040 QStringList replaceStrings;
00041 bool m_initialShowDone;
00042 };
00043
00044 KReplaceDialog::KReplaceDialog(QWidget *parent, const char *name, long options, const QStringList &findStrings, const QStringList &replaceStrings, bool hasSelection) :
00045 KFindDialog(parent, name, true)
00046 {
00047 d = new KReplaceDialogPrivate;
00048 d->replaceStrings = replaceStrings;
00049 init(true, findStrings, hasSelection);
00050 setOptions(options);
00051 }
00052
00053 KReplaceDialog::~KReplaceDialog()
00054 {
00055 delete d;
00056 }
00057
00058 void KReplaceDialog::showEvent( QShowEvent *e )
00059 {
00060 if ( !d->m_initialShowDone )
00061 {
00062 d->m_initialShowDone = true;
00063
00064 if (!d->replaceStrings.isEmpty())
00065 {
00066 setReplacementHistory(d->replaceStrings);
00067 m_replace->lineEdit()->setText( d->replaceStrings[0] );
00068 }
00069 }
00070
00071 KFindDialog::showEvent(e);
00072 }
00073
00074 long KReplaceDialog::options() const
00075 {
00076 long options = 0;
00077
00078 options = KFindDialog::options();
00079 if (m_promptOnReplace->isChecked())
00080 options |= PromptOnReplace;
00081 if (m_backRef->isChecked())
00082 options |= BackReference;
00083 return options;
00084 }
00085
00086 QWidget *KReplaceDialog::replaceExtension()
00087 {
00088 if (!m_replaceExtension)
00089 {
00090 m_replaceExtension = new QWidget(m_replaceGrp);
00091 m_replaceLayout->addMultiCellWidget(m_replaceExtension, 3, 3, 0, 1);
00092 }
00093
00094 return m_replaceExtension;
00095 }
00096
00097 QString KReplaceDialog::replacement() const
00098 {
00099 return m_replace->currentText();
00100 }
00101
00102 QStringList KReplaceDialog::replacementHistory() const
00103 {
00104 return m_replace->historyItems();
00105 }
00106
00107 void KReplaceDialog::setOptions(long options)
00108 {
00109 KFindDialog::setOptions(options);
00110 m_promptOnReplace->setChecked(options & PromptOnReplace);
00111 m_backRef->setChecked(options & BackReference);
00112 }
00113
00114 void KReplaceDialog::setReplacementHistory(const QStringList &strings)
00115 {
00116 if (strings.count() > 0)
00117 m_replace->setHistoryItems(strings, true);
00118 else
00119 m_replace->clearHistory();
00120 }
00121
00122 void KReplaceDialog::slotOk()
00123 {
00124
00125 if ( m_regExp->isChecked() && m_backRef->isChecked() )
00126 {
00127 QRegExp r ( pattern() );
00128 int caps = r.numCaptures();
00129 QRegExp check(QString("((?:\\\\)+)(\\d+)"));
00130 int p = 0;
00131 QString rep = replacement();
00132 while ( (p = check.search( rep, p ) ) > -1 )
00133 {
00134 if ( check.cap(1).length()%2 && check.cap(2).toInt() > caps )
00135 {
00136 KMessageBox::information( this, i18n(
00137 "Your replacement string is referencing a capture greater than '\\%1', ").arg( caps ) +
00138 ( caps ?
00139 i18n("but your pattern only defines 1 capture.",
00140 "but your pattern only defines %n captures.", caps ) :
00141 i18n("but your pattern defines no captures.") ) +
00142 i18n("\nPlease correct.") );
00143 return;
00144 }
00145 p += check.matchedLength();
00146 }
00147
00148 }
00149
00150 KFindDialog::slotOk();
00151 m_replace->addToHistory(replacement());
00152 }
00153
00154
00155 #include "kreplacedialog.moc"
This file is part of the documentation for kutils Library Version 3.4.2.