]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/places/placesitemeditdialog.cpp
Places Panel: Implement eject and teardown actions
[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 <KComponentData>
28 #include <KDebug>
29 #include <KFile>
30 #include <KIconButton>
31 #include <KLineEdit>
32 #include <KLocale>
33 #include <KMimeType>
34 #include <KUrlRequester>
35 #include <QCheckBox>
36 #include <QEvent>
37 #include <QFormLayout>
38 #include <QVBoxLayout>
39
40 PlacesItemEditDialog::PlacesItemEditDialog(QWidget* parent) :
41 KDialog(parent),
42 m_icon(),
43 m_text(),
44 m_url(),
45 m_allowGlobal(false),
46 m_urlEdit(0),
47 m_textEdit(0),
48 m_iconButton(0),
49 m_appLocal(0)
50 {
51 setButtons( Ok | Cancel );
52 setModal(true);
53 setDefaultButton(Ok);
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 KUrl url = m_urlEdit->url();
76 text = url.fileName().isEmpty() ? url.prettyUrl() : url.fileName();
77 }
78 return text;
79 }
80
81 void PlacesItemEditDialog::setUrl(const KUrl& url)
82 {
83 m_url = url;
84 }
85
86 KUrl 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 enableButtonOk(!text.isEmpty());
112 }
113
114 PlacesItemEditDialog::~PlacesItemEditDialog()
115 {
116 }
117
118 void PlacesItemEditDialog::initialize()
119 {
120 QWidget* mainWidget = new QWidget(this);
121 QVBoxLayout* vBox = new QVBoxLayout(mainWidget);
122
123 QFormLayout* formLayout = new QFormLayout();
124 vBox->addLayout( formLayout );
125
126 m_textEdit = new KLineEdit(mainWidget);
127 formLayout->addRow(i18nc("@label", "Label:"), m_textEdit);
128 m_textEdit->setText(m_text);
129 m_textEdit->setClickMessage(i18n("Enter descriptive label here"));
130
131 m_urlEdit = new KUrlRequester(m_url.prettyUrl(), mainWidget);
132 m_urlEdit->setMode(KFile::Directory);
133 formLayout->addRow(i18nc("@label", "Location:"), m_urlEdit);
134 // Provide room for at least 40 chars (average char width is half of height)
135 m_urlEdit->setMinimumWidth(m_urlEdit->fontMetrics().height() * (40 / 2));
136 connect(m_urlEdit->lineEdit(), SIGNAL(textChanged(QString)), this, SLOT(slotUrlChanged(QString)));
137
138 m_iconButton = new KIconButton(mainWidget);
139 formLayout->addRow(i18nc("@label", "Choose an icon:"), m_iconButton);
140 m_iconButton->setIconSize(KIconLoader::SizeLarge);
141 m_iconButton->setIconType(KIconLoader::NoGroup, KIconLoader::Place);
142 if (m_icon.isEmpty()) {
143 m_iconButton->setIcon(KMimeType::iconNameForUrl(m_url));
144 } else {
145 m_iconButton->setIcon(m_icon);
146 }
147
148 if (m_allowGlobal) {
149 QString appName;
150 if (KGlobal::mainComponent().aboutData()) {
151 appName = KGlobal::mainComponent().aboutData()->programName();
152 }
153 if (appName.isEmpty()) {
154 appName = KGlobal::mainComponent().componentName();
155 }
156 m_appLocal = new QCheckBox( i18n("&Only show when using this application (%1)", appName ), mainWidget );
157 m_appLocal->setChecked(false);
158 vBox->addWidget(m_appLocal);
159 }
160
161 if (m_text.isEmpty()) {
162 m_urlEdit->setFocus();
163 } else {
164 m_textEdit->setFocus();
165 }
166
167 setMainWidget(mainWidget);
168 }
169
170 #include "placesitemeditdialog.moc"