# -*- coding: utf-8 -*-
"""
* :py:class:`.curve.GridItem`
.. autoclass:: GridItem
:members:
"""
from __future__ import annotations
import sys
from typing import TYPE_CHECKING
from guidata.utils.misc import assert_interfaces_valid
from qwt import QwtPlot, QwtPlotGrid
from plotpy.config import _
from plotpy.interfaces import IBasePlotItem, IDecoratorItemType
from plotpy.styles.base import GridParam
if TYPE_CHECKING:
from qtpy.QtCore import QPointF
from plotpy.interfaces import IItemType
from plotpy.styles.base import ItemParameters
[docs]
class GridItem(QwtPlotGrid):
"""Grid plot item
Args:
gridparam: Grid parameters
"""
__implements__ = (IBasePlotItem,)
_readonly = True
_private = False
_icon_name = "grid.png"
def __init__(self, gridparam: GridParam | None = None) -> None:
super().__init__()
if gridparam is None:
self.gridparam = GridParam(title=_("Grid"), icon="grid.png")
else:
self.gridparam = gridparam
self.selected = False
self.immutable = True # set to false to allow moving points around
self.update_params() # won't work completely because it's not yet
# attached to plot (actually, only canvas background won't be updated)
[docs]
def types(self) -> tuple[type[IItemType], ...]:
"""Returns a group or category for this item.
This should be a tuple of class objects inheriting from IItemType
Returns:
tuple: Tuple of class objects inheriting from IItemType
"""
return (IDecoratorItemType,)
[docs]
def attach(self, plot: QwtPlot) -> None:
"""Reimplemented to update plot canvas background"""
QwtPlotGrid.attach(self, plot)
self.update_params()
[docs]
def set_readonly(self, state: bool) -> None:
"""Set object readonly state
Args:
state: True if object is readonly, False otherwise
"""
self._readonly = state
[docs]
def is_readonly(self) -> bool:
"""Return object readonly state
Returns:
bool: True if object is readonly, False otherwise
"""
return self._readonly
[docs]
def set_private(self, state: bool) -> None:
"""Set object as private
Args:
state: True if object is private, False otherwise
"""
self._private = state
[docs]
def is_private(self) -> bool:
"""Return True if object is private
Returns:
bool: True if object is private, False otherwise
"""
return self._private
[docs]
def get_icon_name(self) -> str:
"""Return the icon name
Returns:
Icon name
"""
return self._icon_name
[docs]
def set_icon_name(self, icon_name: str) -> None:
"""Set the icon name
Args:
icon_name: Icon name
"""
self._icon_name = icon_name
[docs]
def set_selectable(self, state: bool) -> None:
"""Set item selectable state
Args:
state: True if item is selectable, False otherwise
"""
self._can_select = state
[docs]
def set_resizable(self, state: bool) -> None:
"""Set item resizable state
(or any action triggered when moving an handle, e.g. rotation)
Args:
state: True if item is resizable, False otherwise
"""
self._can_resize = state
[docs]
def set_movable(self, state: bool) -> None:
"""Set item movable state
Args:
state: True if item is movable, False otherwise
"""
self._can_move = state
[docs]
def set_rotatable(self, state: bool) -> None:
"""Set item rotatable state
Args:
state: True if item is rotatable, False otherwise
"""
self._can_rotate = state
[docs]
def can_select(self) -> bool:
"""
Returns True if this item can be selected
Returns:
bool: True if item can be selected, False otherwise
"""
return False
[docs]
def can_resize(self) -> bool:
"""
Returns True if this item can be resized
Returns:
bool: True if item can be resized, False otherwise
"""
return False
[docs]
def can_rotate(self) -> bool:
"""
Returns True if this item can be rotated
Returns:
bool: True if item can be rotated, False otherwise
"""
return False
[docs]
def can_move(self) -> bool:
"""
Returns True if this item can be moved
Returns:
bool: True if item can be moved, False otherwise
"""
return False
[docs]
def select(self) -> None:
"""
Select the object and eventually change its appearance to highlight the
fact that it's selected
"""
[docs]
def unselect(self) -> None:
"""
Unselect the object and eventually restore its original appearance to
highlight the fact that it's not selected anymore
"""
[docs]
def hit_test(self, pos: QPointF) -> tuple[float, float, bool, None]:
"""Return a tuple (distance, attach point, inside, other_object)
Args:
pos: Position
Returns:
tuple: Tuple with four elements: (distance, attach point, inside,
other_object).
Description of the returned values:
* distance: distance in pixels (canvas coordinates) to the closest
attach point
* attach point: handle of the attach point
* inside: True if the mouse button has been clicked inside the object
* other_object: if not None, reference of the object which will be
considered as hit instead of self
"""
return sys.maxsize, 0, False, None
[docs]
def move_local_point_to(self, handle: int, pos: QPointF, ctrl: bool = None) -> None:
"""Move a handle as returned by hit_test to the new position
Args:
handle: Handle
pos: Position
ctrl: True if <Ctrl> button is being pressed, False otherwise
"""
[docs]
def move_local_shape(self, old_pos: QPointF, new_pos: QPointF) -> None:
"""Translate the shape such that old_pos becomes new_pos in canvas coordinates
Args:
old_pos: Old position
new_pos: New position
"""
[docs]
def move_with_selection(self, delta_x: float, delta_y: float) -> None:
"""Translate the item together with other selected items
Args:
delta_x: Translation in plot coordinates along x-axis
delta_y: Translation in plot coordinates along y-axis
"""
[docs]
def update_params(self):
"""Update object parameters (dataset) from object properties"""
self.gridparam.update_grid(self)
[docs]
def update_item_parameters(self) -> None:
"""Update item parameters (dataset) from object properties"""
self.gridparam.update_param(self)
[docs]
def get_item_parameters(self, itemparams: ItemParameters) -> None:
"""
Appends datasets to the list of DataSets describing the parameters
used to customize apearance of this item
Args:
itemparams: Item parameters
"""
itemparams.add("GridParam", self, self.gridparam)
[docs]
def set_item_parameters(self, itemparams: ItemParameters) -> None:
"""
Change the appearance of this item according
to the parameter set provided
Args:
itemparams: Item parameters
"""
self.gridparam: GridParam = itemparams.get("GridParam")
self.gridparam.update_grid(self)
assert_interfaces_valid(GridItem)