]> cloud.milkyroute.net Git - dolphin.git/blob - src/settings/interface/previewssettingspage.cpp
Merge branch 'master' into kf6
[dolphin.git] / src / settings / interface / previewssettingspage.cpp
1 /*
2 * SPDX-FileCopyrightText: 2006 Peter Penz <peter.penz19@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7 #include "previewssettingspage.h"
8
9 #include "dolphin_generalsettings.h"
10 #include "settings/servicemodel.h"
11
12 #include <KIO/PreviewJob>
13 #include <KLocalizedString>
14 #include <KPluginMetaData>
15
16 #include <QHBoxLayout>
17 #include <QLabel>
18 #include <QListView>
19 #include <QScroller>
20 #include <QShowEvent>
21 #include <QSortFilterProxyModel>
22 #include <QSpinBox>
23
24 // default settings
25 namespace
26 {
27 const int DefaultMaxLocalPreviewSize = 0; // 0 MB
28 const int DefaultMaxRemotePreviewSize = 0; // 0 MB
29 }
30
31 PreviewsSettingsPage::PreviewsSettingsPage(QWidget *parent)
32 : SettingsPageBase(parent)
33 , m_initialized(false)
34 , m_listView(nullptr)
35 , m_enabledPreviewPlugins()
36 , m_localFileSizeBox(nullptr)
37 , m_remoteFileSizeBox(nullptr)
38 {
39 QVBoxLayout *topLayout = new QVBoxLayout(this);
40
41 QLabel *showPreviewsLabel = new QLabel(i18nc("@title:group", "Show previews in the view for:"), this);
42
43 m_listView = new QListView(this);
44 QScroller::grabGesture(m_listView->viewport(), QScroller::TouchGesture);
45
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);
51
52 m_listView->setModel(proxyModel);
53 m_listView->setVerticalScrollMode(QListView::ScrollPerPixel);
54 m_listView->setUniformItemSizes(true);
55
56 QLabel *localFileSizeLabel = new QLabel(i18n("Skip previews for local files above:"), this);
57
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"));
63
64 QHBoxLayout *localFileSizeBoxLayout = new QHBoxLayout();
65 localFileSizeBoxLayout->addWidget(localFileSizeLabel);
66 localFileSizeBoxLayout->addStretch(0);
67 localFileSizeBoxLayout->addWidget(m_localFileSizeBox);
68
69 QLabel *remoteFileSizeLabel = new QLabel(i18nc("@label", "Skip previews for remote files above:"), this);
70
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"));
76
77 QHBoxLayout *remoteFileSizeBoxLayout = new QHBoxLayout();
78 remoteFileSizeBoxLayout->addWidget(remoteFileSizeLabel);
79 remoteFileSizeBoxLayout->addStretch(0);
80 remoteFileSizeBoxLayout->addWidget(m_remoteFileSizeBox);
81
82 topLayout->addWidget(showPreviewsLabel);
83 topLayout->addWidget(m_listView);
84 topLayout->addLayout(localFileSizeBoxLayout);
85 topLayout->addLayout(remoteFileSizeBoxLayout);
86
87 loadSettings();
88
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);
92 }
93
94 PreviewsSettingsPage::~PreviewsSettingsPage()
95 {
96 }
97
98 void PreviewsSettingsPage::applySettings()
99 {
100 const QAbstractItemModel *model = m_listView->model();
101 const int rowCount = model->rowCount();
102 if (rowCount > 0) {
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;
107 if (checked) {
108 const QString enabledPlugin = model->data(index, Qt::UserRole).toString();
109 m_enabledPreviewPlugins.append(enabledPlugin);
110 }
111 }
112 }
113
114 KConfigGroup globalConfig(KSharedConfig::openConfig(), QStringLiteral("PreviewSettings"));
115 globalConfig.writeEntry("Plugins", m_enabledPreviewPlugins);
116
117 if (!m_localFileSizeBox->value()) {
118 globalConfig.deleteEntry("MaximumSize", KConfigBase::Normal | KConfigBase::Global);
119 } else {
120 const qulonglong maximumLocalSize = static_cast<qulonglong>(m_localFileSizeBox->value()) * 1024 * 1024;
121 globalConfig.writeEntry("MaximumSize", maximumLocalSize, KConfigBase::Normal | KConfigBase::Global);
122 }
123
124 const qulonglong maximumRemoteSize = static_cast<qulonglong>(m_remoteFileSizeBox->value()) * 1024 * 1024;
125 globalConfig.writeEntry("MaximumRemoteSize", maximumRemoteSize, KConfigBase::Normal | KConfigBase::Global);
126
127 globalConfig.sync();
128 }
129
130 void PreviewsSettingsPage::restoreDefaults()
131 {
132 m_localFileSizeBox->setValue(DefaultMaxLocalPreviewSize);
133 m_remoteFileSizeBox->setValue(DefaultMaxRemotePreviewSize);
134 }
135
136 void PreviewsSettingsPage::showEvent(QShowEvent *event)
137 {
138 if (!event->spontaneous() && !m_initialized) {
139 loadPreviewPlugins();
140 m_initialized = true;
141 }
142 SettingsPageBase::showEvent(event);
143 }
144
145 void PreviewsSettingsPage::loadPreviewPlugins()
146 {
147 QAbstractItemModel *model = m_listView->model();
148
149 const QVector<KPluginMetaData> plugins = KIO::PreviewJob::availableThumbnailerPlugins();
150 for (const KPluginMetaData &plugin : plugins) {
151 const bool show = m_enabledPreviewPlugins.contains(plugin.pluginId());
152
153 model->insertRow(0);
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);
158 }
159
160 model->sort(Qt::DisplayRole);
161 }
162
163 void PreviewsSettingsPage::loadSettings()
164 {
165 const KConfigGroup globalConfig(KSharedConfig::openConfig(), QStringLiteral("PreviewSettings"));
166 m_enabledPreviewPlugins = globalConfig.readEntry("Plugins", KIO::PreviewJob::defaultPlugins());
167
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);
172
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);
177 }
178
179 #include "moc_previewssettingspage.cpp"