from functools import wraps
from qtpy import QtWidgets
from .histogram import HistogramWidget, HistogramModel, HistogramController
from .pdsspect_image_set import PDSSpectImageSetViewBase
[docs]class BasicHistogramModel(HistogramModel):
"""Model for the hhistograms in the Basic Widgets
Attributes
---------
connected_models : :obj:`list`
Other :class:`BasicHistogramModel` for other histograms
"""
def __init__(self, *args, **kwargs):
super(BasicHistogramModel, self).__init__(*args, **kwargs)
self.connected_models = []
def check_model_type(func):
@wraps(func)
def wrapper(self, model):
if not isinstance(model, BasicHistogramModel):
raise ValueError("Model must be a BasicHistogramModel object")
return func(self, model)
return wrapper
[docs] @check_model_type
def connect_model(self, model):
"""Connect another model to this model
Attributes
----------
model : :class:`BasicHistogramModel`
Connect the model to current model
Raises
------
ValueError
When :attr:`model` is not :class:`BasicHistogramModel`
"""
if model not in self.connected_models:
self.connected_models.append(model)
model.cuts = self.cuts
[docs] @check_model_type
def disconnect_model(self, model):
"""Disconnect another model from this model
Attributes
----------
model : :class:`BasicHistogramModel`
Disconnect the model from current model
Raises
------
ValueError
When :attr:`model` is not :class:`BasicHistogramModel`
"""
if model in self.connected_models:
self.connected_models.remove(model)
[docs] def disconnect_from_all_models(self):
"""Disconnect all models from this model"""
self.connected_models = []
[docs]class BasicHistogramController(HistogramController):
"""Controller for histogram views
Parameters
----------
model : :class:`BasicHistogramModel`
histogram model
view : :class:`object`
View with :class:`BasicHistogramModel` as its model
Attributes
----------
model : :class:`BasicHistogramModel`
histogram model
view : :class:`object`
View with :class:`BasicHistogramModel` as its model
"""
[docs] def set_cut_low(self, cut_low):
"""Set the low cut level to a new value
Parameters
----------
cut_low : :obj:`float`
New low cut value
"""
super(BasicHistogramController, self).set_cut_low(cut_low)
for model in self.model.connected_models:
model.cut_low = cut_low
[docs] def set_cut_high(self, cut_high):
"""Set the high cut level to a new value
Parameters
----------
cut_high : :obj:`float`
New high cut value
"""
super(BasicHistogramController, self).set_cut_high(cut_high)
for model in self.model.connected_models:
model.cut_high = cut_high
[docs] def set_cuts(self, cut_low, cut_high):
"""Set both the low and high cut levels
Parameters
----------
cut_low : :obj:`float`
New low cut value
cut_high : :obj:`float`
New high cut value
"""
super(BasicHistogramController, self).set_cuts(cut_low, cut_high)
for model in self.model.connected_models:
model.cuts = cut_low, cut_high
[docs] def restore(self):
"""Restore the histogram"""
super(BasicHistogramController, self).restore()
for model in self.model.connected_models:
model.restore()
[docs]class BasicController(object):
"""Controller for :class:`Basic` window
Parameters
----------
image_set : :class:`~.pdsspect_image_set.PDSSpectImageSet`
pdsspect model
view : :class:`Basic`
View to control
Attributes
----------
image_set : :class:`~.pdsspect_image_set.PDSSpectImageSet`
pdsspect model
view : :class:`Basic`
View to control
"""
def __init__(self, image_set, view):
self.image_set = image_set
self.view = view
[docs] def change_current_image_index(self, new_index):
"""Change the current image index to a new index
Parameters
----------
new_index : :obj:`int`
The new index for
:class:`~.pdsspect_image_set.PDSSpectImageSetViewBase.images` to
determine the current image
"""
self.image_set.current_image_index = new_index
[docs]class Basic(QtWidgets.QWidget, PDSSpectImageSetViewBase):
"""Window to apply cut levels and choose the current image
Parameters
----------
image_set : :class:`~.pdsspect_image_set.PDSSpectImageSet`
pdsspect model
view_canvas : :class:`~.pds_image_view_canvas.PDSImageViewCanvas`
Canvas to view the image
Attributes
----------
image_set : :class:`~.pdsspect_image_set.PDSSpectImageSet`
pdsspect model
view_canvas : :class:`~.pds_image_view_canvas.PDSImageViewCanvas`
Canvas to view the image
controller : :class:`BasicController`
Controller for view
image_menu : :class:`QtWidgets.QComboBox <PySide.QtGui.QComboBox>`
Drop down menu to pick the current image
histogram : :class:`~.histogram.HistogramModel`
Model for the :attr:`histogram_widget`
histogram_widget : :class:`BasicHistogramWidget`
The histogram widget to adjust the cut levels
layout : :class:`QtWidgets.QVBoxLayout <PySide.QtGui.QVBoxLayout>`
The main layout
"""
def __init__(self, image_set, view_canvas, basic_widget):
super(Basic, self).__init__(basic_widget)
self.image_set = image_set
self.image_set.register(self)
self.basic_widget = basic_widget
self.controller = BasicController(image_set, self)
self.view_canvas = view_canvas
self.image_menu = QtWidgets.QComboBox()
for image in self.image_set.images:
self.image_menu.addItem(image.image_name)
self.image_menu.setCurrentIndex(image_set.current_image_index)
self.image_menu.currentIndexChanged.connect(self.change_image)
self.histogram = BasicHistogramModel(self.view_canvas, bins=100)
self.histogram_widget = BasicHistogramWidget(self.histogram, self)
self.layout = QtWidgets.QVBoxLayout()
self.layout.addWidget(self.image_menu)
self.layout.addWidget(self.histogram_widget)
self.setLayout(self.layout)
self.histogram.set_data()
[docs] def change_image(self, new_index):
"""Change the image when new image selected in :attr:`image_menu`
Parameters
----------
new_index : :obj:`int`
The new index to determine the current image
"""
self.image_set.current_image.cuts = self.histogram.cuts
self.controller.change_current_image_index(new_index)
self.basic_widget.connect_model(self)
[docs] def set_image(self):
"""When the image is set, adjust the histogram"""
self.histogram.set_data()
self.histogram.restore()