]> cloud.milkyroute.net Git - dolphin.git/blob - src/settings/previewssettingspage.cpp
use KNewStuff3 instead of the deprecated KNewStuff2
[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 <QCheckBox>
26 #include <QEvent>
27 #include <QGroupBox>
28 #include <QLabel>
29 #include <QListWidget>
30 #include <QRadioButton>
31 #include <QSlider>
32 #include <QBoxLayout>
33
34 #include <kconfiggroup.h>
35 #include <kdialog.h>
36 #include <kglobal.h>
37 #include <khbox.h>
38 #include <klocale.h>
39 #include <KNumInput>
40 #include <kservicetypetrader.h>
41 #include <kservice.h>
42 #include <kvbox.h>
43
44 // default settings
45 const bool USE_THUMBNAILS = true;
46 const int MAX_PREVIEW_SIZE = 5; // 5 MB
47
48 PreviewsSettingsPage::PreviewsSettingsPage(QWidget* parent) :
49 SettingsPageBase(parent),
50 m_initialized(false),
51 m_previewPluginsList(0),
52 m_enabledPreviewPlugins(),
53 m_maxPreviewSize(0),
54 m_spinBox(0),
55 m_useFileThumbnails(0)
56 {
57 QVBoxLayout* topLayout = new QVBoxLayout(this);
58 topLayout->setSpacing(KDialog::spacingHint());
59 topLayout->setMargin(KDialog::marginHint());
60
61 QLabel* listDescription = new QLabel(i18nc("@label", "Show previews for:"), this);
62
63 m_previewPluginsList = new QListWidget(this);
64 m_previewPluginsList->setSortingEnabled(true);
65 m_previewPluginsList->setSelectionMode(QAbstractItemView::NoSelection);
66 connect(m_previewPluginsList, SIGNAL(itemClicked(QListWidgetItem*)),
67 this, SIGNAL(changed()));
68
69 KHBox* hBox = new KHBox(this);
70 hBox->setSpacing(KDialog::spacingHint());
71
72 new QLabel(i18nc("@label:slider", "Maximum file size:"), hBox);
73 m_maxPreviewSize = new QSlider(Qt::Horizontal, hBox);
74 m_maxPreviewSize->setPageStep(10);
75 m_maxPreviewSize->setSingleStep(1);
76 m_maxPreviewSize->setTickPosition(QSlider::TicksBelow);
77 m_maxPreviewSize->setRange(1, 100); /* MB */
78
79 m_spinBox = new KIntSpinBox(hBox);
80 m_spinBox->setSingleStep(1);
81 m_spinBox->setSuffix(" MB");
82 m_spinBox->setRange(1, 100); /* MB */
83
84 connect(m_maxPreviewSize, SIGNAL(valueChanged(int)),
85 m_spinBox, SLOT(setValue(int)));
86 connect(m_spinBox, SIGNAL(valueChanged(int)),
87 m_maxPreviewSize, SLOT(setValue(int)));
88
89 connect(m_maxPreviewSize, SIGNAL(valueChanged(int)),
90 this, SIGNAL(changed()));
91 connect(m_spinBox, SIGNAL(valueChanged(int)),
92 this, SIGNAL(changed()));
93
94 m_useFileThumbnails = new QCheckBox(i18nc("@option:check", "Use thumbnails embedded in files"), this);
95 connect(m_useFileThumbnails, SIGNAL(toggled(bool)), this, SIGNAL(changed()));
96
97 topLayout->addWidget(listDescription);
98 topLayout->addWidget(m_previewPluginsList);
99 topLayout->addWidget(hBox);
100 topLayout->addWidget(m_useFileThumbnails);
101
102 loadSettings();
103 }
104
105
106 PreviewsSettingsPage::~PreviewsSettingsPage()
107 {
108 }
109
110 void PreviewsSettingsPage::applySettings()
111 {
112 m_enabledPreviewPlugins.clear();
113 const int count = m_previewPluginsList->count();
114 for (int i = 0; i < count; ++i) {
115 const QListWidgetItem* item = m_previewPluginsList->item(i);
116 if (item->checkState() == Qt::Checked) {
117 const QString enabledPlugin = item->data(Qt::UserRole).toString();
118 m_enabledPreviewPlugins.append(enabledPlugin);
119 }
120 }
121
122 KConfigGroup globalConfig(KGlobal::config(), "PreviewSettings");
123 globalConfig.writeEntry("Plugins", m_enabledPreviewPlugins);
124
125 const int byteCount = m_maxPreviewSize->value() * 1024 * 1024; // value() returns size in MB
126 globalConfig.writeEntry("MaximumSize",
127 byteCount,
128 KConfigBase::Normal | KConfigBase::Global);
129 globalConfig.writeEntry("UseFileThumbnails",
130 m_useFileThumbnails->isChecked(),
131 KConfigBase::Normal | KConfigBase::Global);
132 globalConfig.sync();
133 }
134
135 void PreviewsSettingsPage::restoreDefaults()
136 {
137 m_maxPreviewSize->setValue(MAX_PREVIEW_SIZE);
138 m_useFileThumbnails->setChecked(USE_THUMBNAILS);
139 }
140
141 bool PreviewsSettingsPage::event(QEvent* event)
142 {
143 if ((event->type() == QEvent::Polish) && !m_initialized) {
144 // load all available plugins for previews
145 const KService::List plugins = KServiceTypeTrader::self()->query("ThumbCreator");
146 foreach (const KSharedPtr<KService>& service, plugins) {
147 QListWidgetItem* item = new QListWidgetItem(service->name(),
148 m_previewPluginsList);
149 item->setData(Qt::UserRole, service->desktopEntryName());
150 const bool show = m_enabledPreviewPlugins.contains(service->desktopEntryName());
151 item->setCheckState(show ? Qt::Checked : Qt::Unchecked);
152 }
153
154 m_initialized = true;
155 }
156 return SettingsPageBase::event(event);
157 }
158
159 void PreviewsSettingsPage::loadSettings()
160 {
161 KConfigGroup globalConfig(KGlobal::config(), "PreviewSettings");
162 m_enabledPreviewPlugins = globalConfig.readEntry("Plugins", QStringList()
163 << "directorythumbnail"
164 << "imagethumbnail"
165 << "jpegthumbnail");
166
167 // TODO: The default value of 5 MB must match with the default value inside
168 // kdelibs/kio/kio/previewjob.cpp. Maybe a static getter method in PreviewJob
169 // should be added for getting the default size?
170 const int min = 1; // MB
171 const int max = 100; // MB
172
173 const int maxByteSize = globalConfig.readEntry("MaximumSize", MAX_PREVIEW_SIZE * 1024 * 1024);
174 int maxMByteSize = maxByteSize / (1024 * 1024);
175 if (maxMByteSize < min) {
176 maxMByteSize = min;
177 } else if (maxMByteSize > max) {
178 maxMByteSize = max;
179 }
180 m_maxPreviewSize->setValue(maxMByteSize);
181
182 const bool useFileThumbnails = globalConfig.readEntry("UseFileThumbnails", USE_THUMBNAILS);
183 m_useFileThumbnails->setChecked(useFileThumbnails);
184 }
185
186 #include "previewssettingspage.moc"