From 31c493d2d1aebbfa1b0512039ef94e031afe5ca2 Mon Sep 17 00:00:00 2001 From: Maziang <929110464@qq.com> Date: Fri, 14 Nov 2025 18:12:41 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8C=89=E9=92=AE=E9=80=BB=E8=BE=91=E4=BF=AE?= =?UTF-8?q?=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/ui/word_style_ui.py | 16 ++++++++ src/word_main_window.py | 82 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+) diff --git a/src/ui/word_style_ui.py b/src/ui/word_style_ui.py index 6a045d1..fa3bdc4 100644 --- a/src/ui/word_style_ui.py +++ b/src/ui/word_style_ui.py @@ -251,6 +251,22 @@ class WordRibbon(QFrame): """正文按钮点击处理""" pass + def on_align_left_clicked(self): + """左对齐按钮点击处理""" + pass + + def on_align_center_clicked(self): + """居中对齐按钮点击处理""" + pass + + def on_align_right_clicked(self): + """右对齐按钮点击处理""" + pass + + def on_align_justify_clicked(self): + """两端对齐按钮点击处理""" + pass + def init_theme(self): """初始化主题""" # 连接主题切换信号 diff --git a/src/word_main_window.py b/src/word_main_window.py index 0633ef2..b3f72e1 100644 --- a/src/word_main_window.py +++ b/src/word_main_window.py @@ -840,6 +840,9 @@ class WordStyleMainWindow(QMainWindow): # 文本变化信号 self.text_edit.textChanged.connect(self.on_text_changed) + # 光标位置变化信号,用于更新按钮状态 + self.text_edit.cursorPositionChanged.connect(self.update_format_buttons) + # Ribbon按钮信号 # 标签栏已删除,相关代码已移除 @@ -872,6 +875,16 @@ class WordStyleMainWindow(QMainWindow): # 页面布局信号已在菜单中直接连接,无需在此重复连接 + # 段落对齐按钮信号 + if hasattr(self.ribbon, 'align_left_btn'): + self.ribbon.align_left_btn.clicked.connect(self.on_align_left_clicked) + if hasattr(self.ribbon, 'align_center_btn'): + self.ribbon.align_center_btn.clicked.connect(self.on_align_center_clicked) + if hasattr(self.ribbon, 'align_right_btn'): + self.ribbon.align_right_btn.clicked.connect(self.on_align_right_clicked) + if hasattr(self.ribbon, 'align_justify_btn'): + self.ribbon.align_justify_btn.clicked.connect(self.on_align_justify_clicked) + # 天气功能信号 if hasattr(self.ribbon, 'city_combo'): self.ribbon.city_combo.currentTextChanged.connect(self.on_city_changed) @@ -1354,6 +1367,22 @@ class WordStyleMainWindow(QMainWindow): """正文按钮点击处理""" self.apply_body_text_style() + def on_align_left_clicked(self): + """左对齐按钮点击处理""" + self.apply_alignment(Qt.AlignLeft) + + def on_align_center_clicked(self): + """居中对齐按钮点击处理""" + self.apply_alignment(Qt.AlignCenter) + + def on_align_right_clicked(self): + """右对齐按钮点击处理""" + self.apply_alignment(Qt.AlignRight) + + def on_align_justify_clicked(self): + """两端对齐按钮点击处理""" + self.apply_alignment(Qt.AlignJustify) + def apply_heading_style(self, level): """应用标题样式""" cursor = self.text_edit.textCursor() @@ -1427,6 +1456,25 @@ class WordStyleMainWindow(QMainWindow): self.text_edit.setCurrentCharFormat(char_format) self.text_edit.textCursor().setBlockFormat(block_format) + def apply_alignment(self, alignment): + """应用段落对齐方式""" + cursor = self.text_edit.textCursor() + + # 创建块格式(段落格式) + block_format = QTextBlockFormat() + block_format.setAlignment(alignment) + + # 应用格式 + if cursor.hasSelection(): + # 如果有选中文本,更改选中文本所在段落的对齐方式 + cursor.mergeBlockFormat(block_format) + else: + # 如果没有选中文本,更改当前段落的对齐方式 + cursor.setBlockFormat(block_format) + + # 更新文本编辑器的默认段落格式 + self.text_edit.textCursor().setBlockFormat(block_format) + def update_weather_display(self, weather_data): """更新天气显示""" if 'error' in weather_data: @@ -3368,6 +3416,40 @@ class WordStyleMainWindow(QMainWindow): except Exception as e: self.status_bar.showMessage(f"提取图片失败: {str(e)}", 3000) + + def update_format_buttons(self): + """更新格式按钮的状态,根据当前光标位置的格式""" + try: + # 获取当前光标位置的字符格式 + cursor = self.text_edit.textCursor() + char_format = cursor.charFormat() + block_format = cursor.blockFormat() + + # 更新粗体按钮状态 + if hasattr(self, 'ribbon') and hasattr(self.ribbon, 'bold_btn'): + is_bold = char_format.font().weight() == QFont.Bold + self.ribbon.bold_btn.setChecked(is_bold) + + # 更新斜体按钮状态 + if hasattr(self, 'ribbon') and hasattr(self.ribbon, 'italic_btn'): + is_italic = char_format.font().italic() + self.ribbon.italic_btn.setChecked(is_italic) + + # 更新下划线按钮状态 + if hasattr(self, 'ribbon') and hasattr(self.ribbon, 'underline_btn'): + is_underline = char_format.font().underline() + self.ribbon.underline_btn.setChecked(is_underline) + + # 更新对齐按钮状态 + if hasattr(self, 'ribbon') and hasattr(self.ribbon, 'align_left_btn'): + alignment = block_format.alignment() + self.ribbon.align_left_btn.setChecked(alignment == Qt.AlignLeft) + self.ribbon.align_center_btn.setChecked(alignment == Qt.AlignCenter) + self.ribbon.align_right_btn.setChecked(alignment == Qt.AlignRight) + self.ribbon.align_justify_btn.setChecked(alignment == Qt.AlignJustify) + + except Exception as e: + print(f"更新格式按钮状态时出错: {e}") if __name__ == "__main__": app = QApplication(sys.argv)