155 lines
5.1 KiB
Markdown
155 lines
5.1 KiB
Markdown
# 右键菜单功能说明
|
||
|
||
## 功能概述
|
||
|
||
TopHub数据查看器的右键菜单功能允许用户通过右键点击表格中的项目,快速执行常用操作,提高操作效率。
|
||
|
||
## 新增功能
|
||
|
||
### 1. 标记为感兴趣
|
||
- **功能描述**:将选中的项目标记为感兴趣状态
|
||
- **数据库操作**:将对应记录的`is_interested`字段设置为1
|
||
- **界面显示**:在"感兴趣"列显示为"是",使用绿色粗体字体
|
||
|
||
### 2. 标记为不感兴趣
|
||
- **功能描述**:将选中的项目标记为不感兴趣状态
|
||
- **数据库操作**:将对应记录的`is_interested`字段设置为0
|
||
- **界面显示**:在"感兴趣"列显示为"否",使用普通字体和颜色
|
||
|
||
### 3. 删除选中项
|
||
- **功能描述**:删除选中的项目
|
||
- **数据库操作**:从数据库中删除对应记录
|
||
- **界面显示**:从表格中移除对应行
|
||
|
||
## 使用方法
|
||
|
||
1. 打开TopHub数据查看器
|
||
2. 在表格中右键点击任意项目
|
||
3. 在弹出的右键菜单中选择所需操作:
|
||
- 点击"标记为感兴趣"将项目标记为感兴趣
|
||
- 点击"标记为不感兴趣"将项目标记为不感兴趣
|
||
- 点击"删除选中项"删除选中的项目
|
||
|
||
## 技术实现
|
||
|
||
### 右键菜单实现
|
||
```python
|
||
# 启用右键菜单
|
||
self.table.setContextMenuPolicy(Qt.CustomContextMenu)
|
||
self.table.customContextMenuRequested.connect(self.show_context_menu)
|
||
|
||
def show_context_menu(self, position):
|
||
"""显示右键菜单"""
|
||
# 获取点击位置的行
|
||
row = self.table.rowAt(position.y())
|
||
if row < 0:
|
||
return
|
||
|
||
# 选中该行
|
||
self.table.selectRow(row)
|
||
|
||
# 创建右键菜单
|
||
menu = QMenu(self)
|
||
|
||
# 添加"标记为感兴趣"动作
|
||
mark_action = QAction("标记为感兴趣", self)
|
||
mark_action.triggered.connect(self.mark_as_interested)
|
||
menu.addAction(mark_action)
|
||
|
||
# 添加"标记为不感兴趣"动作
|
||
unmark_action = QAction("标记为不感兴趣", self)
|
||
unmark_action.triggered.connect(self.mark_as_not_interested)
|
||
menu.addAction(unmark_action)
|
||
|
||
# 添加分隔线
|
||
menu.addSeparator()
|
||
|
||
# 添加"删除"动作
|
||
delete_action = QAction("删除选中项", self)
|
||
delete_action.triggered.connect(self.delete_selected_items)
|
||
menu.addAction(delete_action)
|
||
|
||
# 显示菜单
|
||
menu.exec_(self.table.mapToGlobal(position))
|
||
```
|
||
|
||
### 标记为不感兴趣方法实现
|
||
```python
|
||
def mark_as_not_interested(self):
|
||
"""将选中的项目标记为不感兴趣"""
|
||
# 获取选中的行
|
||
selected_rows = set()
|
||
for item in self.table.selectedItems():
|
||
selected_rows.add(item.row())
|
||
|
||
# 如果没有选中的行,直接返回
|
||
if not selected_rows:
|
||
QMessageBox.information(self, "提示", "请先选中要标记的行")
|
||
return
|
||
|
||
# 弹出确认对话框
|
||
reply = QMessageBox.question(
|
||
self,
|
||
"确认标记",
|
||
f"确定要将选中的 {len(selected_rows)} 行标记为不感兴趣吗?",
|
||
QMessageBox.Yes | QMessageBox.No,
|
||
QMessageBox.Yes
|
||
)
|
||
|
||
if reply == QMessageBox.No:
|
||
return
|
||
|
||
try:
|
||
# 连接数据库
|
||
conn = sqlite3.connect(self.db_path)
|
||
cursor = conn.cursor()
|
||
|
||
# 更新选中的行
|
||
updated_count = 0
|
||
for row in selected_rows:
|
||
# 获取ID
|
||
id_item = self.table.item(row, 0)
|
||
if id_item:
|
||
article_id = id_item.text()
|
||
# 更新数据库中的is_interested字段
|
||
cursor.execute("UPDATE articles SET is_interested = 0 WHERE id = ?", (article_id,))
|
||
|
||
# 更新表格中的显示
|
||
interested_item = QTableWidgetItem("否")
|
||
# 不感兴趣项使用普通字体和颜色
|
||
self.table.setItem(row, 5, interested_item)
|
||
|
||
updated_count += 1
|
||
|
||
# 提交更改
|
||
conn.commit()
|
||
conn.close()
|
||
|
||
# 更新状态栏
|
||
self.status_bar.showMessage(f"已标记 {updated_count} 行为不感兴趣")
|
||
|
||
except sqlite3.Error as e:
|
||
logger.error(f"标记数据时出错: {str(e)}")
|
||
QMessageBox.critical(self, "数据库错误", f"标记数据时出错: {str(e)}")
|
||
self.status_bar.showMessage("标记失败")
|
||
except Exception as e:
|
||
logger.error(f"标记数据时出错: {str(e)}")
|
||
QMessageBox.critical(self, "错误", f"标记数据时出错: {str(e)}")
|
||
self.status_bar.showMessage("标记失败")
|
||
```
|
||
|
||
## 测试
|
||
|
||
测试脚本`test_mark_not_interested.py`验证了"标记为不感兴趣"功能的正确性。测试结果显示功能正常工作,能够正确地将项目标记为不感兴趣,并更新数据库和界面显示。
|
||
|
||
## 注意事项
|
||
|
||
1. 右键菜单操作前必须先选中要操作的项目
|
||
2. 删除操作不可撤销,请谨慎使用
|
||
3. 标记操作会直接更新数据库,确保操作前已确认选择
|
||
4. 批量操作时,所有选中的项目都会被同时处理
|
||
|
||
## 更新记录
|
||
|
||
- 2023-11-07:添加"标记为不感兴趣"功能到右键菜单
|
||
- 2023-11-07:完成功能测试和文档编写 |