# HG changeset patch # User Teemu Piippo # Date 1521279185 -7200 # Node ID 66d2050d3bd9cb1ce11cf859eb0e5cf0a30bf8f2 # Parent 9c570a30c98a3146d29d5964e81b3f84f19ba56c Part history can now be edited 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(); diff -r 9c570a30c98a -r 66d2050d3bd9 src/headerhistorymodel.h --- a/src/headerhistorymodel.h Fri Mar 16 16:28:39 2018 +0200 +++ b/src/headerhistorymodel.h Sat Mar 17 11:33:05 2018 +0200 @@ -15,11 +15,22 @@ HeaderHistoryModel(LDHeader* header, QObject* parent); - int rowCount(const QModelIndex &parent = QModelIndex()) const override; - int columnCount(const QModelIndex &parent = QModelIndex()) const override; - QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; + int rowCount(const QModelIndex& parent = {}) const override; + int columnCount(const QModelIndex& parent = {}) const override; + QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override; + bool setData(const QModelIndex& index, const QVariant& value, int role) override; void setHeader(LDHeader* header); - QVariant headerData(int section, Qt::Orientation orientation, int role) const; + QVariant headerData(int section, Qt::Orientation orientation, int role) const override; + bool insertRows(int row, int count, const QModelIndex&) override; + bool removeRows(int row, int count, const QModelIndex&) override; + bool moveRows( + const QModelIndex&, + int sourceRow, + int count, + const QModelIndex&, + int destinationRow + ) override; + Qt::ItemFlags flags(const QModelIndex& index) const override; private: LDHeader* header; diff -r 9c570a30c98a -r 66d2050d3bd9 src/widgets/headeredit.cpp --- a/src/widgets/headeredit.cpp Fri Mar 16 16:28:39 2018 +0200 +++ b/src/widgets/headeredit.cpp Sat Mar 17 11:33:05 2018 +0200 @@ -117,9 +117,50 @@ } } ); + connect( + ui.historyNew, + &QPushButton::clicked, + [&]() + { + if (this->hasValidHeader()) + { + const QModelIndex index = this->ui.history->selectionModel()->currentIndex(); + int row; + + if (index.isValid()) + row = index.row() + 1; + else + row = this->headerHistoryModel->rowCount(); + + this->headerHistoryModel->insertRows(row, 1, {}); + } + } + ); + connect( + ui.historyDelete, + &QPushButton::clicked, + [&]() + { + const QModelIndex index = this->ui.history->selectionModel()->currentIndex(); + + if (this->hasValidHeader() and index.isValid()) + this->headerHistoryModel->removeRows(index.row(), 1, {}); + } + ); + connect(ui.historyMoveUp, &QPushButton::clicked, [&](){ this->moveRows(-1); }); + connect(ui.historyMoveDown, &QPushButton::clicked, [&](){ this->moveRows(+2); }); this->setEnabled(this->hasValidHeader()); } +void HeaderEdit::moveRows(int direction) +{ + if (this->hasValidHeader()) + { + const QModelIndex index = this->ui.history->selectionModel()->currentIndex(); + this->headerHistoryModel->moveRows({}, index.row(), 1, {}, index.row() + direction); + } +} + HeaderEdit::~HeaderEdit() { delete this->headerHistoryModel; diff -r 9c570a30c98a -r 66d2050d3bd9 src/widgets/headeredit.h --- a/src/widgets/headeredit.h Fri Mar 16 16:28:39 2018 +0200 +++ b/src/widgets/headeredit.h Sat Mar 17 11:33:05 2018 +0200 @@ -22,4 +22,6 @@ class Ui_HeaderEdit& ui; class HeaderHistoryModel* headerHistoryModel = nullptr; LDHeader* m_header = nullptr; + + void moveRows(int direction); }; diff -r 9c570a30c98a -r 66d2050d3bd9 src/widgets/headeredit.ui --- a/src/widgets/headeredit.ui Fri Mar 16 16:28:39 2018 +0200 +++ b/src/widgets/headeredit.ui Sat Mar 17 11:33:05 2018 +0200 @@ -17,7 +17,7 @@ - 2 + 0 @@ -234,9 +234,30 @@ History - + + + true + + + false + + + QAbstractItemView::InternalMove + + + Qt::MoveAction + + + true + + + QAbstractItemView::SingleSelection + + + QAbstractItemView::SelectRows + true @@ -245,12 +266,76 @@ + + + + + + + + + + :/icons/arrow-up.png:/icons/arrow-up.png + + + + + + + + + + + :/icons/file.png:/icons/file.png + + + + + + + + + + + :/icons/delete.png:/icons/delete.png + + + + + + + + + + + :/icons/arrow-down.png:/icons/arrow-down.png + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + - + + + +