35#include <QApplication>
41 KUrlDragPushButton(
QWidget *parent)
46 ~KUrlDragPushButton() {}
48 void setURL(
const KUrl& url )
55 virtual QDrag *dragObject()
60 QDrag *drag =
new QDrag(
this);
61 QMimeData *mimeData =
new QMimeData;
62 m_urls.populateMimeData(mimeData);
63 drag->setMimeData(mimeData);
73class KUrlRequester::KUrlRequesterPrivate
84 ~KUrlRequesterPrivate()
92 void setText(
const QString& text ) {
95 if (combo->isEditable())
97 combo->setEditText( text );
101 int i = combo->findText( text );
104 combo->addItem( text );
105 combo->setCurrentIndex( combo->count()-1 );
109 combo->setCurrentIndex( i );
115 edit->setText( text );
119 void connectSignals(
QObject *receiver )
128 connect( sender, SIGNAL(editTextChanged(QString)),
143 combo->setCompletionObject( comp );
145 edit->setCompletionObject( comp );
148 void updateCompletionStartDir(
const KUrl &newStartDir )
154 QString text()
const {
155 return combo ? combo->currentText() : edit->text();
163 const QString txt = text();
166 comp = qobject_cast<KUrlCompletion*>(combo->completionObject());
168 comp = qobject_cast<KUrlCompletion*>(edit->completionObject());
171 KUrl baseDir(m_startDir);
175 enteredPath =
KUrl( txt );
177 if ( enteredPath.isRelative() && !txt.isEmpty() ) {
178 baseDir.addPath(enteredPath.
path());
186 void _k_slotUpdateUrl();
187 void _k_slotOpenDialog();
188 void _k_slotFileDialogFinished();
191 bool m_startDirCustomized;
195 KFile::Modes fileDialogMode;
196 QString fileDialogFilter;
197#ifndef KDE_NO_DEPRECATED
202 KUrlDragPushButton *myButton;
205 Qt::WindowModality fileDialogModality;
211 :
KHBox( parent),d(new KUrlRequesterPrivate(this))
214 editWidget->setParent(
this );
215 d->combo = qobject_cast<KComboBox*>( editWidget );
216 d->edit = qobject_cast<KLineEdit*>( editWidget );
218 d->edit->setClearButtonShown(
true );
226 :
KHBox( parent),d(new KUrlRequesterPrivate(this))
233 :
KHBox( parent),d(new KUrlRequesterPrivate(this))
245void KUrlRequester::KUrlRequesterPrivate::init()
247 m_parent->setMargin(0);
248 m_parent->setSpacing(-1);
251 fileDialogModality = Qt::ApplicationModal;
253 if ( !combo && !edit ) {
255 edit->setClearButtonShown(
true );
260 myButton =
new KUrlDragPushButton(m_parent);
261 myButton->setIcon(
KIcon(
"document-open"));
262 int buttonSize = myButton->sizeHint().expandedTo(widget->sizeHint()).height();
263 myButton->setFixedSize(buttonSize, buttonSize);
264 myButton->setToolTip(
i18n(
"Open file dialog"));
266 m_parent->connect(myButton, SIGNAL(pressed()), SLOT(_k_slotUpdateUrl()));
268 widget->installEventFilter( m_parent );
269 m_parent->setFocusProxy( widget );
270 m_parent->setFocusPolicy(Qt::StrongFocus);
272 connectSignals( m_parent );
273 m_parent->connect(myButton, SIGNAL(clicked()), m_parent, SLOT(_k_slotOpenDialog()));
276 m_startDirCustomized =
false;
279 updateCompletionStartDir(m_startDir);
280 setCompletionObject( myCompletion );
284 m_parent->connect(openAction, SIGNAL(triggered(
bool)), SLOT(_k_slotOpenDialog()));
292#ifndef KDE_NO_DEPRECATED
306 d->m_startDir = startDir;
307 d->m_startDirCustomized =
true;
308 d->updateCompletionStartDir(startDir);
313 if (e->type()==QEvent::WindowTitleChange) {
314 if (d->myFileDialog) {
315 d->myFileDialog->setCaption(windowTitle());
318 KHBox::changeEvent(e);
328 return d->m_startDir;
336void KUrlRequester::KUrlRequesterPrivate::_k_slotOpenDialog()
339 if ( myFileDialog->isVisible() )
342 myFileDialog->raise();
343 myFileDialog->activateWindow();
352 const KUrl openUrl = (!m_parent->url().isEmpty() && !m_parent->url().isRelative() )
353 ? m_parent->
url() : m_startDir;
363 if ( newurl.isValid() )
365 m_parent->setUrl( newurl );
366 emit m_parent->urlSelected( url() );
371 emit m_parent->openFileDialog( m_parent );
376 if ( !url().isEmpty() && !url().isRelative() ) {
386 if ( dlg->windowModality() != fileDialogModality )
387 dlg->setWindowModality(fileDialogModality);
389 if ( fileDialogModality == Qt::NonModal )
398void KUrlRequester::KUrlRequesterPrivate::_k_slotFileDialogFinished()
403 if ( myFileDialog->result() == QDialog::Accepted )
405 KUrl newUrl = myFileDialog->selectedUrl();
406 if ( newUrl.isValid() )
408 m_parent->setUrl( newUrl );
409 emit m_parent->urlSelected( url() );
411 if ( newUrl.
isLocalFile() && !m_startDirCustomized ) {
413 m_startDir.
setPath(m_startDir.directory());
414 updateCompletionStartDir(m_startDir);
423 d->fileDialogMode = mode;
427 if (d->myFileDialog) {
428 d->myFileDialog->setMode(d->fileDialogMode);
434 return d->fileDialogMode;
439 d->fileDialogFilter = filter;
440 if (d->myFileDialog) {
441 d->myFileDialog->setFilter(d->fileDialogFilter);
447 return d->fileDialogFilter;
452 if (!d->myFileDialog) {
454 d->myFileDialog =
new KFileDialog(QString(), d->fileDialogFilter, p);
455 d->myFileDialog->setMode(d->fileDialogMode);
456 d->myFileDialog->setCaption(windowTitle());
457 d->myFileDialog->setWindowModality(d->fileDialogModality);
458 connect(d->myFileDialog, SIGNAL(finished()), SLOT(_k_slotFileDialogFinished()));
461 return d->myFileDialog;
466 d->setText( QString() );
479void KUrlRequester::KUrlRequesterPrivate::_k_slotUpdateUrl()
482 myButton->setURL( u );
487 if ( ( d->edit == obj ) || ( d->combo == obj ) )
489 if (( ev->type() == QEvent::FocusIn ) || ( ev->type() == QEvent::FocusOut ))
491 QApplication::sendEvent(
this, ev );
493 return QWidget::eventFilter( obj, ev );
503 return d->myCompletion;
509 d->edit->setClickMessage(msg);
515 return d->edit->clickMessage();
522 return d->fileDialogModality;
527 d->fileDialogModality = modality;
530#ifndef KDE_NO_DEPRECATED
536 setSizePolicy(QSizePolicy( QSizePolicy::Preferred,
537 QSizePolicy::Fixed));
540 if ( !edit && d->combo )
541 edit = qobject_cast<KLineEdit*>( d->combo->lineEdit() );
545 kWarning() <<
"KUrlRequester's lineedit is not a KLineEdit!??\n";
549 d->editor.setRepresentationWidget(
this);
550 d->editor.setLineEdit(edit);
559#include "kurlrequester.moc"
Provides a user (and developer) friendly way to select files and directories.
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...
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...
void setUrl(const KUrl &url, bool clearforward=true)
Sets the directory to view.
void setSelection(const QString &name)
Sets the file name to preselect to name.
KFile is a class which provides a namespace for some enumerated values associated with the kfile libr...
static bool supportsListing(const KUrl &url)
Returns whether the protocol can list files/objects.
KUrlComboRequester(QWidget *parent=0)
Constructs a KUrlRequester widget with a combobox.
This class does completion of URLs including user directories (~user) and environment variables.
QString replacedPath(const QString &text) const
Replaces username and/or environment variables, depending on the current settings and returns the fil...
This class is a widget showing a lineedit and a button, which invokes a filedialog.
void setFileDialogModality(Qt::WindowModality modality)
Set the window modality for the file dialog to modality Directory selection dialogs are always modal.
virtual void changeEvent(QEvent *e)
KUrlRequester(QWidget *parent=0)
Constructs a KUrlRequester widget.
void clear()
Clears the lineedit/combobox.
void setClickMessage(const QString &msg)
Set a click message msg.
void setFilter(const QString &filter)
Sets the filter for the file dialog.
void setUrl(const KUrl &url)
Sets the url in the lineedit to url.
KLineEdit * lineEdit() const
const KEditListBox::CustomEditor & customEditor()
void setStartDir(const KUrl &startDir)
Sets the start dir startDir.
KUrlCompletion * completionObject() const
void setText(const QString &text)
Sets the current text in the lineedit or combobox.
Qt::WindowModality fileDialogModality
bool eventFilter(QObject *obj, QEvent *ev)
void setMode(KFile::Modes m)
Sets the mode of the file dialog.
void textChanged(const QString &)
Emitted when the text in the lineedit changes.
KPushButton * button() const
void setPath(const QString &path)
Sets the url in the lineedit to KUrl::fromPath(path).
virtual KFileDialog * fileDialog() const
KComboBox * comboBox() const
void returnPressed()
Emitted when return or enter was pressed in the lineedit.
~KUrlRequester()
Destructs the KUrlRequester.
QString pathOrUrl() const
static KUrl fromPath(const QString &text)
QString url(AdjustPathOption trailing=LeaveTrailingSlash) const
QString path(AdjustPathOption trailing=LeaveTrailingSlash) const
void setPath(const QString &path)
QString toLocalFile(AdjustPathOption trailing=LeaveTrailingSlash) const
QString i18n(const char *text)