KIO

knameandurlinputdialog.cpp
1/*
2 SPDX-FileCopyrightText: 1998, 2008, 2009 David Faure <faure@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
5*/
6
7#include "knameandurlinputdialog.h"
8
9#include <KLineEdit> // For KUrlRequester::lineEdit()
10#include <QDialogButtonBox>
11#include <QFormLayout>
12#include <QVBoxLayout>
13#include <kprotocolmanager.h>
14#include <kurlrequester.h>
15
16class KNameAndUrlInputDialogPrivate
17{
18public:
19 explicit KNameAndUrlInputDialogPrivate(KNameAndUrlInputDialog *qq)
20 : m_fileNameEdited(false)
21 , q(qq)
22 {
23 }
24
25 void slotNameTextChanged(const QString &);
26 void slotURLTextChanged(const QString &);
27
28 /**
29 * The line edit widget for the fileName
30 */
31 QLineEdit *m_leName;
32 /**
33 * The URL requester for the URL :)
34 */
35 KUrlRequester *m_urlRequester;
36 /**
37 * True if the filename was manually edited.
38 */
39 bool m_fileNameEdited;
40
41 QDialogButtonBox *m_buttonBox;
42
44};
45
46KNameAndUrlInputDialog::KNameAndUrlInputDialog(const QString &nameLabel, const QString &urlLabel, const QUrl &startDir, QWidget *parent)
47 : QDialog(parent)
48 , d(new KNameAndUrlInputDialogPrivate(this))
49{
50 QVBoxLayout *topLayout = new QVBoxLayout(this);
51
52 QFormLayout *formLayout = new QFormLayout;
53 formLayout->setContentsMargins(0, 0, 0, 0);
54
55 // First line: filename
56 d->m_leName = new QLineEdit(this);
57 d->m_leName->setMinimumWidth(d->m_leName->sizeHint().width() * 3);
58 d->m_leName->setSelection(0, d->m_leName->text().length()); // autoselect
59 connect(d->m_leName, &QLineEdit::textChanged, this, [this](const QString &text) {
60 d->slotNameTextChanged(text);
61 });
62 formLayout->addRow(nameLabel, d->m_leName);
63
64 // Second line: url
65 d->m_urlRequester = new KUrlRequester(this);
66 d->m_urlRequester->setStartDir(startDir);
67 d->m_urlRequester->setMode(KFile::File | KFile::Directory);
68
69 d->m_urlRequester->setMinimumWidth(d->m_urlRequester->sizeHint().width() * 3);
70 connect(d->m_urlRequester->lineEdit(), &QLineEdit::textChanged, this, [this](const QString &text) {
71 d->slotURLTextChanged(text);
72 });
73 formLayout->addRow(urlLabel, d->m_urlRequester);
74
75 topLayout->addLayout(formLayout);
76
77 d->m_buttonBox = new QDialogButtonBox(this);
78 d->m_buttonBox->setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
79 connect(d->m_buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
80 connect(d->m_buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
81 topLayout->addWidget(d->m_buttonBox);
82
83 d->m_fileNameEdited = false;
84 d->m_buttonBox->button(QDialogButtonBox::Ok)->setEnabled(!d->m_leName->text().isEmpty() && !d->m_urlRequester->url().isEmpty());
85 d->m_leName->setFocus();
86}
87
89
91{
92 return d->m_urlRequester->url();
93}
94
96{
97 return d->m_urlRequester->text();
98}
99
101{
102 return d->m_leName->text();
103}
104
105void KNameAndUrlInputDialogPrivate::slotNameTextChanged(const QString &)
106{
107 m_fileNameEdited = true;
108 m_buttonBox->button(QDialogButtonBox::Ok)->setEnabled(!m_leName->text().isEmpty() && !m_urlRequester->url().isEmpty());
109}
110
111void KNameAndUrlInputDialogPrivate::slotURLTextChanged(const QString &)
112{
113 if (!m_fileNameEdited) {
114 // use URL as default value for the filename
115 // (we copy only its filename if protocol supports listing,
116 // but for HTTP we don't want tons of index.html links)
117 QUrl url(m_urlRequester->url());
118 if (KProtocolManager::supportsListing(url) && !url.fileName().isEmpty()) {
119 m_leName->setText(url.fileName());
120 } else {
121 m_leName->setText(url.toString());
122 }
123 m_fileNameEdited = false; // slotNameTextChanged set it to true erroneously
124 }
125 m_buttonBox->button(QDialogButtonBox::Ok)->setEnabled(!m_leName->text().isEmpty() && !m_urlRequester->url().isEmpty());
126}
127
129{
130 d->m_leName->setText(name);
131 d->m_urlRequester->setFocus();
132}
133
135{
136 d->m_urlRequester->setUrl(url);
137}
138
139#include "moc_knameandurlinputdialog.cpp"
Dialog to ask for a name (e.g. filename) and a URL Basically a merge of KLineEditDlg and KUrlRequeste...
KNameAndUrlInputDialog(const QString &nameLabel, const QString &urlLabel, const QUrl &startDir, QWidget *parent)
~KNameAndUrlInputDialog() override
Destructor.
void setSuggestedUrl(const QUrl &url)
Pre-fill the URL requester.
void setSuggestedName(const QString &name)
Pre-fill the name lineedit.
static bool supportsListing(const QUrl &url)
Returns whether the protocol can list files/objects.
This class is a widget showing a lineedit and a button, which invokes a filedialog.
void addLayout(QLayout *layout, int stretch)
void addWidget(QWidget *widget, int stretch, Qt::Alignment alignment)
virtual void accept()
virtual void reject()
QPushButton * button(StandardButton which) const const
void addRow(QLayout *layout)
void setContentsMargins(const QMargins &margins)
void textChanged(const QString &text)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
bool isEmpty() const const
void setEnabled(bool)
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.