KWin
Loading...
Searching...
No Matches
previewsettings.cpp
Go to the documentation of this file.
1/*
2 SPDX-FileCopyrightText: 2014 Martin Gräßlin <mgraesslin@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
5*/
6#include "previewsettings.h"
7#include "buttonsmodel.h"
8#include "previewbridge.h"
9
10#include <KLocalizedString>
11
12#include <QFontDatabase>
13
14namespace KDecoration2
15{
16
17namespace Preview
18{
19
21 : QAbstractListModel(parent)
22{
23}
24
26
27QVariant BorderSizesModel::data(const QModelIndex &index, int role) const
28{
29 if (!index.isValid() || index.row() < 0 || index.row() >= m_borders.count() || index.column() != 0) {
30 return QVariant();
31 }
32 if (role != Qt::DisplayRole && role != Qt::UserRole) {
33 return QVariant();
34 }
35 return QVariant::fromValue<BorderSize>(m_borders.at(index.row()));
36}
37
38int BorderSizesModel::rowCount(const QModelIndex &parent) const
39{
40 if (parent.isValid()) {
41 return 0;
42 }
43 return m_borders.count();
44}
45
46QHash<int, QByteArray> BorderSizesModel::roleNames() const
47{
48 QHash<int, QByteArray> roles;
49 roles.insert(Qt::DisplayRole, QByteArrayLiteral("display"));
50 return roles;
51}
52
53PreviewSettings::PreviewSettings(DecorationSettings *parent)
54 : QObject()
55 , DecorationSettingsPrivate(parent)
56 , m_alphaChannelSupported(true)
57 , m_onAllDesktopsAvailable(true)
58 , m_closeOnDoubleClick(false)
59 , m_leftButtons(new ButtonsModel(QList<DecorationButtonType>({DecorationButtonType::Menu,
60 DecorationButtonType::ApplicationMenu,
61 DecorationButtonType::OnAllDesktops}),
62 this))
63 , m_rightButtons(new ButtonsModel(QList<DecorationButtonType>({DecorationButtonType::ContextHelp,
64 DecorationButtonType::Minimize,
65 DecorationButtonType::Maximize,
66 DecorationButtonType::Close}),
67 this))
68 , m_availableButtons(new ButtonsModel(QList<DecorationButtonType>({DecorationButtonType::Menu,
69 DecorationButtonType::ApplicationMenu,
70 DecorationButtonType::OnAllDesktops,
71 DecorationButtonType::Minimize,
72 DecorationButtonType::Maximize,
73 DecorationButtonType::Close,
74 DecorationButtonType::ContextHelp,
75 DecorationButtonType::Shade,
76 DecorationButtonType::KeepBelow,
77 DecorationButtonType::KeepAbove}),
78 this))
79 , m_borderSizes(new BorderSizesModel(this))
80 , m_borderSize(int(BorderSize::Normal))
81 , m_font(QFontDatabase::systemFont(QFontDatabase::TitleFont))
82{
83 connect(this, &PreviewSettings::alphaChannelSupportedChanged, parent, &DecorationSettings::alphaChannelSupportedChanged);
84 connect(this, &PreviewSettings::onAllDesktopsAvailableChanged, parent, &DecorationSettings::onAllDesktopsAvailableChanged);
85 connect(this, &PreviewSettings::closeOnDoubleClickOnMenuChanged, parent, &DecorationSettings::closeOnDoubleClickOnMenuChanged);
86 connect(this, &PreviewSettings::fontChanged, parent, &DecorationSettings::fontChanged);
87 auto updateLeft = [this, parent]() {
88 Q_EMIT parent->decorationButtonsLeftChanged(decorationButtonsLeft());
89 };
90 auto updateRight = [this, parent]() {
91 Q_EMIT parent->decorationButtonsRightChanged(decorationButtonsRight());
92 };
93 connect(m_leftButtons, &QAbstractItemModel::rowsRemoved, this, updateLeft);
94 connect(m_leftButtons, &QAbstractItemModel::rowsMoved, this, updateLeft);
95 connect(m_leftButtons, &QAbstractItemModel::rowsInserted, this, updateLeft);
96 connect(m_rightButtons, &QAbstractItemModel::rowsRemoved, this, updateRight);
97 connect(m_rightButtons, &QAbstractItemModel::rowsMoved, this, updateRight);
98 connect(m_rightButtons, &QAbstractItemModel::rowsInserted, this, updateRight);
99}
100
102
103QAbstractItemModel *PreviewSettings::availableButtonsModel() const
104{
105 return m_availableButtons;
106}
107
108QAbstractItemModel *PreviewSettings::leftButtonsModel() const
109{
110 return m_leftButtons;
111}
112
113QAbstractItemModel *PreviewSettings::rightButtonsModel() const
114{
115 return m_rightButtons;
116}
117
119{
120 return m_alphaChannelSupported;
121}
122
124{
125 return m_onAllDesktopsAvailable;
126}
127
129{
130 if (m_alphaChannelSupported == supported) {
131 return;
132 }
133 m_alphaChannelSupported = supported;
134 Q_EMIT alphaChannelSupportedChanged(m_alphaChannelSupported);
135}
136
138{
139 if (m_onAllDesktopsAvailable == available) {
140 return;
141 }
142 m_onAllDesktopsAvailable = available;
143 Q_EMIT onAllDesktopsAvailableChanged(m_onAllDesktopsAvailable);
144}
145
147{
148 if (m_closeOnDoubleClick == enabled) {
149 return;
150 }
151 m_closeOnDoubleClick = enabled;
152 Q_EMIT closeOnDoubleClickOnMenuChanged(enabled);
153}
154
155QList<DecorationButtonType> PreviewSettings::decorationButtonsLeft() const
156{
157 return m_leftButtons->buttons();
158}
159
160QList<DecorationButtonType> PreviewSettings::decorationButtonsRight() const
161{
162 return m_rightButtons->buttons();
163}
164
166{
167 QModelIndex index = m_availableButtons->index(row);
168 if (!index.isValid()) {
169 return;
170 }
171 m_leftButtons->add(index.data(Qt::UserRole).value<DecorationButtonType>());
172}
173
175{
176 QModelIndex index = m_availableButtons->index(row);
177 if (!index.isValid()) {
178 return;
179 }
180 m_rightButtons->add(index.data(Qt::UserRole).value<DecorationButtonType>());
181}
182
184{
185 if (m_borderSize == index) {
186 return;
187 }
188 m_borderSize = index;
189 Q_EMIT borderSizesIndexChanged(index);
190 Q_EMIT decorationSettings()->borderSizeChanged(borderSize());
191}
192
194{
195 return m_borderSizes->index(m_borderSize).data(Qt::UserRole).value<BorderSize>();
196}
197
198void PreviewSettings::setFont(const QFont &font)
199{
200 if (m_font == font) {
201 return;
202 }
203 m_font = font;
204 Q_EMIT fontChanged(m_font);
205}
206
207Settings::Settings(QObject *parent)
208 : QObject(parent)
209{
210 connect(this, &Settings::bridgeChanged, this, &Settings::createSettings);
211}
212
213Settings::~Settings() = default;
214
216{
217 if (m_bridge == bridge) {
218 return;
219 }
220 m_bridge = bridge;
221 Q_EMIT bridgeChanged();
222}
223
225{
226 return m_bridge.data();
227}
228
229void Settings::createSettings()
230{
231 if (m_bridge.isNull()) {
232 m_settings.reset();
233 } else {
234 m_settings = std::make_shared<KDecoration2::DecorationSettings>(m_bridge.get());
235 m_previewSettings = m_bridge->lastCreatedSettings();
236 m_previewSettings->setBorderSizesIndex(m_borderSize);
238 }
239 Q_EMIT settingsChanged();
240}
241
242std::shared_ptr<DecorationSettings> Settings::settings() const
243{
244 return m_settings;
245}
246
247DecorationSettings *Settings::settingsPointer() const
248{
249 return m_settings.get();
250}
251
253{
254 if (m_borderSize == index) {
255 return;
256 }
257 m_borderSize = index;
258 Q_EMIT borderSizesIndexChanged(m_borderSize);
259}
260
261}
262}
263
264#include "moc_previewsettings.cpp"
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
QHash< int, QByteArray > roleNames() const override
int rowCount(const QModelIndex &parent=QModelIndex()) const override
void add(DecorationButtonType type)
QList< DecorationButtonType > buttons() const
Q_INVOKABLE void addButtonToRight(int row)
QList< DecorationButtonType > decorationButtonsRight() const override
BorderSize borderSize() const override
Q_INVOKABLE void addButtonToLeft(int row)
QList< DecorationButtonType > decorationButtonsLeft() const override
PreviewSettings(DecorationSettings *parent)
KDecoration2::Preview::PreviewBridge * bridge
void setBridge(PreviewBridge *bridge)
KDecoration2::DecorationSettings * settings
Settings(QObject *parent=nullptr)
DecorationSettings * settingsPointer() const