mirror of
https://github.com/jorenchik/mdemory.git
synced 2026-03-22 00:26:21 +00:00
Drag and drop groups
This commit is contained in:
@@ -19,6 +19,7 @@
|
||||
#include <QStandardItemModel>
|
||||
#include <QDropEvent>
|
||||
#include <iostream>
|
||||
#include <QDrag>
|
||||
|
||||
#include "trainWindow.h"
|
||||
#include "parser.h"
|
||||
@@ -56,6 +57,26 @@ QStandardItemModel *orderModel;
|
||||
|
||||
QWidget *wGroupQuestion;
|
||||
|
||||
auto listStyle =
|
||||
"QListView {"
|
||||
" background-color: white;"
|
||||
" border: 0px solid black;"
|
||||
"}"
|
||||
"QListView::item::first {"
|
||||
" padding: 5px 0 0 0;"
|
||||
"}"
|
||||
"QListView::item {"
|
||||
" color: black;"
|
||||
" padding: 5px;"
|
||||
" border: 1px solid gray;"
|
||||
" background-color: white;"
|
||||
"}"
|
||||
"QListView::item:selected {"
|
||||
"}"
|
||||
"QListView::item:hover {"
|
||||
" background-color: lightyellow;"
|
||||
"}";
|
||||
|
||||
class OrderListView : public QListView
|
||||
{
|
||||
Q_OBJECT
|
||||
@@ -89,26 +110,33 @@ class MoveListView : public QListView
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit MoveListView(QWidget *parent = nullptr) : QListView(parent) {}
|
||||
explicit MoveListView(QWidget *parent = nullptr) : QListView(parent) {
|
||||
setStyleSheet(listStyle);
|
||||
setAcceptDrops(true);
|
||||
setDragDropMode(QAbstractItemView::DragDrop);
|
||||
setDefaultDropAction(Qt::MoveAction);
|
||||
}
|
||||
|
||||
protected:
|
||||
void dropEvent(QDropEvent *event) override
|
||||
{
|
||||
QModelIndex sourceIndex = currentIndex();
|
||||
QModelIndex targetIndex = indexAt(event->pos());
|
||||
void dragEnterEvent(QDragEnterEvent *event) override {
|
||||
event->acceptProposedAction();
|
||||
}
|
||||
|
||||
if (sourceIndex.isValid() && targetIndex.isValid()) {
|
||||
QStandardItemModel *model = qobject_cast<QStandardItemModel *>(this->model());
|
||||
if (model) {
|
||||
QVariant sourceData = model->data(sourceIndex);
|
||||
QVariant targetData = model->data(targetIndex);
|
||||
model->setData(sourceIndex, targetData);
|
||||
model->setData(targetIndex, sourceData);
|
||||
event->ignore();
|
||||
}
|
||||
} else {
|
||||
event->ignore();
|
||||
void dragMoveEvent(QDragMoveEvent *event) override {
|
||||
event->acceptProposedAction();
|
||||
}
|
||||
|
||||
void dropEvent(QDropEvent *event) override {
|
||||
QListView *source = qobject_cast<QListView *>(event->source());
|
||||
QStandardItemModel *sourceModel = qobject_cast<QStandardItemModel *>(source->model());
|
||||
QStandardItemModel *targetModel = qobject_cast<QStandardItemModel *>(this->model());
|
||||
|
||||
if (source != this) {
|
||||
QList<QStandardItem *> items = sourceModel->takeRow(source->currentIndex().row());
|
||||
targetModel->appendRow(items);
|
||||
}
|
||||
|
||||
event->ignore();
|
||||
}
|
||||
};
|
||||
|
||||
@@ -160,26 +188,6 @@ void initTrainWindow() {
|
||||
lQuestionText->hide();
|
||||
}
|
||||
|
||||
auto listStyle =
|
||||
"QListView {"
|
||||
" background-color: white;"
|
||||
" border: 0px solid black;"
|
||||
"}"
|
||||
"QListView::item::first {"
|
||||
" padding: 5px 0 0 0;"
|
||||
"}"
|
||||
"QListView::item {"
|
||||
" color: black;"
|
||||
" padding: 5px;"
|
||||
" border: 1px solid gray;"
|
||||
" background-color: white;"
|
||||
"}"
|
||||
"QListView::item:selected {"
|
||||
"}"
|
||||
"QListView::item:hover {"
|
||||
" background-color: lightyellow;"
|
||||
"}";
|
||||
|
||||
{ // Make multi-choice list.
|
||||
multiChoiceModel = new QStandardItemModel();
|
||||
listView = new QListView();
|
||||
@@ -278,12 +286,8 @@ void initTrainWindow() {
|
||||
auto vItems = new QVBoxLayout();
|
||||
itemScroll->setLayout(vItems);
|
||||
itemModel = new QStandardItemModel();
|
||||
itemListView = new QListView();
|
||||
itemListView = new MoveListView();
|
||||
itemListView->setModel(itemModel);
|
||||
itemListView->setStyleSheet(listStyle);
|
||||
itemListView->setDragEnabled(true);
|
||||
itemListView->setDragDropMode(QAbstractItemView::DragDrop);
|
||||
itemListView->setDefaultDropAction(Qt::MoveAction);
|
||||
vItems->addWidget(itemListView);
|
||||
hGroupQuestion->addWidget(itemScroll);
|
||||
|
||||
@@ -303,15 +307,11 @@ void initTrainWindow() {
|
||||
auto groupLabel = new QLabel("Group I");
|
||||
auto scrollArea = new QScrollArea();
|
||||
auto groupModel = new QStandardItemModel();
|
||||
auto groupList = new QListView();
|
||||
groupList->setModel(groupModel);
|
||||
groupList->setMaximumHeight(100);
|
||||
auto groupList = new MoveListView();
|
||||
vGroups->addWidget(groupLabel);
|
||||
vGroups->addWidget(groupList);
|
||||
groupList->setStyleSheet(listStyle);
|
||||
groupList->setAcceptDrops(true);
|
||||
groupList->setDragDropMode(QAbstractItemView::DragDrop);
|
||||
groupList->setDefaultDropAction(Qt::MoveAction);
|
||||
groupList->setModel(groupModel);
|
||||
groupList->setMaximumHeight(100);
|
||||
}
|
||||
vGroups->addItem(groupSpacer);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user