KIO
kurlrequester.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
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
00169 void _k_slotUpdateUrl();
00170 void _k_slotOpenDialog();
00171
00172 KUrl m_startDir;
00173 KUrlRequester *m_parent;
00174 KLineEdit *edit;
00175 KComboBox *combo;
00176 KFile::Modes fileDialogMode;
00177 QString fileDialogFilter;
00178 KEditListBox::CustomEditor editor;
00179 KUrlDragPushButton *myButton;
00180 KFileDialog *myFileDialog;
00181 KUrlCompletion *myCompletion;
00182 };
00183
00184
00185
00186 KUrlRequester::KUrlRequester( QWidget *editWidget, QWidget *parent)
00187 : KHBox( parent),d(new KUrlRequesterPrivate(this))
00188 {
00189
00190 editWidget->setParent( this );
00191 d->combo = qobject_cast<KComboBox*>( editWidget );
00192 d->edit = qobject_cast<KLineEdit*>( editWidget );
00193 if ( d->edit ) {
00194 d->edit->setClearButtonShown( true );
00195 }
00196
00197 d->init();
00198 }
00199
00200
00201 KUrlRequester::KUrlRequester( QWidget *parent)
00202 : KHBox( parent),d(new KUrlRequesterPrivate(this))
00203 {
00204 d->init();
00205 }
00206
00207
00208 KUrlRequester::KUrlRequester( const KUrl& url, QWidget *parent)
00209 : KHBox( parent),d(new KUrlRequesterPrivate(this))
00210 {
00211 d->init();
00212 setUrl( url );
00213 }
00214
00215 KUrlRequester::~KUrlRequester()
00216 {
00217 delete d;
00218 }
00219
00220
00221 void KUrlRequester::KUrlRequesterPrivate::init()
00222 {
00223 m_parent->setMargin(0);
00224 m_parent->setSpacing(-1);
00225
00226 myFileDialog = 0L;
00227
00228 if ( !combo && !edit ) {
00229 edit = new KLineEdit( m_parent );
00230 edit->setClearButtonShown( true );
00231 }
00232
00233 QWidget *widget = combo ? (QWidget*) combo : (QWidget*) edit;
00234
00235 myButton = new KUrlDragPushButton(m_parent);
00236 myButton->setIcon(KIcon("document-open"));
00237 int buttonSize = widget->sizeHint().height();
00238 myButton->setFixedSize(buttonSize, buttonSize);
00239 myButton->setToolTip(i18n("Open file dialog"));
00240
00241 m_parent->connect(myButton, SIGNAL(pressed()), SLOT(_k_slotUpdateUrl()));
00242
00243 widget->installEventFilter( m_parent );
00244 m_parent->setFocusProxy( widget );
00245 m_parent->setFocusPolicy(Qt::StrongFocus);
00246
00247 connectSignals( m_parent );
00248 m_parent->connect(myButton, SIGNAL(clicked()), m_parent, SLOT(_k_slotOpenDialog()));
00249
00250 myCompletion = new KUrlCompletion();
00251 setCompletionObject( myCompletion );
00252
00253 QAction* openAction = new QAction(m_parent);
00254 openAction->setShortcut(KStandardShortcut::Open);
00255 m_parent->connect(openAction, SIGNAL(triggered(bool)), SLOT( _k_slotOpenDialog() ));
00256 }
00257
00258 void KUrlRequester::setUrl( const KUrl& url )
00259 {
00260 d->setText( url.pathOrUrl() );
00261 }
00262
00263 void KUrlRequester::setPath( const QString& path )
00264 {
00265 d->setText( path );
00266 }
00267
00268 void KUrlRequester::setText(const QString& text)
00269 {
00270 d->setText(text);
00271 }
00272
00273 void KUrlRequester::setStartDir(const KUrl& startDir)
00274 {
00275 d->m_startDir = startDir;
00276 }
00277
00278 void KUrlRequester::changeEvent(QEvent *e)
00279 {
00280 if (e->type()==QEvent::WindowTitleChange) {
00281 if (d->myFileDialog) {
00282 d->myFileDialog->setCaption(windowTitle());
00283 }
00284 }
00285 KHBox::changeEvent(e);
00286 }
00287
00288 KUrl KUrlRequester::url() const
00289 {
00290 return d->url();
00291 }
00292
00293 KUrl KUrlRequester::startDir() const
00294 {
00295 return d->m_startDir;
00296 }
00297
00298 QString KUrlRequester::text() const
00299 {
00300 return d->text();
00301 }
00302
00303 void KUrlRequester::KUrlRequesterPrivate::_k_slotOpenDialog()
00304 {
00305 KUrl newurl;
00306 if ( ((fileDialogMode & KFile::Directory) && !(fileDialogMode & KFile::File)) ||
00307
00308 (myFileDialog && (myFileDialog->mode() & KFile::Directory) &&
00309 (myFileDialog->mode() & (KFile::File | KFile::Files)) == 0) )
00310 {
00311 const KUrl openUrl = (!m_parent->url().isEmpty() && !m_parent->url().isRelative() )
00312 ? m_parent->url() : m_startDir;
00313
00314 if (fileDialogMode & KFile::LocalOnly)
00315 newurl = KFileDialog::getExistingDirectory( openUrl, m_parent);
00316 else
00317 newurl = KFileDialog::getExistingDirectoryUrl( openUrl, m_parent);
00318 if ( !newurl.isValid() )
00319 {
00320 return;
00321 }
00322 }
00323 else
00324 {
00325 emit m_parent->openFileDialog( m_parent );
00326
00327 KFileDialog *dlg = m_parent->fileDialog();
00328 if ( !url().isEmpty() && !url().isRelative() ) {
00329 KUrl u( url() );
00330
00331 if ( KProtocolManager::supportsListing( u ) )
00332 dlg->setSelection( u.url() );
00333 } else {
00334 dlg->setUrl(m_startDir);
00335 }
00336
00337
00338 if ( dlg->exec() != QDialog::Accepted )
00339 {
00340 return;
00341 }
00342
00343 newurl = dlg->selectedUrl();
00344 }
00345
00346 m_parent->setUrl( newurl );
00347 emit m_parent->urlSelected( url() );
00348 }
00349
00350 void KUrlRequester::setMode( KFile::Modes mode)
00351 {
00352 Q_ASSERT( (mode & KFile::Files) == 0 );
00353 d->fileDialogMode = mode;
00354 if ( (mode & KFile::Directory) && !(mode & KFile::File) )
00355 d->myCompletion->setMode( KUrlCompletion::DirCompletion );
00356
00357 if (d->myFileDialog) {
00358 d->myFileDialog->setMode(d->fileDialogMode);
00359 }
00360 }
00361
00362 KFile::Modes KUrlRequester::mode() const
00363 {
00364 return d->fileDialogMode;
00365 }
00366
00367 void KUrlRequester::setFilter(const QString &filter)
00368 {
00369 d->fileDialogFilter = filter;
00370 if (d->myFileDialog) {
00371 d->myFileDialog->setFilter(d->fileDialogFilter);
00372 }
00373 }
00374
00375 QString KUrlRequester::filter( ) const
00376 {
00377 return d->fileDialogFilter;
00378 }
00379
00380 KFileDialog * KUrlRequester::fileDialog() const
00381 {
00382 if (!d->myFileDialog) {
00383 QWidget *p = parentWidget();
00384 d->myFileDialog = new KFileDialog(QString(), d->fileDialogFilter, p);
00385 d->myFileDialog->setModal(true);
00386 d->myFileDialog->setMode(d->fileDialogMode);
00387 d->myFileDialog->setCaption(windowTitle());
00388 }
00389
00390 return d->myFileDialog;
00391 }
00392
00393 void KUrlRequester::clear()
00394 {
00395 d->setText( QString() );
00396 }
00397
00398 KLineEdit * KUrlRequester::lineEdit() const
00399 {
00400 return d->edit;
00401 }
00402
00403 KComboBox * KUrlRequester::comboBox() const
00404 {
00405 return d->combo;
00406 }
00407
00408 void KUrlRequester::KUrlRequesterPrivate::_k_slotUpdateUrl()
00409 {
00410 KUrl u( KUrl::fromPath( QDir::currentPath() + '/' ), url().url() );
00411 myButton->setURL( u );
00412 }
00413
00414 bool KUrlRequester::eventFilter( QObject *obj, QEvent *ev )
00415 {
00416 if ( ( d->edit == obj ) || ( d->combo == obj ) )
00417 {
00418 if (( ev->type() == QEvent::FocusIn ) || ( ev->type() == QEvent::FocusOut ))
00419
00420 QApplication::sendEvent( this, ev );
00421 }
00422 return QWidget::eventFilter( obj, ev );
00423 }
00424
00425 KPushButton * KUrlRequester::button() const
00426 {
00427 return d->myButton;
00428 }
00429
00430 KUrlCompletion *KUrlRequester::completionObject() const
00431 {
00432 return d->myCompletion;
00433 }
00434
00435 void KUrlRequester::setClickMessage(const QString& msg)
00436 {
00437 if(d->edit)
00438 d->edit->setClickMessage(msg);
00439 }
00440
00441 QString KUrlRequester::clickMessage() const
00442 {
00443 if(d->edit)
00444 return d->edit->clickMessage();
00445 else
00446 return QString();
00447 }
00448
00449 const KEditListBox::CustomEditor &KUrlRequester::customEditor()
00450 {
00451 setSizePolicy(QSizePolicy( QSizePolicy::Preferred,
00452 QSizePolicy::Fixed));
00453
00454 KLineEdit *edit = d->edit;
00455 if ( !edit && d->combo )
00456 edit = qobject_cast<KLineEdit*>( d->combo->lineEdit() );
00457
00458 #ifndef NDEBUG
00459 if ( !edit )
00460 kWarning() << "KUrlRequester's lineedit is not a KLineEdit!??\n";
00461 #endif
00462
00463 d->editor.setRepresentationWidget(this);
00464 d->editor.setLineEdit(edit);
00465 return d->editor;
00466 }
00467
00468 KUrlComboRequester::KUrlComboRequester( QWidget *parent)
00469 : KUrlRequester( new KComboBox(false), parent), d(0)
00470 {
00471 }
00472
00473 #include "kurlrequester.moc"