Fix view combobox not updating when using view menu item, code cleanup, CURA-4411

This commit is contained in:
ChrisTerBeke 2017-10-05 15:01:19 +02:00
parent cd59048475
commit 9ef1caff50

View File

@ -221,40 +221,51 @@ Rectangle
ComboBox ComboBox
{ {
id: viewModeButton id: viewModeButton
anchors
{ anchors {
verticalCenter: parent.verticalCenter verticalCenter: parent.verticalCenter
right: parent.right right: parent.right
rightMargin: UM.Theme.getSize("sidebar").width + UM.Theme.getSize("default_margin").width rightMargin: UM.Theme.getSize("sidebar").width + UM.Theme.getSize("default_margin").width
} }
style: UM.Theme.styles.combobox style: UM.Theme.styles.combobox
visible: !base.monitoringPrint visible: !base.monitoringPrint
model: UM.ViewModel { } model: UM.ViewModel { }
textRole: "name" textRole: "name"
onCurrentIndexChanged: // update the model's active index
{ function update () {
UM.Controller.setActiveView(model.getItem(currentIndex).id); currentIndex = getActiveIndex()
for (var i = 0; i < model.rowCount(); i++) {
// Update the active flag model.getItem(i).active = (i == currentIndex)
for (var i = 0; i < model.rowCount; ++i)
{
const is_active = i == currentIndex;
model.getItem(i).active = is_active;
} }
} }
currentIndex: // get the index of the active model item on start
{ function getActiveIndex () {
for (var i = 0; i < model.rowCount; ++i) for (var i = 0; i < model.rowCount(); i++) {
{ if (model.getItem(i).active) {
if (model.getItem(i).active) return i
{
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()
} }
} }