File: CalibrationWindow.py

package info (click to toggle)
pyfai 0.20.0%2Bdfsg1-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 78,460 kB
  • sloc: python: 49,743; lisp: 7,059; sh: 225; ansic: 165; makefile: 119
file content (283 lines) | stat: -rw-r--r-- 9,831 bytes parent folder | download | duplicates (2)
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
# 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 functools

from silx.gui import qt
from silx.gui import icons

import pyFAI.utils
from .model import MarkerModel
from .utils import projecturl


class MenuItem(qt.QListWidgetItem):

    TextMode = 0
    IconMode = 1

    def __init__(self, parent):
        super(MenuItem, self).__init__(parent)
        self.__text = None
        self.__icon = qt.QIcon()
        self.__warningIcon = None
        self.__warnings = None
        self.__mode = self.TextMode

    def setText(self, text):
        self.__text = text
        self.__updateItem()

    def setIcon(self, icon):
        self.__icon = icon
        self.__updateItem()

    def setWarnings(self, warnings):
        self.__warnings = warnings
        self.__updateItem()

    def setDisplayMode(self, mode):
        self.__mode = mode
        self.__updateItem()

    def __updateItem(self):
        superSelf = super(MenuItem, self)
        if self.__mode == self.TextMode:
            superSelf.setText(self.__text)
            fontMetrics = qt.QFontMetrics(self.font())
            width = fontMetrics.width(self.text())
            width += self.listWidget().iconSize().width() + 20
            superSelf.setSizeHint(qt.QSize(width, 60))
        elif self.__mode == self.IconMode:
            superSelf.setText(None)
            width = self.listWidget().iconSize().width() + 7
            superSelf.setSizeHint(qt.QSize(width, 60))
        else:
            assert(False)

        if self.__warnings is None:
            superSelf.setIcon(self.__icon)
        else:
            if self.__warningIcon is None:
                icon = self.__createWarningIcon(self.__icon)
                self.__warningIcon = icon
            superSelf.setIcon(self.__warningIcon)

        if self.__warnings is None and self.__mode == self.TextMode:
            superSelf.setToolTip("")
        else:
            toolTip = self.__text

            toolTip = ""
            if self.__mode == self.IconMode:
                toolTip += self.__text
            if self.__warnings is not None:
                if toolTip != "":
                    toolTip += "<br/>"
                toolTip += self.__warnings
            superSelf.setToolTip(toolTip)

    def __createCompoundIcon(self, backgroundIcon, foregroundIcon):
        icon = qt.QIcon()

        sizes = backgroundIcon.availableSizes()
        sizes = sorted(sizes, key=lambda s: s.height())
        sizes = filter(lambda s: s.height() < 100, sizes)
        sizes = list(sizes)
        if len(sizes) > 0:
            baseSize = sizes[-1]
        else:
            baseSize = qt.QSize(32, 32)

        modes = [qt.QIcon.Normal, qt.QIcon.Disabled]
        for mode in modes:
            pixmap = qt.QPixmap(baseSize)
            pixmap.fill(qt.Qt.transparent)
            painter = qt.QPainter(pixmap)
            painter.drawPixmap(0, 0, backgroundIcon.pixmap(baseSize, mode=mode))
            painter.drawPixmap(0, 0, foregroundIcon.pixmap(baseSize, mode=mode))
            painter.end()
            icon.addPixmap(pixmap, mode=mode)

        return icon

    def __createWarningIcon(self, baseIcon):
        nxIcon = icons.getQIcon("pyfai:gui/icons/layer-warning")
        icon = self.__createCompoundIcon(baseIcon, nxIcon)
        return icon


class CalibrationWindow(qt.QMainWindow):

    def __init__(self, context):
        super(CalibrationWindow, self).__init__()
        context.setParent(self)
        qt.loadUi(pyFAI.utils.get_ui_file("calibration-main.ui"), self)
        self.__context = context
        model = context.getCalibrationModel()

        pyfaiIcon = icons.getQIcon("pyfai:gui/images/icon")
        self.setWindowIcon(pyfaiIcon)

        context.restoreWindowLocationSettings("main-window", self)

        self.__listMode = MenuItem.TextMode

        self.__tasks = self.createTasks()
        for task in self.__tasks:
            task.nextTaskRequested.connect(self.nextTask)
            item = MenuItem(self._list)
            item.setText(task.windowTitle())
            item.setIcon(task.windowIcon())
            self._stack.addWidget(task)

            task.warningUpdated.connect(functools.partial(self.__updateTaskState, task, item))

        if len(self.__tasks) > 0:
            self._list.setCurrentRow(0)
            # Hide the nextStep button of the last task
            task.setNextStepVisible(False)

        self._list.sizeHint = self._listSizeHint
        self._list.minimumSizeHint = self._listMinimumSizeHint
        self.setModel(model)

        url = projecturl.get_documentation_url("")
        if url.startswith("http"):
            self._help.setText("Online help...")
        self._helpText = self._help.text()
        self._help.clicked.connect(self.__displayHelp)

    def __displayHelp(self):
        subpath = "usage/cookbook/calib-gui/index.html"
        url = projecturl.get_documentation_url(subpath)
        qt.QDesktopServices.openUrl(qt.QUrl(url))

    def __updateTaskState(self, task, item):
        warnings = task.nextStepWarning()
        item.setWarnings(warnings)

    def _listMinimumSizeHint(self):
        return qt.QSize(self._list.iconSize().width() + 7, 10)

    def _listSizeHint(self):
        if self.__listMode == "icon":
            return self._listMinimumSizeHint()
        else:
            maxWidth = 0
            for row in range(self._list.count()):
                item = self._list.item(row)
                width = item.sizeHint().width()
                if maxWidth < width:
                    maxWidth = width
        return qt.QSize(maxWidth, 10)

    def _setListMode(self, mode):
        if self.__listMode == mode:
            return
        self.__listMode = mode
        for row in range(self._list.count()):
            item = self._list.item(row)
            item.setDisplayMode(mode)
        self._list.adjustSize()
        self._list.updateGeometry()

    def __minimizeMenu(self):
        self._setListMode(MenuItem.IconMode)
        icon = icons.getQIcon("pyfai:gui/icons/menu-help")
        self._help.setIcon(icon)
        self._help.setText("")

    def __maximizeMenu(self):
        self._setListMode(MenuItem.TextMode)
        self._help.setIcon(qt.QIcon())
        self._help.setText(self._helpText)

    def resizeEvent(self, event):
        width = event.size().width()
        oldWidth = event.oldSize().width()
        delta = width - oldWidth
        if (delta < 0 or oldWidth == -1) and width < 1100:
            self.__minimizeMenu()
        elif (delta > 0 or oldWidth == -1) and width > 1500:
            self.__maximizeMenu()
        return qt.QMainWindow.resizeEvent(self, event)

    def closeEvent(self, event):
        poniFile = self.model().experimentSettingsModel().poniFile()

        if not poniFile.isSynchronized():
            button = qt.QMessageBox.question(self,
                                             "calib2",
                                             "The PONI file was not saved.\nDo you really want to close the application?",
                                             qt.QMessageBox.Cancel | qt.QMessageBox.No | qt.QMessageBox.Yes,
                                             qt.QMessageBox.Yes)
            if button != qt.QMessageBox.Yes:
                event.ignore()
                return

        event.accept()

        for task in self.__tasks:
            task.aboutToClose()
        self.__context.saveWindowLocationSettings("main-window", self)

    def createTasks(self):
        from pyFAI.gui.tasks.ExperimentTask import ExperimentTask
        from pyFAI.gui.tasks.MaskTask import MaskTask
        from pyFAI.gui.tasks.PeakPickingTask import PeakPickingTask
        from pyFAI.gui.tasks.GeometryTask import GeometryTask
        from pyFAI.gui.tasks.IntegrationTask import IntegrationTask

        tasks = [
            ExperimentTask(),
            MaskTask(),
            PeakPickingTask(),
            GeometryTask(),
            IntegrationTask()
        ]
        return tasks

    def model(self):
        return self.__model

    def setModel(self, model):
        self.__model = model

        if len(self.__model.markerModel()) == 0:
            origin = MarkerModel.PixelMarker("Origin", 0, 0)
            self.__model.markerModel().add(origin)

        for task in self.__tasks:
            task.setModel(self.__model)

    def nextTask(self):
        index = self._list.currentRow() + 1
        if index < self._list.count():
            self._list.setCurrentRow(index)