From 5e7f48cc1be337e78baf5c8e7b2764cf9b97c337 Mon Sep 17 00:00:00 2001 From: Maziang <929110464@qq.com> Date: Sun, 23 Nov 2025 20:08:43 +0800 Subject: [PATCH] =?UTF-8?q?=E6=A0=B7=E5=BC=8F=E8=A1=A8=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/ui/word_style_ui.py | 67 ++++++++++++++++++++++--- src/word_main_window.py | 106 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 165 insertions(+), 8 deletions(-) diff --git a/src/ui/word_style_ui.py b/src/ui/word_style_ui.py index be39837..8e832db 100644 --- a/src/ui/word_style_ui.py +++ b/src/ui/word_style_ui.py @@ -238,18 +238,19 @@ class WordRibbon(QFrame): preview_layout.setSpacing(8) style_items = [ - ("正文", "font-size:14px;"), - ("无间隔", "font-size:14px;"), - ("标题 1", "font-size:22px; font-weight:bold; color:#2E75B6;"), - ("标题 2", "font-size:18px; color:#2E75B6;"), - ("标题 3", "font-size:16px; font-weight:bold;"), - ("副标题", "font-size:14px; font-style:italic; color:#555;"), - ("强调", "font-size:14px; color:#C0504D;"), + ("正文", "font-size:14px;", "body_text_preview_btn"), + ("无间隔", "font-size:14px;", "no_spacing_preview_btn"), + ("标题 1", "font-size:22px; font-weight:bold; color:#2E75B6;", "heading1_preview_btn"), + ("标题 2", "font-size:18px; color:#2E75B6;", "heading2_preview_btn"), + ("标题 3", "font-size:16px; font-weight:bold;", "heading3_preview_btn"), + ("副标题", "font-size:14px; font-style:italic; color:#555;", "subtitle_preview_btn"), + ("强调", "font-size:14px; color:#C0504D;", "emphasis_preview_btn"), ] - for text, style in style_items: + for text, style, obj_name in style_items: btn = QPushButton(text) btn.setFixedSize(95, 60) + btn.setObjectName(obj_name) btn.setStyleSheet(f""" QPushButton {{ background: white; @@ -264,6 +265,9 @@ class WordRibbon(QFrame): }} """) preview_layout.addWidget(btn) + + # 将按钮保存为实例属性,以便主窗口可以连接信号 + setattr(self, obj_name, btn) preview_layout.addStretch() @@ -300,6 +304,9 @@ class WordRibbon(QFrame): self.update_combo_styles(is_dark) self.update_font_button_styles(is_dark) + # 更新样式预览按钮样式 + self.update_style_preview_buttons(is_dark) + # 更新天气组件样式 if hasattr(self, 'weather_icon_label') and self.weather_icon_label is not None: self.weather_icon_label.setStyleSheet(f""" @@ -397,6 +404,50 @@ class WordRibbon(QFrame): # 更新字体工具栏按钮样式 self.update_font_button_styles(is_dark) + def update_style_preview_buttons(self, is_dark): + """更新样式预览按钮样式""" + colors = theme_manager.get_current_theme_colors() + + # 样式预览按钮配置 + style_items = [ + ("正文", "font-size:14px;", "body_text_preview_btn"), + ("无间隔", "font-size:14px;", "no_spacing_preview_btn"), + ("标题 1", "font-size:22px; font-weight:bold; color:#2E75B6;", "heading1_preview_btn"), + ("标题 2", "font-size:18px; color:#2E75B6;", "heading2_preview_btn"), + ("标题 3", "font-size:16px; font-weight:bold;", "heading3_preview_btn"), + ("副标题", "font-size:14px; font-style:italic; color:#555;", "subtitle_preview_btn"), + ("强调", "font-size:14px; color:#C0504D;", "emphasis_preview_btn"), + ] + + for text, style, obj_name in style_items: + if hasattr(self, obj_name): + btn = getattr(self, obj_name) + # 根据主题调整颜色 + if is_dark: + # 黑色模式下的颜色调整 + if "color:#2E75B6" in style: + style = style.replace("color:#2E75B6", "color:#5B9BD5") + elif "color:#555" in style: + style = style.replace("color:#555", "color:#999") + elif "color:#C0504D" in style: + style = style.replace("color:#C0504D", "color:#E74C3C") + + btn.setStyleSheet(f""" + QPushButton {{ + background-color: {colors['surface']}; + border: 1px solid {colors['border']}; + border-radius: 3px; + text-align: left; + padding: 5px; + color: {colors['text']}; + {style} + }} + QPushButton:hover {{ + border: 1px solid {colors['accent']}; + background-color: {colors['surface_hover']}; + }} + """) + def update_font_button_styles(self, is_dark): """更新字体工具栏按钮样式""" colors = theme_manager.get_current_theme_colors() diff --git a/src/word_main_window.py b/src/word_main_window.py index 452a2b8..8e66b19 100644 --- a/src/word_main_window.py +++ b/src/word_main_window.py @@ -974,6 +974,22 @@ class WordStyleMainWindow(QMainWindow): if hasattr(self.ribbon, 'body_text_btn'): self.ribbon.body_text_btn.clicked.connect(self.on_body_text_clicked) + # 样式预览按钮信号 + if hasattr(self.ribbon, 'body_text_preview_btn'): + self.ribbon.body_text_preview_btn.clicked.connect(self.on_body_text_clicked) + if hasattr(self.ribbon, 'no_spacing_preview_btn'): + self.ribbon.no_spacing_preview_btn.clicked.connect(self.on_no_spacing_clicked) + if hasattr(self.ribbon, 'heading1_preview_btn'): + self.ribbon.heading1_preview_btn.clicked.connect(self.on_heading1_clicked) + if hasattr(self.ribbon, 'heading2_preview_btn'): + self.ribbon.heading2_preview_btn.clicked.connect(self.on_heading2_clicked) + if hasattr(self.ribbon, 'heading3_preview_btn'): + self.ribbon.heading3_preview_btn.clicked.connect(self.on_heading3_clicked) + if hasattr(self.ribbon, 'subtitle_preview_btn'): + self.ribbon.subtitle_preview_btn.clicked.connect(self.on_subtitle_clicked) + if hasattr(self.ribbon, 'emphasis_preview_btn'): + self.ribbon.emphasis_preview_btn.clicked.connect(self.on_emphasis_clicked) + # 查找和替换按钮信号 if hasattr(self.ribbon, 'find_btn'): self.ribbon.find_btn.clicked.connect(self.show_find_dialog) @@ -1482,6 +1498,18 @@ class WordStyleMainWindow(QMainWindow): """正文按钮点击处理""" self.apply_body_text_style() + def on_subtitle_clicked(self): + """副标题按钮点击处理""" + self.apply_subtitle_style() + + def on_emphasis_clicked(self): + """强调按钮点击处理""" + self.apply_emphasis_style() + + def on_no_spacing_clicked(self): + """无间隔按钮点击处理""" + self.apply_no_spacing_style() + def on_align_left_clicked(self): """左对齐按钮点击处理""" self.apply_alignment(Qt.AlignLeft) @@ -1589,6 +1617,84 @@ class WordStyleMainWindow(QMainWindow): self.text_edit.setCurrentCharFormat(char_format) self.text_edit.textCursor().setBlockFormat(block_format) + def apply_subtitle_style(self): + """应用副标题样式""" + cursor = self.text_edit.textCursor() + + # 创建字符格式 + char_format = QTextCharFormat() + char_format.setFontPointSize(16) # 副标题字号 + char_format.setFontWeight(QFont.Bold) # 加粗 + char_format.setFontItalic(True) # 斜体 + + # 创建块格式(段落格式) + block_format = QTextBlockFormat() + block_format.setTopMargin(12) + block_format.setBottomMargin(8) + + # 应用格式 + if cursor.hasSelection(): + # 如果有选中文本,只更改选中文本的格式 + cursor.mergeCharFormat(char_format) + else: + # 如果没有选中文本,更改当前段落的格式 + cursor.setBlockFormat(block_format) + cursor.mergeCharFormat(char_format) + # 将光标移动到段落末尾并添加换行 + cursor.movePosition(QTextCursor.EndOfBlock) + cursor.insertText("\n") + + # 设置文本编辑器的默认格式 + self.text_edit.setCurrentCharFormat(char_format) + self.text_edit.textCursor().setBlockFormat(block_format) + + def apply_emphasis_style(self): + """应用强调样式""" + cursor = self.text_edit.textCursor() + + # 创建字符格式 + char_format = QTextCharFormat() + char_format.setFontPointSize(12) # 正文字号 + char_format.setFontWeight(QFont.Bold) # 加粗 + char_format.setFontItalic(True) # 斜体 + char_format.setForeground(QColor(0, 0, 128)) # 深蓝色 + + # 应用格式(只影响字符格式,不影响段落格式) + if cursor.hasSelection(): + # 如果有选中文本,只更改选中文本的格式 + cursor.mergeCharFormat(char_format) + else: + # 如果没有选中文本,设置默认字符格式供后续输入使用 + self.text_edit.setCurrentCharFormat(char_format) + + def apply_no_spacing_style(self): + """应用无间隔样式""" + cursor = self.text_edit.textCursor() + + # 创建字符格式 + char_format = QTextCharFormat() + char_format.setFontPointSize(12) # 正文字号 + char_format.setFontWeight(QFont.Normal) # 正常粗细 + + # 创建块格式(段落格式) + block_format = QTextBlockFormat() + block_format.setTopMargin(0) # 无上边距 + block_format.setBottomMargin(0) # 无下边距 + block_format.setLineHeight(100, QTextBlockFormat.SingleHeight) # 行高100%,无额外间距 + + # 应用格式 + if cursor.hasSelection(): + # 如果有选中文本,只更改选中文本的格式 + cursor.mergeCharFormat(char_format) + else: + # 如果没有选中文本,更改当前段落的格式 + cursor.setBlockFormat(block_format) + cursor.mergeCharFormat(char_format) + + # 设置文本编辑器的默认格式 + self.text_edit.setCurrentCharFormat(char_format) + self.text_edit.textCursor().setBlockFormat(block_format) + def apply_alignment(self, alignment): """应用段落对齐方式""" cursor = self.text_edit.textCursor()