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

KIO

  • sources
  • kde-4.12
  • kdelibs
  • kio
  • kfile
kurlrequester.cpp
Go to the documentation of this file.
1 /* This file is part of the KDE libraries
2  Copyright (C) 1999,2000,2001 Carsten Pfeiffer <pfeiffer@kde.org>
3 
4  library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Library General Public
6  License version 2, as published by the Free Software Foundation.
7 
8  This library is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  Library General Public License for more details.
12 
13  You should have received a copy of the GNU Library General Public License
14  along with this library; see the file COPYING.LIB. If not, write to
15  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16  Boston, MA 02110-1301, USA.
17 */
18 
19 #include "kurlrequester.h"
20 
21 #include <kcombobox.h>
22 #include <kfiledialog.h>
23 #include <klineedit.h>
24 #include <klocale.h>
25 #include <kurlcompletion.h>
26 #include <kprotocolmanager.h>
27 #include <khbox.h>
28 #include <kstandardshortcut.h>
29 #include <kdebug.h>
30 
31 #include <QEvent>
32 #include <QDrag>
33 #include <QMimeData>
34 #include <QAction>
35 #include <QApplication>
36 
37 class KUrlDragPushButton : public KPushButton
38 {
39 public:
40  KUrlDragPushButton( QWidget *parent)
41  : KPushButton( parent)
42  {
43  setDragEnabled( true );
44  }
45  ~KUrlDragPushButton() {}
46 
47  void setURL( const KUrl& url )
48  {
49  m_urls.clear();
50  m_urls.append( url );
51  }
52 
53 protected:
54  virtual QDrag *dragObject()
55  {
56  if (m_urls.isEmpty())
57  return 0;
58 
59  QDrag *drag = new QDrag(this);
60  QMimeData *mimeData = new QMimeData;
61  m_urls.populateMimeData(mimeData);
62  drag->setMimeData(mimeData);
63  return drag;
64  }
65 
66 private:
67  KUrl::List m_urls;
68 
69 };
70 
71 
72 class KUrlRequester::KUrlRequesterPrivate
73 {
74 public:
75  KUrlRequesterPrivate(KUrlRequester *parent)
76  : m_parent(parent),
77  edit(0),
78  combo(0),
79  fileDialogMode(KFile::File | KFile::ExistingOnly | KFile::LocalOnly)
80  {
81  }
82 
83  ~KUrlRequesterPrivate()
84  {
85  delete myCompletion;
86  delete myFileDialog;
87  }
88 
89  void init();
90 
91  void setText( const QString& text ) {
92  if ( combo )
93  {
94  if (combo->isEditable())
95  {
96  combo->setEditText( text );
97  }
98  else
99  {
100  int i = combo->findText( text );
101  if ( i == -1 )
102  {
103  combo->addItem( text );
104  combo->setCurrentIndex( combo->count()-1 );
105  }
106  else
107  {
108  combo->setCurrentIndex( i );
109  }
110  }
111  }
112  else
113  {
114  edit->setText( text );
115  }
116  }
117 
118  void connectSignals( QObject *receiver )
119  {
120  QObject *sender;
121  if ( combo )
122  sender = combo;
123  else
124  sender = edit;
125 
126  if (combo )
127  connect( sender, SIGNAL(editTextChanged(QString)),
128  receiver, SIGNAL(textChanged(QString)));
129  else
130  connect( sender, SIGNAL(textChanged(QString)),
131  receiver, SIGNAL(textChanged(QString)));
132 
133  connect( sender, SIGNAL(returnPressed()),
134  receiver, SIGNAL(returnPressed()));
135  connect( sender, SIGNAL(returnPressed(QString)),
136  receiver, SIGNAL(returnPressed(QString)));
137  }
138 
139  void setCompletionObject( KCompletion *comp )
140  {
141  if ( combo )
142  combo->setCompletionObject( comp );
143  else
144  edit->setCompletionObject( comp );
145  }
146 
147  QString text() const {
148  return combo ? combo->currentText() : edit->text();
149  }
150 
154  KUrl url() const {
155  const QString txt = text();
156  KUrlCompletion *comp;
157  if ( combo )
158  comp = qobject_cast<KUrlCompletion*>(combo->completionObject());
159  else
160  comp = qobject_cast<KUrlCompletion*>(edit->completionObject());
161 
162  if ( comp )
163  return KUrl( comp->replacedPath( txt ) );
164  else
165  return KUrl( txt );
166  }
167 
168  // slots
169  void _k_slotUpdateUrl();
170  void _k_slotOpenDialog();
171  void _k_slotFileDialogFinished();
172 
173  KUrl m_startDir;
174  KUrlRequester *m_parent;
175  KLineEdit *edit;
176  KComboBox *combo;
177  KFile::Modes fileDialogMode;
178  QString fileDialogFilter;
179 #ifndef KDE_NO_DEPRECATED
180  KEditListBox::CustomEditor editor;
181 #else
182  KEditListWidget::CustomEditor editor;
183 #endif
184  KUrlDragPushButton *myButton;
185  KFileDialog *myFileDialog;
186  KUrlCompletion *myCompletion;
187  Qt::WindowModality fileDialogModality;
188 };
189 
190 
191 
192 KUrlRequester::KUrlRequester( QWidget *editWidget, QWidget *parent)
193  : KHBox( parent),d(new KUrlRequesterPrivate(this))
194 {
195  // must have this as parent
196  editWidget->setParent( this );
197  d->combo = qobject_cast<KComboBox*>( editWidget );
198  d->edit = qobject_cast<KLineEdit*>( editWidget );
199  if ( d->edit ) {
200  d->edit->setClearButtonShown( true );
201  }
202 
203  d->init();
204 }
205 
206 
207 KUrlRequester::KUrlRequester( QWidget *parent)
208  : KHBox( parent),d(new KUrlRequesterPrivate(this))
209 {
210  d->init();
211 }
212 
213 
214 KUrlRequester::KUrlRequester( const KUrl& url, QWidget *parent)
215  : KHBox( parent),d(new KUrlRequesterPrivate(this))
216 {
217  d->init();
218  setUrl( url );
219 }
220 
221 KUrlRequester::~KUrlRequester()
222 {
223  delete d;
224 }
225 
226 
227 void KUrlRequester::KUrlRequesterPrivate::init()
228 {
229  m_parent->setMargin(0);
230  m_parent->setSpacing(-1); // use default spacing
231 
232  myFileDialog = 0L;
233  fileDialogModality = Qt::ApplicationModal;
234 
235  if ( !combo && !edit ) {
236  edit = new KLineEdit( m_parent );
237  edit->setClearButtonShown( true );
238  }
239 
240  QWidget *widget = combo ? (QWidget*) combo : (QWidget*) edit;
241 
242  myButton = new KUrlDragPushButton(m_parent);
243  myButton->setIcon(KIcon("document-open"));
244  int buttonSize = myButton->sizeHint().expandedTo(widget->sizeHint()).height();
245  myButton->setFixedSize(buttonSize, buttonSize);
246  myButton->setToolTip(i18n("Open file dialog"));
247 
248  m_parent->connect(myButton, SIGNAL(pressed()), SLOT(_k_slotUpdateUrl()));
249 
250  widget->installEventFilter( m_parent );
251  m_parent->setFocusProxy( widget );
252  m_parent->setFocusPolicy(Qt::StrongFocus);
253 
254  connectSignals( m_parent );
255  m_parent->connect(myButton, SIGNAL(clicked()), m_parent, SLOT(_k_slotOpenDialog()));
256 
257  myCompletion = new KUrlCompletion();
258  setCompletionObject( myCompletion );
259 
260  QAction* openAction = new QAction(m_parent);
261  openAction->setShortcut(KStandardShortcut::Open);
262  m_parent->connect(openAction, SIGNAL(triggered(bool)), SLOT(_k_slotOpenDialog()));
263 }
264 
265 void KUrlRequester::setUrl( const KUrl& url )
266 {
267  d->setText( url.pathOrUrl() );
268 }
269 
270 #ifndef KDE_NO_DEPRECATED
271 void KUrlRequester::setPath( const QString& path )
272 {
273  d->setText( path );
274 }
275 #endif
276 
277 void KUrlRequester::setText(const QString& text)
278 {
279  d->setText(text);
280 }
281 
282 void KUrlRequester::setStartDir(const KUrl& startDir)
283 {
284  d->m_startDir = startDir;
285  if (startDir.isLocalFile())
286  d->myCompletion->setDir(startDir.toLocalFile());
287 }
288 
289 void KUrlRequester::changeEvent(QEvent *e)
290 {
291  if (e->type()==QEvent::WindowTitleChange) {
292  if (d->myFileDialog) {
293  d->myFileDialog->setCaption(windowTitle());
294  }
295  }
296  KHBox::changeEvent(e);
297 }
298 
299 KUrl KUrlRequester::url() const
300 {
301  return d->url();
302 }
303 
304 KUrl KUrlRequester::startDir() const
305 {
306  return d->m_startDir;
307 }
308 
309 QString KUrlRequester::text() const
310 {
311  return d->text();
312 }
313 
314 void KUrlRequester::KUrlRequesterPrivate::_k_slotOpenDialog()
315 {
316  if ( myFileDialog )
317  if ( myFileDialog->isVisible() )
318  {
319  //The file dialog is already being shown, raise it and exit
320  myFileDialog->raise();
321  myFileDialog->activateWindow();
322  return;
323  }
324 
325  if ( ((fileDialogMode & KFile::Directory) && !(fileDialogMode & KFile::File)) ||
326  /* catch possible fileDialog()->setMode( KFile::Directory ) changes */
327  (myFileDialog && (myFileDialog->mode() & KFile::Directory) &&
328  (myFileDialog->mode() & (KFile::File | KFile::Files)) == 0) )
329  {
330  const KUrl openUrl = (!m_parent->url().isEmpty() && !m_parent->url().isRelative() )
331  ? m_parent->url() : m_startDir;
332 
333  /* FIXME We need a new abstract interface for using KDirSelectDialog in a non-modal way */
334 
335  KUrl newurl;
336  if (fileDialogMode & KFile::LocalOnly)
337  newurl = KFileDialog::getExistingDirectory( openUrl, m_parent);
338  else
339  newurl = KFileDialog::getExistingDirectoryUrl( openUrl, m_parent);
340 
341  if ( newurl.isValid() )
342  {
343  m_parent->setUrl( newurl );
344  emit m_parent->urlSelected( url() );
345  }
346  }
347  else
348  {
349  emit m_parent->openFileDialog( m_parent );
350 
351  //Creates the fileDialog if it doesn't exist yet
352  KFileDialog *dlg = m_parent->fileDialog();
353 
354  if ( !url().isEmpty() && !url().isRelative() ) {
355  KUrl u( url() );
356  // If we won't be able to list it (e.g. http), then don't try :)
357  if ( KProtocolManager::supportsListing( u ) )
358  dlg->setSelection( u.url() );
359  } else {
360  dlg->setUrl(m_startDir);
361  }
362 
363  //Update the file dialog window modality
364  if ( dlg->windowModality() != fileDialogModality )
365  dlg->setWindowModality(fileDialogModality);
366 
367  if ( fileDialogModality == Qt::NonModal )
368  {
369  dlg->show();
370  } else {
371  dlg->exec();
372  }
373  }
374 }
375 
376 void KUrlRequester::KUrlRequesterPrivate::_k_slotFileDialogFinished()
377 {
378  if ( !myFileDialog )
379  return;
380 
381  if ( myFileDialog->result() == QDialog::Accepted )
382  {
383  KUrl newUrl = myFileDialog->selectedUrl();
384  if ( newUrl.isValid() )
385  {
386  m_parent->setUrl( newUrl );
387  emit m_parent->urlSelected( url() );
388  }
389  }
390 }
391 
392 void KUrlRequester::setMode( KFile::Modes mode)
393 {
394  Q_ASSERT( (mode & KFile::Files) == 0 );
395  d->fileDialogMode = mode;
396  if ( (mode & KFile::Directory) && !(mode & KFile::File) )
397  d->myCompletion->setMode( KUrlCompletion::DirCompletion );
398 
399  if (d->myFileDialog) {
400  d->myFileDialog->setMode(d->fileDialogMode);
401  }
402 }
403 
404 KFile::Modes KUrlRequester::mode() const
405 {
406  return d->fileDialogMode;
407 }
408 
409 void KUrlRequester::setFilter(const QString &filter)
410 {
411  d->fileDialogFilter = filter;
412  if (d->myFileDialog) {
413  d->myFileDialog->setFilter(d->fileDialogFilter);
414  }
415 }
416 
417 QString KUrlRequester::filter( ) const
418 {
419  return d->fileDialogFilter;
420 }
421 
422 KFileDialog * KUrlRequester::fileDialog() const
423 {
424  if (!d->myFileDialog) {
425  QWidget *p = parentWidget();
426  d->myFileDialog = new KFileDialog(QString(), d->fileDialogFilter, p);
427  d->myFileDialog->setMode(d->fileDialogMode);
428  d->myFileDialog->setCaption(windowTitle());
429  d->myFileDialog->setWindowModality(d->fileDialogModality);
430  connect(d->myFileDialog, SIGNAL(finished()), SLOT(_k_slotFileDialogFinished()));
431  }
432 
433  return d->myFileDialog;
434 }
435 
436 void KUrlRequester::clear()
437 {
438  d->setText( QString() );
439 }
440 
441 KLineEdit * KUrlRequester::lineEdit() const
442 {
443  return d->edit;
444 }
445 
446 KComboBox * KUrlRequester::comboBox() const
447 {
448  return d->combo;
449 }
450 
451 void KUrlRequester::KUrlRequesterPrivate::_k_slotUpdateUrl()
452 {
453  KUrl u( KUrl::fromPath( QDir::currentPath() + '/' ), url().url() );
454  myButton->setURL( u );
455 }
456 
457 bool KUrlRequester::eventFilter( QObject *obj, QEvent *ev )
458 {
459  if ( ( d->edit == obj ) || ( d->combo == obj ) )
460  {
461  if (( ev->type() == QEvent::FocusIn ) || ( ev->type() == QEvent::FocusOut ))
462  // Forward focusin/focusout events to the urlrequester; needed by file form element in khtml
463  QApplication::sendEvent( this, ev );
464  }
465  return QWidget::eventFilter( obj, ev );
466 }
467 
468 KPushButton * KUrlRequester::button() const
469 {
470  return d->myButton;
471 }
472 
473 KUrlCompletion *KUrlRequester::completionObject() const
474 {
475  return d->myCompletion;
476 }
477 
478 void KUrlRequester::setClickMessage(const QString& msg)
479 {
480  if(d->edit)
481  d->edit->setClickMessage(msg);
482 }
483 
484 QString KUrlRequester::clickMessage() const
485 {
486  if(d->edit)
487  return d->edit->clickMessage();
488  else
489  return QString();
490 }
491 
492 Qt::WindowModality KUrlRequester::fileDialogModality() const
493 {
494  return d->fileDialogModality;
495 }
496 
497 void KUrlRequester::setFileDialogModality(Qt::WindowModality modality)
498 {
499  d->fileDialogModality = modality;
500 }
501 
502 #ifndef KDE_NO_DEPRECATED
503 const KEditListBox::CustomEditor &KUrlRequester::customEditor()
504 #else
505 const KEditListWidget::CustomEditor &KUrlRequester::customEditor()
506 #endif
507 {
508  setSizePolicy(QSizePolicy( QSizePolicy::Preferred,
509  QSizePolicy::Fixed));
510 
511  KLineEdit *edit = d->edit;
512  if ( !edit && d->combo )
513  edit = qobject_cast<KLineEdit*>( d->combo->lineEdit() );
514 
515 #ifndef NDEBUG
516  if ( !edit ) {
517  kWarning() << "KUrlRequester's lineedit is not a KLineEdit!??\n";
518  }
519 #endif
520 
521  d->editor.setRepresentationWidget(this);
522  d->editor.setLineEdit(edit);
523  return d->editor;
524 }
525 
526 KUrlComboRequester::KUrlComboRequester( QWidget *parent)
527  : KUrlRequester( new KComboBox(false), parent), d(0)
528 {
529 }
530 
531 #include "kurlrequester.moc"
KUrlRequester::url
KUrl url() const
KUrlRequester::returnPressed
void returnPressed()
Emitted when return or enter was pressed in the lineedit.
KUrlRequester::setFilter
void setFilter(const QString &filter)
Sets the filter for the file dialog.
Definition: kurlrequester.cpp:409
kcombobox.h
KPushButton
kdebug.h
KUrlRequester::startDir
KUrl startDir() const
Definition: kurlrequester.cpp:304
KFile::Directory
Definition: kfile.h:46
KUrlRequester::fileDialog
virtual KFileDialog * fileDialog() const
Definition: kurlrequester.cpp:422
KUrlRequester::text
QString text() const
kfiledialog.h
KUrlRequester
This class is a widget showing a lineedit and a button, which invokes a filedialog.
Definition: kurlrequester.h:60
KFile
KFile is a class which provides a namespace for some enumerated values associated with the kfile libr...
Definition: kfile.h:31
KUrlRequester::setMode
void setMode(KFile::Modes m)
Sets the mode of the file dialog.
Definition: kurlrequester.cpp:392
QWidget
kurlrequester.h
KFile::Files
Definition: kfile.h:47
KUrl::fromPath
static KUrl fromPath(const QString &text)
KUrlRequester::mode
KFile::Modes mode() const
Returns the current mode.
KFileDialog::getExistingDirectoryUrl
static KUrl getExistingDirectoryUrl(const KUrl &startDir=KUrl(), QWidget *parent=0, const QString &caption=QString())
Creates a modal directory-selection dialog and returns the selected directory or an empty string if n...
Definition: kfiledialog.cpp:617
KUrl::toLocalFile
QString toLocalFile(AdjustPathOption trailing=LeaveTrailingSlash) const
QString
KFile::File
Definition: kfile.h:45
QObject
klocale.h
KUrlRequester::setClickMessage
void setClickMessage(const QString &msg)
Set a click message msg.
Definition: kurlrequester.cpp:478
KUrlRequester::setUrl
void setUrl(const KUrl &url)
Sets the url in the lineedit to url.
Definition: kurlrequester.cpp:265
KUrl
KEditListBox::CustomEditor
KFileDialog
Provides a user (and developer) friendly way to select files and directories.
Definition: kfiledialog.h:68
kprotocolmanager.h
KUrlRequester::button
KPushButton * button() const
Definition: kurlrequester.cpp:468
KUrlRequester::setPath
void setPath(const QString &path)
Sets the url in the lineedit to KUrl::fromPath(path).
Definition: kurlrequester.cpp:271
KUrlRequester::filter
QString filter() const
Returns the current filter for the file dialog.
KUrlRequester::eventFilter
bool eventFilter(QObject *obj, QEvent *ev)
Definition: kurlrequester.cpp:457
KUrlRequester::fileDialogModality
Qt::WindowModality fileDialogModality() const
KEditListWidget::CustomEditor
KUrlRequester::customEditor
const KEditListBox::CustomEditor & customEditor()
Definition: kurlrequester.cpp:503
KIO::open
FileJob * open(const KUrl &url, QIODevice::OpenMode mode)
Open ( random access I/O )
Definition: filejob.cpp:211
KFileDialog::setUrl
void setUrl(const KUrl &url, bool clearforward=true)
Sets the directory to view.
Definition: kfiledialog.cpp:444
KUrlRequester::setStartDir
void setStartDir(const KUrl &startDir)
Sets the start dir startDir.
Definition: kurlrequester.cpp:282
KCompletion
KUrlRequester::~KUrlRequester
~KUrlRequester()
Destructs the KUrlRequester.
Definition: kurlrequester.cpp:221
KIcon
KUrlComboRequester::KUrlComboRequester
KUrlComboRequester(QWidget *parent=0)
Constructs a KUrlRequester widget with a combobox.
Definition: kurlrequester.cpp:526
KFileDialog::exec
int exec()
Definition: kfiledialog.cpp:965
KUrlRequester::comboBox
KComboBox * comboBox() const
Definition: kurlrequester.cpp:446
KUrlRequester::KUrlRequester
KUrlRequester(QWidget *parent=0)
Constructs a KUrlRequester widget.
Definition: kurlrequester.cpp:207
kstandardshortcut.h
KUrlRequester::setFileDialogModality
void setFileDialogModality(Qt::WindowModality modality)
Set the window modality for the file dialog to modality Directory selection dialogs are always modal...
Definition: kurlrequester.cpp:497
KUrlCompletion::DirCompletion
Definition: kurlcompletion.h:53
KPushButton::setDragEnabled
void setDragEnabled(bool enable)
KUrl::List
KFile::LocalOnly
Definition: kfile.h:49
KLineEdit
KFileDialog::getExistingDirectory
static QString getExistingDirectory(const KUrl &startDir=KUrl(), QWidget *parent=0, const QString &caption=QString())
Creates a modal directory-selection dialog and returns the selected directory (local only) or an empt...
Definition: kfiledialog.cpp:630
KPushButton::dragObject
virtual QDrag * dragObject()
KUrlRequester::clear
void clear()
Clears the lineedit/combobox.
Definition: kurlrequester.cpp:436
KUrlRequester::textChanged
void textChanged(const QString &)
Emitted when the text in the lineedit changes.
khbox.h
KUrlRequester::completionObject
KUrlCompletion * completionObject() const
Definition: kurlrequester.cpp:473
KComboBox
kWarning
static QDebug kWarning(bool cond, int area=KDE_DEFAULT_DEBUG_AREA)
klineedit.h
KHBox
KUrl::url
QString url(AdjustPathOption trailing=LeaveTrailingSlash) const
KProtocolManager::supportsListing
static bool supportsListing(const KUrl &url)
Returns whether the protocol can list files/objects.
Definition: kprotocolmanager.cpp:1032
KUrlRequester::changeEvent
virtual void changeEvent(QEvent *e)
Definition: kurlrequester.cpp:289
KUrl::isLocalFile
bool isLocalFile() const
KUrlRequester::setText
void setText(const QString &text)
Sets the current text in the lineedit or combobox.
Definition: kurlrequester.cpp:277
QAction
KUrlCompletion
This class does completion of URLs including user directories (~user) and environment variables...
Definition: kurlcompletion.h:41
KUrlRequester::lineEdit
KLineEdit * lineEdit() const
Definition: kurlrequester.cpp:441
KUrlRequester::clickMessage
QString clickMessage() const
kurlcompletion.h
KFileDialog::setSelection
void setSelection(const QString &name)
Sets the file name to preselect to name.
Definition: kfiledialog.cpp:454
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:50:03 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KIO

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

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  • kjsembed
  •   WTF
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Nepomuk-Core
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal