File indexing completed on 2024-11-10 04:56:48

0001 /*
0002     SPDX-FileCopyrightText: 2014 Martin Gräßlin <mgraesslin@kde.org>
0003 
0004     SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005 */
0006 #include "previewsettings.h"
0007 #include "buttonsmodel.h"
0008 #include "previewbridge.h"
0009 
0010 #include <KLocalizedString>
0011 
0012 #include <QFontDatabase>
0013 
0014 namespace KDecoration2
0015 {
0016 
0017 namespace Preview
0018 {
0019 
0020 BorderSizesModel::BorderSizesModel(QObject *parent)
0021     : QAbstractListModel(parent)
0022 {
0023 }
0024 
0025 BorderSizesModel::~BorderSizesModel() = default;
0026 
0027 QVariant BorderSizesModel::data(const QModelIndex &index, int role) const
0028 {
0029     if (!index.isValid() || index.row() < 0 || index.row() >= m_borders.count() || index.column() != 0) {
0030         return QVariant();
0031     }
0032     if (role != Qt::DisplayRole && role != Qt::UserRole) {
0033         return QVariant();
0034     }
0035     return QVariant::fromValue<BorderSize>(m_borders.at(index.row()));
0036 }
0037 
0038 int BorderSizesModel::rowCount(const QModelIndex &parent) const
0039 {
0040     if (parent.isValid()) {
0041         return 0;
0042     }
0043     return m_borders.count();
0044 }
0045 
0046 QHash<int, QByteArray> BorderSizesModel::roleNames() const
0047 {
0048     QHash<int, QByteArray> roles;
0049     roles.insert(Qt::DisplayRole, QByteArrayLiteral("display"));
0050     return roles;
0051 }
0052 
0053 PreviewSettings::PreviewSettings(DecorationSettings *parent)
0054     : QObject()
0055     , DecorationSettingsPrivate(parent)
0056     , m_alphaChannelSupported(true)
0057     , m_onAllDesktopsAvailable(true)
0058     , m_closeOnDoubleClick(false)
0059     , m_leftButtons(new ButtonsModel(QList<DecorationButtonType>({DecorationButtonType::Menu,
0060                                                                   DecorationButtonType::ApplicationMenu,
0061                                                                   DecorationButtonType::OnAllDesktops}),
0062                                      this))
0063     , m_rightButtons(new ButtonsModel(QList<DecorationButtonType>({DecorationButtonType::ContextHelp,
0064                                                                    DecorationButtonType::Minimize,
0065                                                                    DecorationButtonType::Maximize,
0066                                                                    DecorationButtonType::Close}),
0067                                       this))
0068     , m_availableButtons(new ButtonsModel(QList<DecorationButtonType>({DecorationButtonType::Menu,
0069                                                                        DecorationButtonType::ApplicationMenu,
0070                                                                        DecorationButtonType::OnAllDesktops,
0071                                                                        DecorationButtonType::Minimize,
0072                                                                        DecorationButtonType::Maximize,
0073                                                                        DecorationButtonType::Close,
0074                                                                        DecorationButtonType::ContextHelp,
0075                                                                        DecorationButtonType::Shade,
0076                                                                        DecorationButtonType::KeepBelow,
0077                                                                        DecorationButtonType::KeepAbove}),
0078                                           this))
0079     , m_borderSizes(new BorderSizesModel(this))
0080     , m_borderSize(int(BorderSize::Normal))
0081     , m_font(QFontDatabase::systemFont(QFontDatabase::TitleFont))
0082 {
0083     connect(this, &PreviewSettings::alphaChannelSupportedChanged, parent, &DecorationSettings::alphaChannelSupportedChanged);
0084     connect(this, &PreviewSettings::onAllDesktopsAvailableChanged, parent, &DecorationSettings::onAllDesktopsAvailableChanged);
0085     connect(this, &PreviewSettings::closeOnDoubleClickOnMenuChanged, parent, &DecorationSettings::closeOnDoubleClickOnMenuChanged);
0086     connect(this, &PreviewSettings::fontChanged, parent, &DecorationSettings::fontChanged);
0087     auto updateLeft = [this, parent]() {
0088         Q_EMIT parent->decorationButtonsLeftChanged(decorationButtonsLeft());
0089     };
0090     auto updateRight = [this, parent]() {
0091         Q_EMIT parent->decorationButtonsRightChanged(decorationButtonsRight());
0092     };
0093     connect(m_leftButtons, &QAbstractItemModel::rowsRemoved, this, updateLeft);
0094     connect(m_leftButtons, &QAbstractItemModel::rowsMoved, this, updateLeft);
0095     connect(m_leftButtons, &QAbstractItemModel::rowsInserted, this, updateLeft);
0096     connect(m_rightButtons, &QAbstractItemModel::rowsRemoved, this, updateRight);
0097     connect(m_rightButtons, &QAbstractItemModel::rowsMoved, this, updateRight);
0098     connect(m_rightButtons, &QAbstractItemModel::rowsInserted, this, updateRight);
0099 }
0100 
0101 PreviewSettings::~PreviewSettings() = default;
0102 
0103 QAbstractItemModel *PreviewSettings::availableButtonsModel() const
0104 {
0105     return m_availableButtons;
0106 }
0107 
0108 QAbstractItemModel *PreviewSettings::leftButtonsModel() const
0109 {
0110     return m_leftButtons;
0111 }
0112 
0113 QAbstractItemModel *PreviewSettings::rightButtonsModel() const
0114 {
0115     return m_rightButtons;
0116 }
0117 
0118 bool PreviewSettings::isAlphaChannelSupported() const
0119 {
0120     return m_alphaChannelSupported;
0121 }
0122 
0123 bool PreviewSettings::isOnAllDesktopsAvailable() const
0124 {
0125     return m_onAllDesktopsAvailable;
0126 }
0127 
0128 void PreviewSettings::setAlphaChannelSupported(bool supported)
0129 {
0130     if (m_alphaChannelSupported == supported) {
0131         return;
0132     }
0133     m_alphaChannelSupported = supported;
0134     Q_EMIT alphaChannelSupportedChanged(m_alphaChannelSupported);
0135 }
0136 
0137 void PreviewSettings::setOnAllDesktopsAvailable(bool available)
0138 {
0139     if (m_onAllDesktopsAvailable == available) {
0140         return;
0141     }
0142     m_onAllDesktopsAvailable = available;
0143     Q_EMIT onAllDesktopsAvailableChanged(m_onAllDesktopsAvailable);
0144 }
0145 
0146 void PreviewSettings::setCloseOnDoubleClickOnMenu(bool enabled)
0147 {
0148     if (m_closeOnDoubleClick == enabled) {
0149         return;
0150     }
0151     m_closeOnDoubleClick = enabled;
0152     Q_EMIT closeOnDoubleClickOnMenuChanged(enabled);
0153 }
0154 
0155 QList<DecorationButtonType> PreviewSettings::decorationButtonsLeft() const
0156 {
0157     return m_leftButtons->buttons();
0158 }
0159 
0160 QList<DecorationButtonType> PreviewSettings::decorationButtonsRight() const
0161 {
0162     return m_rightButtons->buttons();
0163 }
0164 
0165 void PreviewSettings::addButtonToLeft(int row)
0166 {
0167     QModelIndex index = m_availableButtons->index(row);
0168     if (!index.isValid()) {
0169         return;
0170     }
0171     m_leftButtons->add(index.data(Qt::UserRole).value<DecorationButtonType>());
0172 }
0173 
0174 void PreviewSettings::addButtonToRight(int row)
0175 {
0176     QModelIndex index = m_availableButtons->index(row);
0177     if (!index.isValid()) {
0178         return;
0179     }
0180     m_rightButtons->add(index.data(Qt::UserRole).value<DecorationButtonType>());
0181 }
0182 
0183 void PreviewSettings::setBorderSizesIndex(int index)
0184 {
0185     if (m_borderSize == index) {
0186         return;
0187     }
0188     m_borderSize = index;
0189     Q_EMIT borderSizesIndexChanged(index);
0190     Q_EMIT decorationSettings()->borderSizeChanged(borderSize());
0191 }
0192 
0193 BorderSize PreviewSettings::borderSize() const
0194 {
0195     return m_borderSizes->index(m_borderSize).data(Qt::UserRole).value<BorderSize>();
0196 }
0197 
0198 void PreviewSettings::setFont(const QFont &font)
0199 {
0200     if (m_font == font) {
0201         return;
0202     }
0203     m_font = font;
0204     Q_EMIT fontChanged(m_font);
0205 }
0206 
0207 Settings::Settings(QObject *parent)
0208     : QObject(parent)
0209 {
0210     connect(this, &Settings::bridgeChanged, this, &Settings::createSettings);
0211 }
0212 
0213 Settings::~Settings() = default;
0214 
0215 void Settings::setBridge(PreviewBridge *bridge)
0216 {
0217     if (m_bridge == bridge) {
0218         return;
0219     }
0220     m_bridge = bridge;
0221     Q_EMIT bridgeChanged();
0222 }
0223 
0224 PreviewBridge *Settings::bridge() const
0225 {
0226     return m_bridge.data();
0227 }
0228 
0229 void Settings::createSettings()
0230 {
0231     if (m_bridge.isNull()) {
0232         m_settings.reset();
0233     } else {
0234         m_settings = std::make_shared<KDecoration2::DecorationSettings>(m_bridge.get());
0235         m_previewSettings = m_bridge->lastCreatedSettings();
0236         m_previewSettings->setBorderSizesIndex(m_borderSize);
0237         connect(this, &Settings::borderSizesIndexChanged, m_previewSettings, &PreviewSettings::setBorderSizesIndex);
0238     }
0239     Q_EMIT settingsChanged();
0240 }
0241 
0242 std::shared_ptr<DecorationSettings> Settings::settings() const
0243 {
0244     return m_settings;
0245 }
0246 
0247 DecorationSettings *Settings::settingsPointer() const
0248 {
0249     return m_settings.get();
0250 }
0251 
0252 void Settings::setBorderSizesIndex(int index)
0253 {
0254     if (m_borderSize == index) {
0255         return;
0256     }
0257     m_borderSize = index;
0258     Q_EMIT borderSizesIndexChanged(m_borderSize);
0259 }
0260 
0261 }
0262 }
0263 
0264 #include "moc_previewsettings.cpp"