KIO

kfileplaceeditdialog.cpp
1/*
2 This file is part of the KDE libraries
3 SPDX-FileCopyrightText: 2001, 2002, 2003 Carsten Pfeiffer <pfeiffer@kde.org>
4 SPDX-FileCopyrightText: 2007 Kevin Ottens <ervin@kde.org>
5
6 SPDX-License-Identifier: LGPL-2.0-only
7*/
8
9#include "kfileplaceeditdialog.h"
10
11#include <KAboutData>
12#include <KConfig>
13#include <KIconButton>
14#include <KLineEdit> // For KUrlRequester::lineEdit()
15#include <KLocalizedString>
16#include <kio/global.h>
17#include <kprotocolinfo.h>
18#include <kurlrequester.h>
19
20#include <QApplication>
21#include <QCheckBox>
22#include <QDialogButtonBox>
23#include <QFormLayout>
24#include <qdrawutil.h>
25
26#include <KConfigGroup>
27#include <qplatformdefs.h>
28
30 QUrl &url,
31 QString &label,
32 QString &icon,
33 bool isAddingNewPlace,
34 bool &appLocal,
35 int iconSize,
36 QWidget *parent)
37{
38 KFilePlaceEditDialog *dialog = new KFilePlaceEditDialog(allowGlobal, url, label, icon, isAddingNewPlace, appLocal, iconSize, parent);
39 if (dialog->exec() == QDialog::Accepted) {
40 // set the return parameters
41 url = dialog->url();
42 label = dialog->label();
43 icon = dialog->icon();
44 appLocal = dialog->applicationLocal();
45
46 delete dialog;
47 return true;
48 }
49
50 delete dialog;
51 return false;
52}
53
55 const QUrl &url,
56 const QString &label,
57 const QString &icon,
58 bool isAddingNewPlace,
59 bool appLocal,
60 int iconSize,
61 QWidget *parent)
62 : QDialog(parent)
63 , m_iconButton(nullptr)
64{
65 if (isAddingNewPlace) {
66 setWindowTitle(i18n("Add Places Entry"));
67 } else {
68 setWindowTitle(i18n("Edit Places Entry"));
69 }
70 setModal(true);
71
72 QVBoxLayout *box = new QVBoxLayout(this);
73
75 box->addLayout(layout);
76
77 QString whatsThisText = i18n(
78 "<qt>This is the text that will appear in the Places panel.<br /><br />"
79 "The label should consist of one or two words "
80 "that will help you remember what this entry refers to. "
81 "If you do not enter a label, it will be derived from "
82 "the location's URL.</qt>");
83 m_labelEdit = new QLineEdit(this);
84 layout->addRow(i18n("L&abel:"), m_labelEdit);
85 m_labelEdit->setText(label);
86 m_labelEdit->setPlaceholderText(i18n("Enter descriptive label here"));
87 m_labelEdit->setWhatsThis(whatsThisText);
88 layout->labelForField(m_labelEdit)->setWhatsThis(whatsThisText);
89
90 whatsThisText = i18n(
91 "<qt>This is the location associated with the entry. Any valid URL may be used. For example:<br /><br />"
92 "%1<br />http://www.kde.org<br />ftp://ftp.kde.org/pub/kde/stable<br /><br />"
93 "By clicking on the button next to the text edit box you can browse to an "
94 "appropriate URL.</qt>",
96 m_urlEdit = new KUrlRequester(url, this);
97 m_urlEdit->setMode(KFile::Directory);
98 layout->addRow(i18n("&Location:"), m_urlEdit);
99 m_urlEdit->setWhatsThis(whatsThisText);
100 layout->labelForField(m_urlEdit)->setWhatsThis(whatsThisText);
101 // Room for at least 40 chars (average char width is half of height)
102 m_urlEdit->setMinimumWidth(m_urlEdit->fontMetrics().height() * (40 / 2));
103
104 whatsThisText = i18n(
105 "<qt>This is the icon that will appear in the Places panel.<br /><br />"
106 "Click on the button to select a different icon.</qt>");
107 m_iconButton = new KIconButton(this);
108 m_iconButton->setObjectName(QStringLiteral("icon button"));
109 m_iconButton->setIconSize(iconSize);
111 if (icon.isEmpty()) {
112 m_iconButton->setIcon(KIO::iconNameForUrl(url));
113 } else {
114 m_iconButton->setIcon(icon);
115 }
116 m_iconButton->setWhatsThis(whatsThisText);
117
118 if (url.scheme() == QLatin1String("trash")) {
119 // Since there are separate trash icons when it is empty/non-empty,
120 // the trash item's icon is made non-editable for simplicity
121 m_iconButton->hide();
122 // making the trash item's url editable misleads users into
123 // thinking that the actual trash location is configurable here
124 m_urlEdit->setDisabled(true);
125 } else {
126 layout->addRow(i18n("Choose an &icon:"), m_iconButton);
127 layout->labelForField(m_iconButton)->setWhatsThis(whatsThisText);
128 }
129
130 if (allowGlobal) {
131 QString appName;
133 if (appName.isEmpty()) {
135 }
136 m_appLocal = new QCheckBox(i18n("&Only show when using this application (%1)", appName), this);
137 m_appLocal->setChecked(appLocal);
138 m_appLocal->setWhatsThis(
139 i18n("<qt>Select this setting if you want this "
140 "entry to show only when using the current application (%1).<br /><br />"
141 "If this setting is not selected, the entry will be available in all "
142 "applications.</qt>",
143 appName));
144 box->addWidget(m_appLocal);
145 } else {
146 m_appLocal = nullptr;
147 }
148 connect(m_urlEdit->lineEdit(), &QLineEdit::textChanged, this, &KFilePlaceEditDialog::urlChanged);
149 if (!label.isEmpty()) {
150 // editing existing entry
151 m_labelEdit->setFocus();
152 } else {
153 // new entry
154 m_urlEdit->setFocus();
155 }
156
157 m_buttonBox = new QDialogButtonBox(this);
161 box->addWidget(m_buttonBox);
162}
163
167
168void KFilePlaceEditDialog::urlChanged(const QString &text)
169{
170 m_buttonBox->button(QDialogButtonBox::Ok)->setEnabled(!text.isEmpty());
171}
172
174{
175 return m_urlEdit->url();
176}
177
179{
180 if (!m_labelEdit->text().isEmpty()) {
181 return m_labelEdit->text();
182 }
183
184 // derive descriptive label from the URL
185 QUrl url = m_urlEdit->url();
186 if (!url.fileName().isEmpty()) {
187 return url.fileName();
188 }
189 if (!url.host().isEmpty()) {
190 return url.host();
191 }
192 return url.scheme();
193}
194
196{
197 return m_iconButton->icon();
198}
199
201{
202 if (!m_appLocal) {
203 return true;
204 }
205
206 return m_appLocal->isChecked();
207}
208
209#include "moc_kfileplaceeditdialog.cpp"
A dialog that allows editing entries of a KFilePlacesModel.
KFilePlaceEditDialog(bool allowGlobal, const QUrl &url, const QString &label, const QString &icon, bool isAddingNewPlace, bool appLocal=true, int iconSize=KIconLoader::SizeMedium, QWidget *parent=nullptr)
Constructs a KFilePlaceEditDialog.
~KFilePlaceEditDialog() override
Destroys the dialog.
static bool getInformation(bool allowGlobal, QUrl &url, QString &label, QString &icon, bool isAddingNewPlace, bool &appLocal, int iconSize, QWidget *parent=nullptr)
A convenience method to show the dialog and retrieve all the properties via the given parameters.
void setIcon(const QString &icon)
void setIconType(KIconLoader::Group group, KIconLoader::Context context, bool user=false)
const QString & icon() const
void setIconSize(int size)
This class is a widget showing a lineedit and a button, which invokes a filedialog.
void setMode(KFile::Modes mode)
Sets the mode of the file dialog.
KLineEdit * lineEdit() const
QString i18n(const char *text, const TYPE &arg...)
KIOCORE_EXPORT QString iconNameForUrl(const QUrl &url)
Return the icon name for a URL.
Definition global.cpp:186
void setChecked(bool)
void addLayout(QLayout *layout, int stretch)
void addWidget(QWidget *widget, int stretch, Qt::Alignment alignment)
virtual void accept()
virtual int exec()
void setModal(bool modal)
virtual void reject()
QPushButton * button(StandardButton which) const const
void setStandardButtons(StandardButtons buttons)
QString homePath()
int height() const const
void setPlaceholderText(const QString &)
void setText(const QString &)
void textChanged(const QString &text)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QObject * parent() const const
void setObjectName(QAnyStringView name)
bool isEmpty() const const
QString fileName(ComponentFormattingOptions options) const const
QString host(ComponentFormattingOptions options) const const
QString scheme() const const
void setEnabled(bool)
QFontMetrics fontMetrics() const const
void hide()
QLayout * layout() const const
void setMinimumWidth(int minw)
void setDisabled(bool disable)
void setFocus()
void setWhatsThis(const QString &)
void setWindowTitle(const QString &)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:18:52 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.