Changeset View
Changeset View
Standalone View
Standalone View
components/containmentlayoutmanager/appletslayout.h
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | * Copyright 2019 by Marco Martin <mart@kde.org> | ||||
3 | * | ||||
4 | * This program is free software; you can redistribute it and/or modify | ||||
5 | * it under the terms of the GNU Library General Public License as | ||||
6 | * published by the Free Software Foundation; either version 2, or | ||||
7 | * (at your option) any later version. | ||||
8 | * | ||||
9 | * This program is distributed in the hope that it will be useful, | ||||
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
12 | * GNU Library General Public License for more details | ||||
13 | * | ||||
14 | * You should have received a copy of the GNU Library General Public | ||||
15 | * License along with this program; if not, write to the | ||||
16 | * Free Software Foundation, Inc., | ||||
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||||
18 | * | ||||
19 | */ | ||||
20 | | ||||
21 | #pragma once | ||||
22 | | ||||
23 | #include <QQuickItem> | ||||
24 | #include <QQmlParserStatus> | ||||
25 | #include <QPointer> | ||||
26 | | ||||
27 | class QTimer; | ||||
28 | | ||||
29 | namespace Plasma { | ||||
30 | class Containment; | ||||
31 | } | ||||
32 | | ||||
33 | namespace PlasmaQuick { | ||||
34 | class AppletQuickItem; | ||||
35 | } | ||||
36 | | ||||
37 | class AbstractLayoutManager; | ||||
38 | class AppletContainer; | ||||
39 | class ItemContainer; | ||||
40 | | ||||
41 | class AppletsLayout: public QQuickItem | ||||
42 | { | ||||
43 | Q_OBJECT | ||||
44 | Q_INTERFACES(QQmlParserStatus) | ||||
45 | | ||||
46 | Q_PROPERTY(QString configKey READ configKey WRITE setConfigKey NOTIFY configKeyChanged) | ||||
47 | | ||||
48 | Q_PROPERTY(PlasmaQuick::AppletQuickItem *containment READ containment WRITE setContainment NOTIFY containmentChanged) | ||||
49 | | ||||
50 | Q_PROPERTY(QJSValue acceptsAppletCallback READ acceptsAppletCallback WRITE setAcceptsAppletCallback NOTIFY acceptsAppletCallbackChanged) | ||||
51 | | ||||
52 | Q_PROPERTY(qreal minimumItemWidth READ minimumItemWidth WRITE setMinimumItemWidth NOTIFY minimumItemWidthChanged) | ||||
53 | | ||||
54 | Q_PROPERTY(qreal minimumItemHeight READ minimumItemHeight WRITE setMinimumItemHeight NOTIFY minimumItemHeightChanged) | ||||
55 | | ||||
56 | Q_PROPERTY(qreal defaultItemWidth READ defaultItemWidth WRITE setDefaultItemWidth NOTIFY defaultItemWidthChanged) | ||||
57 | | ||||
58 | Q_PROPERTY(qreal defaultItemHeight READ defaultItemHeight WRITE setDefaultItemHeight NOTIFY defaultItemHeightChanged) | ||||
59 | | ||||
60 | Q_PROPERTY(qreal cellWidth READ cellWidth WRITE setCellWidth NOTIFY cellWidthChanged) | ||||
61 | | ||||
62 | Q_PROPERTY(qreal cellHeight READ cellHeight WRITE setCellHeight NOTIFY cellHeightChanged) | ||||
63 | | ||||
64 | Q_PROPERTY(QQmlComponent *appletContainerComponent READ appletContainerComponent WRITE setAppletContainerComponent NOTIFY appletContainerComponentChanged) | ||||
65 | | ||||
66 | Q_PROPERTY(ItemContainer *placeHolder READ placeHolder WRITE setPlaceHolder NOTIFY placeHolderChanged); | ||||
67 | | ||||
68 | Q_PROPERTY(AppletsLayout::EditModeCondition editModeCondition READ editModeCondition WRITE setEditModeCondition NOTIFY editModeConditionChanged) | ||||
69 | Q_PROPERTY(bool editMode READ editMode WRITE setEditMode NOTIFY editModeChanged) | ||||
70 | | ||||
71 | public: | ||||
72 | enum PreferredLayoutDirection { | ||||
73 | Closest = 0, | ||||
74 | LeftToRight, | ||||
75 | RightToLeft, | ||||
76 | TopToBottom, | ||||
77 | BottomToTop | ||||
78 | }; | ||||
79 | Q_ENUM(PreferredLayoutDirection) | ||||
80 | | ||||
81 | enum EditModeCondition { | ||||
82 | Locked = 0, | ||||
83 | Manual, | ||||
84 | AfterPressAndHold, | ||||
85 | }; | ||||
86 | Q_ENUM(EditModeCondition) | ||||
87 | | ||||
88 | AppletsLayout(QQuickItem *parent = nullptr); | ||||
89 | ~AppletsLayout(); | ||||
90 | | ||||
91 | // QML setters and getters | ||||
92 | QString configKey() const; | ||||
93 | void setConfigKey(const QString &key); | ||||
94 | | ||||
95 | PlasmaQuick::AppletQuickItem *containment() const; | ||||
96 | void setContainment(PlasmaQuick::AppletQuickItem *containment); | ||||
97 | | ||||
98 | QJSValue acceptsAppletCallback() const; | ||||
99 | void setAcceptsAppletCallback(const QJSValue& callback); | ||||
100 | | ||||
101 | qreal minimumItemWidth() const; | ||||
102 | void setMinimumItemWidth(qreal width); | ||||
103 | | ||||
104 | qreal minimumItemHeight() const; | ||||
105 | void setMinimumItemHeight(qreal height); | ||||
106 | | ||||
107 | qreal defaultItemWidth() const; | ||||
108 | void setDefaultItemWidth(qreal width); | ||||
109 | | ||||
110 | qreal defaultItemHeight() const; | ||||
111 | void setDefaultItemHeight(qreal height); | ||||
112 | | ||||
113 | qreal cellWidth() const; | ||||
114 | void setCellWidth(qreal width); | ||||
115 | | ||||
116 | qreal cellHeight() const; | ||||
117 | void setCellHeight(qreal height); | ||||
118 | | ||||
119 | QQmlComponent *appletContainerComponent() const; | ||||
120 | void setAppletContainerComponent(QQmlComponent *component); | ||||
121 | | ||||
122 | ItemContainer *placeHolder() const; | ||||
123 | void setPlaceHolder(ItemContainer *placeHolder); | ||||
124 | | ||||
125 | EditModeCondition editModeCondition() const; | ||||
126 | void setEditModeCondition(EditModeCondition condition); | ||||
127 | | ||||
128 | bool editMode() const; | ||||
129 | void setEditMode(bool edit); | ||||
130 | | ||||
131 | Q_INVOKABLE void save(); | ||||
132 | Q_INVOKABLE void showPlaceHolderAt(const QRectF &geom); | ||||
133 | Q_INVOKABLE void showPlaceHolderForItem(QQuickItem *item); | ||||
134 | Q_INVOKABLE void hidePlaceHolder(); | ||||
135 | | ||||
136 | Q_INVOKABLE bool isRectAvailable(qreal x, qreal y, qreal width, qreal height); | ||||
137 | Q_INVOKABLE bool itemIsManaged(ItemContainer *item); | ||||
138 | Q_INVOKABLE void positionItem(ItemContainer *item); | ||||
139 | Q_INVOKABLE void releaseSpace(ItemContainer *item); | ||||
140 | | ||||
141 | Q_SIGNALS: | ||||
142 | /** | ||||
143 | * An applet has been refused by the layout: acceptsAppletCallback | ||||
144 | * returned false and will need to be managed in a different way | ||||
145 | */ | ||||
146 | void appletRefused(QObject *applet, int x, int y); | ||||
147 | | ||||
148 | void configKeyChanged(); | ||||
149 | void containmentChanged(); | ||||
150 | void minimumItemWidthChanged(); | ||||
151 | void minimumItemHeightChanged(); | ||||
152 | void defaultItemWidthChanged(); | ||||
153 | void defaultItemHeightChanged(); | ||||
154 | void cellWidthChanged(); | ||||
155 | void cellHeightChanged(); | ||||
156 | void acceptsAppletCallbackChanged(); | ||||
157 | void appletContainerComponentChanged(); | ||||
158 | void placeHolderChanged(); | ||||
159 | void editModeConditionChanged(); | ||||
160 | void editModeChanged(); | ||||
161 | | ||||
162 | protected: | ||||
163 | void updatePolish() override; | ||||
164 | void geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry) override; | ||||
165 | | ||||
166 | //void classBegin() override; | ||||
167 | void componentComplete() override; | ||||
168 | void mousePressEvent(QMouseEvent *event) override; | ||||
169 | void mouseMoveEvent(QMouseEvent *event) override; | ||||
170 | void mouseReleaseEvent(QMouseEvent *event) override; | ||||
171 | | ||||
172 | private Q_SLOTS: | ||||
173 | void appletAdded(QObject *applet, int x, int y); | ||||
174 | void appletRemoved(QObject *applet); | ||||
175 | | ||||
176 | private: | ||||
177 | AppletContainer *createContainerForApplet(PlasmaQuick::AppletQuickItem *appletItem); | ||||
178 | | ||||
179 | | ||||
180 | QString m_configKey; | ||||
181 | QTimer *m_saveLayoutTimer; | ||||
182 | QTimer *m_configKeyChangeTimer; | ||||
183 | | ||||
184 | PlasmaQuick::AppletQuickItem *m_containmentItem = nullptr; | ||||
185 | Plasma::Containment *m_containment = nullptr; | ||||
186 | QQmlComponent *m_appletContainerComponent = nullptr; | ||||
187 | | ||||
188 | AbstractLayoutManager *m_layoutManager = nullptr; | ||||
189 | | ||||
190 | QPointer<ItemContainer> m_placeHolder; | ||||
191 | | ||||
192 | QTimer *m_pressAndHoldTimer; | ||||
193 | QTimer *m_sizeSyncTimer; | ||||
194 | | ||||
195 | QJSValue m_acceptsAppletCallback; | ||||
196 | | ||||
197 | AppletsLayout::EditModeCondition m_editModeCondition = AppletsLayout::Manual; | ||||
198 | | ||||
199 | QHash <PlasmaQuick::AppletQuickItem *, AppletContainer*> m_containerForApplet; | ||||
200 | | ||||
201 | QSizeF m_minimumItemSize; | ||||
202 | QSizeF m_defaultItemSize; | ||||
203 | QSizeF m_savedSize; | ||||
204 | QRectF m_geometryBeforeResolutionChange; | ||||
205 | | ||||
206 | QPointF m_mouseDownPosition = QPoint(-1, -1); | ||||
207 | bool m_mouseDownWasEditMode = false; | ||||
208 | bool m_editMode = false; | ||||
209 | }; | ||||
210 | |