Download A GUI to Support Scission-Point Identification - VLE
Transcript
We begin by making the current filename None and then we attempt to read the image into a local
variable. PyQt does not use exception handling, so errors must always be discovered indirectly. In this
case, a null image means that for add- some reason we failed to load the image. If File () the load was
successful we add the new filename to the recently used files list, where it will appear only if another
file is subsequently opened, or if this one is saved under another name. Next, we set the instance
image variable to be a null image: This means that we are free to reset the checkable actions to our
preferred defaults without any side effects. This works because when the checkable actions are
changed, although the relevant methods will be called due to the signal–slot connections, the methods
do nothing if the image is null.
After the preliminaries, we assign the local image to the image instance variable and the local
filename to the filename instance variable. Next, we call showImage() to show the image at the
current zoom factor, clear the dirty flag, and update the size label. Finally, we call updateStatus() to
show the message in the status bar, and to update the log widget.
def fileSave(self):
if self.image.isNull():
return
if self.filename is None:
self.fileSaveAs()
else:
if self.image.save(self.filename, None):
self.updateStatus("Saved as %s" % self.filename)
self.dirty = False
else:
self.updateStatus("Failed to save %s" % self.filename)
The fileSave() method,and many others, act on the application’s data (a QImage instance), but make
no sense if there is no image data. For this reason, many of the methods do nothing and return
immediately if there is no image data for them to work on.
If there is image data, and the filename is None, the user must have invoked the “file new” action, and
is now saving their image for the first time. For this case, we pass on the work to the fileSaveAs()
method.