kutils Library API Documentation

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., 59 Temple Place - Suite 330,
00018     Boston, MA 02111-1307, USA.
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; // only once
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     // If regex and backrefs are enabled, do a sanity check.
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; // abort OKing
00144             }
00145             p += check.matchedLength();
00146         }
00147 
00148     }
00149 
00150     KFindDialog::slotOk();
00151     m_replace->addToHistory(replacement());
00152 }
00153 
00154 // kate: space-indent on; indent-width 4; replace-tabs on;
00155 #include "kreplacedialog.moc"
KDE Logo
This file is part of the documentation for kutils Library Version 3.4.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Thu Sep 15 10:57:09 2005 by doxygen 1.4.4 written by Dimitri van Heesch, © 1997-2003