From c57f9f8ac93ccd54e064c9ae0f6cf141497505f4 Mon Sep 17 00:00:00 2001 From: Chris Cochrun Date: Sun, 17 Sep 2023 14:35:37 -0500 Subject: [PATCH] the moving of rows works but resets on move This breaks fluid movements from QML with KirigamiDragHandles We will need to adjust how this move works in QML or get the begin_move_rows function to build. If it can build we can still use the KirigamiDragHandles. --- src/qml/main.qml | 2 +- src/qml/presenter/ServiceList.qml | 6 +-- src/rust/service_item_model.rs | 73 +++++++++++++++++++++++++++---- 3 files changed, 69 insertions(+), 12 deletions(-) diff --git a/src/qml/main.qml b/src/qml/main.qml index 0c9580c..0a16537 100644 --- a/src/qml/main.qml +++ b/src/qml/main.qml @@ -115,7 +115,7 @@ Kirigami.ApplicationWindow { Controls.Label { Layout.alignment: Qt.AlignRight Layout.rightMargin: Kirigami.Units.smallSpacing * 2 - text: "Total Service Items: " + ServiceItemModel.rowCount() + text: "Total Service Items: " + ServiceItemModel.count() } Controls.Label { Layout.alignment: Qt.AlignRight diff --git a/src/qml/presenter/ServiceList.qml b/src/qml/presenter/ServiceList.qml index 853370f..1d66d6e 100644 --- a/src/qml/presenter/ServiceList.qml +++ b/src/qml/presenter/ServiceList.qml @@ -382,10 +382,10 @@ Item { contextType: "2d" renderStrategy: Canvas.Threaded onPaint: { - console.log(Kirigami.Theme.hoverColor.name()); + console.log(Kirigami.Theme.hoverColor); var ctx = getContext("2d"); ctx.fillRule = Qt.OddEvenFill - ctx.fillStyle = Kirigami.Theme.hoverColor.rgb(); + ctx.fillStyle = Kirigami.Theme.hoverColor; ctx.rotate(30); ctx.transform(0.8, 0, 0, 0.8, 0, 30) ctx.path = tearDropPath; @@ -452,7 +452,7 @@ Item { id: serviceToolBar Layout.fillWidth: true opacity: 1.0 - display: Button.IconOnly + display: Controls.Button.IconOnly actions: [ Kirigami.Action { text: "Up" diff --git a/src/rust/service_item_model.rs b/src/rust/service_item_model.rs index 570e5af..e557c55 100644 --- a/src/rust/service_item_model.rs +++ b/src/rust/service_item_model.rs @@ -322,20 +322,63 @@ mod service_item_model { dest_index: i32, count: i32, ) -> bool { - todo!(); + let model_index = + self.index(source_index, 0, &QModelIndex::default()); + let parent = model_index.parent(); + let source_id = source_index as usize; + let dest_id = dest_index as usize; + let count = count as usize; + let end_service_item = source_id + count - 1; + + println!("rust-end-service_item: {:?}", end_service_item); + println!("rust-dest-service_item: {:?}", dest_index); + unsafe { + // this function doesn't build + // self.as_mut().begin_move_rows( + // &parent, + // source_index, + // source_index + count - 1, + // &parent, + // dest_index, + // ); + self.as_mut().begin_reset_model(); + + if source_id < dest_id { + let move_amount = dest_id - source_id - count + 1; + self.as_mut().service_items_mut() + [source_id..=dest_id] + .rotate_right(move_amount); + println!("rust-move_amount: {:?}", move_amount); + } else { + let move_amount = + end_service_item - dest_id - count + 1; + println!("rust-move_amount: {:?}", move_amount); + self.as_mut().service_items_mut() + [dest_id..=end_service_item] + .rotate_left(move_amount); + } + + // this function works, begin does not + // self.as_mut().end_move_rows(); + self.as_mut().end_reset_model(); + let item = self.as_mut().get_item(dest_index); + self.as_mut().emit_item_moved( + &source_index, + &dest_index, + &item, + ); + true + } } #[qinvokable] - pub fn move_up(mut self: Pin<&mut Self>, index: i32) -> bool { - todo!(); + pub fn move_up(self: Pin<&mut Self>, index: i32) -> bool { + self.move_rows(index, index - 1, 1) } #[qinvokable] - pub fn move_down( - mut self: Pin<&mut Self>, - index: i32, - ) -> bool { - todo!(); + pub fn move_down(self: Pin<&mut Self>, index: i32) -> bool { + self.move_rows(index, index + 1, 1) } #[qinvokable] @@ -904,6 +947,20 @@ mod service_item_model { self: Pin<&mut qobject::ServiceItemMod>, ); + // cxx-qt can't build this function for some reason + // unsafe fn begin_move_rows( + // self: Pin<&mut qobject::ServiceItemMod>, + // source_parent: &QModelIndex, + // source_first: i32, + // source_last: i32, + // destination_parent: &QModelIndex, + // destination_child: i32, + // ); + + unsafe fn end_move_rows( + self: Pin<&mut qobject::ServiceItemMod>, + ); + unsafe fn begin_remove_rows( self: Pin<&mut qobject::ServiceItemMod>, parent: &QModelIndex,