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