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
|
# coding: utf-8
# /*##########################################################################
#
# Copyright (C) 2016-2018 European Synchrotron Radiation Facility
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# ###########################################################################*/
__authors__ = ["V. Valls"]
__license__ = "MIT"
__date__ = "16/10/2020"
import logging
import os.path
from silx.gui import qt
import silx.gui.plot
import silx.gui.icons
from silx.gui.plot.tools import PositionInfo
import pyFAI.utils
from .AbstractCalibrationTask import AbstractCalibrationTask
from ..CalibrationContext import CalibrationContext
from ..helper.SynchronizeRawView import SynchronizeRawView
from ..helper.MarkerManager import MarkerManager
from ..helper.SynchronizeMaskToolColor import SynchronizeMaskToolColor
from ..helper.SynchronizePlotBackground import SynchronizePlotBackground
_logger = logging.getLogger(__name__)
class _MaskToolsWidget(silx.gui.plot.MaskToolsWidget.MaskToolsWidget):
"""Inherite the silx mask to be able to save and restore internally
imported/exported masks to the application model."""
sigUserMaskChanged = qt.Signal()
"""Emitted when the user changes the mask.
sigMaskChanged on silx 0.9 to not provide that. This signal is used with a
filter.
"""
def __init__(self, parent=None, plot=None):
silx.gui.plot.MaskToolsWidget.MaskToolsWidget.__init__(self, parent=parent, plot=plot)
self.__syncColor = SynchronizeMaskToolColor(self)
self.sigMaskChanged.connect(self.__emitUserMaskChanged)
def __emitUserMaskChanged(self):
self.sigUserMaskChanged.emit()
def __extractDirectory(self, filename):
if filename is not None and filename != "":
if os.path.exists(filename):
if os.path.isdir(filename):
return filename
else:
return os.path.dirname(filename)
return None
@property
def maskFileDir(self):
"""The directory from which to load/save mask from/to files."""
model = CalibrationContext.instance().getCalibrationModel()
experimentSettings = model.experimentSettingsModel()
# Reach from the previous mask
previousFile = experimentSettings.mask().filename()
directory = self.__extractDirectory(previousFile)
if directory is None:
previousFile = experimentSettings.image().filename()
directory = self.__extractDirectory(previousFile)
if directory is None:
directory = os.getcwd()
return directory
@maskFileDir.setter
def maskFileDir(self, maskFileDir):
# We dont need to store it
pass
def save(self, filename, kind):
try:
result = silx.gui.plot.MaskToolsWidget.MaskToolsWidget.save(self, filename, kind)
self.__maskFilenameUpdated(filename)
finally:
pass
return result
def load(self, filename):
"""Override the fuction importing a new mask."""
try:
result = silx.gui.plot.MaskToolsWidget.MaskToolsWidget.load(self, filename)
self.__maskFilenameUpdated(filename)
finally:
pass
return result
def __maskFilenameUpdated(self, filename):
model = CalibrationContext.instance().getCalibrationModel()
experimentSettings = model.experimentSettingsModel()
with experimentSettings.mask().lockContext() as mask:
mask.setFilename(filename)
mask.setSynchronized(True)
def setSelectionMask(self, mask, copy=True):
self.sigMaskChanged.disconnect(self.__emitUserMaskChanged)
result = super(_MaskToolsWidget, self).setSelectionMask(mask, copy=copy)
self.sigMaskChanged.connect(self.__emitUserMaskChanged)
return result
def showEvent(self, event):
self.sigMaskChanged.disconnect(self.__emitUserMaskChanged)
result = silx.gui.plot.MaskToolsWidget.MaskToolsWidget.showEvent(self, event)
self.sigMaskChanged.connect(self.__emitUserMaskChanged)
return result
class MaskTask(AbstractCalibrationTask):
def _initGui(self):
qt.loadUi(pyFAI.utils.get_ui_file("calibration-mask.ui"), self)
icon = silx.gui.icons.getQIcon("pyfai:gui/icons/task-mask")
self.setWindowIcon(icon)
self.initNextStep()
self.__plot = None
self.__plot = self.__createPlot(self._imageHolder)
self.__plot.setObjectName("plot-mask")
self.__plotBackground = SynchronizePlotBackground(self.__plot)
markerModel = CalibrationContext.instance().getCalibrationModel().markerModel()
self.__markerManager = MarkerManager(self.__plot, markerModel, pixelBasedPlot=True)
handle = self.__plot.getWidgetHandle()
handle.setContextMenuPolicy(qt.Qt.CustomContextMenu)
handle.customContextMenuRequested.connect(self.__plotContextMenu)
self.__maskPanel = _MaskToolsWidget(parent=self._toolHolder, plot=self.__plot)
self.__maskPanel.setDirection(qt.QBoxLayout.TopToBottom)
self.__maskPanel.setMultipleMasks("single")
layout = self.__maskPanel.layout()
layout.setContentsMargins(0, 0, 0, 0)
layout = qt.QVBoxLayout(self._toolHolder)
layout.addWidget(self.__maskPanel)
layout.setContentsMargins(0, 0, 0, 0)
self._toolHolder.setLayout(layout)
layout = qt.QVBoxLayout(self._imageHolder)
layout.addWidget(self.__plot)
layout.setContentsMargins(1, 1, 1, 1)
self._imageHolder.setLayout(layout)
self.__maskPanel.sigUserMaskChanged.connect(self.__maskFromPlotChanged)
self.widgetShow.connect(self.__widgetShow)
self.widgetHide.connect(self.__widgetHide)
self.__plotMaskChanged = False
self.__modelMaskChanged = False
self.__synchronizeRawView = SynchronizeRawView()
self.__synchronizeRawView.registerTask(self)
self.__synchronizeRawView.registerPlot(self.__plot)
def __plotContextMenu(self, pos):
plot = self.__plot
from silx.gui.plot.actions.control import ZoomBackAction
zoomBackAction = ZoomBackAction(plot=plot, parent=plot)
menu = qt.QMenu(self)
menu.addAction(zoomBackAction)
menu.addSeparator()
menu.addAction(self.__markerManager.createMarkPixelAction(menu, pos))
menu.addAction(self.__markerManager.createMarkGeometryAction(menu, pos))
action = self.__markerManager.createRemoveClosestMaskerAction(menu, pos)
if action is not None:
menu.addAction(action)
handle = plot.getWidgetHandle()
menu.exec_(handle.mapToGlobal(pos))
def __createPlot(self, parent):
plot = silx.gui.plot.PlotWidget(parent=parent)
plot.setKeepDataAspectRatio(True)
plot.setDataMargins(0.1, 0.1, 0.1, 0.1)
plot.setGraphXLabel("Y")
plot.setGraphYLabel("X")
plot.setAxesDisplayed(False)
colormap = CalibrationContext.instance().getRawColormap()
plot.setDefaultColormap(colormap)
from silx.gui.plot import tools
toolBar = tools.InteractiveModeToolBar(parent=self, plot=plot)
plot.addToolBar(toolBar)
toolBar = tools.ImageToolBar(parent=self, plot=plot)
colormapDialog = CalibrationContext.instance().getColormapDialog()
toolBar.getColormapAction().setColorDialog(colormapDialog)
plot.addToolBar(toolBar)
statusBar = self.__createPlotStatusBar(plot)
plot.setStatusBar(statusBar)
return plot
def __createPlotStatusBar(self, plot):
converters = [
('X', lambda x, y: x),
('Y', lambda x, y: y),
('Value', self.__getImageValue)]
hbox = qt.QHBoxLayout()
hbox.setContentsMargins(0, 0, 0, 0)
info = PositionInfo(plot=plot, converters=converters)
info.setSnappingMode(True)
statusBar = qt.QStatusBar(plot)
statusBar.setSizeGripEnabled(False)
statusBar.addWidget(info)
return statusBar
def __getImageValue(self, x, y):
"""Get value of top most image at position (x, y).
:param float x: X position in plot coordinates
:param float y: Y position in plot coordinates
:return: The value at that point or 'n/a'
"""
value = 'n/a'
if self.__plot is None:
# It could happen at the creation of the plot
# the creation of PositionInfo
return value
image = self.__plot.getImage("image")
if image is None:
return value
data = image.getData(copy=False)
ox, oy = image.getOrigin()
sx, sy = image.getScale()
row, col = (y - oy) / sy, (x - ox) / sx
if row >= 0 and col >= 0:
# Test positive before cast otherwise issue with int(-0.5) = 0
row, col = int(row), int(col)
if (row < data.shape[0] and col < data.shape[1]):
value = data[row, col]
return value
def _updateModel(self, model):
self.__synchronizeRawView.registerModel(model.rawPlotView())
settings = model.experimentSettingsModel()
settings.image().changed.connect(self.__imageUpdated)
settings.mask().changed.connect(self.__maskFromModelChanged)
self.__maskFromModelChanged()
self.__imageUpdated()
def __imageUpdated(self):
image = self.model().experimentSettingsModel().image().value()
if image is not None:
self.__plot.addImage(image, legend="image", copy=False)
self.__plot.setGraphXLimits(0, image.shape[0])
self.__plot.setGraphYLimits(0, image.shape[1])
self.__plot.resetZoom()
else:
self.__plot.removeImage("image")
def __widgetShow(self):
# Really make sure to be synchronized
# We can't trust events from libs
self.__modelMaskChanged = True
self.__updateWidgetFromModel()
def __widgetHide(self):
self.__updateModelFromWidget()
def __maskFromPlotChanged(self):
self.__plotMaskChanged = True
def __maskFromModelChanged(self):
self.__modelMaskChanged = True
if self.isVisible():
self.__updateWidgetFromModel()
def __updateWidgetFromModel(self):
"""Update the widget using the mask from the model, only if needed"""
if not self.__modelMaskChanged:
return
mask = self.model().experimentSettingsModel().mask().value()
# FIXME if mask is none, the mask should be cleaned up
if mask is not None:
self.__maskPanel.setSelectionMask(mask)
# Everything is synchronized now
self.__plotMaskChanged = False
self.__modelMaskChanged = False
def __updateModelFromWidget(self):
"""Update the model using the mask stored on the widget, only if needed"""
if not self.__plotMaskChanged:
return
mask = self.__maskPanel.getSelectionMask()
maskModel = self.model().experimentSettingsModel().mask()
maskModel.changed.disconnect(self.__maskFromModelChanged)
maskModel.setValue(mask)
maskModel.changed.connect(self.__maskFromModelChanged)
# Everything is synchronized now
self.__plotMaskChanged = False
self.__modelMaskChanged = False
|