summaryrefslogtreecommitdiff
path: root/silx/gui/plot/ColorBar.py
blob: 2db7b79954ad8bd3dab307a98b4a8965f172eac2 (plain)
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
# coding: utf-8
# /*##########################################################################
#
# Copyright (c) 2016-2017 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.
#
# ###########################################################################*/
"""Module containing several widgets associated to a colormap.
"""

__authors__ = ["H. Payno", "T. Vincent"]
__license__ = "MIT"
__date__ = "15/02/2018"


import logging
import numpy
from ._utils import ticklayout
from .. import qt, icons
from silx.gui.plot import Colormap

_logger = logging.getLogger(__name__)


class ColorBarWidget(qt.QWidget):
    """Colorbar widget displaying a colormap

    It uses a description of colormap as dict compatible with :class:`Plot`.

    .. image:: img/linearColorbar.png
        :width: 80px
        :align: center

    To run the following sample code, a QApplication must be initialized.

    >>> from silx.gui.plot import Plot2D
    >>> from silx.gui.plot.ColorBar import ColorBarWidget

    >>> plot = Plot2D()  # Create a plot widget
    >>> plot.show()

    >>> colorbar = ColorBarWidget(plot=plot, legend='Colormap')  # Associate the colorbar with it
    >>> colorbar.show()

    Initializer parameters:

    :param parent: See :class:`QWidget`
    :param plot: PlotWidget the colorbar is attached to (optional)
    :param str legend: the label to set to the colorbar
    """
    sigVisibleChanged = qt.Signal(bool)
    """Emitted when the property `visible` have changed."""

    def __init__(self, parent=None, plot=None, legend=None):
        self._isConnected = False
        self._plot = None
        self._colormap = None
        self._data = None

        super(ColorBarWidget, self).__init__(parent)

        self.__buildGUI()
        self.setLegend(legend)
        self.setPlot(plot)

    def __buildGUI(self):
        self.setLayout(qt.QHBoxLayout())

        # create color scale widget
        self._colorScale = ColorScaleBar(parent=self,
                                         colormap=None)
        self.layout().addWidget(self._colorScale)

        # legend (is the right group)
        self.legend = _VerticalLegend('', self)
        self.layout().addWidget(self.legend)

        self.layout().setSizeConstraint(qt.QLayout.SetMinAndMaxSize)

    def getPlot(self):
        """Returns the :class:`Plot` associated to this widget or None"""
        return self._plot

    def setPlot(self, plot):
        """Associate a plot to the ColorBar

        :param plot: the plot to associate with the colorbar.
                     If None will remove any connection with a previous plot.
        """
        self._disconnectPlot()
        self._plot = plot
        self._connectPlot()

    def _disconnectPlot(self):
        """Disconnect from Plot signals"""
        if self._plot is not None and self._isConnected:
            self._isConnected = False
            self._plot.sigActiveImageChanged.disconnect(
                self._activeImageChanged)
            self._plot.sigPlotSignal.disconnect(self._defaultColormapChanged)

    def _connectPlot(self):
        """Connect to Plot signals"""
        if self._plot is not None and not self._isConnected:
            activeImageLegend = self._plot.getActiveImage(just_legend=True)
            if activeImageLegend is None:  # Show plot default colormap
                self._syncWithDefaultColormap()
            else:  # Show active image colormap
                self._activeImageChanged(None, activeImageLegend)
            self._plot.sigActiveImageChanged.connect(self._activeImageChanged)
            self._plot.sigPlotSignal.connect(self._defaultColormapChanged)
            self._isConnected = True

    def setVisible(self, isVisible):
        # isHidden looks to be always synchronized, while isVisible is not
        wasHidden = self.isHidden()
        qt.QWidget.setVisible(self, isVisible)
        if wasHidden != self.isHidden():
            self.sigVisibleChanged.emit(not self.isHidden())

    def showEvent(self, event):
        self._connectPlot()

    def hideEvent(self, event):
        self._disconnectPlot()

    def getColormap(self):
        """

        :return: the :class:`.Colormap` colormap displayed in the colorbar.

        """
        return self.getColorScaleBar().getColormap()

    def setColormap(self, colormap, data=None):
        """Set the colormap to be displayed.

        :param colormap: The colormap to apply on the
            ColorBarWidget
        :type colormap: :class:`.Colormap`
        :param numpy.ndarray data: the data to display, needed if the colormap
            require an autoscale
        """
        self._data = data
        self.getColorScaleBar().setColormap(colormap=colormap,
                                            data=data)
        if self._colormap is not None:
            self._colormap.sigChanged.disconnect(self._colormapHasChanged)
        self._colormap = colormap
        if self._colormap is not None:
            self._colormap.sigChanged.connect(self._colormapHasChanged)

    def _colormapHasChanged(self):
        """handler of the Colormap.sigChanged signal
        """
        assert self._colormap is not None
        self.setColormap(colormap=self._colormap,
                         data=self._data)

    def setLegend(self, legend):
        """Set the legend displayed along the colorbar

        :param str legend: The label
        """
        if legend is None or legend == "":
            self.legend.hide()
            self.legend.setText("")
        else:
            assert type(legend) is str
            self.legend.show()
            self.legend.setText(legend)

    def getLegend(self):
        """
        Returns the legend displayed along the colorbar

        :return: return the legend displayed along the colorbar
        :rtype: str
        """
        return self.legend.getText()

    def _activeImageChanged(self, previous, legend):
        """Handle plot active curve changed"""
        if legend is None:  # No active image, display no colormap
            self.setColormap(colormap=None)
            return

        # Sync with active image
        image = self._plot.getActiveImage().getData(copy=False)

        # RGB(A) image, display default colormap
        if image.ndim != 2:
            self.setColormap(colormap=None)
            return

        # data image, sync with image colormap
        # do we need the copy here : used in the case we are changing
        # vmin and vmax but should have already be done by the plot
        self.setColormap(colormap=self._plot.getActiveImage().getColormap(),
                         data=image)

    def _defaultColormapChanged(self, event):
        """Handle plot default colormap changed"""
        if (event['event'] == 'defaultColormapChanged' and
                self._plot.getActiveImage() is None):
            # No active image, take default colormap update into account
            self._syncWithDefaultColormap()

    def _syncWithDefaultColormap(self, data=None):
        """Update colorbar according to plot default colormap"""
        self.setColormap(self._plot.getDefaultColormap(), data)

    def getColorScaleBar(self):
        """

        :return: return the :class:`ColorScaleBar` used to display ColorScale
            and ticks"""
        return self._colorScale


class _VerticalLegend(qt.QLabel):
    """Display vertically the given text
    """
    def __init__(self, text, parent=None):
        """

        :param text: the legend
        :param parent: the Qt parent if any
        """
        qt.QLabel.__init__(self, text, parent)
        self.setLayout(qt.QVBoxLayout())
        self.layout().setContentsMargins(0, 0, 0, 0)

    def paintEvent(self, event):
        painter = qt.QPainter(self)
        painter.setFont(self.font())

        painter.translate(0, self.rect().height())
        painter.rotate(270)
        newRect = qt.QRect(0, 0, self.rect().height(), self.rect().width())

        painter.drawText(newRect, qt.Qt.AlignHCenter, self.text())

        fm = qt.QFontMetrics(self.font())
        preferedHeight = fm.width(self.text())
        preferedWidth = fm.height()
        self.setFixedWidth(preferedWidth)
        self.setMinimumHeight(preferedHeight)


class ColorScaleBar(qt.QWidget):
    """This class is making the composition of a :class:`_ColorScale` and a
    :class:`_TickBar`.

    It is the simplest widget displaying ticks and colormap gradient.

    .. image:: img/colorScaleBar.png
        :width: 150px
        :align: center

    To run the following sample code, a QApplication must be initialized.

    >>> colormap = Colormap(name='gray',
    ...                     norm='log',
    ...                     vmin=1,
    ...                     vmax=100000,
    ...             )
    >>> colorscale = ColorScaleBar(parent=None,
    ...                            colormap=colormap )
    >>> colorscale.show()

    Initializer parameters :

    :param colormap: the colormap to be displayed
    :param parent: the Qt parent if any
    :param displayTicksValues: display the ticks value or only the '-'
    """

    _TEXT_MARGIN = 5
    """The tick bar need a margin to display all labels at the correct place.
    So the ColorScale should have the same margin in order for both to fit"""

    def __init__(self, parent=None, colormap=None, data=None,
                 displayTicksValues=True):
        super(ColorScaleBar, self).__init__(parent)

        self.minVal = None
        """Value set to the _minLabel"""
        self.maxVal = None
        """Value set to the _maxLabel"""

        self.setLayout(qt.QGridLayout())

        # create the left side group (ColorScale)
        self.colorScale = _ColorScale(colormap=colormap,
                                      data=data,
                                      parent=self,
                                      margin=ColorScaleBar._TEXT_MARGIN)
        if colormap:
            vmin, vmax = colormap.getColormapRange(data)
        else:
            vmin, vmax = Colormap.DEFAULT_MIN_LIN, Colormap.DEFAULT_MAX_LIN

        norm = colormap.getNormalization() if colormap else Colormap.Colormap.LINEAR
        self.tickbar = _TickBar(vmin=vmin,
                                vmax=vmax,
                                norm=norm,
                                parent=self,
                                displayValues=displayTicksValues,
                                margin=ColorScaleBar._TEXT_MARGIN)

        self.layout().addWidget(self.tickbar, 1, 0, 1, 1, qt.Qt.AlignRight)
        self.layout().addWidget(self.colorScale, 1, 1, qt.Qt.AlignLeft)

        self.layout().setContentsMargins(0, 0, 0, 0)
        self.layout().setSpacing(0)

        # max label
        self._maxLabel = qt.QLabel(str(1.0), parent=self)
        self._maxLabel.setToolTip(str(0.0))
        self.layout().addWidget(self._maxLabel, 0, 0, 1, 2, qt.Qt.AlignRight)

        # min label
        self._minLabel = qt.QLabel(str(0.0), parent=self)
        self._minLabel.setToolTip(str(0.0))
        self.layout().addWidget(self._minLabel, 2, 0, 1, 2, qt.Qt.AlignRight)

        self.layout().setSizeConstraint(qt.QLayout.SetMinAndMaxSize)
        self.layout().setColumnStretch(0, 1)
        self.layout().setRowStretch(1, 1)

    def getTickBar(self):
        """

        :return: the instanciation of the :class:`_TickBar`
        """
        return self.tickbar

    def getColorScale(self):
        """

        :return: the instanciation of the :class:`_ColorScale`
        """
        return self.colorScale

    def getColormap(self):
        """

        :returns: the colormap.
        :rtype: :class:`.Colormap`
        """
        return self.colorScale.getColormap()

    def setColormap(self, colormap, data=None):
        """Set the new colormap to be displayed

        :param Colormap colormap: the colormap to set
        :param numpy.ndarray data: the data to display, needed if the colormap
            require an autoscale
        """
        self.colorScale.setColormap(colormap, data)

        if colormap is not None:
            vmin, vmax = colormap.getColormapRange(data)
            norm = colormap.getNormalization()
        else:
            vmin, vmax = None, None
            norm = None

        self.tickbar.update(vmin=vmin,
                            vmax=vmax,
                            norm=norm)
        self._setMinMaxLabels(vmin, vmax)

    def setMinMaxVisible(self, val=True):
        """Change visibility of the min label and the max label

        :param val: if True, set the labels visible, otherwise set it not visible
        """
        self._minLabel.setVisible(val)
        self._maxLabel.setVisible(val)

    def _updateMinMax(self):
        """Update the min and max label if we are in the case of the
        configuration 'minMaxValueOnly'"""
        if self.minVal is None:
            text, tooltip = '', ''
        else:
            if self.minVal == 0 or 0 <= numpy.log10(abs(self.minVal)) < 7:
                text = '%.7g' % self.minVal
            else:
                text = '%.2e' % self.minVal
            tooltip = repr(self.minVal)

        self._minLabel.setText(text)
        self._minLabel.setToolTip(tooltip)

        if self.maxVal is None:
            text, tooltip = '', ''
        else:
            if self.maxVal == 0 or 0 <= numpy.log10(abs(self.maxVal)) < 7:
                text = '%.7g' % self.maxVal
            else:
                text = '%.2e' % self.maxVal
            tooltip = repr(self.maxVal)

        self._maxLabel.setText(text)
        self._maxLabel.setToolTip(tooltip)

    def _setMinMaxLabels(self, minVal, maxVal):
        """Change the value of the min and max labels to be displayed.

        :param minVal: the minimal value of the TickBar (not str)
        :param maxVal: the maximal value of the TickBar (not str)
        """
        # bad hack to try to display has much information as possible
        self.minVal = minVal
        self.maxVal = maxVal
        self._updateMinMax()

    def resizeEvent(self, event):
        qt.QWidget.resizeEvent(self, event)
        self._updateMinMax()


class _ColorScale(qt.QWidget):
    """Widget displaying the colormap colorScale.

    Show matching value between the gradient color (from the colormap) at mouse
    position and value.

    .. image:: img/colorScale.png
        :width: 20px
        :align: center


    To run the following sample code, a QApplication must be initialized.

    >>> colormap = Colormap(name='viridis',
    ...                     norm='log',
    ...                     vmin=1,
    ...                     vmax=100000,
    ...             )
    >>> colorscale = ColorScale(parent=None,
    ...                         colormap=colormap)
    >>> colorscale.show()

    Initializer parameters :

    :param colormap: the colormap to be displayed
    :param parent: the Qt parent if any
    :param int margin: the top and left margin to apply.

    .. warning:: Value drawing will be
        done at the center of ticks. So if no margin is done your values
        drawing might not be fully done for extrems values.
    """

    _NB_CONTROL_POINTS = 256

    def __init__(self, colormap, parent=None, margin=5, data=None):
        qt.QWidget.__init__(self, parent)
        self._colormap = None
        self.margin = margin
        self.setColormap(colormap, data)

        self.setLayout(qt.QVBoxLayout())
        self.setSizePolicy(qt.QSizePolicy.Fixed, qt.QSizePolicy.Expanding)
        # needed to get the mouse event without waiting for button click
        self.setMouseTracking(True)
        self.setMargin(margin)
        self.setContentsMargins(0, 0, 0, 0)

        self.setMinimumHeight(self._NB_CONTROL_POINTS // 2 + 2 * self.margin)
        self.setFixedWidth(25)

    def setColormap(self, colormap, data=None):
        """Set the new colormap to be displayed

        :param dict colormap: the colormap to set
        :param data: Optional data for which to compute colormap range.
        """
        self._colormap = colormap
        self.setEnabled(colormap is not None)

        if colormap is None:
            self.vmin, self.vmax = None, None
        else:
            assert colormap.getNormalization() in Colormap.Colormap.NORMALIZATIONS
            self.vmin, self.vmax = self._colormap.getColormapRange(data=data)
        self._updateColorGradient()
        self.update()

    def getColormap(self):
        """Returns the colormap

        :rtype: :class:`.Colormap`
        """
        return None if self._colormap is None else self._colormap

    def _updateColorGradient(self):
        """Compute the color gradient"""
        colormap = self.getColormap()
        if colormap is None:
            return

        indices = numpy.linspace(0., 1., self._NB_CONTROL_POINTS)
        colors = colormap.getNColors(nbColors=self._NB_CONTROL_POINTS)
        self._gradient = qt.QLinearGradient(0, 1, 0, 0)
        self._gradient.setCoordinateMode(qt.QGradient.StretchToDeviceMode)
        self._gradient.setStops(
            [(i, qt.QColor(*color)) for i, color in zip(indices, colors)]
        )

    def paintEvent(self, event):
        """"""
        painter = qt.QPainter(self)
        if self.getColormap() is not None:
            painter.setBrush(self._gradient)
            penColor = self.palette().color(qt.QPalette.Active,
                                            qt.QPalette.Foreground)
        else:
            penColor = self.palette().color(qt.QPalette.Disabled,
                                            qt.QPalette.Foreground)
        painter.setPen(penColor)

        painter.drawRect(qt.QRect(
            0,
            self.margin,
            self.width() - 1.,
            self.height() - 2. * self.margin - 1.))

    def mouseMoveEvent(self, event):
        tooltip = str(self.getValueFromRelativePosition(
            self._getRelativePosition(event.y())))
        qt.QToolTip.showText(event.globalPos(), tooltip, self)
        super(_ColorScale, self).mouseMoveEvent(event)

    def _getRelativePosition(self, yPixel):
        """yPixel : pixel position into _ColorScale widget reference
        """
        # widgets are bottom-top referencial but we display in top-bottom referential
        return 1. - (yPixel - self.margin) / float(self.height() - 2 * self.margin)

    def getValueFromRelativePosition(self, value):
        """Return the value in the colorMap from a relative position in the
        ColorScaleBar (y)

        :param value: float value in [0, 1]
        :return: the value in [colormap['vmin'], colormap['vmax']]
        """
        colormap = self.getColormap()
        if colormap is None:
            return

        value = max(0.0, value)
        value = min(value, 1.0)

        vmin = self.vmin
        vmax = self.vmax
        if colormap.getNormalization() == Colormap.Colormap.LINEAR:
            return vmin + (vmax - vmin) * value
        elif colormap.getNormalization() == Colormap.Colormap.LOGARITHM:
            rpos = (numpy.log10(vmax) - numpy.log10(vmin)) * value + numpy.log10(vmin)
            return numpy.power(10., rpos)
        else:
            err = "normalization type (%s) is not managed by the _ColorScale Widget" % colormap['normalization']
            raise ValueError(err)

    def setMargin(self, margin):
        """Define the margin to fit with a TickBar object.
        This is needed since we can only paint on the viewport of the widget.
        Didn't work with a simple setContentsMargins

        :param int margin: the margin to apply on the top and bottom.
        """
        self.margin = margin
        self.update()


class _TickBar(qt.QWidget):
    """Bar grouping the ticks displayed

    To run the following sample code, a QApplication must be initialized.

    >>> bar = _TickBar(1, 1000, norm='log', parent=None, displayValues=True)
    >>> bar.show()

    .. image:: img/tickbar.png
        :width: 40px
        :align: center

    :param int vmin: smaller value of the range of values
    :param int vmax: higher value of the range of values
    :param str norm: normalization type to be displayed. Valid values are
        'linear' and 'log'
    :param parent: the Qt parent if any
    :param bool displayValues: if True display the values close to the tick,
        Otherwise only signal it by '-'
    :param int nticks: the number of tick we want to display. Should be an
        unsigned int ot None. If None, let the Tick bar find the optimal
        number of ticks from the tick density.
    :param int margin: margin to set on the top and bottom
    """
    _WIDTH_DISP_VAL = 45
    """widget width when displayed with ticks labels"""
    _WIDTH_NO_DISP_VAL = 10
    """widget width when displayed without ticks labels"""
    _FONT_SIZE = 10
    """font size for ticks labels"""
    _LINE_WIDTH = 10
    """width of the line to mark a tick"""

    DEFAULT_TICK_DENSITY = 0.015

    def __init__(self, vmin, vmax, norm, parent=None, displayValues=True,
                 nticks=None, margin=5):
        super(_TickBar, self).__init__(parent)
        self.margin = margin
        self._nticks = None
        self.ticks = ()
        self.subTicks = ()
        self._forcedDisplayType = None
        self.ticksDensity = _TickBar.DEFAULT_TICK_DENSITY

        self._vmin = vmin
        self._vmax = vmax
        self._norm = norm
        self.displayValues = displayValues
        self.setTicksNumber(nticks)

        self.setMargin(margin)
        self.setContentsMargins(0, 0, 0, 0)

        self._resetWidth()

    def setTicksValuesVisible(self, val):
        self.displayValues = val
        self._resetWidth()

    def _resetWidth(self):
        width = self._WIDTH_DISP_VAL if self.displayValues else self._WIDTH_NO_DISP_VAL
        self.setFixedWidth(width)

    def update(self, vmin, vmax, norm):
        self._vmin = vmin
        self._vmax = vmax
        self._norm = norm
        self.computeTicks()
        qt.QWidget.update(self)

    def setMargin(self, margin):
        """Define the margin to fit with a _ColorScale object.
        This is needed since we can only paint on the viewport of the widget

        :param int margin: the margin to apply on the top and bottom.
        """
        self.margin = margin

    def setTicksNumber(self, nticks):
        """Set the number of ticks to display.

        :param nticks: the number of tick to be display. Should be an
            unsigned int ot None. If None, let the :class:`_TickBar` find the
            optimal number of ticks from the tick density.
        """
        self._nticks = nticks
        self.computeTicks()
        qt.QWidget.update(self)

    def setTicksDensity(self, density):
        """If you let :class:`_TickBar` deal with the number of ticks
        (nticks=None) then you can specify a ticks density to be displayed.
        """
        if density < 0.0:
            raise ValueError('Density should be a positive value')
        self.ticksDensity = density

    def computeTicks(self):
        """This function compute ticks values labels. It is called at each
        update and each resize event.
        Deal only with linear and log scale.
        """
        nticks = self._nticks
        if nticks is None:
            nticks = self._getOptimalNbTicks()

        if self._vmin == self._vmax:
            # No range: no ticks
            self.ticks = ()
            self.subTicks = ()
        elif self._norm == Colormap.Colormap.LOGARITHM:
            self._computeTicksLog(nticks)
        elif self._norm == Colormap.Colormap.LINEAR:
            self._computeTicksLin(nticks)
        else:
            err = 'TickBar - Wrong normalization %s' % self._norm
            raise ValueError(err)
        # update the form
        font = qt.QFont()
        font.setPixelSize(_TickBar._FONT_SIZE)

        self.form = self._getFormat(font)

    def _computeTicksLog(self, nticks):
        logMin = numpy.log10(self._vmin)
        logMax = numpy.log10(self._vmax)
        lowBound, highBound, spacing, self._nfrac = ticklayout.niceNumbersForLog10(logMin,
                                                                                   logMax,
                                                                                   nticks)
        self.ticks = numpy.power(10., numpy.arange(lowBound, highBound, spacing))
        if spacing == 1:
            self.subTicks = ticklayout.computeLogSubTicks(ticks=self.ticks,
                                                          lowBound=numpy.power(10., lowBound),
                                                          highBound=numpy.power(10., highBound))
        else:
            self.subTicks = []

    def resizeEvent(self, event):
        qt.QWidget.resizeEvent(self, event)
        self.computeTicks()

    def _computeTicksLin(self, nticks):
        _min, _max, _spacing, self._nfrac = ticklayout.niceNumbers(self._vmin,
                                                                   self._vmax,
                                                                   nticks)

        self.ticks = numpy.arange(_min, _max, _spacing)
        self.subTicks = []

    def _getOptimalNbTicks(self):
        return max(2, int(round(self.ticksDensity * self.rect().height())))

    def paintEvent(self, event):
        painter = qt.QPainter(self)
        font = painter.font()
        font.setPixelSize(_TickBar._FONT_SIZE)
        painter.setFont(font)

        # paint ticks
        for val in self.ticks:
            self._paintTick(val, painter, majorTick=True)

        # paint subticks
        for val in self.subTicks:
            self._paintTick(val, painter, majorTick=False)

    def _getRelativePosition(self, val):
        """Return the relative position of val according to min and max value
        """
        if self._norm == Colormap.Colormap.LINEAR:
            return 1 - (val - self._vmin) / (self._vmax - self._vmin)
        elif self._norm == Colormap.Colormap.LOGARITHM:
            return 1 - (numpy.log10(val) - numpy.log10(self._vmin)) / (numpy.log10(self._vmax) - numpy.log(self._vmin))
        else:
            raise ValueError('Norm is not recognized')

    def _paintTick(self, val, painter, majorTick=True):
        """

        :param bool majorTick: if False will never draw text and will set a line
            with a smaller width
        """
        fm = qt.QFontMetrics(painter.font())
        viewportHeight = self.rect().height() - self.margin * 2 - 1
        relativePos = self._getRelativePosition(val)
        height = viewportHeight * relativePos
        height += self.margin
        lineWidth = _TickBar._LINE_WIDTH
        if majorTick is False:
            lineWidth /= 2

        painter.drawLine(qt.QLine(self.width() - lineWidth,
                                  height,
                                  self.width(),
                                  height))

        if self.displayValues and majorTick is True:
            painter.drawText(qt.QPoint(0.0, height + (fm.height() / 2)),
                             self.form.format(val))

    def setDisplayType(self, disType):
        """Set the type of display we want to set for ticks labels

        :param str disType: The type of display we want to set. disType values
            can be :

            - 'std' for standard, meaning only a formatting on the number of
                digits is done
            - 'e' for scientific display
            - None to let the _TickBar guess the best display for this kind of data.
        """
        if disType not in (None, 'std', 'e'):
            raise ValueError("display type not recognized, value should be in (None, 'std', 'e'")
        self._forcedDisplayType = disType

    def _getStandardFormat(self):
        return "{0:.%sf}" % self._nfrac

    def _getFormat(self, font):
        if self._forcedDisplayType is None:
            return self._guessType(font)
        elif self._forcedDisplayType is 'std':
            return self._getStandardFormat()
        elif self._forcedDisplayType is 'e':
            return self._getScientificForm()
        else:
            err = 'Forced type for display %s is not recognized' % self._forcedDisplayType
            raise ValueError(err)

    def _getScientificForm(self):
        return "{0:.0e}"

    def _guessType(self, font):
        """Try fo find the better format to display the tick's labels

        :param QFont font: the font we want want to use durint the painting
        """
        form = self._getStandardFormat()

        fm = qt.QFontMetrics(font)
        width = 0
        for tick in self.ticks:
            width = max(fm.width(form.format(tick)), width)

        # if the length of the string are too long we are mooving to scientific
        # display
        if width > _TickBar._WIDTH_DISP_VAL - _TickBar._LINE_WIDTH:
            return self._getScientificForm()
        else:
            return form