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
40KUrlRequesterDialog::KUrlRequesterDialog(const QUrl &urlName, QWidget *parent)
41 : QDialog(parent)
42 , d(new KUrlRequesterDialogPrivate(this))
43{
44 d->initDialog(i18n("Location:"), url: 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: _text, url: urlName);
52}
53
54KUrlRequesterDialog::~KUrlRequesterDialog() = default;
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(sender: urlRequester->lineEdit(), signal: &KLineEdit::textChanged, context: q, slot: [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);
78 buttonBox->setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
79 QObject::connect(sender: buttonBox, signal: &QDialogButtonBox::accepted, context: q, slot: &QDialog::accept);
80 QObject::connect(sender: buttonBox, signal: &QDialogButtonBox::rejected, context: q, slot: &QDialog::reject);
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(which: QDialogButtonBox::Ok)->setEnabled(state);
90}
91
92QUrl KUrlRequesterDialog::selectedUrl() const
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()) {
111 KRecentDocument::add(url);
112 }
113
114 return url;
115}
116
117KUrlRequester *KUrlRequesterDialog::urlRequester()
118{
119 return d->urlRequester;
120}
121
122#include "moc_kurlrequesterdialog.cpp"
123

source code of kio/src/widgets/kurlrequesterdialog.cpp