]> cloud.milkyroute.net Git - dolphin.git/blob - src/settings/previewssettingspage.cpp
Use the member variable
[dolphin.git] / src / settings / previewssettingspage.cpp
1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz <peter.penz@gmx.at> *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
19
20 #include "previewssettingspage.h"
21 #include "dolphinsettings.h"
22
23 #include "dolphin_generalsettings.h"
24
25 #include <kconfiggroup.h>
26 #include <kdialog.h>
27 #include <kglobal.h>
28 #include <klocale.h>
29 #include <KNumInput>
30 #include <kservicetypetrader.h>
31 #include <kservice.h>
32
33 #include <QCheckBox>
34 #include <QGroupBox>
35 #include <QLabel>
36 #include <QListWidget>
37 #include <QRadioButton>
38 #include <QShowEvent>
39 #include <QSlider>
40 #include <QGridLayout>
41
42 // default settings
43 namespace {
44 const bool UseThumbnails = true;
45 const int MaxLocalPreviewSize = 5; // 5 MB
46 const int MaxRemotePreviewSize = 0; // 0 MB
47 }
48
49 PreviewsSettingsPage::PreviewsSettingsPage(QWidget* parent) :
50 SettingsPageBase(parent),
51 m_initialized(false),
52 m_previewPluginsList(0),
53 m_enabledPreviewPlugins(),
54 m_localFileSizeBox(0),
55 m_remoteFileSizeBox(0)
56 {
57 QVBoxLayout* topLayout = new QVBoxLayout(this);
58 topLayout->setSpacing(KDialog::spacingHint());
59 topLayout->setMargin(KDialog::marginHint());
60
61 // Create group box "Show previews for:"
62 QGroupBox* listBox = new QGroupBox(i18nc("@title:group", "Show previews for"), this);
63
64 m_previewPluginsList = new QListWidget(this);
65 m_previewPluginsList->setSortingEnabled(true);
66 m_previewPluginsList->setSelectionMode(QAbstractItemView::NoSelection);
67 connect(m_previewPluginsList, SIGNAL(itemClicked(QListWidgetItem*)),
68 this, SIGNAL(changed()));
69
70 QVBoxLayout* listBoxLayout = new QVBoxLayout(listBox);
71 listBoxLayout->addWidget(m_previewPluginsList);
72
73 // Create group box "Don't create previews for"
74 QGroupBox* fileSizeBox = new QGroupBox(i18nc("@title:group", "Do not create previews for"), this);
75
76 QLabel* localFileSizeLabel = new QLabel(i18nc("@label Don't create previews for: <Local files above:> XX MByte",
77 "Local files above:"), this);
78
79 m_localFileSizeBox = new KIntSpinBox(this);
80 m_localFileSizeBox->setSingleStep(1);
81 m_localFileSizeBox->setSuffix(QLatin1String(" MB"));
82 m_localFileSizeBox->setRange(0, 9999); /* MB */
83 connect(m_localFileSizeBox, SIGNAL(valueChanged(int)),
84 this, SIGNAL(changed()));
85
86 QLabel* remoteFileSizeLabel = new QLabel(i18nc("@label Don't create previews for: <Remote files above:> XX MByte",
87 "Remote files above:"), this);
88
89 m_remoteFileSizeBox = new KIntSpinBox(this);
90 m_remoteFileSizeBox->setSingleStep(1);
91 m_remoteFileSizeBox->setSuffix(QLatin1String(" MB"));
92 m_remoteFileSizeBox->setRange(0, 9999); /* MB */
93 connect(m_remoteFileSizeBox, SIGNAL(valueChanged(int)),
94 this, SIGNAL(changed()));
95
96 QGridLayout* fileSizeBoxLayout = new QGridLayout(fileSizeBox);
97 fileSizeBoxLayout->addWidget(localFileSizeLabel, 0, 0, Qt::AlignRight);
98 fileSizeBoxLayout->addWidget(m_localFileSizeBox, 0, 1);
99 fileSizeBoxLayout->addWidget(remoteFileSizeLabel, 1, 0, Qt::AlignRight);
100 fileSizeBoxLayout->addWidget(m_remoteFileSizeBox, 1, 1);
101
102 topLayout->addWidget(listBox);
103 topLayout->addWidget(fileSizeBox);
104
105 loadSettings();
106 }
107
108
109 PreviewsSettingsPage::~PreviewsSettingsPage()
110 {
111 }
112
113 void PreviewsSettingsPage::applySettings()
114 {
115 m_enabledPreviewPlugins.clear();
116 const int count = m_previewPluginsList->count();
117 for (int i = 0; i < count; ++i) {
118 const QListWidgetItem* item = m_previewPluginsList->item(i);
119 if (item->checkState() == Qt::Checked) {
120 const QString enabledPlugin = item->data(Qt::UserRole).toString();
121 m_enabledPreviewPlugins.append(enabledPlugin);
122 }
123 }
124
125 KConfigGroup globalConfig(KGlobal::config(), QLatin1String("PreviewSettings"));
126 globalConfig.writeEntry("Plugins", m_enabledPreviewPlugins);
127
128 globalConfig.writeEntry("MaximumSize",
129 m_localFileSizeBox->value() * 1024 * 1024,
130 KConfigBase::Normal | KConfigBase::Global);
131 globalConfig.writeEntry("MaximumRemoteSize",
132 m_remoteFileSizeBox->value() * 1024 * 1024,
133 KConfigBase::Normal | KConfigBase::Global);
134 globalConfig.sync();
135 }
136
137 void PreviewsSettingsPage::restoreDefaults()
138 {
139 m_localFileSizeBox->setValue(MaxLocalPreviewSize);
140 m_remoteFileSizeBox->setValue(MaxRemotePreviewSize);
141 }
142
143 void PreviewsSettingsPage::showEvent(QShowEvent* event)
144 {
145 if (!event->spontaneous() && !m_initialized) {
146 QMetaObject::invokeMethod(this, "loadPreviewPlugins", Qt::QueuedConnection);
147 m_initialized = true;
148 }
149 SettingsPageBase::showEvent(event);
150 }
151
152 void PreviewsSettingsPage::loadPreviewPlugins()
153 {
154 const KService::List plugins = KServiceTypeTrader::self()->query(QLatin1String("ThumbCreator"));
155 foreach (const KSharedPtr<KService>& service, plugins) {
156 QListWidgetItem* item = new QListWidgetItem(service->name(),
157 m_previewPluginsList);
158 item->setData(Qt::UserRole, service->desktopEntryName());
159 const bool show = m_enabledPreviewPlugins.contains(service->desktopEntryName());
160 item->setCheckState(show ? Qt::Checked : Qt::Unchecked);
161 }
162 }
163
164 void PreviewsSettingsPage::loadSettings()
165 {
166 KConfigGroup globalConfig(KGlobal::config(), "PreviewSettings");
167 m_enabledPreviewPlugins = globalConfig.readEntry("Plugins", QStringList()
168 << QLatin1String("directorythumbnail")
169 << QLatin1String("imagethumbnail")
170 << QLatin1String("jpegthumbnail"));
171
172 const int maxLocalByteSize = globalConfig.readEntry("MaximumSize", MaxLocalPreviewSize * 1024 * 1024);
173 const int maxLocalMByteSize = maxLocalByteSize / (1024 * 1024);
174 m_localFileSizeBox->setValue(maxLocalMByteSize);
175
176 const int maxRemoteByteSize = globalConfig.readEntry("MaximumRemoteSize", MaxRemotePreviewSize * 1024 * 1024);
177 const int maxRemoteMByteSize = maxRemoteByteSize / (1024 * 1024);
178 m_remoteFileSizeBox->setValue(maxRemoteMByteSize);
179 }
180
181 #include "previewssettingspage.moc"