Mon, 19 Mar 2018 12:44:27 +0200
red/green view rework complete
1291 | 1 | #include "headerhistorymodel.h" |
2 | #include "lddocument.h" | |
1292
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
3 | #include "generics/migrate.h" |
1291 | 4 | |
5 | HeaderHistoryModel::HeaderHistoryModel(LDHeader* header, QObject* parent) : | |
6 | QAbstractTableModel {parent}, | |
7 | header {header} {} | |
8 | ||
9 | int HeaderHistoryModel::rowCount(const QModelIndex&) const | |
10 | { | |
11 | if (this->header) | |
12 | return this->header->history.size(); | |
13 | } | |
14 | ||
15 | int HeaderHistoryModel::columnCount(const QModelIndex&) const | |
16 | { | |
17 | return 3; | |
18 | } | |
19 | ||
20 | QVariant HeaderHistoryModel::data(const QModelIndex& index, int role) const | |
21 | { | |
1292
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
22 | if (this->header and (role == Qt::DisplayRole || role == Qt::EditRole)) |
1291 | 23 | { |
24 | const auto& entry = this->header->history[index.row()]; | |
25 | switch (static_cast<Column>(index.column())) | |
26 | { | |
27 | case DateColumn: | |
28 | return entry.date; | |
29 | ||
30 | case AuthorColumn: | |
31 | return entry.author; | |
32 | ||
33 | case DescriptionColumn: | |
34 | return entry.description; | |
35 | ||
36 | default: | |
37 | return {}; | |
38 | } | |
39 | } | |
40 | else | |
41 | { | |
42 | return {}; | |
43 | } | |
44 | } | |
45 | ||
46 | QVariant HeaderHistoryModel::headerData(int section, Qt::Orientation orientation, int role) const | |
47 | { | |
48 | if (orientation == Qt::Horizontal and role == Qt::DisplayRole) | |
49 | { | |
50 | switch (static_cast<Column>(section)) | |
51 | { | |
52 | case DateColumn: | |
53 | return tr("Date"); | |
54 | ||
55 | case AuthorColumn: | |
56 | return tr("Author"); | |
57 | ||
58 | case DescriptionColumn: | |
59 | return tr("Description"); | |
60 | ||
61 | default: | |
62 | return {}; | |
63 | } | |
64 | } | |
65 | else | |
66 | { | |
67 | return {}; | |
68 | } | |
69 | } | |
70 | ||
1292
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
71 | Qt::ItemFlags HeaderHistoryModel::flags(const QModelIndex& index) const |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
72 | { |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
73 | Qt::ItemFlags flags = QAbstractTableModel::flags(index); |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
74 | |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
75 | if (index.isValid()) |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
76 | flags |= Qt::ItemIsEditable; |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
77 | |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
78 | return flags; |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
79 | } |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
80 | |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
81 | bool HeaderHistoryModel::setData(const QModelIndex& index, const QVariant& value, int role) |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
82 | { |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
83 | if (role == Qt::EditRole) |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
84 | { |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
85 | LDHeader::HistoryEntry& entry = this->header->history[index.row()]; |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
86 | |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
87 | switch (static_cast<Column>(index.column())) |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
88 | { |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
89 | case DateColumn: |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
90 | entry.date = value.toDate(); |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
91 | return true; |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
92 | |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
93 | case AuthorColumn: |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
94 | entry.author = value.toString(); |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
95 | return true; |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
96 | |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
97 | case DescriptionColumn: |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
98 | entry.description = value.toString(); |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
99 | return true; |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
100 | |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
101 | default: |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
102 | return false; |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
103 | } |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
104 | } |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
105 | else |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
106 | { |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
107 | return false; |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
108 | } |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
109 | } |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
110 | |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
111 | bool HeaderHistoryModel::moveRows( |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
112 | const QModelIndex&, |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
113 | int sourceRow, |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
114 | int count, |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
115 | const QModelIndex&, |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
116 | int destinationRow |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
117 | ) { |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
118 | int sourceRowLast = sourceRow + count - 1; |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
119 | this->beginMoveRows({}, sourceRow, sourceRowLast, {}, destinationRow); |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
120 | ::migrate(this->header->history, sourceRow, sourceRowLast, destinationRow); |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
121 | this->endMoveRows(); |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
122 | return true; |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
123 | } |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
124 | |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
125 | bool HeaderHistoryModel::removeRows(int row, int count, const QModelIndex&) |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
126 | { |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
127 | if (row >= 0 and row + count - 1 < this->rowCount()) |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
128 | { |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
129 | this->beginRemoveRows({}, row, row + count - 1); |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
130 | this->header->history.remove(row, count); |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
131 | this->endRemoveRows(); |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
132 | return true; |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
133 | } |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
134 | else |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
135 | { |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
136 | return false; |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
137 | } |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
138 | } |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
139 | |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
140 | bool HeaderHistoryModel::insertRows(int startRow, int count, const QModelIndex&) |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
141 | { |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
142 | if (startRow >= 0 and startRow <= this->rowCount()) |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
143 | { |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
144 | this->beginInsertRows({}, startRow, startRow + count - 1); |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
145 | |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
146 | for (int row : range(startRow, startRow + 1, startRow + count - 1)) |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
147 | { |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
148 | this->header->history.insert(row, {}); |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
149 | this->header->history[row].date = QDate::currentDate(); |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
150 | this->header->history[row].author = ::config->defaultUser(); |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
151 | } |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
152 | |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
153 | this->endInsertRows(); |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
154 | return true; |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
155 | } |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
156 | else |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
157 | { |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
158 | return false; |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
159 | } |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
160 | } |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
161 | |
1291 | 162 | void HeaderHistoryModel::setHeader(LDHeader* header) |
163 | { | |
164 | emit layoutAboutToBeChanged(); | |
165 | this->header = header; | |
166 | emit layoutChanged(); | |
167 | } |