Source code for ccpn.ui.gui.widgets.Label

"""Module Documentation here

"""
#=========================================================================================
# Licence, Reference and Credits
#=========================================================================================
__copyright__ = "Copyright (C) CCPN project (http://www.ccpn.ac.uk) 2014 - 2021"
__credits__ = ("Ed Brooksbank, Luca Mureddu, Timothy J Ragan & Geerten W Vuister")
__licence__ = ("CCPN licence. See http://www.ccpn.ac.uk/v3-software/downloads/license")
__reference__ = ("Skinner, S.P., Fogh, R.H., Boucher, W., Ragan, T.J., Mureddu, L.G., & Vuister, G.W.",
                 "CcpNmr AnalysisAssign: a flexible platform for integrated NMR analysis",
                 "J.Biomol.Nmr (2016), 66, 111-124, http://doi.org/10.1007/s10858-016-0060-y")
#=========================================================================================
# Last code modification
#=========================================================================================
__modifiedBy__ = "$modifiedBy: Ed Brooksbank $"
__dateModified__ = "$dateModified: 2021-02-04 12:07:37 +0000 (Thu, February 04, 2021) $"
__version__ = "$Revision: 3.0.3 $"
#=========================================================================================
# Created
#=========================================================================================
__author__ = "$Author: Geerten Vuister $"
__date__ = "$Date: 2017-04-07 10:28:41 +0000 (Fri, April 07, 2017) $"
#=========================================================================================
# Start of code
#=========================================================================================

from PyQt5 import QtWidgets
from pyqtgraph.widgets.VerticalLabel import VerticalLabel as pyqtVerticalLabel
from ccpn.ui.gui.widgets.Base import Base
from ccpn.framework.Translation import translator
import ccpn.ui.gui.guiSettings as guiSettings
from ccpn.ui.gui.widgets.Icon import Icon


[docs]class Label(QtWidgets.QLabel, Base): _styleSheet = """QLabel { color: %s; margin-left: %dpx; margin-top: %dpx; margin-right: %dpx; margin-bottom: %dpx; border: 0px; }""" def __init__(self, parent=None, text='', textColour=None, textSize=None, bold=False, italic=False, margins=[2, 1, 2, 1], icon=None, iconSize=(16, 16), **kwds): super().__init__(parent) Base._init(self, **kwds) text = translator.translate(text) self.setText(text) self._textSize = textSize self._bold = 'bold' if bold else 'normal' self._margins = margins if bold or textSize or italic: _font = self.font() if bold: _font.setBold(True) if italic: _font.setItalic(True) if textSize: _font.setPointSize(textSize) self.setFont(_font) colours = guiSettings.getColours() self._colour = textColour if textColour else colours[guiSettings.LABEL_FOREGROUND] self._setStyleSheet() if isinstance(icon, Icon): self.setPixmap(icon.pixmap(*iconSize))
[docs] def get(self): """get the label text """ return self.text()
[docs] def set(self, text=''): """set label text, applying translator """ text = translator.translate(text) self.setText(text)
def _setStyleSheet(self): self.setStyleSheet(self._styleSheet % ( #self._textSize, # self._bold, self._colour, self._margins[0], self._margins[1], self._margins[2], self._margins[3], ) )
[docs]class ActiveLabel(Label): def __init__(self, parent=None, mainWindow=None, text='', textColour=None, textSize=12, bold=False, margins=[2, 1, 2, 1], selectionCallback=None, actionCallback=None, **kwds): super().__init__(parent=parent, text=text, textColour=textColour, textSize=textSize, bold=bold, margins=margins, **kwds) self.mainWindow = mainWindow self._selectionCallback = selectionCallback self._actionCallback = actionCallback self._enterCallback = None self._leaveCallback = None # required highlighting flag for changing colour themes self.highlighted = False
[docs] def setSelectionCallback(self, callback=None): """Sets callback on mouse click """ self._selectionCallback = callback
[docs] def setActionCallback(self, callback=None): """Sets callback on mouse double click """ self._actionCallback = callback
[docs] def mouseReleaseEvent(self, ev): """Handle double click and call _selectionCallback if set """ if self._selectionCallback: self._selectionCallback() super().mouseReleaseEvent(ev)
[docs] def mouseDoubleClickEvent(self, ev): """Handle double click and call _actionCallback if set """ if self._actionCallback: self._actionCallback() super().mouseDoubleClickEvent(ev)
[docs] def setEnterLeaveCallback(self, enterCallback, leaveCallback): self._enterCallback = enterCallback self._leaveCallback = leaveCallback
[docs] def enterEvent(self, ev) -> None: super().enterEvent(ev) if self._enterCallback: self._enterCallback()
[docs] def leaveEvent(self, ev) -> None: super().leaveEvent(ev) if self._leaveCallback: self._leaveCallback()
[docs]class VerticalLabel(pyqtVerticalLabel, Base): _styleSheet = """ QLabel { font-size: %spt; font-weight: %s; color: %s; margin-left: %dpx; margin-top: %dpx; margin-right: %dpx; margin-bottom: %dpx; border: 0px; } """ def __init__(self, parent=None, text='', textColour=None, textSize=12, bold=False, margins=[2, 1, 2, 1], orientation='horizontal', **kwds): super().__init__(parent, orientation=orientation, forceWidth=False) Base._init(self, **kwds) text = translator.translate(text) self.setText(text) # if textColor: # self.setStyleSheet('QLabel {color: %s}' % textColor) # if textSize and textColor: # self.setStyleSheet('QLabel {font-size: %s; color: %s;}' % (textSize, textColor)) # if bold: # self.setStyleSheet('QLabel {font-weight: bold;}') self._textSize = textSize self._bold = 'bold' if bold else 'normal' self._margins = margins # this appears not to pick up the colour as set by the stylesheet! # self._colour = textColor if textColor else self.palette().color(QtGui.QPalette.WindowText).name() colours = guiSettings.getColours() self._colour = textColour if textColour else colours[guiSettings.LABEL_FOREGROUND] self._setStyleSheet()
[docs] def get(self): "get the label text" return self.text()
[docs] def set(self, text=''): "set label text, applying translator" text = translator.translate(text) self.setText(text)
def _setStyleSheet(self): self.setStyleSheet(self._styleSheet % (self._textSize, self._bold, self._colour, self._margins[0], self._margins[1], self._margins[2], self._margins[3], ) )
if __name__ == '__main__': from ccpn.ui.gui.widgets.Application import TestApplication from ccpn.ui.gui.widgets.Button import Button msg = 'Hello world' count = 0 def func(): global count count += 1 label.set(msg + ' ' + str(count)) print(label.get()) app = TestApplication() window = QtWidgets.QWidget() label = Label(window, text=msg, textColor='red', grid=(0, 0)) button = Button(window, text='Click me', callback=func, grid=(0, 1)) window.show() app.start()