File: Object3DColormap.py

package info (click to toggle)
pymca 5.1.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 40,004 kB
  • ctags: 17,800
  • sloc: python: 132,302; ansic: 20,016; cpp: 827; makefile: 48; sh: 30; xml: 24
file content (353 lines) | stat: -rw-r--r-- 12,730 bytes parent folder | download
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#/*##########################################################################
# Copyright (C) 2004-2014 V.A. Sole, European Synchrotron Radiation Facility
#
# This file is part of the PyMca X-ray Fluorescence Toolkit developed at
# the ESRF by the Software group.
#
# This file is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your option)
# any later version.
#
# This file is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
#
# Please contact the ESRF industrial unit (industry@esrf.fr) if this license
# is a problem for you.
#
#############################################################################*/
__author__ = "V.A. Sole - ESRF Data Analysis"
__contact__ = "sole@esrf.fr"
__license__ = "LGPL2+"
__copyright__ = "European Synchrotron Radiation Facility, Grenoble, France"
import sys
from . import Object3DQt   as qt
from . import Object3DSlider

DEBUG = 0

class Object3DColormap(qt.QGroupBox):
    sigObject3DColormapSignal = qt.pyqtSignal(object)
    def __init__(self, parent = None):
        qt.QGroupBox.__init__(self, parent)
        self.setTitle('Colormap')
        self.colormapList = ["Greyscale", "Reverse Grey", "Temperature",
                             "Red", "Green", "Blue", "Many"]
        # default values
        self.dataMin   = -10
        self.dataMax   = 10
        self.minValue  = 0
        self.maxValue  = 1

        self.colormapIndex  = 2
        self.colormapType   = 0

        self.autoscale   = False
        self.autoscale90 = False

        self.__disconnected = False
        self.build()
        self._update()

    def build(self):
        self.mainLayout = qt.QGridLayout(self)
        self.mainLayout.setContentsMargins(0, 0, 0, 0)
        self.mainLayout.setSpacing(2)

        # the ComboBox
        self.comboBox = qt.QComboBox(self)
        for colormap in self.colormapList:
            self.comboBox.addItem(colormap)

        self.mainLayout.addWidget(self.comboBox, 0, 0)
        self.comboBox.activated[int].connect(self.colormapChanged)

        # autoscale
        self.autoScaleButton = qt.QPushButton("Autoscale", self)
        self.autoScaleButton.setCheckable(True)
        self.autoScaleButton.setAutoDefault(False)
        self.autoScaleButton.toggled[bool].connect(self.autoscaleChanged)
        self.mainLayout.addWidget(self.autoScaleButton, 0, 1)

        # autoscale 90%
        self.autoScale90Button = qt.QPushButton("Autoscale 90%", self)
        self.autoScale90Button.setCheckable(True)
        self.autoScale90Button.setAutoDefault(False)

        self.autoScale90Button.toggled[bool].connect(self.autoscale90Changed)

        self.mainLayout.addWidget(self.autoScale90Button, 0, 2)

        #the checkboxes
        self.buttonGroup = qt.QButtonGroup()
        g1 = qt.QCheckBox(self)
        g1.setText("Linear")
        g2 = qt.QCheckBox(self)
        g2.setText("Logarithmic")
        g3 = qt.QCheckBox(self)
        g3.setText("Gamma")
        self.buttonGroup.addButton(g1, 0)
        self.buttonGroup.addButton(g2, 1)
        self.buttonGroup.addButton(g3, 2)
        self.buttonGroup.setExclusive(True)
        if self.colormapType == 1:
            self.buttonGroup.button(1).setChecked(True)
        elif self.colormapType == 2:
            self.buttonGroup.button(2).setChecked(True)
        else:
            self.buttonGroup.button(0).setChecked(True)
        self.mainLayout.addWidget(g1, 1, 0)
        self.mainLayout.addWidget(g2, 1, 1)
        self.mainLayout.addWidget(g3, 1, 2)
        self.buttonGroup.buttonClicked[int].connect(self.buttonGroupChanged)

        # The max line
        label = qt.QLabel(self)
        label.setText('Maximum')
        self.maxText = qt.QLineEdit(self)
        self.maxText.setText("%f" % self.maxValue)
        self.maxText.setAlignment(qt.Qt.AlignRight)
        self.maxText.setFixedWidth(self.maxText.fontMetrics().width('######.#####'))
        v = qt.QDoubleValidator(self.maxText)
        self.maxText.setValidator(v)
        self.mainLayout.addWidget(label, 0, 3)
        self.mainLayout.addWidget(self.maxText, 0, 4)
        self.maxText.editingFinished.connect(self.textChanged)

        # The min line
        label = qt.QLabel(self)
        label.setText('Minimum')
        self.minText = qt.QLineEdit(self)
        self.minText.setFixedWidth(self.minText.fontMetrics().width('######.#####'))
        self.minText.setAlignment(qt.Qt.AlignRight)
        self.minText.setText("%f" % self.minValue)
        v = qt.QDoubleValidator(self.minText)
        self.minText.setValidator(v)
        self.mainLayout.addWidget(label, 1, 3)
        self.mainLayout.addWidget(self.minText, 1, 4)
        self.minText.editingFinished.connect(self.textChanged)

        # The sliders
        self.dataMin   = -10
        self.dataMax   = 10
        self.minValue  = 0
        self.maxValue  = 1
        self.sliderList = []
        delta =  (self.dataMax-self.dataMin) / 200.
        for i in [0, 1]:
            slider = Object3DSlider.Object3DSlider(self, qt.Qt.Horizontal)
            slider.setRange(self.dataMin, self.dataMax, delta)
            if i == 0:
                slider.setValue(self.maxValue)
            else:
                slider.setValue(self.minValue)
            self.mainLayout.addWidget(slider, i, 5)
            slider.valueChanged.connect(self.sliderChanged)
            self.sliderList.append(slider)

    def colormapChanged(self, value):
        self.colormapIndex = value
        self._emitSignal()

    def autoscaleChanged(self, value):
        self.autoscale = value
        self.__disconnected = True
        self.setAutoscale(value)
        self.__disconnected = False
        self._emitSignal()

    def autoscale90Changed(self, value):
        self.autoscale90 = value
        self.__disconnected = True
        self.setAutoscale90(value)
        self.__disconnected = False
        self._emitSignal()

    def buttonGroupChanged(self, value):
        self.colormapType = value
        self._emitSignal()

    def textChanged(self):
        valueMax = float(str(self.maxText.text()))
        valueMin = float(str(self.minText.text()))
        a = min(valueMax, valueMin)
        b = max(valueMax, valueMin)
        self.maxText.setText("%f" % b)
        self.minText.setText("%f" % a)
        oldMin = min(self.sliderList[0].value(),
                     self.sliderList[1].value())
        oldMax = max(self.sliderList[0].value(),
                     self.sliderList[1].value())
        emit = False
        self.__disconnected = True
        if (oldMax - valueMax) != 0.0:
            emit = True
            self.sliderList[0].setValue(valueMax)
        if (oldMin - valueMin) != 0.0:
            emit = True
            self.sliderList[1].setValue(valueMax)
        self.minValue = valueMin
        self.maxValue = valueMax
        self.__disconnected = False
        if emit: self._emitSignal()

    def sliderChanged(self, value):
        if self.__disconnected:
            return
        if DEBUG:
            print("sliderChanged")
        value0 = self.sliderList[0].value()
        value1 = self.sliderList[1].value()
        self.maxText.setText("%f" % max(value0, value1))
        self.minText.setText("%f" % min(value0, value1))
        self.minValue = min(value0, value1)
        self.maxValue = max(value0, value1)

        self._emitSignal()

    def _update(self):
        if DEBUG:
            print("colormap _update called")
        self.__disconnected = True
        delta = (self.dataMax - self.dataMin)/ 200.
        self.sliderList[0].setRange(self.dataMin, self.dataMax, delta)
        self.sliderList[0].setValue(self.maxValue)
        self.sliderList[1].setRange(self.dataMin, self.dataMax, delta)
        self.sliderList[1].setValue(self.minValue)
        self.maxText.setText("%g" % self.maxValue)
        self.minText.setText("%g" % self.minValue)
        self.comboBox.setCurrentIndex(self.colormapIndex)
        self.buttonGroup.button(self.colormapType).setChecked(True)
        self.setAutoscale(self.autoscale)
        self.setAutoscale90(self.autoscale90)
        self.__disconnected = False

    def _emitSignal(self, event = None):
        if self.__disconnected:
            return
        if event is None:
            event = 'ColormapChanged'
        if DEBUG:
            print("sending colormap")
        ddict = self.getParameters()
        ddict['event'] = event
        self.sigObject3DColormapSignal.emit(ddict)

    def setAutoscale(self, val):
        if DEBUG:
            print("setAutoscale called", val)
        if val:
            self.autoScaleButton.setChecked(True)
            self.autoScale90Button.setChecked(False)
            self.setMinValue(self.dataMin)
            self.setMaxValue(self.dataMax)
            for slider in self.sliderList:
                self.__disconnected = True
                delta = (self.dataMax - self.dataMin)/200.
                slider.setRange(self.dataMin, self.dataMax, delta)
                slider.setEnabled(False)
                self.__disconnected = False

            self.maxText.setEnabled(0)
            self.minText.setEnabled(0)
        else:
            self.autoScaleButton.setChecked(False)
            self.autoScale90Button.setChecked(False)
            self.minText.setEnabled(1)
            self.maxText.setEnabled(1)
            for slider in self.sliderList:
                slider.setEnabled(True)

    def setAutoscale90(self, val):
        if val:
            self.autoScaleButton.setChecked(False)
            self.setMinValue(self.dataMin)
            self.setMaxValue(self.dataMax - abs(self.dataMax/10.))
            for slider in self.sliderList:
                self.__disconnected = True
                delta = (self.dataMax - self.dataMin)/200.
                slider.setRange(self.dataMin, self.dataMax, delta)
                slider.setEnabled(False)
                self.__disconnected = False
            self.minText.setEnabled(0)
            self.maxText.setEnabled(0)
        else:
            self.autoScale90Button.setChecked(False)
            if not self.autoscale:
                self.minText.setEnabled(1)
                self.maxText.setEnabled(1)
                for slider in self.sliderList:
                    self.__disconnected = True
                    delta = (self.dataMax - self.dataMin)/200.
                    slider.setRange(self.dataMin, self.dataMax, delta)
                    slider.setEnabled(True)
                    self.__disconnected = False


    # MINIMUM
    """
    change min value and update colormap
    """
    def setMinValue(self, val):
        v = float(str(val))
        self.minValue = v
        self.minText.setText("%g"%v)
        self.__disconnected = True
        self.sliderList[1].setValue(v)
        self.__disconnected = False

    # MAXIMUM
    """
    change max value and update colormap
    """
    def setMaxValue(self, val):
        v = float(str(val))
        self.maxValue = v
        self.maxText.setText("%g"%v)
        self.__disconnected = True
        self.sliderList[0].setValue(v)
        self.__disconnected = False

    def getParameters(self):
        ddict = {}
        if self.minValue > self.maxValue:
            vMax = self.minValue
        else:
            vMax = self.maxValue
        ddict['colormap'] = [self.colormapIndex, self.autoscale,
                        self.minValue, vMax,
                        self.dataMin, self.dataMax,
                        self.colormapType]
        return ddict

    def setParameters(self, ddict):
        if 'colormap' in ddict:
            self.__disconnected = True
            self.colormapIndex,  self.autoscale, \
                 self.minValue, self.maxValue, \
                 self.dataMin, self.dataMax, \
                        self.colormapType = ddict['colormap']
            self._update()
            self.__disconnected = False


def test():
    app = qt.QApplication(sys.argv)
    app.lastWindowClosed.connect(app.quit)

    def slot(ddict):
        print("ddict= ", ddict)
    demo = Object3DColormap()
    demo.sigObject3DColormapSignal.connect(slot)
    demo.show()
    app.exec_()


if __name__ == "__main__":
    test()