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

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 "previewbutton.h"
0007 #include "previewbridge.h"
0008 #include "previewclient.h"
0009 #include "previewsettings.h"
0010 
0011 #include <KDecoration2/Decoration>
0012 
0013 #include <QPainter>
0014 
0015 namespace KDecoration2
0016 {
0017 
0018 namespace Preview
0019 {
0020 
0021 PreviewButtonItem::PreviewButtonItem(QQuickItem *parent)
0022     : QQuickPaintedItem(parent)
0023 {
0024 }
0025 
0026 PreviewButtonItem::~PreviewButtonItem() = default;
0027 
0028 void PreviewButtonItem::setType(int type)
0029 {
0030     setType(KDecoration2::DecorationButtonType(type));
0031 }
0032 
0033 void PreviewButtonItem::setType(KDecoration2::DecorationButtonType type)
0034 {
0035     if (m_type == type) {
0036         return;
0037     }
0038     m_type = type;
0039     Q_EMIT typeChanged();
0040 }
0041 
0042 KDecoration2::DecorationButtonType PreviewButtonItem::type() const
0043 {
0044     return m_type;
0045 }
0046 
0047 PreviewBridge *PreviewButtonItem::bridge() const
0048 {
0049     return m_bridge.data();
0050 }
0051 
0052 void PreviewButtonItem::setBridge(PreviewBridge *bridge)
0053 {
0054     if (m_bridge == bridge) {
0055         return;
0056     }
0057     m_bridge = bridge;
0058     Q_EMIT bridgeChanged();
0059 }
0060 
0061 Settings *PreviewButtonItem::settings() const
0062 {
0063     return m_settings.data();
0064 }
0065 
0066 void PreviewButtonItem::setSettings(Settings *settings)
0067 {
0068     if (m_settings == settings) {
0069         return;
0070     }
0071     m_settings = settings;
0072     Q_EMIT settingsChanged();
0073 }
0074 
0075 int PreviewButtonItem::typeAsInt() const
0076 {
0077     return int(m_type);
0078 }
0079 
0080 void PreviewButtonItem::componentComplete()
0081 {
0082     QQuickPaintedItem::componentComplete();
0083     createButton();
0084 }
0085 
0086 void PreviewButtonItem::createButton()
0087 {
0088     if (m_type == KDecoration2::DecorationButtonType::Custom || m_decoration || !m_settings || !m_bridge) {
0089         return;
0090     }
0091     m_decoration = m_bridge->createDecoration(this);
0092     if (!m_decoration) {
0093         return;
0094     }
0095     auto client = m_bridge->lastCreatedClient();
0096     client->setMinimizable(true);
0097     client->setMaximizable(true);
0098     client->setActive(false);
0099     client->setProvidesContextHelp(true);
0100     m_decoration->setSettings(m_settings->settings());
0101     m_decoration->init();
0102     m_button = m_bridge->createButton(m_decoration, m_type);
0103     connect(this, &PreviewButtonItem::widthChanged, this, &PreviewButtonItem::syncGeometry);
0104     connect(this, &PreviewButtonItem::heightChanged, this, &PreviewButtonItem::syncGeometry);
0105     syncGeometry();
0106 }
0107 
0108 void PreviewButtonItem::syncGeometry()
0109 {
0110     if (!m_button) {
0111         return;
0112     }
0113     m_button->setGeometry(QRect(0, 0, width(), height()));
0114 }
0115 
0116 void PreviewButtonItem::paint(QPainter *painter)
0117 {
0118     if (!m_button) {
0119         return;
0120     }
0121 
0122     const QRect rect(0, 0, width(), height());
0123     if (type() == KDecoration2::DecorationButtonType::Spacer) {
0124         static const QIcon icon = QIcon::fromTheme(QStringLiteral("distribute-horizontal"));
0125         icon.paint(painter, rect);
0126     } else {
0127         m_button->paint(painter, rect);
0128     }
0129 
0130     painter->setCompositionMode(QPainter::CompositionMode_SourceAtop);
0131     painter->fillRect(rect, m_color);
0132 }
0133 
0134 void PreviewButtonItem::setColor(const QColor &color)
0135 {
0136     m_color = color;
0137     m_color.setAlpha(127);
0138     update();
0139 }
0140 
0141 }
0142 }
0143 
0144 #include "moc_previewbutton.cpp"