]> cloud.milkyroute.net Git - dolphin.git/blob - src/settings/previewssettingspage.cpp
Use the dock widgets actions directly, as they are capable of showing the toggle...
[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 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 QLabel* localFileSizeLabel = new QLabel(i18nc("@label", "Local file size maximum:"), this);
70
71 m_localFileSizeBox = new KIntSpinBox(this);
72 m_localFileSizeBox->setSingleStep(1);
73 m_localFileSizeBox->setSuffix(QLatin1String(" MB"));
74 m_localFileSizeBox->setRange(0, 9999); /* MB */
75 connect(m_localFileSizeBox, SIGNAL(valueChanged(int)),
76 this, SIGNAL(changed()));
77
78 QLabel* remoteFileSizeLabel = new QLabel(i18nc("@label", "Remote file size maximum:"), this);
79 m_remoteFileSizeBox = new KIntSpinBox(this);
80 m_remoteFileSizeBox->setSingleStep(1);
81 m_remoteFileSizeBox->setSuffix(QLatin1String(" MB"));
82 m_remoteFileSizeBox->setRange(0, 9999); /* MB */
83 connect(m_remoteFileSizeBox, SIGNAL(valueChanged(int)),
84 this, SIGNAL(changed()));
85
86 QGridLayout* gridLayout = new QGridLayout();
87 gridLayout->addWidget(localFileSizeLabel, 0, 0);
88 gridLayout->addWidget(m_localFileSizeBox, 0, 1);
89 gridLayout->addWidget(remoteFileSizeLabel, 1, 0);
90 gridLayout->addWidget(m_remoteFileSizeBox, 1, 1);
91
92 topLayout->addWidget(listDescription);
93 topLayout->addWidget(m_previewPluginsList);
94 topLayout->addLayout(gridLayout);
95
96 loadSettings();
97 }
98
99
100 PreviewsSettingsPage::~PreviewsSettingsPage()
101 {
102 }
103
104 void PreviewsSettingsPage::applySettings()
105 {
106 m_enabledPreviewPlugins.clear();
107 const int count = m_previewPluginsList->count();
108 for (int i = 0; i < count; ++i) {
109 const QListWidgetItem* item = m_previewPluginsList->item(i);
110 if (item->checkState() == Qt::Checked) {
111 const QString enabledPlugin = item->data(Qt::UserRole).toString();
112 m_enabledPreviewPlugins.append(enabledPlugin);
113 }
114 }
115
116 KConfigGroup globalConfig(KGlobal::config(), QLatin1String("PreviewSettings"));
117 globalConfig.writeEntry("Plugins", m_enabledPreviewPlugins);
118
119 globalConfig.writeEntry("MaximumSize",
120 m_localFileSizeBox->value() * 1024 * 1024,
121 KConfigBase::Normal | KConfigBase::Global);
122 globalConfig.writeEntry("RemoteMaximumSize",
123 m_remoteFileSizeBox->value() * 1024 * 1024,
124 KConfigBase::Normal | KConfigBase::Global);
125 globalConfig.sync();
126 }
127
128 void PreviewsSettingsPage::restoreDefaults()
129 {
130 m_localFileSizeBox->setValue(MaxLocalPreviewSize);
131 m_remoteFileSizeBox->setValue(MaxRemotePreviewSize);
132 }
133
134 void PreviewsSettingsPage::showEvent(QShowEvent* event)
135 {
136 if (!event->spontaneous() && !m_initialized) {
137 QMetaObject::invokeMethod(this, "loadPreviewPlugins", Qt::QueuedConnection);
138 m_initialized = true;
139 }
140 SettingsPageBase::showEvent(event);
141 }
142
143 void PreviewsSettingsPage::loadPreviewPlugins()
144 {
145 const KService::List plugins = KServiceTypeTrader::self()->query(QLatin1String("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
155 void PreviewsSettingsPage::loadSettings()
156 {
157 KConfigGroup globalConfig(KGlobal::config(), "PreviewSettings");
158 m_enabledPreviewPlugins = globalConfig.readEntry("Plugins", QStringList()
159 << QLatin1String("directorythumbnail")
160 << QLatin1String("imagethumbnail")
161 << QLatin1String("jpegthumbnail"));
162
163 // TODO: The default value of 5 MB must match with the default value inside
164 // kdelibs/kio/kio/previewjob.cpp. Maybe a static getter method in PreviewJob
165 // should be added for getting the default size?
166 const int maxLocalByteSize = globalConfig.readEntry("MaximumSize", MaxLocalPreviewSize * 1024 * 1024);
167 const int maxLocalMByteSize = maxLocalByteSize / (1024 * 1024);
168 m_localFileSizeBox->setValue(maxLocalMByteSize);
169
170 const int maxRemoteByteSize = globalConfig.readEntry("MaximumSize", MaxRemotePreviewSize * 1024 * 1024);
171 const int maxRemoteMByteSize = maxRemoteByteSize / (1024 * 1024);
172 m_localFileSizeBox->setValue(maxRemoteMByteSize);
173 }
174
175 #include "previewssettingspage.moc"