From 6ff83d602cac59295da73319b5365b78ebe4b65e Mon Sep 17 00:00:00 2001 From: Roy Qu Date: Sat, 3 Sep 2022 20:18:32 +0800 Subject: [PATCH] - enhancement: change orders of the problems in the problem set panel by drag&drop - enhancement: change orders of the problem cases in the problem panel by drag&drop --- NEWS.md | 2 + RedPandaIDE/mainwindow.ui | 23 +++++- RedPandaIDE/widgets/ojproblemsetmodel.cpp | 88 ++++++++++++++++++++++- RedPandaIDE/widgets/ojproblemsetmodel.h | 19 +++++ 4 files changed, 129 insertions(+), 3 deletions(-) diff --git a/NEWS.md b/NEWS.md index 510a8e5e..bdc87292 100644 --- a/NEWS.md +++ b/NEWS.md @@ -3,6 +3,8 @@ Red Panda C++ Version 1.3 - enhancement: don't parse all openned files when start up - enhancement: don't parse files when close all and exit - change: reduce time intervals for selection by mouse + - enhancement: change orders of the problems in the problem set panel by drag&drop + - enhancement: change orders of the problem cases in the problem panel by drag&drop Red Panda C++ Version 1.2 diff --git a/RedPandaIDE/mainwindow.ui b/RedPandaIDE/mainwindow.ui index 70fc7cf2..b1a08985 100644 --- a/RedPandaIDE/mainwindow.ui +++ b/RedPandaIDE/mainwindow.ui @@ -473,7 +473,7 @@ QTabWidget::West - 0 + 4 true @@ -835,6 +835,15 @@ + + true + + + QAbstractItemView::InternalMove + + + Qt::MoveAction + true @@ -1704,6 +1713,18 @@ + + true + + + false + + + QAbstractItemView::InternalMove + + + Qt::MoveAction + true diff --git a/RedPandaIDE/widgets/ojproblemsetmodel.cpp b/RedPandaIDE/widgets/ojproblemsetmodel.cpp index fb9b8117..9d691834 100644 --- a/RedPandaIDE/widgets/ojproblemsetmodel.cpp +++ b/RedPandaIDE/widgets/ojproblemsetmodel.cpp @@ -21,6 +21,7 @@ #include #include #include +#include #include "../utils.h" #include "../iconsmanager.h" #include "../systemconsts.h" @@ -241,9 +242,45 @@ bool OJProblemSetModel::setData(const QModelIndex &index, const QVariant &value, return false; } -Qt::ItemFlags OJProblemSetModel::flags(const QModelIndex &) const +Qt::ItemFlags OJProblemSetModel::flags(const QModelIndex &index) const { - return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable; + Qt::ItemFlags flags = Qt::NoItemFlags; + if (index.isValid()) { + flags = Qt::ItemIsEnabled | Qt::ItemIsDragEnabled | Qt::ItemIsEditable | Qt::ItemIsSelectable | Qt::ItemIsDropEnabled; + } else if (index.row() == -1) { + // -1 means it's a drop target? + flags = Qt::ItemIsDropEnabled; + } + return flags ; +} + +Qt::DropActions OJProblemSetModel::supportedDropActions() const +{ + return Qt::DropAction::MoveAction; +} + +bool OJProblemSetModel::moveRows(const QModelIndex &sourceParent, int sourceRow, int count, const QModelIndex &destinationParent, int destinationChild) +{ + if (sourceRow < 0 + || sourceRow + count - 1 >= mProblemSet.problems.count() + || destinationChild < 0 + || destinationChild > mProblemSet.problems.count() + || sourceRow == destinationChild + || count <= 0) { + return false; + } + if (!beginMoveRows(QModelIndex(), sourceRow, sourceRow + count - 1, QModelIndex(), destinationChild)) + return false; + + int fromRow = sourceRow; + if (destinationChild < sourceRow) + fromRow += count - 1; + else + destinationChild--; + while (count--) + mProblemSet.problems.move(fromRow, destinationChild); + endMoveRows(); + return true; } OJProblemModel::OJProblemModel(QObject *parent): QAbstractTableModel(parent) @@ -441,6 +478,9 @@ Qt::ItemFlags OJProblemModel::flags(const QModelIndex &idx) const Qt::ItemFlags flags=Qt::ItemIsEnabled | Qt::ItemIsSelectable; if (idx.column()==0) flags |= Qt::ItemIsEditable ; + if (idx.isValid()) + flags |= Qt::ItemIsDragEnabled; + flags |= Qt::ItemIsDropEnabled; return flags; } @@ -461,3 +501,47 @@ QVariant OJProblemModel::headerData(int section, Qt::Orientation orientation, in } return QVariant(); } + +Qt::DropActions OJProblemModel::supportedDropActions() const +{ + return Qt::DropAction::MoveAction; +} + +bool OJProblemModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) +{ + mMoveTargetRow=row; + return QAbstractTableModel::dropMimeData(data,action,row,0,parent); +} + +bool OJProblemModel::insertRows(int row, int count, const QModelIndex &parent) +{ + return true; +} + +bool OJProblemModel::removeRows(int row, int count, const QModelIndex &parent) +{ + int sourceRow = row; + int destinationChild = mMoveTargetRow; + mMoveTargetRow=-1; + if (sourceRow < 0 + || sourceRow + count - 1 >= mProblem->cases.count() + || destinationChild < 0 + || destinationChild > mProblem->cases.count() + || sourceRow == destinationChild + || count <= 0) { + return false; + } + if (!beginMoveRows(QModelIndex(), sourceRow, sourceRow + count - 1, QModelIndex(), destinationChild)) + return false; + + int fromRow = sourceRow; + if (destinationChild < sourceRow) + fromRow += count - 1; + else + destinationChild--; + while (count--) + mProblem->cases.move(fromRow, destinationChild); + endMoveRows(); + return true; +} + diff --git a/RedPandaIDE/widgets/ojproblemsetmodel.h b/RedPandaIDE/widgets/ojproblemsetmodel.h index f6f5eb2a..fd0da6b1 100644 --- a/RedPandaIDE/widgets/ojproblemsetmodel.h +++ b/RedPandaIDE/widgets/ojproblemsetmodel.h @@ -41,6 +41,7 @@ public: private: POJProblem mProblem; + int mMoveTargetRow; // QAbstractItemModel interface public: @@ -53,6 +54,19 @@ public: public: int columnCount(const QModelIndex &parent) const override; QVariant headerData(int section, Qt::Orientation orientation, int role) const override; + + // QAbstractItemModel interface +public: + Qt::DropActions supportedDropActions() const override; + + // QAbstractItemModel interface +public: + bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) override; + + // QAbstractItemModel interface +public: + bool insertRows(int row, int count, const QModelIndex &parent) override; + bool removeRows(int row, int count, const QModelIndex &parent) override; }; class OJProblemSetModel : public QAbstractListModel @@ -87,6 +101,11 @@ public: QVariant data(const QModelIndex &index, int role) const override; bool setData(const QModelIndex &index, const QVariant &value, int role) override; Qt::ItemFlags flags(const QModelIndex &index) const override; + + // QAbstractItemModel interface +public: + Qt::DropActions supportedDropActions() const override; + bool moveRows(const QModelIndex &sourceParent, int sourceRow, int count, const QModelIndex &destinationParent, int destinationChild) override; }; #endif // OJPROBLEMSETMODEL_H