Part history can now be edited

Sat, 17 Mar 2018 11:33:05 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Sat, 17 Mar 2018 11:33:05 +0200
changeset 1292
66d2050d3bd9
parent 1291
9c570a30c98a
child 1293
52732b1db3f0

Part history can now be edited

src/headerhistorymodel.cpp file | annotate | diff | comparison | revisions
src/headerhistorymodel.h file | annotate | diff | comparison | revisions
src/widgets/headeredit.cpp file | annotate | diff | comparison | revisions
src/widgets/headeredit.h file | annotate | diff | comparison | revisions
src/widgets/headeredit.ui file | annotate | diff | comparison | revisions
--- 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<Column>(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<Column>(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();
--- 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;
--- 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;
--- 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);
 };
--- 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 @@
    <item>
     <widget class="QTabWidget" name="tabWidget">
      <property name="currentIndex">
-      <number>2</number>
+      <number>0</number>
      </property>
      <widget class="QWidget" name="tab">
       <attribute name="title">
@@ -234,9 +234,30 @@
       <attribute name="title">
        <string>History</string>
       </attribute>
-      <layout class="QVBoxLayout" name="verticalLayout_6">
+      <layout class="QHBoxLayout" name="horizontalLayout">
        <item>
         <widget class="QTableView" name="history">
+         <property name="dragEnabled">
+          <bool>true</bool>
+         </property>
+         <property name="dragDropOverwriteMode">
+          <bool>false</bool>
+         </property>
+         <property name="dragDropMode">
+          <enum>QAbstractItemView::InternalMove</enum>
+         </property>
+         <property name="defaultDropAction">
+          <enum>Qt::MoveAction</enum>
+         </property>
+         <property name="alternatingRowColors">
+          <bool>true</bool>
+         </property>
+         <property name="selectionMode">
+          <enum>QAbstractItemView::SingleSelection</enum>
+         </property>
+         <property name="selectionBehavior">
+          <enum>QAbstractItemView::SelectRows</enum>
+         </property>
          <attribute name="horizontalHeaderStretchLastSection">
           <bool>true</bool>
          </attribute>
@@ -245,12 +266,76 @@
          </attribute>
         </widget>
        </item>
+       <item>
+        <layout class="QVBoxLayout" name="verticalLayout_6">
+         <item>
+          <widget class="QPushButton" name="historyMoveUp">
+           <property name="text">
+            <string/>
+           </property>
+           <property name="icon">
+            <iconset resource="../../ldforge.qrc">
+             <normaloff>:/icons/arrow-up.png</normaloff>:/icons/arrow-up.png</iconset>
+           </property>
+          </widget>
+         </item>
+         <item>
+          <widget class="QPushButton" name="historyNew">
+           <property name="text">
+            <string/>
+           </property>
+           <property name="icon">
+            <iconset resource="../../ldforge.qrc">
+             <normaloff>:/icons/file.png</normaloff>:/icons/file.png</iconset>
+           </property>
+          </widget>
+         </item>
+         <item>
+          <widget class="QPushButton" name="historyDelete">
+           <property name="text">
+            <string/>
+           </property>
+           <property name="icon">
+            <iconset resource="../../ldforge.qrc">
+             <normaloff>:/icons/delete.png</normaloff>:/icons/delete.png</iconset>
+           </property>
+          </widget>
+         </item>
+         <item>
+          <widget class="QPushButton" name="historyMoveDown">
+           <property name="text">
+            <string/>
+           </property>
+           <property name="icon">
+            <iconset resource="../../ldforge.qrc">
+             <normaloff>:/icons/arrow-down.png</normaloff>:/icons/arrow-down.png</iconset>
+           </property>
+          </widget>
+         </item>
+         <item>
+          <spacer name="verticalSpacer">
+           <property name="orientation">
+            <enum>Qt::Vertical</enum>
+           </property>
+           <property name="sizeHint" stdset="0">
+            <size>
+             <width>20</width>
+             <height>40</height>
+            </size>
+           </property>
+          </spacer>
+         </item>
+        </layout>
+       </item>
       </layout>
      </widget>
     </widget>
    </item>
   </layout>
  </widget>
- <resources/>
+ <resources>
+  <include location="../../ldforge.qrc"/>
+  <include location="../../ldforge.qrc"/>
+ </resources>
  <connections/>
 </ui>

mercurial