src/model.h

changeset 1244
68e126e8c629
parent 1240
cebb7ef54f41
child 1245
338d66111168
equal deleted inserted replaced
1243:0fb1d3d17b60 1244:68e126e8c629
19 #pragma once 19 #pragma once
20 #include <QAbstractListModel> 20 #include <QAbstractListModel>
21 #include "main.h" 21 #include "main.h"
22 #include "linetypes/modelobject.h" 22 #include "linetypes/modelobject.h"
23 23
24 class IndexGenerator
25 {
26 class Iterator
27 {
28 public:
29 Iterator(const QAbstractListModel* model, int row) :
30 model {model},
31 row {row} {}
32
33 Iterator& operator++()
34 {
35 this->row += 1;
36 return *this;
37 }
38
39 QModelIndex operator*() const
40 {
41 return this->model->index(this->row);
42 }
43
44 bool operator!=(const Iterator& other)
45 {
46 return this->row != other.row or this->model != other.model;
47 }
48
49 private:
50 const QAbstractListModel* model;
51 int row;
52 };
53
54 public:
55 IndexGenerator(const QAbstractListModel* model) :
56 model {model} {}
57
58 Iterator begin() const
59 {
60 return {this->model, 0};
61 }
62
63 Iterator end() const
64 {
65 return {this->model, this->model->rowCount()};
66 }
67
68 private:
69 const QAbstractListModel* model;
70 };
71
24 /* 72 /*
25 * This class represents a LDraw model, consisting of a vector of objects. It manages LDObject ownership. 73 * This class represents a LDraw model, consisting of a vector of objects. It manages LDObject ownership.
26 */ 74 */
27 class Model : public QAbstractListModel 75 class Model : public QAbstractListModel
28 { 76 {
29 Q_OBJECT 77 Q_OBJECT
30 78
31 public: 79 public:
80 enum
81 {
82 ObjectRole = Qt::UserRole
83 };
84
32 Model(class DocumentManager* manager); 85 Model(class DocumentManager* manager);
33 Model(const Model& other) = delete; 86 Model(const Model& other) = delete;
34 ~Model(); 87 ~Model();
35 88
36 void addObject(LDObject* object); 89 void addObject(LDObject* object);
56 bool isEmpty() const; 109 bool isEmpty() const;
57 class DocumentManager* documentManager() const; 110 class DocumentManager* documentManager() const;
58 LDObject* insertFromString(int position, QString line); 111 LDObject* insertFromString(int position, QString line);
59 LDObject* addFromString(QString line); 112 LDObject* addFromString(QString line);
60 LDObject* replaceWithFromString(LDObject* object, QString line); 113 LDObject* replaceWithFromString(LDObject* object, QString line);
114 IndexGenerator indices() const;
61 115
62 int rowCount(const QModelIndex& parent) const override; 116 int rowCount(const QModelIndex& parent) const override;
63 QVariant data(const QModelIndex& index, int role) const override; 117 QVariant data(const QModelIndex& index, int role) const override;
118 bool removeRows(int row, int count, const QModelIndex& ) override;
64 119
65 signals: 120 signals:
66 void objectAdded(LDObject* object); 121 void objectAdded(LDObject* object);
67 void aboutToRemoveObject(LDObject* object); 122 void aboutToRemoveObject(LDObject* object);
68 void objectModified(LDObject* object); 123 void objectModified(LDObject* object);

mercurial