Sat, 24 Mar 2018 12:54:28 +0200
Happy new year 2018
1326 | 1 | /* |
2 | * LDForge: LDraw parts authoring CAD | |
3 | * Copyright (C) 2013 - 2018 Teemu Piippo | |
4 | * | |
5 | * This program is free software: you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License as published by | |
7 | * the Free Software Foundation, either version 3 of the License, or | |
8 | * (at your option) any later version. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
17 | */ | |
18 | ||
1291 | 19 | #include "headerhistorymodel.h" |
20 | #include "lddocument.h" | |
1292
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
21 | #include "generics/migrate.h" |
1291 | 22 | |
23 | HeaderHistoryModel::HeaderHistoryModel(LDHeader* header, QObject* parent) : | |
24 | QAbstractTableModel {parent}, | |
25 | header {header} {} | |
26 | ||
27 | int HeaderHistoryModel::rowCount(const QModelIndex&) const | |
28 | { | |
29 | if (this->header) | |
30 | return this->header->history.size(); | |
1307 | 31 | else |
32 | return 0; | |
1291 | 33 | } |
34 | ||
35 | int HeaderHistoryModel::columnCount(const QModelIndex&) const | |
36 | { | |
37 | return 3; | |
38 | } | |
39 | ||
40 | QVariant HeaderHistoryModel::data(const QModelIndex& index, int role) const | |
41 | { | |
1292
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
42 | if (this->header and (role == Qt::DisplayRole || role == Qt::EditRole)) |
1291 | 43 | { |
44 | const auto& entry = this->header->history[index.row()]; | |
45 | switch (static_cast<Column>(index.column())) | |
46 | { | |
47 | case DateColumn: | |
48 | return entry.date; | |
49 | ||
50 | case AuthorColumn: | |
51 | return entry.author; | |
52 | ||
53 | case DescriptionColumn: | |
54 | return entry.description; | |
55 | ||
56 | default: | |
57 | return {}; | |
58 | } | |
59 | } | |
60 | else | |
61 | { | |
62 | return {}; | |
63 | } | |
64 | } | |
65 | ||
66 | QVariant HeaderHistoryModel::headerData(int section, Qt::Orientation orientation, int role) const | |
67 | { | |
68 | if (orientation == Qt::Horizontal and role == Qt::DisplayRole) | |
69 | { | |
70 | switch (static_cast<Column>(section)) | |
71 | { | |
72 | case DateColumn: | |
73 | return tr("Date"); | |
74 | ||
75 | case AuthorColumn: | |
76 | return tr("Author"); | |
77 | ||
78 | case DescriptionColumn: | |
79 | return tr("Description"); | |
80 | ||
81 | default: | |
82 | return {}; | |
83 | } | |
84 | } | |
85 | else | |
86 | { | |
87 | return {}; | |
88 | } | |
89 | } | |
90 | ||
1292
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
91 | Qt::ItemFlags HeaderHistoryModel::flags(const QModelIndex& index) const |
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 | Qt::ItemFlags flags = QAbstractTableModel::flags(index); |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
94 | |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
95 | if (index.isValid()) |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
96 | flags |= Qt::ItemIsEditable; |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
97 | |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
98 | return flags; |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
99 | } |
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 | 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
|
102 | { |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
103 | if (role == Qt::EditRole) |
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 | LDHeader::HistoryEntry& entry = this->header->history[index.row()]; |
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 | switch (static_cast<Column>(index.column())) |
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 | case DateColumn: |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
110 | entry.date = value.toDate(); |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
111 | return true; |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
112 | |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
113 | case AuthorColumn: |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
114 | entry.author = value.toString(); |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
115 | return true; |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
116 | |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
117 | case DescriptionColumn: |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
118 | entry.description = value.toString(); |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
119 | return true; |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
120 | |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
121 | default: |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
122 | return false; |
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 | else |
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 | return false; |
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 | } |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
130 | |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
131 | bool HeaderHistoryModel::moveRows( |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
132 | const QModelIndex&, |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
133 | int sourceRow, |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
134 | int count, |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
135 | const QModelIndex&, |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
136 | int destinationRow |
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 | int sourceRowLast = sourceRow + count - 1; |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
139 | this->beginMoveRows({}, sourceRow, sourceRowLast, {}, destinationRow); |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
140 | ::migrate(this->header->history, sourceRow, sourceRowLast, destinationRow); |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
141 | this->endMoveRows(); |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
142 | return true; |
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 | |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
145 | 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
|
146 | { |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
147 | 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
|
148 | { |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
149 | this->beginRemoveRows({}, row, row + count - 1); |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
150 | this->header->history.remove(row, count); |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
151 | this->endRemoveRows(); |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
152 | return true; |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
153 | } |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
154 | else |
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 | return false; |
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 | } |
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 | 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
|
161 | { |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
162 | if (startRow >= 0 and startRow <= this->rowCount()) |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
163 | { |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
164 | this->beginInsertRows({}, startRow, startRow + count - 1); |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
165 | |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
166 | 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
|
167 | { |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
168 | this->header->history.insert(row, {}); |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
169 | this->header->history[row].date = QDate::currentDate(); |
1323
05b3e173c900
Config is now a namespace
Teemu Piippo <teemu@hecknology.net>
parents:
1307
diff
changeset
|
170 | this->header->history[row].author = config::defaultUser(); |
1292
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
171 | } |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
172 | |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
173 | this->endInsertRows(); |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
174 | return true; |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
175 | } |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
176 | else |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
177 | { |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
178 | return false; |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
179 | } |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
180 | } |
66d2050d3bd9
Part history can now be edited
Teemu Piippo <teemu@hecknology.net>
parents:
1291
diff
changeset
|
181 | |
1291 | 182 | void HeaderHistoryModel::setHeader(LDHeader* header) |
183 | { | |
184 | emit layoutAboutToBeChanged(); | |
185 | this->header = header; | |
186 | emit layoutChanged(); | |
187 | } |