]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/places/placesitemeditdialog.cpp
Replace kDebug/kWarning by categorized logging (org.kde.dolphin)
[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 <KComponentData>
34 #include <QCheckBox>
35 #include <QEvent>
36 #include <QFormLayout>
37 #include <QVBoxLayout>
38 #include <QDialogButtonBox>
39 #include <QPushButton>
40 #include <QLineEdit>
41
42 PlacesItemEditDialog::PlacesItemEditDialog(QWidget* parent) :
43 QDialog(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 }
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 QString text = m_textEdit->text();
73 if (text.isEmpty()) {
74 const QUrl url = m_urlEdit->url();
75 text = url.fileName().isEmpty() ? url.toDisplayString(QUrl::PreferLocalFile) : url.fileName();
76 }
77 return text;
78 }
79
80 void PlacesItemEditDialog::setUrl(const QUrl& url)
81 {
82 m_url = url;
83 }
84
85 QUrl PlacesItemEditDialog::url() const
86 {
87 return m_urlEdit->url();
88 }
89
90 void PlacesItemEditDialog::setAllowGlobal(bool allow)
91 {
92 m_allowGlobal = allow;
93 }
94
95 bool PlacesItemEditDialog::allowGlobal() const
96 {
97 return m_allowGlobal;
98 }
99
100 bool PlacesItemEditDialog::event(QEvent* event)
101 {
102 if (event->type() == QEvent::Polish) {
103 initialize();
104 }
105 return QWidget::event(event);
106 }
107
108 void PlacesItemEditDialog::slotUrlChanged(const QString& text)
109 {
110 m_okButton->setEnabled(!text.isEmpty());
111 }
112
113 PlacesItemEditDialog::~PlacesItemEditDialog()
114 {
115 }
116
117 void PlacesItemEditDialog::initialize()
118 {
119 QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok|QDialogButtonBox::Cancel);
120 m_okButton = buttonBox->button(QDialogButtonBox::Ok);
121 m_okButton->setDefault(true);
122 m_okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
123 connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
124 connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
125 setModal(true);
126 m_okButton->setDefault(true);
127
128 QVBoxLayout *mainLayout = new QVBoxLayout;
129 setLayout(mainLayout);
130 QWidget* mainWidget = new QWidget(this);
131 mainLayout->addWidget(mainWidget);
132 mainLayout->addWidget(buttonBox);
133
134 QVBoxLayout* vBox = new QVBoxLayout(mainWidget);
135
136 QFormLayout* formLayout = new QFormLayout();
137 vBox->addLayout( formLayout );
138
139 m_textEdit = new QLineEdit(mainWidget);
140 formLayout->addRow(i18nc("@label", "Label:"), m_textEdit);
141 m_textEdit->setText(m_text);
142 m_textEdit->setPlaceholderText(i18n("Enter descriptive label here"));
143
144 m_urlEdit = new KUrlRequester(m_url, mainWidget);
145 m_urlEdit->setMode(KFile::Directory);
146 formLayout->addRow(i18nc("@label", "Location:"), m_urlEdit);
147 // Provide room for at least 40 chars (average char width is half of height)
148 m_urlEdit->setMinimumWidth(m_urlEdit->fontMetrics().height() * (40 / 2));
149 connect(m_urlEdit, &KUrlRequester::textChanged, this, &PlacesItemEditDialog::slotUrlChanged);
150
151 m_iconButton = new KIconButton(mainWidget);
152 formLayout->addRow(i18nc("@label", "Choose an icon:"), m_iconButton);
153 m_iconButton->setIconSize(IconSize(KIconLoader::Desktop));
154 m_iconButton->setIconType(KIconLoader::NoGroup, KIconLoader::Place);
155 if (m_icon.isEmpty()) {
156 QMimeDatabase db;
157 m_iconButton->setIcon(db.mimeTypeForUrl(m_url).iconName());
158 } else {
159 m_iconButton->setIcon(m_icon);
160 }
161
162 if (m_allowGlobal) {
163 const QString appName = KAboutData::applicationData().displayName();
164 m_appLocal = new QCheckBox( i18n("&Only show when using this application (%1)", appName ), mainWidget );
165 m_appLocal->setChecked(false);
166 vBox->addWidget(m_appLocal);
167 }
168
169 if (m_text.isEmpty()) {
170 m_urlEdit->setFocus();
171 } else {
172 m_textEdit->setFocus();
173 }
174
175 }
176