fixing drag to use Kirigami.dragHandler properly - WOOHOO!

This commit is contained in:
Chris Cochrun 2022-11-29 15:06:45 -06:00
parent bbe618403d
commit 2002c67b24
3 changed files with 75 additions and 66 deletions

View file

@ -91,12 +91,7 @@ Item {
id: serviceListItem id: serviceListItem
implicitWidth: serviceItemList.width implicitWidth: serviceItemList.width
height: 30 height: 30
Kirigami.ListItemDragHandle {
anchors.fill: parent
listItem: serviceListItem
listView: serviceItemList
onMoveRequested: serviceItemModel.move(oldIndex, newIndex, 1)
}
DropArea { DropArea {
id: serviceDrop id: serviceDrop
anchors.fill: parent anchors.fill: parent
@ -127,9 +122,9 @@ Item {
dragItemFontSize, dragItemFontSize,
dragItemIndex); dragItemIndex);
} else if (drag.keys[0] === "serviceitem") { } else if (drag.keys[0] === "serviceitem") {
serviceItemModel.move(serviceItemList.indexDragged, /* serviceItemModel.moveRows(serviceItemList.indexDragged, */
serviceItemList.moveToIndex); /* serviceItemList.moveToIndex, 1); */
serviceItemList.currentIndex = moveToIndex; /* serviceItemList.currentIndex = moveToIndex; */
} }
dropHighlightLine.visible = false; dropHighlightLine.visible = false;
} }
@ -157,12 +152,12 @@ Item {
Controls.Label { Controls.Label {
id: label id: label
anchors.left: parent.left anchors.left: dragHandle.right
anchors.verticalCenter: parent.verticalCenter anchors.verticalCenter: parent.verticalCenter
anchors.leftMargin: 5 anchors.leftMargin: 5
text: name text: name
elide: Text.ElideRight elide: Text.ElideRight
width: parent.width - trailing.width - 15 width: parent.width - trailing.width - dragHandle.width - 15
color: { color: {
if (selected || if (selected ||
mouseHandler.containsMouse || active) mouseHandler.containsMouse || active)
@ -187,7 +182,7 @@ Item {
} }
} }
onYChanged: serviceItemList.updateDrag(Math.round(y)); /* onYChanged: serviceItemList.updateDrag(Math.round(y)); */
states: [ states: [
State { State {
@ -208,31 +203,31 @@ Item {
] ]
/* Drag.dragType: Drag.Automatic */ /* Drag.dragType: Drag.Automatic */
Drag.active: mouseHandler.drag.active /* Drag.active: mouseHandler.drag.active */
Drag.hotSpot.x: width / 2 /* Drag.hotSpot.x: width / 2 */
Drag.hotSpot.y: height / 2 /* Drag.hotSpot.y: height / 2 */
Drag.keys: ["serviceitem"] /* Drag.keys: ["serviceitem"] */
MouseArea { MouseArea {
id: mouseHandler id: mouseHandler
anchors.fill: parent anchors.fill: parent
hoverEnabled: true hoverEnabled: true
acceptedButtons: Qt.LeftButton | Qt.RightButton acceptedButtons: Qt.LeftButton | Qt.RightButton
preventStealing: true /* preventStealing: true */
drag { /* drag { */
target: visServiceItem /* target: visServiceItem */
axis: Drag.YAxis /* axis: Drag.YAxis */
/* minimumY: root.y */ /* /\* minimumY: root.y *\/ */
/* maximumY: serviceItemList.height - serviceDrop.height */ /* /\* maximumY: serviceItemList.height - serviceDrop.height *\/ */
smoothed: false /* smoothed: false */
} /* } */
drag.onActiveChanged: { /* drag.onActiveChanged: { */
if (mouseHandler.drag.active) { /* if (mouseHandler.drag.active) { */
serviceItemList.indexDragged = index; /* serviceItemList.indexDragged = index; */
} /* } */
} /* } */
/* onPositionChanged: { */ /* onPositionChanged: { */
/* if (!pressed) { */ /* if (!pressed) { */
@ -267,6 +262,18 @@ Item {
visServiceItem.Drag.drop(); visServiceItem.Drag.drop();
} }
} }
Kirigami.ListItemDragHandle {
id: dragHandle
anchors.left: parent.left
anchors.verticalCenter: parent.verticalCenter
anchors.leftMargin: 5
width: 20
listItem: serviceListItem
listView: serviceItemList
onMoveRequested: serviceItemModel.moveRows(oldIndex, newIndex, 1)
}
} }
Controls.Menu { Controls.Menu {
id: rightClickMenu id: rightClickMenu
@ -346,7 +353,7 @@ Item {
function moveRequested(oldIndex, newIndex) { function moveRequested(oldIndex, newIndex) {
print("moveRequested: ", oldIndex, newIndex); print("moveRequested: ", oldIndex, newIndex);
serviceItemModel.move(oldIndex, newIndex); serviceItemModel.moveRows(oldIndex, newIndex, 1);
indexDragged = newIndex; indexDragged = newIndex;
serviceItemList.currentIndex = newIndex; serviceItemList.currentIndex = newIndex;
} }
@ -439,7 +446,7 @@ Item {
const newid = serviceItemList.currentIndex - 1; const newid = serviceItemList.currentIndex - 1;
showPassiveNotification(oldid + " " + newid); showPassiveNotification(oldid + " " + newid);
showPassiveNotification("Up"); showPassiveNotification("Up");
const ans = serviceItemModel.move(oldid, newid); const ans = serviceItemModel.moveRows(oldid, newid, 1);
if (ans) if (ans)
{ {
serviceItemList.currentIndex = newid; serviceItemList.currentIndex = newid;

View file

@ -322,43 +322,32 @@ void ServiceItemModel::removeItem(int index) {
endRemoveRows(); endRemoveRows();
} }
bool ServiceItemModel::move(int sourceIndex, int destIndex) { bool ServiceItemModel::moveRows(int sourceIndex, int destIndex, int count) {
qDebug() << index(sourceIndex).row(); qDebug() << index(sourceIndex).row();
qDebug() << index(destIndex).row(); qDebug() << index(destIndex).row();
QModelIndex parent = index(sourceIndex).parent();
if (sourceIndex >= 0 && sourceIndex != destIndex && if (sourceIndex < 0 || sourceIndex == destIndex ||
destIndex > -1 && destIndex <= rowCount() && destIndex == -1 || destIndex > rowCount() ||
sourceIndex < rowCount()) { sourceIndex >= rowCount()) {
qDebug() << "starting move: " << "source: " << sourceIndex << "dest: " << destIndex; return false;
bool begsuc = beginMoveRows(parent, sourceIndex, };
sourceIndex, parent, destIndex);
if (begsuc) { const QModelIndex parent = index(sourceIndex).parent();
if (destIndex == -1) const bool isMoveDown = destIndex > sourceIndex;
{
qDebug() << "dest was too small, moving to row 0";
m_items.move(sourceIndex, 0); if (!beginMoveRows(parent, sourceIndex, sourceIndex + count - 1,
} parent, isMoveDown ? destIndex + 1 : destIndex)) {
else qDebug() << "Can't move rows";
{
qDebug() << "dest was not too small";
if (destIndex >= m_items.size())
{
qDebug() << "destIndex too big, moving to end";
m_items.move(sourceIndex, m_items.size() - 1);
}
else
m_items.move(sourceIndex, destIndex);
}
endMoveRows();
return true;
}
qDebug() << "Can't move row, not sure why, sourceIndex: "
<< sourceIndex << " destIndex: " << destIndex;
return false; return false;
} }
qDebug() << "Can't move row, invalid options, sourceIndex: "
<< sourceIndex << " destIndex: " << destIndex; qDebug() << "starting move: " << "source: " << sourceIndex << "dest: " << destIndex;
return false;
m_items.move(sourceIndex, isMoveDown ? destIndex + 1 : destIndex);
endMoveRows();
return true;
} }
bool ServiceItemModel::moveDown(int id) { bool ServiceItemModel::moveDown(int id) {
@ -388,7 +377,20 @@ bool ServiceItemModel::moveUp(int id) {
qDebug() << index(id - 1).row(); qDebug() << index(id - 1).row();
QModelIndex parent = index(id).parent(); QModelIndex parent = index(id).parent();
bool begsuc = beginMoveRows(parent, id,
id, parent, id - 1);
if (begsuc) {
int dest = id - 1;
if (dest <= -1)
{
qDebug() << "dest too big, moving to beginning";
m_items.move(id, 0);
}
else
m_items.move(id, dest);
endMoveRows();
return true;
}
return false; return false;
} }

View file

@ -82,7 +82,7 @@ public:
const QString &backgroundType, const QStringList &text, const QString &backgroundType, const QStringList &text,
const QString &audio, const QString &font, const int &fontSize); const QString &audio, const QString &font, const int &fontSize);
Q_INVOKABLE void removeItem(int index); Q_INVOKABLE void removeItem(int index);
Q_INVOKABLE bool move(int sourceIndex, int destIndex); Q_INVOKABLE bool moveRows(int sourceIndex, int destIndex, int count);
Q_INVOKABLE bool moveDown(int index); Q_INVOKABLE bool moveDown(int index);
Q_INVOKABLE bool moveUp(int index); Q_INVOKABLE bool moveUp(int index);
Q_INVOKABLE bool select(int id); Q_INVOKABLE bool select(int id);