KDevelop API Documentation

replacedlgimpl.cpp

Go to the documentation of this file.
00001 #include <qcheckbox.h>
00002 #include <qradiobutton.h>
00003 #include <qstring.h>
00004 #include <qregexp.h>
00005 #include <qlabel.h>
00006 
00007 #include <ktrader.h>
00008 #include <kparts/componentfactory.h>
00009 #include <kregexpeditorinterface.h>
00010 #include <kurlrequester.h>
00011 #include <kurlcompletion.h>
00012 #include <klineedit.h>
00013 #include <kcombobox.h>
00014 #include <kdebug.h>
00015 
00016 #include "replacedlgimpl.h"
00017 
00018 namespace
00019 {
00023 QString escape(const QString &str)
00024 {
00025     QString escaped("[]{}()\\^$?.+-*");
00026     QString res;
00027 
00028     for (uint i=0; i < str.length(); ++i)
00029     {
00030         if (escaped.find(str[i]) != -1)
00031             res += "\\";
00032         res += str[i];
00033     }
00034 
00035     return res;
00036 }
00037 }
00038 
00039 
00040 ReplaceDlgImpl::ReplaceDlgImpl(QWidget* parent, const char* name, bool modal, WFlags fl)
00041         : ReplaceDlg(parent,name, modal,fl), _regexp_dialog( 0 )
00042 
00043 {
00044     connect( find_button, SIGNAL( clicked() ), SLOT( saveComboHistories() ) );
00045     connect( regexp_button, SIGNAL( clicked() ), SLOT( showRegExpEditor() ) );
00046     connect( find_combo, SIGNAL( textChanged( const QString & ) ),
00047         SLOT( validateFind( const QString & ) ) );
00048     connect( regexp_combo, SIGNAL( textChanged ( const QString & ) ),
00049              SLOT( validateExpression( const QString & ) ) );
00050     connect( strings_regexp_radio, SIGNAL( toggled( bool ) ), SLOT( toggleExpression( bool ) ) );
00051 
00052     // disable the editor button if the regexp editor isn't installed
00053     if ( KTrader::self()->query("KRegExpEditor/KRegExpEditor").isEmpty() )
00054     {
00055         strings_regexp_radio->disconnect( regexp_button );
00056     }
00057 
00058     path_urlreq->completionObject()->setMode(KURLCompletion::DirCompletion);
00059     path_urlreq->setMode( KFile::Directory | KFile::LocalOnly );
00060 
00061     expression_varning_label->hide();
00062 }
00063 
00064 ReplaceDlgImpl::~ReplaceDlgImpl()
00065 {}
00066 
00067 void ReplaceDlgImpl::show( QString const & path )
00068 {
00069     path_urlreq->lineEdit()->setText( path );
00070 
00071     find_combo->setCurrentText( "" );
00072     replacement_combo->setCurrentText( "" );
00073     regexp_combo->setCurrentText( "" );
00074 
00075     strings_all_radio->setChecked( true );
00076     find_combo->setFocus();
00077     
00078     find_button->setEnabled( false );
00079 
00080     QDialog::show();
00081 }
00082 
00083 
00084 void ReplaceDlgImpl::showRegExpEditor()
00085 {
00086     _regexp_dialog = KParts::ComponentFactory::createInstanceFromQuery<QDialog>( "KRegExpEditor/KRegExpEditor" );
00087 
00088     if ( _regexp_dialog )
00089     {
00090         KRegExpEditorInterface *editor =
00091             static_cast<KRegExpEditorInterface *>( _regexp_dialog->qt_cast( "KRegExpEditorInterface" ) );
00092 
00093         editor->setRegExp( regexp_combo->currentText() );
00094 
00095         if ( _regexp_dialog->exec() == QDialog::Accepted )
00096         {
00097             regexp_combo->setCurrentText( editor->regExp() );
00098         }
00099     }
00100 }
00101 
00102 void ReplaceDlgImpl::validateFind( const QString & )
00103 {
00104     //kdDebug(0) << "ReplaceWidget::validateFind()" << endl;
00105 
00106     bool x = find_combo->currentText().isEmpty() && ! strings_regexp_radio->isOn();
00107     find_button->setEnabled( !x );
00108 }
00109 
00110 void ReplaceDlgImpl::validateExpression( const QString & )
00111 {
00112     //kdDebug(0) << "ReplaceWidget::validateExpression()" << endl;
00113 
00114     QString pattern = regexp_combo->currentText();
00115     QRegExp re( pattern );
00116 
00117     if ( pattern.isEmpty() || !re.isValid() )
00118     {
00119         expression_varning_label->show();
00120         find_button->setEnabled( false );
00121     }
00122     else
00123     {
00124         expression_varning_label->hide();
00125         find_button->setEnabled( true );
00126     }
00127 }
00128 
00129 void ReplaceDlgImpl::toggleExpression( bool on )
00130 {
00131     if ( on )
00132     {
00133         validateExpression( QString() );
00134     }
00135     else
00136     {
00137         expression_varning_label->hide();
00138         find_button->setEnabled( true );
00139     }
00140 }
00141 
00142 void ReplaceDlgImpl::saveComboHistories()
00143 {
00144     if ( find_combo->isEnabled() && ! find_combo->currentText().isEmpty() )
00145     {
00146         find_combo->addToHistory( find_combo->currentText() );
00147     }
00148 
00149     if ( ! replacement_combo->currentText().isEmpty() )
00150     {
00151         replacement_combo->addToHistory( replacement_combo->currentText() );
00152     }
00153 
00154     if ( regexp_combo->isEnabled() && ! regexp_combo->currentText().isEmpty() )
00155     {
00156         regexp_combo->addToHistory( regexp_combo->currentText() );
00157     }
00158 }
00159 
00160 QRegExp ReplaceDlgImpl::expressionPattern()
00161 {
00162     QString pattern = escape( find_combo->currentText() );
00163 
00164     QRegExp re;
00165     re.setCaseSensitive( case_box->isChecked() );
00166     re.setMinimal( true );
00167 
00168     if ( strings_wholewords_radio->isChecked() )
00169     {
00170         pattern = "\\b" + pattern + "\\b";
00171     }
00172     else if ( strings_regexp_radio->isChecked() )
00173     {
00174         pattern = regexp_combo->currentText();
00175     }
00176 
00177     re.setPattern( pattern );
00178 
00179     return re;
00180 }
00181 
00182 QString ReplaceDlgImpl::replacementString()
00183 {
00184     return replacement_combo->currentText();
00185 }
00186 
00187 #include "replacedlgimpl.moc"
00188 
KDE Logo
This file is part of the documentation for KDevelop Version 3.1.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Tue Feb 22 09:22:42 2005 by doxygen 1.3.9.1 written by Dimitri van Heesch, © 1997-2003