File indexing completed on 2024-10-13 03:38:21
0001 /* 0002 SPDX-FileCopyrightText: 1998, 2008, 2009 David Faure <faure@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL 0005 */ 0006 0007 #include "knameandurlinputdialog.h" 0008 0009 #include <KLineEdit> // For KUrlRequester::lineEdit() 0010 #include <QDialogButtonBox> 0011 #include <QFormLayout> 0012 #include <QVBoxLayout> 0013 #include <kprotocolmanager.h> 0014 #include <kurlrequester.h> 0015 0016 class KNameAndUrlInputDialogPrivate 0017 { 0018 public: 0019 explicit KNameAndUrlInputDialogPrivate(KNameAndUrlInputDialog *qq) 0020 : m_fileNameEdited(false) 0021 , q(qq) 0022 { 0023 } 0024 0025 void slotNameTextChanged(const QString &); 0026 void slotURLTextChanged(const QString &); 0027 0028 /** 0029 * The line edit widget for the fileName 0030 */ 0031 QLineEdit *m_leName; 0032 /** 0033 * The URL requester for the URL :) 0034 */ 0035 KUrlRequester *m_urlRequester; 0036 /** 0037 * True if the filename was manually edited. 0038 */ 0039 bool m_fileNameEdited; 0040 0041 QDialogButtonBox *m_buttonBox; 0042 0043 KNameAndUrlInputDialog *const q; 0044 }; 0045 0046 KNameAndUrlInputDialog::KNameAndUrlInputDialog(const QString &nameLabel, const QString &urlLabel, const QUrl &startDir, QWidget *parent) 0047 : QDialog(parent) 0048 , d(new KNameAndUrlInputDialogPrivate(this)) 0049 { 0050 QVBoxLayout *topLayout = new QVBoxLayout(this); 0051 0052 QFormLayout *formLayout = new QFormLayout; 0053 formLayout->setContentsMargins(0, 0, 0, 0); 0054 0055 // First line: filename 0056 d->m_leName = new QLineEdit(this); 0057 d->m_leName->setMinimumWidth(d->m_leName->sizeHint().width() * 3); 0058 d->m_leName->setSelection(0, d->m_leName->text().length()); // autoselect 0059 connect(d->m_leName, &QLineEdit::textChanged, this, [this](const QString &text) { 0060 d->slotNameTextChanged(text); 0061 }); 0062 formLayout->addRow(nameLabel, d->m_leName); 0063 0064 // Second line: url 0065 d->m_urlRequester = new KUrlRequester(this); 0066 d->m_urlRequester->setStartDir(startDir); 0067 d->m_urlRequester->setMode(KFile::File | KFile::Directory); 0068 0069 d->m_urlRequester->setMinimumWidth(d->m_urlRequester->sizeHint().width() * 3); 0070 connect(d->m_urlRequester->lineEdit(), &QLineEdit::textChanged, this, [this](const QString &text) { 0071 d->slotURLTextChanged(text); 0072 }); 0073 formLayout->addRow(urlLabel, d->m_urlRequester); 0074 0075 topLayout->addLayout(formLayout); 0076 0077 d->m_buttonBox = new QDialogButtonBox(this); 0078 d->m_buttonBox->setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Cancel); 0079 connect(d->m_buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept); 0080 connect(d->m_buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject); 0081 topLayout->addWidget(d->m_buttonBox); 0082 0083 d->m_fileNameEdited = false; 0084 d->m_buttonBox->button(QDialogButtonBox::Ok)->setEnabled(!d->m_leName->text().isEmpty() && !d->m_urlRequester->url().isEmpty()); 0085 d->m_leName->setFocus(); 0086 } 0087 0088 KNameAndUrlInputDialog::~KNameAndUrlInputDialog() = default; 0089 0090 QUrl KNameAndUrlInputDialog::url() const 0091 { 0092 return d->m_urlRequester->url(); 0093 } 0094 0095 QString KNameAndUrlInputDialog::urlText() const 0096 { 0097 return d->m_urlRequester->text(); 0098 } 0099 0100 QString KNameAndUrlInputDialog::name() const 0101 { 0102 return d->m_leName->text(); 0103 } 0104 0105 void KNameAndUrlInputDialogPrivate::slotNameTextChanged(const QString &) 0106 { 0107 m_fileNameEdited = true; 0108 m_buttonBox->button(QDialogButtonBox::Ok)->setEnabled(!m_leName->text().isEmpty() && !m_urlRequester->url().isEmpty()); 0109 } 0110 0111 void KNameAndUrlInputDialogPrivate::slotURLTextChanged(const QString &) 0112 { 0113 if (!m_fileNameEdited) { 0114 // use URL as default value for the filename 0115 // (we copy only its filename if protocol supports listing, 0116 // but for HTTP we don't want tons of index.html links) 0117 QUrl url(m_urlRequester->url()); 0118 if (KProtocolManager::supportsListing(url) && !url.fileName().isEmpty()) { 0119 m_leName->setText(url.fileName()); 0120 } else { 0121 m_leName->setText(url.toString()); 0122 } 0123 m_fileNameEdited = false; // slotNameTextChanged set it to true erroneously 0124 } 0125 m_buttonBox->button(QDialogButtonBox::Ok)->setEnabled(!m_leName->text().isEmpty() && !m_urlRequester->url().isEmpty()); 0126 } 0127 0128 void KNameAndUrlInputDialog::setSuggestedName(const QString &name) 0129 { 0130 d->m_leName->setText(name); 0131 d->m_urlRequester->setFocus(); 0132 } 0133 0134 void KNameAndUrlInputDialog::setSuggestedUrl(const QUrl &url) 0135 { 0136 d->m_urlRequester->setUrl(url); 0137 } 0138 0139 #include "moc_knameandurlinputdialog.cpp"