File size: 7,110 Bytes
b2fbb41 | 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 | from qtpy.QtWidgets import QPushButton, QHBoxLayout, QLabel, QGroupBox, QScrollArea, QVBoxLayout, QSizePolicy
from qtpy.QtCore import Qt, Signal
from qtpy.QtGui import QFontMetrics, QFontMetrics, QIcon, QMouseEvent
from .scrollbar import ScrollBar
from .widget import Widget
from utils import shared
from utils.config import pcfg
CHEVRON_SIZE = 20
CHEVRON_SIZE_SMALL = 14
def chevron_down():
return QIcon(r'icons/chevron-down.svg').pixmap(CHEVRON_SIZE, CHEVRON_SIZE, mode=QIcon.Mode.Normal)
def chevron_right():
return QIcon(r'icons/chevron-right.svg').pixmap(CHEVRON_SIZE, CHEVRON_SIZE, mode=QIcon.Mode.Normal)
def chevron_down_small():
return QIcon(r'icons/chevron-down.svg').pixmap(CHEVRON_SIZE_SMALL, CHEVRON_SIZE_SMALL, mode=QIcon.Mode.Normal)
def chevron_right_small():
return QIcon(r'icons/chevron-right.svg').pixmap(CHEVRON_SIZE_SMALL, CHEVRON_SIZE_SMALL, mode=QIcon.Mode.Normal)
class HidePanelButton(QPushButton):
pass
class ExpandLabel(Widget):
clicked = Signal()
def __init__(self, text=None, parent=None, size_type='normal', *args, **kwargs):
super().__init__(parent=parent, *args, **kwargs)
self.size_type = size_type
self.textlabel = QLabel(self)
self.textlabel.setAttribute(Qt.WidgetAttribute.WA_TranslucentBackground, True)
self.arrowlabel = QLabel(self)
self.arrowlabel.setAttribute(Qt.WidgetAttribute.WA_TranslucentBackground, True)
font = self.textlabel.font()
if size_type == 'normal':
if shared.ON_MACOS:
font.setPointSize(13)
else:
font.setPointSizeF(10)
self.setFixedHeight(26)
self.arrowlabel.setFixedSize(CHEVRON_SIZE, CHEVRON_SIZE)
elif size_type == 'small':
if shared.ON_MACOS:
font.setPointSize(10)
else:
font.setPointSizeF(8)
self.setFixedHeight(20)
self.arrowlabel.setFixedSize(CHEVRON_SIZE_SMALL, CHEVRON_SIZE_SMALL)
else:
raise
self.textlabel.setFont(font)
self.hidelabel = HidePanelButton(self)
self.hidelabel.setVisible(False)
if text is not None:
self.textlabel.setText(text)
layout = QHBoxLayout(self)
layout.addWidget(self.arrowlabel)
layout.addWidget(self.textlabel)
layout.setContentsMargins(0, 0, 0, 0)
layout.setSpacing(1)
layout.addStretch(-1)
layout.addWidget(self.hidelabel)
self.expanded = True
self.setExpand(True)
def enterEvent(self, event) -> None:
self.hidelabel.setVisible(True)
return super().enterEvent(event)
def leaveEvent(self, event) -> None:
self.hidelabel.setVisible(False)
return super().leaveEvent(event)
def setExpand(self, expand: bool):
self.expanded = expand
if expand:
self.arrowlabel.setPixmap(chevron_down())
else:
self.arrowlabel.setPixmap(chevron_right())
def mousePressEvent(self, e: QMouseEvent) -> None:
if e.button() == Qt.MouseButton.LeftButton:
self.setExpand(not self.expanded)
pcfg.expand_tstyle_panel = self.expanded
self.clicked.emit()
return super().mousePressEvent(e)
class PanelArea(QScrollArea):
def __init__(self, panel_name: str, config_name: str, config_expand_name: str, action_name: str = None):
super().__init__()
self.scrollContent = PanelAreaContent()
self.scrollContent.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Maximum)
self.setWidget(self.scrollContent)
self.setWidgetResizable(True)
self.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Maximum)
ScrollBar(Qt.Orientation.Vertical, self)
self.setVerticalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAlwaysOff)
ScrollBar(Qt.Orientation.Horizontal, self)
self.setHorizontalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAlwaysOff)
self.view_widget = ViewWidget(self, panel_name)
self.view_hide_btn_clicked = self.view_widget.view_hide_btn_clicked
self.expand_changed = self.view_widget.expend_changed
self.title = self.view_widget.title
self.setTitle = self.view_widget.setTitle
self.elidedText = self.view_widget.elidedText
self.set_expend_area = self.view_widget.set_expend_area
if action_name is None:
action_name = panel_name
self.view_widget.register_view_widget(
config_name=config_name,
config_expand_name=config_expand_name,
action_name=action_name
)
def setContentLayout(self, layout):
self.scrollContent.setLayout(layout)
class PanelGroupBox(QGroupBox):
pass
class PanelAreaContent(Widget):
after_resized = Signal()
def resizeEvent(self, event) -> None:
super().resizeEvent(event)
self.after_resized.emit()
class ViewWidget(Widget):
config_name: str = ''
config_expand_name: str = ''
action_name: str = ''
view_hide_btn_clicked = Signal(str)
expend_changed = Signal()
def __init__(self, content_widget: Widget, panel_name: str = None, parent=None, title_size_type='normal', *args, **kwargs):
super().__init__(parent=parent, *args, **kwargs)
self.title_label = ExpandLabel(panel_name, self, size_type=title_size_type)
self.title_label.hidelabel.clicked.connect(self.on_view_hide_btn_clicked)
self.content_widget = content_widget
layout = QVBoxLayout(self)
layout.addWidget(self.title_label)
layout.addWidget(self.content_widget)
layout.setContentsMargins(0, 0, 0, 0)
layout.setSpacing(0)
self.title_label.clicked.connect(self.set_expend_area)
self.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Maximum)
def on_view_hide_btn_clicked(self):
self.view_hide_btn_clicked.emit(self.config_name)
def register_view_widget(self, config_name: str, config_expand_name: str, action_name: str):
self.config_name = config_name
self.config_expand_name = config_expand_name
self.action_name = action_name
shared.register_view_widget(self)
def set_expend_area(self, expend: bool = None, set_config: bool = True):
if expend is None:
return self.set_expend_area(self.title_label.expanded)
if self.title_label.expanded != expend:
self.title_label.setExpand(expend)
self.content_widget.setVisible(expend)
if set_config:
setattr(pcfg, self.config_expand_name, expend)
def setTitle(self, text: str):
self.title_label.textlabel.setText(text)
def elidedText(self, text: str):
fm = QFontMetrics(self.title_label.font())
return fm.elidedText(text, Qt.TextElideMode.ElideRight, self.content_widget.width() - 40)
def title(self) -> str:
return self.title_label.textlabel.text()
|