File indexing completed on 2024-06-09 07:41:05

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 2000 Wilco Greven <greven@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "kurlrequesterdialog.h"
0009 
0010 #include <QDialogButtonBox>
0011 #include <QFileDialog>
0012 #include <QLabel>
0013 #include <QVBoxLayout>
0014 
0015 #include <KLineEdit>
0016 #include <KLocalizedString>
0017 #include <KStandardGuiItem>
0018 #include <krecentdocument.h>
0019 #include <kurlrequester.h>
0020 
0021 class KUrlRequesterDialogPrivate
0022 {
0023 public:
0024     explicit KUrlRequesterDialogPrivate(KUrlRequesterDialog *qq)
0025         : q(qq)
0026     {
0027     }
0028 
0029     KUrlRequesterDialog *const q;
0030 
0031     void initDialog(const QString &text, const QUrl &url);
0032 
0033     // slots
0034     void slotTextChanged(const QString &);
0035 
0036     KUrlRequester *urlRequester;
0037     QDialogButtonBox *buttonBox;
0038 };
0039 
0040 KUrlRequesterDialog::KUrlRequesterDialog(const QUrl &urlName, QWidget *parent)
0041     : QDialog(parent)
0042     , d(new KUrlRequesterDialogPrivate(this))
0043 {
0044     d->initDialog(i18n("Location:"), urlName);
0045 }
0046 
0047 KUrlRequesterDialog::KUrlRequesterDialog(const QUrl &urlName, const QString &_text, QWidget *parent)
0048     : QDialog(parent)
0049     , d(new KUrlRequesterDialogPrivate(this))
0050 {
0051     d->initDialog(_text, urlName);
0052 }
0053 
0054 KUrlRequesterDialog::~KUrlRequesterDialog() = default;
0055 
0056 void KUrlRequesterDialogPrivate::initDialog(const QString &text, const QUrl &urlName)
0057 {
0058     QVBoxLayout *topLayout = new QVBoxLayout(q);
0059 
0060     QLabel *label = new QLabel(text, q);
0061     label->setWordWrap(true);
0062     topLayout->addWidget(label);
0063 
0064     urlRequester = new KUrlRequester(urlName, q);
0065     urlRequester->setMinimumWidth(urlRequester->sizeHint().width() * 3);
0066     topLayout->addWidget(urlRequester);
0067     urlRequester->setFocus();
0068     QObject::connect(urlRequester->lineEdit(), &KLineEdit::textChanged, q, [this](const QString &text) {
0069         slotTextChanged(text);
0070     });
0071     /*
0072     KFile::Mode mode = static_cast<KFile::Mode>( KFile::File |
0073             KFile::ExistingOnly );
0074     urlRequester_->setMode( mode );
0075     */
0076 
0077     buttonBox = new QDialogButtonBox(q);
0078     buttonBox->setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
0079     QObject::connect(buttonBox, &QDialogButtonBox::accepted, q, &QDialog::accept);
0080     QObject::connect(buttonBox, &QDialogButtonBox::rejected, q, &QDialog::reject);
0081     topLayout->addWidget(buttonBox);
0082 
0083     slotTextChanged(urlName.toString());
0084 }
0085 
0086 void KUrlRequesterDialogPrivate::slotTextChanged(const QString &text)
0087 {
0088     bool state = !text.trimmed().isEmpty();
0089     buttonBox->button(QDialogButtonBox::Ok)->setEnabled(state);
0090 }
0091 
0092 QUrl KUrlRequesterDialog::selectedUrl() const
0093 {
0094     if (result() == QDialog::Accepted) {
0095         return d->urlRequester->url();
0096     } else {
0097         return QUrl();
0098     }
0099 }
0100 
0101 QUrl KUrlRequesterDialog::getUrl(const QUrl &dir, QWidget *parent, const QString &title)
0102 {
0103     KUrlRequesterDialog dlg(dir, parent);
0104 
0105     dlg.setWindowTitle(title.isEmpty() ? i18n("Open") : title);
0106 
0107     dlg.exec();
0108 
0109     const QUrl &url = dlg.selectedUrl();
0110     if (url.isValid()) {
0111         KRecentDocument::add(url);
0112     }
0113 
0114     return url;
0115 }
0116 
0117 KUrlRequester *KUrlRequesterDialog::urlRequester()
0118 {
0119     return d->urlRequester;
0120 }
0121 
0122 #include "moc_kurlrequesterdialog.cpp"