treeviewmanager.cpp 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133
  1. #include "treeviewmanager.h"
  2. #include "OriginalWnd.h"
  3. // 构造函数
  4. TreeViewManager::TreeViewManager(OriginalWnd* originalWnd, QWidget *widget2, QWidget *parent)
  5. : QWidget(parent),
  6. widget2(widget2),
  7. treeViewDown(new QTreeView(widget2)),
  8. m_originalWnd(originalWnd),
  9. navigationWidget(nullptr),
  10. buttonOpenFile(nullptr),
  11. buttonUp(nullptr),
  12. buttonDown(nullptr),
  13. buttonLeft(nullptr),
  14. buttonRight(nullptr),
  15. restoring(false)
  16. {
  17. if (!widget2) {
  18. qWarning() << "TreeViewManager: widget2 未初始化";
  19. return;
  20. }
  21. downModel = new QStandardItemModel(this); // 初始化目录树模型
  22. treeViewDown->setModel(downModel);
  23. applyCustomStyles(); // 目录树样式表
  24. treeViewDown->setHeaderHidden(true); // 隐藏显示头部
  25. treeViewDown->setGeometry(16, 106, widget2->width()-16, widget2->height() - 106); //目录树大小和位置
  26. treeViewDown->setEditTriggers(QAbstractItemView::NoEditTriggers); //禁止目录输入
  27. treeViewDown->viewport()->installEventFilter(this); // 给 viewport() 安装事件过滤器,以便拦截其 Paint 事件
  28. QFrame *lineFrame1 = createUnifiedSeparator(widget2, 2); // 使用统一的分隔线创建函数
  29. lineFrame1->setGeometry(16, 89, 428, 2);
  30. lineFrame1->show();
  31. setupButton(); // 创建右上角按钮并设置布局
  32. navigationWidget = new QWidget(widget2); // 创建导航栏
  33. navigationWidget->setGeometry(15, 15, 300, 74);
  34. loadVisitedPaths(); // 加载并展开上次访问过的路径
  35. // 连接目录前的复选框信号与槽
  36. connect(downModel, &QStandardItemModel::itemChanged, this, &TreeViewManager::onItemChanged);
  37. // 目录树连接点击信号
  38. connect(treeViewDown, &QTreeView::clicked, this, [=](const QModelIndex &index) {
  39. QStandardItem *item = downModel->itemFromIndex(index);
  40. if (!item) return;
  41. if (!restoring) { // 仅在非恢复状态下记录选中路径
  42. QStringList path = buildItemPath(item);
  43. addVisitedPath(path);
  44. }
  45. QVariant data = item->data(Qt::UserRole);
  46. if (data.canConvert<QJsonObject>()) {
  47. QJsonObject fields = data.toJsonObject();
  48. if (fields.contains("isThirdLevel") && fields["isThirdLevel"].toBool()) {
  49. updateNavigationBar(index); // 更新导航栏
  50. displayThirdLevelFields(fields); // 显示三级目录字段内容
  51. treeViewDown->hide(); // 隐藏目录
  52. updateSeparatorLine(); // 隐藏目录分割线
  53. return;
  54. }
  55. }
  56. updateNavigationBar(index); // 即使是非三级目录,也更新导航栏
  57. // qDebug() << "更新导航栏,目录项:" << item->text();
  58. });
  59. // 连接目录 expanded 和 collapsed 信号
  60. connect(treeViewDown, &QTreeView::expanded, this, [=](const QModelIndex &index) {
  61. QStandardItem *item = downModel->itemFromIndex(index);
  62. if (!item) return;
  63. QStringList path = buildItemPath(item);
  64. addExpandedPath(path);
  65. updateSeparatorLine();
  66. });
  67. connect(treeViewDown, &QTreeView::collapsed, this, [=](const QModelIndex &index) {
  68. QStandardItem *item = downModel->itemFromIndex(index);
  69. if (!item) return;
  70. QStringList path = buildItemPath(item);
  71. removeExpandedPath(path);
  72. updateSeparatorLine();
  73. });
  74. // 所有展开操作完成后更新分隔线
  75. QTimer::singleShot(0, this, [=]() {
  76. QStandardItem *rootItem = downModel->invisibleRootItem();
  77. QStandardItem *thirdItem = findFirstThirdLevelItemDFS(rootItem);
  78. if (thirdItem) {
  79. QJsonObject thirdLevelObj = thirdItem->data(Qt::UserRole).toJsonObject();
  80. if (thirdLevelObj.contains("isThirdLevel") && thirdLevelObj["isThirdLevel"].toBool()) {
  81. loadButtonConfigForThirdLevel(thirdLevelObj);
  82. }
  83. }
  84. });
  85. connect(buttonOpenFile, &QPushButton::clicked, this, &TreeViewManager::onButtonOpenFileClicked);
  86. connect(buttonUp, &QPushButton::clicked, this, &TreeViewManager::onButtonUpClicked);
  87. connect(buttonDown, &QPushButton::clicked, this, &TreeViewManager::onButtonDownClicked);
  88. connect(buttonLeft, &QPushButton::clicked, this, &TreeViewManager::onButtonLeftClicked);
  89. connect(buttonRight, &QPushButton::clicked, this, &TreeViewManager::onButtonRightClicked);
  90. }
  91. TreeViewManager::~TreeViewManager()
  92. {
  93. }
  94. // 隐藏并删除所有分割线
  95. void TreeViewManager::clearAllSeparators()
  96. {
  97. for (auto separator : firstLevelSeparators.values()) {
  98. if (separator) {
  99. separator->hide();
  100. separator->deleteLater();
  101. }
  102. }
  103. firstLevelSeparators.clear(); // 清空map
  104. }
  105. // 查找非三级目录的内容添加分隔线
  106. QStandardItem* TreeViewManager::findFirstThirdLevelItemDFS(QStandardItem *parentItem)
  107. {
  108. if (!parentItem) {
  109. return nullptr;
  110. }
  111. // 遍历当前 parentItem 的所有子
  112. for (int i = 0; i < parentItem->rowCount(); ++i) {
  113. QStandardItem *child = parentItem->child(i);
  114. if (!child) continue;
  115. // 检查这个子节点是否是三级目录
  116. QVariant data = child->data(Qt::UserRole);
  117. if (data.canConvert<QJsonObject>()) {
  118. QJsonObject obj = data.toJsonObject();
  119. if (obj.contains("isThirdLevel") && obj["isThirdLevel"].toBool()) {
  120. // 找到第一个 isThirdLevel = true 的节点就返回
  121. return child;
  122. }
  123. }
  124. // 如果不是 isThirdLevel,继续往子节点里找
  125. QStandardItem* deeperFound = findFirstThirdLevelItemDFS(child);
  126. if (deeperFound) {
  127. return deeperFound;
  128. }
  129. }
  130. return nullptr;
  131. }
  132. // 递归更新复选框子项状态
  133. void TreeViewManager::updateChildItems(QStandardItem *parentItem, Qt::CheckState state)
  134. {
  135. if (!parentItem)
  136. return;
  137. // qDebug() << "Updating child items of:" << parentItem->text() << "to state:" << state;
  138. for (int i = 0; i < parentItem->rowCount(); ++i)
  139. {
  140. QStandardItem *child = parentItem->child(i);
  141. if (child)
  142. {
  143. child->setCheckState(state);
  144. updateChildItems(child, state); // 递归更新子项
  145. //qDebug() << "updateChildItems:" << parentItem->text() << "to state:" << state;
  146. }
  147. }
  148. }
  149. // 收集所有被选中的复选框路径
  150. QStringList TreeViewManager::collectCheckedPathsRecursive(QStandardItem *item, QStringList path)
  151. {
  152. QStringList checkedList;
  153. if (!item) {
  154. item = downModel->invisibleRootItem();
  155. }
  156. for (int i = 0; i < item->rowCount(); ++i) {
  157. QStandardItem *child = item->child(i);
  158. if (child) {
  159. QStringList currentPath = path;
  160. currentPath << child->text();
  161. if (child->checkState() == Qt::Checked) {
  162. checkedList << currentPath.join("/");
  163. }
  164. // 递归收集子项
  165. checkedList << collectCheckedPathsRecursive(child, currentPath);
  166. }
  167. }
  168. return checkedList;
  169. }
  170. // 无参数版本的 collectCheckedPaths 函数定义
  171. QStringList TreeViewManager::collectCheckedPaths()
  172. {
  173. return collectCheckedPathsRecursive(downModel->invisibleRootItem(), QStringList());
  174. }
  175. void TreeViewManager::setCheckedPaths(const QStringList &checkedPathsList)
  176. {
  177. //qDebug() << "Setting checked paths:" << checkedPathsList;
  178. m_blockItemChanged = true;
  179. // qDebug() << "setCheckedPaths: m_blockItemChanged set to true";
  180. for (const QString &pathStr : checkedPathsList) {
  181. QStringList path = pathStr.split("/");
  182. QModelIndex idx = findItemByPath(path);
  183. if (idx.isValid()) {
  184. QStandardItem *item = downModel->itemFromIndex(idx);
  185. if (item) {
  186. qDebug() << " - Setting item:" << item->text() << "to Checked";
  187. item->setCheckState(Qt::Checked);
  188. visitedPaths.insert(pathStr);
  189. qDebug() << "Added visited path:" << pathStr;
  190. }
  191. } else {
  192. qDebug() << "Path not found:" << pathStr;
  193. }
  194. }
  195. m_blockItemChanged = false;
  196. //qDebug() << "setCheckedPaths: m_blockItemChanged set to false";
  197. }
  198. // 保存复选框状态
  199. void TreeViewManager::saveCheckedPaths()
  200. {
  201. if (currentConfigFilePath.isEmpty()) {
  202. qWarning() << "当前配置文件路径为空,无法保存复选框状态。";
  203. return;
  204. }
  205. QSettings settings("RunCloudTech", "David");
  206. QString configKey = currentConfigKey(currentConfigFilePath);
  207. settings.beginGroup("TreeViewCheckedState");
  208. // 保存选中路径
  209. QString keyChecked = QString("checkedPaths/%1").arg(configKey);
  210. QStringList checkedList = collectCheckedPaths();
  211. settings.setValue(keyChecked, checkedList);
  212. settings.endGroup();
  213. qDebug() << "保存复选框状态路径:" << checkedList;
  214. }
  215. // 加载复选框状态
  216. void TreeViewManager::loadCheckedPaths()
  217. {
  218. if (currentConfigFilePath.isEmpty()) {
  219. qWarning() << "当前配置文件路径为空,无法加载复选框状态。";
  220. return;
  221. }
  222. QSettings settings("RunCloudTech", "David");
  223. QString configKey = currentConfigKey(currentConfigFilePath);
  224. settings.beginGroup("TreeViewCheckedState");
  225. // Read checked paths
  226. QString keyChecked = QString("checkedPaths/%1").arg(configKey);
  227. QStringList loadedChecked = settings.value(keyChecked).toStringList();
  228. settings.endGroup();
  229. qDebug() << "加载复选框状态路径:" << loadedChecked;
  230. m_blockItemChanged = true;
  231. //qDebug() << "loadCheckedPaths: m_blockItemChanged set to true";
  232. setCheckedPaths(loadedChecked);
  233. m_blockItemChanged = false;
  234. //qDebug() << "loadCheckedPaths: m_blockItemChanged set to false";
  235. }
  236. void TreeViewManager::onItemChanged(QStandardItem *item)
  237. {
  238. //qDebug() << "onItemChanged called for item:" << item->text();
  239. //qDebug() << "Flags - m_blockItemChanged:" << m_blockItemChanged << ", restoring:" << restoring;
  240. if (m_blockItemChanged || restoring) {
  241. qDebug() << "Exiting onItemChanged due to flags.";
  242. return; // 防止递归和恢复期间触发
  243. }
  244. m_blockItemChanged = true;
  245. qDebug() << "Processing item:" << item->text();
  246. Qt::CheckState state = item->checkState();
  247. updateChildItems(item, state); // 更新所有子项的复选框状态
  248. updateParentItems(item->parent()); // 更新所有父项的复选框状态
  249. m_blockItemChanged = false;
  250. // 如果不在恢复模式下,记录被选中的路径
  251. if (!restoring) {
  252. QStringList path = buildItemPath(item);
  253. if (state == Qt::Checked) {
  254. addVisitedPath(path);
  255. } else {
  256. // 如果需要在取消选中时执行某些操作,例如从 visitedPaths 中移除
  257. QString joinedPath = path.join("/");
  258. if (visitedPaths.contains(joinedPath)) {
  259. visitedPaths.remove(joinedPath);
  260. qDebug() << "移除选中路径:" << joinedPath;
  261. }
  262. }
  263. // 记录复选框状态
  264. saveCheckedPaths();
  265. }
  266. }
  267. //更新所有父项的复选框状态
  268. void TreeViewManager::updateParentItems(QStandardItem *parentItem)
  269. {
  270. if (!parentItem)
  271. return;
  272. int checkedCount = 0;
  273. int totalCount = parentItem->rowCount();
  274. qDebug() << "Updating child items of:" ;
  275. for (int i = 0; i < totalCount; ++i)
  276. {
  277. QStandardItem *child = parentItem->child(i);
  278. if (child && child->checkState() == Qt::Checked)
  279. {
  280. checkedCount++;
  281. }
  282. }
  283. if (checkedCount == totalCount)
  284. {
  285. parentItem->setCheckState(Qt::Checked);
  286. }
  287. else
  288. {
  289. parentItem->setCheckState(Qt::Unchecked);
  290. }
  291. // 递归更新上层父项
  292. updateParentItems(parentItem->parent());
  293. }
  294. //创建横线样式
  295. QFrame* TreeViewManager::createUnifiedSeparator(QWidget *parent, int height)
  296. {
  297. QFrame *separator = new QFrame(parent);
  298. separator->setFrameShape(QFrame::NoFrame); // 移除默认框架
  299. separator->setFixedHeight(height); // 设置固定高度
  300. separator->setStyleSheet("background-color: #C7CAEB;"); // 设置背景颜色
  301. separator->hide(); // 根据需要初始化为隐藏
  302. //qDebug() << "创建统一分隔符 QFrame,父级:" << parent;
  303. return separator;
  304. }
  305. // 目录树的横线
  306. void TreeViewManager::updateSeparatorLine() {
  307. // 检查 treeViewDown 是否可见
  308. if (!treeViewDown->isVisible()) {
  309. // 隐藏所有横线
  310. for (auto separator : firstLevelSeparators) {
  311. if (separator)
  312. separator->hide();
  313. }
  314. return;
  315. }
  316. // 遍历记录的一级目录
  317. for (auto it = firstLevelSeparators.begin(); it != firstLevelSeparators.end(); ++it) {
  318. QStandardItem *firstLevelItem = it.key();
  319. QFrame *separator = it.value();
  320. if (!firstLevelItem || !separator) continue;
  321. QModelIndex firstLevelIndex = downModel->indexFromItem(firstLevelItem);
  322. QRect firstLevelRect = treeViewDown->visualRect(firstLevelIndex);
  323. if (!firstLevelRect.isValid()) {
  324. separator->hide();
  325. continue;
  326. }
  327. if (treeViewDown->isExpanded(firstLevelIndex) && firstLevelItem->hasChildren()) {
  328. // 找到最后一个可见子目录
  329. QModelIndex lastVisibleChild = findLastVisibleChild(firstLevelIndex);
  330. if (lastVisibleChild.isValid()) {
  331. QRect lastChildRect = treeViewDown->visualRect(lastVisibleChild);
  332. if (lastChildRect.isValid()) {
  333. // 将横线放置在最后一个子目录下方
  334. separator->setGeometry(16, lastChildRect.bottom() + 105, widget2->width() - 32, 2);
  335. separator->show();
  336. } else {
  337. // 如果子目录不可见,则将横线放在一级目录下方
  338. separator->setGeometry(16, firstLevelRect.bottom() + 105, widget2->width() - 32, 2);
  339. separator->show();
  340. }
  341. } else {
  342. // 没有可见子目录,则放在一级目录下方
  343. separator->setGeometry(16, firstLevelRect.bottom() + 105, widget2->width() - 32, 2);
  344. separator->show();
  345. }
  346. } else {
  347. // 一级目录收起,横线直接在下方
  348. separator->setGeometry(16, firstLevelRect.bottom() + 105, widget2->width() - 32, 2);
  349. separator->show();
  350. }
  351. }
  352. }
  353. // 分割线 找到最后一个可见子项
  354. QModelIndex TreeViewManager::findLastVisibleChild(const QModelIndex &parentIndex) {
  355. if (!parentIndex.isValid()) return QModelIndex();
  356. int childCount = downModel->rowCount(parentIndex);
  357. QModelIndex lastVisibleChild;
  358. for (int i = childCount - 1; i >= 0; --i) {
  359. QModelIndex childIndex = downModel->index(i, 0, parentIndex);
  360. if (!treeViewDown->isRowHidden(i, parentIndex)) { // 确保子项未被隐藏
  361. if (treeViewDown->isExpanded(childIndex) && downModel->rowCount(childIndex) > 0) {
  362. QModelIndex deeperChild = findLastVisibleChild(childIndex);
  363. if (deeperChild.isValid()) {
  364. return deeperChild;
  365. }
  366. }
  367. lastVisibleChild = childIndex;
  368. break; // 找到最后一个可见子项后退出循环
  369. }
  370. }
  371. return lastVisibleChild;
  372. }
  373. // 使用文件名作为唯一标识符
  374. QString TreeViewManager::currentConfigKey(const QString &configFilePath)
  375. {
  376. QFileInfo fileInfo(configFilePath);
  377. QString key = fileInfo.fileName();
  378. qDebug() << "Generated config key:" << key;
  379. return key;
  380. }
  381. void TreeViewManager::saveVisitedPaths()
  382. {
  383. if (currentConfigFilePath.isEmpty()) {
  384. qWarning() << "当前配置文件路径为空,无法保存复选框状态。";
  385. return;
  386. }
  387. QSettings settings("RunCloudTech", "David");
  388. QString configKey = currentConfigKey(currentConfigFilePath);
  389. settings.beginGroup("TreeViewState");
  390. // 保存选中路径
  391. QString keyVisited = QString("visitedPaths/%1").arg(configKey);
  392. QStringList visitedList(visitedPaths.begin(), visitedPaths.end());
  393. settings.setValue(keyVisited, visitedList);
  394. // 保存展开路径
  395. QString keyExpanded = QString("expandedPaths/%1").arg(configKey);
  396. QStringList expandedList(expandedPaths.begin(), expandedPaths.end());
  397. settings.setValue(keyExpanded, expandedList);
  398. settings.endGroup();
  399. qDebug() << "保存选中路径:" << visitedList;
  400. qDebug() << "保存展开路径:" << expandedList;
  401. // 调用保存复选框状态
  402. saveCheckedPaths();
  403. }
  404. void TreeViewManager::loadVisitedPaths()
  405. {
  406. if (currentConfigFilePath.isEmpty()) {
  407. qWarning() << "当前配置文件路径为空,无法加载访问路径。";
  408. return;
  409. }
  410. QSettings settings("RunCloudTech", "David");
  411. QString configKey = currentConfigKey(currentConfigFilePath);
  412. settings.beginGroup("TreeViewState");
  413. // 读取选中路径
  414. QString keyVisited = QString("visitedPaths/%1").arg(configKey);
  415. QStringList loadedVisited = settings.value(keyVisited).toStringList();
  416. // 读取展开路径
  417. QString keyExpanded = QString("expandedPaths/%1").arg(configKey);
  418. QStringList loadedExpanded = settings.value(keyExpanded).toStringList();
  419. settings.endGroup();
  420. qDebug() << "加载选中路径:" << loadedVisited;
  421. qDebug() << "加载展开路径:" << loadedExpanded;
  422. restoring = true; // 进入恢复模式
  423. // 恢复展开路径
  424. for (const QString &p : loadedExpanded) {
  425. QStringList path = p.split("/");
  426. QModelIndex idx = findItemByPath(path);
  427. if (idx.isValid()) {
  428. treeViewDown->expand(idx);
  429. expandedPaths.insert(p); // 重新插入
  430. qDebug() << "成功恢复展开路径:" << p;
  431. } else {
  432. qDebug() << "未找到展开路径部分: " << p;
  433. }
  434. }
  435. // 恢复选中路径
  436. for (const QString &p : loadedVisited) {
  437. QStringList path = p.split("/");
  438. QModelIndex idx = findItemByPath(path);
  439. if (idx.isValid()) {
  440. QStandardItem *item = downModel->itemFromIndex(idx);
  441. if (item) {
  442. item->setCheckState(Qt::Checked);
  443. visitedPaths.insert(p);
  444. qDebug() << "成功恢复选中路径:" << p;
  445. }
  446. } else {
  447. qDebug() << "未找到选中路径部分: " << p;
  448. }
  449. }
  450. // 恢复复选框状态
  451. loadCheckedPaths();
  452. restoring = false; // 退出恢复模式
  453. // 更新导航栏
  454. if (!loadedVisited.isEmpty()) {
  455. QString lastPathStr = loadedVisited.last();
  456. QStringList lastPath = lastPathStr.split("/");
  457. QModelIndex lastIdx = findItemByPath(lastPath);
  458. if (lastIdx.isValid()) {
  459. treeViewDown->setCurrentIndex(lastIdx);
  460. updateNavigationBar(lastIdx);
  461. }
  462. } else {
  463. // 如果没有加载到任何路径,自动选择第一个目录
  464. QStandardItem *rootItem = downModel->invisibleRootItem();
  465. if (rootItem->rowCount() > 0) {
  466. QModelIndex firstIndex = downModel->index(0, 0, QModelIndex());
  467. if (firstIndex.isValid()) {
  468. treeViewDown->setCurrentIndex(firstIndex);
  469. updateNavigationBar(firstIndex);
  470. treeViewDown->expand(firstIndex); // 展开第一个目录
  471. QStandardItem *firstItem = downModel->itemFromIndex(firstIndex);
  472. QVariant data = firstItem->data(Qt::UserRole);
  473. if (data.canConvert<QJsonObject>()) {
  474. QJsonObject thirdLevelObj = data.toJsonObject();
  475. if (thirdLevelObj.contains("isThirdLevel") && thirdLevelObj["isThirdLevel"].toBool()) {
  476. loadButtonConfigForThirdLevel(thirdLevelObj);
  477. }
  478. }
  479. }
  480. }
  481. }
  482. // 使用 singleShot 确保在所有展开操作完成后更新分隔线
  483. QTimer::singleShot(0, this, &TreeViewManager::updateSeparatorLine);
  484. }
  485. //记录选中的路径
  486. void TreeViewManager::addVisitedPath(const QStringList &path)
  487. {
  488. QString joined = path.join("/");
  489. if (!visitedPaths.contains(joined)) {
  490. visitedPaths.insert(joined);
  491. qDebug() << "记录[选中]路径:" << joined;
  492. }
  493. }
  494. //记录展开的路径
  495. void TreeViewManager::addExpandedPath(const QStringList &path)
  496. {
  497. QString joined = path.join("/");
  498. if (!expandedPaths.contains(joined)) {
  499. expandedPaths.insert(joined);
  500. qDebug() << "记录[展开]路径:" << joined;
  501. }
  502. }
  503. //移除展开的路径
  504. void TreeViewManager::removeExpandedPath(const QStringList &path)
  505. {
  506. QString joinedPath = path.join("/");
  507. if (expandedPaths.contains(joinedPath)) {
  508. expandedPaths.remove(joinedPath);
  509. qDebug() << "移除展开路径:" << joinedPath;
  510. }
  511. }
  512. // 构建节点的完整路径
  513. QStringList TreeViewManager::buildItemPath(QStandardItem *item)
  514. {
  515. QStringList path;
  516. QStandardItem *currentItem = item;
  517. while (currentItem) {
  518. path.prepend(currentItem->text());
  519. currentItem = currentItem->parent();
  520. }
  521. return path;
  522. }
  523. // 根据路径查找对应的节点
  524. QModelIndex TreeViewManager::findItemByPath(const QStringList &path)
  525. {
  526. if (path.isEmpty()) return QModelIndex();
  527. QStandardItem *currentItem = downModel->invisibleRootItem();
  528. QModelIndex currentIndex;
  529. for (const QString &part : path) {
  530. bool found = false;
  531. for (int i = 0; i < currentItem->rowCount(); ++i) {
  532. QStandardItem *child = currentItem->child(i);
  533. if (child->text() == part) {
  534. currentIndex = downModel->indexFromItem(child);
  535. currentItem = child;
  536. found = true;
  537. break;
  538. }
  539. }
  540. if (!found) {
  541. qWarning() << "未找到路径部分:" << part;
  542. return QModelIndex();
  543. }
  544. }
  545. return currentIndex;
  546. }
  547. bool TreeViewManager::eventFilter(QObject *watched, QEvent *event)
  548. {
  549. // 拦截 Paint 事件
  550. if (watched == treeViewDown->viewport() && event->type() == QEvent::Paint)
  551. {
  552. // 进行默认绘制“树节点”都画出来
  553. bool handled = QWidget::eventFilter(watched, event);
  554. //使用 QPainter 叠加画“拐角线”
  555. QPainter painter(treeViewDown->viewport());
  556. if (!painter.isActive()) {
  557. qWarning() << "Painter not active";
  558. return handled;
  559. }
  560. painter.save();
  561. painter.setPen(QPen(Qt::gray, 1, Qt::DashLine)); // 灰色、1px 宽、虚线
  562. // 调用一个递归或遍历函数,把所有节点的父->子线、兄弟延续竖线都画上
  563. paintAllBranches(QModelIndex(), painter);
  564. painter.restore();
  565. return handled;
  566. }
  567. // 其余事件交给父类默认处理
  568. return QWidget::eventFilter(watched, event);
  569. }
  570. void TreeViewManager::paintAllBranches(const QModelIndex &parentIndex, QPainter &painter)
  571. {
  572. int rowCount = downModel->rowCount(parentIndex);
  573. for(int i = 0; i < rowCount; ++i)
  574. {
  575. // 当前子节点
  576. QModelIndex childIndex = downModel->index(i, 0, parentIndex);
  577. if (!childIndex.isValid()) continue;
  578. // 1) 父->子拐角线
  579. drawParentChildLine(childIndex, painter);
  580. // 2) 兄弟延续竖线(如果本节点不是最后一个兄弟,就在拐点列画条向下的线)
  581. if (i < rowCount - 1) {
  582. drawSiblingLine(childIndex, painter);
  583. }
  584. // 3) 递归处理子节点
  585. paintAllBranches(childIndex, painter);
  586. }
  587. }
  588. //在“父节点 -> 子节点”间画一条“L”型拐角线,调整横向线段的长度
  589. void TreeViewManager::drawParentChildLine(const QModelIndex &childIndex, QPainter &painter)
  590. {
  591. QModelIndex parentIndex = childIndex.parent();
  592. if (!parentIndex.isValid()) {
  593. // “顶层节点”定义一个固定的起点 (rootX, rootY)
  594. int indent = treeViewDown->indentation();
  595. int depth = 0; // 顶层节点深度为0
  596. int branchX = (depth + 1) * indent - indent / 2; // 计算 branchX 基于缩进和深度
  597. // 定义 rootY 为节点中心 Y
  598. QRect childRect = treeViewDown->visualRect(childIndex);
  599. if (!childRect.isValid())
  600. return;
  601. int rootY = childRect.center().y();
  602. // 定义横向偏移量
  603. const int hOffset = -20;
  604. // 绘制竖线
  605. painter.drawLine(QPoint(branchX, rootY),
  606. QPoint(branchX, childRect.center().y()));
  607. // 计算新的横线终点
  608. int newX = childRect.left() + hOffset;
  609. // 绘制横线
  610. painter.drawLine(QPoint(branchX, childRect.center().y()),
  611. QPoint(newX, childRect.center().y()));
  612. return;
  613. }
  614. QRect parentRect = treeViewDown->visualRect(parentIndex);
  615. QRect childRect = treeViewDown->visualRect(childIndex);
  616. if (!parentRect.isValid() || !childRect.isValid()) {
  617. // 父或子超出可视区域
  618. return;
  619. }
  620. int pMidY = parentRect.center().y();
  621. int cMidY = childRect.center().y();
  622. // 计算节点深度
  623. int depth = 0;
  624. QModelIndex p = parentIndex;
  625. while (p.isValid()) {
  626. depth++;
  627. p = p.parent();
  628. }
  629. int indent = treeViewDown->indentation();
  630. int branchX = depth * indent - indent / 2;
  631. // branchX 不超出视图范围
  632. branchX = std::max(branchX, 0);
  633. // 定义横向偏移量
  634. const int hOffset = -15;
  635. // 绘制竖线
  636. painter.drawLine(QPoint(branchX, pMidY), QPoint(branchX, cMidY));
  637. // 计算新的横线终点
  638. int newX = childRect.left() + hOffset;
  639. // 绘制横线
  640. painter.drawLine(QPoint(branchX, cMidY), QPoint(newX, cMidY));
  641. }
  642. // 节点下面还有兄弟,则在拐点列那里继续往下画竖线
  643. void TreeViewManager::drawSiblingLine(const QModelIndex &childIndex, QPainter &painter)
  644. {
  645. QModelIndex parentIndex = childIndex.parent();
  646. if (!parentIndex.isValid()) {
  647. return; // 没有父节点
  648. }
  649. // 下一个兄弟
  650. int row = childIndex.row();
  651. int lastRow = downModel->rowCount(parentIndex) - 1;
  652. if (row >= lastRow) {
  653. return; // 说明是最后一个兄弟,不用画延伸线
  654. }
  655. QModelIndex nextSibling = downModel->index(row + 1, 0, parentIndex);
  656. QRect currRect = treeViewDown->visualRect(childIndex);
  657. QRect nextRect = treeViewDown->visualRect(nextSibling);
  658. if (!currRect.isValid() || !nextRect.isValid()) {
  659. return;
  660. }
  661. // 计算节点深度
  662. int depth = 0;
  663. QModelIndex p = parentIndex;
  664. while (p.isValid()) {
  665. depth++;
  666. p = p.parent();
  667. }
  668. int indent = treeViewDown->indentation();
  669. int branchX = depth * indent - indent / 2;
  670. // 确保 branchX 不超出视图范围
  671. branchX = std::max(branchX, 0);
  672. // 从当前节点底部向下延伸到下一个兄弟节点顶部
  673. int startY = currRect.bottom();
  674. int endY = nextRect.top();
  675. painter.drawLine(QPoint(branchX, startY), QPoint(branchX, endY));
  676. }
  677. // 一次性读取 configPaths 里的所有 JSON 文件并解析存到 m_configCache
  678. void TreeViewManager::preloadAllConfigs(const QStringList &configPaths)
  679. {
  680. m_configCache.clear(); // 先清空
  681. for (const QString &path : configPaths) {
  682. QFile file(path);
  683. if (!file.exists()) {
  684. qWarning() << "preloadAllConfigs: JSON 文件不存在:" << path;
  685. continue;
  686. }
  687. if (!file.open(QIODevice::ReadOnly)) {
  688. qWarning() << "preloadAllConfigs: 无法打开 JSON 文件:" << path;
  689. continue;
  690. }
  691. QByteArray data = file.readAll();
  692. file.close();
  693. QJsonDocument doc = QJsonDocument::fromJson(data);
  694. if (doc.isNull() || !doc.isObject()) {
  695. qWarning() << "preloadAllConfigs: JSON 文件格式错误:" << path;
  696. continue;
  697. }
  698. // 以 fileName 作为 key,比如 "home_config.json"
  699. QString key = QFileInfo(path).fileName();
  700. m_configCache.insert(key, doc);
  701. qDebug() << "preloadAllConfigs: 已缓存配置文件" << key;
  702. }
  703. }
  704. //切换配置文件
  705. void TreeViewManager::switchConfig(const QString &configKey)
  706. {
  707. qDebug() << "切换配置文件到:" << configKey;
  708. // 先保存当前配置的状态
  709. saveVisitedPaths();
  710. // 清理控件列表和焦点索引
  711. m_fieldWidgets.clear();
  712. m_currentFieldIndex = -1;
  713. // 检查 configKey 是否存在于缓存中
  714. if (!m_configCache.contains(configKey)) {
  715. qWarning() << "switchConfig: 未找到 configKey =" << configKey << ",无法切换";
  716. return;
  717. }
  718. // 获取对应的 QJsonDocument
  719. QJsonDocument doc = m_configCache.value(configKey);
  720. // 清理上一个配置产生的横线
  721. clearAllSeparators();
  722. // 隐藏三级目录菜单内容
  723. clearThirdLevelMenu();
  724. //加载新的 JSON 配置
  725. loadJsonDoc(doc, configKey);
  726. }
  727. //清理并隐藏三级目录菜单内容
  728. void TreeViewManager::clearThirdLevelMenu()
  729. {
  730. // 遍历所有子控件,找到标题为 "字段展示" 的窗口并关闭
  731. foreach (QObject *child, widget2->children()) {
  732. QWidget *childWidget = qobject_cast<QWidget*>(child);
  733. if (childWidget && childWidget->windowTitle() == "字段展示") {
  734. qDebug() << "关闭现有的字段展示窗口";
  735. childWidget->close();
  736. }
  737. }
  738. // 显示主目录树和分隔线
  739. treeViewDown->show();
  740. for (auto separator : firstLevelSeparators) {
  741. separator->show();
  742. }
  743. }
  744. void TreeViewManager::loadJsonDoc(const QJsonDocument &doc, const QString &configFilePath)
  745. {
  746. currentConfigFilePath = configFilePath;
  747. //qDebug() << "[DEBUG] loadJsonDoc 完成,对应 configKey =" << configFilePath;
  748. m_blockItemChanged = true;
  749. //qDebug() << "Loading JSON config, m_blockItemChanged set to true";
  750. // 清空
  751. downModel->clear();
  752. firstLevelSeparators.clear();
  753. visitedPaths.clear();
  754. expandedPaths.clear();
  755. // 构建树
  756. if (!doc.isObject()) {
  757. qWarning() << "loadJsonDoc: doc 不是对象结构";
  758. return;
  759. }
  760. QJsonObject rootObj = doc.object();
  761. buildTree(rootObj, downModel->invisibleRootItem());
  762. loadVisitedPaths();// 恢复 记忆路径 + 复选框状态
  763. m_blockItemChanged = false;
  764. // qDebug() << "Finished loading JSON config, m_blockItemChanged set to false";
  765. }
  766. //目录树样式
  767. void TreeViewManager::applyCustomStyles() {
  768. treeViewDown->setStyleSheet(R"(
  769. /* 设置分支图标 */
  770. QTreeView::branch:closed:has-children {
  771. border-image: none;
  772. image: url(:/images/home_add.png);
  773. }
  774. QTreeView::branch:open:has-children {
  775. border-image: none;
  776. image: url(:/images/home_minus.png);
  777. }
  778. /* 设置多选框 */
  779. QTreeView::indicator:unchecked {
  780. image: url(:/images/home_NotSelecte.png);
  781. }
  782. QTreeView::indicator:checked {
  783. image: url(:/images/home_selected.png);
  784. }
  785. /* 背景透明,行间距 */
  786. QTreeView {
  787. background: transparent;
  788. border: none;
  789. }
  790. /* 设置项目选中的背景色 */
  791. QTreeView::item:selected {
  792. background-color: #A9B4FF;
  793. }
  794. /* 设置项目的行间距 */
  795. QTreeView::item {
  796. padding-top: 5px; /* 上边距 */
  797. padding-bottom: 5px; /* 下边距 */
  798. }
  799. )");
  800. }
  801. //构建目录树
  802. void TreeViewManager::buildTree(const QJsonObject &jsonObj, QStandardItem *parent)
  803. {
  804. for (auto it = jsonObj.begin(); it != jsonObj.end(); ++it) {
  805. // 跳过不需要的字段
  806. if (it.key() == "isThirdLevel" || it.key() == "separator" || it.key() == "buttons") {
  807. continue;
  808. }
  809. QStandardItem *item = new QStandardItem(it.key());
  810. item->setCheckable(true); // 添加多选框
  811. item->setCheckState(Qt::Unchecked); // 默认状态为未选中
  812. parent->appendRow(item);
  813. //qDebug() << "创建目录项:" << it.key();
  814. if (it.value().isObject()) {
  815. QJsonObject childObj = it.value().toObject();
  816. if (childObj.contains("isThirdLevel") && childObj["isThirdLevel"].toBool()) {
  817. // 是三级目录,存储整个对象到 UserRole
  818. item->setData(childObj, Qt::UserRole);
  819. //qDebug() << "目录项为三级目录:" << it.key();
  820. }
  821. else {
  822. // 递归处理二级目录
  823. buildTree(childObj, item);
  824. }
  825. }
  826. // 如果是一级目录,检查是否需要创建分隔线
  827. if (parent == downModel->invisibleRootItem()) {
  828. QJsonObject currentObj = it.value().toObject();
  829. bool hasSeparator = currentObj.contains("separator") && currentObj["separator"].toBool();
  830. if (hasSeparator) {
  831. QFrame *separator = createUnifiedSeparator(widget2, 2);
  832. separator->hide(); // 初始隐藏
  833. firstLevelSeparators.insert(item, separator);
  834. //qDebug() << "为一级目录创建统一分隔线:" << it.key();
  835. }
  836. }
  837. }
  838. // 在所有子项添加完成后,更新父项的复选框状态
  839. if (parent != downModel->invisibleRootItem()) {
  840. updateParentItems(parent);
  841. }
  842. }
  843. //导航栏动态更新
  844. void TreeViewManager::updateNavigationBar(const QModelIndex &index)
  845. {
  846. //如果当前 index 无效,尝试从 QSettings 读取“上次导航路径”并恢复
  847. if (!index.isValid()) {
  848. //qWarning() << "updateNavigationBar(): 当前 index 无效,尝试从 QSettings 恢复上次导航";
  849. QSettings settings("RunCloudTech", "David");
  850. settings.beginGroup("TreeViewNav");
  851. QStringList lastNavPath = settings.value("lastNavPath").toStringList();
  852. settings.endGroup();
  853. if (!lastNavPath.isEmpty()) {
  854. // 找到对应的节点索引
  855. QModelIndex savedIndex = findItemByPath(lastNavPath);
  856. if (savedIndex.isValid()) {
  857. qDebug() << "成功从 QSettings 恢复导航路径:" << lastNavPath
  858. << ",对应项:" << downModel->itemFromIndex(savedIndex)->text();
  859. updateNavigationBar(savedIndex); // 再次调用本函数,更新导航栏
  860. } else {
  861. qWarning() << "QSettings 里存的路径无法找到对应节点,无法恢复。";
  862. }
  863. } else {
  864. qWarning() << "QSettings 里没有存任何导航路径,或为空。";
  865. }
  866. return;
  867. }
  868. QStandardItem *item = downModel->itemFromIndex(index);
  869. if (!item) {
  870. qWarning() << "导航栏更新失败:未找到对应项";
  871. return;
  872. }
  873. //qDebug() << "导航栏更新,目录项:" << item->text();
  874. // 如果导航栏已有布局,先清理
  875. if (navigationWidget->layout()) {
  876. QLayoutItem *child;
  877. while ((child = navigationWidget->layout()->takeAt(0)) != nullptr) {
  878. if (child->widget()) {
  879. child->widget()->deleteLater();
  880. }
  881. delete child;
  882. }
  883. delete navigationWidget->layout();
  884. }
  885. // 构建路径列表,从当前项回溯到根节点
  886. QList<QStandardItem *> path;
  887. QStandardItem *temp = item;
  888. while (temp) {
  889. if (temp->text() != "isThirdLevel") { // 排除 "isThirdLevel" 标识
  890. path.prepend(temp); // 从根节点开始
  891. }
  892. temp = temp->parent();
  893. }
  894. qDebug() << "导航路径:" << [path]() {
  895. QStringList pathNames;
  896. for (QStandardItem *p : path) {
  897. pathNames.append(p->text());
  898. }
  899. return pathNames.join(" -> ");
  900. }();
  901. QVBoxLayout *newLayout = new QVBoxLayout; // 创建新的导航栏布局
  902. newLayout->setContentsMargins(0, 0, 3, 3);
  903. newLayout->setSpacing(0);
  904. // 确保显示三行
  905. for (int i = 0; i < 3; ++i) {
  906. QLabel *label = new QLabel;
  907. if (i < path.size()) {
  908. QString text = path[i]->text();
  909. if (i == 1) text = " " + text; // 二级目录缩进
  910. if (i == 2) text = " " + text; // 三级目录缩进
  911. label->setText(text);
  912. } else {
  913. label->setText(""); // 填充空白行
  914. }
  915. QFont font = label->font(); // 设置字体和样式
  916. font.setPointSize(14);
  917. font.setFamily("思源黑体");
  918. font.setBold(true); //设置文字为粗体
  919. font.setLetterSpacing(QFont::PercentageSpacing,105); //设置字间距100%为默认
  920. label->setFont(font);
  921. label->setFixedHeight(navigationWidget->height() / 3);
  922. newLayout->addWidget(label);
  923. }
  924. // 设置布局并更新导航栏
  925. navigationWidget->setLayout(newLayout);
  926. navigationWidget->update();
  927. //qDebug() << "导航栏更新完成:" << path.size() << "项";
  928. //当前路径存到 QSettings,供下次启动恢复
  929. QStringList pathStrings;
  930. for (QStandardItem *p : path) {
  931. pathStrings << p->text();
  932. }
  933. QSettings settings("RunCloudTech", "David");
  934. settings.beginGroup("TreeViewNav");
  935. settings.setValue("lastNavPath", pathStrings);
  936. settings.endGroup();
  937. //qDebug() << "已将当前导航路径写入 QSettings:" << pathStrings;
  938. }
  939. //加载并显示三级目录对应按钮配置信息
  940. void TreeViewManager::loadButtonConfigForThirdLevel(const QJsonObject &thirdLevelObj)
  941. {
  942. if (!m_originalWnd) {
  943. qWarning() << "OriginalWnd 指针为空,无法加载按钮配置";
  944. return;
  945. }
  946. if (!thirdLevelObj.contains("buttons")) {
  947. qWarning() << "三级目录配置中不包含 'buttons' 字段";
  948. return;
  949. }
  950. QJsonArray buttonsArray = thirdLevelObj.value("buttons").toArray();
  951. // 获取 widget_left
  952. QWidget* widgetLeft = m_originalWnd->getWidgetLeft();
  953. if (!widgetLeft) {
  954. qWarning() << "无法访问 widget_left";
  955. return;
  956. }
  957. // 清空 widget_left 中由 loadButtonConfigForThirdLevel 创建的按钮
  958. QList<QPushButton*> existingButtons = widgetLeft->findChildren<QPushButton*>();
  959. for (QPushButton* button : existingButtons) {
  960. if (button->objectName().startsWith("thirdLevelBtn_")) { // 仅删除特定按钮
  961. button->deleteLater();
  962. }
  963. }
  964. // 使用绝对定位创建按钮
  965. for (int i = 0; i < buttonsArray.size() && i < 12; ++i) {
  966. QJsonObject buttonObj = buttonsArray[i].toObject();
  967. QString buttonId = buttonObj.value("id").toString();
  968. QString buttonIcon = buttonObj.value("icon").toString();
  969. QString buttonText = buttonObj.value("text").toString();
  970. bool isEnabled = buttonObj.value("enabled").toBool();
  971. // 创建按钮
  972. QPushButton *button = new QPushButton(widgetLeft);
  973. button->setObjectName("thirdLevelBtn_" + buttonId); // 设置带前缀的对象名称
  974. //设置按钮的样式,调整图标和文本的位置
  975. button->setStyleSheet(R"(
  976. QPushButton {
  977. position: absolute;
  978. border-radius: 6px;
  979. opacity: 1;
  980. background: #CBD0FF;
  981. border: none;
  982. }
  983. QPushButton:hover {
  984. background-color: #A9B4FF; /* 鼠标悬停效果 */
  985. }
  986. )");
  987. // 设置按钮的位置和大小
  988. int x = 16;
  989. int y = 245 + i * (48 + 13); // 第一个按钮 y=245,后续每个按钮间隔13px
  990. button->setGeometry(x, y, 158, 48);
  991. // 设置按钮的可见性,根据 "enabled" 字段显示或隐藏按钮
  992. button->setVisible(isEnabled);
  993. // 创建图标标签
  994. QLabel *iconLabel = new QLabel(button);
  995. iconLabel->setPixmap(QIcon(buttonIcon).pixmap(16, 16));
  996. iconLabel->setGeometry(10, 16, 16, 16); // 图标距离左边10px,顶部16px
  997. iconLabel->setFixedSize(16, 16);
  998. iconLabel->setStyleSheet("background-color: transparent;");
  999. iconLabel->setVisible(isEnabled); // 根据按钮的可见性设置图标的可见性
  1000. // 创建文本标签
  1001. QLabel *textLabel = new QLabel(buttonText, button);
  1002. textLabel->setGeometry(34, 0, 90, 48); // 文本距离左边34px,占用剩余空间
  1003. textLabel->setWordWrap(true); // 允许换行
  1004. textLabel->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
  1005. //textLabel->setStyleSheet("background-color: transparent;");
  1006. textLabel->setStyleSheet(R"(
  1007. QLabel {
  1008. background: transparent;
  1009. font-family: "思源黑体";
  1010. font-size: 14px;
  1011. font-weight: 500;
  1012. color: #4E51CE;
  1013. }
  1014. )");
  1015. textLabel->setVisible(isEnabled); // 根据按钮的可见性设置文本的可见性
  1016. // 创建 F1-F12 标签
  1017. QString fLabelText = QString("F%1").arg(i + 1); // F1, F2, ..., F12
  1018. QLabel *fLabel = new QLabel(fLabelText, button);
  1019. fLabel->setFixedSize(21, 16); // 设置大小为21x16
  1020. fLabel->setAlignment(Qt::AlignCenter);
  1021. fLabel->setStyleSheet(R"(
  1022. QLabel {
  1023. background-color: transparent;
  1024. color: #2A7ED8;
  1025. font-size: 12px;
  1026. font-weight: bold;
  1027. }
  1028. )");
  1029. // 设置标签的位置
  1030. int fX = 134;
  1031. int fY = 2;
  1032. fLabel->setGeometry(fX, fY, 14, 16);
  1033. fLabel->setVisible(isEnabled); // 根据按钮的可见性设置标签的可见性
  1034. //qDebug() << "创建按钮:" << buttonId << ", 文本:" << buttonText << ", 图标:" << buttonIcon << ", 启用:" << isEnabled;
  1035. }
  1036. }
  1037. //三级目录字段窗口
  1038. void TreeViewManager::displayThirdLevelFields(const QJsonObject &fields)
  1039. {
  1040. if (fields.isEmpty()) {
  1041. qWarning() << "字段数据为空,无法显示";
  1042. return;
  1043. }
  1044. // qDebug() << "显示的字段数据:" << fields;
  1045. // 检查是否为三级目录
  1046. if (!fields.contains("isThirdLevel") || !fields["isThirdLevel"].toBool()) {
  1047. qWarning() << "不是三级目录,跳过按钮加载";
  1048. return;
  1049. }
  1050. // 隐藏目录树
  1051. treeViewDown->hide();
  1052. // 隐藏所有横线
  1053. for (auto separator : firstLevelSeparators) {
  1054. separator->hide();
  1055. }
  1056. // 加载按钮配置
  1057. loadButtonConfigForThirdLevel(fields);
  1058. // 清理之前的控件列表
  1059. m_fieldWidgets.clear();
  1060. m_currentFieldIndex = -1;
  1061. // 检查是否已存在字段窗口,防止重复创建
  1062. foreach (QObject *child, widget2->children()) {
  1063. QWidget *childWidget = qobject_cast<QWidget*>(child);
  1064. if (childWidget && childWidget->windowTitle() == "字段展示") {
  1065. // qDebug() << "字段窗口已存在,关闭旧窗口";
  1066. childWidget->close();
  1067. }
  1068. }
  1069. // 创建一个新的 “字段展示” 窗口
  1070. QWidget *fieldWindow = new QWidget(widget2);
  1071. fieldWindow->setWindowTitle("字段展示");
  1072. fieldWindow->setGeometry(treeViewDown->geometry());
  1073. //创建一个滚动区域 QScrollArea
  1074. QScrollArea *scrollArea = new QScrollArea(fieldWindow);
  1075. scrollArea->setWidgetResizable(true); // 内容自适应大小
  1076. scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded); // 保留水平滚动条
  1077. //qDebug() << "QScrollArea 已创建,水平滚动条策略设置为 ScrollBarAsNeeded";
  1078. //创建滚动容器 scrollWidget,并设置垂直布局
  1079. QWidget *scrollWidget = new QWidget;
  1080. scrollWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred); // 宽度跟随父窗口
  1081. QVBoxLayout *scrollLayout = new QVBoxLayout(scrollWidget);
  1082. scrollLayout->setSpacing(10); // 字段的垂直间距
  1083. scrollLayout->setContentsMargins(10, 10, 10, 10); // 减少边距
  1084. // 遍历 JSON 中的字段,生成控件
  1085. for (auto it = fields.begin(); it != fields.end(); ++it) {
  1086. QString fieldName = it.key();
  1087. if (fieldName == "isThirdLevel") {
  1088. continue; // 跳过 isThirdLevel 字段
  1089. }
  1090. if (fieldName == "buttons") {
  1091. continue; // 跳过 buttons 字段
  1092. }
  1093. QJsonObject fieldConfig = it.value().toObject();
  1094. QString fieldType = fieldConfig["type"].toString();
  1095. // qDebug() << "处理字段:" << fieldName << "类型:" << fieldType;
  1096. // 每个字段一行:Label在左,控件在右
  1097. QHBoxLayout *fieldLayout = new QHBoxLayout;
  1098. fieldLayout->setSpacing(5); // 减少每行的水平间距
  1099. // 左侧:字段名
  1100. QLabel *label = new QLabel(fieldName);
  1101. label->setAlignment(Qt::AlignVCenter | Qt::AlignLeft);
  1102. label->setFixedHeight(24);
  1103. label->setMinimumWidth(120);
  1104. fieldLayout->addWidget(label);
  1105. // 在中间插入弹性伸缩,使后面的控件靠右
  1106. fieldLayout->addStretch(1);
  1107. // 根据 fieldType 创建相应控件
  1108. QWidget *rightWidget = new QWidget;
  1109. QHBoxLayout *rightLayout = new QHBoxLayout(rightWidget);
  1110. rightLayout->setContentsMargins(0, 0, 20, 0); // 控件右侧边距20
  1111. rightLayout->setSpacing(5); // 减少控件之间的间距
  1112. QWidget *createdWidget = nullptr; // 用于存储创建的控件
  1113. if (fieldType == "input") {
  1114. // 创建 QLineEdit 输入框
  1115. QLineEdit *lineEdit = new QLineEdit(fieldConfig["value"].toString());
  1116. lineEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); // 水平方向扩展
  1117. lineEdit->setFixedSize(120, 28); // 减少宽度和高度
  1118. lineEdit->setAlignment(Qt::AlignLeft); // 输入框左对齐
  1119. lineEdit->setStyleSheet(R"(
  1120. QLineEdit {
  1121. background: #FFFFFF;
  1122. border: 1px solid #BABBDC;
  1123. border-radius: 6px;
  1124. padding: 2px 5px;
  1125. }
  1126. )");
  1127. rightLayout->addWidget(lineEdit);
  1128. createdWidget = lineEdit; // 记录创建的控件
  1129. //qDebug() << "添加输入框:" << fieldName;
  1130. } else if (fieldType == "radio") {
  1131. // 创建一组 QRadioButton 单选框
  1132. QHBoxLayout *radioLayout = new QHBoxLayout;
  1133. radioLayout->setSpacing(5); // 减少单选按钮之间的间距
  1134. QButtonGroup *radioGroup = new QButtonGroup(rightWidget);
  1135. QString currentValue = fieldConfig["value"].toString();
  1136. QJsonArray options = fieldConfig["options"].toArray();
  1137. for (const QJsonValue &option : options) {
  1138. QRadioButton *radioButton = new QRadioButton(option.toString());
  1139. if (option.toString() == currentValue) {
  1140. radioButton->setChecked(true);
  1141. }
  1142. radioGroup->addButton(radioButton);
  1143. radioLayout->addWidget(radioButton);
  1144. m_fieldWidgets.append(radioButton); // 添加到控件列表
  1145. //qDebug() << "添加单选按钮:" << option.toString();
  1146. }
  1147. rightLayout->addLayout(radioLayout);
  1148. } else if (fieldType == "checkbox") {
  1149. // 创建 QCheckBox 复选框不显示文字
  1150. QCheckBox *checkBox = new QCheckBox;
  1151. checkBox->setChecked(fieldConfig["value"].toBool());
  1152. // 应用指定的样式
  1153. checkBox->setStyleSheet(R"(
  1154. QCheckBox::indicator {
  1155. width: 20px;
  1156. height: 20px;
  1157. }
  1158. QCheckBox::indicator:unchecked {
  1159. background-color: #FFFFFF;
  1160. border: 1px solid #BABBDC;
  1161. border-radius: 2px;
  1162. }
  1163. QCheckBox::indicator:checked {
  1164. image: url(:/images/three_Selecte.png);
  1165. }
  1166. QCheckBox {
  1167. /* 取消显示文本 */
  1168. spacing: 0px;
  1169. }
  1170. )");
  1171. rightLayout->addWidget(checkBox);
  1172. createdWidget = checkBox; // 记录创建的控件
  1173. //qDebug() << "添加复选框:" << fieldName;
  1174. } else if (fieldType == "dropdown") {
  1175. // 创建 QComboBox 下拉框
  1176. QComboBox *comboBox = new QComboBox;
  1177. comboBox->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); // 固定宽度
  1178. comboBox->setFixedSize(120, 28);
  1179. comboBox->setStyleSheet(R"(
  1180. QComboBox {
  1181. background: #FFFFFF;
  1182. border: 1px solid #BABBDC;
  1183. border-radius: 6px;
  1184. padding: 2px 5px;
  1185. }
  1186. QComboBox::drop-down {
  1187. width: 20px;
  1188. }
  1189. )");
  1190. QJsonArray options = fieldConfig["options"].toArray();
  1191. for (const QJsonValue &option : options) {
  1192. comboBox->addItem(option.toString());
  1193. }
  1194. comboBox->setCurrentText(fieldConfig["value"].toString());
  1195. rightLayout->addWidget(comboBox);
  1196. createdWidget = comboBox; // 记录创建的控件
  1197. //qDebug() << "添加下拉框:" << fieldName;
  1198. } else if (fieldType == "time") {
  1199. // 创建 QTimeEdit 时间选择框
  1200. QTimeEdit *timeEdit = new QTimeEdit;
  1201. timeEdit->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); // 固定宽度
  1202. timeEdit->setFixedSize(120, 28); // 减少宽度和高度
  1203. timeEdit->setStyleSheet(R"(
  1204. QTimeEdit {
  1205. background: #FFFFFF;
  1206. border: 1px solid #BABBDC;
  1207. border-radius: 6px;
  1208. padding: 2px 5px;
  1209. }
  1210. )");
  1211. timeEdit->setDisplayFormat("HH:mm:ss");
  1212. timeEdit->setTime(QTime::fromString(fieldConfig["value"].toString(), "HH:mm:ss"));
  1213. rightLayout->addWidget(timeEdit);
  1214. createdWidget = timeEdit; // 记录创建的控件
  1215. //qDebug() << "添加时间选择框:" << fieldName;
  1216. } else if (fieldType == "switch") {
  1217. // 创建 QCheckBox 开关控件,并添加文字标签“开”和“关”
  1218. QWidget *switchContainer = new QWidget;
  1219. QHBoxLayout *switchLayout = new QHBoxLayout(switchContainer);
  1220. switchLayout->setSpacing(5); // 减少开关和标签之间的间距
  1221. switchLayout->setContentsMargins(0, 0, 0, 0);
  1222. QCheckBox *switchBox = new QCheckBox;
  1223. switchBox->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
  1224. switchBox->setFixedSize(30, 30); // 调整开关大小
  1225. switchBox->setStyleSheet(R"(
  1226. QCheckBox::indicator {
  1227. width: 30px;
  1228. height: 30px;
  1229. }
  1230. QCheckBox::indicator:unchecked {
  1231. background-color: #BABBDC;
  1232. border-radius: 6px;
  1233. }
  1234. QCheckBox::indicator:checked {
  1235. background-color: #4CAF50;
  1236. border-radius: 6px;
  1237. }
  1238. )");
  1239. // 设置初始状态
  1240. QString switchValue = fieldConfig["value"].toString();
  1241. if (switchValue == "on") {
  1242. switchBox->setChecked(true);
  1243. } else {
  1244. switchBox->setChecked(false);
  1245. }
  1246. // 添加文字标签
  1247. QLabel *switchLabel = new QLabel(switchBox->isChecked() ? "开" : "关");
  1248. switchLabel->setAlignment(Qt::AlignVCenter | Qt::AlignLeft);
  1249. switchLabel->setStyleSheet("font-size: 14px;"); //调整字体大小
  1250. // 连接开关状态改变信号以更新标签文字
  1251. connect(switchBox, &QCheckBox::stateChanged, [switchLabel](int state){
  1252. if (state == Qt::Checked) {
  1253. switchLabel->setText("开");
  1254. } else {
  1255. switchLabel->setText("关");
  1256. }
  1257. });
  1258. switchLayout->addWidget(switchBox);
  1259. switchLayout->addWidget(switchLabel);
  1260. rightLayout->addWidget(switchContainer);
  1261. createdWidget = switchBox; // 将开关控件添加到控件列表
  1262. //qDebug() << "添加开关控件和标签:" << fieldName;
  1263. } else if (fieldType == "combined") {
  1264. // 组合控件(包含 QLineEdit 和两个 QToolButton)
  1265. QJsonObject combinedValue = fieldConfig["value"].toObject();
  1266. // 确保必要的字段存在
  1267. if (!combinedValue.contains("inputValue") ||
  1268. !combinedValue.contains("moduleButton") ||
  1269. !combinedValue.contains("axisButton")) {
  1270. //qWarning() << "组合控件的 value 字段不完整:" << fieldName;
  1271. continue;
  1272. }
  1273. QLineEdit *comboInput = new QLineEdit(combinedValue["inputValue"].toString());
  1274. comboInput->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); // 水平方向扩展
  1275. comboInput->setFixedHeight(28); // 减少高度
  1276. comboInput->setStyleSheet(R"(
  1277. QLineEdit {
  1278. background: #FFFFFF;
  1279. border: 1px solid #BABBDC;
  1280. border-radius: 5px;
  1281. padding: 2px 5px;
  1282. }
  1283. )");
  1284. m_fieldWidgets.append(comboInput);
  1285. //qDebug() << "添加组合控件的输入框:" << fieldName;
  1286. // 创建一个容器 widget 来包含组合控件
  1287. QWidget *combinedWidget = new QWidget;
  1288. QHBoxLayout *combinedLayout = new QHBoxLayout(combinedWidget);
  1289. combinedLayout->setSpacing(5); // 减少控件之间的间距
  1290. combinedLayout->setContentsMargins(0, 0, 0, 0);
  1291. // “模组” QToolButton
  1292. QToolButton *moduleButton = new QToolButton(combinedWidget); // 设置父级
  1293. moduleButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
  1294. moduleButton->setFixedSize(80, 28); // 调整按钮大小
  1295. moduleButton->setStyleSheet(R"(
  1296. QToolButton {
  1297. background: #FFFFFF;
  1298. border: 1px solid #BABBDC;
  1299. border-radius: 5px;
  1300. padding: 0px; /* 移除默认内边距 */
  1301. text-align: center; /* 使文本居中 */
  1302. }
  1303. QToolButton::menu-indicator {
  1304. image: none;
  1305. }
  1306. QToolButton::checked {
  1307. background: #4CAF50; /* 高亮颜色 */
  1308. }
  1309. /* 强制文本居中 */
  1310. QToolButton::hover {
  1311. qproperty-textAlignment: 'AlignCenter';
  1312. }
  1313. )");
  1314. moduleButton->setText("模组");
  1315. moduleButton->setCheckable(true);
  1316. moduleButton->setPopupMode(QToolButton::InstantPopup);
  1317. m_fieldWidgets.append(moduleButton);
  1318. // “轴向” QToolButton
  1319. QToolButton *axisButton = new QToolButton(combinedWidget); // 设置父级
  1320. axisButton->setObjectName("axisButton"); // 为轴向按钮设置对象名以便查找
  1321. axisButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
  1322. axisButton->setFixedSize(80, 28); // 调整按钮大小
  1323. axisButton->setStyleSheet(R"(
  1324. QToolButton {
  1325. background: #FFFFFF;
  1326. border: 1px solid #BABBDC;
  1327. border-radius: 5px;
  1328. padding: 0px; /* 移除默认内边距 */
  1329. text-align: center; /* 使文本居中 */
  1330. }
  1331. QToolButton::menu-indicator {
  1332. image: none;
  1333. }
  1334. QToolButton::checked {
  1335. background: #4CAF50; /* 高亮颜色 */
  1336. }
  1337. /* 强制文本居中 */
  1338. QToolButton::hover {
  1339. qproperty-textAlignment: 'AlignCenter';
  1340. }
  1341. )");
  1342. axisButton->setText("轴向");
  1343. axisButton->setCheckable(true);
  1344. axisButton->setPopupMode(QToolButton::InstantPopup);
  1345. axisButton->setEnabled(false); // 初始时禁用轴向按钮
  1346. m_fieldWidgets.append(axisButton);
  1347. // 创建 QMenu 用于“轴向”按钮,并设置样式表
  1348. QMenu *axisMenu = new QMenu(axisButton);
  1349. axisMenu->setStyleSheet(R"(
  1350. QMenu {
  1351. background-color: white;
  1352. color: black;
  1353. border: 1px solid #BABBDC;
  1354. }
  1355. QMenu::item:selected {
  1356. background-color: #4CAF50; /* 选中时背景颜色 */
  1357. color: white; /* 选中时字体颜色 */
  1358. }
  1359. )");
  1360. QJsonObject axisMap = fieldConfig["axisMap"].toObject();
  1361. // 初始化轴向菜单内容为空,等待选择模组后填充
  1362. axisButton->setMenu(axisMenu);
  1363. //qDebug() << "初始化轴向菜单为空:" << fieldName;
  1364. // 连接“轴向”按钮的菜单触发事件来更新“轴向”菜单
  1365. connect(axisMenu, &QMenu::triggered, this, [axisButton](QAction *action){
  1366. axisButton->setText(action->text());
  1367. axisButton->setChecked(true);
  1368. //qDebug() << "轴向选择:" << action->text();
  1369. });
  1370. // “模组” QMenu
  1371. QMenu *moduleMenu = new QMenu(moduleButton);
  1372. moduleMenu->setStyleSheet(R"(
  1373. QMenu {
  1374. background-color: white;
  1375. color: black;
  1376. border: 1px solid #BABBDC;
  1377. }
  1378. QMenu::item:selected {
  1379. background-color: #4CAF50; /* 选中时背景颜色 */
  1380. color: white; /* 选中时字体颜色 */
  1381. }
  1382. )");
  1383. QJsonArray moduleOptions = fieldConfig["moduleOptions"].toArray();
  1384. for (const QJsonValue &mod : moduleOptions) {
  1385. QAction *action = moduleMenu->addAction(mod.toString());
  1386. // 使用按值捕获,避免悬空指针
  1387. connect(action, &QAction::triggered, this, [moduleButton, action, fieldConfig,
  1388. combinedWidget, fieldName,
  1389. axisButton, axisMenu, axisMap]() {
  1390. moduleButton->setText(action->text());
  1391. moduleButton->setChecked(true);
  1392. //qDebug() << "模组选择:" << action->text();
  1393. // 更改背景颜色为高亮色
  1394. moduleButton->setStyleSheet("background: #4CAF50;");
  1395. axisButton->setStyleSheet("background: #4CAF50;");
  1396. //qDebug() << "组合控件颜色已切换为高亮颜色";
  1397. // 启用轴向按钮
  1398. axisButton->setEnabled(true);
  1399. // 动态填充轴向菜单
  1400. axisMenu->clear(); // 清空现有菜单项
  1401. if (axisMap.contains(action->text())) {
  1402. QJsonArray axes = axisMap[action->text()].toArray();
  1403. for (const QJsonValue &axis : axes) {
  1404. axisMenu->addAction(axis.toString());
  1405. }
  1406. //qDebug() << "轴向菜单已根据模组选择动态填充";
  1407. } else {
  1408. //qDebug() << "没有找到对应模组的轴向选项";
  1409. }
  1410. // 启动定时器
  1411. int timeoutSec = fieldConfig["timeout"].toInt(180); // 默认180秒(3分钟)
  1412. QTimer *timer = new QTimer(combinedWidget);
  1413. timer->setSingleShot(true);
  1414. connect(timer, &QTimer::timeout, combinedWidget, [moduleButton, axisButton]() {
  1415. // 恢复背景颜色为默认
  1416. moduleButton->setStyleSheet("background: #FFFFFF;");
  1417. axisButton->setStyleSheet("background: #FFFFFF;");
  1418. //qDebug() << "组合控件颜色已切换为默认(超时)";
  1419. });
  1420. timer->start(timeoutSec * 1000); // 修正为 timeoutSec * 1000 毫秒 (3分钟)
  1421. //qDebug() << "启动定时器," << timeoutSec << "秒后更改组合控件颜色";
  1422. });
  1423. }
  1424. moduleButton->setMenu(moduleMenu);
  1425. //qDebug() << "添加组合控件的模组按钮:" << fieldName;
  1426. // 创建一个容器 widget 来包含组合控件
  1427. combinedLayout->addWidget(comboInput);
  1428. combinedLayout->addWidget(moduleButton);
  1429. combinedLayout->addWidget(axisButton);
  1430. combinedLayout->addStretch(); // 保证组合控件紧凑排列
  1431. rightLayout->addWidget(combinedWidget);
  1432. //qDebug() << "组合控件已添加到布局:" << fieldName;
  1433. } else {
  1434. qWarning() << "未知字段类型:" << fieldType;
  1435. }
  1436. // 将创建的控件添加到控件列表
  1437. if (createdWidget) {
  1438. m_fieldWidgets.append(createdWidget);
  1439. }
  1440. // 将 rightWidget 加到 fieldLayout 的右侧
  1441. fieldLayout->addWidget(rightWidget);
  1442. // qDebug() << "将 rightWidget 添加到 fieldLayout";
  1443. // 将 fieldLayout 添加到 scrollLayout
  1444. scrollLayout->addLayout(fieldLayout);
  1445. //qDebug() << "添加字段布局:" << fieldName;
  1446. if (fieldConfig.contains("separator") && fieldConfig["separator"].toBool()) {
  1447. QFrame *separator = createUnifiedSeparator(scrollWidget, 2);
  1448. QHBoxLayout *separatorLayout = new QHBoxLayout;
  1449. separatorLayout->setContentsMargins(0, 5, 0, 5); // 5px 上下边距
  1450. separatorLayout->addWidget(separator);
  1451. scrollLayout->addLayout(separatorLayout);
  1452. separator->show(); // 显示分隔线
  1453. //qDebug() << "添加统一的动态分割线,并已显示";
  1454. }
  1455. }
  1456. // 弹性伸缩,让底部也有一定空隙
  1457. scrollLayout->addStretch();
  1458. // 将 scrollWidget 设置给 scrollArea
  1459. scrollArea->setWidget(scrollWidget);
  1460. // scrollArea 放到 fieldWindow 的主布局中
  1461. QVBoxLayout *mainLayout = new QVBoxLayout(fieldWindow);
  1462. mainLayout->setContentsMargins(0, 0, 0, 0);
  1463. mainLayout->addWidget(scrollArea);
  1464. // 显示窗口
  1465. fieldWindow->show();
  1466. //qDebug() << "三级目录字段窗口已打开";
  1467. }
  1468. void TreeViewManager::onButtonUpClicked()
  1469. {
  1470. if (m_fieldWidgets.isEmpty()) {
  1471. // 如果当前没有三级菜单打开或没有可导航的控件,执行原有的目录导航逻辑
  1472. // 您可以选择忽略按钮点击或恢复原有的目录导航逻辑(如导航至上一个目录项)
  1473. return;
  1474. }
  1475. if (m_currentFieldIndex <= 0) {
  1476. // 已经在第一个控件,无法向上移动
  1477. return;
  1478. }
  1479. // 移动到上一个控件
  1480. m_currentFieldIndex--;
  1481. QWidget *widgetToFocus = m_fieldWidgets[m_currentFieldIndex];
  1482. if (widgetToFocus) {
  1483. widgetToFocus->setFocus();
  1484. }
  1485. }
  1486. // 修改 onButtonDownClicked 方法
  1487. void TreeViewManager::onButtonDownClicked()
  1488. {
  1489. if (m_fieldWidgets.isEmpty()) {
  1490. // 如果当前没有三级菜单打开或没有可导航的控件,执行原有的目录导航逻辑
  1491. // 您可以选择忽略按钮点击或恢复原有的目录导航逻辑(如导航至下一个目录项)
  1492. return;
  1493. }
  1494. if (m_currentFieldIndex < 0) {
  1495. // 如果当前没有任何控件被聚焦,默认聚焦第一个控件
  1496. m_currentFieldIndex = 0;
  1497. }
  1498. else if (m_currentFieldIndex >= m_fieldWidgets.size() - 1) {
  1499. // 已经在最后一个控件,无法向下移动
  1500. return;
  1501. }
  1502. else {
  1503. // 移动到下一个控件
  1504. m_currentFieldIndex++;
  1505. }
  1506. QWidget *widgetToFocus = m_fieldWidgets[m_currentFieldIndex];
  1507. if (widgetToFocus) {
  1508. widgetToFocus->setFocus();
  1509. }
  1510. }
  1511. //设置所有按钮
  1512. void TreeViewManager::setupButton()
  1513. {
  1514. // 创建按钮并设置属性
  1515. auto createButton = [&](QPushButton*& button, QWidget* parent, const QString& iconPath, const QRect& geometry) {
  1516. button = new QPushButton(parent);
  1517. button->setIcon(QIcon(iconPath));
  1518. button->setGeometry(geometry);
  1519. button->setStyleSheet(R"(
  1520. QPushButton {
  1521. position: absolute;
  1522. border-radius: 6px;
  1523. opacity: 1;
  1524. background: #FFFFFF;
  1525. border: 1px solid #BABBDC;
  1526. }
  1527. QPushButton:hover {
  1528. background-color: #A9B4FF; /* 鼠标悬停效果 */
  1529. }
  1530. )");
  1531. button->show();
  1532. };
  1533. createButton(buttonOpenFile, widget2, ":/images/home_openFile.png", QRect(328, 16, 76, 30));
  1534. createButton(buttonUp, widget2, ":/images/home_up.png", QRect(408, 16, 36, 30));
  1535. createButton(buttonDown, widget2, ":/images/home_down.png", QRect(408, 50, 36, 30));
  1536. createButton(buttonLeft, widget2, ":/images/home_left.png", QRect(328, 50, 36, 30));
  1537. createButton(buttonRight, widget2, ":/images/home_right.png", QRect(368, 50, 36, 30));
  1538. }
  1539. void TreeViewManager::onButtonOpenFileClicked()
  1540. {
  1541. foreach (QObject *child, widget2->children()) {
  1542. QWidget *childWidget = qobject_cast<QWidget *>(child);
  1543. if (childWidget && childWidget->windowTitle() == "字段展示") {
  1544. //qDebug() << "关闭字段窗口:" << childWidget;
  1545. childWidget->close();
  1546. }
  1547. }
  1548. treeViewDown->show(); // 显示 treeViewDown
  1549. treeViewDown->collapseAll(); // 收起所有节点
  1550. treeViewDown->clearSelection();
  1551. // 清理控件列表和焦点索引
  1552. m_fieldWidgets.clear();
  1553. m_currentFieldIndex = -1;
  1554. // 展开一级目录
  1555. QStandardItem *rootItem = downModel->invisibleRootItem();
  1556. if (rootItem) {
  1557. for (int i = 0; i < rootItem->rowCount(); ++i) {
  1558. QStandardItem *childItem = rootItem->child(i);
  1559. if (childItem) {
  1560. treeViewDown->expand(downModel->indexFromItem(childItem));
  1561. // qDebug() << "展开一级目录:" << childItem->text();
  1562. }
  1563. }
  1564. }
  1565. // 重置导航栏
  1566. updateNavigationBar(QModelIndex());
  1567. // 更新横线的位置和可见性
  1568. QTimer::singleShot(0, this, &TreeViewManager::updateSeparatorLine);
  1569. //qDebug() << "成功返回到一级菜单";
  1570. }
  1571. void TreeViewManager::onButtonLeftClicked()
  1572. {
  1573. //qDebug() << "TreeViewManager: 返回上一级目录";
  1574. // 检查是否是三级目录字段展示窗口
  1575. bool isFieldWindowClosed = false;
  1576. // 更新横线的位置和可见性
  1577. QTimer::singleShot(0, this, &TreeViewManager::updateSeparatorLine);
  1578. foreach (QObject *child, widget2->children()) {
  1579. QWidget *childWidget = qobject_cast<QWidget *>(child);
  1580. if (childWidget && childWidget->windowTitle() == "字段展示") {
  1581. //qDebug() << "关闭三级目录窗口并返回到三级目录";
  1582. childWidget->close(); // 关闭字段展示窗口
  1583. isFieldWindowClosed = true;
  1584. }
  1585. }
  1586. if (isFieldWindowClosed) {
  1587. // 重新显示 treeViewDown 并恢复到对应的三级目录索引
  1588. treeViewDown->show();
  1589. QModelIndex currentIndex = treeViewDown->currentIndex();
  1590. if (currentIndex.isValid()) {
  1591. updateNavigationBar(currentIndex); // 更新导航栏
  1592. //qDebug() << "返回到三级目录索引:" << downModel->itemFromIndex(currentIndex)->text();
  1593. } else {
  1594. qWarning() << "当前索引无效,无法更新导航栏";
  1595. }
  1596. // 清理控件列表和焦点索引
  1597. m_fieldWidgets.clear();
  1598. m_currentFieldIndex = -1;
  1599. // 继续执行向上遍历的逻辑
  1600. QModelIndex parentIndex = currentIndex.parent();
  1601. if (parentIndex.isValid()) {
  1602. treeViewDown->setCurrentIndex(parentIndex);
  1603. treeViewDown->expand(parentIndex); // 确保父节点展开
  1604. updateNavigationBar(parentIndex); // 更新导航栏
  1605. QStandardItem *parentItem = downModel->itemFromIndex(parentIndex);
  1606. if (parentItem) {
  1607. qDebug() << "返回上一级目录:" << parentItem->text();
  1608. } else {
  1609. qWarning() << "未找到父目录项";
  1610. }
  1611. } else {
  1612. qWarning() << "当前节点没有父节点";
  1613. }
  1614. return;
  1615. }
  1616. // 获取当前选中项的索引
  1617. QModelIndex currentIndex = treeViewDown->currentIndex();
  1618. if (!currentIndex.isValid()) {
  1619. qDebug() << "当前无有效索引,无法返回";
  1620. return;
  1621. }
  1622. // 获取父节点索引
  1623. QModelIndex parentIndex = currentIndex.parent();
  1624. if (parentIndex.isValid()) {
  1625. // 如果有父节点,返回到父节点
  1626. treeViewDown->setCurrentIndex(parentIndex);
  1627. treeViewDown->expand(parentIndex); // 展开父节点
  1628. updateNavigationBar(parentIndex); // 更新导航栏
  1629. QStandardItem *parentItem = downModel->itemFromIndex(parentIndex);
  1630. if (parentItem) {
  1631. //qDebug() << "返回上一级目录:" << parentItem->text();
  1632. } else {
  1633. qWarning() << "未找到父目录项";
  1634. }
  1635. } else {
  1636. // 如果没有父节点,返回到一级目录的第一行
  1637. QModelIndex firstIndex = downModel->index(0, 0);
  1638. if (firstIndex.isValid()) {
  1639. treeViewDown->setCurrentIndex(firstIndex);
  1640. treeViewDown->expand(firstIndex); // 展开一级目录
  1641. updateNavigationBar(firstIndex); // 更新导航栏
  1642. QStandardItem *firstItem = downModel->itemFromIndex(firstIndex);
  1643. if (firstItem) {
  1644. qDebug() << "返回到一级目录的第一行:" << firstItem->text();
  1645. } else {
  1646. qWarning() << "未找到一级目录的第一行";
  1647. }
  1648. } else {
  1649. qWarning() << "无法找到任何一级目录";
  1650. }
  1651. }
  1652. }
  1653. void TreeViewManager::onButtonRightClicked()
  1654. {
  1655. //qDebug() << "TreeViewManager: 进入下一级目录";
  1656. // 获取当前选中项索引
  1657. QModelIndex currentIndex = treeViewDown->currentIndex();
  1658. if (!currentIndex.isValid()) {
  1659. //qDebug() << "当前无有效索引,自动从第一个根节点开始";
  1660. currentIndex = downModel->index(0, 0); // 从根节点第一个开始
  1661. }
  1662. QStandardItem *currentItem = downModel->itemFromIndex(currentIndex);
  1663. if (!currentItem) {
  1664. qWarning() << "无法获取当前目录项";
  1665. return;
  1666. }
  1667. // 检查当前项是否有子节点
  1668. if (currentItem->hasChildren()) {
  1669. //qDebug() << "当前目录有子目录,展开并进入第一个子目录:" << currentItem->text();
  1670. // 展开当前项并进入第一个子项
  1671. treeViewDown->expand(currentIndex);
  1672. QModelIndex childIndex = downModel->index(0, 0, currentIndex); // 使用 QAbstractItemModel::index()
  1673. if (childIndex.isValid()) {
  1674. treeViewDown->setCurrentIndex(childIndex);
  1675. updateNavigationBar(childIndex); // 更新导航栏
  1676. // qDebug() << "进入子目录:" << downModel->itemFromIndex(childIndex)->text();
  1677. return;
  1678. } else {
  1679. qWarning() << "展开失败:未找到子节点";
  1680. return;
  1681. }
  1682. }
  1683. // 当前目录没有子节点,寻找同级的下一个节点
  1684. QModelIndex nextSiblingIndex = currentIndex.siblingAtRow(currentIndex.row() + 1);
  1685. while (!nextSiblingIndex.isValid()) {
  1686. // 如果没有下一个同级节点,向上查找父节点的下一个同级节点
  1687. QModelIndex parentIndex = currentIndex.parent();
  1688. if (!parentIndex.isValid()) {
  1689. // qDebug() << "已到达最底层目录,没有更多的下一级节点";
  1690. return; // 已经遍历完所有节点
  1691. }
  1692. nextSiblingIndex = parentIndex.siblingAtRow(parentIndex.row() + 1);
  1693. currentIndex = parentIndex;
  1694. }
  1695. // 进入下一个同级节点
  1696. treeViewDown->setCurrentIndex(nextSiblingIndex);
  1697. updateNavigationBar(nextSiblingIndex); // 更新导航栏
  1698. //qDebug() << "进入同级目录:" << downModel->itemFromIndex(nextSiblingIndex)->text();
  1699. }