2 * SPDX-FileCopyrightText: 2006 Peter Penz <peter.penz19@gmail.com>
4 * SPDX-License-Identifier: GPL-2.0-or-later
7 #include "previewssettingspage.h"
9 #include "dolphin_generalsettings.h"
10 #include "configurepreviewplugindialog.h"
11 #include "settings/serviceitemdelegate.h"
12 #include "settings/servicemodel.h"
14 #include <KIO/PreviewJob>
15 #include <KLocalizedString>
16 #include <KPluginMetaData>
18 #include <QHBoxLayout>
23 #include <QSortFilterProxyModel>
28 const int DefaultMaxLocalPreviewSize
= 0; // 0 MB
29 const int DefaultMaxRemotePreviewSize
= 0; // 0 MB
32 PreviewsSettingsPage::PreviewsSettingsPage(QWidget
* parent
) :
33 SettingsPageBase(parent
),
36 m_enabledPreviewPlugins(),
37 m_localFileSizeBox(nullptr),
38 m_remoteFileSizeBox(nullptr)
40 QVBoxLayout
* topLayout
= new QVBoxLayout(this);
42 QLabel
* showPreviewsLabel
= new QLabel(i18nc("@title:group", "Show previews in the view for:"), this);
44 m_listView
= new QListView(this);
45 QScroller::grabGesture(m_listView
->viewport(), QScroller::TouchGesture
);
47 #if KIOWIDGETS_BUILD_DEPRECATED_SINCE(5, 87)
48 ServiceItemDelegate
* delegate
= new ServiceItemDelegate(m_listView
, m_listView
);
49 connect(delegate
, &ServiceItemDelegate::requestServiceConfiguration
,
50 this, &PreviewsSettingsPage::configureService
);
51 m_listView
->setItemDelegate(delegate
);
54 ServiceModel
* serviceModel
= new ServiceModel(this);
55 QSortFilterProxyModel
* proxyModel
= new QSortFilterProxyModel(this);
56 proxyModel
->setSourceModel(serviceModel
);
57 proxyModel
->setSortRole(Qt::DisplayRole
);
58 proxyModel
->setSortCaseSensitivity(Qt::CaseInsensitive
);
60 m_listView
->setModel(proxyModel
);
61 m_listView
->setVerticalScrollMode(QListView::ScrollPerPixel
);
62 m_listView
->setUniformItemSizes(true);
64 QLabel
* localFileSizeLabel
= new QLabel(i18n("Skip previews for local files above:"), this);
66 m_localFileSizeBox
= new QSpinBox(this);
67 m_localFileSizeBox
->setSingleStep(1);
68 m_localFileSizeBox
->setSuffix(i18nc("Mebibytes; used as a suffix in a spinbox showing e.g. '3 MiB'", " MiB"));
69 m_localFileSizeBox
->setRange(0, 9999999); /* MB */
70 m_localFileSizeBox
->setSpecialValueText(i18n("No limit"));
72 QHBoxLayout
* localFileSizeBoxLayout
= new QHBoxLayout();
73 localFileSizeBoxLayout
->addWidget(localFileSizeLabel
);
74 localFileSizeBoxLayout
->addStretch(0);
75 localFileSizeBoxLayout
->addWidget(m_localFileSizeBox
);
77 QLabel
* remoteFileSizeLabel
= new QLabel(i18nc("@label", "Skip previews for remote files above:"), this);
79 m_remoteFileSizeBox
= new QSpinBox(this);
80 m_remoteFileSizeBox
->setSingleStep(1);
81 m_remoteFileSizeBox
->setSuffix(i18nc("Mebibytes; used as a suffix in a spinbox showing e.g. '3 MiB'", " MiB"));
82 m_remoteFileSizeBox
->setRange(0, 9999999); /* MB */
83 m_remoteFileSizeBox
->setSpecialValueText(i18n("No previews"));
85 QHBoxLayout
* remoteFileSizeBoxLayout
= new QHBoxLayout();
86 remoteFileSizeBoxLayout
->addWidget(remoteFileSizeLabel
);
87 remoteFileSizeBoxLayout
->addStretch(0);
88 remoteFileSizeBoxLayout
->addWidget(m_remoteFileSizeBox
);
90 topLayout
->addWidget(showPreviewsLabel
);
91 topLayout
->addWidget(m_listView
);
92 topLayout
->addLayout(localFileSizeBoxLayout
);
93 topLayout
->addLayout(remoteFileSizeBoxLayout
);
97 connect(m_listView
, &QListView::clicked
, this, &PreviewsSettingsPage::changed
);
98 connect(m_localFileSizeBox
, &QSpinBox::valueChanged
, this, &PreviewsSettingsPage::changed
);
99 connect(m_remoteFileSizeBox
, &QSpinBox::valueChanged
, this, &PreviewsSettingsPage::changed
);
102 PreviewsSettingsPage::~PreviewsSettingsPage()
106 void PreviewsSettingsPage::applySettings()
108 const QAbstractItemModel
* model
= m_listView
->model();
109 const int rowCount
= model
->rowCount();
111 m_enabledPreviewPlugins
.clear();
112 for (int i
= 0; i
< rowCount
; ++i
) {
113 const QModelIndex index
= model
->index(i
, 0);
114 const bool checked
= model
->data(index
, Qt::CheckStateRole
).toBool();
116 const QString enabledPlugin
= model
->data(index
, Qt::UserRole
).toString();
117 m_enabledPreviewPlugins
.append(enabledPlugin
);
122 KConfigGroup
globalConfig(KSharedConfig::openConfig(), QStringLiteral("PreviewSettings"));
123 globalConfig
.writeEntry("Plugins", m_enabledPreviewPlugins
);
125 if (!m_localFileSizeBox
->value()) {
126 globalConfig
.deleteEntry("MaximumSize", KConfigBase::Normal
| KConfigBase::Global
);
128 const qulonglong maximumLocalSize
= static_cast<qulonglong
>(m_localFileSizeBox
->value()) * 1024 * 1024;
129 globalConfig
.writeEntry("MaximumSize",
131 KConfigBase::Normal
| KConfigBase::Global
);
134 const qulonglong maximumRemoteSize
= static_cast<qulonglong
>(m_remoteFileSizeBox
->value()) * 1024 * 1024;
135 globalConfig
.writeEntry("MaximumRemoteSize",
137 KConfigBase::Normal
| KConfigBase::Global
);
142 void PreviewsSettingsPage::restoreDefaults()
144 m_localFileSizeBox
->setValue(DefaultMaxLocalPreviewSize
);
145 m_remoteFileSizeBox
->setValue(DefaultMaxRemotePreviewSize
);
148 void PreviewsSettingsPage::showEvent(QShowEvent
* event
)
150 if (!event
->spontaneous() && !m_initialized
) {
151 loadPreviewPlugins();
152 m_initialized
= true;
154 SettingsPageBase::showEvent(event
);
157 #if KIOWIDGETS_BUILD_DEPRECATED_SINCE(5, 87)
158 void PreviewsSettingsPage::configureService(const QModelIndex
& index
)
160 const QAbstractItemModel
* model
= index
.model();
161 const QString pluginName
= model
->data(index
).toString();
162 const QString desktopEntryName
= model
->data(index
, ServiceModel::DesktopEntryNameRole
).toString();
164 ConfigurePreviewPluginDialog
* dialog
= new ConfigurePreviewPluginDialog(pluginName
, desktopEntryName
, this);
165 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
170 void PreviewsSettingsPage::loadPreviewPlugins()
172 QAbstractItemModel
* model
= m_listView
->model();
174 const QVector
<KPluginMetaData
> plugins
= KIO::PreviewJob::availableThumbnailerPlugins();
175 for (const KPluginMetaData
&plugin
: plugins
) {
176 const bool show
= m_enabledPreviewPlugins
.contains(plugin
.pluginId());
179 const QModelIndex index
= model
->index(0, 0);
180 model
->setData(index
, show
, Qt::CheckStateRole
);
181 model
->setData(index
, plugin
.name(), Qt::DisplayRole
);
182 model
->setData(index
, plugin
.pluginId(), ServiceModel::DesktopEntryNameRole
);
184 #if KIOWIDGETS_BUILD_DEPRECATED_SINCE(5, 87)
185 const bool configurable
= plugin
.value(QStringLiteral("Configurable"), false);
186 model
->setData(index
, configurable
, ServiceModel::ConfigurableRole
);
190 model
->sort(Qt::DisplayRole
);
193 void PreviewsSettingsPage::loadSettings()
195 const KConfigGroup
globalConfig(KSharedConfig::openConfig(), QStringLiteral("PreviewSettings"));
196 m_enabledPreviewPlugins
= globalConfig
.readEntry("Plugins", KIO::PreviewJob::defaultPlugins());
198 const qulonglong defaultLocalPreview
= static_cast<qulonglong
>(DefaultMaxLocalPreviewSize
) * 1024 * 1024;
199 const qulonglong maxLocalByteSize
= globalConfig
.readEntry("MaximumSize", defaultLocalPreview
);
200 const int maxLocalMByteSize
= maxLocalByteSize
/ (1024 * 1024);
201 m_localFileSizeBox
->setValue(maxLocalMByteSize
);
203 const qulonglong defaultRemotePreview
= static_cast<qulonglong
>(DefaultMaxRemotePreviewSize
) * 1024 * 1024;
204 const qulonglong maxRemoteByteSize
= globalConfig
.readEntry("MaximumRemoteSize", defaultRemotePreview
);
205 const int maxRemoteMByteSize
= maxRemoteByteSize
/ (1024 * 1024);
206 m_remoteFileSizeBox
->setValue(maxRemoteMByteSize
);