1/*
2 * SPDX-FileCopyrightText: 2020 Arjen Hiemstra <ahiemstra@heimr.nl>
3 *
4 * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
5 */
6
7#ifndef TOOLBARLAYOUTDELEGATE_H
8#define TOOLBARLAYOUTDELEGATE_H
9
10#include "enums.h"
11#include <QQmlIncubator>
12#include <QQuickItem>
13
14class ToolBarLayout;
15
16/*
17 * A helper subclass of QQmlIncubator that allows us to do some things more
18 * easily.
19 */
20class ToolBarDelegateIncubator : public QQmlIncubator
21{
22public:
23 ToolBarDelegateIncubator(QQmlComponent *component, QQmlContext *context);
24
25 void setStateCallback(std::function<void(QQuickItem *)> callback);
26 void setCompletedCallback(std::function<void(ToolBarDelegateIncubator *)> callback);
27
28 void create();
29
30 bool isFinished();
31
32private:
33 void setInitialState(QObject *object) override;
34 void statusChanged(QQmlIncubator::Status status) override;
35
36 QQmlComponent *m_component;
37 QQmlContext *m_context;
38 std::function<void(QQuickItem *)> m_stateCallback;
39 std::function<void(ToolBarDelegateIncubator *)> m_completedCallback;
40 bool m_finished = false;
41};
42
43/*
44 * A helper class to encapsulate some of the delegate functionality used by
45 * ToolBarLayout. Primarily, this hides some of the difference that delegates
46 * are two items instead of one.
47 */
48class ToolBarLayoutDelegate : public QObject
49{
50 Q_OBJECT
51public:
52 ToolBarLayoutDelegate(ToolBarLayout *parent);
53 ~ToolBarLayoutDelegate() override;
54
55 QObject *action() const;
56 void setAction(QObject *action);
57 void createItems(QQmlComponent *fullComponent, QQmlComponent *iconComponent, std::function<void(QQuickItem *)> callback);
58
59 bool isReady() const;
60 bool isActionVisible() const;
61 bool isHidden() const;
62 bool isIconOnly() const;
63 bool isKeepVisible() const;
64
65 bool isVisible() const;
66
67 void hide();
68 void showIcon();
69 void showFull();
70 void show();
71
72 void setPosition(qreal x, qreal y);
73 void setHeight(qreal height);
74 void resetHeight();
75
76 qreal width() const;
77 qreal height() const;
78 qreal implicitWidth() const;
79 qreal implicitHeight() const;
80 qreal maxHeight() const;
81 qreal iconWidth() const;
82 qreal fullWidth() const;
83
84private:
85 Q_SLOT void actionVisibleChanged();
86 Q_SLOT void displayHintChanged();
87 inline void ensureItemVisibility()
88 {
89 if (m_full) {
90 m_full->setVisible(m_fullVisible);
91 }
92 if (m_icon) {
93 m_icon->setVisible(m_iconVisible);
94 }
95 }
96 void cleanupIncubators();
97 void triggerRelayout();
98
99 ToolBarLayout *m_parent = nullptr;
100 QObject *m_action = nullptr;
101 QQuickItem *m_full = nullptr;
102 QQuickItem *m_icon = nullptr;
103 ToolBarDelegateIncubator *m_fullIncubator = nullptr;
104 ToolBarDelegateIncubator *m_iconIncubator = nullptr;
105
106 DisplayHint::DisplayHints m_displayHint = DisplayHint::NoPreference;
107 bool m_ready = false;
108 bool m_actionVisible = true;
109 bool m_fullVisible = false;
110 bool m_iconVisible = false;
111};
112
113#endif // TOOLBARLAYOUTDELEGATE_H
114

source code of kirigami/src/toolbarlayoutdelegate.h