-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathgridmodel.cpp
More file actions
74 lines (60 loc) · 1.67 KB
/
gridmodel.cpp
File metadata and controls
74 lines (60 loc) · 1.67 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
#include "gridmodel.h"
class GridModel::GridModelPrivate
{
public:
explicit GridModelPrivate(GridModel *parent)
: q_ptr(parent)
{}
GridModel *q_ptr;
GridCellList cellList;
int cellWidth = GRID_CELL_SIZE;
int cellHeight = GRID_CELL_SIZE;
};
GridModel::GridModel(QObject *parent)
: QAbstractListModel(parent)
, d_ptr(new GridModelPrivate(this))
{}
GridModel::~GridModel() = default;
int GridModel::rowCount(const QModelIndex &parent) const
{
return parent.isValid() ? 0 : d_ptr->cellList.size();
}
QVariant GridModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid() || index.row() < 0 || index.row() >= d_ptr->cellList.size()) {
return {};
}
const auto &cell = d_ptr->cellList.at(index.row());
switch (role) {
case Qt::DecorationRole: return cell.image;
case Qt::ToolTipRole: return cell.label;
case Qt::SizeHintRole: return QSize{d_ptr->cellWidth, d_ptr->cellHeight};
case Qt::TextAlignmentRole: return Qt::AlignCenter;
default: break;
}
return {};
}
void GridModel::setCellList(const GridCellList &cellList)
{
beginResetModel();
d_ptr->cellList = cellList;
endResetModel();
}
void GridModel::clearCells()
{
beginResetModel();
d_ptr->cellList.clear();
endResetModel();
}
void GridModel::setCellSize(int width, int height)
{
if (d_ptr->cellWidth != width || d_ptr->cellHeight != height) {
d_ptr->cellWidth = width;
d_ptr->cellHeight = height;
emit dataChanged(createIndex(0, 0), createIndex(rowCount() - 1, 0), {Qt::SizeHintRole});
}
}
QSize GridModel::cellSize() const
{
return QSize{d_ptr->cellWidth, d_ptr->cellHeight};
}