KIO

kurlrequesterdialog.cpp
1/*
2 This file is part of the KDE libraries
3 SPDX-FileCopyrightText: 2000 Wilco Greven <greven@kde.org>
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6*/
7
8#include "kurlrequesterdialog.h"
9
10#include <QDialogButtonBox>
11#include <QFileDialog>
12#include <QLabel>
13#include <QVBoxLayout>
14
15#include <KLineEdit>
16#include <KLocalizedString>
17#include <KStandardGuiItem>
18#include <krecentdocument.h>
19#include <kurlrequester.h>
20
21class KUrlRequesterDialogPrivate
22{
23public:
24 explicit KUrlRequesterDialogPrivate(KUrlRequesterDialog *qq)
25 : q(qq)
26 {
27 }
28
29 KUrlRequesterDialog *const q;
30
31 void initDialog(const QString &text, const QUrl &url);
32
33 // slots
34 void slotTextChanged(const QString &);
35
36 KUrlRequester *urlRequester;
37 QDialogButtonBox *buttonBox;
38};
39
41 : QDialog(parent)
42 , d(new KUrlRequesterDialogPrivate(this))
43{
44 d->initDialog(i18n("Location:"), urlName);
45}
46
47KUrlRequesterDialog::KUrlRequesterDialog(const QUrl &urlName, const QString &_text, QWidget *parent)
48 : QDialog(parent)
49 , d(new KUrlRequesterDialogPrivate(this))
50{
51 d->initDialog(_text, urlName);
52}
53
55
56void KUrlRequesterDialogPrivate::initDialog(const QString &text, const QUrl &urlName)
57{
58 QVBoxLayout *topLayout = new QVBoxLayout(q);
59
60 QLabel *label = new QLabel(text, q);
61 label->setWordWrap(true);
62 topLayout->addWidget(label);
63
64 urlRequester = new KUrlRequester(urlName, q);
65 urlRequester->setMinimumWidth(urlRequester->sizeHint().width() * 3);
66 topLayout->addWidget(urlRequester);
67 urlRequester->setFocus();
68 QObject::connect(urlRequester->lineEdit(), &KLineEdit::textChanged, q, [this](const QString &text) {
69 slotTextChanged(text);
70 });
71 /*
72 KFile::Mode mode = static_cast<KFile::Mode>( KFile::File |
73 KFile::ExistingOnly );
74 urlRequester_->setMode( mode );
75 */
76
77 buttonBox = new QDialogButtonBox(q);
81 topLayout->addWidget(buttonBox);
82
83 slotTextChanged(urlName.toString());
84}
85
86void KUrlRequesterDialogPrivate::slotTextChanged(const QString &text)
87{
88 bool state = !text.trimmed().isEmpty();
89 buttonBox->button(QDialogButtonBox::Ok)->setEnabled(state);
90}
91
93{
94 if (result() == QDialog::Accepted) {
95 return d->urlRequester->url();
96 } else {
97 return QUrl();
98 }
99}
100
101QUrl KUrlRequesterDialog::getUrl(const QUrl &dir, QWidget *parent, const QString &title)
102{
103 KUrlRequesterDialog dlg(dir, parent);
104
105 dlg.setWindowTitle(title.isEmpty() ? i18n("Open") : title);
106
107 dlg.exec();
108
109 const QUrl &url = dlg.selectedUrl();
110 if (url.isValid()) {
112 }
113
114 return url;
115}
116
118{
119 return d->urlRequester;
120}
121
122#include "moc_kurlrequesterdialog.cpp"
static void add(const QUrl &url)
Add a new item to the Recent Document menu.
Dialog in which a user can enter a filename or url.
~KUrlRequesterDialog() override
Destructs the dialog.
QUrl selectedUrl() const
Returns the fully qualified filename.
KUrlRequester * urlRequester()
Returns a pointer to the KUrlRequester.
static QUrl getUrl(const QUrl &url=QUrl(), QWidget *parent=nullptr, const QString &title=QString())
Creates a modal dialog, executes it and returns the selected URL.
KUrlRequesterDialog(const QUrl &url, QWidget *parent=nullptr)
Constructs a KUrlRequesterDialog.
This class is a widget showing a lineedit and a button, which invokes a filedialog.
KLineEdit * lineEdit() const
QString i18n(const char *text, const TYPE &arg...)
void addWidget(QWidget *widget, int stretch, Qt::Alignment alignment)
virtual void accept()
virtual int exec()
virtual void reject()
int result() const const
QPushButton * button(StandardButton which) const const
void setStandardButtons(StandardButtons buttons)
void textChanged(const QString &text)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QObject * parent() const const
bool isEmpty() const const
QString trimmed() const const
bool isValid() const const
QString toString(FormattingOptions options) const const
void setEnabled(bool)
void setMinimumWidth(int minw)
void setFocus()
void setWindowTitle(const QString &)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:58:45 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.