• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

KIO

kurlrequester.cpp

Go to the documentation of this file.
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., 51 Franklin Street, Fifth Floor,
00016     Boston, MA 02110-1301, USA.
00017 */
00018 
00019 #include "kurlrequester.h"
00020 
00021 #include <kcombobox.h>
00022 #include <kfiledialog.h>
00023 #include <klineedit.h>
00024 #include <klocale.h>
00025 #include <kurlcompletion.h>
00026 #include <kprotocolmanager.h>
00027 #include <khbox.h>
00028 #include <kstandardshortcut.h>
00029 #include <kdebug.h>
00030 
00031 #include <QEvent>
00032 #include <QDrag>
00033 #include <QMimeData>
00034 #include <QAction>
00035 #include <QApplication>
00036 
00037 class KUrlDragPushButton : public KPushButton
00038 {
00039 public:
00040     KUrlDragPushButton( QWidget *parent)
00041         : KPushButton( parent)
00042     {
00043         setDragEnabled( true );
00044     }
00045     ~KUrlDragPushButton() {}
00046 
00047     void setURL( const KUrl& url )
00048     {
00049         m_urls.clear();
00050         m_urls.append( url );
00051     }
00052 
00053 protected:
00054     virtual QDrag *dragObject()
00055     {
00056         if (m_urls.isEmpty())
00057             return 0;
00058 
00059         QDrag *drag = new QDrag(this);
00060         QMimeData *mimeData = new QMimeData;
00061         m_urls.populateMimeData(mimeData);
00062         drag->setMimeData(mimeData);
00063         return drag;
00064     }
00065 
00066 private:
00067     KUrl::List m_urls;
00068 
00069 };
00070 
00071 
00072 class KUrlRequester::KUrlRequesterPrivate
00073 {
00074 public:
00075     KUrlRequesterPrivate(KUrlRequester *parent)
00076         : m_parent(parent),
00077           edit(0),
00078           combo(0),
00079           fileDialogMode(KFile::File | KFile::ExistingOnly | KFile::LocalOnly)
00080     {
00081     }
00082 
00083     ~KUrlRequesterPrivate()
00084     {
00085         delete myCompletion;
00086         delete myFileDialog;
00087     }
00088 
00089     void init();
00090 
00091     void setText( const QString& text ) {
00092         if ( combo )
00093         {
00094             if (combo->isEditable())
00095             {
00096                combo->setEditText( text );
00097             }
00098             else
00099             {
00100                int i = combo->findText( text );
00101                if ( i == -1 )
00102                {
00103                   combo->addItem( text );
00104                   combo->setCurrentIndex( combo->count()-1 );
00105                }
00106                else
00107                {
00108                   combo->setCurrentIndex( i );
00109                }
00110             }
00111         }
00112         else
00113         {
00114             edit->setText( text );
00115         }
00116     }
00117 
00118     void connectSignals( QObject *receiver )
00119     {
00120         QObject *sender;
00121         if ( combo )
00122             sender = combo;
00123         else
00124             sender = edit;
00125 
00126         if (combo )
00127             connect( sender, SIGNAL( editTextChanged( const QString& )),
00128                      receiver, SIGNAL( textChanged( const QString& )));
00129         else
00130             connect( sender, SIGNAL( textChanged( const QString& )),
00131                      receiver, SIGNAL( textChanged( const QString& )));
00132 
00133         connect( sender, SIGNAL( returnPressed() ),
00134                  receiver, SIGNAL( returnPressed() ));
00135         connect( sender, SIGNAL( returnPressed( const QString& ) ),
00136                  receiver, SIGNAL( returnPressed( const QString& ) ));
00137     }
00138 
00139     void setCompletionObject( KCompletion *comp )
00140     {
00141         if ( combo )
00142             combo->setCompletionObject( comp );
00143         else
00144             edit->setCompletionObject( comp );
00145     }
00146 
00147     QString text() const {
00148         return combo ? combo->currentText() : edit->text();
00149     }
00150 
00154     KUrl url() const {
00155         const QString txt = text();
00156         KUrlCompletion *comp;
00157         if ( combo )
00158             comp = qobject_cast<KUrlCompletion*>(combo->completionObject());
00159         else
00160             comp = qobject_cast<KUrlCompletion*>(edit->completionObject());
00161 
00162         if ( comp )
00163             return KUrl( comp->replacedPath( txt ) );
00164         else
00165             return KUrl( txt );
00166     }
00167 
00168     // slots
00169     void _k_slotUpdateUrl();
00170     void _k_slotOpenDialog();
00171 
00172     KUrlRequester *m_parent;
00173     KLineEdit *edit;
00174     KComboBox *combo;
00175     KFile::Modes fileDialogMode;
00176     QString fileDialogFilter;
00177     KEditListBox::CustomEditor editor;
00178     KUrlDragPushButton *myButton;
00179     KFileDialog *myFileDialog;
00180     KUrlCompletion *myCompletion;
00181 };
00182 
00183 
00184 
00185 KUrlRequester::KUrlRequester( QWidget *editWidget, QWidget *parent)
00186   : KHBox( parent),d(new KUrlRequesterPrivate(this))
00187 {
00188     // must have this as parent
00189     editWidget->setParent( this );
00190     d->combo = qobject_cast<KComboBox*>( editWidget );
00191     d->edit = qobject_cast<KLineEdit*>( editWidget );
00192     if ( d->edit ) {
00193         d->edit->setClearButtonShown( true );
00194     }
00195 
00196     d->init();
00197 }
00198 
00199 
00200 KUrlRequester::KUrlRequester( QWidget *parent)
00201   : KHBox( parent),d(new KUrlRequesterPrivate(this))
00202 {
00203     d->init();
00204 }
00205 
00206 
00207 KUrlRequester::KUrlRequester( const KUrl& url, QWidget *parent)
00208   : KHBox( parent),d(new KUrlRequesterPrivate(this))
00209 {
00210     d->init();
00211     setUrl( url );
00212 }
00213 
00214 
00215 KUrlRequester::~KUrlRequester()
00216 {
00217     delete d;
00218 }
00219 
00220 
00221 void KUrlRequester::KUrlRequesterPrivate::init()
00222 {
00223     m_parent->setMargin(0);
00224 
00225     myFileDialog = 0L;
00226 
00227     if ( !combo && !edit ) {
00228         edit = new KLineEdit( m_parent );
00229         edit->setClearButtonShown( true );
00230     }
00231 
00232     QWidget *widget = combo ? (QWidget*) combo : (QWidget*) edit;
00233 
00234     myButton = new KUrlDragPushButton(m_parent);
00235     myButton->setIcon(KIcon("document-open"));
00236     int buttonSize = widget->sizeHint().height();
00237     myButton->setFixedSize(buttonSize, buttonSize);
00238     myButton->setToolTip(i18n("Open file dialog"));
00239 
00240     m_parent->connect(myButton, SIGNAL(pressed()), SLOT(_k_slotUpdateUrl()));
00241 
00242     m_parent->setSpacing( KDialog::spacingHint() );
00243 
00244     widget->installEventFilter( m_parent );
00245     m_parent->setFocusProxy( widget );
00246     m_parent->setFocusPolicy(Qt::StrongFocus);
00247 
00248     connectSignals( m_parent );
00249     m_parent->connect(myButton, SIGNAL(clicked()), m_parent, SLOT(_k_slotOpenDialog()));
00250 
00251     myCompletion = new KUrlCompletion();
00252     setCompletionObject( myCompletion );
00253 
00254     QAction* openAction = new QAction(m_parent);
00255     openAction->setShortcut(KStandardShortcut::Open);
00256     m_parent->connect(openAction, SIGNAL(triggered(bool)), SLOT( _k_slotOpenDialog() ));
00257 }
00258 
00259 void KUrlRequester::setUrl( const KUrl& url )
00260 {
00261     d->setText( url.pathOrUrl() );
00262 }
00263 
00264 void KUrlRequester::setPath( const QString& path )
00265 {
00266     d->setText( path );
00267 }
00268 
00269 void KUrlRequester::changeEvent(QEvent *e)
00270 {
00271    if (e->type()==QEvent::WindowTitleChange) {
00272      if (d->myFileDialog) {
00273         d->myFileDialog->setCaption(windowTitle());
00274      }
00275    }
00276    KHBox::changeEvent(e);
00277 }
00278 
00279 KUrl KUrlRequester::url() const
00280 {
00281     return d->url();
00282 }
00283 
00284 QString KUrlRequester::text() const
00285 {
00286     return d->text();
00287 }
00288 
00289 void KUrlRequester::KUrlRequesterPrivate::_k_slotOpenDialog()
00290 {
00291     KUrl newurl;
00292     if ( (fileDialogMode & KFile::Directory) && !(fileDialogMode & KFile::File) ||
00293          /* catch possible fileDialog()->setMode( KFile::Directory ) changes */
00294          (myFileDialog && ((myFileDialog->mode() & KFile::Directory) &&
00295          (myFileDialog->mode() & (KFile::File | KFile::Files)) == 0 ) ) )
00296     {
00297         if (fileDialogMode & KFile::LocalOnly)
00298             newurl = KFileDialog::getExistingDirectory(m_parent->url(), m_parent);
00299         else
00300             newurl = KFileDialog::getExistingDirectoryUrl(m_parent->url(), m_parent);
00301         if ( !newurl.isValid() )
00302         {
00303             return;
00304         }
00305     }
00306     else
00307     {
00308       emit m_parent->openFileDialog( m_parent );
00309 
00310       KFileDialog *dlg = m_parent->fileDialog();
00311       if ( !url().isEmpty() ) {
00312           KUrl u( url() );
00313           // If we won't be able to list it (e.g. http), then don't try :)
00314           if ( KProtocolManager::supportsListing( u ) )
00315               dlg->setSelection( u.url() );
00316       }
00317 
00318       if ( dlg->exec() != QDialog::Accepted )
00319       {
00320           return;
00321       }
00322 
00323       newurl = dlg->selectedUrl();
00324     }
00325 
00326     m_parent->setUrl( newurl );
00327     emit m_parent->urlSelected( url() );
00328 }
00329 
00330 void KUrlRequester::setMode( KFile::Modes mode)
00331 {
00332     Q_ASSERT( (mode & KFile::Files) == 0 );
00333     d->fileDialogMode = mode;
00334     if ( (mode & KFile::Directory) && !(mode & KFile::File) )
00335         d->myCompletion->setMode( KUrlCompletion::DirCompletion );
00336 
00337     if (d->myFileDialog) {
00338         d->myFileDialog->setMode(d->fileDialogMode);
00339     }
00340 }
00341 
00342 KFile::Modes KUrlRequester::mode() const
00343 {
00344     return d->fileDialogMode;
00345 }
00346 
00347 void KUrlRequester::setFilter(const QString &filter)
00348 {
00349     d->fileDialogFilter = filter;
00350     if (d->myFileDialog) {
00351         d->myFileDialog->setFilter(d->fileDialogFilter);
00352     }
00353 }
00354 
00355 QString KUrlRequester::filter( ) const
00356 {
00357     return d->fileDialogFilter;
00358 }
00359 
00360 KFileDialog * KUrlRequester::fileDialog() const
00361 {
00362     if (!d->myFileDialog) {
00363         QWidget *p = parentWidget();
00364         d->myFileDialog = new KFileDialog(QString(), d->fileDialogFilter, p);
00365         d->myFileDialog->setModal(true);
00366         d->myFileDialog->setMode(d->fileDialogMode);
00367         d->myFileDialog->setCaption(windowTitle());
00368     }
00369 
00370     return d->myFileDialog;
00371 }
00372 
00373 void KUrlRequester::clear()
00374 {
00375     d->setText( QString() );
00376 }
00377 
00378 KLineEdit * KUrlRequester::lineEdit() const
00379 {
00380     return d->edit;
00381 }
00382 
00383 KComboBox * KUrlRequester::comboBox() const
00384 {
00385     return d->combo;
00386 }
00387 
00388 void KUrlRequester::KUrlRequesterPrivate::_k_slotUpdateUrl()
00389 {
00390     KUrl u( KUrl::fromPath( QDir::currentPath() + '/' ), url().url() );
00391     myButton->setURL( u );
00392 }
00393 
00394 bool KUrlRequester::eventFilter( QObject *obj, QEvent *ev )
00395 {
00396     if ( ( d->edit == obj ) || ( d->combo == obj ) )
00397     {
00398         if (( ev->type() == QEvent::FocusIn ) || ( ev->type() == QEvent::FocusOut ))
00399             // Forward focusin/focusout events to the urlrequester; needed by file form element in khtml
00400             QApplication::sendEvent( this, ev );
00401     }
00402     return QWidget::eventFilter( obj, ev );
00403 }
00404 
00405 KPushButton * KUrlRequester::button() const
00406 {
00407     return d->myButton;
00408 }
00409 
00410 KUrlCompletion *KUrlRequester::completionObject() const
00411 {
00412     return d->myCompletion;
00413 }
00414 
00415 void KUrlRequester::setClickMessage(const QString& msg)
00416 {
00417     if(d->edit)
00418         d->edit->setClickMessage(msg);
00419 }
00420 
00421 QString KUrlRequester::clickMessage() const
00422 {
00423     if(d->edit)
00424         return d->edit->clickMessage();
00425     else
00426         return QString();
00427 }
00428 
00429 const KEditListBox::CustomEditor &KUrlRequester::customEditor()
00430 {
00431     setSizePolicy(QSizePolicy( QSizePolicy::Preferred,
00432                                QSizePolicy::Fixed));
00433 
00434     KLineEdit *edit = d->edit;
00435     if ( !edit && d->combo )
00436         edit = qobject_cast<KLineEdit*>( d->combo->lineEdit() );
00437 
00438 #ifndef NDEBUG
00439     if ( !edit )
00440         kWarning() << "KUrlRequester's lineedit is not a KLineEdit!??\n";
00441 #endif
00442 
00443     d->editor.setRepresentationWidget(this);
00444     d->editor.setLineEdit(edit);
00445     return d->editor;
00446 }
00447 
00448 KUrlComboRequester::KUrlComboRequester( QWidget *parent)
00449   : KUrlRequester( new KComboBox(false), parent), d(0)
00450 {
00451 }
00452 
00453 #include "kurlrequester.moc"

KIO

Skip menu "KIO"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.7
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal