kmail

regexplineedit.cpp

Go to the documentation of this file.
00001 /*  -*- mode: C++; c-file-style: "gnu" -*-
00002     regexplineedit.cpp
00003 
00004     This file is part of KMail, the KDE mail client.
00005     Copyright (c) 2004 Ingo Kloecker <kloecker@kde.org>
00006 
00007     KMail is free software; you can redistribute it and/or modify
00008     it under the terms of the GNU General Public License as published by
00009     the Free Software Foundation; either version 2 of the License, or
00010     (at your option) any later version.
00011 
00012     KMail is distributed in the hope that it will be useful, but
00013     WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015     GNU General Public License for more details.
00016 
00017     You should have received a copy of the GNU General Public License
00018     along with this program; if not, write to the Free Software
00019     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00020 
00021     In addition, as a special exception, the copyright holders give
00022     permission to link the code of this program with any edition of
00023     the Qt library by Trolltech AS, Norway (or with modified versions
00024     of Qt that use the same license as Qt), and distribute linked
00025     combinations including the two.  You must obey the GNU General
00026     Public License in all respects for all of the code used other than
00027     Qt.  If you modify this file, you may extend this exception to
00028     your version of the file, but you are not obligated to do so.  If
00029     you do not wish to do so, delete this exception statement from
00030     your version.
00031 */
00032 
00033 #ifdef HAVE_CONFIG_H
00034 #include <config.h>
00035 #endif
00036 
00037 #include "regexplineedit.h"
00038 
00039 #include <klocale.h>
00040 #include <klineedit.h>
00041 #include <kparts/componentfactory.h>
00042 #include <kregexpeditorinterface.h>
00043 #include <kdialog.h>
00044 
00045 #include <qlayout.h>
00046 #include <qstring.h>
00047 #include <qpushbutton.h>
00048 #include <qdialog.h>
00049 
00050 namespace KMail {
00051 
00052   RegExpLineEdit::RegExpLineEdit( QWidget *parent, const char *name )
00053     : QWidget( parent, name ),
00054       mLineEdit( 0 ),
00055       mRegExpEditButton( 0 ),
00056       mRegExpEditDialog( 0 )
00057   {
00058     initWidget();
00059   }
00060 
00061   RegExpLineEdit::RegExpLineEdit( const QString &str, QWidget *parent,
00062                                   const char *name )
00063     : QWidget( parent, name ),
00064       mLineEdit( 0 ),
00065       mRegExpEditButton( 0 ),
00066       mRegExpEditDialog( 0 )
00067   {
00068     initWidget( str );
00069   }
00070 
00071   void RegExpLineEdit::initWidget( const QString &str )
00072   {
00073     QHBoxLayout * hlay = new QHBoxLayout( this, 0, KDialog::spacingHint() );
00074 
00075     mLineEdit = new KLineEdit( str, this );
00076     setFocusProxy( mLineEdit );
00077     hlay->addWidget( mLineEdit );
00078 
00079     connect( mLineEdit, SIGNAL( textChanged( const QString & ) ),
00080              this, SIGNAL( textChanged( const QString & ) ) );
00081 
00082     if( !KTrader::self()->query("KRegExpEditor/KRegExpEditor").isEmpty() ) {
00083       mRegExpEditButton = new QPushButton( i18n("Edit..."), this,
00084                                            "mRegExpEditButton" );
00085       mRegExpEditButton->setSizePolicy( QSizePolicy::Minimum,
00086                                         QSizePolicy::Fixed );
00087       hlay->addWidget( mRegExpEditButton );
00088 
00089       connect( mRegExpEditButton, SIGNAL( clicked() ),
00090                this, SLOT( slotEditRegExp() ) );
00091     }
00092   }
00093 
00094   void RegExpLineEdit::clear()
00095   {
00096     mLineEdit->clear();
00097   }
00098 
00099   QString RegExpLineEdit::text() const
00100   {
00101     return mLineEdit->text();
00102   }
00103 
00104   void RegExpLineEdit::setText( const QString & str )
00105   {
00106     mLineEdit->setText( str );
00107   }
00108 
00109   void RegExpLineEdit::showEditButton( bool show )
00110   {
00111     if ( !mRegExpEditButton )
00112       return;
00113 
00114     if ( show )
00115       mRegExpEditButton->show();
00116     else
00117       mRegExpEditButton->hide();
00118   }
00119 
00120   void RegExpLineEdit::slotEditRegExp()
00121   {
00122     if ( !mRegExpEditDialog )
00123       mRegExpEditDialog = KParts::ComponentFactory::createInstanceFromQuery<QDialog>( "KRegExpEditor/KRegExpEditor", QString::null, this );
00124 
00125     KRegExpEditorInterface *iface =
00126       static_cast<KRegExpEditorInterface *>( mRegExpEditDialog->qt_cast( "KRegExpEditorInterface" ) );
00127     if( iface ) {
00128       iface->setRegExp( mLineEdit->text() );
00129       if( mRegExpEditDialog->exec() == QDialog::Accepted )
00130         mLineEdit->setText( iface->regExp() );
00131     }
00132   }
00133 
00134 } // namespace KMail
00135 
00136 #include "regexplineedit.moc"