From 9ef1caff50e9741a33e5db87acaaf59639d5d2d3 Mon Sep 17 00:00:00 2001 From: ChrisTerBeke Date: Thu, 5 Oct 2017 15:01:19 +0200 Subject: [PATCH] Fix view combobox not updating when using view menu item, code cleanup, CURA-4411 --- resources/qml/Topbar.qml | 49 ++++++++++++++++++++++++---------------- 1 file changed, 30 insertions(+), 19 deletions(-) diff --git a/resources/qml/Topbar.qml b/resources/qml/Topbar.qml index b3a5137111..556b627394 100644 --- a/resources/qml/Topbar.qml +++ b/resources/qml/Topbar.qml @@ -221,40 +221,51 @@ Rectangle ComboBox { id: viewModeButton - anchors - { + + anchors { verticalCenter: parent.verticalCenter right: parent.right rightMargin: UM.Theme.getSize("sidebar").width + UM.Theme.getSize("default_margin").width } + style: UM.Theme.styles.combobox visible: !base.monitoringPrint model: UM.ViewModel { } textRole: "name" - onCurrentIndexChanged: - { - UM.Controller.setActiveView(model.getItem(currentIndex).id); - - // Update the active flag - for (var i = 0; i < model.rowCount; ++i) - { - const is_active = i == currentIndex; - model.getItem(i).active = is_active; + // update the model's active index + function update () { + currentIndex = getActiveIndex() + for (var i = 0; i < model.rowCount(); i++) { + model.getItem(i).active = (i == currentIndex) } } - currentIndex: - { - for (var i = 0; i < model.rowCount; ++i) - { - if (model.getItem(i).active) - { - return i; + // get the index of the active model item on start + function getActiveIndex () { + for (var i = 0; i < model.rowCount(); i++) { + if (model.getItem(i).active) { + return i } } - return 0; + return 0 + } + + // set the active index + function setActiveIndex (index) { + UM.Controller.setActiveView(index) + // the connection to UM.ActiveView will trigger update so there is no reason to call it manually here + } + + onCurrentIndexChanged: viewModeButton.setActiveIndex(model.getItem(currentIndex).id) + currentIndex: getActiveIndex() + + // watch the active view proxy for changes made from the menu item + Connections + { + target: UM.ActiveView + onActiveViewChanged: viewModeButton.update() } }