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/servicemodel.h"
12 #include <KIO/PreviewJob>
13 #include <KLocalizedString>
14 #include <KPluginMetaData>
16 #include <QHBoxLayout>
21 #include <QSortFilterProxyModel>
27 const int DefaultMaxLocalPreviewSize
= 0; // 0 MB
28 const int DefaultMaxRemotePreviewSize
= 0; // 0 MB
31 PreviewsSettingsPage::PreviewsSettingsPage(QWidget
*parent
)
32 : SettingsPageBase(parent
)
33 , m_initialized(false)
35 , m_enabledPreviewPlugins()
36 , m_localFileSizeBox(nullptr)
37 , m_remoteFileSizeBox(nullptr)
39 QVBoxLayout
*topLayout
= new QVBoxLayout(this);
41 QLabel
*showPreviewsLabel
= new QLabel(i18nc("@title:group", "Show previews in the view for:"), this);
43 m_listView
= new QListView(this);
44 QScroller::grabGesture(m_listView
->viewport(), QScroller::TouchGesture
);
46 ServiceModel
*serviceModel
= new ServiceModel(this);
47 QSortFilterProxyModel
*proxyModel
= new QSortFilterProxyModel(this);
48 proxyModel
->setSourceModel(serviceModel
);
49 proxyModel
->setSortRole(Qt::DisplayRole
);
50 proxyModel
->setSortCaseSensitivity(Qt::CaseInsensitive
);
52 m_listView
->setModel(proxyModel
);
53 m_listView
->setVerticalScrollMode(QListView::ScrollPerPixel
);
54 m_listView
->setUniformItemSizes(true);
56 QLabel
*localFileSizeLabel
= new QLabel(i18n("Skip previews for local files above:"), this);
58 m_localFileSizeBox
= new QSpinBox(this);
59 m_localFileSizeBox
->setSingleStep(1);
60 m_localFileSizeBox
->setSuffix(i18nc("Mebibytes; used as a suffix in a spinbox showing e.g. '3 MiB'", " MiB"));
61 m_localFileSizeBox
->setRange(0, 9999999); /* MB */
62 m_localFileSizeBox
->setSpecialValueText(i18n("No limit"));
64 QHBoxLayout
*localFileSizeBoxLayout
= new QHBoxLayout();
65 localFileSizeBoxLayout
->addWidget(localFileSizeLabel
);
66 localFileSizeBoxLayout
->addStretch(0);
67 localFileSizeBoxLayout
->addWidget(m_localFileSizeBox
);
69 QLabel
*remoteFileSizeLabel
= new QLabel(i18nc("@label", "Skip previews for remote files above:"), this);
71 m_remoteFileSizeBox
= new QSpinBox(this);
72 m_remoteFileSizeBox
->setSingleStep(1);
73 m_remoteFileSizeBox
->setSuffix(i18nc("Mebibytes; used as a suffix in a spinbox showing e.g. '3 MiB'", " MiB"));
74 m_remoteFileSizeBox
->setRange(0, 9999999); /* MB */
75 m_remoteFileSizeBox
->setSpecialValueText(i18n("No previews"));
77 QHBoxLayout
*remoteFileSizeBoxLayout
= new QHBoxLayout();
78 remoteFileSizeBoxLayout
->addWidget(remoteFileSizeLabel
);
79 remoteFileSizeBoxLayout
->addStretch(0);
80 remoteFileSizeBoxLayout
->addWidget(m_remoteFileSizeBox
);
82 topLayout
->addWidget(showPreviewsLabel
);
83 topLayout
->addWidget(m_listView
);
84 topLayout
->addLayout(localFileSizeBoxLayout
);
85 topLayout
->addLayout(remoteFileSizeBoxLayout
);
89 connect(m_listView
, &QListView::clicked
, this, &PreviewsSettingsPage::changed
);
90 connect(m_localFileSizeBox
, &QSpinBox::valueChanged
, this, &PreviewsSettingsPage::changed
);
91 connect(m_remoteFileSizeBox
, &QSpinBox::valueChanged
, this, &PreviewsSettingsPage::changed
);
94 PreviewsSettingsPage::~PreviewsSettingsPage()
98 void PreviewsSettingsPage::applySettings()
100 const QAbstractItemModel
*model
= m_listView
->model();
101 const int rowCount
= model
->rowCount();
103 m_enabledPreviewPlugins
.clear();
104 for (int i
= 0; i
< rowCount
; ++i
) {
105 const QModelIndex index
= model
->index(i
, 0);
106 const bool checked
= model
->data(index
, Qt::CheckStateRole
).value
<Qt::CheckState
>() == Qt::Checked
;
108 const QString enabledPlugin
= model
->data(index
, Qt::UserRole
).toString();
109 m_enabledPreviewPlugins
.append(enabledPlugin
);
114 KConfigGroup
globalConfig(KSharedConfig::openConfig(), QStringLiteral("PreviewSettings"));
115 globalConfig
.writeEntry("Plugins", m_enabledPreviewPlugins
);
117 if (!m_localFileSizeBox
->value()) {
118 globalConfig
.deleteEntry("MaximumSize", KConfigBase::Normal
| KConfigBase::Global
);
120 const qulonglong maximumLocalSize
= static_cast<qulonglong
>(m_localFileSizeBox
->value()) * 1024 * 1024;
121 globalConfig
.writeEntry("MaximumSize", maximumLocalSize
, KConfigBase::Normal
| KConfigBase::Global
);
124 const qulonglong maximumRemoteSize
= static_cast<qulonglong
>(m_remoteFileSizeBox
->value()) * 1024 * 1024;
125 globalConfig
.writeEntry("MaximumRemoteSize", maximumRemoteSize
, KConfigBase::Normal
| KConfigBase::Global
);
130 void PreviewsSettingsPage::restoreDefaults()
132 m_localFileSizeBox
->setValue(DefaultMaxLocalPreviewSize
);
133 m_remoteFileSizeBox
->setValue(DefaultMaxRemotePreviewSize
);
136 void PreviewsSettingsPage::showEvent(QShowEvent
*event
)
138 if (!event
->spontaneous() && !m_initialized
) {
139 loadPreviewPlugins();
140 m_initialized
= true;
142 SettingsPageBase::showEvent(event
);
145 void PreviewsSettingsPage::loadPreviewPlugins()
147 QAbstractItemModel
*model
= m_listView
->model();
149 const QVector
<KPluginMetaData
> plugins
= KIO::PreviewJob::availableThumbnailerPlugins();
150 for (const KPluginMetaData
&plugin
: plugins
) {
151 const bool show
= m_enabledPreviewPlugins
.contains(plugin
.pluginId());
154 const QModelIndex index
= model
->index(0, 0);
155 model
->setData(index
, show
? Qt::Checked
: Qt::Unchecked
, Qt::CheckStateRole
);
156 model
->setData(index
, plugin
.name(), Qt::DisplayRole
);
157 model
->setData(index
, plugin
.pluginId(), ServiceModel::DesktopEntryNameRole
);
160 model
->sort(Qt::DisplayRole
);
163 void PreviewsSettingsPage::loadSettings()
165 const KConfigGroup
globalConfig(KSharedConfig::openConfig(), QStringLiteral("PreviewSettings"));
166 m_enabledPreviewPlugins
= globalConfig
.readEntry("Plugins", KIO::PreviewJob::defaultPlugins());
168 const qulonglong defaultLocalPreview
= static_cast<qulonglong
>(DefaultMaxLocalPreviewSize
) * 1024 * 1024;
169 const qulonglong maxLocalByteSize
= globalConfig
.readEntry("MaximumSize", defaultLocalPreview
);
170 const int maxLocalMByteSize
= maxLocalByteSize
/ (1024 * 1024);
171 m_localFileSizeBox
->setValue(maxLocalMByteSize
);
173 const qulonglong defaultRemotePreview
= static_cast<qulonglong
>(DefaultMaxRemotePreviewSize
) * 1024 * 1024;
174 const qulonglong maxRemoteByteSize
= globalConfig
.readEntry("MaximumRemoteSize", defaultRemotePreview
);
175 const int maxRemoteMByteSize
= maxRemoteByteSize
/ (1024 * 1024);
176 m_remoteFileSizeBox
->setValue(maxRemoteMByteSize
);
179 #include "moc_previewssettingspage.cpp"