From 6b3d6bec552270009b9932fab6bcdc7ec4624b6d Mon Sep 17 00:00:00 2001 From: BlueGreenMagick <50060875+BlueGreenMagick@users.noreply.github.com> Date: Tue, 7 Apr 2020 16:42:33 +0900 Subject: [PATCH] allow dragging fields to change its position --- qt/aqt/fields.py | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/qt/aqt/fields.py b/qt/aqt/fields.py index 39ec81013..c5cfd79ba 100644 --- a/qt/aqt/fields.py +++ b/qt/aqt/fields.py @@ -27,6 +27,8 @@ class FieldDialog(QDialog): self.oldSortField = self.model["sortf"] self.fillFields() self.setupSignals() + self.form.fieldList.setDragDropMode(QAbstractItemView.InternalMove) + self.form.fieldList.dropEvent = self.onDrop self.form.fieldList.setCurrentRow(0) self.exec_() @@ -48,6 +50,17 @@ class FieldDialog(QDialog): f.sortField.clicked.connect(self.onSortField) f.buttonBox.helpRequested.connect(self.onHelp) + def onDrop(self, ev): + fieldList = self.form.fieldList + indicatorPos = fieldList.dropIndicatorPosition() + dropPos = fieldList.indexAt(ev.pos()).row() + if indicatorPos == QAbstractItemView.OnViewport: # to bottom + self.moveField(fieldList.count()) + elif indicatorPos == QAbstractItemView.AboveItem: + self.moveField(dropPos) + elif indicatorPos == QAbstractItemView.BelowItem: + self.moveField(dropPos + 1) + def onRowChange(self, idx): if idx == -1: return @@ -115,6 +128,14 @@ class FieldDialog(QDialog): return if not 0 < pos <= l: return + self.moveField(pos) + + def onSortField(self): + # don't allow user to disable; it makes no sense + self.form.sortField.setChecked(True) + self.model["sortf"] = self.form.fieldList.currentRow() + + def moveField(self, pos): self.saveField() f = self.model["flds"][self.currentIdx] self.mw.progress.start() @@ -123,11 +144,6 @@ class FieldDialog(QDialog): self.fillFields() self.form.fieldList.setCurrentRow(pos - 1) - def onSortField(self): - # don't allow user to disable; it makes no sense - self.form.sortField.setChecked(True) - self.model["sortf"] = self.form.fieldList.currentRow() - def loadField(self, idx): self.currentIdx = idx fld = self.model["flds"][idx]