butterfly_viewer.aux_labels

QLabel widgets for Butterfly Viewer and Registrator.

Not intended as a script.

 1#!/usr/bin/env python3
 2
 3"""QLabel widgets for Butterfly Viewer and Registrator.
 4
 5Not intended as a script.
 6"""
 7# SPDX-License-Identifier: GPL-3.0-or-later
 8
 9
10
11from PyQt5 import QtCore, QtGui, QtWidgets
12
13
14
15class FilenameLabel(QtWidgets.QLabel):
16    """Styled label for overlaying filenames on viewers and image previews.
17    
18    Args:
19        text (str): The text to show (the filename).
20        remove_path (bool): True to remove path from the filename (filepath).
21        visibilty_based_on_text (bool): True to hide label when text is None; False to always show.
22        belongs_to_split (bool): True for improved style for sliding overlays in SplitView; False as default.
23    """
24    
25    def __init__(self, text=None, remove_path=True, visibility_based_on_text=False, belongs_to_split=False):
26        super().__init__()
27        
28        self.remove_path = remove_path
29        self.visibility_based_on_text = visibility_based_on_text
30        
31        if text is not None:
32            self.setText(text)
33
34        self.make_visible_based_on_text()
35            
36        self.setFrameStyle(QtWidgets.QFrame.Panel)
37        self.setAttribute(QtCore.Qt.WA_TransparentForMouseEvents)
38        self.setBackgroundRole(QtGui.QPalette.ToolTipBase)
39        if belongs_to_split:
40            self.setStyleSheet("QLabel {color: white; background-color: rgba(0, 0, 0, 191); border: 0px solid black; margin: 0.3em; font-size: 7.5pt; border-radius: 0px; }")
41        else:
42            self.setStyleSheet("QLabel {color: white; background-color: rgba(0, 0, 0, 191); border: 0px solid black; margin: 0.2em; font-size: 7.5pt; border-radius: 0px; }")
43        self.setSizePolicy(QtWidgets.QSizePolicy.Ignored, QtWidgets.QSizePolicy.Preferred)
44        
45    def setText(self, text):
46        """str: Override setText to remove path in filename and set visibilty as specified."""
47        if text is not None:
48            if self.remove_path:
49                text = text.split("/")[-1]
50        
51        super().setText(text)
52
53        self.make_visible_based_on_text()
54
55    def make_visible_based_on_text(self, text=None):
56        """Make label visible if label has text; hide label if no text (only if set to do so).
57        
58        Only call with no arguments (for example, make_visible_based_on_text()).
59        """
60        text = self.text()
61        if text is "":
62            text = None
63        if self.visibility_based_on_text:
64            if text is not None:
65                self.setVisible(True)
66            else:
67                self.setVisible(False)
68
69    def set_visible_based_on_text(self, value):
70        """bool: Set visibilty of label but take into account the setting for visibilty based on text."""
71        text = self.text()
72        if text is "":
73            text = None
74        if self.visibility_based_on_text:
75            if text is None:
76                value = False
77        self.setVisible(value)
class FilenameLabel(PyQt5.QtWidgets.QLabel):
16class FilenameLabel(QtWidgets.QLabel):
17    """Styled label for overlaying filenames on viewers and image previews.
18    
19    Args:
20        text (str): The text to show (the filename).
21        remove_path (bool): True to remove path from the filename (filepath).
22        visibilty_based_on_text (bool): True to hide label when text is None; False to always show.
23        belongs_to_split (bool): True for improved style for sliding overlays in SplitView; False as default.
24    """
25    
26    def __init__(self, text=None, remove_path=True, visibility_based_on_text=False, belongs_to_split=False):
27        super().__init__()
28        
29        self.remove_path = remove_path
30        self.visibility_based_on_text = visibility_based_on_text
31        
32        if text is not None:
33            self.setText(text)
34
35        self.make_visible_based_on_text()
36            
37        self.setFrameStyle(QtWidgets.QFrame.Panel)
38        self.setAttribute(QtCore.Qt.WA_TransparentForMouseEvents)
39        self.setBackgroundRole(QtGui.QPalette.ToolTipBase)
40        if belongs_to_split:
41            self.setStyleSheet("QLabel {color: white; background-color: rgba(0, 0, 0, 191); border: 0px solid black; margin: 0.3em; font-size: 7.5pt; border-radius: 0px; }")
42        else:
43            self.setStyleSheet("QLabel {color: white; background-color: rgba(0, 0, 0, 191); border: 0px solid black; margin: 0.2em; font-size: 7.5pt; border-radius: 0px; }")
44        self.setSizePolicy(QtWidgets.QSizePolicy.Ignored, QtWidgets.QSizePolicy.Preferred)
45        
46    def setText(self, text):
47        """str: Override setText to remove path in filename and set visibilty as specified."""
48        if text is not None:
49            if self.remove_path:
50                text = text.split("/")[-1]
51        
52        super().setText(text)
53
54        self.make_visible_based_on_text()
55
56    def make_visible_based_on_text(self, text=None):
57        """Make label visible if label has text; hide label if no text (only if set to do so).
58        
59        Only call with no arguments (for example, make_visible_based_on_text()).
60        """
61        text = self.text()
62        if text is "":
63            text = None
64        if self.visibility_based_on_text:
65            if text is not None:
66                self.setVisible(True)
67            else:
68                self.setVisible(False)
69
70    def set_visible_based_on_text(self, value):
71        """bool: Set visibilty of label but take into account the setting for visibilty based on text."""
72        text = self.text()
73        if text is "":
74            text = None
75        if self.visibility_based_on_text:
76            if text is None:
77                value = False
78        self.setVisible(value)

Styled label for overlaying filenames on viewers and image previews.

Arguments:
  • text (str): The text to show (the filename).
  • remove_path (bool): True to remove path from the filename (filepath).
  • visibilty_based_on_text (bool): True to hide label when text is None; False to always show.
  • belongs_to_split (bool): True for improved style for sliding overlays in SplitView; False as default.
def setText(self, text):
46    def setText(self, text):
47        """str: Override setText to remove path in filename and set visibilty as specified."""
48        if text is not None:
49            if self.remove_path:
50                text = text.split("/")[-1]
51        
52        super().setText(text)
53
54        self.make_visible_based_on_text()

str: Override setText to remove path in filename and set visibilty as specified.

def make_visible_based_on_text(self, text=None):
56    def make_visible_based_on_text(self, text=None):
57        """Make label visible if label has text; hide label if no text (only if set to do so).
58        
59        Only call with no arguments (for example, make_visible_based_on_text()).
60        """
61        text = self.text()
62        if text is "":
63            text = None
64        if self.visibility_based_on_text:
65            if text is not None:
66                self.setVisible(True)
67            else:
68                self.setVisible(False)

Make label visible if label has text; hide label if no text (only if set to do so).

Only call with no arguments (for example, make_visible_based_on_text()).

def set_visible_based_on_text(self, value):
70    def set_visible_based_on_text(self, value):
71        """bool: Set visibilty of label but take into account the setting for visibilty based on text."""
72        text = self.text()
73        if text is "":
74            text = None
75        if self.visibility_based_on_text:
76            if text is None:
77                value = False
78        self.setVisible(value)

bool: Set visibilty of label but take into account the setting for visibilty based on text.