• Skip to content
  • Skip to link menu
KDE 4.1 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 
00150     KUrl url() {
00151         QString txt = combo ? combo->currentText() : edit->text();
00152         KUrlCompletion *comp;
00153         if ( combo )
00154             comp = qobject_cast<KUrlCompletion*>(combo->completionObject());
00155         else
00156             comp = qobject_cast<KUrlCompletion*>(edit->completionObject());
00157 
00158         if ( comp )
00159             return KUrl( comp->replacedPath( txt ) );
00160         else
00161             return KUrl( txt );
00162     }
00163 
00164     // slots
00165     void _k_slotUpdateUrl();
00166     void _k_slotOpenDialog();
00167 
00168     KUrlRequester *m_parent;
00169     KLineEdit *edit;
00170     KComboBox *combo;
00171     KFile::Modes fileDialogMode;
00172     QString fileDialogFilter;
00173     KEditListBox::CustomEditor editor;
00174     KUrlDragPushButton *myButton;
00175     KFileDialog *myFileDialog;
00176     KUrlCompletion *myCompletion;
00177 };
00178 
00179 
00180 
00181 KUrlRequester::KUrlRequester( QWidget *editWidget, QWidget *parent)
00182   : KHBox( parent),d(new KUrlRequesterPrivate(this))
00183 {
00184     // must have this as parent
00185     editWidget->setParent( this );
00186     d->combo = qobject_cast<KComboBox*>( editWidget );
00187     d->edit = qobject_cast<KLineEdit*>( editWidget );
00188     if ( d->edit ) {
00189         d->edit->setClearButtonShown( true );
00190     }
00191 
00192     d->init();
00193 }
00194 
00195 
00196 KUrlRequester::KUrlRequester( QWidget *parent)
00197   : KHBox( parent),d(new KUrlRequesterPrivate(this))
00198 {
00199     d->init();
00200 }
00201 
00202 
00203 KUrlRequester::KUrlRequester( const KUrl& url, QWidget *parent)
00204   : KHBox( parent),d(new KUrlRequesterPrivate(this))
00205 {
00206     d->init();
00207     setUrl( url );
00208 }
00209 
00210 
00211 KUrlRequester::~KUrlRequester()
00212 {
00213     delete d;
00214 }
00215 
00216 
00217 void KUrlRequester::KUrlRequesterPrivate::init()
00218 {
00219     m_parent->setMargin(0);
00220 
00221     myFileDialog = 0L;
00222 
00223     if ( !combo && !edit ) {
00224         edit = new KLineEdit( m_parent );
00225         edit->setClearButtonShown( true );
00226     }
00227 
00228     QWidget *widget = combo ? (QWidget*) combo : (QWidget*) edit;
00229 
00230     myButton = new KUrlDragPushButton(m_parent);
00231     myButton->setIcon(KIcon("document-open"));
00232     int buttonSize = widget->sizeHint().height();
00233     myButton->setFixedSize(buttonSize, buttonSize);
00234     myButton->setToolTip(i18n("Open file dialog"));
00235 
00236     m_parent->connect(myButton, SIGNAL(pressed()), SLOT(_k_slotUpdateUrl()));
00237 
00238     m_parent->setSpacing( KDialog::spacingHint() );
00239 
00240     widget->installEventFilter( m_parent );
00241     m_parent->setFocusProxy( widget );
00242     m_parent->setFocusPolicy(Qt::StrongFocus);
00243 
00244     connectSignals( m_parent );
00245     m_parent->connect(myButton, SIGNAL(clicked()), m_parent, SLOT(_k_slotOpenDialog()));
00246 
00247     myCompletion = new KUrlCompletion();
00248     setCompletionObject( myCompletion );
00249 
00250     QAction* openAction = new QAction(m_parent);
00251     openAction->setShortcut(KStandardShortcut::Open);
00252     m_parent->connect(openAction, SIGNAL(triggered(bool)), SLOT( _k_slotOpenDialog() ));
00253 }
00254 
00255 void KUrlRequester::setUrl( const KUrl& url )
00256 {
00257     d->setText( url.pathOrUrl() );
00258 }
00259 
00260 void KUrlRequester::setPath( const QString& path )
00261 {
00262     d->setText( path );
00263 }
00264 
00265 void KUrlRequester::changeEvent(QEvent *e)
00266 {
00267    if (e->type()==QEvent::WindowTitleChange) {
00268      if (d->myFileDialog) {
00269         d->myFileDialog->setCaption(windowTitle());
00270      }
00271    }
00272    KHBox::changeEvent(e);
00273 }
00274 
00275 KUrl KUrlRequester::url() const
00276 {
00277     return d->url();
00278 }
00279 
00280 void KUrlRequester::KUrlRequesterPrivate::_k_slotOpenDialog()
00281 {
00282     KUrl newurl;
00283     if ( (fileDialogMode & KFile::Directory) && !(fileDialogMode & KFile::File) ||
00284          /* catch possible fileDialog()->setMode( KFile::Directory ) changes */
00285          (myFileDialog && ((myFileDialog->mode() & KFile::Directory) &&
00286          (myFileDialog->mode() & (KFile::File | KFile::Files)) == 0 ) ) )
00287     {
00288         if (fileDialogMode & KFile::LocalOnly)
00289             newurl = KFileDialog::getExistingDirectory(m_parent->url(), m_parent);
00290         else
00291             newurl = KFileDialog::getExistingDirectoryUrl(m_parent->url(), m_parent);
00292         if ( !newurl.isValid() )
00293         {
00294             return;
00295         }
00296     }
00297     else
00298     {
00299       emit m_parent->openFileDialog( m_parent );
00300 
00301       KFileDialog *dlg = m_parent->fileDialog();
00302       if ( !url().isEmpty() ) {
00303           KUrl u( url() );
00304           // If we won't be able to list it (e.g. http), then don't try :)
00305           if ( KProtocolManager::supportsListing( u ) )
00306               dlg->setSelection( u.url() );
00307       }
00308 
00309       if ( dlg->exec() != QDialog::Accepted )
00310       {
00311           return;
00312       }
00313 
00314       newurl = dlg->selectedUrl();
00315     }
00316 
00317     m_parent->setUrl( newurl );
00318     emit m_parent->urlSelected( url() );
00319 }
00320 
00321 void KUrlRequester::setMode( KFile::Modes mode)
00322 {
00323     Q_ASSERT( (mode & KFile::Files) == 0 );
00324     d->fileDialogMode = mode;
00325     if ( (mode & KFile::Directory) && !(mode & KFile::File) )
00326         d->myCompletion->setMode( KUrlCompletion::DirCompletion );
00327 
00328     if (d->myFileDialog) {
00329         d->myFileDialog->setMode(d->fileDialogMode);
00330     }
00331 }
00332 
00333 KFile::Modes KUrlRequester::mode() const
00334 {
00335     return d->fileDialogMode;
00336 }
00337 
00338 void KUrlRequester::setFilter(const QString &filter)
00339 {
00340     d->fileDialogFilter = filter;
00341     if (d->myFileDialog) {
00342         d->myFileDialog->setFilter(d->fileDialogFilter);
00343     }
00344 }
00345 
00346 QString KUrlRequester::filter( ) const
00347 {
00348     return d->fileDialogFilter;
00349 }
00350 
00351 KFileDialog * KUrlRequester::fileDialog() const
00352 {
00353     if (!d->myFileDialog) {
00354         QWidget *p = parentWidget();
00355         d->myFileDialog = new KFileDialog(QString(), d->fileDialogFilter, p);
00356         d->myFileDialog->setModal(true);
00357         d->myFileDialog->setMode(d->fileDialogMode);
00358         d->myFileDialog->setCaption(windowTitle());
00359     }
00360 
00361     return d->myFileDialog;
00362 }
00363 
00364 void KUrlRequester::clear()
00365 {
00366     d->setText( QString() );
00367 }
00368 
00369 KLineEdit * KUrlRequester::lineEdit() const
00370 {
00371     return d->edit;
00372 }
00373 
00374 KComboBox * KUrlRequester::comboBox() const
00375 {
00376     return d->combo;
00377 }
00378 
00379 void KUrlRequester::KUrlRequesterPrivate::_k_slotUpdateUrl()
00380 {
00381     KUrl u( KUrl::fromPath( QDir::currentPath() + '/' ), url().url() );
00382     myButton->setURL( u );
00383 }
00384 
00385 bool KUrlRequester::eventFilter( QObject *obj, QEvent *ev )
00386 {
00387     if ( ( d->edit == obj ) || ( d->combo == obj ) )
00388     {
00389         if (( ev->type() == QEvent::FocusIn ) || ( ev->type() == QEvent::FocusOut ))
00390             // Forward focusin/focusout events to the urlrequester; needed by file form element in khtml
00391             QApplication::sendEvent( this, ev );
00392     }
00393     return QWidget::eventFilter( obj, ev );
00394 }
00395 
00396 KPushButton * KUrlRequester::button() const
00397 {
00398     return d->myButton;
00399 }
00400 
00401 KUrlCompletion *KUrlRequester::completionObject() const
00402 {
00403     return d->myCompletion;
00404 }
00405 
00406 const KEditListBox::CustomEditor &KUrlRequester::customEditor()
00407 {
00408     setSizePolicy(QSizePolicy( QSizePolicy::Preferred,
00409                                QSizePolicy::Fixed));
00410 
00411     KLineEdit *edit = d->edit;
00412     if ( !edit && d->combo )
00413         edit = qobject_cast<KLineEdit*>( d->combo->lineEdit() );
00414 
00415 #ifndef NDEBUG
00416     if ( !edit )
00417         kWarning() << "KUrlRequester's lineedit is not a KLineEdit!??\n";
00418 #endif
00419 
00420     d->editor.setRepresentationWidget(this);
00421     d->editor.setLineEdit(edit);
00422     return d->editor;
00423 }
00424 
00425 KUrlComboRequester::KUrlComboRequester( QWidget *parent)
00426   : KUrlRequester( new KComboBox(false), parent), d(0)
00427 {
00428 }
00429 
00430 #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
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.4
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