Commit a4cb9232 authored by Valentin Valls's avatar Valentin Valls
Browse files

Create a dedicated component for DurationAction

parent ea6dfd6f
......@@ -11,88 +11,13 @@ import logging
import numpy
from silx.gui import qt
from silx.gui import icons
from silx.gui.plot import Plot1D
from silx.gui.plot.items import axis as axis_mdl
from bliss.flint.widgets.utils.duration_action import DurationAction
_logger = logging.getLogger(__name__)
class DurationAction(qt.QAction):
valueChanged = qt.Signal(float)
def __init__(self):
super(DurationAction, self).__init__()
self.__duration = None
self.__durations = {}
self.__menu = qt.QMenu()
self.__menu.aboutToShow.connect(self.__menuAboutToShow)
self.setMenu(self.__menu)
def __menuAboutToShow(self):
menu = self.sender()
menu.clear()
currentDuration = self.__duration
currentWasFound = False
group = qt.QActionGroup(menu)
group.setExclusive(True)
for value, (label, icon) in self.__durations.items():
action = qt.QAction()
action.setText(label)
action.setData(value)
action.setIcon(icon)
action.setCheckable(True)
if currentDuration == value:
action.setChecked(True)
currentWasFound = True
group.addAction(action)
menu.addAction(action)
if currentDuration is not None and not currentWasFound:
menu.addSeparator()
action = qt.QAction()
action.setText(f"{currentDuration}s")
action.setData(currentDuration)
action.setCheckable(True)
action.setChecked(True)
currentWasFound = True
group.addAction(action)
menu.addAction(action)
group.triggered.connect(self.__actionSelected)
def __actionSelected(self, action):
duration = action.data()
self.setDuration(duration)
def setDuration(self, duration):
if self.__duration == duration:
return
self.__duration = duration
self.__updateLookAndFeel()
self.valueChanged.emit(duration)
def addDuration(self, label, value, icon):
if isinstance(icon, str):
icon = icons.getQIcon(icon)
self.__durations[value] = label, icon
def duration(self):
"""Return a duration in second"""
return self.__duration
def __updateLookAndFeel(self):
duration = self.__duration
label, icon = self.__durations.get(duration, (None, None))
if icon is None:
icon = icons.getQIcon("flint:icons/duration-x")
if label is None:
label = f"{duration}s"
self.setToolTip(f"Duration of {label} selected")
self.setIcon(icon)
class TimeCurvePlot(qt.QWidget):
"""Curve plot which handle data following the time
......@@ -115,13 +40,13 @@ class TimeCurvePlot(qt.QWidget):
self.__durationAction = DurationAction()
self.__durationAction.setCheckable(True)
self.__durationAction.setChecked(True)
self.__durationAction.addDuration("1h", 60 * 60, "flint:icons/duration-1h")
self.__durationAction.addDuration("30m", 30 * 60, "flint:icons/duration-30m")
self.__durationAction.addDuration("10m", 10 * 60, "flint:icons/duration-10m")
self.__durationAction.addDuration("5m", 5 * 60, "flint:icons/duration-5m")
self.__durationAction.addDuration("2m", 2 * 60, "flint:icons/duration-2m")
self.__durationAction.addDuration("1m", 1 * 60, "flint:icons/duration-1m")
self.__durationAction.addDuration("30s", 30, "flint:icons/duration-30s")
self.__durationAction.addDuration("1h", 60 * 60)
self.__durationAction.addDuration("30m", 30 * 60)
self.__durationAction.addDuration("10m", 10 * 60)
self.__durationAction.addDuration("5m", 5 * 60)
self.__durationAction.addDuration("2m", 2 * 60)
self.__durationAction.addDuration("1m", 1 * 60)
self.__durationAction.addDuration("30s", 30)
self.__durationAction.setDuration(self.__duration)
self.__durationAction.valueChanged.connect(self.__durationChanged)
......
# -*- coding: utf-8 -*-
#
# This file is part of the bliss project
#
# Copyright (c) 2015-2022 Beamline Control Unit, ESRF
# Distributed under the GNU LGPLv3. See LICENSE for more info.
from __future__ import annotations
import typing
from silx.gui import qt
from silx.gui import icons
class DurationAction(qt.QAction):
valueChanged = qt.Signal(float)
DEFAULT_ICONS = {
60 * 60: "flint:icons/duration-1h",
30 * 60: "flint:icons/duration-30m",
10 * 60: "flint:icons/duration-10m",
5 * 60: "flint:icons/duration-5m",
2 * 60: "flint:icons/duration-2m",
1 * 60: "flint:icons/duration-1m",
30: "flint:icons/duration-30s",
}
def __init__(self, parent=None):
super(DurationAction, self).__init__(parent)
self.__duration = None
self.__durations = {}
self.__menu = qt.QMenu(parent)
self.__menu.aboutToShow.connect(self.__menuAboutToShow)
self.setMenu(self.__menu)
def __menuAboutToShow(self):
menu = self.sender()
menu.clear()
currentDuration = self.__duration
currentWasFound = False
group = qt.QActionGroup(menu)
group.setExclusive(True)
for value, (label, icon) in self.__durations.items():
action = qt.QAction()
action.setText(label)
action.setData(value)
action.setIcon(icon)
action.setCheckable(True)
if currentDuration == value:
action.setChecked(True)
currentWasFound = True
group.addAction(action)
menu.addAction(action)
if currentDuration is not None and not currentWasFound:
menu.addSeparator()
action = qt.QAction()
action.setText(f"{currentDuration}s")
action.setData(currentDuration)
action.setCheckable(True)
action.setChecked(True)
currentWasFound = True
group.addAction(action)
menu.addAction(action)
group.triggered.connect(self.__actionSelected)
def __actionSelected(self, action):
duration = action.data()
self.setDuration(duration)
def setDuration(self, duration):
if self.__duration == duration:
return
self.__duration = duration
self.__updateLookAndFeel()
self.valueChanged.emit(duration)
def addDuration(
self, label: str, value: float, icon: typing.Union[str, qt.QIcon] = None
):
"""Add a selectable duration in second
Attributes:
label: Text to display with the item
value: Duration in second
icon: silx icon id to display with the item, else a default icon is
loaded.
"""
if icon is None:
icon = self.DEFAULT_ICONS[value]
if isinstance(icon, str):
icon = icons.getQIcon(icon)
self.__durations[value] = label, icon
def duration(self) -> float:
"""Return a duration in second"""
return self.__duration
def __updateLookAndFeel(self):
duration = self.__duration
label, icon = self.__durations.get(duration, (None, None))
if icon is None:
icon = icons.getQIcon("flint:icons/duration-x")
if label is None:
label = f"{duration}s"
self.setToolTip(f"Duration of {label} selected")
self.setIcon(icon)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment