-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdynamic_menu_widgets.py
More file actions
144 lines (110 loc) · 4.76 KB
/
dynamic_menu_widgets.py
File metadata and controls
144 lines (110 loc) · 4.76 KB
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
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QSizePolicy, QWidget, QVBoxLayout, QSpacerItem, QScrollArea, QLabel
#Menu widget without scrolling
class MenuWidget(QWidget):
MIN_ELEMENT_WIDTH = 350
def __init__(self):
super().__init__()
#Menu infos
self.number_of_child_widgets = 0
#Widget elements, the widget allows to dynamically
#add new widgets but at initialization we define a 'QSpacerItem'
#so that all the widget within are 'pushed' to the top
self.bottom_spacer = QSpacerItem(MenuWidget.MIN_ELEMENT_WIDTH,0,QSizePolicy.Fixed,QSizePolicy.Expanding)
#for easy customization we define a name for the widget itself
#and all the widget within
self.setObjectName("Menu")
# Setting resize policy for elements
self.setMinimumWidth(MenuWidget.MIN_ELEMENT_WIDTH)
self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
#DefineLayout: Vertical
self.layout = QVBoxLayout()
#We want zeros spaces between elements
self.layout.setContentsMargins(0, 0, 0, 0)
self.layout.setSpacing(0)
self.layout.addSpacerItem(self.bottom_spacer)
self.setLayout(self.layout)
#Apply css
self.apply_css()
def addWidget(self, widget : QWidget):
#Add Widgets to the layout to the end
#but before the spacer!
self.layout.insertWidget(self.number_of_child_widgets,widget)
#Increment number of child widgets
self.number_of_child_widgets += 1
def apply_css(self):
# MUST: By default, for efficiency reason, css on custom widget is disabled
# to enable it:
self.setAttribute(Qt.WA_StyledBackground, True)
self.setStyleSheet("""
QWidget#Menu{
background-color: rgb(67,110,144);
}
""")
class SubMenuHeaderWidget(QLabel):
def __init__(self, location : str, color: str):
super().__init__()
# Text
self.setText(f"""<p style="color: white; font-family: 'JetBrains Mono'; font-size: 26px">
{location}
</p>
""")
# Alignment
self.setAlignment(Qt.AlignLeft)
# Size
self.setFixedHeight(40)
# Slight left margin
self.setContentsMargins(10,3,0,0)
# Apply css
self.apply_css(color)
def apply_css(self, color: str):
self.setStyleSheet(f"""
border-top: 1px solid white;
background-color: {color};
""")
class SubMenuWidget(MenuWidget):
SUB_MENU_NUMBER = 0
def __init__(self, location: str):
super().__init__()
# The only difference in the style
# is a left border
self.setAttribute(Qt.WA_StyledBackground, True)
self.setObjectName("SubMenu")
color = ["rgb(3,63,107)","#06538c"]
self.setStyleSheet(f"""
border-left: 7px solid {color[SubMenuWidget.SUB_MENU_NUMBER % 2]};
""")
# We add the header to indicate the nodes location
self.addWidget(SubMenuHeaderWidget(location,color[SubMenuWidget.SUB_MENU_NUMBER % 2]))
SubMenuWidget.SUB_MENU_NUMBER += 1
#Menu widget with scrolling
class ScrollableMenuWidget(QScrollArea):
def __init__(self):
super().__init__()
#Define widget with which we provide a scrollable view
self.menu_widget = MenuWidget()
#Set the widget as scrollable
self.setWidget(self.menu_widget)
self.setWidgetResizable(True)
# for easy customization we define a name for the widget itself
self.setObjectName("ScrollableMenu")
#Setting the width size
self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
self.setMaximumWidth(MenuWidget.MIN_ELEMENT_WIDTH)
#Apply css
self.apply_css()
def apply_css(self):
# MUST: By default, for efficiency reason, css on custom widget is disabled
# to enable it:
self.setAttribute(Qt.WA_StyledBackground, True)
self.setStyleSheet("""
QWidget#ScrollableMenu{
background-color: rgb(67,110,144);
margin: 0px;
border: 0px;
}
QScrollBar:vertical, QScrollBar:horizontal {
width: 0px;
height: 0px;
}
""")