- 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
This commit is contained in:
Roy Qu 2022-09-03 20:18:32 +08:00
parent ca4687c7cd
commit 6ff83d602c
4 changed files with 129 additions and 3 deletions

View File

@ -3,6 +3,8 @@ Red Panda C++ Version 1.3
- enhancement: don't parse all openned files when start up - enhancement: don't parse all openned files when start up
- enhancement: don't parse files when close all and exit - enhancement: don't parse files when close all and exit
- change: reduce time intervals for selection by mouse - 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 Red Panda C++ Version 1.2

View File

@ -473,7 +473,7 @@
<enum>QTabWidget::West</enum> <enum>QTabWidget::West</enum>
</property> </property>
<property name="currentIndex"> <property name="currentIndex">
<number>0</number> <number>4</number>
</property> </property>
<property name="usesScrollButtons"> <property name="usesScrollButtons">
<bool>true</bool> <bool>true</bool>
@ -835,6 +835,15 @@
</item> </item>
<item> <item>
<widget class="QListView" name="lstProblemSet"> <widget class="QListView" name="lstProblemSet">
<property name="dragEnabled">
<bool>true</bool>
</property>
<property name="dragDropMode">
<enum>QAbstractItemView::InternalMove</enum>
</property>
<property name="defaultDropAction">
<enum>Qt::MoveAction</enum>
</property>
<property name="alternatingRowColors"> <property name="alternatingRowColors">
<bool>true</bool> <bool>true</bool>
</property> </property>
@ -1704,6 +1713,18 @@
</item> </item>
<item> <item>
<widget class="QTableView" name="tblProblemCases"> <widget class="QTableView" name="tblProblemCases">
<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"> <property name="alternatingRowColors">
<bool>true</bool> <bool>true</bool>
</property> </property>

View File

@ -21,6 +21,7 @@
#include <QJsonArray> #include <QJsonArray>
#include <QJsonDocument> #include <QJsonDocument>
#include <QJsonObject> #include <QJsonObject>
#include <QMimeData>
#include "../utils.h" #include "../utils.h"
#include "../iconsmanager.h" #include "../iconsmanager.h"
#include "../systemconsts.h" #include "../systemconsts.h"
@ -241,9 +242,45 @@ bool OJProblemSetModel::setData(const QModelIndex &index, const QVariant &value,
return false; 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) 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; Qt::ItemFlags flags=Qt::ItemIsEnabled | Qt::ItemIsSelectable;
if (idx.column()==0) if (idx.column()==0)
flags |= Qt::ItemIsEditable ; flags |= Qt::ItemIsEditable ;
if (idx.isValid())
flags |= Qt::ItemIsDragEnabled;
flags |= Qt::ItemIsDropEnabled;
return flags; return flags;
} }
@ -461,3 +501,47 @@ QVariant OJProblemModel::headerData(int section, Qt::Orientation orientation, in
} }
return QVariant(); 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;
}

View File

@ -41,6 +41,7 @@ public:
private: private:
POJProblem mProblem; POJProblem mProblem;
int mMoveTargetRow;
// QAbstractItemModel interface // QAbstractItemModel interface
public: public:
@ -53,6 +54,19 @@ public:
public: public:
int columnCount(const QModelIndex &parent) const override; int columnCount(const QModelIndex &parent) const override;
QVariant headerData(int section, Qt::Orientation orientation, int role) 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 class OJProblemSetModel : public QAbstractListModel
@ -87,6 +101,11 @@ public:
QVariant data(const QModelIndex &index, int role) const override; QVariant data(const QModelIndex &index, int role) const override;
bool setData(const QModelIndex &index, const QVariant &value, int role) override; bool setData(const QModelIndex &index, const QVariant &value, int role) override;
Qt::ItemFlags flags(const QModelIndex &index) const 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 #endif // OJPROBLEMSETMODEL_H