File indexing completed on 2024-05-12 05:46:49

0001 /* This file is part of the KDE libraries
0002     Copyright (C) 2000 Wilco Greven <greven@kde.org>
0003 
0004     library is free software; you can redistribute it and/or
0005     modify it under the terms of the GNU Library General Public
0006     License as published by the Free Software Foundation; either
0007     version 2 of the License, or (at your option) any later version.
0008 
0009     This library is distributed in the hope that it will be useful,
0010     but WITHOUT ANY WARRANTY; without even the implied warranty of
0011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012     Library General Public License for more details.
0013 
0014     You should have received a copy of the GNU Library General Public License
0015     along with this library; see the file COPYING.LIB.  If not, write to
0016     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017     Boston, MA 02110-1301, USA.
0018 */
0019 
0020 #include "kurlrequesterdialog.h"
0021 
0022 #include <QDialogButtonBox>
0023 #include <QLabel>
0024 #include <QFileDialog>
0025 #include <QVBoxLayout>
0026 
0027 #include <klineedit.h>
0028 #include <klocalizedstring.h>
0029 #include <krecentdocument.h>
0030 #include <kstandardguiitem.h>
0031 #include <kurlrequester.h>
0032 
0033 class KUrlRequesterDialogPrivate
0034 {
0035 public:
0036     explicit KUrlRequesterDialogPrivate(KUrlRequesterDialog *qq)
0037         : q(qq)
0038     {
0039     }
0040 
0041     KUrlRequesterDialog * const q;
0042 
0043     void initDialog(const QString &text, const QUrl &url);
0044 
0045     // slots
0046     void _k_slotTextChanged(const QString &);
0047 
0048     KUrlRequester *urlRequester;
0049     QDialogButtonBox *buttonBox;
0050 };
0051 
0052 KUrlRequesterDialog::KUrlRequesterDialog(const QUrl &urlName, QWidget *parent)
0053     : QDialog(parent), d(new KUrlRequesterDialogPrivate(this))
0054 {
0055     d->initDialog(i18n("Location:"), urlName);
0056 }
0057 
0058 KUrlRequesterDialog::KUrlRequesterDialog(const QUrl &urlName, const QString &_text, QWidget *parent)
0059     : QDialog(parent), d(new KUrlRequesterDialogPrivate(this))
0060 {
0061     d->initDialog(_text, urlName);
0062 }
0063 
0064 KUrlRequesterDialog::~KUrlRequesterDialog()
0065 {
0066     delete d;
0067 }
0068 
0069 void KUrlRequesterDialogPrivate::initDialog(const QString &text, const QUrl &urlName)
0070 {
0071     QVBoxLayout *topLayout = new QVBoxLayout;
0072     q->setLayout(topLayout);
0073 
0074     QLabel *label = new QLabel(text, q);
0075     topLayout->addWidget(label);
0076 
0077     urlRequester = new KUrlRequester(urlName, q);
0078     urlRequester->setMinimumWidth(urlRequester->sizeHint().width() * 3);
0079     topLayout->addWidget(urlRequester);
0080     urlRequester->setFocus();
0081     QObject::connect(urlRequester->lineEdit(), SIGNAL(textChanged(QString)),
0082                      q, SLOT(_k_slotTextChanged(QString)));
0083     /*
0084     KFile::Mode mode = static_cast<KFile::Mode>( KFile::File |
0085             KFile::ExistingOnly );
0086     urlRequester_->setMode( mode );
0087     */
0088 
0089     buttonBox = new QDialogButtonBox(q);
0090     buttonBox->setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
0091     QObject::connect(buttonBox, &QDialogButtonBox::accepted, q, &QDialog::accept);
0092     QObject::connect(buttonBox, &QDialogButtonBox::rejected, q, &QDialog::reject);
0093     topLayout->addWidget(buttonBox);
0094 
0095     _k_slotTextChanged(urlName.toString());
0096 }
0097 
0098 void KUrlRequesterDialogPrivate::_k_slotTextChanged(const QString &text)
0099 {
0100     bool state = !text.trimmed().isEmpty();
0101     buttonBox->button(QDialogButtonBox::Ok)->setEnabled(state);
0102 }
0103 
0104 QUrl KUrlRequesterDialog::selectedUrl() const
0105 {
0106     if (result() == QDialog::Accepted) {
0107         return d->urlRequester->url();
0108     } else {
0109         return QUrl();
0110     }
0111 }
0112 
0113 QUrl KUrlRequesterDialog::getUrl(const QUrl &dir, QWidget *parent,
0114                                  const QString &caption)
0115 {
0116     KUrlRequesterDialog dlg(dir, parent);
0117 
0118     dlg.setWindowTitle(caption.isEmpty() ? i18n("Open") : caption);
0119 
0120     dlg.exec();
0121 
0122     const QUrl &url = dlg.selectedUrl();
0123     if (url.isValid()) {
0124         KRecentDocument::add(url);
0125     }
0126 
0127     return url;
0128 }
0129 
0130 QFileDialog *KUrlRequesterDialog::fileDialog()
0131 {
0132     return d->urlRequester->fileDialog();
0133 }
0134 
0135 KUrlRequester *KUrlRequesterDialog::urlRequester()
0136 {
0137     return d->urlRequester;
0138 }
0139 
0140 #include "moc_kurlrequesterdialog.cpp"
0141