PlotPyStack / guiqwt

Efficient 2D plotting Python library based on PythonQwt
https://pypi.python.org/pypi/guiqwt
Other
80 stars 20 forks source link

Examples for mouse interaction with widgets (NOT dialogs) needed. #96

Closed marcel-goldschen-ohm closed 1 year ago

marcel-goldschen-ohm commented 2 years ago

It would be very helpful to have some example code for adding mouse interaction to plot curve and image widgets (NOT dialogs). If I've missed something please point me in the right direction, but note that current examples with dialogs such as ImageDialog don't work with an ImageWidget that is in a custom UI as far as I can tell. For example, the following does NOT allow dragging a zoom rectangle with the mouse:

widget = guiqwt.plot.ImageWidget()
widget.add_tool(guiqwt.tools.RectZoomTool)
widget.set_active_tool(widget.get_tool(guiqwt.tools.RectZoomTool))
im = guiqwt.image.TrImageItem(data=np.random.rand(512, 256))
widget.plot.add_item(im)

It is not clear to me what is the correct procedure to get something like a mouse zoom functioning? Any help would be appreciated.

Cheers

PierreRaybaut commented 1 year ago

Hi there,

Very sorry for the late answer ; I've been busy these last months.

Here is a simple example derived from your own, providing a toolbar that you may used for selecting the right tool:

import numpy as np
from qtpy import QtWidgets as QW
import guidata
import guiqwt.plot as gqp
import guiqwt.image as gqi

class Window(QW.QMainWindow):
    """Custom window"""

    def __init__(self):
        super().__init__()
        toolbar = QW.QToolBar("Tools")
        self.addToolBar(toolbar)
        self.imagewidget = gqp.ImageWidget()
        self.setCentralWidget(self.imagewidget)
        self.imagewidget.add_toolbar(toolbar)
        self.imagewidget.register_all_image_tools()

    def add_image(self, data):
        """Add image to plot"""
        item = gqi.TrImageItem(data)
        self.imagewidget.plot.add_item(item)

def test():
    """Test window"""
    app = guidata.qapplication()
    win = Window()
    win.add_image(np.random.rand(512, 256))
    win.show()
    app.exec()

if __name__ == "__main__":
    test()

Or even simpler (and maybe closer to your needs):

import numpy as np
import guidata
import guiqwt.plot as gqp
import guiqwt.image as gqi
import guiqwt.tools as gqt

def test():
    """Test window"""
    app = guidata.qapplication()
    widget = gqp.ImageWidget()
    widget.register_all_image_tools()
    tool = widget.get_tool(gqt.RectZoomTool)
    tool.activate()
    im = gqi.TrImageItem(data=np.random.rand(512, 256))
    widget.plot.add_item(im)
    widget.show()
    app.exec()

if __name__ == "__main__":
    test()