Skip to content

Commit ffe9a68

Browse files
authored
Merge pull request #55 from SECTL/develop
resize sidebar
2 parents 159b483 + 46d9a0a commit ffe9a68

6 files changed

Lines changed: 481 additions & 129 deletions

File tree

app/tools/button_draw_utils.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""按钮绘制工具函数"""
2+
3+
from PySide6.QtCore import Qt
4+
from PySide6.QtGui import QPainter, QColor
5+
from qfluentwidgets.common.color import autoFallbackThemeColor
6+
from qfluentwidgets.common.config import isDarkTheme
7+
8+
9+
def centered_draw_background(button, painter: QPainter, button_size: int):
10+
"""绘制按钮背景,使选中指示条(小蓝条)垂直居中"""
11+
if button.isSelected:
12+
painter.setBrush(QColor(255, 255, 255, 42) if isDarkTheme() else Qt.white)
13+
painter.drawRoundedRect(button.rect(), 5, 5)
14+
15+
# 绘制指示条(小蓝条),垂直居中
16+
painter.setBrush(
17+
autoFallbackThemeColor(button.lightSelectedColor, button.darkSelectedColor)
18+
)
19+
indicator_height = 24 if not button.isPressed else 18
20+
indicator_y = (button_size - indicator_height) // 2
21+
if button.isPressed:
22+
indicator_y += 3 # 按下时稍微下移
23+
painter.drawRoundedRect(0, indicator_y, 4, indicator_height, 2, 2)
24+
elif button.isPressed or button.isEnter:
25+
c = 255 if isDarkTheme() else 0
26+
alpha = 9 if button.isEnter else 6
27+
painter.setBrush(QColor(c, c, c, alpha))
28+
painter.drawRoundedRect(button.rect(), 5, 5)

app/view/main/lottery.py

Lines changed: 67 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ def initUI(self):
108108
get_content_pushbutton_name_async("lottery", "reset_button")
109109
)
110110
self._set_widget_font(self.reset_button, 15)
111-
self.reset_button.setFixedSize(165, 45)
111+
self.reset_button.setFixedHeight(45)
112112
self.reset_button.clicked.connect(lambda: self.reset_count())
113113

114114
self.minus_button = PushButton("-")
@@ -141,21 +141,24 @@ def initUI(self):
141141
self.count_widget = QWidget()
142142
horizontal_layout = QHBoxLayout()
143143
horizontal_layout.setContentsMargins(0, 0, 0, 0)
144-
horizontal_layout.addWidget(self.minus_button, 0, Qt.AlignmentFlag.AlignLeft)
145-
horizontal_layout.addWidget(self.count_label, 0, Qt.AlignmentFlag.AlignLeft)
146-
horizontal_layout.addWidget(self.plus_button, 0, Qt.AlignmentFlag.AlignLeft)
144+
horizontal_layout.setSpacing(0)
145+
horizontal_layout.addWidget(self.minus_button)
146+
horizontal_layout.addStretch()
147+
horizontal_layout.addWidget(self.count_label)
148+
horizontal_layout.addStretch()
149+
horizontal_layout.addWidget(self.plus_button)
147150
self.count_widget.setLayout(horizontal_layout)
148151

149152
self.start_button = PrimaryPushButton(
150153
get_content_pushbutton_name_async("lottery", "start_button")
151154
)
152155
self._set_widget_font(self.start_button, 15)
153-
self.start_button.setFixedSize(165, 45)
156+
self.start_button.setFixedHeight(45)
154157
self.start_button.clicked.connect(lambda: self.start_draw())
155158

156159
self.pool_list_combobox = ComboBox()
157160
self._set_widget_font(self.pool_list_combobox, 12)
158-
self.pool_list_combobox.setFixedSize(165, 45)
161+
self.pool_list_combobox.setFixedHeight(45)
159162
self.pool_list_combobox.setPlaceholderText(
160163
get_content_name_async("lottery", "default_empty_item")
161164
)
@@ -164,27 +167,27 @@ def initUI(self):
164167

165168
self.list_combobox = ComboBox()
166169
self._set_widget_font(self.list_combobox, 12)
167-
self.list_combobox.setFixedSize(165, 45)
170+
self.list_combobox.setFixedHeight(45)
168171
# 延迟填充班级列表,避免启动时进行文件IO
169172
self.list_combobox.currentTextChanged.connect(self.on_class_changed)
170173

171174
self.range_combobox = ComboBox()
172175
self._set_widget_font(self.range_combobox, 12)
173-
self.range_combobox.setFixedSize(165, 45)
176+
self.range_combobox.setFixedHeight(45)
174177
# 延迟填充范围选项
175178
self.range_combobox.currentTextChanged.connect(self.on_filter_changed)
176179

177180
self.gender_combobox = ComboBox()
178181
self._set_widget_font(self.gender_combobox, 12)
179-
self.gender_combobox.setFixedSize(165, 45)
182+
self.gender_combobox.setFixedHeight(45)
180183
# 延迟填充性别选项
181184
self.gender_combobox.currentTextChanged.connect(self.on_filter_changed)
182185

183186
self.remaining_button = PushButton(
184187
get_content_pushbutton_name_async("lottery", "remaining_button")
185188
)
186189
self._set_widget_font(self.remaining_button, 12)
187-
self.remaining_button.setFixedSize(165, 45)
190+
self.remaining_button.setFixedHeight(45)
188191
self.remaining_button.clicked.connect(lambda: self.show_remaining_list())
189192

190193
# 初始时不进行昂贵的数据加载,改为延迟填充
@@ -197,7 +200,6 @@ def initUI(self):
197200
self.many_count_label = BodyLabel(formatted_text)
198201
self.many_count_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
199202
self._set_widget_font(self.many_count_label, 10)
200-
self.many_count_label.setFixedWidth(165)
201203

202204
self.control_widget = QWidget()
203205
self.control_layout = QVBoxLayout(self.control_widget)
@@ -285,6 +287,9 @@ def initUI(self):
285287
main_layout.addWidget(scroll, 1)
286288
main_layout.addWidget(self.control_widget)
287289

290+
# 统一调整控件宽度以适应文本内容
291+
self._adjustControlWidgetWidths()
292+
288293
# 在事件循环中延迟填充下拉框和初始统计,减少启动阻塞
289294
QTimer.singleShot(0, self.populate_lists)
290295

@@ -301,6 +306,54 @@ def add_control_widget_if_enabled(
301306
# 出错时默认添加控件
302307
layout.addWidget(widget, alignment=Qt.AlignmentFlag.AlignCenter)
303308

309+
def _adjustControlWidgetWidths(self):
310+
"""统一调整控件宽度以适应文本内容"""
311+
try:
312+
# 收集所有需要调整宽度的控件
313+
widgets_to_adjust = [
314+
self.reset_button,
315+
self.count_widget,
316+
self.start_button,
317+
self.pool_list_combobox,
318+
self.list_combobox,
319+
self.range_combobox,
320+
self.gender_combobox,
321+
self.remaining_button,
322+
self.many_count_label,
323+
]
324+
325+
# 计算所有控件文本所需的最大宽度
326+
max_text_width = 0
327+
for widget in widgets_to_adjust:
328+
fm = widget.fontMetrics()
329+
# 检查按钮/标签文本
330+
if hasattr(widget, "text") and widget.text():
331+
text_width = fm.horizontalAdvance(widget.text())
332+
max_text_width = max(max_text_width, text_width)
333+
# 检查占位符文本
334+
if hasattr(widget, "placeholderText") and widget.placeholderText():
335+
text_width = fm.horizontalAdvance(widget.placeholderText())
336+
max_text_width = max(max_text_width, text_width)
337+
# 检查下拉框所有选项的宽度
338+
if hasattr(widget, "count"):
339+
for i in range(widget.count()):
340+
item_text = widget.itemText(i)
341+
if item_text:
342+
text_width = fm.horizontalAdvance(item_text)
343+
max_text_width = max(max_text_width, text_width)
344+
345+
# 计算统一宽度(文本宽度 + 边距 + 下拉框箭头空间)
346+
padding = 60 # 左右边距 + 下拉箭头空间
347+
min_width = 165 # 最小宽度
348+
unified_width = max(min_width, max_text_width + padding)
349+
350+
# 设置所有控件的固定宽度
351+
for widget in widgets_to_adjust:
352+
widget.setFixedWidth(int(unified_width))
353+
354+
except Exception as e:
355+
logger.debug(f"调整控件宽度时出错: {e}")
356+
304357
def on_pool_changed(self):
305358
"""当奖池选择改变时,更新奖数显示"""
306359
try:
@@ -961,6 +1014,9 @@ def populate_lists(self):
9611014

9621015
LotteryUtils.update_start_button_state(self.start_button, total_count)
9631016

1017+
# 重新调整控件宽度以适应下拉框内容
1018+
self._adjustControlWidgetWidths()
1019+
9641020
except Exception as e:
9651021
logger.error(f"延迟填充列表失败: {e}")
9661022

app/view/main/roll_call.py

Lines changed: 65 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ def initUI(self):
108108
get_content_pushbutton_name_async("roll_call", "reset_button")
109109
)
110110
self._set_widget_font(self.reset_button, 15)
111-
self.reset_button.setFixedSize(165, 45)
111+
self.reset_button.setFixedHeight(45)
112112
self.reset_button.clicked.connect(lambda: self.reset_count())
113113

114114
self.minus_button = PushButton("-")
@@ -141,21 +141,24 @@ def initUI(self):
141141
self.count_widget = QWidget()
142142
horizontal_layout = QHBoxLayout()
143143
horizontal_layout.setContentsMargins(0, 0, 0, 0)
144-
horizontal_layout.addWidget(self.minus_button, 0, Qt.AlignmentFlag.AlignLeft)
145-
horizontal_layout.addWidget(self.count_label, 0, Qt.AlignmentFlag.AlignLeft)
146-
horizontal_layout.addWidget(self.plus_button, 0, Qt.AlignmentFlag.AlignLeft)
144+
horizontal_layout.setSpacing(0)
145+
horizontal_layout.addWidget(self.minus_button)
146+
horizontal_layout.addStretch()
147+
horizontal_layout.addWidget(self.count_label)
148+
horizontal_layout.addStretch()
149+
horizontal_layout.addWidget(self.plus_button)
147150
self.count_widget.setLayout(horizontal_layout)
148151

149152
self.start_button = PrimaryPushButton(
150153
get_content_pushbutton_name_async("roll_call", "start_button")
151154
)
152155
self._set_widget_font(self.start_button, 15)
153-
self.start_button.setFixedSize(165, 45)
156+
self.start_button.setFixedHeight(45)
154157
self.start_button.clicked.connect(lambda: self.start_draw())
155158

156159
self.list_combobox = ComboBox()
157160
self._set_widget_font(self.list_combobox, 12)
158-
self.list_combobox.setFixedSize(165, 45)
161+
self.list_combobox.setFixedHeight(45)
159162
self.list_combobox.setPlaceholderText(
160163
get_content_name_async("roll_call", "default_empty_item")
161164
)
@@ -164,21 +167,21 @@ def initUI(self):
164167

165168
self.range_combobox = ComboBox()
166169
self._set_widget_font(self.range_combobox, 12)
167-
self.range_combobox.setFixedSize(165, 45)
170+
self.range_combobox.setFixedHeight(45)
168171
# 延迟填充范围选项
169172
self.range_combobox.currentTextChanged.connect(self.on_filter_changed)
170173

171174
self.gender_combobox = ComboBox()
172175
self._set_widget_font(self.gender_combobox, 12)
173-
self.gender_combobox.setFixedSize(165, 45)
176+
self.gender_combobox.setFixedHeight(45)
174177
# 延迟填充性别选项
175178
self.gender_combobox.currentTextChanged.connect(self.on_filter_changed)
176179

177180
self.remaining_button = PushButton(
178181
get_content_pushbutton_name_async("roll_call", "remaining_button")
179182
)
180183
self._set_widget_font(self.remaining_button, 12)
181-
self.remaining_button.setFixedSize(165, 45)
184+
self.remaining_button.setFixedHeight(45)
182185
self.remaining_button.clicked.connect(lambda: self.show_remaining_list())
183186

184187
# 初始时不进行昂贵的数据加载,改为延迟填充
@@ -193,7 +196,6 @@ def initUI(self):
193196
self.many_count_label = BodyLabel(formatted_text)
194197
self.many_count_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
195198
self._set_widget_font(self.many_count_label, 10)
196-
self.many_count_label.setFixedWidth(165)
197199

198200
self.control_widget = QWidget()
199201
self.control_layout = QVBoxLayout(self.control_widget)
@@ -271,6 +273,9 @@ def initUI(self):
271273
main_layout.addWidget(scroll, 1)
272274
main_layout.addWidget(self.control_widget)
273275

276+
# 统一调整控件宽度以适应文本内容
277+
self._adjustControlWidgetWidths()
278+
274279
# 在事件循环中延迟填充下拉框和初始统计,减少启动阻塞
275280
QTimer.singleShot(0, self.populate_lists)
276281

@@ -287,6 +292,53 @@ def add_control_widget_if_enabled(
287292
# 出错时默认添加控件
288293
layout.addWidget(widget, alignment=Qt.AlignmentFlag.AlignCenter)
289294

295+
def _adjustControlWidgetWidths(self):
296+
"""统一调整控件宽度以适应文本内容"""
297+
try:
298+
# 收集所有需要调整宽度的控件
299+
widgets_to_adjust = [
300+
self.reset_button,
301+
self.count_widget,
302+
self.start_button,
303+
self.list_combobox,
304+
self.range_combobox,
305+
self.gender_combobox,
306+
self.remaining_button,
307+
self.many_count_label,
308+
]
309+
310+
# 计算所有控件文本所需的最大宽度
311+
max_text_width = 0
312+
for widget in widgets_to_adjust:
313+
fm = widget.fontMetrics()
314+
# 检查按钮/标签文本
315+
if hasattr(widget, "text") and widget.text():
316+
text_width = fm.horizontalAdvance(widget.text())
317+
max_text_width = max(max_text_width, text_width)
318+
# 检查占位符文本
319+
if hasattr(widget, "placeholderText") and widget.placeholderText():
320+
text_width = fm.horizontalAdvance(widget.placeholderText())
321+
max_text_width = max(max_text_width, text_width)
322+
# 检查下拉框所有选项的宽度
323+
if hasattr(widget, "count"):
324+
for i in range(widget.count()):
325+
item_text = widget.itemText(i)
326+
if item_text:
327+
text_width = fm.horizontalAdvance(item_text)
328+
max_text_width = max(max_text_width, text_width)
329+
330+
# 计算统一宽度(文本宽度 + 边距 + 下拉框箭头空间)
331+
padding = 60 # 左右边距 + 下拉箭头空间
332+
min_width = 165 # 最小宽度
333+
unified_width = max(min_width, max_text_width + padding)
334+
335+
# 设置所有控件的固定宽度
336+
for widget in widgets_to_adjust:
337+
widget.setFixedWidth(int(unified_width))
338+
339+
except Exception as e:
340+
logger.debug(f"调整控件宽度时出错: {e}")
341+
290342
def on_class_changed(self):
291343
"""当班级选择改变时,更新范围选择、性别选择和人数显示"""
292344
self.range_combobox.blockSignals(True)
@@ -884,6 +936,9 @@ def populate_lists(self):
884936
# 根据总人数是否为0,启用或禁用开始按钮
885937
RollCallUtils.update_start_button_state(self.start_button, total_count)
886938

939+
# 重新调整控件宽度以适应下拉框内容
940+
self._adjustControlWidgetWidths()
941+
887942
except Exception as e:
888943
logger.error(f"延迟填充列表失败: {e}")
889944

0 commit comments

Comments
 (0)