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 "settings/serviceitemdelegate.h"
11 #include "settings/servicemodel.h"
13 #include <KIO/PreviewJob>
14 #include <KLocalizedString>
15 #include <KPluginMetaData>
17 #include <QHBoxLayout>
22 #include <QSortFilterProxyModel>
28 const int DefaultMaxLocalPreviewSize
= 0; // 0 MB
29 const int DefaultMaxRemotePreviewSize
= 0; // 0 MB
32 PreviewsSettingsPage::PreviewsSettingsPage(QWidget
*parent
)
33 : SettingsPageBase(parent
)
34 , m_initialized(false)
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 ServiceModel
*serviceModel
= new ServiceModel(this);
48 QSortFilterProxyModel
*proxyModel
= new QSortFilterProxyModel(this);
49 proxyModel
->setSourceModel(serviceModel
);
50 proxyModel
->setSortRole(Qt::DisplayRole
);
51 proxyModel
->setSortCaseSensitivity(Qt::CaseInsensitive
);
53 m_listView
->setModel(proxyModel
);
54 m_listView
->setVerticalScrollMode(QListView::ScrollPerPixel
);
55 m_listView
->setUniformItemSizes(true);
57 QLabel
*localFileSizeLabel
= new QLabel(i18n("Skip previews for local files above:"), this);
59 m_localFileSizeBox
= new QSpinBox(this);
60 m_localFileSizeBox
->setSingleStep(1);
61 m_localFileSizeBox
->setSuffix(i18nc("Mebibytes; used as a suffix in a spinbox showing e.g. '3 MiB'", " MiB"));
62 m_localFileSizeBox
->setRange(0, 9999999); /* MB */
63 m_localFileSizeBox
->setSpecialValueText(i18n("No limit"));
65 QHBoxLayout
*localFileSizeBoxLayout
= new QHBoxLayout();
66 localFileSizeBoxLayout
->addWidget(localFileSizeLabel
);
67 localFileSizeBoxLayout
->addStretch(0);
68 localFileSizeBoxLayout
->addWidget(m_localFileSizeBox
);
70 QLabel
*remoteFileSizeLabel
= new QLabel(i18nc("@label", "Skip previews for remote files above:"), this);
72 m_remoteFileSizeBox
= new QSpinBox(this);
73 m_remoteFileSizeBox
->setSingleStep(1);
74 m_remoteFileSizeBox
->setSuffix(i18nc("Mebibytes; used as a suffix in a spinbox showing e.g. '3 MiB'", " MiB"));
75 m_remoteFileSizeBox
->setRange(0, 9999999); /* MB */
76 m_remoteFileSizeBox
->setSpecialValueText(i18n("No previews"));
78 QHBoxLayout
*remoteFileSizeBoxLayout
= new QHBoxLayout();
79 remoteFileSizeBoxLayout
->addWidget(remoteFileSizeLabel
);
80 remoteFileSizeBoxLayout
->addStretch(0);
81 remoteFileSizeBoxLayout
->addWidget(m_remoteFileSizeBox
);
83 topLayout
->addWidget(showPreviewsLabel
);
84 topLayout
->addWidget(m_listView
);
85 topLayout
->addLayout(localFileSizeBoxLayout
);
86 topLayout
->addLayout(remoteFileSizeBoxLayout
);
90 connect(m_listView
, &QListView::clicked
, this, &PreviewsSettingsPage::changed
);
91 connect(m_localFileSizeBox
, &QSpinBox::valueChanged
, this, &PreviewsSettingsPage::changed
);
92 connect(m_remoteFileSizeBox
, &QSpinBox::valueChanged
, this, &PreviewsSettingsPage::changed
);
95 PreviewsSettingsPage::~PreviewsSettingsPage()
99 void PreviewsSettingsPage::applySettings()
101 const QAbstractItemModel
*model
= m_listView
->model();
102 const int rowCount
= model
->rowCount();
104 m_enabledPreviewPlugins
.clear();
105 for (int i
= 0; i
< rowCount
; ++i
) {
106 const QModelIndex index
= model
->index(i
, 0);
107 const bool checked
= model
->data(index
, Qt::CheckStateRole
).toBool();
109 const QString enabledPlugin
= model
->data(index
, Qt::UserRole
).toString();
110 m_enabledPreviewPlugins
.append(enabledPlugin
);
115 KConfigGroup
globalConfig(KSharedConfig::openConfig(), QStringLiteral("PreviewSettings"));
116 globalConfig
.writeEntry("Plugins", m_enabledPreviewPlugins
);
118 if (!m_localFileSizeBox
->value()) {
119 globalConfig
.deleteEntry("MaximumSize", KConfigBase::Normal
| KConfigBase::Global
);
121 const qulonglong maximumLocalSize
= static_cast<qulonglong
>(m_localFileSizeBox
->value()) * 1024 * 1024;
122 globalConfig
.writeEntry("MaximumSize", maximumLocalSize
, KConfigBase::Normal
| KConfigBase::Global
);
125 const qulonglong maximumRemoteSize
= static_cast<qulonglong
>(m_remoteFileSizeBox
->value()) * 1024 * 1024;
126 globalConfig
.writeEntry("MaximumRemoteSize", maximumRemoteSize
, KConfigBase::Normal
| KConfigBase::Global
);
131 void PreviewsSettingsPage::restoreDefaults()
133 m_localFileSizeBox
->setValue(DefaultMaxLocalPreviewSize
);
134 m_remoteFileSizeBox
->setValue(DefaultMaxRemotePreviewSize
);
137 void PreviewsSettingsPage::showEvent(QShowEvent
*event
)
139 if (!event
->spontaneous() && !m_initialized
) {
140 loadPreviewPlugins();
141 m_initialized
= true;
143 SettingsPageBase::showEvent(event
);
146 void PreviewsSettingsPage::loadPreviewPlugins()
148 QAbstractItemModel
*model
= m_listView
->model();
150 const QVector
<KPluginMetaData
> plugins
= KIO::PreviewJob::availableThumbnailerPlugins();
151 for (const KPluginMetaData
&plugin
: plugins
) {
152 const bool show
= m_enabledPreviewPlugins
.contains(plugin
.pluginId());
155 const QModelIndex index
= model
->index(0, 0);
156 model
->setData(index
, show
, Qt::CheckStateRole
);
157 model
->setData(index
, plugin
.name(), Qt::DisplayRole
);
158 model
->setData(index
, plugin
.pluginId(), ServiceModel::DesktopEntryNameRole
);
161 model
->sort(Qt::DisplayRole
);
164 void PreviewsSettingsPage::loadSettings()
166 const KConfigGroup
globalConfig(KSharedConfig::openConfig(), QStringLiteral("PreviewSettings"));
167 m_enabledPreviewPlugins
= globalConfig
.readEntry("Plugins", KIO::PreviewJob::defaultPlugins());
169 const qulonglong defaultLocalPreview
= static_cast<qulonglong
>(DefaultMaxLocalPreviewSize
) * 1024 * 1024;
170 const qulonglong maxLocalByteSize
= globalConfig
.readEntry("MaximumSize", defaultLocalPreview
);
171 const int maxLocalMByteSize
= maxLocalByteSize
/ (1024 * 1024);
172 m_localFileSizeBox
->setValue(maxLocalMByteSize
);
174 const qulonglong defaultRemotePreview
= static_cast<qulonglong
>(DefaultMaxRemotePreviewSize
) * 1024 * 1024;
175 const qulonglong maxRemoteByteSize
= globalConfig
.readEntry("MaximumRemoteSize", defaultRemotePreview
);
176 const int maxRemoteMByteSize
= maxRemoteByteSize
/ (1024 * 1024);
177 m_remoteFileSizeBox
->setValue(maxRemoteMByteSize
);