|
|
|
|
@ -674,6 +674,11 @@ class WordStyleMainWindow(QMainWindow):
|
|
|
|
|
# 插入菜单
|
|
|
|
|
insert_menu = menubar.addMenu('插入(I)')
|
|
|
|
|
|
|
|
|
|
# 插入图片功能
|
|
|
|
|
insert_image_action = QAction('插入图片', self)
|
|
|
|
|
insert_image_action.triggered.connect(self.insert_image_in_typing_mode)
|
|
|
|
|
insert_menu.addAction(insert_image_action)
|
|
|
|
|
|
|
|
|
|
# 绘图菜单
|
|
|
|
|
paint_menu = menubar.addMenu('绘图(D)')
|
|
|
|
|
|
|
|
|
|
@ -2729,6 +2734,79 @@ class WordStyleMainWindow(QMainWindow):
|
|
|
|
|
# 这个方法现在不需要了,因为图片会直接插入到文本中
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def insert_image_in_typing_mode(self):
|
|
|
|
|
"""在打字模式下插入图片"""
|
|
|
|
|
try:
|
|
|
|
|
# 检查当前是否在打字模式下
|
|
|
|
|
if self.view_mode != "typing":
|
|
|
|
|
self.status_bar.showMessage("请在打字模式下使用插入图片功能", 3000)
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
# 打开文件对话框选择图片
|
|
|
|
|
from PyQt5.QtWidgets import QFileDialog
|
|
|
|
|
file_path, _ = QFileDialog.getOpenFileName(
|
|
|
|
|
self,
|
|
|
|
|
"选择图片文件",
|
|
|
|
|
"",
|
|
|
|
|
"图片文件 (*.png *.jpg *.jpeg *.bmp *.gif *.ico)"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if not file_path:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
# 加载图片文件
|
|
|
|
|
pixmap = QPixmap(file_path)
|
|
|
|
|
if pixmap.isNull():
|
|
|
|
|
self.status_bar.showMessage("无法加载图片文件", 3000)
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
# 获取当前光标位置
|
|
|
|
|
cursor = self.text_edit.textCursor()
|
|
|
|
|
|
|
|
|
|
# 创建图片格式
|
|
|
|
|
image_format = QTextImageFormat()
|
|
|
|
|
|
|
|
|
|
# 调整图片大小
|
|
|
|
|
scaled_pixmap = pixmap.scaled(200, 150, Qt.KeepAspectRatio, Qt.SmoothTransformation)
|
|
|
|
|
|
|
|
|
|
# 将图片保存到临时文件
|
|
|
|
|
import tempfile
|
|
|
|
|
import os
|
|
|
|
|
temp_dir = tempfile.gettempdir()
|
|
|
|
|
filename = os.path.basename(file_path)
|
|
|
|
|
safe_filename = "".join(c for c in filename if c.isalnum() or c in ('.', '_', '-'))
|
|
|
|
|
temp_file = os.path.join(temp_dir, safe_filename)
|
|
|
|
|
|
|
|
|
|
if scaled_pixmap.save(temp_file):
|
|
|
|
|
# 设置图片格式
|
|
|
|
|
image_format.setName(temp_file)
|
|
|
|
|
image_format.setWidth(200)
|
|
|
|
|
image_format.setHeight(150)
|
|
|
|
|
|
|
|
|
|
# 在光标位置插入图片
|
|
|
|
|
cursor.insertImage(image_format)
|
|
|
|
|
|
|
|
|
|
# 在图片后插入一个空格,让文字继续
|
|
|
|
|
cursor.insertText(" ")
|
|
|
|
|
|
|
|
|
|
# 标记文档为已修改
|
|
|
|
|
if not self.is_modified:
|
|
|
|
|
self.is_modified = True
|
|
|
|
|
self.update_window_title()
|
|
|
|
|
|
|
|
|
|
# 显示成功消息
|
|
|
|
|
self.status_bar.showMessage(f"图片已插入: {filename}", 3000)
|
|
|
|
|
|
|
|
|
|
# 添加到临时文件列表以便清理
|
|
|
|
|
self.temp_files.append(temp_file)
|
|
|
|
|
else:
|
|
|
|
|
self.status_bar.showMessage("保存临时图片文件失败", 3000)
|
|
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
self.status_bar.showMessage(f"插入图片失败: {str(e)}", 3000)
|
|
|
|
|
import traceback
|
|
|
|
|
traceback.print_exc()
|
|
|
|
|
|
|
|
|
|
def closeEvent(self, event):
|
|
|
|
|
"""关闭事件处理"""
|
|
|
|
|
# 清理临时文件
|
|
|
|
|
|