Need help with QSlider collating as 1 undo

29 views
Skip to first unread message

likage

unread,
Jul 31, 2019, 1:34:44 PM7/31/19
to Python Programming for Autodesk Maya
Hi all, I need some insights on this gui signals + undo.

In my tool, there is a QLineEdit and a QSlider in which both are connected/ linked to each other, where if you made changes in the QLineEdit, it will updates the QSlider, likewise if you made changes on the QSlider, it will updates the values in the QLineEdit.

Currently I am having issues with QSlider where I am trying to collate the actions (as User slides it) as 1 undo action.

Here is my code:
# The signal for the slider
self.ui.planeSizeHorizontalSlider.valueChanged.connect(self.plane_size_slider)


def plane_size_slider(self, value):
    img_plane_node = self.get_current_img_plane()
    value /= 10.00
    self.ui.planeSizeLineEdit.setText(str(value))
    self.set_plane_size(img_plane_node, value)

def set_plane_size(self, node, value):
    width = cmds.getAttr("{0}.width".format(node))
    height = cmds.getAttr("{0}.height".format(node))
    ratio =  height / width

    with UndoManager(): # UndoManager is a contextmanager
        cmds.setAttr("{0}.maintainRatio".format(node), 0)
        cmds.setAttr("{0}.width".format(node), value)
        cmds.setAttr("{0}.height".format(node), value * ratio)
        cmds.setAttr("{0}.maintainRatio".format(node), 1)

While in viewport, the imagePlane does gets scaled accordingly as I perform changes on the slider for visual purposes, but when it comes to undo, it seems to be to factoring into account of all the values that I have made (eg. the slider value was initially at 0 and I slide it to 10, and so 10 actions/ undos)

My question here is, if there are ways in which I can tell my QSlider to:
  • still uses valueChanged (so that User can still visualize the scaling)
  • but collate the 'sliding' as one action/ undo instead?
I tried using `sliderReleased`  but it only seems to scale the item after it has been released.

# The signal for the slider
self.ui.planeSizeHorizontalSlider.valueChanged.connect(self.plane_size_slider)
self.ui.planeSizeHorizontalSlider.sliderReleased.connect(self.plane_size_slider)

def plane_size_slider(self, value):
    img_plane_node = self.get_current_img_plane()
    value /= 10.00
    self.ui.planeSizeLineEdit.setText(str(value))

def plane_size_slider_release(self):
    img_plane_node = self.get_current_img_plane()
    value = float(self.ui.planeSizeLineEdit.text())
    self.set_plane_size(img_plane_node, value)

def set_plane_size(self, node, value):
    width = cmds.getAttr("{0}.width".format(node))
    height = cmds.getAttr("{0}.height".format(node))
    ratio =  height / width

    with UndoManager(): # UndoManager is a contextmanager
        cmds.setAttr("{0}.maintainRatio".format(node), 0)
        cmds.setAttr("{0}.width".format(node), value)
        cmds.setAttr("{0}.height".format(node), value * ratio)
        cmds.setAttr("{0}.maintainRatio".format(node), 1)



Message has been deleted

likage

unread,
Aug 2, 2019, 7:16:35 PM8/2/19
to Python Programming for Autodesk Maya
I managed to make some leeway however I am still having issues with it.

# Connections that I have set for my slider
self.ui.planeSizeHorizontalSlider.sliderPressed.connect(self.slider_pressed)
self.ui.planeSizeHorizontalSlider.sliderReleased.connect(self.slider_released)
self.ui.planeSizeHorizontalSlider.valueChanged.connect(self.plane_size_slider)


def slider_pressed(self):
    cmds
.undoInfo(openChunk=True, chunkName='SlideTest')
   
def slider_released(self):
    cmds
.undoInfo(closeChunk=True, chunkName='SlideTest')


def plane_size_slider(self, value):
    img_plane_node
= self.get_current_img_plane()
    value
/= 10.00
   
self.ui.planeSizeLineEdit.setText(str(value))
   
self.set_plane_size(img_plane_node, value)

def set_plane_size(self, node, value):
    width
= cmds.getAttr("{0}.width".format(node))
    height
= cmds.getAttr("{0}.height".format(node))
    ratio
=  height /
width

   
# This got regiters as additional actions...

    cmds
.setAttr("{0}.maintainRatio".format(node), 0)
    cmds
.setAttr("{0}.width".format(node), value)
    cmds
.setAttr("{0}.height".format(node), value * ratio)
    cmds
.setAttr("{0}.maintainRatio".format(node), 1)


However my undo queue is incorrect. Current queue stemming from the above code (where the queue is empty to begin with) where I perform 2 slides...
# 1:  #
# 2:  #
# 3:  #
# 4:  #
# 5: SlideTest #
# 6:  #
# 7:  #
# 8:  #
# 9:  #
# 10: SlideTest #


Even so, when I do try to do an undo, it only works the first time and nothing was changing thereafter. I presume the 'empty' entries are coming from the `setAttr` in which I am expecting my undo queue to be something as follows:
# 1: SlideTest #
# 2: SlideTest #



Appreciate in advance for any replies.
Reply all
Reply to author
Forward
0 new messages