]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/places/placesitemeditdialog.cpp
Remove the automoc noise
[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 <k4aboutdata.h>
28 #include <KComponentData>
29 #include <KDebug>
30 #include <KFile>
31 #include <KGlobal>
32 #include <KIconButton>
33 #include <KLineEdit>
34 #include <KLocale>
35 #include <KMimeType>
36 #include <KUrlRequester>
37 #include <QCheckBox>
38 #include <QEvent>
39 #include <QFormLayout>
40 #include <QVBoxLayout>
41
42 PlacesItemEditDialog::PlacesItemEditDialog(QWidget* parent) :
43 KDialog(parent),
44 m_icon(),
45 m_text(),
46 m_url(),
47 m_allowGlobal(false),
48 m_urlEdit(0),
49 m_textEdit(0),
50 m_iconButton(0),
51 m_appLocal(0)
52 {
53 setButtons( Ok | Cancel );
54 setModal(true);
55 setDefaultButton(Ok);
56 }
57
58 void PlacesItemEditDialog::setIcon(const QString& icon)
59 {
60 m_icon = icon;
61 }
62
63 QString PlacesItemEditDialog::icon() const
64 {
65 return m_iconButton->icon();
66 }
67
68 void PlacesItemEditDialog::setText(const QString& text)
69 {
70 m_text = text;
71 }
72
73 QString PlacesItemEditDialog::text() const
74 {
75 QString text = m_textEdit->text();
76 if (text.isEmpty()) {
77 const KUrl url = m_urlEdit->url();
78 text = url.fileName().isEmpty() ? url.prettyUrl() : url.fileName();
79 }
80 return text;
81 }
82
83 void PlacesItemEditDialog::setUrl(const KUrl& url)
84 {
85 m_url = url;
86 }
87
88 KUrl PlacesItemEditDialog::url() const
89 {
90 return m_urlEdit->url();
91 }
92
93 void PlacesItemEditDialog::setAllowGlobal(bool allow)
94 {
95 m_allowGlobal = allow;
96 }
97
98 bool PlacesItemEditDialog::allowGlobal() const
99 {
100 return m_allowGlobal;
101 }
102
103 bool PlacesItemEditDialog::event(QEvent* event)
104 {
105 if (event->type() == QEvent::Polish) {
106 initialize();
107 }
108 return QWidget::event(event);
109 }
110
111 void PlacesItemEditDialog::slotUrlChanged(const QString& text)
112 {
113 enableButtonOk(!text.isEmpty());
114 }
115
116 PlacesItemEditDialog::~PlacesItemEditDialog()
117 {
118 }
119
120 void PlacesItemEditDialog::initialize()
121 {
122 QWidget* mainWidget = new QWidget(this);
123 QVBoxLayout* vBox = new QVBoxLayout(mainWidget);
124
125 QFormLayout* formLayout = new QFormLayout();
126 vBox->addLayout( formLayout );
127
128 m_textEdit = new KLineEdit(mainWidget);
129 formLayout->addRow(i18nc("@label", "Label:"), m_textEdit);
130 m_textEdit->setText(m_text);
131 m_textEdit->setClickMessage(i18n("Enter descriptive label here"));
132
133 m_urlEdit = new KUrlRequester(m_url.prettyUrl(), mainWidget);
134 m_urlEdit->setMode(KFile::Directory);
135 formLayout->addRow(i18nc("@label", "Location:"), m_urlEdit);
136 // Provide room for at least 40 chars (average char width is half of height)
137 m_urlEdit->setMinimumWidth(m_urlEdit->fontMetrics().height() * (40 / 2));
138 connect(m_urlEdit->lineEdit(), &KLineEdit::textChanged, this, &PlacesItemEditDialog::slotUrlChanged);
139
140 m_iconButton = new KIconButton(mainWidget);
141 formLayout->addRow(i18nc("@label", "Choose an icon:"), m_iconButton);
142 m_iconButton->setIconSize(IconSize(KIconLoader::Desktop));
143 m_iconButton->setIconType(KIconLoader::NoGroup, KIconLoader::Place);
144 if (m_icon.isEmpty()) {
145 m_iconButton->setIcon(KMimeType::iconNameForUrl(m_url));
146 } else {
147 m_iconButton->setIcon(m_icon);
148 }
149
150 if (m_allowGlobal) {
151 QString appName;
152 if (KGlobal::mainComponent().aboutData()) {
153 appName = KGlobal::mainComponent().aboutData()->programName();
154 }
155 if (appName.isEmpty()) {
156 appName = KGlobal::mainComponent().componentName();
157 }
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 setMainWidget(mainWidget);
170 }
171