/usr/share/pyshared/spykeutils/plot/dialog.py is in python-spykeutils 0.4.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 | """ Contains a helper class for creating windows containing guiqwt plots.
"""
from PyQt4 import QtGui
from PyQt4.QtCore import Qt
from PyQt4.QtGui import (QDialog, QGridLayout, QToolBar, QHBoxLayout,
QVBoxLayout, QFrame, QWidget)
try:
from PyQt4 import QtCore
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
_fromUtf8 = lambda s: s
from PyQt4.QtGui import QColor, QScrollArea, QWidget, QFrame
from guiqwt.baseplot import BasePlot
from guiqwt.curve import CurvePlot
from guiqwt.image import ImagePlot
from guiqwt.plot import PlotManager
from guiqwt.shapes import Marker
from guiqwt.curve import CurveItem
from guiqwt.tools import (SelectTool, RectZoomTool, BasePlotMenuTool,
DeleteItemTool, ItemListPanelTool,
AntiAliasingTool, AxisScaleTool, DisplayCoordsTool,
ExportItemDataTool, ItemCenterTool, SignalStatsTool,
ColormapTool, ReverseYAxisTool, AspectRatioTool,
ContrastPanelTool, XCSPanelTool, YCSPanelTool,
CrossSectionTool, AverageCrossSectionTool,
SaveAsTool, PrintTool, CopyToClipboardTool)
from guiqwt.signals import SIG_PLOT_AXIS_CHANGED
from guidata.configtools import get_icon
import icons_rc
import guiqwt_tools
# Monkeypatch curve and image plot so synchronizing axes works with all tools
def fixed_do_zoom_rect_view(self, *args, **kwargs):
CurvePlot.old_do_zoom_rect_view(self, *args, **kwargs)
self.emit(SIG_PLOT_AXIS_CHANGED, self)
CurvePlot.old_do_zoom_rect_view = CurvePlot.do_zoom_rect_view
CurvePlot.do_zoom_rect_view = fixed_do_zoom_rect_view
def fixed_do_autoscale(self, *args, **kwargs):
CurvePlot.old_do_autoscale(self, *args, **kwargs)
if not isinstance(self, ImagePlot):
self.emit(SIG_PLOT_AXIS_CHANGED, self)
CurvePlot.old_do_autoscale = CurvePlot.do_autoscale
CurvePlot.do_autoscale = fixed_do_autoscale
def fixed_do_autoscale_image(self, *args, **kwargs):
ImagePlot.old_do_autoscale(self, *args, **kwargs)
self.emit(SIG_PLOT_AXIS_CHANGED, self)
ImagePlot.old_do_autoscale = ImagePlot.do_autoscale
ImagePlot.do_autoscale = fixed_do_autoscale_image
class PlotDialog(QDialog, PlotManager):
""" Implements a dialog to which an arbitrary number of plots can be
added.
This class implements a `QDialog` with a number of plots on it. The
axes of the plots can be arbitrarily synchronized and option checkboxes
can be added which provide callbacks when the checkbox state changes.
:param str wintitle: Title of the window.
:param bool major_grid: Show major grid in plots.
:param bool minor_grid: Show minor grid in plots.
:param bool toolbar: Show toolbar.
:param parent: Parent window.
:param panels: A list of guiqwt panels to be added to the window.
:param int min_plot_width: Default minimum width for plots.
:param int min_plot_height: Default minimum height for plots.
"""
def __init__(self, wintitle='Plot window', major_grid=True,
minor_grid=False, toolbar=True, parent=None,
panels=None, min_plot_width=100, min_plot_height=75):
QDialog.__init__(self, parent)
self.setWindowFlags(Qt.Window)
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap(_fromUtf8(':/Application/Main')),
QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.setWindowIcon(icon)
self.major_grid = major_grid
self.minor_grid = minor_grid
self.min_plot_width = min_plot_width
self.min_plot_height = min_plot_height
# WidgetMixin copy
PlotManager.__init__(self, main=self)
self.main_layout = QVBoxLayout(self)
self.color_layout = QHBoxLayout()
self.plot_layout = QGridLayout()
self.plot_layout.setMargin(0)
self.plot_scroll_widget = QWidget()
self.plot_scroll_area = QScrollArea()
self.plot_scroll_area.setFrameShape(QFrame.NoFrame)
self.plot_scroll_area.setWidgetResizable(True)
self.option_layout = QHBoxLayout()
self.plot_widget = None
if panels is not None:
for panel in panels:
self.add_panel(panel)
self.toolbar = QToolBar('Tools')
if not toolbar:
self.toolbar.hide()
# Configuring widget layout
self._setup_widget_properties(wintitle=wintitle, icon=icon)
self._setup_widget_layout()
# Options
self.option_callbacks = {}
self.legend = None
self.axis_syncplots = {}
def _setup_widget_properties(self, wintitle, icon):
self.setWindowTitle(wintitle)
if isinstance(icon, basestring):
icon = get_icon(icon)
self.setWindowIcon(icon)
self.setMinimumSize(320, 240)
self.resize(720, 540)
def _setup_widget_layout(self):
self.main_layout.addWidget(self.toolbar)
self.main_layout.addLayout(self.color_layout)
self.main_layout.addWidget(self.plot_scroll_area)
self.plot_scroll_area.setWidget(self.plot_scroll_widget)
self.plot_scroll_widget.setLayout(self.plot_layout)
self.main_layout.addLayout(self.option_layout)
self.setLayout(self.main_layout)
def add_custom_curve_tools(self, antialiasing=True,
activate_zoom=True,
signal_stats=False):
""" Adds typically needed curve tools to the window.
:param bool antialiasing: Determines if the antialiasing tool is
added.
:param bool activate_zoom: Determines if the zoom tool is activated
initially (otherwise, the selection tool will be activated).
:param bool signal_stats: Determines if the signal stats tool is
available.
"""
self.add_toolbar(self.toolbar)
t = self.add_tool(SelectTool)
if not activate_zoom:
self.set_default_tool(t)
self.add_tool(BasePlotMenuTool, "item")
self.add_tool(ExportItemDataTool)
try: # Old versions of guiqwt and spyderlib do not support this
import spyderlib.widgets.objecteditor
from guiqwt.tools import EditItemDataTool
self.add_tool(EditItemDataTool)
except ImportError:
pass
self.add_tool(ItemCenterTool)
self.add_tool(DeleteItemTool)
self.add_separator_tool()
t = self.add_tool(RectZoomTool)
if activate_zoom:
self.set_default_tool(t)
self.add_tool(guiqwt_tools.HomeTool)
self.add_tool(guiqwt_tools.PanTool)
self.add_separator_tool()
self.add_tool(BasePlotMenuTool, "grid")
self.add_tool(BasePlotMenuTool, "axes")
self.add_tool(DisplayCoordsTool)
if self.get_itemlist_panel():
self.add_tool(ItemListPanelTool)
if signal_stats:
self.add_separator_tool()
self.add_tool(SignalStatsTool)
if antialiasing:
self.add_tool(AntiAliasingTool)
self.add_tool(AxisScaleTool)
self.add_separator_tool()
self.add_tool(SaveAsTool)
self.add_tool(CopyToClipboardTool)
self.add_tool(PrintTool)
self.add_tool(guiqwt_tools.HelpTool)
self.add_separator_tool()
self.get_default_tool().activate()
def add_custom_image_tools(self, activate_zoom=True):
""" Adds typically needed image tools to the window.
"""
self.add_toolbar(self.toolbar)
t = self.add_tool(SelectTool)
if not activate_zoom:
self.set_default_tool(t)
self.add_tool(BasePlotMenuTool, "item")
self.add_tool(ExportItemDataTool)
try: # Old versions of guiqwt and spyderlib do not support this
import spyderlib.widgets.objecteditor
from guiqwt.tools import EditItemDataTool
self.add_tool(EditItemDataTool)
except ImportError:
pass
self.add_tool(ItemCenterTool)
self.add_tool(DeleteItemTool)
self.add_separator_tool()
t = self.add_tool(RectZoomTool)
if activate_zoom:
self.set_default_tool(t)
self.add_tool(guiqwt_tools.HomeTool)
self.add_tool(guiqwt_tools.PanTool)
self.add_separator_tool()
self.add_tool(BasePlotMenuTool, "grid")
self.add_tool(BasePlotMenuTool, "axes")
self.add_tool(DisplayCoordsTool)
if self.get_itemlist_panel():
self.add_tool(ItemListPanelTool)
self.add_separator_tool()
self.add_tool(ColormapTool)
self.add_tool(ReverseYAxisTool)
self.add_tool(AspectRatioTool)
if self.get_contrast_panel():
self.add_tool(ContrastPanelTool)
if self.get_xcs_panel() and self.get_ycs_panel():
self.add_tool(XCSPanelTool)
self.add_tool(YCSPanelTool)
self.add_tool(CrossSectionTool)
self.add_tool(AverageCrossSectionTool)
self.add_separator_tool()
self.add_tool(SaveAsTool)
self.add_tool(CopyToClipboardTool)
self.add_tool(PrintTool)
self.add_tool(guiqwt_tools.HelpTool)
self.add_separator_tool()
self.get_default_tool().activate()
def add_option(self, name, change_callback, active=False):
""" Add an option (using a checkbox) to the window.
:param str name: The name of the option.
:param func change_callback: A function accepting the new state as
a parameter. The function will be called whenever the state
of the option changes.
:param bool active: Determines if the option is activated initially.
"""
checkBox = QtGui.QCheckBox(self)
checkBox.setChecked(active)
checkBox.setText(name)
checkBox.stateChanged.connect(self._option_callback)
self.option_callbacks[checkBox] = change_callback
self.option_layout.addWidget(checkBox)
def add_x_synchronization_option(self, active, ids=None):
""" Offer an option for X axes synchronization. This method should
be called after show(), so that a proper initial synchronization
can be performed.
:param bool active: Determines whether the axes are synchronized
initially.
:param sequence ids: List of plot ids to synchronize.
"""
self.axis_syncplots[BasePlot.X_BOTTOM] = ids
if active and ids:
self.synchronize_axis(BasePlot.X_BOTTOM)
self.add_option('Synchronize X Axes',
PlotDialog._synchronization_option_x, active)
def add_y_synchronization_option(self, active, ids=None):
""" Offer an option for Y axes synchronization. This method should
be called after show(), so that a proper initial synchronization
can be performed.
:param bool active: Determines whether the axes are synchronized
initially
:param sequence ids: List of plot ids to synchronize.
"""
self.axis_syncplots[BasePlot.Y_LEFT] = ids
if active and ids:
self.synchronize_axis(BasePlot.Y_LEFT)
self.add_option('Synchronize Y Axes',
PlotDialog._synchronization_option_y, active)
def _synchronization_option_x(self, state):
""" Callback for x-axis synchronization
"""
if state:
self.synchronize_axis(BasePlot.X_BOTTOM)
else:
self.unsynchronize_axis(BasePlot.X_BOTTOM)
def _synchronization_option_y(self, state):
""" Callback for y-axis synchronization
"""
if state:
self.synchronize_axis(BasePlot.Y_LEFT)
else:
self.unsynchronize_axis(BasePlot.Y_LEFT)
def replace_colors(self, replace_list):
""" Replace colors of items in all plots.
This can be useful when changing the background color to black
and black items should be drawn in white:
``replace_colors([('#000000', '#ffffff']))``
:param list replace_list: A list of tuples of Qt color names. The
first color in each tuple is replaced by the second color.
"""
for plot in self.plots.itervalues():
for i in plot.get_items():
if isinstance(i, CurveItem):
pen = i.pen()
elif isinstance(i, Marker):
pen = i.linePen()
else:
continue
for color in replace_list:
c1 = QColor(color[0])
c2 = QColor(color[1])
if pen.color() != c1:
continue
pen.setColor(c2)
break
if isinstance(i, CurveItem):
i.setPen(pen)
elif isinstance(i, Marker):
i.setLinePen(pen)
plot.replot()
def set_background_color(self, color):
""" Set the background color for all plots.
:param str color: A Qt color name (e.g. '#ff0000')
"""
for p in self.plots.itervalues():
p.setCanvasBackground(QColor(color))
p.replot()
def add_unit_color(self, color, name='Unit color:'):
""" Create a small legend on top of the window with only one entry.
:param str color: A Qt color name (e.g. '#ff0000')
:param str name: The name of the legend item. It will be displayed
on the left of the color.
"""
label = QtGui.QLabel(self)
label.setText(name)
label.setAlignment(Qt.AlignRight)
self.color_layout.addWidget(label)
label = QtGui.QLabel(self)
label.setStyleSheet('background-color:' + str(color))
label.setFrameShape(QFrame.StyledPanel)
label.setMaximumWidth(80)
self.color_layout.addWidget(label)
def add_custom_label(self, legend_string):
""" Add a label on the right of the plots
:param str legend_string: An arbitrary string (which can contain
newlines) to display on the right of the plots
"""
label = QtGui.QLabel(self)
label.setText(legend_string)
self.plot_layout.addWidget(
label, 0, self.plot_layout.columnCount(), -1, 1)
def add_color_legend(self, legend, show_option=None):
""" Create a legend on the right of the plots with colors and names.
:param sequence legend: List of (color, text) tuples, where `color`
is a Qt color name (e.g. '#ff0000') and `text` is the
corresponding text to display in the legend.
:param bool show_option: Determines whether a toggle for the legend
will be shown (if the parameter is not ``None``) and if the legend
is visible initially (``True``/``False``).
"""
widget = QWidget(self)
layout = QGridLayout(widget)
widget.setLayout(layout)
for l in legend:
label = QtGui.QLabel(self)
label.setStyleSheet('background-color:' + str(l[0]))
label.setFrameShape(QFrame.StyledPanel)
label.setMaximumWidth(80)
label.setMaximumHeight(12)
layout.addWidget(label, layout.rowCount(), 0, 1, 1)
label = QtGui.QLabel(self)
label.setText(l[1])
layout.addWidget(label, layout.rowCount() - 1, 1, 1, 1)
self.plot_layout.addWidget(
widget, 0, self.plot_layout.columnCount(), -1, 1)
if show_option is not None:
widget.setVisible(show_option)
self.add_option(
'Show Legend Sidebar', lambda w, s: widget.setVisible(s),
show_option)
def add_legend_option(self, legends, active):
""" Create a user option to show or hide a list of legend objects.
:param sequence legends: The legend objects affected by the option.
:param bool active: Determines whether the legends will be visible
initially.
"""
self.legends = legends
self._set_legend_visibility(active)
self.add_option('Show legend', self._legend_callback, active)
if active:
self._set_legend_visibility(True)
def _option_callback(self, state):
self.option_callbacks[self.sender()](self, state)
#noinspection PyUnusedLocal
def _legend_callback(self, win, state):
self._set_legend_visibility(state > 0)
def _set_legend_visibility(self, visible):
for p in self.plots.itervalues():
for l in self.legends:
p.set_item_visible(l, visible)
def add_plot_widget(self, plot_widget, plot_id, row=-1, column=0,
min_plot_width=None, min_plot_height=None):
""" Adds a guiqwt plot to the window.
:param plot_widget: The plot to add.
:type plot_widget: guiqwt plot widget
:param int plot_id: The id of the new plot.
:param int row: The row of the new plot. If this is -1, the new plot
will be added in a new row (if `column` is 0) or
in the last row.
:param int column: The column of the new plot.
:param int min_plot_width: The minimum width of this plot. If
``None``, the default minimum width for this dialog
is used.
:param int max_plot_height: The minimum height of this plot. If
``None``, the default minimum height for this dialog
is used.
"""
if row == -1:
if column == 0:
row = self.plot_layout.rowCount()
else:
row = self.plot_layout.rowCount() - 1
pw = min_plot_width
if pw is None:
pw = self.min_plot_width
ph = min_plot_height
if ph is None:
ph = self.min_plot_height
plot_widget.setMinimumSize(pw, ph)
self.plot_layout.addWidget(plot_widget, row, column)
new_plot = plot_widget.plot
self.add_plot(new_plot, plot_id)
def synchronize_axis(self, axis, plots=None):
if plots is None:
if axis in self.axis_syncplots:
plots = self.axis_syncplots[axis]
else:
plots = self.plots.keys()
if len(plots) < 1:
return
PlotManager.synchronize_axis(self, axis, plots)
# Find interval that needs to be shown in order to include all
# currently shown parts in the synchronized plots
plot_objects = [self.plots[p] for p in plots]
lb = min((p.axisScaleDiv(axis).lowerBound() for p in plot_objects))
ub = max((p.axisScaleDiv(axis).upperBound() for p in plot_objects))
for p in plot_objects:
p.setAxisScale(axis, lb, ub)
p.replot()
def unsynchronize_axis(self, axis, plots=None):
if plots is None:
if axis in self.axis_syncplots:
plots = self.axis_syncplots[axis]
else:
plots = self.plots.keys()
for plot_id in plots:
if not plot_id in self.synchronized_plots:
continue
synclist = self.synchronized_plots[plot_id]
for plot2_id in plots:
if plot_id == plot2_id:
continue
item = (axis, plot2_id)
if item in synclist:
synclist.remove(item)
def plot_axis_changed(self, plot):
ids = [k for k, p in self.plots.iteritems() if p == plot]
if len(ids) < 1:
return
plot_id = ids[0]
if plot_id not in self.synchronized_plots:
return
for (axis, other_plot_id) in self.synchronized_plots[plot_id]:
scalediv = plot.axisScaleDiv(axis)
other = self.get_plot(other_plot_id)
lb = scalediv.lowerBound()
ub = scalediv.upperBound()
other.setAxisScale(axis, lb, ub)
other.replot()
def set_plot_title(self, plot, title):
""" Set the title of a guiqwt plot and use the same font as for the
rest of the window.
:param plot: The plot for which the title is set.
:param str title: The new title of the plot.
"""
plot.setTitle(title)
l = plot.titleLabel()
l.setFont(self.font())
plot.setTitle(l.text())
def show(self):
for p in self.plots.itervalues():
if not self.minor_grid:
p.grid.gridparam.min_xenabled = False
p.grid.gridparam.min_yenabled = False
if not self.major_grid:
p.grid.gridparam.maj_xenabled = False
p.grid.gridparam.maj_yenabled = False
p.grid.update_params()
super(PlotDialog, self).show()
|