QmlMaterial 0.1.0
Loading...
Searching...
No Matches
snake_model.hpp
1#pragma once
2
3#include <QtCore/QAbstractListModel>
4#include <QtCore/QTimer>
5#include <QtQml/QQmlEngine>
6
7#include "qml_material/core/enum.hpp"
8
9namespace qml_material
10{
11struct SnakeModelItem {
12 Q_GADGET
13
14 Q_PROPERTY(qint32 sid MEMBER id FINAL)
15 Q_PROPERTY(QString text MEMBER text FINAL)
16 Q_PROPERTY(qint32 duration MEMBER duration FINAL)
17 Q_PROPERTY(Enum::ToastFlags flag MEMBER flag FINAL)
18 Q_PROPERTY(QObject* action MEMBER action FINAL)
19 Q_PROPERTY(qreal maximumWidth MEMBER maximum_width FINAL)
20 Q_PROPERTY(bool actionOnNewLine MEMBER action_on_new_line FINAL)
21public:
22 qint32 id { 0 };
23 QString text {};
24 qint32 duration { 0 };
25 Enum::ToastFlags flag {};
26 QObject* action { nullptr };
27 qreal maximum_width { 0 };
28 bool action_on_new_line { false };
29};
30
31class SnakeModel : public QAbstractListModel {
32 Q_OBJECT
33 QML_ELEMENT
34public:
35 using iterator = std::vector<SnakeModelItem>::iterator;
36
37 SnakeModel(QObject* parent = nullptr);
38 virtual ~SnakeModel();
39
40 int rowCount(const QModelIndex& parent = QModelIndex()) const override;
41 QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
42 QHash<int, QByteArray> roleNames() const override;
43
44 Q_INVOKABLE SnakeModelItem createSnake();
45
46 bool removeRows(int row, int count, const QModelIndex& parent = QModelIndex()) override;
47 auto findById(qint32 id) -> iterator;
48
49 Q_SLOT void showSnake(const SnakeModelItem&);
50 Q_SLOT void removeById(qint32 id);
51
52private:
53 Q_SLOT void onRemovePriv(const QModelIndex& parent, int first, int last);
54 qint32 removeExtra(qint32 id);
55
56 std::int32_t m_snake_id;
57 std::vector<SnakeModelItem> m_items;
58 std::vector<SnakeModelItem> m_saved_items;
59
60 struct Extra {
61 QTimer* timer { nullptr };
62 };
63 std::unordered_map<qint32, Extra> m_extra;
64};
65} // namespace qml_material