blob: dc5f67729a2ec4c436a151a21f209cf164d53876 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
// Copyright (C) 2018 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
#ifndef GAMEOFLIFEMODEL_H
#define GAMEOFLIFEMODEL_H
#include <array>
#include <QAbstractTableModel>
#include <QPoint>
#include <QtQml/qqml.h>
//! [modelclass]
class GameOfLifeModel : public QAbstractTableModel
{
Q_OBJECT
QML_ELEMENT
Q_ENUMS(Roles)
public:
enum Roles {
CellRole
};
QHash<int, QByteArray> roleNames() const override {
return {
{ CellRole, "value" }
};
}
explicit GameOfLifeModel(QObject *parent = nullptr);
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
int columnCount(const QModelIndex &parent = QModelIndex()) const override;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
bool setData(const QModelIndex &index, const QVariant &value,
int role = Qt::EditRole) override;
Qt::ItemFlags flags(const QModelIndex &index) const override;
Q_INVOKABLE void nextStep();
Q_INVOKABLE bool loadFile(const QString &fileName);
Q_INVOKABLE void loadPattern(const QString &plainText);
Q_INVOKABLE void clear();
private:
static constexpr int width = 256;
static constexpr int height = 256;
static constexpr int size = width * height;
using StateContainer = std::array<bool, size>;
StateContainer m_currentState;
int cellNeighborsCount(const QPoint &cellCoordinates) const;
static bool areCellCoordinatesValid(const QPoint &coordinates);
static QPoint cellCoordinatesFromIndex(int cellIndex);
static std::size_t cellIndex(const QPoint &coordinates);
};
//! [modelclass]
#endif // GAMEOFLIFEMODEL_H
|