Debian Codegraph @

GUIWidgets/QtWidgets/QtTree.h

raw
1 /* Copyright (c) 2020, Dyssol Development Team. All rights reserved. This file is part of Dyssol. See LICENSE file for license information. */
2
3 #pragma once
4
5 #include <QCheckBox>
6 #include <QTreeWidget>
7 #include <QComboBox>
8
9 namespace QtTreeUtils
10 {
11 enum class EFlag : uint8_t
12 {
13 DEFAULT = 1 << 0,
14 EDIT = 1 << 1,
15 NO_EDIT = 1 << 2,
16 SELECT = 1 << 3,
17 NO_SELECT = 1 << 4,
18 ENABLED = 1 << 5,
19 DISABLED = 1 << 6,
20 };
21
22 constexpr EFlag operator|(EFlag _lhs, EFlag _rhs)
23 {
24 using type = std::underlying_type_t<EFlag>;
25 return static_cast<EFlag>(static_cast<type>(_lhs) | static_cast<type>(_rhs));
26 }
27
28 constexpr EFlag operator&(EFlag _lhs, EFlag _rhs)
29 {
30 using type = std::underlying_type_t<EFlag>;
31 return static_cast<EFlag>(static_cast<type>(_lhs) & static_cast<type>(_rhs));
32 }
33
34 /**
35 * \brief Checks if the group of flags contains the target one.
36 * \param _group Group of flags to search in.
37 * \param _flag Target flag to search for.
38 * \return True if \p _group contains \p _flag.
39 */
40 constexpr bool Contains(EFlag _group, EFlag _flag)
41 {
42 return (_group & _flag) == _flag;
43 }
44 }
45
46 class CQtTree : public QTreeWidget
47 {
48 Q_OBJECT
49
50 public:
51 using EFlags = QtTreeUtils::EFlag;
52 inline static constexpr auto ItemSelectableEditable = (EFlags::EDIT | EFlags::SELECT);
53 inline static constexpr auto ItemSelectableNotEditable = (EFlags::NO_EDIT | EFlags::SELECT);
54 inline static constexpr auto ItemSelectableEnabledNotEditable = (EFlags::NO_EDIT | EFlags::SELECT | EFlags::ENABLED);
55 inline static constexpr auto ItemNotSelectabledNotEditable = (EFlags::NO_EDIT | EFlags::NO_SELECT);
56
57 CQtTree(QWidget* _parent = nullptr);
58
59 // Creates a new item as a child of the tree.
60 QTreeWidgetItem* CreateItem(EFlags _flags = EFlags::DEFAULT);
61 // Creates a new item as a child of the widget.
62 static QTreeWidgetItem* CreateItem(QTreeWidgetItem* _parent, EFlags _flags = EFlags::DEFAULT);
63 // Creates a new item as a child of the tree and sets the given text and data to the specified column.
64 QTreeWidgetItem* CreateItem(int _col, const std::string& _text, EFlags _flags = EFlags::DEFAULT, const QVariant& _data = -1);
65 // Creates a new item as a child of the tree and sets the given text and data to the specified column.
66 static QTreeWidgetItem* CreateItem(QTreeWidget* _parent, int _col, const std::string& _text, EFlags _flags = EFlags::DEFAULT, const QVariant& _data = -1);
67 // Creates a new item as a child of the widget and sets the given text and data to the specified column.
68 static QTreeWidgetItem* CreateItem(QTreeWidgetItem* _parent, int _col, const std::string& _text, EFlags _flags = EFlags::DEFAULT, const QVariant& _data = -1);
69 // Creates a new item as a child of the widget and sets the given text and data to the specified column.
70 static QTreeWidgetItem* CreateItem(QTreeWidgetItem* _parent, int _col, const std::string& _text, EFlags _flags = EFlags::DEFAULT, const std::string& _data = {});
71
72 // Sets new text and data to the selected column of the given existing item.
73 static void SetText(QTreeWidgetItem* _item, int _col, const std::string& _text, const QVariant& _data = -1);
74
75 // Adds a combo box to the selected column of the given existing item.
76 QComboBox* SetComboBox(QTreeWidgetItem* _item, int _col, const std::vector<QString>& _names, const std::vector<QVariant>& _data, const QVariant& _selected);
77 // Configures a combo box widget at the selected column of the given existing item.
78 void SetupComboBox(QTreeWidgetItem* _item, int _col, const std::vector<QString>& _names, const std::vector<QVariant>& _data, const QVariant& _selected) const;
79 // Configures a combo box widget at the selected column of the given existing item.
80 void SetupComboBox(QTreeWidgetItem* _item, int _col, const std::vector<std::string>& _names, const std::vector<std::string>& _data, const std::string& _selected) const;
81 // Sets a value of the combo box widget at the selected column of the given existing item.
82 void SetComboBoxValue(QTreeWidgetItem* _item, int _col, const QVariant& _value) const;
83 // Returns a value of the combo box widget at the selected column of the given existing item.
84 QVariant GetComboBoxValue(QTreeWidgetItem* _item, int _col) const;
85
86 // Adds a check box widget to the selected column of the given existing item.
87 QCheckBox* SetCheckBox(QTreeWidgetItem* _item, int _col, const QString& _text, bool _value);
88 // Sets a value of the check box widget at the selected column of the given existing item.
89 void SetCheckBoxValue(QTreeWidgetItem* _item, int _col, bool _value) const;
90 // Returns a value of the check box widget at the selected column of the given existing item.
91 bool GetCheckBoxValue(QTreeWidgetItem* _item, int _col) const;
92
93 // Sets flags to the given item.
94 static void SetItemFlags(QTreeWidgetItem* _item, EFlags _flags);
95
96 // Sets an item with the specified index as current.
97 void SetCurrentItem(const std::vector<size_t>& _indices);
98 // Sets an item with the specified user data as current.
99 void SetCurrentItem(const QVariant& _data);
100 // Returns an item with the specified user data.
101 QTreeWidgetItem* GetItem(const QVariant& _data) const;
102
103 // Returns user data of the given column in the specified item.
104 static QVariant GetData(const QTreeWidgetItem* _item, int _col = 0);
105 // Returns user data of the given column in the specified item.
106 static QString GetDataQStr(const QTreeWidgetItem* _item, int _col = 0);
107 // Returns user data of the given column in the specified item.
108 static std::string GetDataStr(const QTreeWidgetItem* _item, int _col = 0);
109 // Returns user data of the given column in current item.
110 QString GetCurrentDataQStr(int _col = 0) const;
111
112 /**
113 * \brief Check if one item is a successor of another.
114 * \param _parent Parent item.
115 * \param _child Testing child item.
116 * \return Whether child is a successor of the parent.
117 */
118 static bool IsSuccessor(const QTreeWidgetItem* _parent, const QTreeWidgetItem* _child);
119
120 bool blockSignals(bool _flag);
121 bool eventFilter(QObject* _object, QEvent* _event) override;
122
123 private:
124 // Creates a new item as a child of the widget.
125 template<typename T>
126 static QTreeWidgetItem* CreateItem(T* _parent, int _col, const std::string& _text, EFlags _flags = EFlags::DEFAULT, const QVariant& _data = -1);
127
128 signals:
129 void ComboBoxIndexChanged(QComboBox* _combo, QTreeWidgetItem* _item);
130 void CheckBoxStateChanged(QCheckBox* _check, QTreeWidgetItem* _item);
131 };