]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/places/placesitemeditdialog.cpp
Remove unused #include
[dolphin.git] / src / panels / places / placesitemeditdialog.cpp
1 /***************************************************************************
2 * Copyright (C) 2012 by Peter Penz <peter.penz19@gmail.com> *
3 * *
4 * Based on KFilePlaceEditDialog from kdelibs: *
5 * Copyright (C) 2001,2002,2003 Carsten Pfeiffer <pfeiffer@kde.org> *
6 * Copyright (C) 2007 Kevin Ottens <ervin@kde.org> * *
7 * *
8 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (at your option) any later version. *
12 * *
13 * This program is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU General Public License *
19 * along with this program; if not, write to the *
20 * Free Software Foundation, Inc., *
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
22 ***************************************************************************/
23
24 #include "placesitemeditdialog.h"
25
26 #include <KAboutData>
27 #include "dolphindebug.h"
28 #include <KFile>
29 #include <KIconButton>
30 #include <KLocalizedString>
31 #include <QMimeDatabase>
32 #include <KUrlRequester>
33 #include <QCheckBox>
34 #include <QEvent>
35 #include <QFormLayout>
36 #include <QDialogButtonBox>
37 #include <QLineEdit>
38
39 PlacesItemEditDialog::PlacesItemEditDialog(QWidget* parent) :
40 QDialog(parent),
41 m_icon(),
42 m_text(),
43 m_url(),
44 m_allowGlobal(false),
45 m_urlEdit(nullptr),
46 m_textEdit(nullptr),
47 m_iconButton(nullptr),
48 m_appLocal(nullptr),
49 m_buttonBox(nullptr)
50 {
51 }
52
53 void PlacesItemEditDialog::setIcon(const QString& icon)
54 {
55 m_icon = icon;
56 }
57
58 QString PlacesItemEditDialog::icon() const
59 {
60 return m_iconButton->icon();
61 }
62
63 void PlacesItemEditDialog::setText(const QString& text)
64 {
65 m_text = text;
66 }
67
68 QString PlacesItemEditDialog::text() const
69 {
70 QString text = m_textEdit->text();
71 if (text.isEmpty()) {
72 const QUrl url = m_urlEdit->url();
73 text = url.fileName().isEmpty() ? url.toDisplayString(QUrl::PreferLocalFile) : url.fileName();
74 }
75 return text;
76 }
77
78 void PlacesItemEditDialog::setUrl(const QUrl& url)
79 {
80 m_url = url;
81 }
82
83 QUrl PlacesItemEditDialog::url() const
84 {
85 return m_urlEdit->url();
86 }
87
88 void PlacesItemEditDialog::setAllowGlobal(bool allow)
89 {
90 m_allowGlobal = allow;
91 }
92
93 bool PlacesItemEditDialog::allowGlobal() const
94 {
95 return m_allowGlobal;
96 }
97
98 bool PlacesItemEditDialog::event(QEvent* event)
99 {
100 if (event->type() == QEvent::Polish) {
101 initialize();
102 }
103 return QWidget::event(event);
104 }
105
106 void PlacesItemEditDialog::slotUrlChanged(const QString& text)
107 {
108 m_buttonBox->button(QDialogButtonBox::Ok)->setEnabled(!text.isEmpty());
109 }
110
111 PlacesItemEditDialog::~PlacesItemEditDialog()
112 {
113 }
114
115 void PlacesItemEditDialog::initialize()
116 {
117 m_buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok|QDialogButtonBox::Cancel, this);
118 connect(m_buttonBox, &QDialogButtonBox::accepted, this, &PlacesItemEditDialog::accept);
119 connect(m_buttonBox, &QDialogButtonBox::rejected, this, &PlacesItemEditDialog::reject);
120 setModal(true);
121
122 QVBoxLayout *mainLayout = new QVBoxLayout;
123 setLayout(mainLayout);
124 QWidget* mainWidget = new QWidget(this);
125 mainLayout->addWidget(mainWidget);
126 mainLayout->addWidget(m_buttonBox);
127
128 QVBoxLayout* vBox = new QVBoxLayout(mainWidget);
129
130 QFormLayout* formLayout = new QFormLayout();
131 vBox->addLayout( formLayout );
132
133 m_textEdit = new QLineEdit(mainWidget);
134 formLayout->addRow(i18nc("@label", "Label:"), m_textEdit);
135 m_textEdit->setText(m_text);
136 m_textEdit->setPlaceholderText(i18n("Enter descriptive label here"));
137
138 m_urlEdit = new KUrlRequester(m_url, mainWidget);
139 m_urlEdit->setMode(KFile::Directory);
140 formLayout->addRow(i18nc("@label", "Location:"), m_urlEdit);
141 // Provide room for at least 40 chars (average char width is half of height)
142 m_urlEdit->setMinimumWidth(m_urlEdit->fontMetrics().height() * (40 / 2));
143 connect(m_urlEdit, &KUrlRequester::textChanged, this, &PlacesItemEditDialog::slotUrlChanged);
144
145 m_iconButton = new KIconButton(mainWidget);
146 formLayout->addRow(i18nc("@label", "Choose an icon:"), m_iconButton);
147 m_iconButton->setIconSize(IconSize(KIconLoader::Desktop));
148 m_iconButton->setIconType(KIconLoader::NoGroup, KIconLoader::Place);
149 if (m_icon.isEmpty()) {
150 QMimeDatabase db;
151 m_iconButton->setIcon(db.mimeTypeForUrl(m_url).iconName());
152 } else {
153 m_iconButton->setIcon(m_icon);
154 }
155
156 if (m_allowGlobal) {
157 const QString appName = KAboutData::applicationData().displayName();
158 m_appLocal = new QCheckBox( i18n("&Only show when using this application (%1)", appName ), mainWidget );
159 m_appLocal->setChecked(false);
160 vBox->addWidget(m_appLocal);
161 }
162
163 if (m_text.isEmpty()) {
164 m_urlEdit->setFocus();
165 } else {
166 m_textEdit->setFocus();
167 }
168
169 }
170