]> cloud.milkyroute.net Git - dolphin.git/blob - src/settings/general/previewssettingspage.cpp
1059e854770bcd3413137e012464199ef0fa9e23
[dolphin.git] / src / settings / general / 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
22 #include "dolphin_generalsettings.h"
23
24 #include <kconfiggroup.h>
25 #include <kdialog.h>
26 #include <kglobal.h>
27 #include <klocale.h>
28 #include <KNumInput>
29 #include <kservicetypetrader.h>
30 #include <kservice.h>
31
32 #include <settings/dolphinsettings.h>
33
34 #include <QCheckBox>
35 #include <QGroupBox>
36 #include <QLabel>
37 #include <QListWidget>
38 #include <QRadioButton>
39 #include <QShowEvent>
40 #include <QSlider>
41 #include <QGridLayout>
42
43 // default settings
44 namespace {
45 const bool UseThumbnails = true;
46 const int MaxLocalPreviewSize = 5; // 5 MB
47 const int MaxRemotePreviewSize = 0; // 0 MB
48 }
49
50 PreviewsSettingsPage::PreviewsSettingsPage(QWidget* parent) :
51 SettingsPageBase(parent),
52 m_initialized(false),
53 m_previewPluginsList(0),
54 m_enabledPreviewPlugins(),
55 m_localFileSizeBox(0),
56 m_remoteFileSizeBox(0)
57 {
58 QVBoxLayout* topLayout = new QVBoxLayout(this);
59 topLayout->setSpacing(KDialog::spacingHint());
60 topLayout->setMargin(KDialog::marginHint());
61
62 // Create group box "Show previews for:"
63 QGroupBox* listBox = new QGroupBox(i18nc("@title:group", "Show previews for"), this);
64
65 m_previewPluginsList = new QListWidget(this);
66 m_previewPluginsList->setSortingEnabled(true);
67 m_previewPluginsList->setSelectionMode(QAbstractItemView::NoSelection);
68 connect(m_previewPluginsList, SIGNAL(itemClicked(QListWidgetItem*)),
69 this, SIGNAL(changed()));
70
71 QVBoxLayout* listBoxLayout = new QVBoxLayout(listBox);
72 listBoxLayout->addWidget(m_previewPluginsList);
73
74 // Create group box "Don't create previews for"
75 QGroupBox* fileSizeBox = new QGroupBox(i18nc("@title:group", "Do not create previews for"), this);
76
77 QLabel* localFileSizeLabel = new QLabel(i18nc("@label Don't create previews for: <Local files above:> XX MByte",
78 "Local files above:"), this);
79
80 m_localFileSizeBox = new KIntSpinBox(this);
81 m_localFileSizeBox->setSingleStep(1);
82 m_localFileSizeBox->setSuffix(QLatin1String(" MB"));
83 m_localFileSizeBox->setRange(0, 9999); /* MB */
84 connect(m_localFileSizeBox, SIGNAL(valueChanged(int)),
85 this, SIGNAL(changed()));
86
87 QLabel* remoteFileSizeLabel = new QLabel(i18nc("@label Don't create previews for: <Remote files above:> XX MByte",
88 "Remote files above:"), this);
89
90 m_remoteFileSizeBox = new KIntSpinBox(this);
91 m_remoteFileSizeBox->setSingleStep(1);
92 m_remoteFileSizeBox->setSuffix(QLatin1String(" MB"));
93 m_remoteFileSizeBox->setRange(0, 9999); /* MB */
94 connect(m_remoteFileSizeBox, SIGNAL(valueChanged(int)),
95 this, SIGNAL(changed()));
96
97 QGridLayout* fileSizeBoxLayout = new QGridLayout(fileSizeBox);
98 fileSizeBoxLayout->addWidget(localFileSizeLabel, 0, 0, Qt::AlignRight);
99 fileSizeBoxLayout->addWidget(m_localFileSizeBox, 0, 1);
100 fileSizeBoxLayout->addWidget(remoteFileSizeLabel, 1, 0, Qt::AlignRight);
101 fileSizeBoxLayout->addWidget(m_remoteFileSizeBox, 1, 1);
102
103 topLayout->addWidget(listBox);
104 topLayout->addWidget(fileSizeBox);
105
106 loadSettings();
107 }
108
109
110 PreviewsSettingsPage::~PreviewsSettingsPage()
111 {
112 }
113
114 void PreviewsSettingsPage::applySettings()
115 {
116 m_enabledPreviewPlugins.clear();
117 const int count = m_previewPluginsList->count();
118 for (int i = 0; i < count; ++i) {
119 const QListWidgetItem* item = m_previewPluginsList->item(i);
120 if (item->checkState() == Qt::Checked) {
121 const QString enabledPlugin = item->data(Qt::UserRole).toString();
122 m_enabledPreviewPlugins.append(enabledPlugin);
123 }
124 }
125
126 KConfigGroup globalConfig(KGlobal::config(), QLatin1String("PreviewSettings"));
127 globalConfig.writeEntry("Plugins", m_enabledPreviewPlugins);
128
129 globalConfig.writeEntry("MaximumSize",
130 m_localFileSizeBox->value() * 1024 * 1024,
131 KConfigBase::Normal | KConfigBase::Global);
132 globalConfig.writeEntry("MaximumRemoteSize",
133 m_remoteFileSizeBox->value() * 1024 * 1024,
134 KConfigBase::Normal | KConfigBase::Global);
135 globalConfig.sync();
136 }
137
138 void PreviewsSettingsPage::restoreDefaults()
139 {
140 m_localFileSizeBox->setValue(MaxLocalPreviewSize);
141 m_remoteFileSizeBox->setValue(MaxRemotePreviewSize);
142 }
143
144 void PreviewsSettingsPage::showEvent(QShowEvent* event)
145 {
146 if (!event->spontaneous() && !m_initialized) {
147 QMetaObject::invokeMethod(this, "loadPreviewPlugins", Qt::QueuedConnection);
148 m_initialized = true;
149 }
150 SettingsPageBase::showEvent(event);
151 }
152
153 void PreviewsSettingsPage::loadPreviewPlugins()
154 {
155 const KService::List plugins = KServiceTypeTrader::self()->query(QLatin1String("ThumbCreator"));
156 foreach (const KSharedPtr<KService>& service, plugins) {
157 QListWidgetItem* item = new QListWidgetItem(service->name(),
158 m_previewPluginsList);
159 item->setData(Qt::UserRole, service->desktopEntryName());
160 const bool show = m_enabledPreviewPlugins.contains(service->desktopEntryName());
161 item->setCheckState(show ? Qt::Checked : Qt::Unchecked);
162 }
163 }
164
165 void PreviewsSettingsPage::loadSettings()
166 {
167 KConfigGroup globalConfig(KGlobal::config(), "PreviewSettings");
168 m_enabledPreviewPlugins = globalConfig.readEntry("Plugins", QStringList()
169 << QLatin1String("directorythumbnail")
170 << QLatin1String("imagethumbnail")
171 << QLatin1String("jpegthumbnail"));
172
173 const int maxLocalByteSize = globalConfig.readEntry("MaximumSize", MaxLocalPreviewSize * 1024 * 1024);
174 const int maxLocalMByteSize = maxLocalByteSize / (1024 * 1024);
175 m_localFileSizeBox->setValue(maxLocalMByteSize);
176
177 const int maxRemoteByteSize = globalConfig.readEntry("MaximumRemoteSize", MaxRemotePreviewSize * 1024 * 1024);
178 const int maxRemoteMByteSize = maxRemoteByteSize / (1024 * 1024);
179 m_remoteFileSizeBox->setValue(maxRemoteMByteSize);
180 }
181
182 #include "previewssettingspage.moc"