Skip to content

Commit

Permalink
refactor(context menu): Move model context menu to reusable components.
Browse files Browse the repository at this point in the history
  • Loading branch information
imoize committed Sep 5, 2024
1 parent 424a275 commit 141feac
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 28 deletions.
72 changes: 44 additions & 28 deletions package/contents/ui/ModelsDelegate.qml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import org.kde.plasma.components as PlasmaComponents
import "Utils.js" as Utils

PlasmaComponents.ItemDelegate {
id: modeltem
id: modelItem
height: Math.max(label.height, Math.round(Kirigami.Units.gridUnit * 1.6)) + 2 * Kirigami.Units.smallSpacing
enabled: true

Expand All @@ -18,7 +18,7 @@ PlasmaComponents.ItemDelegate {
anchors.fill: parent
hoverEnabled: true
onEntered: {
modelListView.currentIndex = index
modelListView.currentIndex = index;
}
onExited: {
if (modelListView.currentIndex === index)
Expand All @@ -27,14 +27,14 @@ PlasmaComponents.ItemDelegate {

Item {
id: label
height: labelLayout.height
anchors {
left: parent.left
right: parent.right
leftMargin: Kirigami.Units.gridUnit - 9.2
// rightMargin: Kirigami.Units.gridUnit - 3.2
verticalCenter: parent.verticalCenter
}
height: labelLayout.height

RowLayout {
id: labelLayout
Expand Down Expand Up @@ -114,40 +114,56 @@ PlasmaComponents.ItemDelegate {
text: i18n("Load Model")
icon.name: Qt.resolvedUrl("icons/up-square.svg")
onClicked: {
var model = modelName
Utils.loadModel(model);
var model = modelName;
}
display: QQC2.AbstractButton.IconOnly
PlasmaComponents.ToolTip {
text: parent.text
}
display:QQC2.AbstractButton.IconOnly
PlasmaComponents.ToolTip { text: parent.text }
}

PlasmaComponents.ToolButton {
id: contextMenuButton
property var contextMenu: null
// anchors.centerIn: parent
checkable: true
checked: contextMenu.opened
text: i18n("More")
icon.name: Qt.resolvedUrl("icons/options.svg")
onClicked: {
contextMenu.open();
onClicked: {
createContextMenu(modelName);
}
display:QQC2.AbstractButton.IconOnly
PlasmaComponents.ToolTip { text: parent.text }

QQC2.Menu {
id: contextMenu
modal: true
y: contextMenuButton.height + Kirigami.Units.smallSpacing
margins: Kirigami.Units.smallSpacing * 5
// width: Kirigami.Units.gridUnit * 7
closePolicy: QQC2.Popup.CloseOnPressOutside | QQC2.Popup.CloseOnReleaseOutside

QQC2.MenuItem {
text: i18n("Delete (WIP)")
icon.name: Qt.resolvedUrl("icons/delete.svg")
// onTriggered: {
// listPage.view.openDialog(modelName);
// }
display: QQC2.AbstractButton.IconOnly
PlasmaComponents.ToolTip {
text: parent.text
}

function createContextMenu(modelName) {
if (contextMenu === null) {
var component = Qt.createComponent("./components/ContextMenu.qml");
contextMenu = component.createObject(contextMenuButton);
contextMenuButton.checked = true;
contextMenu.modelName = modelName;
contextMenu.open();
if (contextMenu !== null) {
contextMenu.closeContextMenu.connect(destroyContextMenu);
}
}
}

function destroyContextMenu() {
if (contextMenu !== null) {
contextMenu.destroy();
contextMenuButton.checked = false;
contextMenu = null;
}
}
}

Connections {
target: main
function onExpandedChanged() {
if (!main.expanded) {
contextMenuButton.destroyContextMenu();
}
}
}
Expand All @@ -166,4 +182,4 @@ PlasmaComponents.ItemDelegate {
width: parent.width - Kirigami.Units.gridUnit
visible: showSeparator
}
}
}
80 changes: 80 additions & 0 deletions package/contents/ui/components/ContextMenu.qml
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import QtQuick
import QtQuick.Controls as QQC2
import org.kde.kirigami as Kirigami
import org.kde.plasma.components as PlasmaComponents

PlasmaComponents.Menu {
id: contextMenu

property string modelName: ""
signal closeContextMenu

width: Kirigami.Units.gridUnit * 8
margins: Kirigami.Units.smallSpacing * 3
y: contextMenuButton.height
modal: true
closePolicy: QQC2.Popup.CloseOnPressOutside

PlasmaComponents.MenuItem {
id: copyMenuItem
text: i18n("Copy")

onTriggered: {

}
onHoveredChanged: {
if (!hovered) {
highlighted = false;
}
}
PlasmaComponents.ToolTip {
text: i18n("Creates a model with another name from an existing model.")
}
}

// PlasmaComponents.MenuSeparator {}

// PlasmaComponents.MenuItem {
// id: pushMenuItem
// text: i18n("Push")
// icon.name: Qt.resolvedUrl("../icons/up-tray.svg")
// onTriggered: {}
// onHoveredChanged: {
// if (!hovered) {
// highlighted = false;
// }
// }
// }

// PlasmaComponents.MenuItem {
// id: updateMenuItem
// text: i18n("Update")
// icon.name: Qt.resolvedUrl("../icons/down-tray.svg")
// onTriggered: {}
// onHoveredChanged: {
// if (!hovered) {
// highlighted = false;
// }
// }
// }

PlasmaComponents.MenuSeparator {}

PlasmaComponents.MenuItem {
id: deleteMenuItem
text: i18n("Delete")
icon.name: Qt.resolvedUrl("../icons/delete.svg")
onTriggered: {

}
onHoveredChanged: {
if (!hovered) {
highlighted = false;
}
}
}

onClosed: {
closeContextMenu();
}
}

0 comments on commit 141feac

Please sign in to comment.