-
Notifications
You must be signed in to change notification settings - Fork 95
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Extend button editor to allow dynamically changing button image #1566
Conversation
The added demo is hopefully useful for documentation purposes of all the button editor's functionality. Note that there is other functionality not in demos, but it may not be working or not fully implemented, eg #879 |
TBH I am still sort of confused by where to use |
class ImageChoice(InstanceChoice): | ||
def get_view(self): | ||
return View( | ||
UItem('name', editor=ImageEditor(image=self.object)) | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I went with the InstanceEditor approach because trying to use ImageEnumEditor
was giving me trouble/ I couldn't get things working how I wanted them to.
This demo is now a little bit showcasing the secret InstanceEditor Api as well.
Also, relating to my other comment, it seems that if I had used the Image
trait correctly here, this code would be un needed as the Image trait sets the editor to ImageEditor for the ImageResource that is stored.
@rahulporuri |
traitsui/examples/demo/Advanced/Dynamically_changing_buttons_demo.py
Outdated
Show resolved
Hide resolved
@observe("image") | ||
def _image_updated(self, event): | ||
image = event.new | ||
self._control.image = image |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't seem right - should it be image.get_icon()
or image.get_image()
or something like that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So for Qt, image.create_icon()
returns a QtGui.QIcon
which can then be passed as the argument of the setIcon
method on the control.
In this case on wx, the _control
is a pyface.ui.wx.image_button.ImageButton
and control
is the control of that (ie self.control = self._control.control
) which is a wx.Window
. Neither of which have "icon" related methods, etc. ImageButton
simply has an image = Instance(ImageResource, allow_none=True)
trait, and so I set it directly to the ImageResource
instance from the image
trait here.
I agree it seems incorrect, and in fact it ends up working not exactly right, see the following screenshot:
ImageButton
has an _image_changed
method which sets
self._img = image.create_image()
self._image = self._img.ConvertToBitmap()
which seems like maybe we would want to use create_icon instead or something? I am not sure I was unable to figure this out. There is code in ImageButton.__init__
method which looks as though it is trying to figure out the size of the image and the size of the label (and that seems to work correctly - as in the above image looks as though it has the perfect button size to fit the image and label side by side). However, it seems both the image and label are getting drawn at the center which likely isn't intentional (?)
I suspect this is might be a problem in Imagebutton._on_paint
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also don't know how I'm just seeing this, but the dynamic label isn't working either!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The label can be fixed by overwriting the _label_changed
method in the traitsui.wx.button_editor.CustomEditor
class to be self._control.label = self.string_value(label)
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've opened enthought/pyface#932
Thank you for the feedback! I've made the suggested changes and I think with enthought/pyface#932 things look correct now. Also using |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mostly LGTM with a few question around the duplicated image files. Also, there seems to be unused "enthought-icon.png" files in this PR, which can probably be removed.
traitsui/qt4/button_editor.py
Outdated
@@ -25,8 +25,9 @@ | |||
|
|||
|
|||
from pyface.qt import QtCore, QtGui | |||
from pyface.ui_traits import Image |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if this doesn't exist in pyface.api
, let's create an issue in pyface.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i dont see an issue for this in pyface - not sure if you had written this down on your personal to-do list.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It actually is available from the api, I've updated the import statement on this PR
…d to copy images everywhere
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM 🎉
part of #1513
This PR adds an
image_value
trait to the custom style of the button editor (analogous to thelabel_value
trait) to allow for dynamically changing the buttons image. It also adds a demo example to showcase both of these features.