| # kio Library API Documentation
Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Source Files

# kurlrequester.cpp

    00001 /* This file is part of the KDE libraries
    00002     Copyright (C) 1999,2000,2001 Carsten Pfeiffer <pfeiffer@kde.org>
    00003
    00004     library is free software; you can redistribute it and/or
    00005     modify it under the terms of the GNU Library General Public
    00006     License version 2, as published by the Free Software Foundation.
    00007
    00008     This library is distributed in the hope that it will be useful,
    00009     but WITHOUT ANY WARRANTY; without even the implied warranty of
    00010     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00011     Library General Public License for more details.
    00012
    00013     You should have received a copy of the GNU Library General Public License
    00014     along with this library; see the file COPYING.LIB.  If not, write to
    00015     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    00016     Boston, MA 02111-1307, USA.
    00017 */
    00018
    00019
    00020 #include <sys/stat.h>
    00021 #include <unistd.h>
    00022
    00023 #include <qstring.h>
    00024 #include <qtooltip.h>
    00025
    00026 #include <kaccel.h>
    00027 #include <kcombobox.h>
    00028 #include <kdebug.h>
    00029 #include <kdialog.h>
    00030 #include <kfiledialog.h>
    00031 #include <kglobal.h>
    00032 #include <kiconloader.h>
    00033 #include <klineedit.h>
    00034 #include <klocale.h>
    00035 #include <kurlcompletion.h>
    00036 #include <kurldrag.h>
    00037 #include <kprotocolinfo.h>
    00038
    00039 #include "kurlrequester.h"
    00040
    00041
    00042 class KURLDragPushButton : public KPushButton
    00043 {
    00044 public:
    00045     KURLDragPushButton( QWidget *parent, const char *name=0 )
    00046         : KPushButton( parent, name ) {
    00047         setDragEnabled( true );
    00048     }
    00049     ~KURLDragPushButton() {}
    00050
    00051     void setURL( const KURL& url ) {
    00052         m_urls.clear();
    00053         m_urls.append( url );
    00054     }
    00055
    00056     /* not needed so far
    00057     void setURLs( const KURL::List& urls ) {
    00058         m_urls = urls;
    00059     }
    00060     const KURL::List& urls() const { return m_urls; }
    00061     */
    00062
    00063 protected:
    00064     virtual QDragObject *dragObject() {
    00065         if ( m_urls.isEmpty() )
    00066             return 0L;
    00067
    00068         QDragObject *drag = KURLDrag::newDrag( m_urls, this, "url drag" );
    00069         return drag;
    00070     }
    00071
    00072 private:
    00073     KURL::List m_urls;
    00074
    00075 };
    00076
    00077
    00078 /*
    00079 *************************************************************************
    00080 */
    00081
    00082 class KURLRequester::KURLRequesterPrivate
    00083 {
    00084 public:
    00085     KURLRequesterPrivate() {
    00086         edit = 0L;
    00087         combo = 0L;
    00088         fileDialogMode = KFile::File | KFile::ExistingOnly | KFile::LocalOnly;
    00089     }
    00090
    00091     void setText( const QString& text ) {
    00092         if ( combo )
    00093             combo->setEditText( text );
    00094         else
    00095             edit->setText( text );
    00096     }
    00097
    00098     void connectSignals( QObject *receiver ) {
    00099         QObject *sender;
    00100         if ( combo )
    00101             sender = combo;
    00102         else
    00103             sender = edit;
    00104
    00105         connect( sender, SIGNAL( textChanged( const QString& )),
    00106                  receiver, SIGNAL( textChanged( const QString& )));
    00107         connect( sender, SIGNAL( returnPressed() ),
    00108                  receiver, SIGNAL( returnPressed() ));
    00109         connect( sender, SIGNAL( returnPressed( const QString& ) ),
    00110                  receiver, SIGNAL( returnPressed( const QString& ) ));
    00111     }
    00112
    00113     void setCompletionObject( KCompletion *comp ) {
    00114         if ( combo )
    00115             combo->setCompletionObject( comp );
    00116         else
    00117             edit->setCompletionObject( comp );
    00118     }
    00119
    00123     QString url() {
    00124         QString txt = combo ? combo->currentText() : edit->text();
    00125         KURLCompletion *comp;
    00126         if ( combo )
    00127             comp = dynamic_cast<KURLCompletion*>(combo->completionObject());
    00128         else
    00129             comp = dynamic_cast<KURLCompletion*>(edit->completionObject());
    00130
    00131         if ( comp )
    00132             return comp->replacedPath( txt );
    00133         else
    00134             return txt;
    00135     }
    00136
    00137     KLineEdit *edit;
    00138     KComboBox *combo;
    00139     int fileDialogMode;
    00140     QString fileDialogFilter;
    00141 };
    00142
    00143
    00144
    00145 KURLRequester::KURLRequester( QWidget *editWidget, QWidget *parent,
    00146                               const char *name )
    00147   : QHBox( parent, name )
    00148 {
    00149     d = new KURLRequesterPrivate;
    00150
    00151     // must have this as parent
    00152     editWidget->reparent( this, 0, QPoint(0,0) );
    00153     d->edit = dynamic_cast<KLineEdit*>( editWidget );
    00154     d->combo = dynamic_cast<KComboBox*>( editWidget );
    00155
    00156     init();
    00157 }
    00158
    00159
    00160 KURLRequester::KURLRequester( QWidget *parent, const char *name )
    00161   : QHBox( parent, name )
    00162 {
    00163     d = new KURLRequesterPrivate;
    00164     init();
    00165 }
    00166
    00167
    00168 KURLRequester::KURLRequester( const QString& url, QWidget *parent,
    00169                               const char *name )
    00170   : QHBox( parent, name )
    00171 {
    00172     d = new KURLRequesterPrivate;
    00173     init();
    00174     setURL( url );
    00175 }
    00176
    00177
    00178 KURLRequester::~KURLRequester()
    00179 {
    00180     delete myCompletion;
    00181     delete myFileDialog;
    00182     delete d;
    00183 }
    00184
    00185
    00186 void KURLRequester::init()
    00187 {
    00188     myFileDialog    = 0L;
    00189     myShowLocalProt = false;
    00190
    00191     if ( !d->combo && !d->edit )
    00192         d->edit = new KLineEdit( this, "line edit" );
    00193
    00194     myButton = new KURLDragPushButton( this, "kfile button");
    00195     QIconSet iconSet = SmallIconSet(QString::fromLatin1("fileopen"));
    00196     QPixmap pixMap = iconSet.pixmap( QIconSet::Small, QIconSet::Normal );
    00197     myButton->setIconSet( iconSet );
    00198     myButton->setFixedSize( pixMap.width()+8, pixMap.height()+8 );
    00199     QToolTip::add(myButton, i18n("Open File Dialog"));
    00200
    00201     connect( myButton, SIGNAL( pressed() ), SLOT( slotUpdateURL() ));
    00202
    00203     setSpacing( KDialog::spacingHint() );
    00204
    00205     QWidget *widget = d->combo ? (QWidget*) d->combo : (QWidget*) d->edit;
    00206     setFocusProxy( widget );
    00207
    00208     d->connectSignals( this );
    00209     connect( myButton, SIGNAL( clicked() ), this, SLOT( slotOpenDialog() ));
    00210
    00211     myCompletion = new KURLCompletion();
    00212     d->setCompletionObject( myCompletion );
    00213
    00214     KAccel *accel = new KAccel( this );
    00215     accel->insert( KStdAccel::Open, this, SLOT( slotOpenDialog() ));
    00216     accel->readSettings();
    00217 }
    00218
    00219
    00220 void KURLRequester::setURL( const QString& url )
    00221 {
    00222     bool hasLocalPrefix = (url.startsWith("file:"));
    00223
    00224     if ( !myShowLocalProt && hasLocalPrefix )
    00225         d->setText( url.mid( 5, url.length()-5 ));
    00226     else
    00227         d->setText( url );
    00228 }
    00229
    00230 void KURLRequester::setCaption( const QString& caption )
    00231 {
    00232         fileDialog()->setCaption( caption );
    00233         QWidget::setCaption( caption );
    00234 }
    00235
    00236 QString KURLRequester::url() const
    00237 {
    00238     return d->url();
    00239 }
    00240
    00241
    00242 void KURLRequester::slotOpenDialog()
    00243 {
    00244     emit openFileDialog( this );
    00245
    00246     KFileDialog *dlg = fileDialog();
    00247     if ( !d->url().isEmpty() ) {
    00248         KURL u( url() );
    00249         // If we won't be able to list it (e.g. http), then don't try :)
    00250         if ( KProtocolInfo::supportsListing( u.protocol() ) )
    00251             dlg->setSelection( u.url() );
    00252     }
    00253
    00254     if ( dlg->exec() == QDialog::Accepted )
    00255     {
    00256         setURL( dlg->selectedURL().prettyURL() );
    00257         emit urlSelected( d->url() );
    00258     }
    00259 }
    00260
    00261 void KURLRequester::setMode(unsigned int mode)
    00262 {
    00263     Q_ASSERT( (mode & KFile::Files) == 0 );
    00264     d->fileDialogMode = mode;
    00265     if ( (mode & KFile::Directory) && !(mode & KFile::File) )
    00266         myCompletion->setMode( KURLCompletion::DirCompletion );
    00267
    00268     if (myFileDialog)
    00269        myFileDialog->setMode( d->fileDialogMode );
    00270 }
    00271
    00272 void KURLRequester::setFilter(const QString &filter)
    00273 {
    00274     d->fileDialogFilter = filter;
    00275     if (myFileDialog)
    00276        myFileDialog->setFilter( d->fileDialogFilter );
    00277 }
    00278
    00279 KFileDialog * KURLRequester::fileDialog() const
    00280 {
    00281     if ( !myFileDialog ) {
    00282         QWidget *p = parentWidget();
    00283         myFileDialog = new KFileDialog( QString::null, QString::null, p,
    00284                                         "file dialog", true );
    00285
    00286         myFileDialog->setMode( d->fileDialogMode );
    00287         myFileDialog->setFilter( d->fileDialogFilter );
    00288     }
    00289
    00290     return myFileDialog;
    00291 }
    00292
    00293
    00294 void KURLRequester::setShowLocalProtocol( bool b )
    00295 {
    00296     if ( myShowLocalProt == b )
    00297         return;
    00298
    00299     myShowLocalProt = b;
    00300     setURL( url() );
    00301 }
    00302
    00303 void KURLRequester::clear()
    00304 {
    00305     d->setText( QString::null );
    00306 }
    00307
    00308 KLineEdit * KURLRequester::lineEdit() const
    00309 {
    00310     return d->edit;
    00311 }
    00312
    00313 KComboBox * KURLRequester::comboBox() const
    00314 {
    00315     return d->combo;
    00316 }
    00317
    00318 void KURLRequester::slotUpdateURL()
    00319 {
    00320     // bin compat, myButton is declared as QPushButton
    00321     KURL u( QDir::currentDirPath() + '/', url() );
    00322     (static_cast<KURLDragPushButton *>( myButton))->setURL( u );
    00323 }
    00324
    00325 KPushButton * KURLRequester::button() const
    00326 {
    00327     return myButton;
    00328 }
    00329
    00330 KEditListBox::CustomEditor KURLRequester::customEditor()
    00331 {
    00332     setSizePolicy(QSizePolicy( QSizePolicy::Preferred,
    00333                                QSizePolicy::Fixed));
    00334
    00335     KLineEdit *edit = d->edit;
    00336     if ( !edit && d->combo )
    00337         edit = dynamic_cast<KLineEdit*>( d->combo->lineEdit() );
    00338
    00339 #ifndef NDEBUG
    00340     if ( !edit )
    00341         kdWarning() << "KURLRequester's lineedit is not a KLineEdit!??\n";
    00342 #endif
    00343
    00344     KEditListBox::CustomEditor editor( this, edit );
    00345     return editor;
    00346 }
    00347
    00348 void KURLRequester::virtual_hook( int, void* )
    00349 { /*BASE::virtual_hook( id, data );*/ }
    00350
    00351 #include "kurlrequester.moc"

This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Jan 28 13:14:34 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001