summaryrefslogtreecommitdiff
path: root/silx/gui/widgets/ElidedLabel.py
blob: fe53bb90c5ed4369db6ce2199a21238e77e5cd95 (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
# coding: utf-8
# /*##########################################################################
#
# Copyright (c) 2004-2020 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 contains an elidable label
"""

__license__ = "MIT"
__date__ = "07/12/2018"

from silx.gui import qt


class ElidedLabel(qt.QLabel):
    """QLabel with an edile property.

    By default if the text is too big, it is elided on the right.

    This mode can be changed with :func:`setElideMode`.

    In case the text is elided, the full content is displayed as part of the
    tool tip. This behavior can be disabled with :func:`setTextAsToolTip`.
    """

    def __init__(self, parent=None):
        super(ElidedLabel, self).__init__(parent)
        self.__text = ""
        self.__toolTip = ""
        self.__textAsToolTip = True
        self.__textIsElided = False
        self.__elideMode = qt.Qt.ElideRight
        self.__updateMinimumSize()

    def resizeEvent(self, event):
        self.__updateText()
        return qt.QLabel.resizeEvent(self, event)

    def setFont(self, font):
        qt.QLabel.setFont(self, font)
        self.__updateMinimumSize()
        self.__updateText()

    def __updateMinimumSize(self):
        metrics = self.fontMetrics()
        width = metrics.width("...")
        self.setMinimumWidth(width)

    def __updateText(self):
        metrics = self.fontMetrics()
        elidedText = metrics.elidedText(self.__text, self.__elideMode, self.width())
        qt.QLabel.setText(self, elidedText)
        wasElided = self.__textIsElided
        self.__textIsElided = elidedText != self.__text
        if self.__textIsElided or wasElided != self.__textIsElided:
            self.__updateToolTip()

    def __updateToolTip(self):
        if self.__textIsElided and self.__textAsToolTip:
            qt.QLabel.setToolTip(self, self.__text + "<br/>" + self.__toolTip)
        else:
            qt.QLabel.setToolTip(self, self.__toolTip)

    # Properties

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

    def getText(self):
        return self.__text

    text = qt.Property(str, getText, setText)

    def setToolTip(self, toolTip):
        self.__toolTip = toolTip
        self.__updateToolTip()

    def getToolTip(self):
        return self.__toolTip

    toolTip = qt.Property(str, getToolTip, setToolTip)

    def setElideMode(self, elideMode):
        """Set the elide mode.

        :param qt.Qt.TextElideMode elidMode: Elide mode to use
        """
        self.__elideMode = elideMode
        self.__updateText()

    def getElideMode(self):
        """Returns the used elide mode.

        :rtype: qt.Qt.TextElideMode
        """
        return self.__elideMode

    elideMode = qt.Property(qt.Qt.TextElideMode, getToolTip, setToolTip)

    def setTextAsToolTip(self, enabled):
        """Enable displaying text as part of the tooltip if it is elided.

        :param bool enabled: Enable the behavior
        """
        if self.__textAsToolTip == enabled:
            return
        self.__textAsToolTip = enabled
        self.__updateToolTip()

    def getTextAsToolTip(self):
        """True if an elided text is displayed as part of the tooltip.

        :rtype: bool
        """
        return self.__textAsToolTip

    textAsToolTip = qt.Property(bool, getTextAsToolTip, setTextAsToolTip)