File indexing completed on 2024-04-14 03:53:49
0001 /* 0002 * SPDX-FileCopyrightText: 2020 Arjen Hiemstra <ahiemstra@heimr.nl> 0003 * 0004 * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL 0005 */ 0006 0007 #ifndef TOOLBARLAYOUTDELEGATE_H 0008 #define TOOLBARLAYOUTDELEGATE_H 0009 0010 #include "enums.h" 0011 #include <QQmlIncubator> 0012 #include <QQuickItem> 0013 0014 class ToolBarLayout; 0015 0016 /* 0017 * A helper subclass of QQmlIncubator that allows us to do some things more 0018 * easily. 0019 */ 0020 class ToolBarDelegateIncubator : public QQmlIncubator 0021 { 0022 public: 0023 ToolBarDelegateIncubator(QQmlComponent *component, QQmlContext *context); 0024 0025 void setStateCallback(std::function<void(QQuickItem *)> callback); 0026 void setCompletedCallback(std::function<void(ToolBarDelegateIncubator *)> callback); 0027 0028 void create(); 0029 0030 bool isFinished(); 0031 0032 private: 0033 void setInitialState(QObject *object) override; 0034 void statusChanged(QQmlIncubator::Status status) override; 0035 0036 QQmlComponent *m_component; 0037 QQmlContext *m_context; 0038 std::function<void(QQuickItem *)> m_stateCallback; 0039 std::function<void(ToolBarDelegateIncubator *)> m_completedCallback; 0040 bool m_finished = false; 0041 }; 0042 0043 /* 0044 * A helper class to encapsulate some of the delegate functionality used by 0045 * ToolBarLayout. Primarily, this hides some of the difference that delegates 0046 * are two items instead of one. 0047 */ 0048 class ToolBarLayoutDelegate : public QObject 0049 { 0050 Q_OBJECT 0051 public: 0052 ToolBarLayoutDelegate(ToolBarLayout *parent); 0053 ~ToolBarLayoutDelegate() override; 0054 0055 QObject *action() const; 0056 void setAction(QObject *action); 0057 void createItems(QQmlComponent *fullComponent, QQmlComponent *iconComponent, std::function<void(QQuickItem *)> callback); 0058 0059 bool isReady() const; 0060 bool isActionVisible() const; 0061 bool isHidden() const; 0062 bool isIconOnly() const; 0063 bool isKeepVisible() const; 0064 0065 bool isVisible() const; 0066 0067 void hide(); 0068 void showIcon(); 0069 void showFull(); 0070 void show(); 0071 0072 void setPosition(qreal x, qreal y); 0073 void setHeight(qreal height); 0074 void resetHeight(); 0075 0076 qreal width() const; 0077 qreal height() const; 0078 qreal implicitWidth() const; 0079 qreal implicitHeight() const; 0080 qreal maxHeight() const; 0081 qreal iconWidth() const; 0082 qreal fullWidth() const; 0083 0084 private: 0085 Q_SLOT void actionVisibleChanged(); 0086 Q_SLOT void displayHintChanged(); 0087 inline void ensureItemVisibility() 0088 { 0089 if (m_full) { 0090 m_full->setVisible(m_fullVisible); 0091 } 0092 if (m_icon) { 0093 m_icon->setVisible(m_iconVisible); 0094 } 0095 } 0096 void cleanupIncubators(); 0097 void triggerRelayout(); 0098 0099 ToolBarLayout *m_parent = nullptr; 0100 QObject *m_action = nullptr; 0101 QQuickItem *m_full = nullptr; 0102 QQuickItem *m_icon = nullptr; 0103 ToolBarDelegateIncubator *m_fullIncubator = nullptr; 0104 ToolBarDelegateIncubator *m_iconIncubator = nullptr; 0105 0106 DisplayHint::DisplayHints m_displayHint = DisplayHint::NoPreference; 0107 bool m_ready = false; 0108 bool m_actionVisible = true; 0109 bool m_fullVisible = false; 0110 bool m_iconVisible = false; 0111 }; 0112 0113 #endif // TOOLBARLAYOUTDELEGATE_H