1/*
2 * SPDX-FileCopyrightText: 2018 Marco Martin <mart@kde.org>
3 *
4 * SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6
7#ifndef ALIGNEDSIZEATTACHED_H
8#define ALIGNEDSIZEATTACHED_H
9
10#include <QObject>
11#include <QQmlEngine>
12#include <QQuickWindow>
13
14class QQuickItem;
15
16/*!
17 * \qmltype AlignedSize
18 * \inqmlmodule org.kde.kirigami.primitives
19 *
20 * \brief An attached property that aligns a size on the physical pixel grid.
21 *
22 * Transforms a size to the nearest one perfectly aligned to the pixel grid
23 * in case fractional scaling in used.
24 * \qml
25 * import org.kde.kirigami as Kirigami
26 *
27 * Rectangle {
28 * width: AlignedSize.alignedWidth
29 * height: AlignedSize.alignedHeight
30 * AlignedSize.width: Units.gridUnit
31 * AlignedSize.height: 1
32 * }
33 * \endqml
34 *
35 * \since 6.20
36 */
37class AlignedSizeAttached : public QObject
38{
39 Q_OBJECT
40 QML_ATTACHED(AlignedSizeAttached)
41 QML_NAMED_ELEMENT(AlignedSize)
42 QML_UNCREATABLE("")
43
44 /*!
45 * \qmlattachedproperty real AlignedSize::width
46 *
47 * The width of the item in logical pixels
48 *
49 */
50 Q_PROPERTY(qreal width READ width WRITE setWidth NOTIFY widthChanged RESET resetWidth FINAL)
51
52 /*!
53 * \qmlattachedproperty real AlignedSize::height
54 *
55 * The height of the item in logical pixels
56 *
57 */
58 Q_PROPERTY(qreal height READ height WRITE setHeight NOTIFY heightChanged RESET resetHeight FINAL)
59
60public:
61 explicit AlignedSizeAttached(QObject *parent = nullptr);
62 ~AlignedSizeAttached() override;
63
64 qreal width() const;
65 void setWidth(qreal width);
66 void resetWidth();
67
68 qreal height() const;
69 void setHeight(qreal height);
70 void resetHeight();
71
72 // QML attached property
73 static AlignedSizeAttached *qmlAttachedProperties(QObject *object);
74
75 bool eventFilter(QObject *watched, QEvent *event) override;
76
77Q_SIGNALS:
78 void widthChanged();
79 void heightChanged();
80
81private:
82 qreal alignedWidth() const;
83 qreal alignedHeight() const;
84 void dprChanged();
85
86 QQuickItem *m_item = nullptr;
87 QPointer<QQuickWindow> m_window;
88 qreal m_dpr = 1.0;
89 qreal m_width = -1.0;
90 qreal m_height = -1.0;
91};
92
93QML_DECLARE_TYPEINFO(AlignedSizeAttached, QML_HAS_ATTACHED_PROPERTIES)
94
95#endif // ALIGNEDSIZEATTACHED_H
96

source code of kirigami/src/primitives/alignedsizeattached.h