DbTreeViewManager.h 15 KB

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