mirror of
https://github.com/HChaZZY/Any2MIF.git
synced 2025-12-06 10:33:49 +08:00
init
This commit is contained in:
8
ui/__init__.py
Normal file
8
ui/__init__.py
Normal file
@@ -0,0 +1,8 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
# Copyright (c) 2025 Any2MIF Project
|
||||
# All rights reserved.
|
||||
|
||||
"""
|
||||
Any2MIF - UI包
|
||||
"""
|
||||
254
ui/file_selector.py
Normal file
254
ui/file_selector.py
Normal file
@@ -0,0 +1,254 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
# Copyright (c) 2025 Any2MIF Project
|
||||
# All rights reserved.
|
||||
|
||||
"""
|
||||
Any2MIF - 文件选择模块
|
||||
负责文件的选择和管理
|
||||
"""
|
||||
|
||||
import os
|
||||
from PyQt6.QtWidgets import (
|
||||
QWidget, QVBoxLayout, QHBoxLayout, QPushButton,
|
||||
QListWidget, QListWidgetItem, QFileDialog, QLabel,
|
||||
QMenu, QAbstractItemView
|
||||
)
|
||||
from PyQt6.QtCore import Qt, QSettings, pyqtSignal
|
||||
from PyQt6.QtGui import QIcon, QDragEnterEvent, QDropEvent
|
||||
|
||||
class FileSelector(QWidget):
|
||||
"""文件选择器组件"""
|
||||
|
||||
# 自定义信号
|
||||
file_selected = pyqtSignal(str) # 文件被选择时发出的信号
|
||||
convert_clicked = pyqtSignal() # 转换按钮被点击时发出的信号
|
||||
batch_convert_clicked = pyqtSignal() # 批量转换按钮被点击时发出的信号
|
||||
|
||||
def __init__(self, settings: QSettings, parent=None):
|
||||
"""初始化文件选择器"""
|
||||
super().__init__(parent)
|
||||
self.settings = settings
|
||||
self.recent_files = self.settings.value("recent_files", [])
|
||||
if not isinstance(self.recent_files, list):
|
||||
self.recent_files = []
|
||||
|
||||
self._init_ui()
|
||||
self._load_recent_files()
|
||||
|
||||
def _init_ui(self):
|
||||
"""初始化UI"""
|
||||
# 设置接受拖放
|
||||
self.setAcceptDrops(True)
|
||||
|
||||
# 创建主布局
|
||||
layout = QVBoxLayout(self)
|
||||
layout.setContentsMargins(0, 0, 0, 0)
|
||||
|
||||
# 创建标题标签
|
||||
title_label = QLabel("文件选择")
|
||||
title_label.setStyleSheet("font-weight: bold; font-size: 14px;")
|
||||
layout.addWidget(title_label)
|
||||
|
||||
# 创建文件列表
|
||||
self.file_list = QListWidget()
|
||||
self.file_list.setSelectionMode(QAbstractItemView.SelectionMode.ExtendedSelection)
|
||||
self.file_list.itemDoubleClicked.connect(self._on_item_double_clicked)
|
||||
self.file_list.setContextMenuPolicy(Qt.ContextMenuPolicy.CustomContextMenu)
|
||||
self.file_list.customContextMenuRequested.connect(self._show_context_menu)
|
||||
layout.addWidget(self.file_list)
|
||||
|
||||
# 创建按钮容器
|
||||
button_container = QWidget()
|
||||
button_container_layout = QVBoxLayout(button_container)
|
||||
button_container_layout.setContentsMargins(0, 0, 0, 0)
|
||||
button_container_layout.setSpacing(5)
|
||||
layout.addWidget(button_container)
|
||||
|
||||
# 创建上方按钮布局
|
||||
top_button_layout = QHBoxLayout()
|
||||
top_button_layout.setContentsMargins(0, 0, 0, 0)
|
||||
button_container_layout.addLayout(top_button_layout)
|
||||
|
||||
# 添加文件按钮
|
||||
self.add_file_button = QPushButton("添加文件")
|
||||
self.add_file_button.clicked.connect(self._add_files)
|
||||
top_button_layout.addWidget(self.add_file_button)
|
||||
|
||||
# 添加文件夹按钮
|
||||
self.add_folder_button = QPushButton("添加文件夹")
|
||||
self.add_folder_button.clicked.connect(self._add_folder)
|
||||
top_button_layout.addWidget(self.add_folder_button)
|
||||
|
||||
# 清除按钮
|
||||
self.clear_button = QPushButton("清除")
|
||||
self.clear_button.clicked.connect(self._clear_files)
|
||||
top_button_layout.addWidget(self.clear_button)
|
||||
|
||||
# 创建下方按钮布局(转换和批量转换按钮)
|
||||
bottom_button_layout = QHBoxLayout()
|
||||
bottom_button_layout.setContentsMargins(0, 0, 0, 0)
|
||||
button_container_layout.addLayout(bottom_button_layout)
|
||||
|
||||
# 转换按钮
|
||||
self.convert_button = QPushButton("转换")
|
||||
self.convert_button.clicked.connect(self._on_convert_clicked)
|
||||
bottom_button_layout.addWidget(self.convert_button)
|
||||
|
||||
# 批量转换按钮
|
||||
self.batch_convert_button = QPushButton("批量转换")
|
||||
self.batch_convert_button.clicked.connect(self._on_batch_convert_clicked)
|
||||
bottom_button_layout.addWidget(self.batch_convert_button)
|
||||
|
||||
# 确保两行按钮的总宽度相同
|
||||
# 为上面三个按钮设置相同的最小宽度
|
||||
min_width = 80
|
||||
self.add_file_button.setMinimumWidth(min_width)
|
||||
self.add_folder_button.setMinimumWidth(min_width)
|
||||
self.clear_button.setMinimumWidth(min_width)
|
||||
|
||||
# 为下面两个按钮设置宽度,使它们的总宽度与上面三个按钮相同
|
||||
convert_width = (min_width * 3) // 2
|
||||
self.convert_button.setMinimumWidth(convert_width)
|
||||
self.batch_convert_button.setMinimumWidth(convert_width)
|
||||
|
||||
def _load_recent_files(self):
|
||||
"""加载最近使用的文件"""
|
||||
for file_path in self.recent_files:
|
||||
if os.path.exists(file_path):
|
||||
self._add_file_to_list(file_path)
|
||||
|
||||
def _add_file_to_list(self, file_path):
|
||||
"""将文件添加到列表中"""
|
||||
# 检查文件是否已经在列表中
|
||||
for i in range(self.file_list.count()):
|
||||
if self.file_list.item(i).data(Qt.ItemDataRole.UserRole) == file_path:
|
||||
return
|
||||
|
||||
# 创建列表项
|
||||
item = QListWidgetItem(os.path.basename(file_path))
|
||||
item.setData(Qt.ItemDataRole.UserRole, file_path)
|
||||
item.setToolTip(file_path)
|
||||
|
||||
# 添加到列表
|
||||
self.file_list.addItem(item)
|
||||
|
||||
# 更新最近文件列表
|
||||
if file_path in self.recent_files:
|
||||
self.recent_files.remove(file_path)
|
||||
self.recent_files.insert(0, file_path)
|
||||
# 限制最近文件数量
|
||||
self.recent_files = self.recent_files[:10]
|
||||
self.settings.setValue("recent_files", self.recent_files)
|
||||
|
||||
def _add_files(self):
|
||||
"""添加文件"""
|
||||
files, _ = QFileDialog.getOpenFileNames(
|
||||
self,
|
||||
"选择文件",
|
||||
os.path.expanduser("~"),
|
||||
"所有文件 (*.*)"
|
||||
)
|
||||
|
||||
if files:
|
||||
for file_path in files:
|
||||
self._add_file_to_list(file_path)
|
||||
|
||||
# 发出信号
|
||||
if len(files) == 1:
|
||||
self.file_selected.emit(files[0])
|
||||
|
||||
def _add_folder(self):
|
||||
"""添加文件夹中的所有文件"""
|
||||
folder = QFileDialog.getExistingDirectory(
|
||||
self,
|
||||
"选择文件夹",
|
||||
os.path.expanduser("~")
|
||||
)
|
||||
|
||||
if folder:
|
||||
files_added = 0
|
||||
for root, _, files in os.walk(folder):
|
||||
for file in files:
|
||||
file_path = os.path.join(root, file)
|
||||
self._add_file_to_list(file_path)
|
||||
files_added += 1
|
||||
|
||||
if files_added == 1:
|
||||
self.file_selected.emit(self.file_list.item(self.file_list.count() - 1).data(Qt.ItemDataRole.UserRole))
|
||||
|
||||
def _clear_files(self):
|
||||
"""清除文件列表"""
|
||||
self.file_list.clear()
|
||||
# 清除最近文件列表
|
||||
self.recent_files = []
|
||||
# 更新设置
|
||||
self.settings.setValue("recent_files", self.recent_files)
|
||||
|
||||
def _on_item_double_clicked(self, item):
|
||||
"""处理项目双击事件"""
|
||||
file_path = item.data(Qt.ItemDataRole.UserRole)
|
||||
self.file_selected.emit(file_path)
|
||||
|
||||
def _show_context_menu(self, position):
|
||||
"""显示上下文菜单"""
|
||||
menu = QMenu()
|
||||
|
||||
# 添加菜单项
|
||||
remove_action = menu.addAction("移除")
|
||||
remove_action.triggered.connect(self._remove_selected_files)
|
||||
|
||||
# 显示菜单
|
||||
menu.exec(self.file_list.mapToGlobal(position))
|
||||
|
||||
def _remove_selected_files(self):
|
||||
"""移除选定的文件"""
|
||||
for item in self.file_list.selectedItems():
|
||||
self.file_list.takeItem(self.file_list.row(item))
|
||||
|
||||
def get_selected_files(self):
|
||||
"""获取选定的文件列表"""
|
||||
files = []
|
||||
|
||||
# 如果有选定的项目,返回选定的文件
|
||||
if self.file_list.selectedItems():
|
||||
for item in self.file_list.selectedItems():
|
||||
files.append(item.data(Qt.ItemDataRole.UserRole))
|
||||
# 否则返回所有文件
|
||||
else:
|
||||
for i in range(self.file_list.count()):
|
||||
files.append(self.file_list.item(i).data(Qt.ItemDataRole.UserRole))
|
||||
|
||||
return files
|
||||
|
||||
def dragEnterEvent(self, event: QDragEnterEvent):
|
||||
"""处理拖动进入事件"""
|
||||
if event.mimeData().hasUrls():
|
||||
event.acceptProposedAction()
|
||||
|
||||
def dropEvent(self, event: QDropEvent):
|
||||
"""处理放置事件"""
|
||||
urls = event.mimeData().urls()
|
||||
|
||||
for url in urls:
|
||||
path = url.toLocalFile()
|
||||
|
||||
if os.path.isfile(path):
|
||||
self._add_file_to_list(path)
|
||||
elif os.path.isdir(path):
|
||||
for root, _, files in os.walk(path):
|
||||
for file in files:
|
||||
file_path = os.path.join(root, file)
|
||||
self._add_file_to_list(file_path)
|
||||
|
||||
# 如果只添加了一个文件,发出信号
|
||||
if len(urls) == 1 and os.path.isfile(urls[0].toLocalFile()):
|
||||
self.file_selected.emit(urls[0].toLocalFile())
|
||||
|
||||
def _on_convert_clicked(self):
|
||||
"""处理转换按钮点击事件"""
|
||||
self.convert_clicked.emit()
|
||||
|
||||
def _on_batch_convert_clicked(self):
|
||||
"""处理批量转换按钮点击事件"""
|
||||
self.batch_convert_clicked.emit()
|
||||
432
ui/image_toolbar.py
Normal file
432
ui/image_toolbar.py
Normal file
@@ -0,0 +1,432 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
# Copyright (c) 2025 Any2MIF Project
|
||||
# All rights reserved.
|
||||
|
||||
"""
|
||||
Any2MIF - 图像处理工具栏
|
||||
负责图像的灰度化、二值化等操作
|
||||
"""
|
||||
|
||||
import os
|
||||
from PyQt6.QtWidgets import (
|
||||
QWidget, QVBoxLayout, QHBoxLayout, QLabel,
|
||||
QPushButton, QSlider, QGroupBox, QToolButton,
|
||||
QSpinBox, QComboBox, QCheckBox, QFileDialog
|
||||
)
|
||||
from PyQt6.QtCore import Qt, QSettings, pyqtSignal, QTimer
|
||||
from PyQt6.QtGui import QPixmap, QImage
|
||||
from PIL import Image, ImageQt
|
||||
|
||||
from core.image_processor import ImageProcessor
|
||||
|
||||
class ImageToolbar(QWidget):
|
||||
"""图像处理工具栏"""
|
||||
|
||||
def __init__(self, image_processor: ImageProcessor, settings: QSettings, preview_label=None, parent=None):
|
||||
"""初始化图像处理工具栏"""
|
||||
super().__init__(parent)
|
||||
self.image_processor = image_processor
|
||||
self.settings = settings
|
||||
self.preview_label = preview_label # 外部传入的预览标签
|
||||
self.original_image = None
|
||||
self.processed_image = None
|
||||
self._last_size = None # 初始化上一次窗口大小
|
||||
self._fixed_preview_height = None # 固定的预览高度
|
||||
|
||||
self._init_ui()
|
||||
self._load_settings()
|
||||
self._connect_signals()
|
||||
|
||||
# 默认禁用工具栏
|
||||
self.setEnabled(False)
|
||||
|
||||
def _init_ui(self):
|
||||
"""初始化UI"""
|
||||
# 创建主布局
|
||||
layout = QVBoxLayout(self)
|
||||
layout.setContentsMargins(0, 0, 0, 0)
|
||||
|
||||
# 创建标题标签
|
||||
title_label = QLabel("图像处理")
|
||||
title_label.setStyleSheet("font-weight: bold; font-size: 14px;")
|
||||
layout.addWidget(title_label)
|
||||
|
||||
# 创建2x2网格布局来放置四个功能区
|
||||
grid_layout = QHBoxLayout()
|
||||
layout.addLayout(grid_layout)
|
||||
|
||||
# 创建左侧和右侧的垂直布局
|
||||
left_column = QVBoxLayout()
|
||||
right_column = QVBoxLayout()
|
||||
grid_layout.addLayout(left_column)
|
||||
grid_layout.addLayout(right_column)
|
||||
|
||||
# 创建灰度化组 (左上)
|
||||
grayscale_group = QGroupBox("灰度化")
|
||||
grayscale_layout = QVBoxLayout(grayscale_group)
|
||||
left_column.addWidget(grayscale_group)
|
||||
|
||||
# 灰度化方法选择
|
||||
self.grayscale_method_combo = QComboBox()
|
||||
self.grayscale_method_combo.addItems(["平均值法", "加权平均法", "最大值法"])
|
||||
grayscale_layout.addWidget(self.grayscale_method_combo)
|
||||
|
||||
# 灰度化按钮
|
||||
self.grayscale_button = QPushButton("应用灰度化")
|
||||
grayscale_layout.addWidget(self.grayscale_button)
|
||||
|
||||
# 创建二值化组 (右上)
|
||||
threshold_group = QGroupBox("二值化")
|
||||
threshold_layout = QVBoxLayout(threshold_group)
|
||||
right_column.addWidget(threshold_group)
|
||||
|
||||
# 阈值滑块
|
||||
threshold_layout_h = QHBoxLayout()
|
||||
threshold_layout.addLayout(threshold_layout_h)
|
||||
|
||||
threshold_layout_h.addWidget(QLabel("阈值:"))
|
||||
self.threshold_slider = QSlider(Qt.Orientation.Horizontal)
|
||||
self.threshold_slider.setRange(0, 255)
|
||||
self.threshold_slider.setValue(128)
|
||||
threshold_layout_h.addWidget(self.threshold_slider)
|
||||
|
||||
self.threshold_value_label = QLabel("128")
|
||||
threshold_layout_h.addWidget(self.threshold_value_label)
|
||||
|
||||
# 二值化方法选择
|
||||
self.threshold_method_combo = QComboBox()
|
||||
self.threshold_method_combo.addItems(["固定阈值", "自适应阈值", "Otsu阈值"])
|
||||
threshold_layout.addWidget(self.threshold_method_combo)
|
||||
|
||||
# 二值化按钮
|
||||
self.threshold_button = QPushButton("应用二值化")
|
||||
threshold_layout.addWidget(self.threshold_button)
|
||||
|
||||
# 创建降噪组 (左下)
|
||||
denoise_group = QGroupBox("降噪处理")
|
||||
denoise_layout = QVBoxLayout(denoise_group)
|
||||
left_column.addWidget(denoise_group)
|
||||
|
||||
# 降噪方法选择
|
||||
self.denoise_method_combo = QComboBox()
|
||||
self.denoise_method_combo.addItems(["中值滤波", "高斯滤波", "双边滤波"])
|
||||
denoise_layout.addWidget(self.denoise_method_combo)
|
||||
|
||||
# 降噪强度
|
||||
denoise_strength_layout = QHBoxLayout()
|
||||
denoise_layout.addLayout(denoise_strength_layout)
|
||||
|
||||
denoise_strength_layout.addWidget(QLabel("强度:"))
|
||||
self.denoise_strength_slider = QSlider(Qt.Orientation.Horizontal)
|
||||
self.denoise_strength_slider.setRange(1, 10)
|
||||
self.denoise_strength_slider.setValue(3)
|
||||
denoise_strength_layout.addWidget(self.denoise_strength_slider)
|
||||
|
||||
self.denoise_strength_label = QLabel("3")
|
||||
denoise_strength_layout.addWidget(self.denoise_strength_label)
|
||||
|
||||
# 降噪按钮
|
||||
self.denoise_button = QPushButton("应用降噪")
|
||||
denoise_layout.addWidget(self.denoise_button)
|
||||
|
||||
# 创建尺寸标准化组 (右下)
|
||||
resize_group = QGroupBox("尺寸标准化")
|
||||
resize_layout = QVBoxLayout(resize_group)
|
||||
right_column.addWidget(resize_group)
|
||||
|
||||
# 尺寸设置
|
||||
size_layout = QHBoxLayout()
|
||||
resize_layout.addLayout(size_layout)
|
||||
|
||||
size_layout.addWidget(QLabel("宽度:"))
|
||||
self.width_spin = QSpinBox()
|
||||
self.width_spin.setRange(1, 1000)
|
||||
self.width_spin.setValue(128)
|
||||
size_layout.addWidget(self.width_spin)
|
||||
|
||||
size_layout.addWidget(QLabel("高度:"))
|
||||
self.height_spin = QSpinBox()
|
||||
self.height_spin.setRange(1, 1000)
|
||||
self.height_spin.setValue(128)
|
||||
size_layout.addWidget(self.height_spin)
|
||||
|
||||
# 保持纵横比
|
||||
self.keep_aspect_check = QCheckBox("保持纵横比")
|
||||
self.keep_aspect_check.setChecked(True)
|
||||
resize_layout.addWidget(self.keep_aspect_check)
|
||||
|
||||
# 调整尺寸按钮
|
||||
self.resize_button = QPushButton("应用尺寸调整")
|
||||
resize_layout.addWidget(self.resize_button)
|
||||
|
||||
# 创建操作按钮布局
|
||||
button_layout = QHBoxLayout()
|
||||
layout.addLayout(button_layout)
|
||||
|
||||
# 重置按钮
|
||||
self.reset_button = QPushButton("重置图像")
|
||||
button_layout.addWidget(self.reset_button)
|
||||
|
||||
# 保存按钮
|
||||
self.save_button = QPushButton("保存图像")
|
||||
button_layout.addWidget(self.save_button)
|
||||
|
||||
def _load_settings(self):
|
||||
"""加载设置"""
|
||||
# 加载灰度化方法
|
||||
grayscale_method_index = self.settings.value("image/grayscale_method_index", 0, int)
|
||||
self.grayscale_method_combo.setCurrentIndex(grayscale_method_index)
|
||||
|
||||
# 加载二值化设置
|
||||
threshold = self.settings.value("image/threshold", 128, int)
|
||||
self.threshold_slider.setValue(threshold)
|
||||
self.threshold_value_label.setText(str(threshold))
|
||||
|
||||
threshold_method_index = self.settings.value("image/threshold_method_index", 0, int)
|
||||
self.threshold_method_combo.setCurrentIndex(threshold_method_index)
|
||||
|
||||
# 加载降噪设置
|
||||
denoise_method_index = self.settings.value("image/denoise_method_index", 0, int)
|
||||
self.denoise_method_combo.setCurrentIndex(denoise_method_index)
|
||||
|
||||
denoise_strength = self.settings.value("image/denoise_strength", 3, int)
|
||||
self.denoise_strength_slider.setValue(denoise_strength)
|
||||
self.denoise_strength_label.setText(str(denoise_strength))
|
||||
|
||||
# 加载尺寸设置
|
||||
width = self.settings.value("image/width", 128, int)
|
||||
self.width_spin.setValue(width)
|
||||
|
||||
height = self.settings.value("image/height", 128, int)
|
||||
self.height_spin.setValue(height)
|
||||
|
||||
keep_aspect = self.settings.value("image/keep_aspect", True, bool)
|
||||
self.keep_aspect_check.setChecked(keep_aspect)
|
||||
|
||||
def _connect_signals(self):
|
||||
"""连接信号和槽"""
|
||||
# 灰度化
|
||||
self.grayscale_button.clicked.connect(self._apply_grayscale)
|
||||
self.grayscale_method_combo.currentIndexChanged.connect(self._save_settings)
|
||||
|
||||
# 二值化
|
||||
self.threshold_slider.valueChanged.connect(self._on_threshold_changed)
|
||||
self.threshold_button.clicked.connect(self._apply_threshold)
|
||||
self.threshold_method_combo.currentIndexChanged.connect(self._save_settings)
|
||||
|
||||
# 降噪
|
||||
self.denoise_strength_slider.valueChanged.connect(self._on_denoise_strength_changed)
|
||||
self.denoise_button.clicked.connect(self._apply_denoise)
|
||||
self.denoise_method_combo.currentIndexChanged.connect(self._save_settings)
|
||||
|
||||
# 尺寸调整
|
||||
self.resize_button.clicked.connect(self._apply_resize)
|
||||
self.width_spin.valueChanged.connect(self._save_settings)
|
||||
self.height_spin.valueChanged.connect(self._save_settings)
|
||||
self.keep_aspect_check.toggled.connect(self._save_settings)
|
||||
|
||||
# 操作按钮
|
||||
self.reset_button.clicked.connect(self._reset_image)
|
||||
self.save_button.clicked.connect(self._save_image)
|
||||
|
||||
def _on_threshold_changed(self, value):
|
||||
"""处理阈值变化事件"""
|
||||
self.threshold_value_label.setText(str(value))
|
||||
self._save_settings()
|
||||
|
||||
def _on_denoise_strength_changed(self, value):
|
||||
"""处理降噪强度变化事件"""
|
||||
self.denoise_strength_label.setText(str(value))
|
||||
self._save_settings()
|
||||
|
||||
def _save_settings(self):
|
||||
"""保存设置"""
|
||||
# 保存灰度化设置
|
||||
self.settings.setValue("image/grayscale_method_index", self.grayscale_method_combo.currentIndex())
|
||||
|
||||
# 保存二值化设置
|
||||
self.settings.setValue("image/threshold", self.threshold_slider.value())
|
||||
self.settings.setValue("image/threshold_method_index", self.threshold_method_combo.currentIndex())
|
||||
|
||||
# 保存降噪设置
|
||||
self.settings.setValue("image/denoise_method_index", self.denoise_method_combo.currentIndex())
|
||||
self.settings.setValue("image/denoise_strength", self.denoise_strength_slider.value())
|
||||
|
||||
# 保存尺寸设置
|
||||
self.settings.setValue("image/width", self.width_spin.value())
|
||||
self.settings.setValue("image/height", self.height_spin.value())
|
||||
self.settings.setValue("image/keep_aspect", self.keep_aspect_check.isChecked())
|
||||
|
||||
def load_image(self, image_path):
|
||||
"""加载图像"""
|
||||
if not os.path.exists(image_path):
|
||||
return
|
||||
|
||||
# 加载原始图像
|
||||
self.original_image = Image.open(image_path)
|
||||
self.processed_image = self.original_image.copy()
|
||||
|
||||
# 如果是首次加载图像,设置固定的预览高度
|
||||
if self._fixed_preview_height is None and self.preview_label is not None:
|
||||
self._fixed_preview_height = self.preview_label.height()
|
||||
# 设置预览标签的固定高度
|
||||
self.preview_label.setFixedHeight(self._fixed_preview_height)
|
||||
|
||||
# 更新预览
|
||||
self._update_preview()
|
||||
|
||||
def _update_preview(self):
|
||||
"""更新预览"""
|
||||
if self.processed_image is None or self.preview_label is None:
|
||||
return
|
||||
|
||||
# 调整图像大小以适应预览区域
|
||||
preview_width = self.preview_label.width()
|
||||
|
||||
# 使用固定的预览高度,如果没有设置则使用当前高度
|
||||
preview_height = self._fixed_preview_height if self._fixed_preview_height is not None else self.preview_label.height()
|
||||
|
||||
# 确保预览区域有有效尺寸
|
||||
if preview_width <= 10 or preview_height <= 10:
|
||||
return
|
||||
|
||||
# 计算缩放比例 - 使用宽度比例,但保持图片纵横比
|
||||
width_ratio = preview_width / self.processed_image.width
|
||||
height_ratio = preview_height / self.processed_image.height
|
||||
|
||||
# 使用较小的比例以确保图片完全适应预览区域,同时保持纵横比
|
||||
ratio = min(width_ratio, height_ratio)
|
||||
|
||||
# 计算新尺寸 - 保持纵横比
|
||||
new_width = max(int(self.processed_image.width * ratio), 1)
|
||||
new_height = max(int(self.processed_image.height * ratio), 1)
|
||||
|
||||
# 调整图像大小
|
||||
preview_image = self.processed_image.resize((new_width, new_height), Image.Resampling.LANCZOS)
|
||||
|
||||
# 转换为QPixmap并显示
|
||||
q_image = ImageQt.ImageQt(preview_image)
|
||||
pixmap = QPixmap.fromImage(q_image)
|
||||
self.preview_label.setPixmap(pixmap)
|
||||
|
||||
# 确保预览标签的大小策略正确
|
||||
self.preview_label.setScaledContents(False) # 不自动缩放内容
|
||||
|
||||
def _apply_grayscale(self):
|
||||
"""应用灰度化"""
|
||||
if self.processed_image is None:
|
||||
return
|
||||
|
||||
# 获取灰度化方法
|
||||
method_index = self.grayscale_method_combo.currentIndex()
|
||||
method_map = {0: "average", 1: "weighted", 2: "max"}
|
||||
method = method_map[method_index]
|
||||
|
||||
# 应用灰度化
|
||||
self.processed_image = self.image_processor.grayscale(self.processed_image, method)
|
||||
|
||||
# 更新预览
|
||||
self._update_preview()
|
||||
|
||||
def _apply_threshold(self):
|
||||
"""应用二值化"""
|
||||
if self.processed_image is None:
|
||||
return
|
||||
|
||||
# 获取二值化参数
|
||||
threshold = self.threshold_slider.value()
|
||||
method_index = self.threshold_method_combo.currentIndex()
|
||||
method_map = {0: "fixed", 1: "adaptive", 2: "otsu"}
|
||||
method = method_map[method_index]
|
||||
|
||||
# 应用二值化
|
||||
self.processed_image = self.image_processor.threshold(self.processed_image, threshold, method)
|
||||
|
||||
# 更新预览
|
||||
self._update_preview()
|
||||
|
||||
def _apply_denoise(self):
|
||||
"""应用降噪"""
|
||||
if self.processed_image is None:
|
||||
return
|
||||
|
||||
# 获取降噪参数
|
||||
method_index = self.denoise_method_combo.currentIndex()
|
||||
method_map = {0: "median", 1: "gaussian", 2: "bilateral"}
|
||||
method = method_map[method_index]
|
||||
|
||||
strength = self.denoise_strength_slider.value()
|
||||
|
||||
# 应用降噪
|
||||
self.processed_image = self.image_processor.denoise(self.processed_image, method, strength)
|
||||
|
||||
# 更新预览
|
||||
self._update_preview()
|
||||
|
||||
def _apply_resize(self):
|
||||
"""应用尺寸调整"""
|
||||
if self.processed_image is None:
|
||||
return
|
||||
|
||||
# 获取尺寸参数
|
||||
width = self.width_spin.value()
|
||||
height = self.height_spin.value()
|
||||
keep_aspect = self.keep_aspect_check.isChecked()
|
||||
|
||||
# 应用尺寸调整
|
||||
self.processed_image = self.image_processor.resize(self.processed_image, width, height, keep_aspect)
|
||||
|
||||
# 更新预览
|
||||
self._update_preview()
|
||||
|
||||
def _reset_image(self):
|
||||
"""重置图像"""
|
||||
if self.original_image is None:
|
||||
return
|
||||
|
||||
# 重置为原始图像
|
||||
self.processed_image = self.original_image.copy()
|
||||
|
||||
# 更新预览
|
||||
self._update_preview()
|
||||
|
||||
def _save_image(self):
|
||||
"""保存图像"""
|
||||
if self.processed_image is None:
|
||||
return
|
||||
|
||||
# 获取保存路径
|
||||
file_path, _ = QFileDialog.getSaveFileName(
|
||||
self,
|
||||
"保存图像",
|
||||
os.path.expanduser("~"),
|
||||
"图像文件 (*.png *.jpg *.bmp)"
|
||||
)
|
||||
|
||||
if file_path:
|
||||
# 保存图像
|
||||
self.processed_image.save(file_path)
|
||||
|
||||
def get_processed_image(self):
|
||||
"""获取处理后的图像"""
|
||||
return self.processed_image
|
||||
|
||||
|
||||
def resizeEvent(self, event):
|
||||
"""处理大小调整事件"""
|
||||
# 获取当前窗口大小
|
||||
current_size = event.size()
|
||||
|
||||
# 调用父类的resizeEvent
|
||||
super().resizeEvent(event)
|
||||
|
||||
# 如果已经设置了固定预览高度,确保预览标签保持该高度
|
||||
if self.preview_label is not None and self._fixed_preview_height is not None:
|
||||
self.preview_label.setFixedHeight(self._fixed_preview_height)
|
||||
|
||||
# 更新上一次窗口大小
|
||||
self._last_size = current_size
|
||||
|
||||
# 使用QTimer延迟更新预览,确保布局已完全调整
|
||||
QTimer.singleShot(0, self._update_preview)
|
||||
256
ui/main_window.py
Normal file
256
ui/main_window.py
Normal file
@@ -0,0 +1,256 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
# Copyright (c) 2025 Any2MIF Project
|
||||
# All rights reserved.
|
||||
|
||||
"""
|
||||
Any2MIF - 主窗口类
|
||||
包含应用程序的主界面和核心功能组织
|
||||
"""
|
||||
|
||||
import os
|
||||
import sys
|
||||
from PyQt6.QtWidgets import (
|
||||
QMainWindow, QVBoxLayout, QHBoxLayout, QWidget,
|
||||
QSplitter, QStatusBar, QToolBar, QMessageBox,
|
||||
QFileDialog, QGroupBox, QLabel
|
||||
)
|
||||
from PyQt6.QtCore import Qt, QSettings, QSize, QTimer
|
||||
from PyQt6.QtGui import QIcon, QAction
|
||||
|
||||
from ui.file_selector import FileSelector
|
||||
from ui.param_panel import ParamPanel
|
||||
from ui.image_toolbar import ImageToolbar
|
||||
from ui.theme_controller import ThemeController
|
||||
from core.converter import MifConverter
|
||||
from core.batch_manager import BatchManager
|
||||
from core.image_processor import ImageProcessor
|
||||
|
||||
class MainWindow(QMainWindow):
|
||||
"""主窗口类"""
|
||||
|
||||
def __init__(self, settings: QSettings, parent=None):
|
||||
"""初始化主窗口"""
|
||||
super().__init__(parent)
|
||||
self.settings = settings
|
||||
self.converter = MifConverter()
|
||||
self.batch_manager = BatchManager(self.converter)
|
||||
self.image_processor = ImageProcessor()
|
||||
|
||||
self._init_ui()
|
||||
self._load_settings()
|
||||
self._connect_signals()
|
||||
|
||||
def _init_ui(self):
|
||||
"""初始化UI"""
|
||||
# 设置窗口属性
|
||||
self.setWindowTitle("Any2MIF - 文件转换器")
|
||||
self.setMinimumSize(900, 600)
|
||||
|
||||
# 创建中央部件
|
||||
central_widget = QWidget()
|
||||
self.setCentralWidget(central_widget)
|
||||
|
||||
# 创建主布局
|
||||
main_layout = QVBoxLayout(central_widget)
|
||||
main_layout.setContentsMargins(10, 10, 10, 10)
|
||||
main_layout.setSpacing(10)
|
||||
|
||||
# 创建左侧面板
|
||||
left_panel = QWidget()
|
||||
left_layout = QVBoxLayout(left_panel)
|
||||
left_layout.setContentsMargins(0, 0, 0, 0)
|
||||
left_layout.setSpacing(10)
|
||||
|
||||
# 创建文件选择器
|
||||
self.file_selector = FileSelector(self.settings)
|
||||
left_layout.addWidget(self.file_selector)
|
||||
|
||||
# 创建图像预览标签
|
||||
preview_group = QGroupBox("图像预览")
|
||||
preview_layout = QVBoxLayout(preview_group)
|
||||
self.preview_label = QLabel()
|
||||
self.preview_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
|
||||
# 设置固定高度而不是最小高度,确保在整个应用程序运行期间保持一致
|
||||
self.preview_label.setFixedHeight(200)
|
||||
self.preview_label.setStyleSheet("background-color: #333333; border: 1px solid #5d5d5d;")
|
||||
preview_layout.addWidget(self.preview_label)
|
||||
left_layout.addWidget(preview_group)
|
||||
|
||||
# 创建右侧面板
|
||||
right_panel = QWidget()
|
||||
right_layout = QVBoxLayout(right_panel)
|
||||
right_layout.setContentsMargins(0, 0, 0, 0)
|
||||
right_layout.setSpacing(10)
|
||||
|
||||
# 创建参数配置面板
|
||||
self.param_panel = ParamPanel(self.settings)
|
||||
right_layout.addWidget(self.param_panel)
|
||||
|
||||
# 创建图像处理工具栏
|
||||
self.image_toolbar = ImageToolbar(self.image_processor, self.settings, self.preview_label)
|
||||
right_layout.addWidget(self.image_toolbar)
|
||||
|
||||
# 创建分割器
|
||||
splitter = QSplitter(Qt.Orientation.Horizontal)
|
||||
splitter.addWidget(left_panel)
|
||||
splitter.addWidget(right_panel)
|
||||
main_layout.addWidget(splitter)
|
||||
|
||||
# 设置分割器比例
|
||||
splitter.setSizes([int(self.width() * 0.4), int(self.width() * 0.6)])
|
||||
|
||||
# 创建状态栏
|
||||
self.status_bar = QStatusBar()
|
||||
self.setStatusBar(self.status_bar)
|
||||
self.status_bar.showMessage("就绪")
|
||||
|
||||
# 创建主题控制器(不显示在界面上)
|
||||
self.theme_controller = ThemeController(self.settings)
|
||||
|
||||
def _load_settings(self):
|
||||
"""加载设置"""
|
||||
# 恢复窗口几何形状
|
||||
geometry = self.settings.value("geometry")
|
||||
if geometry:
|
||||
self.restoreGeometry(geometry)
|
||||
|
||||
# 应用主题(锁定为深色模式)
|
||||
self.theme_controller.apply_theme("dark")
|
||||
|
||||
def _connect_signals(self):
|
||||
"""连接信号和槽"""
|
||||
# 文件选择器信号
|
||||
self.file_selector.file_selected.connect(self.on_file_selected)
|
||||
self.file_selector.convert_clicked.connect(self.convert_files)
|
||||
self.file_selector.batch_convert_clicked.connect(self.batch_convert)
|
||||
|
||||
# 主题控制器信号
|
||||
self.theme_controller.theme_changed.connect(self.on_theme_changed)
|
||||
|
||||
# 批量管理器信号
|
||||
self.batch_manager.progress_updated.connect(self.update_progress)
|
||||
self.batch_manager.conversion_completed.connect(self.on_conversion_completed)
|
||||
|
||||
def on_file_selected(self, file_path):
|
||||
"""处理文件选择事件"""
|
||||
self.status_bar.showMessage(f"已选择文件: {os.path.basename(file_path)}")
|
||||
|
||||
# 如果是图像文件,启用图像处理工具栏
|
||||
if self.image_processor.is_image_file(file_path):
|
||||
self.image_toolbar.setEnabled(True)
|
||||
self.image_toolbar.load_image(file_path)
|
||||
# 确保预览标签显示图像
|
||||
self.preview_label.setVisible(True)
|
||||
else:
|
||||
self.image_toolbar.setEnabled(False)
|
||||
# 隐藏预览标签
|
||||
self.preview_label.setVisible(False)
|
||||
|
||||
def on_theme_changed(self, theme):
|
||||
"""处理主题变更事件"""
|
||||
self.settings.setValue("theme", theme)
|
||||
|
||||
def update_progress(self, current, total, file_name):
|
||||
"""更新进度信息"""
|
||||
self.status_bar.showMessage(f"正在转换 {file_name}... ({current}/{total})")
|
||||
|
||||
def on_conversion_completed(self, success_count, fail_count):
|
||||
"""处理转换完成事件"""
|
||||
self.status_bar.showMessage(f"转换完成。成功: {success_count}, 失败: {fail_count}")
|
||||
|
||||
if success_count > 0:
|
||||
QMessageBox.information(
|
||||
self,
|
||||
"转换完成",
|
||||
f"已成功转换 {success_count} 个文件" +
|
||||
(f",{fail_count} 个文件失败" if fail_count > 0 else "")
|
||||
)
|
||||
elif fail_count > 0:
|
||||
QMessageBox.warning(
|
||||
self,
|
||||
"转换失败",
|
||||
f"所有 {fail_count} 个文件转换失败"
|
||||
)
|
||||
|
||||
def convert_files(self):
|
||||
"""转换选定的文件"""
|
||||
files = self.file_selector.get_selected_files()
|
||||
if not files:
|
||||
QMessageBox.warning(self, "警告", "请先选择要转换的文件")
|
||||
return
|
||||
|
||||
# 获取输出目录
|
||||
output_dir = QFileDialog.getExistingDirectory(
|
||||
self, "选择输出目录", os.path.expanduser("~")
|
||||
)
|
||||
if not output_dir:
|
||||
return
|
||||
|
||||
# 获取转换参数
|
||||
params = self.param_panel.get_params()
|
||||
|
||||
# 执行转换
|
||||
for file_path in files:
|
||||
try:
|
||||
# 如果是图像文件且图像处理工具栏已启用,先处理图像
|
||||
if self.image_processor.is_image_file(file_path) and self.image_toolbar.isEnabled():
|
||||
processed_image = self.image_toolbar.get_processed_image()
|
||||
if processed_image:
|
||||
# 直接使用处理后的图像进行转换,而不是保存为临时文件
|
||||
output_file = os.path.join(
|
||||
output_dir,
|
||||
f"{os.path.splitext(os.path.basename(file_path))[0]}.mif"
|
||||
)
|
||||
self.converter.convert_from_image(processed_image, output_file, params)
|
||||
else:
|
||||
# 如果没有处理后的图像,使用原始文件
|
||||
output_file = os.path.join(
|
||||
output_dir,
|
||||
f"{os.path.splitext(os.path.basename(file_path))[0]}.mif"
|
||||
)
|
||||
self.converter.convert(file_path, output_file, params)
|
||||
else:
|
||||
# 非图像文件或图像处理工具栏未启用,直接转换
|
||||
output_file = os.path.join(
|
||||
output_dir,
|
||||
f"{os.path.splitext(os.path.basename(file_path))[0]}.mif"
|
||||
)
|
||||
self.converter.convert(file_path, output_file, params)
|
||||
|
||||
self.status_bar.showMessage(f"已转换: {os.path.basename(file_path)}")
|
||||
except Exception as e:
|
||||
QMessageBox.critical(self, "转换错误", f"转换文件 {os.path.basename(file_path)} 时出错: {str(e)}")
|
||||
|
||||
QMessageBox.information(self, "转换完成", "文件转换完成")
|
||||
|
||||
def batch_convert(self):
|
||||
"""批量转换文件"""
|
||||
files = self.file_selector.get_selected_files()
|
||||
if not files:
|
||||
QMessageBox.warning(self, "警告", "请先选择要转换的文件")
|
||||
return
|
||||
|
||||
# 获取输出目录
|
||||
output_dir = QFileDialog.getExistingDirectory(
|
||||
self, "选择输出目录", os.path.expanduser("~")
|
||||
)
|
||||
if not output_dir:
|
||||
return
|
||||
|
||||
# 获取转换参数
|
||||
params = self.param_panel.get_params()
|
||||
|
||||
# 检查是否有需要预处理的图像
|
||||
processed_image = None
|
||||
if self.image_toolbar.isEnabled():
|
||||
processed_image = self.image_toolbar.get_processed_image()
|
||||
|
||||
# 启动批量转换,传递处理后的图像
|
||||
self.batch_manager.start_batch(files, output_dir, params, processed_image)
|
||||
|
||||
def closeEvent(self, event):
|
||||
"""处理窗口关闭事件"""
|
||||
# 保存窗口几何形状
|
||||
self.settings.setValue("geometry", self.saveGeometry())
|
||||
super().closeEvent(event)
|
||||
209
ui/param_panel.py
Normal file
209
ui/param_panel.py
Normal file
@@ -0,0 +1,209 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
# Copyright (c) 2025 Any2MIF Project
|
||||
# All rights reserved.
|
||||
|
||||
"""
|
||||
Any2MIF - 参数配置面板
|
||||
负责MIF转换的参数设置
|
||||
"""
|
||||
|
||||
from PyQt6.QtWidgets import (
|
||||
QWidget, QVBoxLayout, QHBoxLayout, QLabel,
|
||||
QComboBox, QSpinBox, QLineEdit, QGroupBox,
|
||||
QFormLayout, QCheckBox, QSlider
|
||||
)
|
||||
from PyQt6.QtCore import Qt, QSettings, QRegularExpression
|
||||
from PyQt6.QtGui import QRegularExpressionValidator
|
||||
|
||||
class ParamPanel(QWidget):
|
||||
"""参数配置面板"""
|
||||
|
||||
def __init__(self, settings: QSettings, parent=None):
|
||||
"""初始化参数配置面板"""
|
||||
super().__init__(parent)
|
||||
self.settings = settings
|
||||
|
||||
self._init_ui()
|
||||
self._load_settings()
|
||||
self._connect_signals()
|
||||
|
||||
def _init_ui(self):
|
||||
"""初始化UI"""
|
||||
# 创建主布局
|
||||
layout = QVBoxLayout(self)
|
||||
layout.setContentsMargins(0, 0, 0, 0)
|
||||
|
||||
# 创建标题标签
|
||||
title_label = QLabel("参数配置")
|
||||
title_label.setStyleSheet("font-weight: bold; font-size: 14px;")
|
||||
layout.addWidget(title_label)
|
||||
|
||||
# 创建基本参数组
|
||||
basic_group = QGroupBox("基本参数")
|
||||
basic_layout = QFormLayout(basic_group)
|
||||
layout.addWidget(basic_group)
|
||||
|
||||
# WIDTH 参数
|
||||
self.width_combo = QComboBox()
|
||||
self.width_combo.addItems(["8位", "16位", "32位"])
|
||||
basic_layout.addRow("数据宽度 (WIDTH):", self.width_combo)
|
||||
|
||||
# DEPTH 参数
|
||||
self.depth_layout = QHBoxLayout()
|
||||
self.auto_depth_check = QCheckBox("自动计算")
|
||||
self.auto_depth_check.setChecked(True)
|
||||
self.depth_spin = QSpinBox()
|
||||
self.depth_spin.setRange(1, 1000000)
|
||||
self.depth_spin.setValue(1024)
|
||||
self.depth_spin.setEnabled(False)
|
||||
self.depth_layout.addWidget(self.auto_depth_check)
|
||||
self.depth_layout.addWidget(self.depth_spin)
|
||||
basic_layout.addRow("深度 (DEPTH):", self.depth_layout)
|
||||
|
||||
# 基数设置
|
||||
self.radix_combo = QComboBox()
|
||||
self.radix_combo.addItems(["十六进制 (HEX)", "二进制 (BIN)", "十进制 (DEC)"])
|
||||
basic_layout.addRow("数据格式:", self.radix_combo)
|
||||
|
||||
# 地址偏移量
|
||||
self.offset_edit = QLineEdit("0")
|
||||
# 设置十六进制验证器
|
||||
hex_validator = QRegularExpressionValidator(QRegularExpression("0x[0-9A-Fa-f]+|[0-9A-Fa-f]+"))
|
||||
self.offset_edit.setValidator(hex_validator)
|
||||
self.offset_edit.setPlaceholderText("十六进制值 (例如: 0x100)")
|
||||
basic_layout.addRow("地址偏移量:", self.offset_edit)
|
||||
|
||||
# 创建高级参数组
|
||||
advanced_group = QGroupBox("高级参数")
|
||||
advanced_layout = QFormLayout(advanced_group)
|
||||
layout.addWidget(advanced_group)
|
||||
|
||||
# 文件头始终添加,不再提供选项
|
||||
|
||||
# 地址格式
|
||||
self.addr_format_combo = QComboBox()
|
||||
self.addr_format_combo.addItems(["标准格式 (@XXXX)", "紧凑格式 (XXXX:)"])
|
||||
advanced_layout.addRow("地址格式:", self.addr_format_combo)
|
||||
|
||||
# 地址进制
|
||||
self.addr_radix_combo = QComboBox()
|
||||
self.addr_radix_combo.addItems(["十六进制 (HEX)", "二进制 (BIN)", "十进制 (DEC)"])
|
||||
advanced_layout.addRow("地址进制:", self.addr_radix_combo)
|
||||
|
||||
# 每行数据数量
|
||||
self.data_per_line_spin = QSpinBox()
|
||||
self.data_per_line_spin.setRange(1, 16)
|
||||
self.data_per_line_spin.setValue(1)
|
||||
advanced_layout.addRow("每行数据数量:", self.data_per_line_spin)
|
||||
|
||||
# 添加弹性空间
|
||||
layout.addStretch(1)
|
||||
|
||||
def _load_settings(self):
|
||||
"""加载设置"""
|
||||
# 加载WIDTH设置
|
||||
width_index = self.settings.value("params/width_index", 0, int)
|
||||
self.width_combo.setCurrentIndex(width_index)
|
||||
|
||||
# 加载DEPTH设置
|
||||
auto_depth = self.settings.value("params/auto_depth", True, bool)
|
||||
self.auto_depth_check.setChecked(auto_depth)
|
||||
self.depth_spin.setEnabled(not auto_depth)
|
||||
depth = self.settings.value("params/depth", 1024, int)
|
||||
self.depth_spin.setValue(depth)
|
||||
|
||||
# 加载基数设置
|
||||
radix_index = self.settings.value("params/radix_index", 0, int)
|
||||
self.radix_combo.setCurrentIndex(radix_index)
|
||||
|
||||
# 加载地址偏移量
|
||||
offset = self.settings.value("params/offset", "0")
|
||||
self.offset_edit.setText(offset)
|
||||
|
||||
# 加载高级设置
|
||||
|
||||
addr_format_index = self.settings.value("params/addr_format_index", 0, int)
|
||||
self.addr_format_combo.setCurrentIndex(addr_format_index)
|
||||
|
||||
addr_radix_index = self.settings.value("params/addr_radix_index", 0, int)
|
||||
self.addr_radix_combo.setCurrentIndex(addr_radix_index)
|
||||
|
||||
data_per_line = self.settings.value("params/data_per_line", 1, int)
|
||||
self.data_per_line_spin.setValue(data_per_line)
|
||||
|
||||
def _connect_signals(self):
|
||||
"""连接信号和槽"""
|
||||
# 自动深度复选框
|
||||
self.auto_depth_check.toggled.connect(self._on_auto_depth_toggled)
|
||||
|
||||
# 保存设置
|
||||
self.width_combo.currentIndexChanged.connect(self._save_settings)
|
||||
self.auto_depth_check.toggled.connect(self._save_settings)
|
||||
self.depth_spin.valueChanged.connect(self._save_settings)
|
||||
self.radix_combo.currentIndexChanged.connect(self._save_settings)
|
||||
self.offset_edit.editingFinished.connect(self._save_settings)
|
||||
# 文件头选项已移除
|
||||
self.addr_format_combo.currentIndexChanged.connect(self._save_settings)
|
||||
self.addr_radix_combo.currentIndexChanged.connect(self._save_settings)
|
||||
self.data_per_line_spin.valueChanged.connect(self._save_settings)
|
||||
|
||||
def _on_auto_depth_toggled(self, checked):
|
||||
"""处理自动深度复选框切换事件"""
|
||||
self.depth_spin.setEnabled(not checked)
|
||||
|
||||
def _save_settings(self):
|
||||
"""保存设置"""
|
||||
self.settings.setValue("params/width_index", self.width_combo.currentIndex())
|
||||
self.settings.setValue("params/auto_depth", self.auto_depth_check.isChecked())
|
||||
self.settings.setValue("params/depth", self.depth_spin.value())
|
||||
self.settings.setValue("params/radix_index", self.radix_combo.currentIndex())
|
||||
self.settings.setValue("params/offset", self.offset_edit.text())
|
||||
# 文件头设置已移除,始终为True
|
||||
self.settings.setValue("params/header", True)
|
||||
self.settings.setValue("params/addr_format_index", self.addr_format_combo.currentIndex())
|
||||
self.settings.setValue("params/addr_radix_index", self.addr_radix_combo.currentIndex())
|
||||
self.settings.setValue("params/data_per_line", self.data_per_line_spin.value())
|
||||
|
||||
def get_params(self):
|
||||
"""获取参数"""
|
||||
# 获取WIDTH值
|
||||
width_map = {0: 8, 1: 16, 2: 32}
|
||||
width = width_map[self.width_combo.currentIndex()]
|
||||
|
||||
# 获取DEPTH值
|
||||
auto_depth = self.auto_depth_check.isChecked()
|
||||
depth = self.depth_spin.value() if not auto_depth else None
|
||||
|
||||
# 获取基数
|
||||
radix_map = {0: "HEX", 1: "BIN", 2: "DEC"}
|
||||
radix = radix_map[self.radix_combo.currentIndex()]
|
||||
|
||||
# 获取地址偏移量
|
||||
offset_text = self.offset_edit.text()
|
||||
if offset_text.startswith("0x"):
|
||||
offset = int(offset_text, 16)
|
||||
else:
|
||||
offset = int(offset_text, 16)
|
||||
|
||||
# 获取高级参数
|
||||
addr_format = "standard" if self.addr_format_combo.currentIndex() == 0 else "compact"
|
||||
|
||||
# 获取地址进制
|
||||
addr_radix_map = {0: "HEX", 1: "BIN", 2: "DEC"}
|
||||
addr_radix = addr_radix_map[self.addr_radix_combo.currentIndex()]
|
||||
|
||||
data_per_line = self.data_per_line_spin.value()
|
||||
|
||||
# 返回参数字典
|
||||
return {
|
||||
"width": width,
|
||||
"auto_depth": auto_depth,
|
||||
"depth": depth,
|
||||
"radix": radix,
|
||||
"offset": offset,
|
||||
"header": True, # 始终添加文件头
|
||||
"addr_format": addr_format,
|
||||
"addr_radix": addr_radix,
|
||||
"data_per_line": data_per_line
|
||||
}
|
||||
245
ui/theme_controller.py
Normal file
245
ui/theme_controller.py
Normal file
@@ -0,0 +1,245 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
# Copyright (c) 2025 Any2MIF Project
|
||||
# All rights reserved.
|
||||
|
||||
"""
|
||||
Any2MIF - 主题控制器
|
||||
负责应用程序的主题设置,锁定为深色模式
|
||||
"""
|
||||
|
||||
import os
|
||||
from PyQt6.QtWidgets import QWidget
|
||||
from PyQt6.QtCore import QSettings, pyqtSignal
|
||||
|
||||
class ThemeController(QWidget):
|
||||
"""主题控制器,锁定为深色模式"""
|
||||
|
||||
# 自定义信号
|
||||
theme_changed = pyqtSignal(str) # 主题变更时发出的信号
|
||||
|
||||
def __init__(self, settings: QSettings, parent=None):
|
||||
"""初始化主题控制器"""
|
||||
super().__init__(parent)
|
||||
self.settings = settings
|
||||
|
||||
# 直接设置为深色模式
|
||||
self.settings.setValue("theme", "dark")
|
||||
self.apply_theme("dark")
|
||||
self.theme_changed.emit("dark")
|
||||
|
||||
def apply_theme(self, theme):
|
||||
"""应用主题"""
|
||||
# 加载样式表
|
||||
style_file = os.path.join("resources", "styles", f"{theme}.qss")
|
||||
|
||||
if os.path.exists(style_file):
|
||||
with open(style_file, "r", encoding="utf-8") as f:
|
||||
style_sheet = f.read()
|
||||
# 应用样式表到应用程序
|
||||
app = self.window().parent()
|
||||
if app:
|
||||
app.setStyleSheet(style_sheet)
|
||||
else:
|
||||
# 如果样式文件不存在,使用默认样式
|
||||
self._apply_default_style(theme)
|
||||
|
||||
def _apply_default_style(self, theme):
|
||||
"""应用默认样式"""
|
||||
if theme == "light":
|
||||
style_sheet = """
|
||||
QMainWindow, QDialog {
|
||||
background-color: #f5f5f5;
|
||||
color: #333333;
|
||||
}
|
||||
|
||||
QWidget {
|
||||
background-color: #f5f5f5;
|
||||
color: #333333;
|
||||
}
|
||||
|
||||
QLabel {
|
||||
color: #333333;
|
||||
}
|
||||
|
||||
QPushButton {
|
||||
background-color: #e0e0e0;
|
||||
border: 1px solid #b0b0b0;
|
||||
border-radius: 4px;
|
||||
padding: 5px 10px;
|
||||
color: #333333;
|
||||
}
|
||||
|
||||
QPushButton:hover {
|
||||
background-color: #d0d0d0;
|
||||
}
|
||||
|
||||
QPushButton:pressed {
|
||||
background-color: #c0c0c0;
|
||||
}
|
||||
|
||||
QLineEdit, QSpinBox, QComboBox {
|
||||
background-color: #ffffff;
|
||||
border: 1px solid #b0b0b0;
|
||||
border-radius: 4px;
|
||||
padding: 3px;
|
||||
color: #333333;
|
||||
}
|
||||
|
||||
QGroupBox {
|
||||
border: 1px solid #b0b0b0;
|
||||
border-radius: 4px;
|
||||
margin-top: 10px;
|
||||
padding-top: 10px;
|
||||
}
|
||||
|
||||
QGroupBox::title {
|
||||
subcontrol-origin: margin;
|
||||
subcontrol-position: top center;
|
||||
padding: 0 5px;
|
||||
color: #333333;
|
||||
}
|
||||
|
||||
QListWidget {
|
||||
background-color: #ffffff;
|
||||
border: 1px solid #b0b0b0;
|
||||
border-radius: 4px;
|
||||
color: #333333;
|
||||
}
|
||||
|
||||
QListWidget::item:selected {
|
||||
background-color: #b0b0b0;
|
||||
color: #333333;
|
||||
}
|
||||
|
||||
QSlider::groove:horizontal {
|
||||
border: 1px solid #b0b0b0;
|
||||
height: 8px;
|
||||
background: #ffffff;
|
||||
margin: 2px 0;
|
||||
border-radius: 4px;
|
||||
}
|
||||
|
||||
QSlider::handle:horizontal {
|
||||
background: #e0e0e0;
|
||||
border: 1px solid #b0b0b0;
|
||||
width: 18px;
|
||||
margin: -2px 0;
|
||||
border-radius: 9px;
|
||||
}
|
||||
|
||||
QSlider::handle:horizontal:hover {
|
||||
background: #d0d0d0;
|
||||
}
|
||||
|
||||
QStatusBar {
|
||||
background-color: #e0e0e0;
|
||||
color: #333333;
|
||||
}
|
||||
|
||||
QToolBar {
|
||||
background-color: #e0e0e0;
|
||||
border-bottom: 1px solid #b0b0b0;
|
||||
}
|
||||
"""
|
||||
else: # dark
|
||||
style_sheet = """
|
||||
QMainWindow, QDialog {
|
||||
background-color: #2d2d2d;
|
||||
color: #f0f0f0;
|
||||
}
|
||||
|
||||
QWidget {
|
||||
background-color: #2d2d2d;
|
||||
color: #f0f0f0;
|
||||
}
|
||||
|
||||
QLabel {
|
||||
color: #f0f0f0;
|
||||
}
|
||||
|
||||
QPushButton {
|
||||
background-color: #3d3d3d;
|
||||
border: 1px solid #5d5d5d;
|
||||
border-radius: 4px;
|
||||
padding: 5px 10px;
|
||||
color: #f0f0f0;
|
||||
}
|
||||
|
||||
QPushButton:hover {
|
||||
background-color: #4d4d4d;
|
||||
}
|
||||
|
||||
QPushButton:pressed {
|
||||
background-color: #5d5d5d;
|
||||
}
|
||||
|
||||
QLineEdit, QSpinBox, QComboBox {
|
||||
background-color: #3d3d3d;
|
||||
border: 1px solid #5d5d5d;
|
||||
border-radius: 4px;
|
||||
padding: 3px;
|
||||
color: #f0f0f0;
|
||||
}
|
||||
|
||||
QGroupBox {
|
||||
border: 1px solid #5d5d5d;
|
||||
border-radius: 4px;
|
||||
margin-top: 10px;
|
||||
padding-top: 10px;
|
||||
}
|
||||
|
||||
QGroupBox::title {
|
||||
subcontrol-origin: margin;
|
||||
subcontrol-position: top center;
|
||||
padding: 0 5px;
|
||||
color: #f0f0f0;
|
||||
}
|
||||
|
||||
QListWidget {
|
||||
background-color: #3d3d3d;
|
||||
border: 1px solid #5d5d5d;
|
||||
border-radius: 4px;
|
||||
color: #f0f0f0;
|
||||
}
|
||||
|
||||
QListWidget::item:selected {
|
||||
background-color: #5d5d5d;
|
||||
color: #f0f0f0;
|
||||
}
|
||||
|
||||
QSlider::groove:horizontal {
|
||||
border: 1px solid #5d5d5d;
|
||||
height: 8px;
|
||||
background: #3d3d3d;
|
||||
margin: 2px 0;
|
||||
border-radius: 4px;
|
||||
}
|
||||
|
||||
QSlider::handle:horizontal {
|
||||
background: #5d5d5d;
|
||||
border: 1px solid #7d7d7d;
|
||||
width: 18px;
|
||||
margin: -2px 0;
|
||||
border-radius: 9px;
|
||||
}
|
||||
|
||||
QSlider::handle:horizontal:hover {
|
||||
background: #6d6d6d;
|
||||
}
|
||||
|
||||
QStatusBar {
|
||||
background-color: #3d3d3d;
|
||||
color: #f0f0f0;
|
||||
}
|
||||
|
||||
QToolBar {
|
||||
background-color: #3d3d3d;
|
||||
border-bottom: 1px solid #5d5d5d;
|
||||
}
|
||||
"""
|
||||
|
||||
# 应用样式表到应用程序
|
||||
app = self.window().parent()
|
||||
if app:
|
||||
app.setStyleSheet(style_sheet)
|
||||
Reference in New Issue
Block a user