mark bg color

This commit is contained in:
Julian Freeman
2025-08-11 00:50:35 -04:00
parent 276ff9e227
commit d15e2e59ba

47
main.py
View File

@@ -96,6 +96,7 @@ class NoteEditor(QtWidgets.QPlainTextEdit):
自定义编辑器:
- Shift+Enter 插入换行
- Enter (无 Shift) 触发保存emit save_requested 信号)
- 右键菜单设置背景色(绿色、红色、黄色、移除)
"""
save_requested = QtCore.Signal()
@@ -104,6 +105,10 @@ class NoteEditor(QtWidgets.QPlainTextEdit):
# 可选:设置合适的样式/行高
self.setTabChangesFocus(False)
self.setPlaceholderText("按 Enter 保存Shift+Enter 换行)")
# 右键菜单
self.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
self.customContextMenuRequested.connect(self._show_bg_menu)
self.bg_color = "" # 可选值: "", "green", "red", "yellow"
def keyPressEvent(self, event: QtGui.QKeyEvent):
if event.key() in (QtCore.Qt.Key_Return, QtCore.Qt.Key_Enter):
@@ -119,6 +124,38 @@ class NoteEditor(QtWidgets.QPlainTextEdit):
return
super().keyPressEvent(event)
def _show_bg_menu(self, pos):
menu = QtWidgets.QMenu(self)
green_action = menu.addAction("设置绿色背景")
red_action = menu.addAction("设置红色背景")
yellow_action = menu.addAction("设置黄色背景")
menu.addSeparator()
clear_action = menu.addAction("移除背景色")
action = menu.exec(self.mapToGlobal(pos))
if action == green_action:
self.setStyleSheet("background-color: #234d2a; color: #e0e0e0;")
self.bg_color = "green"
elif action == red_action:
self.setStyleSheet("background-color: #4d2323; color: #e0e0e0;")
self.bg_color = "red"
elif action == yellow_action:
self.setStyleSheet("background-color: #4d4d23; color: #e0e0e0;")
self.bg_color = "yellow"
elif action == clear_action:
self.setStyleSheet("")
self.bg_color = ""
def set_bg_color(self, color):
"""用于初始化时恢复背景色"""
self.bg_color = color
if color == "green":
self.setStyleSheet("background-color: #234d2a; color: #e0e0e0;")
elif color == "red":
self.setStyleSheet("background-color: #4d2323; color: #e0e0e0;")
elif color == "yellow":
self.setStyleSheet("background-color: #4d4d23; color: #e0e0e0;")
else:
self.setStyleSheet("")
class ImageRowWidget(QtWidgets.QWidget):
"""每一行的组合组件:缩略图 + 多个 NoteEditor"""
@@ -154,6 +191,9 @@ class ImageRowWidget(QtWidgets.QWidget):
# 初始化内容(如果没有该项目,则为空)
editor.setPlainText(self.note_dict.get(proj, ""))
editor.setMinimumHeight(80)
# 恢复背景色
bg = self.note_dict.get(f"{proj}_bg", "")
editor.set_bg_color(bg)
# 当触发保存信号时,调用保存回调(只保存当前图片)
editor.save_requested.connect(partial(self._on_save_requested))
self.editors[proj] = editor
@@ -175,11 +215,12 @@ class ImageRowWidget(QtWidgets.QWidget):
return None
def collect_notes(self):
"""收集当前编辑器里所有项目字段,返回 dict"""
"""收集当前编辑器里所有项目字段和背景色,返回 dict"""
d = {}
for proj, editor in self.editors.items():
text = editor.toPlainText().strip()
d[proj] = text
d[f"{proj}_bg"] = editor.bg_color
return d
@QtCore.Slot()
@@ -198,8 +239,8 @@ class ImageRowWidget(QtWidgets.QWidget):
QtWidgets.QMessageBox.warning(self, "保存失败", f"图片 {self.filename} 写入失败:\n{err}")
def _clear_editor_styles(self):
for editor in self.editors.values():
editor.setStyleSheet("")
for proj, editor in self.editors.items():
editor.set_bg_color(editor.bg_color)
class MainWindow(QtWidgets.QMainWindow):