]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/places/placesitemeditdialog.cpp
port Dolphin from KUrl to QUrl
[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 <k4aboutdata.h>
27 #include <KDebug>
28 #include <KFile>
29 #include <KGlobal>
30 #include <KIconButton>
31 #include <KLineEdit>
32 #include <KLocalizedString>
33 #include <KMimeType>
34 #include <KUrlRequester>
35 #include <KComponentData>
36 #include <QCheckBox>
37 #include <QEvent>
38 #include <QFormLayout>
39 #include <QVBoxLayout>
40 #include <QDialogButtonBox>
41 #include <QPushButton>
42
43 PlacesItemEditDialog::PlacesItemEditDialog(QWidget* parent) :
44 QDialog(parent),
45 m_icon(),
46 m_text(),
47 m_url(),
48 m_allowGlobal(false),
49 m_urlEdit(0),
50 m_textEdit(0),
51 m_iconButton(0),
52 m_appLocal(0)
53 {
54 }
55
56 void PlacesItemEditDialog::setIcon(const QString& icon)
57 {
58 m_icon = icon;
59 }
60
61 QString PlacesItemEditDialog::icon() const
62 {
63 return m_iconButton->icon();
64 }
65
66 void PlacesItemEditDialog::setText(const QString& text)
67 {
68 m_text = text;
69 }
70
71 QString PlacesItemEditDialog::text() const
72 {
73 QString text = m_textEdit->text();
74 if (text.isEmpty()) {
75 const QUrl url = m_urlEdit->url();
76 text = url.fileName().isEmpty() ? url.toDisplayString(QUrl::PreferLocalFile) : url.fileName();
77 }
78 return text;
79 }
80
81 void PlacesItemEditDialog::setUrl(const QUrl& url)
82 {
83 m_url = url;
84 }
85
86 QUrl PlacesItemEditDialog::url() const
87 {
88 return m_urlEdit->url();
89 }
90
91 void PlacesItemEditDialog::setAllowGlobal(bool allow)
92 {
93 m_allowGlobal = allow;
94 }
95
96 bool PlacesItemEditDialog::allowGlobal() const
97 {
98 return m_allowGlobal;
99 }
100
101 bool PlacesItemEditDialog::event(QEvent* event)
102 {
103 if (event->type() == QEvent::Polish) {
104 initialize();
105 }
106 return QWidget::event(event);
107 }
108
109 void PlacesItemEditDialog::slotUrlChanged(const QString& text)
110 {
111 m_okButton->setEnabled(!text.isEmpty());
112 }
113
114 PlacesItemEditDialog::~PlacesItemEditDialog()
115 {
116 }
117
118 void PlacesItemEditDialog::initialize()
119 {
120 QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok|QDialogButtonBox::Cancel);
121 m_okButton = buttonBox->button(QDialogButtonBox::Ok);
122 m_okButton->setDefault(true);
123 m_okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
124 connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
125 connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
126 setModal(true);
127 m_okButton->setDefault(true);
128
129 QVBoxLayout *mainLayout = new QVBoxLayout;
130 setLayout(mainLayout);
131 QWidget* mainWidget = new QWidget(this);
132 mainLayout->addWidget(mainWidget);
133 mainLayout->addWidget(buttonBox);
134
135 QVBoxLayout* vBox = new QVBoxLayout(mainWidget);
136
137 QFormLayout* formLayout = new QFormLayout();
138 vBox->addLayout( formLayout );
139
140 m_textEdit = new KLineEdit(mainWidget);
141 formLayout->addRow(i18nc("@label", "Label:"), m_textEdit);
142 m_textEdit->setText(m_text);
143 m_textEdit->setClickMessage(i18n("Enter descriptive label here"));
144
145 m_urlEdit = new KUrlRequester(m_url, mainWidget);
146 m_urlEdit->setMode(KFile::Directory);
147 formLayout->addRow(i18nc("@label", "Location:"), m_urlEdit);
148 // Provide room for at least 40 chars (average char width is half of height)
149 m_urlEdit->setMinimumWidth(m_urlEdit->fontMetrics().height() * (40 / 2));
150 connect(m_urlEdit->lineEdit(), &KLineEdit::textChanged, this, &PlacesItemEditDialog::slotUrlChanged);
151
152 m_iconButton = new KIconButton(mainWidget);
153 formLayout->addRow(i18nc("@label", "Choose an icon:"), m_iconButton);
154 m_iconButton->setIconSize(IconSize(KIconLoader::Desktop));
155 m_iconButton->setIconType(KIconLoader::NoGroup, KIconLoader::Place);
156 if (m_icon.isEmpty()) {
157 m_iconButton->setIcon(KMimeType::iconNameForUrl(m_url));
158 } else {
159 m_iconButton->setIcon(m_icon);
160 }
161
162 if (m_allowGlobal) {
163 QString appName;
164 if (KGlobal::mainComponent().aboutData()) {
165 appName = KGlobal::mainComponent().aboutData()->programName();
166 }
167 if (appName.isEmpty()) {
168 appName = KGlobal::mainComponent().componentName();
169 }
170 m_appLocal = new QCheckBox( i18n("&Only show when using this application (%1)", appName ), mainWidget );
171 m_appLocal->setChecked(false);
172 vBox->addWidget(m_appLocal);
173 }
174
175 if (m_text.isEmpty()) {
176 m_urlEdit->setFocus();
177 } else {
178 m_textEdit->setFocus();
179 }
180
181 }
182