src/headerhistorymodel.cpp

changeset 1292
66d2050d3bd9
parent 1291
9c570a30c98a
child 1307
adb9d32a1426
equal deleted inserted replaced
1291:9c570a30c98a 1292:66d2050d3bd9
1 #include "headerhistorymodel.h" 1 #include "headerhistorymodel.h"
2 #include "lddocument.h" 2 #include "lddocument.h"
3 #include "generics/migrate.h"
3 4
4 HeaderHistoryModel::HeaderHistoryModel(LDHeader* header, QObject* parent) : 5 HeaderHistoryModel::HeaderHistoryModel(LDHeader* header, QObject* parent) :
5 QAbstractTableModel {parent}, 6 QAbstractTableModel {parent},
6 header {header} {} 7 header {header} {}
7 8
16 return 3; 17 return 3;
17 } 18 }
18 19
19 QVariant HeaderHistoryModel::data(const QModelIndex& index, int role) const 20 QVariant HeaderHistoryModel::data(const QModelIndex& index, int role) const
20 { 21 {
21 if (this->header and role == Qt::DisplayRole) 22 if (this->header and (role == Qt::DisplayRole || role == Qt::EditRole))
22 { 23 {
23 const auto& entry = this->header->history[index.row()]; 24 const auto& entry = this->header->history[index.row()];
24 switch (static_cast<Column>(index.column())) 25 switch (static_cast<Column>(index.column()))
25 { 26 {
26 case DateColumn: 27 case DateColumn:
65 { 66 {
66 return {}; 67 return {};
67 } 68 }
68 } 69 }
69 70
71 Qt::ItemFlags HeaderHistoryModel::flags(const QModelIndex& index) const
72 {
73 Qt::ItemFlags flags = QAbstractTableModel::flags(index);
74
75 if (index.isValid())
76 flags |= Qt::ItemIsEditable;
77
78 return flags;
79 }
80
81 bool HeaderHistoryModel::setData(const QModelIndex& index, const QVariant& value, int role)
82 {
83 if (role == Qt::EditRole)
84 {
85 LDHeader::HistoryEntry& entry = this->header->history[index.row()];
86
87 switch (static_cast<Column>(index.column()))
88 {
89 case DateColumn:
90 entry.date = value.toDate();
91 return true;
92
93 case AuthorColumn:
94 entry.author = value.toString();
95 return true;
96
97 case DescriptionColumn:
98 entry.description = value.toString();
99 return true;
100
101 default:
102 return false;
103 }
104 }
105 else
106 {
107 return false;
108 }
109 }
110
111 bool HeaderHistoryModel::moveRows(
112 const QModelIndex&,
113 int sourceRow,
114 int count,
115 const QModelIndex&,
116 int destinationRow
117 ) {
118 int sourceRowLast = sourceRow + count - 1;
119 this->beginMoveRows({}, sourceRow, sourceRowLast, {}, destinationRow);
120 ::migrate(this->header->history, sourceRow, sourceRowLast, destinationRow);
121 this->endMoveRows();
122 return true;
123 }
124
125 bool HeaderHistoryModel::removeRows(int row, int count, const QModelIndex&)
126 {
127 if (row >= 0 and row + count - 1 < this->rowCount())
128 {
129 this->beginRemoveRows({}, row, row + count - 1);
130 this->header->history.remove(row, count);
131 this->endRemoveRows();
132 return true;
133 }
134 else
135 {
136 return false;
137 }
138 }
139
140 bool HeaderHistoryModel::insertRows(int startRow, int count, const QModelIndex&)
141 {
142 if (startRow >= 0 and startRow <= this->rowCount())
143 {
144 this->beginInsertRows({}, startRow, startRow + count - 1);
145
146 for (int row : range(startRow, startRow + 1, startRow + count - 1))
147 {
148 this->header->history.insert(row, {});
149 this->header->history[row].date = QDate::currentDate();
150 this->header->history[row].author = ::config->defaultUser();
151 }
152
153 this->endInsertRows();
154 return true;
155 }
156 else
157 {
158 return false;
159 }
160 }
161
70 void HeaderHistoryModel::setHeader(LDHeader* header) 162 void HeaderHistoryModel::setHeader(LDHeader* header)
71 { 163 {
72 emit layoutAboutToBeChanged(); 164 emit layoutAboutToBeChanged();
73 this->header = header; 165 this->header = header;
74 emit layoutChanged(); 166 emit layoutChanged();

mercurial