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