File indexing completed on 2024-05-19 05:38:25

0001 /*
0002     SPDX-FileCopyrightText: 2003-2007 Fredrik Höglund <fredrik@kde.org>
0003     SPDX-FileCopyrightText: 2019 Kai Uwe Broulik <kde@broulik.de>
0004 
0005     SPDX-License-Identifier: GPL-2.0-only
0006 */
0007 
0008 #pragma once
0009 
0010 #include <QPointer>
0011 #include <QQuickPaintedItem>
0012 
0013 #include "ui_stylepreview.h"
0014 
0015 class QWidget;
0016 class QStyle;
0017 
0018 class PreviewItem : public QQuickPaintedItem
0019 {
0020     Q_OBJECT
0021 
0022     Q_PROPERTY(QString styleName READ styleName WRITE setStyleName NOTIFY styleNameChanged)
0023 
0024     Q_PROPERTY(bool valid READ isValid NOTIFY validChanged)
0025 
0026 public:
0027     explicit PreviewItem(QQuickItem *parent = nullptr);
0028     ~PreviewItem() override;
0029 
0030     QString styleName() const;
0031     void setStyleName(const QString &styleName);
0032     Q_SIGNAL void styleNameChanged();
0033 
0034     bool isValid() const;
0035     Q_SIGNAL void validChanged();
0036 
0037     Q_INVOKABLE void reload();
0038 
0039     void componentComplete() override;
0040 
0041 protected:
0042     bool eventFilter(QObject *watched, QEvent *event) override;
0043 
0044     void paint(QPainter *painter) override;
0045 
0046     void hoverEnterEvent(QHoverEvent *event) override;
0047     void hoverMoveEvent(QHoverEvent *event) override;
0048     void hoverLeaveEvent(QHoverEvent *event) override;
0049     void timerEvent(QTimerEvent *event) override;
0050     void geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry) override;
0051 
0052 private:
0053     void sendHoverEvent(QHoverEvent *event);
0054     void dispatchEnterLeave(QWidget *enter, QWidget *leave, const QPointF &globalPosF);
0055     void resizeWidget(const QSizeF &newSize);
0056 
0057     QString m_styleName;
0058 
0059     Ui::StylePreview m_ui;
0060 
0061     std::unique_ptr<QWidget> m_widget;
0062     QPointer<QWidget> m_lastWidgetUnderMouse;
0063     int m_timerId = 0;
0064     bool m_containsMouse = false;
0065 
0066     std::unique_ptr<QStyle> m_style;
0067 };