QmlMaterial 0.1.0
Loading...
Searching...
No Matches
dialog_button_box.hpp
1#pragma once
2
3#include <QQmlComponent>
4#include <qqml.h>
5#include "qml_material/control/panel.hpp"
6#include "qml_material/control/abstract_button.hpp"
7
8namespace qml_material
9{
10class DialogButtonBoxAttached;
11class QML_MATERIAL_API DialogButtonBox : public Panel {
12 Q_OBJECT
13 QML_NAMED_ELEMENT(DialogButtonBoxBase)
14 QML_ATTACHED(DialogButtonBoxAttached)
15 Q_PROPERTY(int standardButtons READ standardButtons WRITE setStandardButtons NOTIFY
16 standardButtonsChanged FINAL)
17 Q_PROPERTY(QQmlComponent* delegate READ delegate WRITE setDelegate NOTIFY delegateChanged FINAL)
18 Q_PROPERTY(int count READ count NOTIFY countChanged FINAL)
19 Q_PROPERTY(
20 Qt::Alignment alignment READ alignment WRITE setAlignment NOTIFY alignmentChanged FINAL)
21public:
22 enum StandardButton
23 {
24 NoButton = 0,
25 Ok = 0x400,
26 Save = 0x800,
27 SaveAll = 0x1000,
28 Open = 0x2000,
29 Yes = 0x4000,
30 YesToAll = 0x8000,
31 No = 0x10000,
32 NoToAll = 0x20000,
33 Abort = 0x40000,
34 Retry = 0x80000,
35 Ignore = 0x100000,
36 Close = 0x200000,
37 Cancel = 0x400000,
38 Discard = 0x800000,
39 Help = 0x1000000,
40 Apply = 0x2000000,
41 Reset = 0x4000000,
42 RestoreDefaults = 0x8000000
43 };
44 Q_ENUM(StandardButton)
45 enum ButtonRole
46 {
47 InvalidRole = -1,
48 AcceptRole,
49 RejectRole,
50 DestructiveRole,
51 ActionRole,
52 HelpRole,
53 YesRole,
54 NoRole,
55 ResetRole,
56 ApplyRole
57 };
58 Q_ENUM(ButtonRole)
59 explicit DialogButtonBox(QQuickItem* parent = nullptr);
60 ~DialogButtonBox() override;
61 static DialogButtonBoxAttached* qmlAttachedProperties(QObject*);
62 int standardButtons() const { return m_standard_buttons; }
63 void setStandardButtons(int);
64 QQmlComponent* delegate() const { return m_delegate; }
65 void setDelegate(QQmlComponent*);
66 int count() const { return m_buttons.size(); }
67 Qt::Alignment alignment() const { return m_alignment; }
68 void setAlignment(Qt::Alignment);
69 Q_INVOKABLE AbstractButton* standardButton(int) const;
70 Q_INVOKABLE AbstractButton* itemAt(int) const;
71 Q_INVOKABLE void addItem(AbstractButton*);
72 Q_INVOKABLE void removeItem(AbstractButton*);
73 Q_SIGNAL void standardButtonsChanged();
74 Q_SIGNAL void delegateChanged();
75 Q_SIGNAL void countChanged();
76 Q_SIGNAL void alignmentChanged();
77 Q_SIGNAL void clicked(AbstractButton*);
78 Q_SIGNAL void accepted();
79 Q_SIGNAL void rejected();
80 Q_SIGNAL void applied();
81 Q_SIGNAL void reset();
82 Q_SIGNAL void discarded();
83 Q_SIGNAL void helpRequested();
84
85protected:
86 void componentComplete() override;
87 void updatePolish() override;
88
89private:
90 void rebuild();
91 void observe();
92 void activate(AbstractButton*);
93 QPointer<QQmlComponent> m_delegate;
94 QHash<int, QPointer<AbstractButton>> m_standard_items;
95 QList<QPointer<AbstractButton>> m_buttons;
96 QList<QMetaObject::Connection> m_connections;
97 int m_standard_buttons = NoButton;
98 Qt::Alignment m_alignment = Qt::AlignRight;
99 bool m_complete = false;
100 bool m_rebuilding = false;
101 quint64 m_observation = 0;
102};
103class QML_MATERIAL_API DialogButtonBoxAttached : public QObject {
104 Q_OBJECT
105 Q_PROPERTY(DialogButtonBox* buttonBox READ buttonBox NOTIFY buttonBoxChanged FINAL)
106 Q_PROPERTY(DialogButtonBox::ButtonRole buttonRole READ buttonRole WRITE setButtonRole NOTIFY
107 buttonRoleChanged FINAL)
108public:
109 explicit DialogButtonBoxAttached(QObject* parent): QObject(parent) {}
110 DialogButtonBox* buttonBox() const { return m_box; }
111 DialogButtonBox::ButtonRole buttonRole() const { return m_role; }
112 void setButtonRole(DialogButtonBox::ButtonRole role) {
113 if (m_role == role) return;
114 m_role = role;
115 Q_EMIT buttonRoleChanged();
116 }
117 Q_SIGNAL void buttonBoxChanged();
118 Q_SIGNAL void buttonRoleChanged();
119
120private:
121 friend class DialogButtonBox;
122 QPointer<DialogButtonBox> m_box;
123 DialogButtonBox::ButtonRole m_role = DialogButtonBox::InvalidRole;
124};
125} // namespace qml_material
126QML_DECLARE_TYPEINFO(qml_material::DialogButtonBox, QML_HAS_ATTACHED_PROPERTIES)