diff -r 9c570a30c98a -r 66d2050d3bd9 src/headerhistorymodel.cpp --- a/src/headerhistorymodel.cpp Fri Mar 16 16:28:39 2018 +0200 +++ b/src/headerhistorymodel.cpp Sat Mar 17 11:33:05 2018 +0200 @@ -1,5 +1,6 @@ #include "headerhistorymodel.h" #include "lddocument.h" +#include "generics/migrate.h" HeaderHistoryModel::HeaderHistoryModel(LDHeader* header, QObject* parent) : QAbstractTableModel {parent}, @@ -18,7 +19,7 @@ QVariant HeaderHistoryModel::data(const QModelIndex& index, int role) const { - if (this->header and role == Qt::DisplayRole) + if (this->header and (role == Qt::DisplayRole || role == Qt::EditRole)) { const auto& entry = this->header->history[index.row()]; switch (static_cast(index.column())) @@ -67,6 +68,97 @@ } } +Qt::ItemFlags HeaderHistoryModel::flags(const QModelIndex& index) const +{ + Qt::ItemFlags flags = QAbstractTableModel::flags(index); + + if (index.isValid()) + flags |= Qt::ItemIsEditable; + + return flags; +} + +bool HeaderHistoryModel::setData(const QModelIndex& index, const QVariant& value, int role) +{ + if (role == Qt::EditRole) + { + LDHeader::HistoryEntry& entry = this->header->history[index.row()]; + + switch (static_cast(index.column())) + { + case DateColumn: + entry.date = value.toDate(); + return true; + + case AuthorColumn: + entry.author = value.toString(); + return true; + + case DescriptionColumn: + entry.description = value.toString(); + return true; + + default: + return false; + } + } + else + { + return false; + } +} + +bool HeaderHistoryModel::moveRows( + const QModelIndex&, + int sourceRow, + int count, + const QModelIndex&, + int destinationRow +) { + int sourceRowLast = sourceRow + count - 1; + this->beginMoveRows({}, sourceRow, sourceRowLast, {}, destinationRow); + ::migrate(this->header->history, sourceRow, sourceRowLast, destinationRow); + this->endMoveRows(); + return true; +} + +bool HeaderHistoryModel::removeRows(int row, int count, const QModelIndex&) +{ + if (row >= 0 and row + count - 1 < this->rowCount()) + { + this->beginRemoveRows({}, row, row + count - 1); + this->header->history.remove(row, count); + this->endRemoveRows(); + return true; + } + else + { + return false; + } +} + +bool HeaderHistoryModel::insertRows(int startRow, int count, const QModelIndex&) +{ + if (startRow >= 0 and startRow <= this->rowCount()) + { + this->beginInsertRows({}, startRow, startRow + count - 1); + + for (int row : range(startRow, startRow + 1, startRow + count - 1)) + { + this->header->history.insert(row, {}); + this->header->history[row].date = QDate::currentDate(); + this->header->history[row].author = ::config->defaultUser(); + } + + this->endInsertRows(); + return true; + } + else + { + return false; + } +} + void HeaderHistoryModel::setHeader(LDHeader* header) { emit layoutAboutToBeChanged();