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