DbTreeViewManager.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. #ifndef DBTREEVIEWMANAGER_H
  2. #define DBTREEVIEWMANAGER_H
  3. #define NOMINMAX
  4. #include <QWidget>
  5. #include <QTreeView>
  6. #include <QJsonObject>
  7. #include <QStandardItemModel>
  8. #include <QCheckBox>
  9. #include <QJsonArray>
  10. #include <QFile>
  11. #include <QLineEdit>
  12. #include <QComboBox>
  13. #include <QRadioButton>
  14. #include <QButtonGroup>
  15. #include <QStyleFactory>
  16. #include <QEvent>
  17. #include <QStringList>
  18. #include <QSettings>
  19. #include <QSet>
  20. #include <QVariant>
  21. #include <QMenu>
  22. #include <QAction>
  23. #include <QTimeEdit>
  24. #include <QCursor>
  25. #include <QScrollArea>
  26. #include <QToolButton>
  27. #include <QtSql>
  28. #include <QSqlDatabase>
  29. #include <QSqlQuery>
  30. #include <QSqlError>
  31. #include <QMessageBox>
  32. #include <QPainter>
  33. #include <QTimer>
  34. #include <QFileInfo>
  35. #include <QLabel>
  36. #include <QHBoxLayout>
  37. #include <QPushButton>
  38. #include <QDebug>
  39. #include <QVBoxLayout>
  40. #include <QStandardItem>
  41. #include <QMap>
  42. #include <QJsonDocument>
  43. #include <array>
  44. #include "CViewInterface.h"
  45. #include "Src/Sql/SqlOperation.h"
  46. class OriginalWnd; ///< 前向声明
  47. /**
  48. * @class DbTreeViewManager
  49. * @brief 管理树状视图、导航栏、以及与数据库交互的相关逻辑。
  50. *
  51. * 该类负责从数据库加载目录树 ,并根据用户权限和配置 ID 显示相应的字段与控件。
  52. * 同时还支持切换 Home 界面或其他配置界面、隐藏/显示按钮与分隔线等功能。
  53. */
  54. class DbTreeViewManager : public QWidget
  55. {
  56. Q_OBJECT
  57. public:
  58. /**
  59. * @brief 构造函数
  60. * @param originalWnd 指向 OriginalWnd 对象的指针
  61. * @param widget2 另一个父级或容器部件,用于添加子控件
  62. * @param parent 父窗口指针,可为空
  63. */
  64. explicit DbTreeViewManager(OriginalWnd* originalWnd,
  65. QWidget* widget2, QWidget* parent = nullptr);
  66. /**
  67. * @brief 析构函数,清理资源
  68. */
  69. ~DbTreeViewManager();
  70. /**
  71. * @brief 初始化目录树,从数据库加载数据
  72. * @param name 一级目录名字,对应数据库中的 Configurations 表
  73. * @param userPrivilege 当前用户的权限等级
  74. */
  75. void initializeTree(QString name, const int &userPrivilege);
  76. /**
  77. * @brief 清理所有分隔线(内部调用,用于清除旧有的一级目录分隔线等)
  78. */
  79. void clearAllSeparators();
  80. /**
  81. * @brief 收集所有第三层目录的 QStandardItem
  82. * @return 包含第三层目录项的列表
  83. */
  84. QList<QStandardItem*> collectAllThirdLevelItems();
  85. /**
  86. * @brief 保存所有当前已展开的路径
  87. * 对应 expandedPaths 中的路径保存到持久化存储(如 QSettings)
  88. */
  89. void saveExpandedPaths();
  90. /**
  91. * @brief 加载已展开的路径
  92. * 从持久化存储(如 QSettings)中读取展开路径并恢复
  93. */
  94. void loadExpandedPaths();
  95. /**
  96. * @brief 将日志写入文件中
  97. *
  98. */
  99. void writeLogToFile(const QString& logMessage, const QString& filePath);
  100. void loginput(const QString& fieldTableName,const QString& fieldName,const QString& modifies);
  101. /**
  102. * @brief 存储已展开路径的容器
  103. * 格式通常为 "Root/Child/Child" 的字符串
  104. */
  105. QSet<QString> expandedPaths;
  106. public:
  107. struct menuInfo {
  108. int menuId;
  109. bool isthird;
  110. int index;
  111. };
  112. int currentMenuId;
  113. std::array<menuInfo, 5> menuArray;
  114. void loadpage(const int& configId);
  115. QList<QStandardItem*> thirdLevelListtest;
  116. QStandardItem* deepCopyItem(const QStandardItem* item);
  117. signals:
  118. void RunFunSignals(ns_module::ST_BUTTON_FUN pra);
  119. private slots:
  120. /**
  121. * @brief 当树中某个项目被点击时触发
  122. * @param index 被点击的 QModelIndex
  123. */
  124. void onTreeViewClicked(const QModelIndex &index);
  125. /**
  126. * @brief 当树中某个项目被点击时触发,专门给up和down按钮使用
  127. * @param index 被点击的 QModelIndex
  128. */
  129. void onTreeViewClicked_updown(const QModelIndex &index);
  130. /**
  131. * @brief “返回”按钮点击槽函数
  132. * 用于在第三层目录与上层目录间进行返回逻辑
  133. */
  134. void onButtonBackClicked();
  135. /**
  136. * @brief “上”按钮点击槽函数
  137. * 用于在所有第三层目录间向上移动
  138. */
  139. void onButtonUpClicked();
  140. /**
  141. * @brief “下”按钮点击槽函数
  142. * 用于在所有第三层目录间向下移动
  143. */
  144. void onButtonDownClicked();
  145. /**
  146. * @brief “左”按钮点击槽函数
  147. * 用于目录层级的向左逻辑(如果有需要)
  148. */
  149. void onButtonLeftClicked();
  150. /**
  151. * @brief “右”按钮点击槽函数
  152. * 用于目录层级的向右逻辑(如果有需要)
  153. */
  154. void onButtonRightClicked();
  155. /**
  156. * @brief 当树状模型中某个 QStandardItem 的复选框状态发生变化时触发
  157. * @param item 发生变化的 QStandardItem
  158. */
  159. void onItemChanged(QStandardItem *item);
  160. private:
  161. /**
  162. * @brief 指向 OriginalWnd 对象的指针,用于访问或联动其它窗口功能
  163. */
  164. OriginalWnd* m_originalWnd = nullptr;
  165. /**
  166. * @brief widget2 外部传入的父级/容器,用于承载子控件
  167. */
  168. QWidget *widget2 = nullptr;
  169. /**
  170. * @brief 树视图控件
  171. */
  172. QTreeView *treeViewDown = nullptr;
  173. /**
  174. * @brief 导航栏 QWidget,用于显示路径信息
  175. */
  176. QWidget *navigationWidget = nullptr;
  177. /**
  178. * @brief 树状模型,用于存放目录层级
  179. */
  180. QStandardItemModel *model = nullptr;
  181. /**
  182. * @brief 用于存储第三层字段展示窗口的指针(单实例)
  183. */
  184. QWidget* m_thirdLevelFieldWnd = nullptr;
  185. /**
  186. * @brief 上部的几个操作按钮(例如 “返回”、“上”、“下”、“左”、“右”)
  187. */
  188. QPushButton *ButtonBack = nullptr; ///< 返回按钮
  189. QPushButton *buttonUp = nullptr; ///< 上按钮
  190. QPushButton *buttonDown = nullptr; ///< 下按钮
  191. QPushButton *buttonLeft = nullptr; ///< 左按钮
  192. QPushButton *buttonRight = nullptr; ///< 右按钮
  193. // 定义一个结构来记录当前所处页面的信息
  194. struct PageState {
  195. QStringList path; // 记录目录的文本路径
  196. bool isThirdLevel; // 是否是第三层目录
  197. int directoryId; // 第三层目录ID
  198. QString multiTableName = "";
  199. };
  200. // 存储“后退栈”和“前进栈”,
  201. QList<PageState> m_backStack;
  202. QList<PageState> m_forwardStack;
  203. /**
  204. * @brief restoring 标记是否在恢复阶段
  205. */
  206. bool restoring = false;
  207. /**
  208. * @brief 存储“一级目录”和相应分隔线的映射,用于更新和控制分隔线位置、可见性等
  209. */
  210. QMap<QStandardItem*, QFrame*> firstLevelSeparators;
  211. /**
  212. * @brief 标记是否阻止触发 onItemChanged 等复选框变更响应
  213. */
  214. bool m_blockItemChanged = false;
  215. /**
  216. * @brief 用于存储第三层界面生成的控件列表,方便统一管理和销毁
  217. */
  218. QList<QWidget*> m_fieldWidgets;
  219. /**
  220. * @brief 当前正在使用的配置名字 ????
  221. */
  222. QString m_currentConfigName = "";
  223. // int m_currentConfigId = -1;
  224. /**
  225. * @brief 特定分隔线,Home 界面时隐藏,其他界面时显示
  226. */
  227. QFrame* lineFrame1 = nullptr;
  228. SqlOperation* m_sqlOper = nullptr;
  229. private:
  230. // 每次切换控制屏幕时,清空历史
  231. void clearHistory();
  232. // 用于把当前选中的目录或第三层界面封装成 PageState
  233. PageState getCurrentPageState();
  234. // 加载指定 PageState,相当于“跳转”到该状态
  235. void loadPageState(const PageState &st, bool isByHistoryNav = false);
  236. /**
  237. * @brief 从数据库加载指定 configId 下的目录记录,并构建树状结构
  238. * @param configId 配置ID
  239. * @param userPrivilege 当前用户权限
  240. */
  241. //!!!读数据库,修改为我们自己的数据库,或者封装对象
  242. void loadDirectories(int configId, const int &userPrivilege);
  243. /**
  244. * @brief 在给定父节点内,DFS 查找第一个第三层目录项
  245. * @param parentItem 父节点
  246. * @return 若找到第三层节点则返回指针,否则返回 nullptr
  247. */
  248. QStandardItem* findFirstThirdLevelItemDFS(QStandardItem *parentItem);
  249. /**
  250. * @brief 递归更新父级目录的复选框状态
  251. * @param item 当前变更的子项
  252. */
  253. void updateParentItems(QStandardItem *parentItem);
  254. /**
  255. * @brief 由 loadDirectories 获取的目录列表,构建树模型
  256. * @param directories 包含目录信息的 QJsonObject 列表
  257. */
  258. void buildTreeFromDirectories(const QList<QJsonObject> &directories);
  259. /**
  260. * @brief 更新父项的复选框状态(如果所有子项选中则父项也选中,否则取消)
  261. * @param item 当前变更的子项
  262. */
  263. void updateParentCheckState(QStandardItem *item);
  264. /**
  265. * @brief 更新按钮可见性:Home 界面隐藏,其他配置显示
  266. */
  267. void updateButtonsVisibility();
  268. /**
  269. * @brief 清理并隐藏第三层菜单内容
  270. */
  271. void clearThirdLevelMenu();
  272. /**
  273. * @brief 由给定的 DirectoryId 查询数据库中的字段信息(BondHead、Buttons)并返回
  274. * @param directoryId 目录ID
  275. * @param userPrivilege 当前用户权限
  276. * @return 包含第三层目录字段和按钮信息的 QJsonObject
  277. */
  278. //查询用户权限 !!!每次查询读取数据库,需要做修改
  279. //QJsonObject fetchThirdLevelData(int directoryId, const int &userPrivilege);
  280. /**
  281. * @brief 自定义事件过滤器,用于在 QTreeView 上绘制虚线等
  282. * @param watched 监视的对象
  283. * @param event 事件
  284. * @return 是否拦截事件
  285. */
  286. bool eventFilter(QObject *watched, QEvent *event) override;
  287. /**
  288. * @brief 递归绘制所有分支
  289. * @param parentIndex 父节点索引
  290. * @param painter QPainter 引用
  291. */
  292. void paintAllBranches(const QModelIndex &parentIndex, QPainter &painter);
  293. /**
  294. * @brief 绘制父节点到子节点的“拐角线”
  295. * @param childIndex 子节点索引
  296. * @param painter QPainter 引用
  297. */
  298. void drawParentChildLine(const QModelIndex &childIndex, QPainter &painter);
  299. /**
  300. * @brief 兄弟节点之间的竖线连接
  301. * @param childIndex 当前节点索引
  302. * @param painter QPainter 引用
  303. */
  304. void drawSiblingLine(const QModelIndex &childIndex, QPainter &painter);
  305. /**
  306. * @brief 应用自定义样式(例如树的图标、复选框等)
  307. */
  308. void applyCustomStyles();
  309. /**
  310. * @brief 创建顶部按钮,并绑定槽函数
  311. */
  312. void setupButton();
  313. /**
  314. * @brief 创建并返回一个统一样式的分隔线 QFrame
  315. * @param parent 父对象
  316. * @param height 分隔线高度
  317. * @return 分隔线 QFrame 指针
  318. */
  319. QFrame* createUnifiedSeparator(QWidget *parent, int height);
  320. /**
  321. * @brief 更新分隔线位置和可见性(主要针对一级目录和其子项)
  322. */
  323. void updateSeparatorLine();
  324. /**
  325. * @brief 查找父节点下最后一个可见子项,用于放置分隔线
  326. * @param parentIndex 父节点索引
  327. * @return 最后一个可见子节点的索引
  328. */
  329. QModelIndex findLastVisibleChild(const QModelIndex &parentIndex);
  330. /**
  331. * @brief 更新导航栏,显示选中的路径信息
  332. * @param index 选中节点的 QModelIndex
  333. */
  334. void updateNavigationBar(const QModelIndex &index);
  335. /**
  336. * @brief 加载第三层目录按钮配置并在 widget_left 显示
  337. * @param thirdLevelObj 包含第三层目录 button 信息的 JSON
  338. */
  339. // 加载右侧按钮
  340. // void loadButtonConfigForThirdLevel(const QJsonObject &thirdLevelObj);
  341. void displayThirdLevelButtons(const QList<CONFIG_BASE_STRUCT>& buttons);
  342. /**
  343. * @brief 显示第三层目录的字段信息
  344. * @param data 第三层目录数据(包含 fields, buttons 等)
  345. * @param isHome 是否是 Home 界面,若是则填满 widget2
  346. */
  347. //选中三级目录时显示控件,包括控件类型
  348. //void displayThirdLevelFields(const QJsonObject &data, bool isHome);
  349. //选中三级目录时显示控件,包括控件类型
  350. void displayThirdLevelFields(const QList<Table_Control_Data>& data, bool allChangeFalg, bool isHome);
  351. /**
  352. * @brief 保存当前选中的复选框路径到 QSettings
  353. */
  354. void saveCheckedPaths();
  355. /**
  356. * @brief 从 QSettings 中加载复选框选中路径,并恢复
  357. */
  358. void loadCheckedPaths();
  359. /**
  360. * @brief 收集所有被选中的复选框路径
  361. * @return 被选中的路径列表,每项为 "Root/Child/Child"
  362. */
  363. QStringList collectCheckedPaths();
  364. /**
  365. * @brief 递归收集选中的路径
  366. * @param item 当前节点
  367. * @param path 当前路径
  368. * @return 收集到的选中路径列表
  369. */
  370. QStringList collectCheckedPathsRecursive(QStandardItem *item, QStringList path);
  371. /**
  372. * @brief 批量设置选中的复选框
  373. * @param checkedPathsList 已选中的路径列表
  374. */
  375. void setCheckedPaths(const QStringList &checkedPathsList);
  376. /**
  377. * @brief 构建节点的完整路径
  378. * @param item 当前 QStandardItem
  379. * @return 路径列表,如 ["Root", "Child", "Child2"]
  380. */
  381. QStringList buildItemPath(QStandardItem *item);
  382. /**
  383. * @brief 根据路径查找对应的节点索引
  384. * @param path 路径列表
  385. * @return 对应的 QModelIndex,若未找到则无效
  386. */
  387. QModelIndex findItemByPath(const QStringList &path);
  388. /**
  389. * @brief 检查权限,用于判断某字段/按钮是否可见
  390. * @param UserGrade 数据库中存储的权限列表,如 "1,2,3"
  391. * @param userPrivilege 当前用户权限
  392. * @return 是否包含此权限
  393. */
  394. bool hasPrivilege(const int &UserGrade, const int &userPrivilege);
  395. /**
  396. * @brief 实时的数据写入库
  397. * @param TableName 数据所在表的name
  398. * @param Id 在表中的id
  399. * @param currentValue 当前修改的id
  400. */
  401. //void updateDb(const QString &TableName, const int &Id,const QString currentValue);
  402. //封装一个函数,用于读取给定一级目录下的所有目录
  403. };
  404. #endif // DBTREEVIEWMANAGER_H