]> cloud.milkyroute.net Git - dolphin.git/blob - src/settings/general/previewssettingspage.cpp
431722b62f824196c865daa3603e014e07f809e1
[dolphin.git] / src / settings / general / previewssettingspage.cpp
1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz <peter.penz19@gmail.com> *
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 #include "configurepreviewplugindialog.h"
24
25 #include <KConfigGroup>
26 #include <KGlobal>
27 #include <KLocalizedString>
28 #include <KServiceTypeTrader>
29 #include <KService>
30
31 #include <settings/serviceitemdelegate.h>
32 #include <settings/servicemodel.h>
33
34 #include <QCheckBox>
35 #include <QGroupBox>
36 #include <QHBoxLayout>
37 #include <QLabel>
38 #include <QListView>
39 #include <QPainter>
40 #include <QScrollBar>
41 #include <QShowEvent>
42 #include <QSlider>
43 #include <QSpinBox>
44 #include <QSortFilterProxyModel>
45 #include <QVBoxLayout>
46
47 // default settings
48 namespace {
49 const bool UseThumbnails = true;
50 const int MaxRemotePreviewSize = 0; // 0 MB
51 }
52
53 PreviewsSettingsPage::PreviewsSettingsPage(QWidget* parent) :
54 SettingsPageBase(parent),
55 m_initialized(false),
56 m_listView(0),
57 m_enabledPreviewPlugins(),
58 m_remoteFileSizeBox(0)
59 {
60 QVBoxLayout* topLayout = new QVBoxLayout(this);
61
62 QLabel* showPreviewsLabel = new QLabel(i18nc("@title:group", "Show previews for:"), this);
63
64 m_listView = new QListView(this);
65
66 ServiceItemDelegate* delegate = new ServiceItemDelegate(m_listView, m_listView);
67 connect(delegate, &ServiceItemDelegate::requestServiceConfiguration,
68 this, &PreviewsSettingsPage::configureService);
69
70 ServiceModel* serviceModel = new ServiceModel(this);
71 QSortFilterProxyModel* proxyModel = new QSortFilterProxyModel(this);
72 proxyModel->setSourceModel(serviceModel);
73 proxyModel->setSortRole(Qt::DisplayRole);
74
75 m_listView->setModel(proxyModel);
76 m_listView->setItemDelegate(delegate);
77 m_listView->setVerticalScrollMode(QListView::ScrollPerPixel);
78
79 QLabel* remoteFileSizeLabel = new QLabel(i18nc("@label", "Skip previews for remote files above:"), this);
80
81 m_remoteFileSizeBox = new QSpinBox(this);
82 m_remoteFileSizeBox->setSingleStep(1);
83 m_remoteFileSizeBox->setSuffix(QLatin1String(" MB"));
84 m_remoteFileSizeBox->setRange(0, 9999999); /* MB */
85
86 QHBoxLayout* fileSizeBoxLayout = new QHBoxLayout(this);
87 fileSizeBoxLayout->addWidget(remoteFileSizeLabel, 0, Qt::AlignRight);
88 fileSizeBoxLayout->addWidget(m_remoteFileSizeBox);
89
90 topLayout->addWidget(showPreviewsLabel);
91 topLayout->addWidget(m_listView);
92 topLayout->addLayout(fileSizeBoxLayout);
93
94 loadSettings();
95
96 connect(m_listView, &QListView::clicked, this, &PreviewsSettingsPage::changed);
97 connect(m_remoteFileSizeBox, static_cast<void(QSpinBox::*)(int)>(&QSpinBox::valueChanged), this, &PreviewsSettingsPage::changed);
98 }
99
100 PreviewsSettingsPage::~PreviewsSettingsPage()
101 {
102 }
103
104 void PreviewsSettingsPage::applySettings()
105 {
106 const QAbstractItemModel* model = m_listView->model();
107 const int rowCount = model->rowCount();
108 if (rowCount > 0) {
109 m_enabledPreviewPlugins.clear();
110 for (int i = 0; i < rowCount; ++i) {
111 const QModelIndex index = model->index(i, 0);
112 const bool checked = model->data(index, Qt::CheckStateRole).toBool();
113 if (checked) {
114 const QString enabledPlugin = model->data(index, Qt::UserRole).toString();
115 m_enabledPreviewPlugins.append(enabledPlugin);
116 }
117 }
118 }
119
120 KConfigGroup globalConfig(KSharedConfig::openConfig(), QLatin1String("PreviewSettings"));
121 globalConfig.writeEntry("Plugins", m_enabledPreviewPlugins);
122
123 const qulonglong maximumRemoteSize = static_cast<qulonglong>(m_remoteFileSizeBox->value()) * 1024 * 1024;
124 globalConfig.writeEntry("MaximumRemoteSize",
125 maximumRemoteSize,
126 KConfigBase::Normal | KConfigBase::Global);
127 globalConfig.sync();
128 }
129
130 void PreviewsSettingsPage::restoreDefaults()
131 {
132 m_remoteFileSizeBox->setValue(MaxRemotePreviewSize);
133 }
134
135 void PreviewsSettingsPage::showEvent(QShowEvent* event)
136 {
137 if (!event->spontaneous() && !m_initialized) {
138 loadPreviewPlugins();
139 m_initialized = true;
140 }
141 SettingsPageBase::showEvent(event);
142 }
143
144 void PreviewsSettingsPage::configureService(const QModelIndex& index)
145 {
146 const QAbstractItemModel* model = index.model();
147 const QString pluginName = model->data(index).toString();
148 const QString desktopEntryName = model->data(index, ServiceModel::DesktopEntryNameRole).toString();
149
150 ConfigurePreviewPluginDialog* dialog = new ConfigurePreviewPluginDialog(pluginName, desktopEntryName, this);
151 dialog->setAttribute(Qt::WA_DeleteOnClose);
152 dialog->show();
153 }
154
155 void PreviewsSettingsPage::loadPreviewPlugins()
156 {
157 QAbstractItemModel* model = m_listView->model();
158
159 const KService::List plugins = KServiceTypeTrader::self()->query(QLatin1String("ThumbCreator"));
160 foreach (const KService::Ptr& service, plugins) {
161 const bool configurable = service->property("Configurable", QVariant::Bool).toBool();
162 const bool show = m_enabledPreviewPlugins.contains(service->desktopEntryName());
163
164 model->insertRow(0);
165 const QModelIndex index = model->index(0, 0);
166 model->setData(index, show, Qt::CheckStateRole);
167 model->setData(index, configurable, ServiceModel::ConfigurableRole);
168 model->setData(index, service->name(), Qt::DisplayRole);
169 model->setData(index, service->desktopEntryName(), ServiceModel::DesktopEntryNameRole);
170 }
171
172 model->sort(Qt::DisplayRole);
173 }
174
175 void PreviewsSettingsPage::loadSettings()
176 {
177 KConfigGroup globalConfig(KSharedConfig::openConfig(), "PreviewSettings");
178 m_enabledPreviewPlugins = globalConfig.readEntry("Plugins", QStringList()
179 << QLatin1String("directorythumbnail")
180 << QLatin1String("imagethumbnail")
181 << QLatin1String("jpegthumbnail"));
182
183 const qulonglong defaultRemotePreview = static_cast<qulonglong>(MaxRemotePreviewSize) * 1024 * 1024;
184 const qulonglong maxRemoteByteSize = globalConfig.readEntry("MaximumRemoteSize", defaultRemotePreview);
185 const int maxRemoteMByteSize = maxRemoteByteSize / (1024 * 1024);
186 m_remoteFileSizeBox->setValue(maxRemoteMByteSize);
187 }
188