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