From f06a441c2a2cdda8d5f4122040dddbe7c5473416 Mon Sep 17 00:00:00 2001 From: Maziang <929110464@qq.com> Date: Mon, 17 Nov 2025 10:38:50 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=97=A5=E5=8E=86=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/ui/calendar_widget.py | 280 ++++++++++++++++++++++++++++++++++++++ src/word_main_window.py | 65 +++++++++ 2 files changed, 345 insertions(+) create mode 100644 src/ui/calendar_widget.py diff --git a/src/ui/calendar_widget.py b/src/ui/calendar_widget.py new file mode 100644 index 0000000..ed66583 --- /dev/null +++ b/src/ui/calendar_widget.py @@ -0,0 +1,280 @@ +# -*- coding: utf-8 -*- + +""" +日历组件模块 +提供一个可嵌入的日历控件,用于在应用程序中显示和选择日期 +""" + +import sys +from PyQt5.QtWidgets import ( + QWidget, QCalendarWidget, QVBoxLayout, QHBoxLayout, + QPushButton, QLabel, QFrame +) +from PyQt5.QtCore import QDate, Qt, pyqtSignal +from PyQt5.QtGui import QFont + + +class CalendarWidget(QWidget): + """日历组件类""" + + # 自定义信号 + date_selected = pyqtSignal(str) # 日期字符串信号,用于插入功能 + + def __init__(self, parent=None): + super().__init__(parent) + self.setup_ui() + self.setup_connections() + + def setup_ui(self): + """设置UI界面""" + # 设置窗口属性 + self.setWindowTitle("日历") + # 不再固定宽度,让其可以调整大小 + + # 设置白色背景 + self.setStyleSheet("background-color: white;") + + # 主布局 + main_layout = QVBoxLayout() + main_layout.setContentsMargins(10, 10, 10, 10) + main_layout.setSpacing(10) + + # 标题栏 + title_layout = QHBoxLayout() + title_label = QLabel("日历") + title_label.setFont(QFont("Arial", 12, QFont.Bold)) + title_layout.addWidget(title_label) + title_layout.addStretch() + + # 关闭按钮 + self.close_btn = QPushButton("×") + self.close_btn.setFixedSize(25, 25) + self.close_btn.setStyleSheet(""" + QPushButton { + background-color: #f0f0f0; + border: 1px solid #ccc; + border-radius: 12px; + font-size: 16px; + font-weight: bold; + } + QPushButton:hover { + background-color: #e0e0e0; + } + """) + title_layout.addWidget(self.close_btn) + + main_layout.addLayout(title_layout) + + # 分隔线 + separator = QFrame() + separator.setFrameShape(QFrame.HLine) + separator.setFrameShadow(QFrame.Sunken) + main_layout.addWidget(separator) + + # 日历控件 + self.calendar = QCalendarWidget() + self.calendar.setGridVisible(True) + self.calendar.setVerticalHeaderFormat(QCalendarWidget.NoVerticalHeader) + self.calendar.setNavigationBarVisible(True) + + # 设置样式 + self.calendar.setStyleSheet(""" + QCalendarWidget { + background-color: white; + border: 1px solid #ccc; + border-radius: 4px; + } + QCalendarWidget QToolButton { + height: 30px; + width: 80px; + color: #333; + font-size: 12px; + font-weight: bold; + background-color: #f0f0f0; + border: 1px solid #ccc; + border-radius: 4px; + } + QCalendarWidget QToolButton:hover { + background-color: #e0e0e0; + } + QCalendarWidget QMenu { + width: 150px; + left: 20px; + color: white; + font-size: 12px; + background-color: rgb(64, 64, 64); + } + QCalendarWidget QSpinBox { + width: 80px; + font-size: 12px; + background-color: #f0f0f0; + selection-background-color: rgb(64, 64, 64); + selection-color: white; + border: 1px solid #ccc; + border-radius: 4px; + } + QCalendarWidget QSpinBox::up-button { + subcontrol-origin: border; + subcontrol-position: top right; + width: 20px; + } + QCalendarWidget QSpinBox::down-button { + subcontrol-origin: border; + subcontrol-position: bottom right; + width: 20px; + } + QCalendarWidget QSpinBox::up-arrow { + width: 10px; + height: 10px; + } + QCalendarWidget QSpinBox::down-arrow { + width: 10px; + height: 10px; + } + QCalendarWidget QWidget { + alternate-background-color: #f0f0f0; + } + QCalendarWidget QAbstractItemView:enabled { + font-size: 12px; + selection-background-color: #0078d7; + selection-color: white; + } + QCalendarWidget QWidget#qt_calendar_navigationbar { + background-color: #f8f8f8; + } + """) + + main_layout.addWidget(self.calendar) + + # 当前日期显示 + self.date_label = QLabel() + self.date_label.setAlignment(Qt.AlignCenter) + self.date_label.setFont(QFont("Arial", 10)) + self.date_label.setStyleSheet("QLabel { color: #666; }") + self.update_date_label() + + main_layout.addWidget(self.date_label) + + # 操作按钮 + button_layout = QHBoxLayout() + + self.today_btn = QPushButton("今天") + self.today_btn.setStyleSheet(""" + QPushButton { + background-color: #0078d7; + color: white; + border: none; + border-radius: 4px; + padding: 5px 10px; + } + QPushButton:hover { + background-color: #005a9e; + } + """) + + self.clear_btn = QPushButton("清除") + self.clear_btn.setStyleSheet(""" + QPushButton { + background-color: #f0f0f0; + color: #333; + border: 1px solid #ccc; + border-radius: 4px; + padding: 5px 10px; + } + QPushButton:hover { + background-color: #e0e0e0; + } + """) + + # 插入按钮 + self.insert_btn = QPushButton("插入") + self.insert_btn.setStyleSheet(""" + QPushButton { + background-color: #4CAF50; + color: white; + border: none; + border-radius: 4px; + padding: 5px 10px; + } + QPushButton:hover { + background-color: #45a049; + } + """) + + button_layout.addWidget(self.today_btn) + button_layout.addWidget(self.insert_btn) + button_layout.addStretch() + button_layout.addWidget(self.clear_btn) + + main_layout.addLayout(button_layout) + + self.setLayout(main_layout) + + def setup_connections(self): + """设置信号连接""" + self.calendar.clicked.connect(self.on_date_selected) + self.today_btn.clicked.connect(self.on_today_clicked) + self.clear_btn.clicked.connect(self.on_clear_clicked) + self.close_btn.clicked.connect(self.on_close_clicked) + self.insert_btn.clicked.connect(self.on_insert_clicked) + + def on_date_selected(self, date): + """日期选择事件""" + self.update_date_label(date) + + def on_today_clicked(self): + """今天按钮点击事件""" + today = QDate.currentDate() + self.calendar.setSelectedDate(today) + self.update_date_label(today) + + def on_clear_clicked(self): + """清除按钮点击事件""" + self.calendar.setSelectedDate(QDate()) + self.date_label.setText("未选择日期") + + def on_close_clicked(self): + """关闭按钮点击事件""" + self.hide() + + def on_insert_clicked(self): + """插入按钮点击事件""" + selected_date = self.calendar.selectedDate() + if selected_date.isValid(): + # 发送信号,将选中的日期传递给主窗口 + date_str = selected_date.toString("yyyy年MM月dd日 dddd") + self.date_selected.emit(date_str) + + def update_date_label(self, date=None): + """更新日期显示标签""" + if date is None: + date = self.calendar.selectedDate() + + if date.isValid(): + date_str = date.toString("yyyy年MM月dd日 (ddd)") + self.date_label.setText(f"选中日期: {date_str}") + else: + self.date_label.setText("未选择日期") + + def get_selected_date(self): + """获取选中的日期""" + return self.calendar.selectedDate() + + def set_selected_date(self, date): + """设置选中的日期""" + if isinstance(date, str): + date = QDate.fromString(date, "yyyy-MM-dd") + self.calendar.setSelectedDate(date) + self.update_date_label(date) + + +if __name__ == "__main__": + from PyQt5.QtWidgets import QApplication + + app = QApplication(sys.argv) + + # 创建并显示日历组件 + calendar = CalendarWidget() + calendar.show() + + sys.exit(app.exec_()) \ No newline at end of file diff --git a/src/word_main_window.py b/src/word_main_window.py index 0b21977..80508e8 100644 --- a/src/word_main_window.py +++ b/src/word_main_window.py @@ -17,6 +17,7 @@ from typing_logic import TypingLogic from ui.word_style_ui import WeatherAPI from file_parser import FileParser from input_handler.input_processor import InputProcessor +from ui.calendar_widget import CalendarWidget # 导入主题管理器 from ui.theme_manager import theme_manager @@ -102,6 +103,10 @@ class WordStyleMainWindow(QMainWindow): self.network_service = NetworkService() self.weather_api = WeatherAPI() + # 初始化日历组件 + self.calendar_widget = CalendarWidget(self) + self.calendar_widget.hide() # 默认隐藏 + # 设置窗口属性 self.setWindowTitle("文档1 - MagicWord") self.setGeometry(100, 100, 1200, 800) @@ -508,6 +513,14 @@ class WordStyleMainWindow(QMainWindow): background-color: #f3f2f1; } """) + + # 创建日历组件并添加到窗口中(默认隐藏) + try: + from ui.calendar_widget import CalendarWidget + self.calendar_widget = CalendarWidget(self) + self.calendar_widget.hide() # 默认隐藏 + except Exception as e: + print(f"创建日历组件失败: {e}") def create_menu_bar(self): """创建菜单栏""" @@ -684,6 +697,11 @@ class WordStyleMainWindow(QMainWindow): # 绘图菜单 paint_menu = menubar.addMenu('绘图(D)') + # 添加日历按钮 + calendar_action = QAction('日历', self) + calendar_action.triggered.connect(self.toggle_calendar) + paint_menu.addAction(calendar_action) + # 设计菜单 design_menu = menubar.addMenu('设计(G)') @@ -889,6 +907,10 @@ class WordStyleMainWindow(QMainWindow): self.ribbon.city_combo.currentTextChanged.connect(self.on_city_changed) if hasattr(self.ribbon, 'refresh_weather_btn'): self.ribbon.refresh_weather_btn.clicked.connect(self.refresh_weather) + + # 日历组件信号 + if hasattr(self, 'calendar_widget'): + self.calendar_widget.date_selected.connect(self.insert_date_to_editor) def on_text_changed(self): """文本变化处理 - 根据视图模式处理文本变化""" @@ -1382,6 +1404,24 @@ class WordStyleMainWindow(QMainWindow): """两端对齐按钮点击处理""" self.apply_alignment(Qt.AlignJustify) + def insert_date_to_editor(self, date_str): + """将选中的日期插入到编辑器中""" + # 获取当前光标位置 + cursor = self.text_edit.textCursor() + + # 在光标位置插入日期字符串 + cursor.insertText(date_str) + + # 更新文本编辑器的光标 + self.text_edit.setTextCursor(cursor) + + # 隐藏日历组件 + if hasattr(self, 'calendar_widget'): + self.calendar_widget.hide() + + # 更新状态栏提示 + self.status_bar.showMessage(f"已插入日期: {date_str}", 2000) + def apply_heading_style(self, level): """应用标题样式""" cursor = self.text_edit.textCursor() @@ -3449,6 +3489,31 @@ class WordStyleMainWindow(QMainWindow): except Exception as e: print(f"更新格式按钮状态时出错: {e}") + + def resizeEvent(self, event): + """窗口大小改变事件处理""" + super().resizeEvent(event) + + # 如果日历组件可见,调整其大小和位置以适应窗口底部 + if hasattr(self, 'calendar_widget') and self.calendar_widget.isVisible(): + calendar_height = 250 # 减小高度使比例更美观 + self.calendar_widget.setGeometry(0, self.height() - calendar_height, + self.width(), calendar_height) + + def toggle_calendar(self): + """切换日历组件的显示/隐藏状态""" + if hasattr(self, 'calendar_widget'): + if self.calendar_widget.isVisible(): + self.calendar_widget.hide() + else: + # 设置日历组件位置在窗口底部 + calendar_height = 250 # 减小高度使比例更美观 + + # 将日历组件放置在窗口底部,占据整个宽度 + self.calendar_widget.setGeometry(0, self.height() - calendar_height, + self.width(), calendar_height) + self.calendar_widget.show() + self.calendar_widget.raise_() # 确保日历组件在最上层显示 if __name__ == "__main__": app = QApplication(sys.argv) -- 2.34.1