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

View file

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

View file

@ -82,7 +82,7 @@ public:
const QString &backgroundType, const QStringList &text,
const QString &audio, const QString &font, const int &fontSize);
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 moveUp(int index);
Q_INVOKABLE bool select(int id);