DbTreeViewManager.cpp 136 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641
  1. #include "DbTreeViewManager.h"
  2. #include "OriginalWnd/OriginalWnd.h"
  3. #include "OriginalWnd/NonInteractiveCheckDelegate.h"
  4. #include <QFile>
  5. #include <QTextStream>
  6. #include <QDateTime>
  7. #include "Src/common/JMessageTip.h"
  8. #include "CViewInterface.h"
  9. // 构造函数
  10. DbTreeViewManager::DbTreeViewManager(OriginalWnd* originalWnd, QWidget* widget2, QWidget* parent)
  11. : QWidget(parent),
  12. m_originalWnd(originalWnd),
  13. widget2(widget2),
  14. treeViewDown(new QTreeView(this)),
  15. model(new QStandardItemModel(this)),
  16. restoring(false),
  17. m_blockItemChanged(false)
  18. {
  19. if (!widget2) {
  20. // qWarning() << "DbTreeViewManager: widget2 未初始化";
  21. return;
  22. }
  23. //设置样式
  24. //创建数据库对象
  25. m_sqlOper = &SqlOperation::GetInstance();
  26. // 设置模型
  27. treeViewDown->setModel(model);
  28. treeViewDown->setHeaderHidden(true);
  29. treeViewDown->setEditTriggers(QAbstractItemView::NoEditTriggers);
  30. // 设置树视图的几何位置
  31. treeViewDown->setGeometry(16, 106, widget2->width() - 16, widget2->height() - 106);
  32. // 安装事件过滤器以自定义绘制虚线
  33. treeViewDown->viewport()->installEventFilter(this);
  34. // 创建统一分隔线
  35. lineFrame1 = createUnifiedSeparator(widget2, 2);
  36. lineFrame1->setGeometry(16, 100, 460, 2);
  37. // 创建按钮并设置布局
  38. setupButton();
  39. // 创建导航栏
  40. navigationWidget = new QWidget(widget2);
  41. navigationWidget->setGeometry(15, 15, 300, 74);
  42. // 连接目录前的复选框信号与槽
  43. connect(model, &QStandardItemModel::itemChanged, this, &DbTreeViewManager::onItemChanged);
  44. // 目录树连接点击信号
  45. connect(treeViewDown, &QTreeView::clicked, this, &DbTreeViewManager::onTreeViewClicked);
  46. // 连接目录 expanded 和 collapsed 信号
  47. connect(treeViewDown, &QTreeView::expanded, this, [=](const QModelIndex &index) {
  48. QStandardItem *item = model->itemFromIndex(index);
  49. if (!item) return;
  50. QStringList path = buildItemPath(item);
  51. expandedPaths.insert(path.join("/"));
  52. updateSeparatorLine();
  53. // 保存展开路径
  54. saveExpandedPaths();
  55. });
  56. connect(treeViewDown, &QTreeView::collapsed, this, [=](const QModelIndex &index) {
  57. QStandardItem *item = model->itemFromIndex(index);
  58. if (!item) return;
  59. QStringList path = buildItemPath(item);
  60. expandedPaths.remove(path.join("/"));
  61. updateSeparatorLine();
  62. // 保存展开路径
  63. saveExpandedPaths();
  64. });
  65. // 所有展开操作完成后更新分隔线
  66. QTimer::singleShot(0, this, [=]() {
  67. QStandardItem *rootItem = model->invisibleRootItem();
  68. QStandardItem *thirdItem = findFirstThirdLevelItemDFS(rootItem);
  69. if (thirdItem)
  70. {
  71. QJsonObject thirdLevelObj = thirdItem->data(Qt::UserRole + 2).toJsonObject();
  72. if (thirdLevelObj.contains("IsThirdLevel") && thirdLevelObj["IsThirdLevel"].toBool())
  73. {
  74. QString multiTableName = "";
  75. if (thirdLevelObj.contains("TableName"))
  76. {
  77. multiTableName = thirdLevelObj["TableName"].toString();
  78. }
  79. QList<CONFIG_BASE_STRUCT> buttons;
  80. int userGrade = 0x1;
  81. m_sqlOper->GetThirdDirButtons(multiTableName, userGrade, buttons);
  82. displayThirdLevelButtons(buttons);
  83. // loadButtonConfigForThirdLevel(thirdLevelObj);
  84. }
  85. }
  86. });
  87. // 应用自定义只读复选框委托
  88. treeViewDown->setItemDelegate(new NonInteractiveCheckDelegate(treeViewDown));
  89. menuArray[0] = {2, false,-1};
  90. menuArray[1] = {5, false,-1};
  91. menuArray[2] = {3, false,-1};
  92. menuArray[3] = {4, false,-1};
  93. menuArray[4] = {6, false,-1};
  94. currentMenuId = -1;
  95. }
  96. DbTreeViewManager::~DbTreeViewManager()
  97. {
  98. }
  99. // 创建横线样式
  100. QFrame* DbTreeViewManager::createUnifiedSeparator(QWidget *parent, int height)
  101. {
  102. QFrame *separator = new QFrame(parent);
  103. separator->setFrameShape(QFrame::NoFrame); // 移除默认框架
  104. separator->setFixedHeight(height); // 设置固定高度
  105. separator->setStyleSheet("background-color: #C7CAEB;");
  106. separator->hide(); // 初始化为隐藏
  107. return separator;
  108. }
  109. // 目录树样式
  110. void DbTreeViewManager::applyCustomStyles() {
  111. treeViewDown->setStyleSheet(R"(
  112. /* 设置分支图标 */
  113. QTreeView::branch:closed:has-children {
  114. border-image: none;
  115. image: url(:/images/home_add.png);
  116. }
  117. QTreeView::branch:open:has-children {
  118. border-image: none;
  119. image: url(:/images/home_minus.png);
  120. }
  121. /* 设置多选框 */
  122. QTreeView::indicator:unchecked {
  123. image: url(:/images/home_NotSelecte.png);
  124. }
  125. QTreeView::indicator:checked {
  126. image: url(:/images/home_selected.png);
  127. }
  128. /* 背景透明,行间距 */
  129. QTreeView {
  130. background: transparent;
  131. border: none;
  132. }
  133. /* 设置项目选中的背景色 */
  134. QTreeView::item:selected {
  135. background-color: #A9B4FF;
  136. }
  137. /* 设置项目的行间距 */
  138. QTreeView::item {
  139. padding-top: 5px; /* 上边距 */
  140. padding-bottom: 5px; /* 下边距 */
  141. }
  142. )");
  143. }
  144. // 检查权限
  145. bool DbTreeViewManager::hasPrivilege(const int &UserGrade, const int &userPrivilege)
  146. {
  147. bool hasPriv;
  148. // UserGrade 是以逗号分隔的字符串,如 "1,2,3"
  149. if(UserGrade==userPrivilege){
  150. hasPriv = true;
  151. }else{
  152. hasPriv = false;
  153. }
  154. // qDebug() << "Checking privilege. UserGrade:" << UserGrade << "userPrivilege:" << userPrivilege << "result:" << hasPriv;
  155. return hasPriv;
  156. }
  157. //void DbTreeViewManager::updateDb(const QString &TableName, const int &Id,const QString currentValue){
  158. // QSqlDatabase db = QSqlDatabase::database();
  159. // if (!db.isOpen()) {
  160. // qWarning() << "数据库未打开";
  161. // }
  162. // QString sql = QString("UPDATE %1 SET currentValue = :newValue WHERE id = :id").arg(TableName);
  163. // QSqlQuery query;
  164. // query.prepare(sql);
  165. // query.bindValue(":newValue", currentValue);
  166. // query.bindValue(":id", Id);
  167. // // 执行更新操作
  168. // if (!query.exec()) {
  169. // qWarning() << "Failed to update currentValue:" << query.lastError().text();
  170. // } else {
  171. // qDebug() << "currentValue updated successfully.";
  172. // }
  173. //}
  174. void DbTreeViewManager::initializeTree(QString name, const int &userPrivilege)
  175. {
  176. qDebug() << "Initializing tree with configId:" << name << "and userPrivilege:" << userPrivilege;
  177. m_currentConfigName = name; // 设置当前配置ID
  178. // 先清空历史
  179. clearHistory();
  180. // 保存之前的选中/展开状态
  181. m_blockItemChanged = true;
  182. restoring = true;
  183. // 清理当前的分隔线
  184. clearAllSeparators();
  185. // 清空已有模型数据和相关记录
  186. model->clear();
  187. firstLevelSeparators.clear();
  188. expandedPaths.clear();
  189. // 清理并隐藏三级目录窗口
  190. clearThirdLevelMenu();
  191. // **Home 界面逻辑**
  192. if (name == "Home") {
  193. // 隐藏目录树和分隔线
  194. treeViewDown->hide();
  195. for (auto separator : firstLevelSeparators) {
  196. if (separator)
  197. separator->hide();
  198. }
  199. // 隐藏导航栏
  200. navigationWidget->hide();
  201. // 隐藏按钮
  202. updateButtonsVisibility(); // 将按钮隐藏
  203. // 隐藏特定的分隔线 lineFrame1
  204. if (lineFrame1) {
  205. lineFrame1->hide();
  206. }
  207. // 从 BondHead 和 Buttons 表中加载 DirectoryId = 1 的数据
  208. QList<Table_Control_Data> tableControlDatas;
  209. bool allChangeFlag = false;
  210. m_sqlOper->GetThirdDirControlData("HomeProduct", userPrivilege, tableControlDatas, allChangeFlag);
  211. //QJsonObject data = fetchThirdLevelData(1, userPrivilege);
  212. displayThirdLevelFields(tableControlDatas, allChangeFlag, true); // 传递参数表示 Home 界面
  213. // 把当前页面压到 m_backStack 里
  214. PageState st;
  215. st.path << "Home";
  216. st.isThirdLevel = true;
  217. st.directoryId = 1;
  218. st.multiTableName = "HomeProduct";
  219. m_backStack.append(st);
  220. if(m_backStack.size() >= 2){
  221. buttonLeft->setIcon(QIcon(":/images/home_left.png"));
  222. }
  223. } else {
  224. // **其他配置逻辑**
  225. // 设置 treeViewDown 的几何位置
  226. treeViewDown->setGeometry(16, 106, widget2->width() - 16, widget2->height() - 106);
  227. // 显示目录树和分隔线
  228. treeViewDown->show();
  229. for (auto separator : firstLevelSeparators) {
  230. if (separator)
  231. separator->show();
  232. }
  233. // 应用自定义样式
  234. applyCustomStyles();
  235. // 显示导航栏
  236. navigationWidget->show();
  237. // 显示按钮
  238. updateButtonsVisibility();
  239. // 显示特定的分隔线 lineFrame1
  240. if (lineFrame1) {
  241. lineFrame1->show();
  242. }
  243. // 加载目录数据 并进行权限判断
  244. QString tableName = "Dir_" + name;
  245. QList<QJsonObject> directories;
  246. m_sqlOper->GetDirectories(tableName, userPrivilege, directories);
  247. //m_sqlOper.loadDirectories(tableName, userPrivilege, directories);
  248. buildTreeFromDirectories(directories);
  249. //loadDirectories(configId, userPrivilege);
  250. //
  251. // 更新分隔线
  252. updateSeparatorLine();
  253. m_blockItemChanged = false;
  254. restoring = false;
  255. // qDebug() << "Tree initialization completed.";
  256. // 恢复展开路径
  257. loadExpandedPaths();
  258. // 恢复复选框状态
  259. loadCheckedPaths();
  260. // 自动选中第一个根目录项并更新导航栏
  261. QStandardItem *rootItem = model->invisibleRootItem()->child(0);
  262. if (rootItem) {
  263. QModelIndex rootIndex = model->indexFromItem(rootItem);
  264. treeViewDown->setCurrentIndex(rootIndex);
  265. updateNavigationBar(rootIndex);
  266. // 如果当前根目录是三级目录,则加载其字段内容
  267. QVariant dataVar = rootItem->data(Qt::UserRole + 2).toJsonObject();
  268. bool isThirdLevel = false;
  269. int directoryId = -1;
  270. QString multiTableName = "";
  271. if (dataVar.canConvert<QJsonObject>()) {
  272. QJsonObject thirdLevelObj = dataVar.toJsonObject();
  273. if (thirdLevelObj.contains("IsThirdLevel") && thirdLevelObj["IsThirdLevel"].toBool()) {
  274. isThirdLevel = true;
  275. if (thirdLevelObj.contains("Id")) {
  276. directoryId = thirdLevelObj["Id"].toInt();
  277. }
  278. if (thirdLevelObj.contains("TableName"))
  279. {
  280. multiTableName = thirdLevelObj["TableName"].toString();
  281. }
  282. }
  283. }
  284. if (isThirdLevel) {
  285. if (directoryId == -1) {
  286. // qWarning() << "无效的 DirectoryId,无法加载第三层目录内容。";
  287. return;
  288. }
  289. // userPrivilege 同样传递
  290. //QJsonObject data = fetchThirdLevelData(directoryId, userPrivilege);
  291. QList<Table_Control_Data> tableControlDatas;
  292. bool allChangeFlag = false;
  293. m_sqlOper->GetThirdDirControlData(multiTableName, userPrivilege, tableControlDatas, allChangeFlag);
  294. //QJsonObject data = fetchThirdLevelData(1, userPrivilege);
  295. displayThirdLevelFields(tableControlDatas, allChangeFlag, false); // 传递参数表示 Home 界面
  296. //QJsonObject data = m_sqlOper.fetchThirdLevelData(multiTableName, directoryId, userPrivilege);
  297. //displayThirdLevelFields(data, false); // 传递参数表示非 Home 界面
  298. // 把当前状态压入 m_backStack
  299. PageState st;
  300. st.path = buildItemPath(rootItem);
  301. st.isThirdLevel = true;
  302. st.directoryId = directoryId;
  303. st.multiTableName = multiTableName;
  304. m_backStack.append(st);
  305. if(m_backStack.size() >= 2){
  306. buttonLeft->setIcon(QIcon(":/images/home_left.png"));
  307. }
  308. }
  309. } else {
  310. // qWarning() << "树形模型中没有根目录项。";
  311. }
  312. //寻找第一个三级目录,显示第一个三级目录的按钮
  313. // 自动找第一个三级目录
  314. QStandardItem *rootItem1 = model->invisibleRootItem();
  315. QStandardItem *firstThirdItem = findFirstThirdLevelItemDFS(rootItem1);
  316. if (!firstThirdItem) {
  317. // qDebug() << "[initializeTree] No third-level item found, do nothing.";
  318. return;
  319. }
  320. // QStandardItem 里获取它的目录信息
  321. QVariant dataVar = firstThirdItem->data(Qt::UserRole + 2);
  322. if (!dataVar.canConvert<QJsonObject>()) {
  323. // qWarning() << "[initializeTree] firstThirdItem->data 无法转换为 QJsonObject";
  324. return;
  325. }
  326. QJsonObject dirObj = dataVar.toJsonObject();
  327. if (!dirObj.contains("Id")) {
  328. // qWarning() << "[initializeTree] 第一个三级目录无 Id 字段,无法加载";
  329. return;
  330. }
  331. int directoryId = dirObj["Id"].toInt();
  332. if (!dirObj.contains("TableName"))
  333. {
  334. return;
  335. }
  336. QString multiTableName = dirObj["TableName"].toString();
  337. //根据目录ID找出按钮
  338. QTimer::singleShot(0, this, &DbTreeViewManager::updateSeparatorLine);
  339. QList<CONFIG_BASE_STRUCT> buttons;
  340. m_sqlOper->GetThirdDirButtons(multiTableName, userPrivilege, buttons);
  341. //QJsonObject data = fetchThirdLevelData(1, userPrivilege);
  342. displayThirdLevelButtons(buttons); // 传递参数表示 Home 界面
  343. // 数据库BondHead/Buttons
  344. //QJsonObject data = fetchThirdLevelData(directoryId, userPrivilege);
  345. //if (!data.contains("buttons")) {
  346. // // 第三层没有 buttons 字段
  347. // qWarning() << "[initializeTree] fetchThirdLevelData: no 'buttons', skip";
  348. // return;
  349. //}
  350. // singleShot 在所有展开操作完成后更新分隔线
  351. // loadButtonConfigForThirdLevel 显示按钮
  352. //loadButtonConfigForThirdLevel(data);
  353. }
  354. }
  355. // 从数据库加载目录 包含权限判断
  356. void DbTreeViewManager::loadDirectories(int configId, const int &userPrivilege)
  357. {
  358. // qDebug() << "Loading directories for configId:" << configId << "with userPrivilege:" << userPrivilege;
  359. QSqlDatabase db = QSqlDatabase::database();
  360. if (!db.isOpen()) {
  361. qWarning() << "数据库未打开";
  362. return;
  363. }
  364. QSqlQuery query(db);
  365. query.prepare(R"(
  366. SELECT Id, Name, ParentId, IsThirdLevel, Separator, UserGrade, Circle
  367. FROM Directories
  368. WHERE ConfigId = :configId
  369. )");
  370. query.bindValue(":configId", configId);
  371. if (!query.exec()) {
  372. qWarning() << "查询 Directories 失败:" << query.lastError().text();
  373. return;
  374. }
  375. // 以 QJsonObject 保存所有目录
  376. QList<QJsonObject> directories;
  377. while (query.next()) {
  378. QJsonObject obj;
  379. int id = query.value("Id").toInt();
  380. obj["Id"] = id;
  381. obj["Name"] = query.value("Name").toString();
  382. obj["ParentId"] = query.value("ParentId").isNull() ? -1 : query.value("ParentId").toInt();
  383. obj["IsThirdLevel"] = query.value("IsThirdLevel").toBool();
  384. obj["Separator"] = query.value("Separator").toInt();
  385. obj["UserGrade"] = query.value("UserGrade").toString();
  386. obj["Circle"] = query.value("Circle").toInt();
  387. qDebug() << "加载目录:"<< obj["Name"].toString() << " ParentId:" << obj["ParentId"].toInt()<< " Separator:" << obj["Separator"].toInt();
  388. qDebug() << " Circle:" << obj["Circle"].toInt();
  389. // 权限判断:只有当记录的 UserGrade 中包含当前用户权限时才加入
  390. bool ok;
  391. if (hasPrivilege(obj["UserGrade"].toString().toInt(&ok,16), userPrivilege))
  392. directories.append(obj);
  393. else
  394. qDebug() << "目录跳过(权限不足):" << obj["Name"].toString();
  395. }
  396. // qDebug() << "Loaded directories count:" << directories.size();
  397. // 根据目录列表构建树模型
  398. buildTreeFromDirectories(directories);
  399. }
  400. void DbTreeViewManager::buildTreeFromDirectories(const QList<QJsonObject> &directories)
  401. {
  402. // 建立 Id->QStandardItem 的映射
  403. QMap<int, QStandardItem*> idItemMap;
  404. for (const QJsonObject &dir : directories) {
  405. QStandardItem *item = new QStandardItem(dir["Name"].toString());
  406. item->setData(dir["Id"].toInt(), Qt::UserRole + 1); // 保存目录Id
  407. // 如果是三级目录,将整个 JSON 对象存到 UserRole+2
  408. if (dir["IsThirdLevel"].toBool()) {
  409. item->setData(dir, Qt::UserRole + 2);
  410. } else {
  411. item->setData(false, Qt::UserRole + 2); // 标记为非三级目录
  412. }
  413. item->setCheckable(true);
  414. // 复选框状态由程序逻辑设置
  415. item->setCheckState(Qt::Unchecked);
  416. idItemMap.insert(dir["Id"].toInt(), item);
  417. }
  418. // 建立父子关系
  419. for (const QJsonObject &dir : directories) {
  420. int parentId = dir["ParentId"].toInt();
  421. int currentId = dir["Id"].toInt();
  422. QStandardItem *childItem = idItemMap.value(currentId);
  423. if (childItem == nullptr)
  424. {
  425. qDebug() << "child is nullptr";
  426. }
  427. if (parentId == -1) {
  428. // 说明这是一级目录
  429. model->invisibleRootItem()->appendRow(childItem);
  430. qDebug() << " !!!!! first :" << "child : " << currentId;
  431. // 如果一级目录的 "Separator" == 1,则为其创建一条分割线
  432. if (dir["Separator"].toInt() == 1) {
  433. QFrame *sep = createUnifiedSeparator(widget2, 2);
  434. sep->hide();
  435. firstLevelSeparators.insert(childItem, sep);
  436. }
  437. } else {
  438. // 非一级目录 => 子目录
  439. QStandardItem *parentItem = idItemMap.value(parentId);
  440. if (parentItem)
  441. {
  442. parentItem->appendRow(childItem);
  443. qDebug() << "parent:" << parentId << "child : " << currentId;
  444. }
  445. else
  446. qWarning() << "无法找到 ParentId:" << parentId << "的父目录项";
  447. }
  448. }
  449. }
  450. // 查询三级目录下的 BondHead 和 Buttons 数据 均判断权限
  451. //QJsonObject DbTreeViewManager::fetchThirdLevelData(int directoryId, const int &userPrivilege)
  452. //{
  453. // QJsonObject data;
  454. // QJsonArray fieldsArray;
  455. // QJsonArray buttonsArray;
  456. // QString temp;
  457. // bool allChangeFlag = true; // 默认所有 ChangeFlag 为 true
  458. //
  459. // QSqlDatabase db = QSqlDatabase::database();
  460. // if (!db.isOpen()) {
  461. // qWarning() << "数据库未打开";
  462. // return data;
  463. // }
  464. // // 查询 TableName 表
  465. // QSqlQuery queryBondtest(db);
  466. // queryBondtest.prepare(R"(
  467. // SELECT TableName
  468. // FROM Directories
  469. // WHERE Id = :dirId
  470. // )");
  471. // queryBondtest.bindValue(":dirId",directoryId);
  472. // if (queryBondtest.exec()) {
  473. // while (queryBondtest.next()) {
  474. // temp = queryBondtest.value("TableName").toString();
  475. // }
  476. // }
  477. // QStringList tableName = temp.split(",");
  478. // QString currentTableName;
  479. // for (const QString &item : tableName) {
  480. // if (item.isEmpty()) {
  481. // qDebug() << "Encountered an empty string, stopping traversal.";
  482. // break; // 遇到空字符串时退出循环
  483. // }else{
  484. // currentTableName = item;
  485. // QString sql = QString("SELECT Id, DirectoryId, Describe, Type, Value,CurrentValue,UpLimit,DownLimit,units, Groupld, UserGrade, ChangeFlag FROM %1 WHERE DirectoryId = :dirId").arg(currentTableName);
  486. // // 查询 BondHead 表
  487. // QSqlQuery queryBond(db);
  488. // queryBond.prepare(sql);
  489. // queryBond.bindValue(":dirId", directoryId);
  490. // if (queryBond.exec()) {
  491. // while (queryBond.next()) {
  492. // bool ok;
  493. // int dbUserGrade = queryBond.value("UserGrade").toString().toInt(&ok, 16);
  494. // // 只有当数据库记录的 UserGrade 包含当前用户权限时才加载该字段
  495. // if (!hasPrivilege(dbUserGrade, userPrivilege))
  496. // continue;
  497. // QJsonObject field;
  498. // field["Id"] = queryBond.value("Id").toInt();
  499. // field["DirectoryId"] = queryBond.value("DirectoryId").toInt();
  500. // field["Describe"] = queryBond.value("Describe").toString();
  501. // field["Type"] = queryBond.value("Type").toString();
  502. // field["Value"] = queryBond.value("Value").toString();
  503. // field["CurrentValue"] = queryBond.value("CurrentValue").toString();
  504. // field["UpLimit"] = queryBond.value("UpLimit").toString();
  505. // field["DownLimit"] = queryBond.value("DownLimit").toString();
  506. // field["units"] = queryBond.value("units").toString();
  507. // field["Groupld"] = queryBond.value("Groupld").toInt();
  508. // field["ChangeFlag"] = queryBond.value("ChangeFlag").toInt();
  509. // field["TableName"] = currentTableName;
  510. // fieldsArray.append(field);
  511. //
  512. // // 如果有任一字段 ChangeFlag 为 0,则 allChangeFlag = false
  513. // if (queryBond.value("ChangeFlag").toInt() == 0)
  514. // allChangeFlag = false;
  515. // }
  516. // // qDebug() << "Fetched fields:" << fieldsArray;
  517. // } else {
  518. // qWarning() << "查询 BondHead 失败:" << queryBond.lastError().text();
  519. // }
  520. // }
  521. // }
  522. // //QString sql = QString("SELECT Id, DirectoryId, Describe, Type, Value,units, Groupld, UserGrade, ChangeFlag FROM %1 WHERE DirectoryId = :dirId").arg(currentTableName);
  523. // // // 查询 BondHead 表
  524. // // QSqlQuery queryBond(db);
  525. // // queryBond.prepare(sql);
  526. // // queryBond.bindValue(":dirId", directoryId);
  527. // // if (queryBond.exec()) {
  528. // // while (queryBond.next()) {
  529. // // bool ok;
  530. // // int dbUserGrade = queryBond.value("UserGrade").toString().toInt(&ok, 16);
  531. // // // 只有当数据库记录的 UserGrade 包含当前用户权限时才加载该字段
  532. // // if (!hasPrivilege(dbUserGrade, userPrivilege))
  533. // // continue;
  534. // // QJsonObject field;
  535. // // field["Id"] = queryBond.value("Id").toInt();
  536. // // field["DirectoryId"] = queryBond.value("DirectoryId").toInt();
  537. // // field["Describe"] = queryBond.value("Describe").toString();
  538. // // field["Type"] = queryBond.value("Type").toString();
  539. // // field["Value"] = queryBond.value("Value").toString();
  540. // // field["units"] = queryBond.value("units").toString();
  541. // // field["Groupld"] = queryBond.value("Groupld").toInt();
  542. // // field["ChangeFlag"] = queryBond.value("ChangeFlag").toInt();
  543. // // fieldsArray.append(field);
  544. //
  545. // // // 如果有任一字段 ChangeFlag 为 0,则 allChangeFlag = false
  546. // // if (queryBond.value("ChangeFlag").toInt() == 0)
  547. // // allChangeFlag = false;
  548. // // }
  549. // // // qDebug() << "Fetched fields:" << fieldsArray;
  550. // // } else {
  551. // // qWarning() << "查询 BondHead 失败:" << queryBond.lastError().text();
  552. // // }
  553. //
  554. // // 查询 Buttons 表
  555. // QSqlQuery queryBtn(db);
  556. // queryBtn.prepare(R"(
  557. // SELECT ButtonId, Icon, Text, Enabled, UserGrade
  558. // FROM Buttons
  559. // WHERE DirectoryId = :dirId
  560. // )");
  561. // queryBtn.bindValue(":dirId", directoryId);
  562. // if (queryBtn.exec()) {
  563. // while (queryBtn.next()) {
  564. // bool ok;
  565. // int btnUserGrade = queryBtn.value("UserGrade").toString().toInt(&ok, 16);
  566. // if (!hasPrivilege(btnUserGrade, userPrivilege))
  567. // continue;
  568. // QJsonObject button;
  569. // button["id"] = queryBtn.value("ButtonId").toString();
  570. // button["icon"] = queryBtn.value("Icon").toString();
  571. // button["text"] = queryBtn.value("Text").toString();
  572. // button["enabled"] = queryBtn.value("Enabled").toBool();
  573. // buttonsArray.append(button);
  574. // }
  575. // // qDebug() << "Fetched buttons:" << buttonsArray;
  576. // } else {
  577. // qWarning() << "查询 Buttons 失败:" << queryBtn.lastError().text();
  578. // }
  579. //
  580. // data["fields"] = fieldsArray;
  581. // data["buttons"] = buttonsArray;
  582. // data["IsThirdLevel"] = true;
  583. // data["allChangeFlag"] = allChangeFlag;
  584. // return data;
  585. //}
  586. // 事件过滤器,用于自定义绘制虚线
  587. bool DbTreeViewManager::eventFilter(QObject *watched, QEvent *event)
  588. {
  589. // 拦截 Paint 事件
  590. if (watched == treeViewDown->viewport() && event->type() == QEvent::Paint)
  591. {
  592. // 进行默认绘制
  593. bool handled = QWidget::eventFilter(watched, event);
  594. // 使用 QPainter 叠加画“拐角线”
  595. QPainter painter(treeViewDown->viewport());
  596. if (!painter.isActive()) {
  597. qWarning() << "Painter not active";
  598. return handled;
  599. }
  600. painter.save();
  601. painter.setPen(QPen(Qt::gray, 1, Qt::DashLine)); // 灰色、1px 宽、虚线
  602. // 调用递归函数,绘制所有分支
  603. paintAllBranches(QModelIndex(), painter);
  604. painter.restore();
  605. return handled;
  606. }
  607. // 其余事件交给父类默认处理
  608. return QWidget::eventFilter(watched, event);
  609. }
  610. // 递归绘制所有分支
  611. void DbTreeViewManager::paintAllBranches(const QModelIndex &parentIndex, QPainter &painter)
  612. {
  613. int rowCount = model->rowCount(parentIndex);
  614. for(int i = 0; i < rowCount; ++i)
  615. {
  616. // 当前子节点
  617. QModelIndex childIndex = model->index(i, 0, parentIndex);
  618. if (!childIndex.isValid()) continue;
  619. // 1) 父->子拐角线
  620. drawParentChildLine(childIndex, painter);
  621. // 2) 兄弟延续竖线(如果本节点不是最后一个兄弟,就在拐点列画条向下的线)
  622. if (i < rowCount - 1) {
  623. drawSiblingLine(childIndex, painter);
  624. }
  625. // 3) 递归处理子节点
  626. paintAllBranches(childIndex, painter);
  627. }
  628. }
  629. // 在“父节点 -> 子节点”间画一条“L”型拐角线,调整横向线段的长度
  630. void DbTreeViewManager::drawParentChildLine(const QModelIndex &childIndex, QPainter &painter)
  631. {
  632. QModelIndex parentIndex = childIndex.parent();
  633. if (!parentIndex.isValid()) {
  634. // “顶层节点”定义一个固定的起点 (rootX, rootY)
  635. int indent = treeViewDown->indentation();
  636. int depth = 0; // 顶层节点深度为0
  637. int branchX = (depth + 1) * indent - indent / 2; // 计算 branchX 基于缩进和深度
  638. // 定义 rootY 为节点中心 Y
  639. QRect childRect = treeViewDown->visualRect(childIndex);
  640. if (!childRect.isValid())
  641. return;
  642. int rootY = childRect.center().y();
  643. // 定义横向偏移量
  644. const int hOffset = -20;
  645. // 绘制竖线
  646. painter.drawLine(QPoint(branchX, rootY),
  647. QPoint(branchX, childRect.center().y()));
  648. // 计算新的横线终点
  649. int newX = childRect.left() + hOffset;
  650. // 绘制横线
  651. painter.drawLine(QPoint(branchX, childRect.center().y()),
  652. QPoint(newX, childRect.center().y()));
  653. return;
  654. }
  655. QRect parentRect = treeViewDown->visualRect(parentIndex);
  656. QRect childRect = treeViewDown->visualRect(childIndex);
  657. if (!parentRect.isValid() || !childRect.isValid()) {
  658. // 父或子超出可视区域
  659. return;
  660. }
  661. int pMidY = parentRect.center().y();
  662. int cMidY = childRect.center().y();
  663. // 计算节点深度
  664. int depth = 0;
  665. QModelIndex p = parentIndex;
  666. while (p.isValid()) {
  667. depth++;
  668. p = p.parent();
  669. }
  670. int indent = treeViewDown->indentation();
  671. int branchX = depth * indent - indent / 2;
  672. // branchX 不超出视图范围
  673. branchX = std::max(branchX, 0);
  674. // 定义横向偏移量
  675. const int hOffset = -15;
  676. // 绘制竖线
  677. painter.drawLine(QPoint(branchX, pMidY), QPoint(branchX, cMidY));
  678. // 计算新的横线终点
  679. int newX = childRect.left() + hOffset;
  680. // 绘制横线
  681. painter.drawLine(QPoint(branchX, cMidY), QPoint(newX, cMidY));
  682. }
  683. // 节点下面还有兄弟,则在拐点列那里继续往下画竖线
  684. void DbTreeViewManager::drawSiblingLine(const QModelIndex &childIndex, QPainter &painter)
  685. {
  686. QModelIndex parentIndex = childIndex.parent();
  687. if (!parentIndex.isValid()) {
  688. return; // 没有父节点
  689. }
  690. // 下一个兄弟
  691. int row = childIndex.row();
  692. int lastRow = model->rowCount(parentIndex) - 1;
  693. if (row >= lastRow) {
  694. return; // 说明是最后一个兄弟,不用画延伸线
  695. }
  696. QModelIndex nextSibling = model->index(row + 1, 0, parentIndex);
  697. QRect currRect = treeViewDown->visualRect(childIndex);
  698. QRect nextRect = treeViewDown->visualRect(nextSibling);
  699. if (!currRect.isValid() || !nextRect.isValid()) {
  700. return;
  701. }
  702. // 计算节点深度
  703. int depth = 0;
  704. QModelIndex p = parentIndex;
  705. while (p.isValid()) {
  706. depth++;
  707. p = p.parent();
  708. }
  709. int indent = treeViewDown->indentation();
  710. int branchX = depth * indent - indent / 2;
  711. // 确保 branchX 不超出视图范围
  712. branchX = std::max(branchX, 0);
  713. // 从当前节点底部向下延伸到下一个兄弟节点顶部
  714. int startY = currRect.bottom();
  715. int endY = nextRect.top();
  716. painter.drawLine(QPoint(branchX, startY), QPoint(branchX, endY));
  717. }
  718. // 递归查找第一个三级目录项
  719. QStandardItem* DbTreeViewManager::findFirstThirdLevelItemDFS(QStandardItem *parentItem)
  720. {
  721. if (!parentItem) {
  722. return nullptr;
  723. }
  724. // 遍历当前 parentItem 的所有子
  725. for (int i = 0; i < parentItem->rowCount(); ++i) {
  726. QStandardItem *child = parentItem->child(i);
  727. if (!child) continue;
  728. // 检查这个子节点是否是三级目录
  729. QVariant data = child->data(Qt::UserRole + 2);
  730. if (data.canConvert<QJsonObject>()) {
  731. QJsonObject obj = data.toJsonObject();
  732. if (obj.contains("IsThirdLevel") && obj["IsThirdLevel"].toBool()) {
  733. // 找到第一个 IsThirdLevel = true 的节点就返回
  734. return child;
  735. }
  736. }
  737. // 如果不是 IsThirdLevel,继续往子节点里找
  738. QStandardItem* deeperFound = findFirstThirdLevelItemDFS(child);
  739. if (deeperFound) {
  740. return deeperFound;
  741. }
  742. }
  743. return nullptr;
  744. }
  745. void DbTreeViewManager::onItemChanged(QStandardItem *item)
  746. {
  747. if (m_blockItemChanged || restoring) {
  748. return; // 防止递归和恢复期间触发
  749. }
  750. m_blockItemChanged = true;
  751. Qt::CheckState state = item->checkState();
  752. // 更新所有子项的复选框状态
  753. for (int i = 0; i < item->rowCount(); ++i) {
  754. QStandardItem *child = item->child(i);
  755. if (child) {
  756. child->setCheckState(state);
  757. // 递归更新子项
  758. onItemChanged(child);
  759. }
  760. }
  761. // 更新所有父项的复选框状态
  762. QModelIndex parentIndex = item->index().parent();
  763. while (parentIndex.isValid()) {
  764. QStandardItem *parentItem = model->itemFromIndex(parentIndex);
  765. if (!parentItem) break;
  766. int checkedCount = 0;
  767. int totalCount = parentItem->rowCount();
  768. for (int i = 0; i < totalCount; ++i) {
  769. QStandardItem *child = parentItem->child(i);
  770. if (child && child->checkState() == Qt::Checked) {
  771. checkedCount++;
  772. }
  773. }
  774. if (checkedCount == totalCount) {
  775. parentItem->setCheckState(Qt::Checked);
  776. }
  777. else {
  778. parentItem->setCheckState(Qt::Unchecked);
  779. }
  780. parentIndex = parentIndex.parent();
  781. }
  782. m_blockItemChanged = false;
  783. // 记录复选框状态
  784. saveCheckedPaths();
  785. }
  786. // 初始化和设置按钮
  787. void DbTreeViewManager::setupButton()
  788. {
  789. // 创建按钮并设置属性
  790. auto createButton = [&](QPushButton*& button, const QString& iconPath, const QRect& geometry) {
  791. button = new QPushButton(this);
  792. button->setIcon(QIcon(iconPath));
  793. button->setGeometry(geometry);
  794. button->setStyleSheet(R"(
  795. QPushButton {
  796. position: absolute;
  797. border-radius: 6px;
  798. opacity: 1;
  799. background: #FFFFFF;
  800. border: 1px solid #BABBDC;
  801. }
  802. QPushButton:hover {
  803. background-color: #A9B4FF; /* 鼠标悬停效果 */
  804. }
  805. )");
  806. button->show();
  807. };
  808. // 根据原始代码调整按钮的位置和大小
  809. createButton(ButtonBack, ":/images/home_openFile.png", QRect(328, 6, 76, 30));
  810. createButton(buttonUp, ":/images/home_up.png", QRect(408, 6, 36, 30));
  811. createButton(buttonDown, ":/images/home_down.png", QRect(408, 40, 36, 30));
  812. createButton(buttonLeft, ":/images/home_left_hide.png", QRect(328, 40, 36, 30));
  813. createButton(buttonRight, ":/images/home_right.png", QRect(368, 40, 36, 30));
  814. // 连接按钮的点击信号到相应的槽函数
  815. connect(ButtonBack, &QPushButton::clicked, this, &DbTreeViewManager::onButtonBackClicked);
  816. connect(buttonUp, &QPushButton::clicked, this, &DbTreeViewManager::onButtonUpClicked);
  817. connect(buttonDown, &QPushButton::clicked, this, &DbTreeViewManager::onButtonDownClicked);
  818. connect(buttonLeft, &QPushButton::clicked, this, &DbTreeViewManager::onButtonLeftClicked);
  819. connect(buttonRight, &QPushButton::clicked, this, &DbTreeViewManager::onButtonRightClicked);
  820. }
  821. void DbTreeViewManager::updateButtonsVisibility()
  822. {
  823. if (m_currentConfigName == "Home") {
  824. // Home 界面,隐藏所有按钮
  825. ButtonBack->hide();
  826. buttonUp->hide();
  827. buttonDown->hide();
  828. buttonLeft->hide();
  829. buttonRight->hide();
  830. }
  831. else {
  832. // 其他配置,显示所有按钮
  833. ButtonBack->show();
  834. buttonUp->show();
  835. buttonDown->show();
  836. buttonLeft->show();
  837. buttonRight->show();
  838. }
  839. }
  840. QStandardItem* DbTreeViewManager::deepCopyItem(const QStandardItem *item){
  841. if (!item) {
  842. return nullptr;
  843. }
  844. // 创建一个新的 QStandardItem
  845. QStandardItem *newItem = new QStandardItem(item->text());
  846. newItem->setIcon(item->icon());
  847. newItem->setToolTip(item->toolTip());
  848. newItem->setEditable(item->isEditable());
  849. newItem->setCheckable(item->isCheckable());
  850. newItem->setCheckState(item->checkState());
  851. // 递归复制子项
  852. for (int i = 0; i < item->rowCount(); ++i) {
  853. QStandardItem *childItem = item->child(i);
  854. QStandardItem *newChildItem = deepCopyItem(childItem);
  855. newItem->appendRow(newChildItem);
  856. }
  857. return newItem;
  858. }
  859. void DbTreeViewManager::loadpage(const int &configId){
  860. for(int i=0;i<5;i++){
  861. if(configId == menuArray[i].menuId){
  862. // QJsonObject data = fetchThirdLevelData(menuArray[i].index, 0x1);
  863. //displayThirdLevelFields(data, false);
  864. }
  865. }
  866. }
  867. void DbTreeViewManager::onTreeViewClicked(const QModelIndex &index)
  868. {
  869. QStandardItem *item = model->itemFromIndex(index);
  870. if (!item) return;
  871. // 是否第三层,提取第三层的目录ID及目录表格名字
  872. bool isThird = false;
  873. int dirId = -1;
  874. //QString dirTableName = "";
  875. QString mutliTableName = "";
  876. QVariant dataVar = item->data(Qt::UserRole + 2);
  877. if (dataVar.canConvert<QJsonObject>()) {
  878. QJsonObject obj = dataVar.toJsonObject();
  879. if (obj.contains("IsThirdLevel") && obj["IsThirdLevel"].toBool()) {
  880. isThird = true;
  881. if (obj.contains("Id")) {
  882. dirId = obj["Id"].toInt();
  883. }
  884. if (obj.contains("TableName"))
  885. {
  886. mutliTableName = obj["TableName"].toString();
  887. }
  888. }
  889. }
  890. updateNavigationBar(index);
  891. if (isThird) {
  892. //!!!修改权限
  893. int userPrivilege = 0x1; // 示例
  894. if (dirId < 0) {
  895. qWarning() << "无效directoryId";
  896. return;
  897. }
  898. if (mutliTableName == "")
  899. {
  900. qWarning() << "无效directoryId";
  901. return;
  902. }
  903. QList<Table_Control_Data> tableControlDatas;
  904. bool allChangeFlag = false;
  905. m_sqlOper->GetThirdDirControlData(mutliTableName, userPrivilege, tableControlDatas, allChangeFlag);
  906. displayThirdLevelFields(tableControlDatas, allChangeFlag, false); // 传递参数表示 Home 界面
  907. //QJsonObject data = m_sqlOper.fetchThirdLevelData(mutliTableName, dirId, userPrivilege);
  908. //QJsonObject data = fetchThirdLevelData(dirId, userPrivilege);
  909. //displayThirdLevelFields(data, false);
  910. for(int i=0;i<5;i++){
  911. if(currentMenuId == menuArray[i].menuId){
  912. menuArray[i].isthird = true;
  913. menuArray[i].index = dirId;
  914. }
  915. }
  916. // 将“新页面”压入后退栈, 并清空前进栈
  917. PageState st;
  918. st.path = buildItemPath(item);
  919. st.isThirdLevel = true;
  920. st.directoryId = dirId;
  921. st.multiTableName = mutliTableName;
  922. m_backStack.append(st);
  923. if(m_backStack.size() >= 2){
  924. buttonLeft->setIcon(QIcon(":/images/home_left.png"));
  925. }
  926. m_forwardStack.clear();
  927. buttonRight->setIcon(QIcon(":/images/home_right.png"));
  928. } else {
  929. // 不是第三层,只是选中目录
  930. // 清理第三层窗口
  931. if (m_thirdLevelFieldWnd) {
  932. m_thirdLevelFieldWnd->close();
  933. m_thirdLevelFieldWnd->deleteLater();
  934. m_thirdLevelFieldWnd = nullptr;
  935. }
  936. // 同样更新历史
  937. PageState st;
  938. st.path = buildItemPath(item);
  939. st.isThirdLevel = false;
  940. st.directoryId = -1;
  941. st.multiTableName = mutliTableName;
  942. m_backStack.append(st);
  943. if(m_backStack.size() >= 2){
  944. buttonLeft->setIcon(QIcon(":/images/home_left.png"));
  945. }
  946. m_forwardStack.clear();
  947. buttonRight->setIcon(QIcon(":/images/home_right.png"));
  948. }
  949. }
  950. void DbTreeViewManager::onTreeViewClicked_updown(const QModelIndex &index){
  951. QStandardItem *item = model->itemFromIndex(index);
  952. if (!item) return;
  953. // 是否第三层
  954. bool isThird = false;
  955. int dirId = -1;
  956. QString multiTableName = "";
  957. QVariant dataVar = item->data(Qt::UserRole + 2);
  958. if (dataVar.canConvert<QJsonObject>()) {
  959. QJsonObject obj = dataVar.toJsonObject();
  960. if (obj.contains("IsThirdLevel") && obj["IsThirdLevel"].toBool()) {
  961. isThird = true;
  962. if (obj.contains("Id")) {
  963. dirId = obj["Id"].toInt();
  964. }
  965. if (obj.contains("TableName"))
  966. {
  967. multiTableName = obj["TableName"].toString();
  968. }
  969. }
  970. }
  971. updateNavigationBar(index);
  972. if (isThird) {
  973. int userPrivilege = 0x1; // 示例
  974. if (dirId < 0) {
  975. qWarning() << "无效directoryId";
  976. return;
  977. }
  978. if (multiTableName == "")
  979. {
  980. qWarning() << "无效multiTableName";
  981. return;
  982. }
  983. QList<Table_Control_Data> tableControlDatas;
  984. bool allChangeFlag = false;
  985. m_sqlOper->GetThirdDirControlData(multiTableName, userPrivilege, tableControlDatas, allChangeFlag);
  986. displayThirdLevelFields(tableControlDatas, allChangeFlag, false); // 传递参数表示 Home 界面
  987. // QJsonObject data = m_sqlOper.fetchThirdLevelData(multiTableName, dirId, userPrivilege);
  988. //displayThirdLevelFields(data, false);
  989. // QJsonObject data = fetchThirdLevelData(dirId, userPrivilege);
  990. // 将“新页面”压入后退栈, 并清空前进栈
  991. PageState st;
  992. st.path = buildItemPath(item);
  993. st.isThirdLevel = true;
  994. st.directoryId = dirId;
  995. st.multiTableName = multiTableName;
  996. m_backStack.append(st);
  997. if(m_backStack.size() >= 2){
  998. buttonLeft->setIcon(QIcon(":/images/home_left.png"));
  999. }
  1000. m_forwardStack.clear();
  1001. buttonRight->setIcon(QIcon(":/images/home_right.png"));
  1002. } else {
  1003. // 不是第三层,只是选中目录
  1004. // 清理第三层窗口
  1005. if (m_thirdLevelFieldWnd) {
  1006. m_thirdLevelFieldWnd->close();
  1007. m_thirdLevelFieldWnd->deleteLater();
  1008. m_thirdLevelFieldWnd = nullptr;
  1009. }
  1010. // 同样更新历史
  1011. PageState st;
  1012. st.path = buildItemPath(item);
  1013. st.isThirdLevel = false;
  1014. st.directoryId = -1;
  1015. st.multiTableName = multiTableName;
  1016. m_backStack.append(st);
  1017. if(m_backStack.size() >= 2){
  1018. buttonLeft->setIcon(QIcon(":/images/home_left.png"));
  1019. }
  1020. m_forwardStack.clear();
  1021. buttonRight->setIcon(QIcon(":/images/home_right.png"));
  1022. }
  1023. }
  1024. void DbTreeViewManager::updateNavigationBar(const QModelIndex &index)
  1025. {
  1026. QStandardItem *item = model->itemFromIndex(index);
  1027. if (!item) {
  1028. qWarning() << "导航栏更新失败:未找到对应项";
  1029. return;
  1030. }
  1031. // 清理现有布局
  1032. if (navigationWidget->layout()) {
  1033. QLayoutItem *child;
  1034. while ((child = navigationWidget->layout()->takeAt(0)) != nullptr) {
  1035. if (child->widget()) {
  1036. child->widget()->deleteLater();
  1037. }
  1038. delete child;
  1039. }
  1040. delete navigationWidget->layout();
  1041. }
  1042. // 构建路径列表,从当前项回溯到根节点
  1043. QList<QStandardItem *> path;
  1044. QStandardItem *temp = item;
  1045. while (temp) {
  1046. path.prepend(temp); // 从根节点开始
  1047. temp = temp->parent();
  1048. }
  1049. QVBoxLayout *newLayout = new QVBoxLayout; // 创建新的导航栏布局
  1050. newLayout->setContentsMargins(0, 0, 3, 3);
  1051. newLayout->setSpacing(0);
  1052. // 确保显示三行
  1053. for (int i = 0; i < 3; ++i) {
  1054. QLabel *label = new QLabel;
  1055. if (i < path.size()) {
  1056. QString text = path[i]->text();
  1057. if (i == 1) text = " " + text; // 二级目录缩进
  1058. if (i == 2) text = " " + text; // 三级目录缩进
  1059. label->setText(text);
  1060. } else {
  1061. label->setText(""); // 填充空白行
  1062. }
  1063. QFont font = label->font(); // 设置字体和样式
  1064. font.setPointSize(14);
  1065. font.setFamily("思源黑体");
  1066. font.setBold(true); // 设置文字为粗体
  1067. font.setLetterSpacing(QFont::PercentageSpacing,105); // 设置字间距100%为默认
  1068. label->setFont(font);
  1069. label->setFixedHeight(navigationWidget->height() / 3);
  1070. newLayout->addWidget(label);
  1071. }
  1072. // 设置布局并更新导航栏
  1073. navigationWidget->setLayout(newLayout);
  1074. navigationWidget->update();
  1075. }
  1076. void DbTreeViewManager::clearAllSeparators()
  1077. {
  1078. // 遍历 firstLevelSeparators 中所有 QFrame,
  1079. // 隐藏后调用 deleteLater()
  1080. for (auto separator : firstLevelSeparators.values()) {
  1081. if(separator) {
  1082. separator->hide();
  1083. separator->deleteLater();
  1084. }
  1085. }
  1086. // 清空映射
  1087. firstLevelSeparators.clear();
  1088. }
  1089. void DbTreeViewManager::updateSeparatorLine()
  1090. {
  1091. // 如果目录树被隐藏,就把分割线都藏起来
  1092. if (!treeViewDown->isVisible()) {
  1093. for (auto it = firstLevelSeparators.begin(); it != firstLevelSeparators.end(); ++it) {
  1094. QFrame *sep = it.value();
  1095. if (sep) sep->hide();
  1096. }
  1097. return;
  1098. }
  1099. // 遍历所有“一级目录”与其分隔线
  1100. for (auto it = firstLevelSeparators.begin(); it != firstLevelSeparators.end(); ++it) {
  1101. QStandardItem *firstLevelItem = it.key();
  1102. QFrame *separator = it.value();
  1103. if (!firstLevelItem || !separator) continue;
  1104. // 拿到可视区域
  1105. QModelIndex firstLevelIndex = model->indexFromItem(firstLevelItem);
  1106. QRect firstLevelRect = treeViewDown->visualRect(firstLevelIndex);
  1107. // 如果这个一级目录滚动到看不见了,就把分割线也藏起来
  1108. if (!firstLevelRect.isValid()) {
  1109. separator->hide();
  1110. continue;
  1111. }
  1112. // 如果展开并且它有子节点,就找“最后一个可见子项”
  1113. if (treeViewDown->isExpanded(firstLevelIndex) && firstLevelItem->hasChildren()) {
  1114. QModelIndex lastVisibleChild = findLastVisibleChild(firstLevelIndex);
  1115. if (lastVisibleChild.isValid()) {
  1116. QRect lastChildRect = treeViewDown->visualRect(lastVisibleChild);
  1117. if (lastChildRect.isValid()) {
  1118. // 将分割线放在最后一个子项下面
  1119. separator->setGeometry(16,lastChildRect.bottom() + 115,widget2->width() - 40,1);
  1120. separator->show();
  1121. continue;
  1122. }
  1123. }
  1124. }
  1125. // 如果收起或找不到可见子项,就放在本一级目录下面
  1126. separator->setGeometry(16,firstLevelRect.bottom() + 115,widget2->width() - 40,1);
  1127. separator->show();
  1128. }
  1129. }
  1130. QModelIndex DbTreeViewManager::findLastVisibleChild(const QModelIndex &parentIndex) {
  1131. if (!parentIndex.isValid()) return QModelIndex();
  1132. int childCount = model->rowCount(parentIndex);
  1133. QModelIndex lastVisible;
  1134. for (int i = childCount - 1; i >= 0; --i) {
  1135. QModelIndex childIndex = model->index(i, 0, parentIndex);
  1136. if (!treeViewDown->isRowHidden(i, parentIndex)) {
  1137. if (treeViewDown->isExpanded(childIndex) && model->rowCount(childIndex) > 0) {
  1138. QModelIndex deeper = findLastVisibleChild(childIndex);
  1139. if (deeper.isValid()) {
  1140. return deeper;
  1141. }
  1142. }
  1143. lastVisible = childIndex;
  1144. break;
  1145. }
  1146. }
  1147. return lastVisible;
  1148. }
  1149. //void DbTreeViewManager::displayThirdLevelFields(const QJsonObject &data, bool isHome)
  1150. //{
  1151. // if (data.isEmpty()) {
  1152. // qWarning() << "字段数据为空,无法显示";
  1153. // return;
  1154. // }
  1155. //
  1156. // if (!data.contains("IsThirdLevel") || !data["IsThirdLevel"].toBool()) {
  1157. // qWarning() << "不是三级目录,跳过按钮加载";
  1158. // return;
  1159. // }
  1160. //
  1161. // bool allChangeFlag = false;
  1162. // if (data.contains("allChangeFlag") && data["allChangeFlag"].isBool()) {
  1163. // allChangeFlag = data["allChangeFlag"].toBool();
  1164. // }
  1165. // else {
  1166. // qWarning() << "数据中缺少 'allChangeFlag' 字段或类型不正确";
  1167. // }
  1168. //
  1169. // // 若已有第三层窗口,先关闭并释放
  1170. // if (m_thirdLevelFieldWnd) {
  1171. // m_thirdLevelFieldWnd->close();
  1172. // m_thirdLevelFieldWnd->deleteLater();
  1173. // m_thirdLevelFieldWnd = nullptr;
  1174. // }
  1175. //
  1176. // // 获取当前选中的目录项,设置状态
  1177. // QModelIndex currentIndex = treeViewDown->currentIndex();
  1178. // if (currentIndex.isValid()) {
  1179. // QStandardItem* currentItem = model->itemFromIndex(currentIndex);
  1180. // if (currentItem) {
  1181. // currentItem->setCheckState(allChangeFlag ? Qt::Checked : Qt::Unchecked);
  1182. // qDebug() << "设置当前目录项复选框状态为:" << (allChangeFlag ? "Checked" : "Unchecked");
  1183. // }
  1184. // }
  1185. // else {
  1186. // qWarning() << "当前没有选中的目录项";
  1187. // }
  1188. //
  1189. // // 隐藏目录树及所有分隔线
  1190. // treeViewDown->hide();
  1191. // for (auto separator : firstLevelSeparators) {
  1192. // if (separator)
  1193. // separator->hide();
  1194. // }
  1195. //
  1196. // // 显示按钮
  1197. // loadButtonConfigForThirdLevel(data);
  1198. //
  1199. // // 清理之前的控件列表
  1200. // m_fieldWidgets.clear();
  1201. //
  1202. // // 创建新的 "字段展示" 窗口,并赋值给 m_thirdLevelFieldWnd
  1203. // m_thirdLevelFieldWnd = new QWidget(widget2);
  1204. // m_thirdLevelFieldWnd->setWindowTitle("字段展示");
  1205. // m_thirdLevelFieldWnd->setStyleSheet("background-color: transparent;");
  1206. // if (isHome) {
  1207. // // Home 界面,填满 widget2
  1208. // m_thirdLevelFieldWnd->setGeometry(0, 0, widget2->width(), widget2->height());
  1209. // }
  1210. // else {
  1211. // // 其他配置界面,设置与 treeViewDown 相同的几何位置
  1212. // m_thirdLevelFieldWnd->setGeometry(treeViewDown->geometry());
  1213. // }
  1214. //
  1215. // // 创建滚动区域
  1216. // QScrollArea *scrollArea = new QScrollArea(m_thirdLevelFieldWnd);
  1217. // scrollArea->setWidgetResizable(true);
  1218. // scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
  1219. //
  1220. // // 创建滚动容器并设置布局
  1221. // QWidget *scrollWidget = new QWidget;
  1222. // scrollWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
  1223. // QVBoxLayout *scrollLayout = new QVBoxLayout(scrollWidget);
  1224. // scrollLayout->setSpacing(10);
  1225. // scrollLayout->setContentsMargins(10, 10, 10, 10);
  1226. //
  1227. //
  1228. // //!!!提成一个类
  1229. // int previousGroupId = -1;
  1230. // // 遍历字段数组
  1231. // if (data.contains("fields") && data["fields"].isArray()) {
  1232. // QJsonArray fields = data["fields"].toArray();
  1233. // for (const QJsonValue &val : fields) {
  1234. // QJsonObject field = val.toObject();
  1235. // int fieldId = field["Id"].toInt();
  1236. // QString fieldName = field["Name"].toString();
  1237. // QString fieldType = field["Type"].toString();
  1238. // QString fieldValue = field["Value"].toString();
  1239. // //QString fieldCurrentValue = field["CurrentValue"].toString();
  1240. // QString fieldTableName = field["TableName"].toString();
  1241. // QString fieldUpLimit = field["UpLimit"].toString();
  1242. // QString fieldDownLimit = field["DownLimit"].toString();
  1243. // QString fieldUnits = field["units"].toString();
  1244. // int groupId = field["Groupld"].toInt();
  1245. //
  1246. // // 如果组变化,插入分隔线
  1247. // if (previousGroupId != -1 && groupId != previousGroupId) {
  1248. // QFrame *separator = createUnifiedSeparator(scrollWidget, 2);
  1249. // QHBoxLayout *separatorLayout = new QHBoxLayout;
  1250. // separatorLayout->setContentsMargins(0, 5, 0, 5);
  1251. // separatorLayout->addWidget(separator);
  1252. // scrollLayout->addLayout(separatorLayout);
  1253. // separator->show();
  1254. // }
  1255. // previousGroupId = groupId;
  1256. //
  1257. // // 每个字段一行
  1258. // QHBoxLayout *fieldLayout = new QHBoxLayout;
  1259. // fieldLayout->setSpacing(5);
  1260. //
  1261. // QLabel *label = new QLabel(fieldName);
  1262. // label->setAlignment(Qt::AlignVCenter | Qt::AlignLeft);
  1263. // label->setFixedHeight(24);
  1264. // label->setMinimumWidth(120);
  1265. // fieldLayout->addWidget(label);
  1266. // fieldLayout->addStretch(1);
  1267. //
  1268. // QWidget *rightWidget = new QWidget;
  1269. // QHBoxLayout *rightLayout = new QHBoxLayout(rightWidget);
  1270. // rightLayout->setContentsMargins(0, 0, 20, 0);
  1271. // rightLayout->setSpacing(5);
  1272. //
  1273. // QWidget *createdWidget = nullptr;
  1274. //
  1275. // if (fieldType == "input") {
  1276. // // 检查 Value 中是否包含逗号分隔(比如 "请输入内容1,分钟")
  1277. // if (fieldUnits!= "") {
  1278. // // // 采用逗号分割,trim去除空白
  1279. // // QStringList parts = fieldValue.split(",", Qt::SkipEmptyParts);
  1280. // // QString leftText = parts.at(0).trimmed();
  1281. // // QString rightText = (parts.size() > 1 ? parts.at(1).trimmed() : QString());
  1282. //
  1283. // // 创建 QLineEdit 显示左边文本
  1284. // QLineEdit *lineEdit = new QLineEdit();
  1285. // if((fieldUpLimit=="")&&(fieldDownLimit=="")){
  1286. // lineEdit->setText(fieldValue);
  1287. // lineEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
  1288. // lineEdit->setFixedSize(105, 28);
  1289. // lineEdit->setAlignment(Qt::AlignLeft);
  1290. // //!!!提到前面
  1291. // lineEdit->setStyleSheet(R"(
  1292. // QLineEdit {
  1293. // background: #FFFFFF;
  1294. // border: 1px solid #BABBDC;
  1295. // border-radius: 6px;
  1296. // padding: 2px 5px;
  1297. // }
  1298. // )");
  1299. // }else{//上下限至少设置了一个值
  1300. // if(fieldValue == ""){
  1301. // lineEdit->setPlaceholderText("请输入数字");
  1302. // }else{
  1303. // lineEdit->setText(fieldValue);
  1304. // }
  1305. //
  1306. //
  1307. // lineEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
  1308. // lineEdit->setFixedSize(105, 28);
  1309. // lineEdit->setAlignment(Qt::AlignLeft);
  1310. // lineEdit->setStyleSheet(R"(
  1311. // QLineEdit {
  1312. // background: #FFFFFF;
  1313. // border: 1px solid #BABBDC;
  1314. // border-radius: 6px;
  1315. // padding: 2px 5px;
  1316. // }
  1317. // )");
  1318. // }
  1319. //
  1320. // // 创建 QLabel 显示右边文本(比如 "分钟")
  1321. // QLabel *unitLabel = new QLabel(fieldUnits);
  1322. // unitLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
  1323. // unitLabel->setFixedHeight(28);
  1324. // unitLabel->setStyleSheet("QLabel { font-size: 14px; }");
  1325. //
  1326. // // 把 QLineEdit 和 QLabel 水平排列,确保 Qlabel 显示在 QLineEdit 右侧
  1327. // QHBoxLayout *inputLayout = new QHBoxLayout;
  1328. // inputLayout->setSpacing(10);
  1329. // inputLayout->setContentsMargins(0,0,0,0);
  1330. // inputLayout->addWidget(lineEdit);
  1331. // inputLayout->addWidget(unitLabel);
  1332. //
  1333. // // 用一个容器部件来装下这个布局
  1334. // QWidget *inputWidget = new QWidget;
  1335. // inputWidget->setLayout(inputLayout);
  1336. // rightLayout->addWidget(inputWidget);
  1337. //
  1338. // // 保存控件指针到 m_fieldWidgets(可选)
  1339. // m_fieldWidgets.append(lineEdit);
  1340. // m_fieldWidgets.append(unitLabel);
  1341. // createdWidget = inputWidget;
  1342. // // // 连接 textChanged 信号到 lambda 函数
  1343. // connect(lineEdit, &QLineEdit::textChanged, [this,lineEdit,fieldTableName,fieldId,fieldUpLimit,fieldDownLimit]() {
  1344. // if((fieldUpLimit!="")&&(fieldDownLimit!="")){
  1345. // int uplimit = fieldUpLimit.toInt();
  1346. // int downlimit = fieldDownLimit.toInt();
  1347. // int inputvalue = (lineEdit->text()).toInt();
  1348. // if((inputvalue<=uplimit)&&(inputvalue>=downlimit)){
  1349. // updateDb(fieldTableName,fieldId,lineEdit->text());
  1350. // }else{
  1351. // lineEdit->setPlaceholderText("超出设定范围,请重新输入");
  1352. // }
  1353. // }else if(fieldDownLimit!=""){
  1354. // int downlimit = fieldDownLimit.toInt();
  1355. // int inputvalue = (lineEdit->text()).toInt();
  1356. // if(inputvalue>=downlimit){
  1357. // updateDb(fieldTableName,fieldId,lineEdit->text());
  1358. // }else{
  1359. // lineEdit->setPlaceholderText("超出设定范围,请重新输入");
  1360. // }
  1361. // }else if(fieldUpLimit!=""){
  1362. // int uplimit = fieldUpLimit.toInt();
  1363. // int inputvalue = (lineEdit->text()).toInt();
  1364. // if(inputvalue<=uplimit){
  1365. // updateDb(fieldTableName,fieldId,lineEdit->text());
  1366. // }else{
  1367. // lineEdit->setPlaceholderText("超出设定范围,请重新输入");
  1368. // }
  1369. // }else{
  1370. // updateDb(fieldTableName,fieldId,lineEdit->text());
  1371. // }
  1372. //
  1373. // });
  1374. // } else {
  1375. // // 如果不包含逗号,按原有逻辑只创建 QLineEdit
  1376. // QLineEdit *lineEdit1 = new QLineEdit(fieldValue);
  1377. // lineEdit1->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
  1378. // lineEdit1->setFixedSize(140, 28);
  1379. // lineEdit1->setAlignment(Qt::AlignLeft);
  1380. // lineEdit1->setStyleSheet(R"(
  1381. // QLineEdit {
  1382. // background: #FFFFFF;
  1383. // border: 1px solid #BABBDC;
  1384. // border-radius: 6px;
  1385. // padding: 2px 5px;
  1386. // }
  1387. // )");
  1388. // rightLayout->addWidget(lineEdit1);
  1389. // createdWidget = lineEdit1;
  1390. // // // 连接 textChanged 信号到 lambda 函数
  1391. // connect(lineEdit1, &QLineEdit::textChanged, [this,lineEdit1,fieldTableName,fieldId]() {
  1392. // updateDb(fieldTableName,fieldId,lineEdit1->text());
  1393. // loginput(fieldTableName,fieldId,lineEdit1->text());
  1394. // });
  1395. // }
  1396. // }
  1397. // else if (fieldType == "radio") {
  1398. // // 用逗号分隔的选项
  1399. // QStringList optionList = fieldValue.split(QRegExp("[,,]"), Qt::SkipEmptyParts);
  1400. // QHBoxLayout *radioLayout = new QHBoxLayout;
  1401. // radioLayout->setSpacing(5);
  1402. // QButtonGroup *radioGroup = new QButtonGroup(rightWidget);
  1403. // for (const QString &optionText : optionList) {
  1404. // QString trimmedOpt = optionText.trimmed();
  1405. // if (trimmedOpt.isEmpty()) continue;
  1406. // QRadioButton *radioButton = new QRadioButton(trimmedOpt);
  1407. // radioGroup->addButton(radioButton);
  1408. // radioLayout->addWidget(radioButton);
  1409. // m_fieldWidgets.append(radioButton);
  1410. // if(fieldValue == trimmedOpt){
  1411. // radioButton->setChecked(true);
  1412. // }
  1413. // }
  1414. // // 使用Lambda函数连接 buttonToggled 信号
  1415. // connect(radioGroup, static_cast<void (QButtonGroup::*)(QAbstractButton *, bool)>(&QButtonGroup::buttonToggled),[this,fieldTableName,fieldId](QAbstractButton *button,bool checked) {
  1416. // if (checked) {
  1417. // // qDebug() << "Button toggled:" << button->text();
  1418. // // 在这里添加你想要执行的操作
  1419. // updateDb(fieldTableName,fieldId,button->text());
  1420. // }
  1421. // });
  1422. // rightLayout->addLayout(radioLayout);
  1423. // }
  1424. // else if (fieldType == "checkbox") {
  1425. // bool isChecked;
  1426. // if(fieldValue == ""){
  1427. // isChecked = (fieldValue == "1");
  1428. // }else{
  1429. // isChecked = (fieldValue == "1");
  1430. // }
  1431. //
  1432. // QCheckBox *checkBox = new QCheckBox;
  1433. // checkBox->setChecked(isChecked);
  1434. // checkBox->setStyleSheet(R"(
  1435. // QCheckBox::indicator {
  1436. // width: 20px;
  1437. // height: 20px;
  1438. // }
  1439. // QCheckBox::indicator:unchecked {
  1440. // background-color: #FFFFFF;
  1441. // border: 1px solid #BABBDC;
  1442. // border-radius: 2px;
  1443. // }
  1444. // QCheckBox::indicator:checked {
  1445. // image: url(:/images/three_Selecte.png);
  1446. // }
  1447. // QCheckBox {
  1448. // spacing: 0px;
  1449. // }
  1450. // )");
  1451. // rightLayout->addWidget(checkBox);
  1452. // createdWidget = checkBox;
  1453. // connect(checkBox, &QCheckBox::stateChanged, [this,fieldTableName,fieldId](int state) {
  1454. // if (state == Qt::Checked) {
  1455. // // qDebug() << "CheckBox is checked"<<fieldId;
  1456. // updateDb(fieldTableName,fieldId,"1");
  1457. // } else if (state == Qt::Unchecked) {
  1458. // // qDebug() << "CheckBox is unchecked"<<fieldId;
  1459. // // 在这里添加你想要执行的操作
  1460. // updateDb(fieldTableName,fieldId,"0");
  1461. // } else if (state == Qt::PartiallyChecked) {
  1462. // qDebug() << "CheckBox is partially checked";
  1463. // // 在这里添加你想要执行的操作
  1464. // }
  1465. // });
  1466. // }
  1467. // else if (fieldType == "ComboBox") {
  1468. // QStringList optionList = fieldValue.split(QRegExp("[,,]"), Qt::SkipEmptyParts);
  1469. // QComboBox *comboBox = new QComboBox;
  1470. // comboBox->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
  1471. // comboBox->setFixedSize(140, 28);
  1472. // comboBox->setStyleSheet(R"(
  1473. // QComboBox {
  1474. // background: #FFFFFF;
  1475. // border: 1px solid #BABBDC;
  1476. // border-radius: 6px;
  1477. // padding: 2px 5px;
  1478. // }
  1479. // QComboBox::drop-down {
  1480. // width: 20px;
  1481. // }
  1482. // )");
  1483. // // int newIndex = -1;
  1484. // for (const QString &opt : optionList) {
  1485. // QString trimmedOpt = opt.trimmed();
  1486. // if (!trimmedOpt.isEmpty())
  1487. // comboBox->addItem(trimmedOpt);
  1488. // }
  1489. // if(fieldValue == ""){
  1490. // comboBox->setCurrentIndex(0);
  1491. // }else{
  1492. // comboBox->setCurrentIndex(fieldValue.toInt());
  1493. // }
  1494. // rightLayout->addWidget(comboBox);
  1495. // createdWidget = comboBox;
  1496. // // 使用Lambda函数连接 currentIndexChanged 信号
  1497. // connect(comboBox, QOverload<int>::of(&QComboBox::currentIndexChanged), [this,comboBox,fieldTableName,fieldId](int index) {
  1498. // QString selectedText = comboBox->itemText(index);
  1499. // QString indexString = QString::number(index);
  1500. // // qDebug() << "ComboBox index changed to:" << index << "Text:" << selectedText;
  1501. // updateDb(fieldTableName,fieldId,indexString);
  1502. // // 在这里添加你想要执行的操作
  1503. // });
  1504. // }
  1505. // else if (fieldType == "time") {
  1506. // QTimeEdit *timeEdit = new QTimeEdit;
  1507. // timeEdit->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
  1508. // timeEdit->setFixedSize(120, 28);
  1509. // timeEdit->setStyleSheet(R"(
  1510. // QTimeEdit {
  1511. // background: #FFFFFF;
  1512. // border: 1px solid #BABBDC;
  1513. // border-radius: 6px;
  1514. // padding: 2px 5px;
  1515. // }
  1516. // )");
  1517. // timeEdit->setDisplayFormat("HH:mm:ss");
  1518. // timeEdit->setTime(QTime::fromString(fieldValue, "HH:mm:ss"));
  1519. // rightLayout->addWidget(timeEdit);
  1520. // createdWidget = timeEdit;
  1521. // }
  1522. // else if (fieldType == "switch") {
  1523. // QWidget *switchContainer = new QWidget;
  1524. // QHBoxLayout *switchLayout = new QHBoxLayout(switchContainer);
  1525. // switchLayout->setSpacing(5);
  1526. // switchLayout->setContentsMargins(0, 0, 0, 0);
  1527. // QCheckBox *switchBox = new QCheckBox;
  1528. // switchBox->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
  1529. // switchBox->setFixedSize(30, 30);
  1530. // switchBox->setStyleSheet(R"(
  1531. // QCheckBox::indicator {
  1532. // width: 30px;
  1533. // height: 30px;
  1534. // }
  1535. // QCheckBox::indicator:unchecked {
  1536. // background-color: #BABBDC;
  1537. // border-radius: 6px;
  1538. // }
  1539. // QCheckBox::indicator:checked {
  1540. // background-color: #4CAF50;
  1541. // border-radius: 6px;
  1542. // }
  1543. // )");
  1544. // QString switchValue;
  1545. //
  1546. // if(fieldValue ==""){
  1547. // switchValue = fieldValue.toLower();
  1548. // }else{
  1549. // switchValue = fieldValue.toLower();
  1550. // }
  1551. // if (switchValue == "on")
  1552. // switchBox->setChecked(true);
  1553. // else
  1554. // switchBox->setChecked(false);
  1555. // QLabel *switchLabel = new QLabel(switchBox->isChecked() ? "开" : "关");
  1556. // switchLabel->setAlignment(Qt::AlignVCenter | Qt::AlignLeft);
  1557. // switchLabel->setStyleSheet("font-size: 14px;");
  1558. // connect(switchBox, &QCheckBox::stateChanged, [fieldTableName,fieldId,switchLabel, this](int state){
  1559. // if (state == Qt::Checked){
  1560. // switchLabel->setText("开");
  1561. // updateDb(fieldTableName,fieldId,"on");
  1562. // }else{
  1563. // switchLabel->setText("关");
  1564. // updateDb(fieldTableName,fieldId,"off");
  1565. // }
  1566. // });
  1567. // switchLayout->addWidget(switchBox);
  1568. // switchLayout->addWidget(switchLabel);
  1569. // rightLayout->addWidget(switchContainer);
  1570. // createdWidget = switchBox;
  1571. // }
  1572. // else if (fieldType == "combined") {
  1573. // // 用逗号分隔
  1574. // QStringList parts = fieldValue.split(QRegExp("[,,]"), Qt::SkipEmptyParts);
  1575. // QString inputText = (parts.size() >= 1 ? parts.at(0).trimmed() : QString());
  1576. // QString button1Text = (parts.size() >= 2 ? parts.at(1).trimmed() : QStringLiteral("获取"));
  1577. // QString button2Text = (parts.size() >= 3 ? parts.at(2).trimmed() : QStringLiteral("设置"));
  1578. //
  1579. // QLineEdit *comboInput = new QLineEdit(inputText);
  1580. // comboInput->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
  1581. // comboInput->setFixedHeight(28);
  1582. // comboInput->setStyleSheet(R"(
  1583. // QLineEdit {
  1584. // background: #FFFFFF;
  1585. // border: 1px solid #BABBDC;
  1586. // border-radius: 5px;
  1587. // padding: 2px 5px;
  1588. // }
  1589. // )");
  1590. // m_fieldWidgets.append(comboInput);
  1591. //
  1592. // QWidget *combinedWidget = new QWidget;
  1593. // QHBoxLayout *combinedLayout = new QHBoxLayout(combinedWidget);
  1594. // combinedLayout->setSpacing(5);
  1595. // combinedLayout->setContentsMargins(0, 0, 0, 0);
  1596. //
  1597. // QPushButton *btnGet = new QPushButton(button1Text, combinedWidget);
  1598. // btnGet->setFixedSize(80, 28);
  1599. // btnGet->setStyleSheet(R"(
  1600. // QPushButton {
  1601. // background: #FFFFFF;
  1602. // border: 1px solid #BABBDC;
  1603. // border-radius: 5px;
  1604. // }
  1605. // QPushButton:hover {
  1606. // background-color: #F0F0F0;
  1607. // }
  1608. // )");
  1609. // m_fieldWidgets.append(btnGet);
  1610. //
  1611. // QPushButton *btnSet = new QPushButton(button2Text, combinedWidget);
  1612. // btnSet->setFixedSize(80, 28);
  1613. // btnSet->setStyleSheet(R"(
  1614. // QPushButton {
  1615. // background: #FFFFFF;
  1616. // border: 1px solid #BABBDC;
  1617. // border-radius: 5px;
  1618. // }
  1619. // QPushButton:hover {
  1620. // background-color: #F0F0F0;
  1621. // }
  1622. // )");
  1623. // m_fieldWidgets.append(btnSet);
  1624. //
  1625. // combinedLayout->addWidget(comboInput);
  1626. // combinedLayout->addWidget(btnGet);
  1627. // combinedLayout->addWidget(btnSet);
  1628. // combinedLayout->addStretch();
  1629. // rightLayout->addWidget(combinedWidget);
  1630. // }
  1631. // else if (fieldType == "Text")
  1632. // {
  1633. // QLabel* label = new QLabel();
  1634. // label->setText(fieldValue);
  1635. // label->setAlignment(Qt::AlignVCenter | Qt::AlignLeft);
  1636. // label->setFixedHeight(24);
  1637. // label->setMinimumWidth(120);
  1638. // rightLayout->addWidget(label);
  1639. //
  1640. // } else
  1641. // {
  1642. // qWarning() << "未知字段类型:" << fieldType;
  1643. // }
  1644. //
  1645. // if (createdWidget)
  1646. // m_fieldWidgets.append(createdWidget);
  1647. //
  1648. // fieldLayout->addWidget(rightWidget);
  1649. // scrollLayout->addLayout(fieldLayout);
  1650. // }
  1651. // }
  1652. //
  1653. // scrollLayout->addStretch();
  1654. //
  1655. // scrollArea->setWidget(scrollWidget);
  1656. //
  1657. // QVBoxLayout *mainLayout = new QVBoxLayout(m_thirdLevelFieldWnd);
  1658. // mainLayout->setContentsMargins(0, 0, 0, 0);
  1659. // mainLayout->addWidget(scrollArea);
  1660. //
  1661. // m_thirdLevelFieldWnd->show();
  1662. //}
  1663. void DbTreeViewManager::displayThirdLevelFields(const QList<Table_Control_Data>& data, bool allChangeFlag, bool isHome)
  1664. {
  1665. if (data.isEmpty())
  1666. {
  1667. qWarning() << "字段数据为空,无法显示";
  1668. return;
  1669. }
  1670. //遍历得到结果allChangeFalg
  1671. // 若已有第三层窗口,先关闭并释放
  1672. if (m_thirdLevelFieldWnd)
  1673. {
  1674. m_thirdLevelFieldWnd->close();
  1675. m_thirdLevelFieldWnd->deleteLater();
  1676. m_thirdLevelFieldWnd = nullptr;
  1677. }
  1678. // 获取当前选中的目录项,设置状态
  1679. QModelIndex currentIndex = treeViewDown->currentIndex();
  1680. if (currentIndex.isValid())
  1681. {
  1682. QStandardItem* currentItem = model->itemFromIndex(currentIndex);
  1683. if (currentItem)
  1684. {
  1685. currentItem->setCheckState(allChangeFlag ? Qt::Checked : Qt::Unchecked);
  1686. qDebug() << "设置当前目录项复选框状态为:" << (allChangeFlag ? "Checked" : "Unchecked");
  1687. }
  1688. } else
  1689. {
  1690. qWarning() << "当前没有选中的目录项";
  1691. }
  1692. // 隐藏目录树及所有分隔线
  1693. treeViewDown->hide();
  1694. for (auto separator : firstLevelSeparators)
  1695. {
  1696. if (separator)
  1697. separator->hide();
  1698. }
  1699. // 清理之前的控件列表
  1700. m_fieldWidgets.clear();
  1701. // 创建新的 "字段展示" 窗口,并赋值给 m_thirdLevelFieldWnd
  1702. m_thirdLevelFieldWnd = new QWidget(widget2);
  1703. m_thirdLevelFieldWnd->setWindowTitle("字段展示");
  1704. m_thirdLevelFieldWnd->setStyleSheet("background-color: transparent;");
  1705. if (isHome)
  1706. {
  1707. // Home 界面,填满 widget2
  1708. m_thirdLevelFieldWnd->setGeometry(0, 0, widget2->width(), widget2->height());
  1709. } else
  1710. {
  1711. // 其他配置界面,设置与 treeViewDown 相同的几何位置
  1712. m_thirdLevelFieldWnd->setGeometry(treeViewDown->geometry());
  1713. }
  1714. // 创建滚动区域
  1715. QScrollArea* scrollArea = new QScrollArea(m_thirdLevelFieldWnd);
  1716. scrollArea->setWidgetResizable(true);
  1717. scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
  1718. // 创建滚动容器并设置布局
  1719. QWidget* scrollWidget = new QWidget;
  1720. scrollWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
  1721. QVBoxLayout* scrollLayout = new QVBoxLayout(scrollWidget);
  1722. scrollLayout->setSpacing(10);
  1723. scrollLayout->setContentsMargins(10, 10, 10, 10);
  1724. //不同表格的全部插进来
  1725. int previousGroupId = -1;
  1726. QList<CONFIG_BASE_STRUCT> buttonControls;
  1727. for (const Table_Control_Data& tableData : data)
  1728. {
  1729. //遍历控件list
  1730. for (const CONFIG_BASE_STRUCT& control : tableData.controlDataList)
  1731. {
  1732. //判断特殊的按钮控件
  1733. if (control.strType == "Button")
  1734. {
  1735. buttonControls.append(control);
  1736. }
  1737. else
  1738. {
  1739. //先创建label
  1740. //int fieldId = control.iId;
  1741. QString fieldName = QString::fromStdString(control.strName);
  1742. QString fieldDescribe = QString::fromStdString(control.strDescribe);
  1743. QString fieldType = QString::fromStdString(control.strType);
  1744. QString fieldValue = QString::fromStdString(control.strValue);
  1745. QString fieldUpLimit = QString::fromStdString(control.strUpperLimit);
  1746. QString fieldDownLimit = QString::fromStdString(control.strLowerLimit);
  1747. QString fieldUnits = QString::fromStdString(control.strUnit);
  1748. int groupId = control.iGroupId;
  1749. QString fieldTableName = tableData.tableName;
  1750. // 如果组变化,插入分隔线
  1751. if (previousGroupId != -1 && groupId != previousGroupId)
  1752. {
  1753. QFrame* separator = createUnifiedSeparator(scrollWidget, 2);
  1754. QHBoxLayout* separatorLayout = new QHBoxLayout;
  1755. separatorLayout->setContentsMargins(0, 5, 0, 5);
  1756. separatorLayout->addWidget(separator);
  1757. scrollLayout->addLayout(separatorLayout);
  1758. separator->show();
  1759. }
  1760. previousGroupId = groupId;
  1761. // 每个字段一行
  1762. QHBoxLayout* fieldLayout = new QHBoxLayout;
  1763. fieldLayout->setSpacing(5);
  1764. QLabel* label = new QLabel(fieldDescribe);
  1765. label->setAlignment(Qt::AlignVCenter | Qt::AlignLeft);
  1766. label->setFixedHeight(24);
  1767. label->setMinimumWidth(120);
  1768. fieldLayout->addWidget(label);
  1769. fieldLayout->addStretch(1);
  1770. QWidget* rightWidget = new QWidget;
  1771. QHBoxLayout* rightLayout = new QHBoxLayout(rightWidget);
  1772. rightLayout->setContentsMargins(0, 0, 20, 0);
  1773. rightLayout->setSpacing(5);
  1774. QWidget* createdWidget = nullptr;
  1775. if (fieldType == "input" || fieldType == "POSITION" || fieldType == "LONG" || fieldType == "STRING")
  1776. {
  1777. // 检查 Value 中是否包含逗号分隔(比如 "请输入内容1,分钟")
  1778. if (fieldUnits != "")
  1779. {
  1780. // // 采用逗号分割,trim去除空白
  1781. // QStringList parts = fieldValue.split(",", Qt::SkipEmptyParts);
  1782. // QString leftText = parts.at(0).trimmed();
  1783. // QString rightText = (parts.size() > 1 ? parts.at(1).trimmed() : QString());
  1784. // 创建 QLineEdit 显示左边文本
  1785. QLineEdit* lineEdit = new QLineEdit();
  1786. if ((fieldUpLimit == "") && (fieldDownLimit == ""))
  1787. {
  1788. lineEdit->setText(fieldValue);
  1789. lineEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
  1790. lineEdit->setFixedSize(105, 28);
  1791. lineEdit->setAlignment(Qt::AlignLeft);
  1792. //!!!提到前面
  1793. lineEdit->setStyleSheet(R"(
  1794. QLineEdit {
  1795. background: #FFFFFF;
  1796. border: 1px solid #BABBDC;
  1797. border-radius: 6px;
  1798. padding: 2px 5px;
  1799. }
  1800. )");
  1801. } else
  1802. {//上下限至少设置了一个值
  1803. if (fieldValue == "")
  1804. {
  1805. lineEdit->setPlaceholderText("请输入数字");
  1806. } else
  1807. {
  1808. lineEdit->setText(fieldValue);
  1809. }
  1810. lineEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
  1811. lineEdit->setFixedSize(105, 28);
  1812. lineEdit->setAlignment(Qt::AlignLeft);
  1813. lineEdit->setStyleSheet(R"(
  1814. QLineEdit {
  1815. background: #FFFFFF;
  1816. border: 1px solid #BABBDC;
  1817. border-radius: 6px;
  1818. padding: 2px 5px;
  1819. }
  1820. )");
  1821. }
  1822. // 创建 QLabel 显示右边文本(比如 "分钟")
  1823. QLabel* unitLabel = new QLabel(fieldUnits);
  1824. unitLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
  1825. unitLabel->setFixedHeight(28);
  1826. unitLabel->setStyleSheet("QLabel { font-size: 14px; }");
  1827. // 把 QLineEdit 和 QLabel 水平排列,确保 Qlabel 显示在 QLineEdit 右侧
  1828. QHBoxLayout* inputLayout = new QHBoxLayout;
  1829. inputLayout->setSpacing(10);
  1830. inputLayout->setContentsMargins(0, 0, 0, 0);
  1831. inputLayout->addWidget(lineEdit);
  1832. inputLayout->addWidget(unitLabel);
  1833. // 用一个容器部件来装下这个布局
  1834. QWidget* inputWidget = new QWidget;
  1835. inputWidget->setLayout(inputLayout);
  1836. rightLayout->addWidget(inputWidget);
  1837. // 保存控件指针到 m_fieldWidgets(可选)
  1838. m_fieldWidgets.append(lineEdit);
  1839. m_fieldWidgets.append(unitLabel);
  1840. createdWidget = inputWidget;
  1841. // // 连接 textChanged 信号到 lambda 函数
  1842. connect(lineEdit, &QLineEdit::textChanged, [this, lineEdit, fieldTableName, fieldName, fieldUpLimit, fieldDownLimit]() {
  1843. if ((fieldUpLimit != "") && (fieldDownLimit != ""))
  1844. {
  1845. int uplimit = fieldUpLimit.toInt();
  1846. int downlimit = fieldDownLimit.toInt();
  1847. int inputvalue = (lineEdit->text()).toInt();
  1848. if ((inputvalue <= uplimit) && (inputvalue >= downlimit))
  1849. {
  1850. m_sqlOper->updateControlData(fieldTableName, fieldName, lineEdit->text());
  1851. //updateDb(fieldTableName, fieldId, lineEdit->text());
  1852. } else
  1853. {
  1854. lineEdit->setPlaceholderText("超出设定范围,请重新输入");
  1855. }
  1856. } else if (fieldDownLimit != "")
  1857. {
  1858. int downlimit = fieldDownLimit.toInt();
  1859. int inputvalue = (lineEdit->text()).toInt();
  1860. if (inputvalue >= downlimit)
  1861. {
  1862. m_sqlOper->updateControlData(fieldTableName, fieldName, lineEdit->text());
  1863. //updateDb(fieldTableName, fieldId, lineEdit->text());
  1864. } else
  1865. {
  1866. lineEdit->setPlaceholderText("超出设定范围,请重新输入");
  1867. }
  1868. } else if (fieldUpLimit != "")
  1869. {
  1870. int uplimit = fieldUpLimit.toInt();
  1871. int inputvalue = (lineEdit->text()).toInt();
  1872. if (inputvalue <= uplimit)
  1873. {
  1874. m_sqlOper->updateControlData(fieldTableName, fieldName, lineEdit->text());
  1875. //updateDb(fieldTableName, fieldId, lineEdit->text());
  1876. } else
  1877. {
  1878. lineEdit->setPlaceholderText("超出设定范围,请重新输入");
  1879. }
  1880. } else
  1881. {
  1882. m_sqlOper->updateControlData(fieldTableName, fieldName, lineEdit->text());
  1883. //updateDb(fieldTableName, fieldId, lineEdit->text());
  1884. }
  1885. });
  1886. }
  1887. else
  1888. {
  1889. // 如果不包含逗号,按原有逻辑只创建 QLineEdit
  1890. QLineEdit* lineEdit1 = new QLineEdit(fieldValue);
  1891. lineEdit1->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
  1892. lineEdit1->setFixedSize(140, 28);
  1893. lineEdit1->setAlignment(Qt::AlignLeft);
  1894. lineEdit1->setStyleSheet(R"(
  1895. QLineEdit {
  1896. background: #FFFFFF;
  1897. border: 1px solid #BABBDC;
  1898. border-radius: 6px;
  1899. padding: 2px 5px;
  1900. }
  1901. )");
  1902. rightLayout->addWidget(lineEdit1);
  1903. createdWidget = lineEdit1;
  1904. // // 连接 textChanged 信号到 lambda 函数
  1905. connect(lineEdit1, &QLineEdit::textChanged, [this, lineEdit1, fieldTableName, fieldName]() {
  1906. m_sqlOper->updateControlData(fieldTableName, fieldName, lineEdit1->text());
  1907. //updateDb(fieldTableName, fieldId, lineEdit1->text());
  1908. loginput(fieldTableName, fieldName, lineEdit1->text());
  1909. });
  1910. }
  1911. }
  1912. else if (fieldType == "radio")
  1913. {
  1914. // 用逗号分隔的选项
  1915. QStringList optionList = fieldUnits.split(QRegExp("[,,]"), Qt::SkipEmptyParts);
  1916. QHBoxLayout* radioLayout = new QHBoxLayout;
  1917. radioLayout->setSpacing(5);
  1918. QButtonGroup* radioGroup = new QButtonGroup(rightWidget);
  1919. for (const QString& optionText : optionList)
  1920. {
  1921. QString trimmedOpt = optionText.trimmed();
  1922. if (trimmedOpt.isEmpty()) continue;
  1923. QRadioButton* radioButton = new QRadioButton(trimmedOpt);
  1924. radioGroup->addButton(radioButton);
  1925. radioLayout->addWidget(radioButton);
  1926. m_fieldWidgets.append(radioButton);
  1927. if (fieldValue == trimmedOpt)
  1928. {
  1929. radioButton->setChecked(true);
  1930. }
  1931. }
  1932. // 使用Lambda函数连接 buttonToggled 信号
  1933. connect(radioGroup, static_cast<void (QButtonGroup::*)(QAbstractButton*, bool)>(&QButtonGroup::buttonToggled), [this, fieldTableName, fieldName](QAbstractButton* button, bool checked) {
  1934. if (checked)
  1935. {
  1936. // qDebug() << "Button toggled:" << button->text();
  1937. // 在这里添加你想要执行的操作
  1938. m_sqlOper->updateControlData(fieldTableName, fieldName, button->text());
  1939. //updateDb(fieldTableName, fieldId, button->text());
  1940. }
  1941. });
  1942. rightLayout->addLayout(radioLayout);
  1943. }
  1944. else if (fieldType == "checkbox" || fieldType == "BOOL")
  1945. {
  1946. bool isChecked;
  1947. if (fieldValue == "")
  1948. {
  1949. isChecked = (fieldValue == "1");
  1950. } else
  1951. {
  1952. isChecked = (fieldValue == "1");
  1953. }
  1954. QCheckBox* checkBox = new QCheckBox;
  1955. checkBox->setChecked(isChecked);
  1956. checkBox->setStyleSheet(R"(
  1957. QCheckBox::indicator {
  1958. width: 20px;
  1959. height: 20px;
  1960. }
  1961. QCheckBox::indicator:unchecked {
  1962. background-color: #FFFFFF;
  1963. border: 1px solid #BABBDC;
  1964. border-radius: 2px;
  1965. }
  1966. QCheckBox::indicator:checked {
  1967. image: url(:/images/three_Selecte.png);
  1968. }
  1969. QCheckBox {
  1970. spacing: 0px;
  1971. }
  1972. )");
  1973. rightLayout->addWidget(checkBox);
  1974. createdWidget = checkBox;
  1975. connect(checkBox, &QCheckBox::stateChanged, [this, fieldTableName, fieldName](int state) {
  1976. if (state == Qt::Checked)
  1977. {
  1978. // qDebug() << "CheckBox is checked"<<fieldId;
  1979. m_sqlOper->updateControlData(fieldTableName, fieldName, "1");
  1980. //updateDb(fieldTableName, fieldId, "1");
  1981. } else if (state == Qt::Unchecked)
  1982. {
  1983. // qDebug() << "CheckBox is unchecked"<<fieldId;
  1984. // 在这里添加你想要执行的操作
  1985. m_sqlOper->updateControlData(fieldTableName, fieldName, "0");
  1986. //updateDb(fieldTableName, fieldId, "0");
  1987. } else if (state == Qt::PartiallyChecked)
  1988. {
  1989. qDebug() << "CheckBox is partially checked";
  1990. // 在这里添加你想要执行的操作
  1991. }
  1992. });
  1993. }
  1994. else if (fieldType == "ComboBox")
  1995. {
  1996. QStringList optionList = fieldValue.split(QRegExp("[,,]"), Qt::SkipEmptyParts);
  1997. QComboBox* comboBox = new QComboBox;
  1998. comboBox->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
  1999. comboBox->setFixedSize(140, 28);
  2000. comboBox->setStyleSheet(R"(
  2001. QComboBox {
  2002. background: #FFFFFF;
  2003. border: 1px solid #BABBDC;
  2004. border-radius: 6px;
  2005. padding: 2px 5px;
  2006. }
  2007. QComboBox::drop-down {
  2008. width: 20px;
  2009. }
  2010. )");
  2011. // int newIndex = -1;
  2012. for (const QString& opt : optionList)
  2013. {
  2014. QString trimmedOpt = opt.trimmed();
  2015. if (!trimmedOpt.isEmpty())
  2016. comboBox->addItem(trimmedOpt);
  2017. }
  2018. if (fieldValue == "")
  2019. {
  2020. comboBox->setCurrentIndex(0);
  2021. } else
  2022. {
  2023. comboBox->setCurrentIndex(fieldValue.toInt());
  2024. }
  2025. rightLayout->addWidget(comboBox);
  2026. createdWidget = comboBox;
  2027. // 使用Lambda函数连接 currentIndexChanged 信号
  2028. connect(comboBox, QOverload<int>::of(&QComboBox::currentIndexChanged), [this, comboBox, fieldTableName, fieldName](int index) {
  2029. QString selectedText = comboBox->itemText(index);
  2030. QString indexString = QString::number(index);
  2031. // qDebug() << "ComboBox index changed to:" << index << "Text:" << selectedText;
  2032. m_sqlOper->updateControlData(fieldTableName, fieldName, indexString);
  2033. //updateDb(fieldTableName, fieldId, indexString);
  2034. // 在这里添加你想要执行的操作
  2035. });
  2036. } else if (fieldType == "time")
  2037. {
  2038. QTimeEdit* timeEdit = new QTimeEdit;
  2039. timeEdit->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
  2040. timeEdit->setFixedSize(120, 28);
  2041. timeEdit->setStyleSheet(R"(
  2042. QTimeEdit {
  2043. background: #FFFFFF;
  2044. border: 1px solid #BABBDC;
  2045. border-radius: 6px;
  2046. padding: 2px 5px;
  2047. }
  2048. )");
  2049. timeEdit->setDisplayFormat("HH:mm:ss");
  2050. timeEdit->setTime(QTime::fromString(fieldValue, "HH:mm:ss"));
  2051. rightLayout->addWidget(timeEdit);
  2052. createdWidget = timeEdit;
  2053. } else if (fieldType == "switch")
  2054. {
  2055. QWidget* switchContainer = new QWidget;
  2056. QHBoxLayout* switchLayout = new QHBoxLayout(switchContainer);
  2057. switchLayout->setSpacing(5);
  2058. switchLayout->setContentsMargins(0, 0, 0, 0);
  2059. QCheckBox* switchBox = new QCheckBox;
  2060. switchBox->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
  2061. switchBox->setFixedSize(30, 30);
  2062. switchBox->setStyleSheet(R"(
  2063. QCheckBox::indicator {
  2064. width: 30px;
  2065. height: 30px;
  2066. }
  2067. QCheckBox::indicator:unchecked {
  2068. background-color: #BABBDC;
  2069. border-radius: 6px;
  2070. }
  2071. QCheckBox::indicator:checked {
  2072. background-color: #4CAF50;
  2073. border-radius: 6px;
  2074. }
  2075. )");
  2076. QString switchValue;
  2077. if (fieldValue == "")
  2078. {
  2079. switchValue = fieldValue.toLower();
  2080. } else
  2081. {
  2082. switchValue = fieldValue.toLower();
  2083. }
  2084. if (switchValue == "on")
  2085. switchBox->setChecked(true);
  2086. else
  2087. switchBox->setChecked(false);
  2088. QLabel* switchLabel = new QLabel(switchBox->isChecked() ? "开" : "关");
  2089. switchLabel->setAlignment(Qt::AlignVCenter | Qt::AlignLeft);
  2090. switchLabel->setStyleSheet("font-size: 14px;");
  2091. connect(switchBox, &QCheckBox::stateChanged, [fieldTableName, fieldName, switchLabel, this](int state) {
  2092. if (state == Qt::Checked)
  2093. {
  2094. switchLabel->setText("开");
  2095. m_sqlOper->updateControlData(fieldTableName, fieldName, "on");
  2096. //updateDb(fieldTableName, fieldId, "on");
  2097. } else
  2098. {
  2099. switchLabel->setText("关");
  2100. m_sqlOper->updateControlData(fieldTableName, fieldName, "off");
  2101. //updateDb(fieldTableName, fieldId, "off");
  2102. }
  2103. });
  2104. switchLayout->addWidget(switchBox);
  2105. switchLayout->addWidget(switchLabel);
  2106. rightLayout->addWidget(switchContainer);
  2107. createdWidget = switchBox;
  2108. } else if (fieldType == "combined")
  2109. {
  2110. // 用逗号分隔
  2111. QStringList parts = fieldValue.split(QRegExp("[,,]"), Qt::SkipEmptyParts);
  2112. QString inputText = (parts.size() >= 1 ? parts.at(0).trimmed() : QString());
  2113. QString button1Text = (parts.size() >= 2 ? parts.at(1).trimmed() : QStringLiteral("获取"));
  2114. QString button2Text = (parts.size() >= 3 ? parts.at(2).trimmed() : QStringLiteral("设置"));
  2115. QLineEdit* comboInput = new QLineEdit(inputText);
  2116. comboInput->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
  2117. comboInput->setFixedHeight(28);
  2118. comboInput->setStyleSheet(R"(
  2119. QLineEdit {
  2120. background: #FFFFFF;
  2121. border: 1px solid #BABBDC;
  2122. border-radius: 5px;
  2123. padding: 2px 5px;
  2124. }
  2125. )");
  2126. m_fieldWidgets.append(comboInput);
  2127. QWidget* combinedWidget = new QWidget;
  2128. QHBoxLayout* combinedLayout = new QHBoxLayout(combinedWidget);
  2129. combinedLayout->setSpacing(5);
  2130. combinedLayout->setContentsMargins(0, 0, 0, 0);
  2131. QPushButton* btnGet = new QPushButton(button1Text, combinedWidget);
  2132. btnGet->setFixedSize(80, 28);
  2133. btnGet->setStyleSheet(R"(
  2134. QPushButton {
  2135. background: #FFFFFF;
  2136. border: 1px solid #BABBDC;
  2137. border-radius: 5px;
  2138. }
  2139. QPushButton:hover {
  2140. background-color: #F0F0F0;
  2141. }
  2142. )");
  2143. m_fieldWidgets.append(btnGet);
  2144. QPushButton* btnSet = new QPushButton(button2Text, combinedWidget);
  2145. btnSet->setFixedSize(80, 28);
  2146. btnSet->setStyleSheet(R"(
  2147. QPushButton {
  2148. background: #FFFFFF;
  2149. border: 1px solid #BABBDC;
  2150. border-radius: 5px;
  2151. }
  2152. QPushButton:hover {
  2153. background-color: #F0F0F0;
  2154. }
  2155. )");
  2156. m_fieldWidgets.append(btnSet);
  2157. combinedLayout->addWidget(comboInput);
  2158. combinedLayout->addWidget(btnGet);
  2159. combinedLayout->addWidget(btnSet);
  2160. combinedLayout->addStretch();
  2161. rightLayout->addWidget(combinedWidget);
  2162. } else if (fieldType == "Text")
  2163. {
  2164. QLabel* label = new QLabel();
  2165. label->setText(fieldValue);
  2166. label->setAlignment(Qt::AlignVCenter | Qt::AlignLeft);
  2167. label->setFixedHeight(24);
  2168. label->setMinimumWidth(120);
  2169. rightLayout->addWidget(label);
  2170. } else
  2171. {
  2172. qWarning() << "未知字段类型:" << fieldType;
  2173. }
  2174. if (createdWidget)
  2175. m_fieldWidgets.append(createdWidget);
  2176. fieldLayout->addWidget(rightWidget);
  2177. scrollLayout->addLayout(fieldLayout);
  2178. }
  2179. }
  2180. }
  2181. scrollLayout->addStretch();
  2182. scrollArea->setWidget(scrollWidget);
  2183. QVBoxLayout* mainLayout = new QVBoxLayout(m_thirdLevelFieldWnd);
  2184. mainLayout->setContentsMargins(0, 0, 0, 0);
  2185. mainLayout->addWidget(scrollArea);
  2186. m_thirdLevelFieldWnd->show();
  2187. // 显示按钮
  2188. displayThirdLevelButtons(buttonControls);
  2189. //loadButtonConfigForThirdLevel(data);
  2190. }
  2191. void DbTreeViewManager::updateParentCheckState(QStandardItem *item)
  2192. {
  2193. if (!item)
  2194. return;
  2195. QStandardItem *parentItem = item->parent();
  2196. if (!parentItem)
  2197. return;
  2198. bool allChecked = true;
  2199. bool noneChecked = true;
  2200. for (int i = 0; i < parentItem->rowCount(); ++i) {
  2201. QStandardItem *child = parentItem->child(i);
  2202. if (child->checkState() != Qt::Checked) {
  2203. allChecked = false;
  2204. }
  2205. if (child->checkState() == Qt::Checked) {
  2206. noneChecked = false;
  2207. }
  2208. }
  2209. if (allChecked) {
  2210. parentItem->setCheckState(Qt::Checked);
  2211. } else {
  2212. parentItem->setCheckState(Qt::Unchecked);
  2213. }
  2214. if (noneChecked) {
  2215. qDebug() << "noneChecked状态为:" << noneChecked;
  2216. }
  2217. // 递归更新上一级
  2218. updateParentCheckState(parentItem);
  2219. }
  2220. void DbTreeViewManager::onButtonBackClicked()
  2221. {
  2222. if (m_thirdLevelFieldWnd) {
  2223. // 当前在第三层目录,返回到上一层目录
  2224. qDebug() << "当前在第三层目录,准备返回上一层。";
  2225. for(int i=0;i<5;i++){
  2226. if(currentMenuId == menuArray[i].menuId){
  2227. menuArray[i].isthird = false;
  2228. }
  2229. }
  2230. // 关闭并销毁字段展示窗口
  2231. m_thirdLevelFieldWnd->close();
  2232. m_thirdLevelFieldWnd->deleteLater();
  2233. m_thirdLevelFieldWnd = nullptr;
  2234. // 显示目录树和分隔线
  2235. treeViewDown->show();
  2236. for (auto separator : firstLevelSeparators) {
  2237. if (separator)
  2238. separator->show();
  2239. }
  2240. // 获取当前导航路径
  2241. QStringList path;
  2242. QLayout* layout = navigationWidget->layout();
  2243. if (!layout) {
  2244. qWarning() << "导航栏没有布局,无法获取路径。";
  2245. return;
  2246. }
  2247. for (int i = 0; i < layout->count(); ++i) {
  2248. QLayoutItem* item = layout->itemAt(i);
  2249. if (item && item->widget()) {
  2250. QLabel* label = qobject_cast<QLabel*>(item->widget());
  2251. if (label) {
  2252. QString text = label->text().trimmed();
  2253. if (!text.isEmpty()) {
  2254. path << text;
  2255. }
  2256. }
  2257. }
  2258. }
  2259. if (path.isEmpty()) {
  2260. qWarning() << "导航路径为空,无法返回上一层。";
  2261. return;
  2262. }
  2263. // 移除最后一个目录名称以返回上一层
  2264. path.removeLast();
  2265. if (path.isEmpty()) {
  2266. qDebug() << "已经在根目录,无法返回。";
  2267. return;
  2268. }
  2269. // 根据新的路径查找对应的 QModelIndex
  2270. QModelIndex parentIndex = findItemByPath(path);
  2271. if (!parentIndex.isValid()) {
  2272. qWarning() << "无法找到路径对应的目录项:" << path;
  2273. return;
  2274. }
  2275. // 选中父目录项
  2276. treeViewDown->setCurrentIndex(parentIndex);
  2277. treeViewDown->scrollTo(parentIndex);
  2278. // 更新导航栏
  2279. updateNavigationBar(parentIndex);
  2280. // 检查父目录是否为第三层目录
  2281. QStandardItem* parentItem = model->itemFromIndex(parentIndex);
  2282. if (!parentItem) {
  2283. qWarning() << "父目录项无效。";
  2284. return;
  2285. }
  2286. QVariant dataVar = parentItem->data(Qt::UserRole + 2);
  2287. bool isThirdLevel = false;
  2288. int directoryId = -1;
  2289. QString multiTableName = "";
  2290. if (dataVar.canConvert<QJsonObject>()) {
  2291. QJsonObject dataObj = dataVar.toJsonObject();
  2292. if (dataObj.contains("IsThirdLevel") && dataObj["IsThirdLevel"].toBool()) {
  2293. isThirdLevel = true;
  2294. if (dataObj.contains("Id")) {
  2295. directoryId = dataObj["Id"].toInt();
  2296. }
  2297. if (dataObj.contains("TableName"))
  2298. {
  2299. multiTableName = dataObj["TableName"].toString();
  2300. }
  2301. }
  2302. }
  2303. if (isThirdLevel) {
  2304. if (directoryId == -1) {
  2305. qWarning() << "无效的 DirectoryId,无法加载第三层目录内容。";
  2306. return;
  2307. }
  2308. int userPrivilege = 0x1; // 当前用户的权限等级
  2309. QList<Table_Control_Data> tableControlDatas;
  2310. bool allChangeFlag = false;
  2311. m_sqlOper->GetThirdDirControlData(multiTableName, userPrivilege, tableControlDatas, allChangeFlag);
  2312. displayThirdLevelFields(tableControlDatas, allChangeFlag, false); // 传递参数表示 Home 界面
  2313. // 获取第三层目录的数据
  2314. //m_sqlOper->GetThirdDirControlData();
  2315. //QJsonObject data = fetchThirdLevelData(directoryId, userPrivilege);
  2316. // 显示字段展示窗口
  2317. //displayThirdLevelFields(data, false); // 传递参数表示非 Home 界面
  2318. } else {
  2319. // 父目录不是第三层目录,确保目录树和分隔线可见
  2320. treeViewDown->show();
  2321. for (auto separator : firstLevelSeparators) {
  2322. if (separator)
  2323. separator->show();
  2324. }
  2325. }
  2326. return;
  2327. }
  2328. // 如果不在第三层目录,则在目录树中返回上一层
  2329. qDebug() << "当前不在第三层目录,尝试在目录树中返回上一层。";
  2330. QModelIndex currentIndex = treeViewDown->currentIndex();
  2331. if (!currentIndex.isValid()) {
  2332. qWarning() << "当前没有选中的目录项,无法返回。";
  2333. return;
  2334. }
  2335. QStandardItem* currentItem = model->itemFromIndex(currentIndex);
  2336. if (!currentItem) {
  2337. qWarning() << "当前选中的目录项无效。";
  2338. return;
  2339. }
  2340. QStandardItem* parentItem = currentItem->parent();
  2341. if (!parentItem) {
  2342. qDebug() << "当前已经在根目录,无法返回。";
  2343. return;
  2344. }
  2345. QModelIndex parentIndex = model->indexFromItem(parentItem);
  2346. if (!parentIndex.isValid()) {
  2347. qWarning() << "父目录的 QModelIndex 无效。";
  2348. return;
  2349. }
  2350. // 选中父目录项
  2351. treeViewDown->setCurrentIndex(parentIndex);
  2352. treeViewDown->scrollTo(parentIndex);
  2353. // 更新导航栏
  2354. updateNavigationBar(parentIndex);
  2355. // 检查父目录是否为第三层目录
  2356. QVariant dataVar = parentItem->data(Qt::UserRole + 2);
  2357. bool isThirdLevel = false;
  2358. int directoryId = -1;
  2359. QString multiTableName = "";
  2360. if (dataVar.canConvert<QJsonObject>()) {
  2361. QJsonObject dataObj = dataVar.toJsonObject();
  2362. if (dataObj.contains("IsThirdLevel") && dataObj["IsThirdLevel"].toBool()) {
  2363. isThirdLevel = true;
  2364. if (dataObj.contains("Id")) {
  2365. directoryId = dataObj["Id"].toInt();
  2366. }
  2367. if (dataObj.contains("TableName"))
  2368. {
  2369. multiTableName = dataObj["TableName"].toString();
  2370. }
  2371. }
  2372. }
  2373. if (isThirdLevel) {
  2374. if (directoryId == -1) {
  2375. qWarning() << "无效的 DirectoryId,无法加载第三层目录内容。";
  2376. return;
  2377. }
  2378. int userPrivilege = 0x1; // 根据实际情况获取当前用户的权限等级
  2379. QList<Table_Control_Data> tableControlDatas;
  2380. bool allChangeFlag = false;
  2381. m_sqlOper->GetThirdDirControlData(multiTableName, userPrivilege, tableControlDatas, allChangeFlag);
  2382. displayThirdLevelFields(tableControlDatas, allChangeFlag, false); // 传递参数表示 Home 界面
  2383. //QJsonObject data = fetchThirdLevelData(directoryId, userPrivilege);
  2384. //displayThirdLevelFields(data, false); // 传递参数表示非 Home 界面
  2385. }
  2386. }
  2387. QList<QStandardItem*> DbTreeViewManager::collectAllThirdLevelItems()
  2388. {
  2389. QList<QStandardItem*> result;
  2390. // 从根节点开始递归搜集
  2391. QStandardItem *root = model->invisibleRootItem();
  2392. if (!root) return result;
  2393. std::function<void(QStandardItem*)> dfsCollectThirdLevel;
  2394. dfsCollectThirdLevel = [&](QStandardItem* parentItem)
  2395. {
  2396. if (!parentItem) return;
  2397. int rowCount = parentItem->rowCount();
  2398. for (int i = 0; i < rowCount; ++i) {
  2399. QStandardItem *child = parentItem->child(i);
  2400. if (!child) continue;
  2401. // 判断是否是第三层
  2402. QVariant dataVar = child->data(Qt::UserRole + 2);
  2403. if (dataVar.canConvert<QJsonObject>()) {
  2404. QJsonObject obj = dataVar.toJsonObject();
  2405. if (obj.contains("IsThirdLevel") && obj["IsThirdLevel"].toBool()) {
  2406. // 收集这个第三层节点
  2407. result.append(child);
  2408. }
  2409. }
  2410. // 即使不是第三层,也要继续往下找子节点
  2411. dfsCollectThirdLevel(child);
  2412. }
  2413. };
  2414. dfsCollectThirdLevel(root);
  2415. return result;
  2416. }
  2417. void DbTreeViewManager::onButtonDownClicked()
  2418. {
  2419. // 1) 收集所有 第三层 节点
  2420. QList<QStandardItem*> thirdLevelList = collectAllThirdLevelItems();
  2421. if (thirdLevelList.isEmpty()) {
  2422. qDebug() << "[onButtonDownClicked] 没有任何三级目录,无法遍历。";
  2423. return;
  2424. }
  2425. // 2) 找到 当前选中的节点 在 thirdLevelList 里的索引
  2426. QModelIndex curIndex = treeViewDown->currentIndex();
  2427. if (!curIndex.isValid()) {
  2428. qDebug() << "[onButtonDownClicked] 当前没有选中节点,默认跳到第一个三级目录。";
  2429. // 如果想默认跳到第一个
  2430. QStandardItem *firstItem = thirdLevelList.first();
  2431. QModelIndex idx = model->indexFromItem(firstItem);
  2432. treeViewDown->setCurrentIndex(idx);
  2433. // 模拟点击一下
  2434. onTreeViewClicked_updown(idx);
  2435. // 保存复选框状态
  2436. saveCheckedPaths();
  2437. // 保存展开路径
  2438. saveExpandedPaths();
  2439. return;
  2440. }
  2441. QStandardItem *curItem = model->itemFromIndex(curIndex);
  2442. if (!curItem) {
  2443. qWarning() << "[onButtonDownClicked] currentItem 无效。";
  2444. return;
  2445. }
  2446. // 若不是三级目录,就先找它最近的父级是不是三级目录,否则当做没找到。
  2447. int currentPos = -1;
  2448. {
  2449. // 在 thirdLevelList 里找到 curItem
  2450. currentPos = thirdLevelList.indexOf(curItem);
  2451. if (currentPos < 0) {
  2452. qDebug() << "[onButtonDownClicked] 当前节点不是三级目录,尝试找父级是不是三级目录…";
  2453. // 向上找父节点是否在 thirdLevelList 里
  2454. QStandardItem *temp = curItem->parent();
  2455. while (temp) {
  2456. int pos = thirdLevelList.indexOf(temp);
  2457. if (pos >= 0) {
  2458. currentPos = pos;
  2459. break;
  2460. }
  2461. temp = temp->parent();
  2462. }
  2463. if (currentPos < 0) {
  2464. qDebug() << "还是找不到,说明当前目录不属于任何三级目录,直接跳到第一个";
  2465. QStandardItem *firstItem = thirdLevelList.first();
  2466. QModelIndex idx = model->indexFromItem(firstItem);
  2467. treeViewDown->setCurrentIndex(idx);
  2468. onTreeViewClicked_updown(idx);
  2469. // 保存复选框状态
  2470. saveCheckedPaths();
  2471. // 保存展开路径
  2472. saveExpandedPaths();
  2473. return;
  2474. }
  2475. }
  2476. }
  2477. // 尝试下一个
  2478. int nextPos = currentPos + 1;
  2479. if (nextPos >= thirdLevelList.size()) {
  2480. qDebug() << "[onButtonDownClicked] 已经是最后一个三级目录了,无法再向下。";
  2481. return;
  2482. }
  2483. // 跳到下一个
  2484. QStandardItem *nextItem = thirdLevelList[nextPos];
  2485. QModelIndex nextIndex = model->indexFromItem(nextItem);
  2486. treeViewDown->setCurrentIndex(nextIndex);
  2487. // 相当于模拟点击
  2488. onTreeViewClicked_updown(nextIndex);
  2489. // 保存复选框状态
  2490. saveCheckedPaths();
  2491. // 保存展开路径
  2492. saveExpandedPaths();
  2493. }
  2494. void DbTreeViewManager::onButtonUpClicked()
  2495. {
  2496. // 1) 收集所有 第三层 节点
  2497. QList<QStandardItem*> thirdLevelList = collectAllThirdLevelItems();
  2498. if (thirdLevelList.isEmpty()) {
  2499. qDebug() << "[onButtonUpClicked] 没有任何三级目录,无法遍历。";
  2500. return;
  2501. }
  2502. // 2) 找到 当前选中的节点
  2503. QModelIndex curIndex = treeViewDown->currentIndex();
  2504. if (!curIndex.isValid()) {
  2505. qDebug() << "[onButtonUpClicked] 当前无选中节点,默认跳到最后一个三级目录。";
  2506. // 跳到最后一个
  2507. QStandardItem *lastItem = thirdLevelList.last();
  2508. QModelIndex idx = model->indexFromItem(lastItem);
  2509. treeViewDown->setCurrentIndex(idx);
  2510. // 模拟点击
  2511. onTreeViewClicked_updown(idx);
  2512. // 保存复选框状态
  2513. saveCheckedPaths();
  2514. // 保存展开路径
  2515. saveExpandedPaths();
  2516. return;
  2517. }
  2518. QStandardItem *curItem = model->itemFromIndex(curIndex);
  2519. if (!curItem) {
  2520. qWarning() << "[onButtonUpClicked] currentItem 无效。";
  2521. return;
  2522. }
  2523. // 如果当前节点不是三级目录, 向上查找
  2524. int currentPos = thirdLevelList.indexOf(curItem);
  2525. if (currentPos < 0) {
  2526. qDebug() << "[onButtonUpClicked] 当前节点不是三级目录,尝试找父级是不是三级目录。";
  2527. QStandardItem *temp = curItem->parent();
  2528. while (temp) {
  2529. int pos = thirdLevelList.indexOf(temp);
  2530. if (pos >= 0) {
  2531. currentPos = pos;
  2532. break;
  2533. }
  2534. temp = temp->parent();
  2535. }
  2536. if (currentPos < 0) {
  2537. qDebug() << "找不到任何三级目录,直接跳到最后一个。";
  2538. QStandardItem *lastItem = thirdLevelList.last();
  2539. QModelIndex idx = model->indexFromItem(lastItem);
  2540. treeViewDown->setCurrentIndex(idx);
  2541. onTreeViewClicked_updown(idx);
  2542. // 保存复选框状态
  2543. saveCheckedPaths();
  2544. // 保存展开路径
  2545. saveExpandedPaths();
  2546. return;
  2547. }
  2548. }
  2549. // 尝试上一个
  2550. int prevPos = currentPos - 1;
  2551. if (prevPos < 0) {
  2552. qDebug() << "[onButtonUpClicked] 已经是第一个三级目录了,无法再向上。";
  2553. return;
  2554. }
  2555. // 跳到上一个
  2556. QStandardItem *prevItem = thirdLevelList[prevPos];
  2557. QModelIndex prevIndex = model->indexFromItem(prevItem);
  2558. treeViewDown->setCurrentIndex(prevIndex);
  2559. // 模拟点击
  2560. onTreeViewClicked_updown(prevIndex);
  2561. // 保存复选框状态
  2562. saveCheckedPaths();
  2563. // 保存展开路径
  2564. saveExpandedPaths();
  2565. }
  2566. void DbTreeViewManager::onButtonRightClicked()
  2567. {
  2568. if (m_forwardStack.isEmpty()) {
  2569. qDebug() << "[Forward] 已经没有可以前进的页面";
  2570. buttonRight->setIcon(QIcon(":/images/home_right.png"));
  2571. return;
  2572. }
  2573. // 1. 弹出 forwardStack 的最后一个
  2574. PageState st = m_forwardStack.takeLast();
  2575. // 2. 把当前页面 push 到 backStack
  2576. PageState cur = getCurrentPageState();
  2577. m_backStack.append(cur);
  2578. if(m_backStack.size() >= 2){
  2579. buttonLeft->setIcon(QIcon(":/images/home_left.png"));
  2580. }
  2581. // 3. 加载 st
  2582. loadPageState(st, true);
  2583. }
  2584. void DbTreeViewManager::onButtonLeftClicked()
  2585. {
  2586. if (m_backStack.size() < 2) {
  2587. qDebug() << "[Back] 已经无法再后退";
  2588. buttonLeft->setIcon(QIcon(":/images/home_left_hide.png"));
  2589. return;
  2590. }
  2591. // 1. 先把当前页面(相当于 m_backStack 最后一个)弹出,放到 forwardStack
  2592. PageState cur = m_backStack.takeLast();
  2593. if(m_backStack.size() >= 2){
  2594. buttonLeft->setIcon(QIcon(":/images/home_left.png"));
  2595. }else{
  2596. buttonLeft->setIcon(QIcon(":/images/home_left_hide.png"));
  2597. }
  2598. m_forwardStack.append(cur);
  2599. buttonRight->setIcon(QIcon(":/images/home_right_show.png"));
  2600. // 2. 拿到新的 m_backStack 末尾(即上一次看的页面)
  2601. const PageState &prev = m_backStack.last();
  2602. // 3. 加载 prev
  2603. loadPageState(prev, true );
  2604. }
  2605. // 构建节点的完整路径
  2606. QStringList DbTreeViewManager::buildItemPath(QStandardItem *item)
  2607. {
  2608. QStringList path;
  2609. QStandardItem *currentItem = item;
  2610. while (currentItem) {
  2611. path.prepend(currentItem->text());
  2612. currentItem = currentItem->parent();
  2613. }
  2614. return path;
  2615. }
  2616. // 根据路径查找对应的节点
  2617. QModelIndex DbTreeViewManager::findItemByPath(const QStringList &path)
  2618. {
  2619. if (path.isEmpty()) return QModelIndex();
  2620. QStandardItem *currentItem = model->invisibleRootItem();
  2621. QModelIndex currentIndex;
  2622. for (const QString &part : path) {
  2623. bool found = false;
  2624. for (int i = 0; i < currentItem->rowCount(); ++i) {
  2625. QStandardItem *child = currentItem->child(i);
  2626. if (child == nullptr)
  2627. {
  2628. qDebug() << "child is nullptr";
  2629. }
  2630. if (child->text() == part) {
  2631. currentIndex = model->indexFromItem(child);
  2632. currentItem = child;
  2633. found = true;
  2634. qDebug() << "找到路径部分:" << part;
  2635. break;
  2636. }
  2637. }
  2638. if (!found) {
  2639. qWarning() << "路径部分未找到:" << part;
  2640. return QModelIndex();
  2641. }
  2642. }
  2643. return currentIndex;
  2644. }
  2645. void DbTreeViewManager::setCheckedPaths(const QStringList &checkedPathsList)
  2646. {
  2647. m_blockItemChanged = true;
  2648. for (const QString &pathStr : checkedPathsList) {
  2649. QStringList path = pathStr.split("/");
  2650. QModelIndex idx = findItemByPath(path);
  2651. if (idx.isValid()) {
  2652. QStandardItem *item = model->itemFromIndex(idx);
  2653. if (item) {
  2654. item->setCheckState(Qt::Checked);
  2655. qDebug() << " Setting item:" << item->text() << "to Checked";
  2656. }
  2657. } else {
  2658. qDebug() << "Path not found:" << pathStr;
  2659. }
  2660. }
  2661. m_blockItemChanged = false;
  2662. }
  2663. // 递归收集被选中的路径
  2664. QStringList DbTreeViewManager::collectCheckedPathsRecursive(QStandardItem *item, QStringList path)
  2665. {
  2666. QStringList checkedList;
  2667. if (!item) {
  2668. item = model->invisibleRootItem();
  2669. }
  2670. for (int i = 0; i < item->rowCount(); ++i) {
  2671. QStandardItem *child = item->child(i);
  2672. if (child) {
  2673. QStringList currentPath = path;
  2674. currentPath << child->text();
  2675. if (child->checkState() == Qt::Checked) {
  2676. checkedList << currentPath.join("/");
  2677. }
  2678. // 递归收集子项
  2679. checkedList << collectCheckedPathsRecursive(child, currentPath);
  2680. }
  2681. }
  2682. return checkedList;
  2683. }
  2684. // 收集所有被选中的复选框路径
  2685. QStringList DbTreeViewManager::collectCheckedPaths()
  2686. {
  2687. return collectCheckedPathsRecursive(model->invisibleRootItem(), QStringList());
  2688. }
  2689. // 更新父项的复选框状态
  2690. void DbTreeViewManager::updateParentItems(QStandardItem *parentItem)
  2691. {
  2692. if (!parentItem)
  2693. return;
  2694. int checkedCount = 0;
  2695. int totalCount = parentItem->rowCount();
  2696. qDebug() << "Updating child items of:" << parentItem->text();
  2697. for (int i = 0; i < totalCount; ++i)
  2698. {
  2699. QStandardItem *child = parentItem->child(i);
  2700. if (child && child->checkState() == Qt::Checked)
  2701. {
  2702. checkedCount++;
  2703. }
  2704. }
  2705. if (checkedCount == totalCount)
  2706. {
  2707. parentItem->setCheckState(Qt::Checked);
  2708. }
  2709. else
  2710. {
  2711. parentItem->setCheckState(Qt::Unchecked);
  2712. }
  2713. // 递归更新上层父项
  2714. updateParentItems(parentItem->parent());
  2715. }
  2716. // 加载并显示三级目录对应按钮配置信息
  2717. //void DbTreeViewManager::loadButtonConfigForThirdLevel(const QJsonObject &thirdLevelObj)
  2718. //{
  2719. // if (!m_originalWnd) {
  2720. // qWarning() << "OriginalWnd 指针为空,无法加载按钮配置";
  2721. // return;
  2722. // }
  2723. //
  2724. // if (!thirdLevelObj.contains("buttons")) {
  2725. // qWarning() << "三级目录配置中不包含 'buttons' 字段";
  2726. // return;
  2727. // }
  2728. //
  2729. // QJsonArray buttonsArray = thirdLevelObj.value("buttons").toArray();
  2730. //
  2731. // // 获取 widget_left
  2732. // QWidget* widgetLeft = m_originalWnd->getWidgetLeft();
  2733. // if (!widgetLeft) {
  2734. // qWarning() << "无法访问 widget_left";
  2735. // return;
  2736. // }
  2737. //
  2738. // // 清空 widget_left 中由 loadButtonConfigForThirdLevel 创建的按钮
  2739. // QList<QPushButton*> existingButtons = widgetLeft->findChildren<QPushButton*>();
  2740. // for (QPushButton* button : existingButtons) {
  2741. // if (button->objectName().startsWith("thirdLevelBtn_")) { // 仅删除特定按钮
  2742. // button->deleteLater();
  2743. // }
  2744. // }
  2745. //
  2746. // // 使用绝对定位创建按钮
  2747. // for (int i = 0; i < buttonsArray.size() && i < 12; ++i) {
  2748. // QJsonObject buttonObj = buttonsArray[i].toObject();
  2749. // QString buttonId = buttonObj.value("id").toString();
  2750. // QString buttonIcon = buttonObj.value("icon").toString();
  2751. // QString buttonText = buttonObj.value("text").toString();
  2752. // bool isEnabled = buttonObj.value("enabled").toBool();
  2753. //
  2754. // // 创建按钮
  2755. // QPushButton *button = new QPushButton(widgetLeft);
  2756. // button->setObjectName("thirdLevelBtn_" + buttonId); // 设置带前缀的对象名称
  2757. // connect(button, &QPushButton::clicked, this, []() {
  2758. // JMessageTip::Message_question("Q111");
  2759. // });
  2760. //
  2761. // // 设置按钮的样式,调整图标和文本的位置
  2762. // button->setStyleSheet(R"(
  2763. // QPushButton {
  2764. // position: absolute;
  2765. // border-radius: 6px;
  2766. // opacity: 1;
  2767. // background: #CBD0FF;
  2768. // border: none;
  2769. // }
  2770. // QPushButton:hover {
  2771. // background-color: #A9B4FF; /* 鼠标悬停效果 */
  2772. // }
  2773. // )");
  2774. //
  2775. // // 设置按钮的位置和大小
  2776. // int x = 16;
  2777. // int y = 245 + i * (48 + 13); // 第一个按钮 y=245,后续每个按钮间隔13px
  2778. // button->setGeometry(x, y, 158, 48);
  2779. //
  2780. // // 设置按钮的可见性,根据 "enabled" 字段显示或隐藏按钮
  2781. // button->setVisible(isEnabled);
  2782. //
  2783. // // 创建图标标签
  2784. // QLabel *iconLabel = new QLabel(button);
  2785. // iconLabel->setPixmap(QIcon(buttonIcon).pixmap(16, 16));
  2786. // iconLabel->setGeometry(10, 16, 16, 16); // 图标距离左边10px,顶部16px
  2787. // iconLabel->setFixedSize(16, 16);
  2788. // iconLabel->setStyleSheet("background-color: transparent;");
  2789. // iconLabel->setVisible(isEnabled); // 根据按钮的可见性设置图标的可见性
  2790. //
  2791. // // 创建文本标签
  2792. // QLabel *textLabel = new QLabel(buttonText, button);
  2793. // textLabel->setGeometry(34, 0, 90, 48); // 文本距离左边34px
  2794. // textLabel->setWordWrap(true); // 允许换行
  2795. // textLabel->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
  2796. // textLabel->setStyleSheet(R"(
  2797. // QLabel {
  2798. // background: transparent;
  2799. // font-family: "思源黑体";
  2800. // font-size: 14px;
  2801. // font-weight: 500;
  2802. // color: #4E51CE;
  2803. // }
  2804. // )");
  2805. //
  2806. // textLabel->setVisible(isEnabled); // 根据按钮的可见性设置文本的可见性
  2807. //
  2808. // // 创建 F1-F12 标签
  2809. // QString fLabelText = QString("F%1").arg(i + 1); // F1, F2, ..., F12
  2810. // QLabel *fLabel = new QLabel(fLabelText, button);
  2811. // fLabel->setFixedSize(21, 16); // 设置大小为21x16
  2812. // fLabel->setAlignment(Qt::AlignCenter);
  2813. // fLabel->setStyleSheet(R"(
  2814. // QLabel {
  2815. // background-color: transparent;
  2816. // color: #2A7ED8;
  2817. // font-size: 12px;
  2818. // font-weight: bold;
  2819. // }
  2820. // )");
  2821. //
  2822. // // 设置标签的位置
  2823. // int fX = 134;
  2824. // int fY = 2;
  2825. // fLabel->setGeometry(fX, fY, 14, 16);
  2826. // fLabel->setVisible(isEnabled); // 根据按钮的可见性设置标签的可见性
  2827. //
  2828. // // 将按钮、图标、文本和标签添加到控件列表
  2829. // m_fieldWidgets.append(button);
  2830. // m_fieldWidgets.append(iconLabel);
  2831. // m_fieldWidgets.append(textLabel);
  2832. // m_fieldWidgets.append(fLabel);
  2833. // }
  2834. //}
  2835. void DbTreeViewManager::displayThirdLevelButtons(const QList<CONFIG_BASE_STRUCT>& buttons)
  2836. {
  2837. if (!m_originalWnd)
  2838. {
  2839. qWarning() << "OriginalWnd 指针为空,无法加载按钮配置";
  2840. return;
  2841. }
  2842. // 获取 widget_left
  2843. QWidget* widgetLeft = m_originalWnd->getWidgetLeft();
  2844. if (!widgetLeft)
  2845. {
  2846. qWarning() << "无法访问 widget_left";
  2847. return;
  2848. }
  2849. // 清空 widget_left 中由 loadButtonConfigForThirdLevel 创建的按钮
  2850. QList<QPushButton*> existingButtons = widgetLeft->findChildren<QPushButton*>();
  2851. for (QPushButton* button : existingButtons)
  2852. {
  2853. QString objName = button->objectName();
  2854. if (button->objectName().startsWith("thirdLevelBtn_F"))
  2855. { // 仅删除特定按钮
  2856. button->deleteLater();
  2857. }
  2858. }
  2859. // 使用绝对定位创建按钮
  2860. for (int i = 0; i < buttons.size() && i < 12; ++i)
  2861. {
  2862. int buttonId = buttons[i].iGroupId;
  2863. QString buttonIcon = QString::fromStdString(buttons[i].sIcon);
  2864. QString buttonText = QString::fromStdString(buttons[i].strName);
  2865. QString strCallFun = QString::fromStdString(buttons[i].strCallFun);
  2866. bool isEnabled = buttons[i].bEnable;
  2867. //for (int i = 0; i < buttonsArray.size() && i < 12; ++i) {
  2868. //QJsonObject buttonObj = buttonsArray[i].toObject();
  2869. //QString buttonId = buttonObj.value("id").toString();
  2870. //QString strNumID = buttonId; // 不改变原来的意思
  2871. //strNumID.remove('F');
  2872. //QString strDirectoryId = buttonObj.value("DirectoryId").toString();
  2873. //QString buttonIcon = buttonObj.value("icon").toString();
  2874. //QString buttonText = buttonObj.value("text").toString();
  2875. //QString strCallFun = buttonObj.value("CallFun").toString();
  2876. //bool isEnabled = buttonObj.value("enabled").toBool();
  2877. // 创建按钮
  2878. QPushButton *button = new QPushButton(widgetLeft);
  2879. QString ObjectName = "thirdLevelBtn_F" + QString::number(buttonId);
  2880. button->setObjectName(ObjectName); // 设置带前缀的对象名称
  2881. ObjectName = button->objectName();
  2882. connect(button, &QPushButton::clicked, this, [&, strCallFun, buttonId]()
  2883. {
  2884. ns_module::ST_BUTTON_FUN callFun = {};
  2885. //callFun.DirectoryId = strDirectoryId.toInt();
  2886. callFun.strCallFun = strCallFun.toStdString();
  2887. callFun.nId = buttonId;
  2888. emit RunFunSignals(callFun);
  2889. });
  2890. // 设置按钮的样式,调整图标和文本的位置
  2891. button->setStyleSheet(R"(
  2892. QPushButton {
  2893. position: absolute;
  2894. border-radius: 6px;
  2895. opacity: 1;
  2896. background: #CBD0FF;
  2897. border: none;
  2898. }
  2899. QPushButton:hover {
  2900. background-color: #A9B4FF; /* 鼠标悬停效果 */
  2901. }
  2902. )");
  2903. if (buttonId > 12)
  2904. {
  2905. qWarning() << "buttonId 超过最大ID";
  2906. continue;
  2907. }
  2908. // 设置按钮的位置和大小
  2909. int x = 16;
  2910. int y = 245 + (buttonId - 1) * (48 + 13); // 第一个按钮 y=245,后续每个按钮间隔13px
  2911. button->setGeometry(x, y, 158, 48);
  2912. // 设置按钮的可见性,根据 "enabled" 字段显示或隐藏按钮
  2913. button->setVisible(isEnabled);
  2914. // 创建图标标签
  2915. QLabel* iconLabel = new QLabel(button);
  2916. iconLabel->setPixmap(QIcon(buttonIcon).pixmap(16, 16));
  2917. iconLabel->setGeometry(10, 16, 16, 16); // 图标距离左边10px,顶部16px
  2918. iconLabel->setFixedSize(16, 16);
  2919. iconLabel->setStyleSheet("background-color: transparent;");
  2920. iconLabel->setVisible(isEnabled); // 根据按钮的可见性设置图标的可见性
  2921. // 创建文本标签
  2922. QLabel* textLabel = new QLabel(buttonText, button);
  2923. textLabel->setGeometry(34, 0, 90, 48); // 文本距离左边34px
  2924. textLabel->setWordWrap(true); // 允许换行
  2925. textLabel->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
  2926. textLabel->setStyleSheet(R"(
  2927. QLabel {
  2928. background: transparent;
  2929. font-family: "思源黑体";
  2930. font-size: 14px;
  2931. font-weight: 500;
  2932. color: #4E51CE;
  2933. }
  2934. )");
  2935. textLabel->setVisible(isEnabled); // 根据按钮的可见性设置文本的可见性
  2936. // 创建 F1-F12 标签
  2937. QString fLabelText = QString("F%1").arg(buttonId); // F1, F2, ..., F12
  2938. QLabel* fLabel = new QLabel(fLabelText, button);
  2939. fLabel->setFixedSize(21, 16); // 设置大小为21x16
  2940. fLabel->setAlignment(Qt::AlignCenter);
  2941. fLabel->setStyleSheet(R"(
  2942. QLabel {
  2943. background-color: transparent;
  2944. color: #2A7ED8;
  2945. font-size: 12px;
  2946. font-weight: bold;
  2947. }
  2948. )");
  2949. // 设置标签的位置
  2950. int fX = 134;
  2951. int fY = 2;
  2952. fLabel->setGeometry(fX, fY, 14, 16);
  2953. fLabel->setVisible(isEnabled); // 根据按钮的可见性设置标签的可见性
  2954. // 将按钮、图标、文本和标签添加到控件列表
  2955. m_fieldWidgets.append(button);
  2956. m_fieldWidgets.append(iconLabel);
  2957. m_fieldWidgets.append(textLabel);
  2958. m_fieldWidgets.append(fLabel);
  2959. }
  2960. }
  2961. // 清理并隐藏三级目录菜单内容
  2962. void DbTreeViewManager::clearThirdLevelMenu()
  2963. {
  2964. // 遍历所有子控件,找到标题为 "字段展示" 的窗口并关闭
  2965. foreach (QObject *child, widget2->children()) {
  2966. QWidget *childWidget = qobject_cast<QWidget*>(child);
  2967. if (childWidget && childWidget->windowTitle() == "字段展示") {
  2968. qDebug() << "关闭现有的字段展示窗口";
  2969. childWidget->close();
  2970. }
  2971. }
  2972. // 显示主目录树和分隔线
  2973. treeViewDown->show();
  2974. for (auto separator : firstLevelSeparators) {
  2975. separator->show();
  2976. }
  2977. }
  2978. // 保存选中路径
  2979. void DbTreeViewManager::saveCheckedPaths()
  2980. {
  2981. if (m_currentConfigName == "") {
  2982. qWarning() << "当前配置ID无效,无法保存复选框状态。";
  2983. return;
  2984. }
  2985. QSettings settings("RunCloudTech", "David");
  2986. //QString configKey = QString::number(m_currentConfigId);
  2987. settings.beginGroup("TreeViewCheckedState");
  2988. // 保存选中路径
  2989. QString keyChecked = QString("checkedPaths/%1").arg(m_currentConfigName);
  2990. QStringList checkedList = collectCheckedPaths();
  2991. settings.setValue(keyChecked, checkedList);
  2992. settings.endGroup();
  2993. qDebug() << "保存复选框状态路径:" << checkedList;
  2994. }
  2995. // 加载选中路径
  2996. void DbTreeViewManager::loadCheckedPaths()
  2997. {
  2998. if (m_currentConfigName == "") {
  2999. qWarning() << "当前配置ID无效,无法加载复选框状态。";
  3000. return;
  3001. }
  3002. QSettings settings("RunCloudTech", "David");
  3003. //QString configKey = QString::number(m_currentConfigId);
  3004. settings.beginGroup("TreeViewCheckedState");
  3005. // 读取选中路径
  3006. QString keyChecked = QString("checkedPaths/%1").arg(m_currentConfigName);
  3007. QStringList loadedChecked = settings.value(keyChecked).toStringList();
  3008. settings.endGroup();
  3009. qDebug() << "加载复选框状态路径:" << loadedChecked;
  3010. m_blockItemChanged = true;
  3011. setCheckedPaths(loadedChecked);
  3012. m_blockItemChanged = false;
  3013. // 更新导航栏
  3014. if (!loadedChecked.isEmpty()) {
  3015. QString lastPathStr = loadedChecked.last();
  3016. QStringList lastPath = lastPathStr.split("/");
  3017. QModelIndex lastIdx = findItemByPath(lastPath);
  3018. if (lastIdx.isValid()) {
  3019. treeViewDown->setCurrentIndex(lastIdx);
  3020. updateNavigationBar(lastIdx);
  3021. }
  3022. } else {
  3023. // 如果没有加载到任何路径,自动选择第一个目录
  3024. QStandardItem *rootItem = model->invisibleRootItem();
  3025. if (rootItem->rowCount() > 0) {
  3026. QModelIndex firstIndex = model->index(0, 0, QModelIndex());
  3027. if (firstIndex.isValid()) {
  3028. treeViewDown->setCurrentIndex(firstIndex);
  3029. treeViewDown->expand(firstIndex); // 展开第一个目录
  3030. QStandardItem *firstItem = model->itemFromIndex(firstIndex);
  3031. QVariant data = firstItem->data(Qt::UserRole + 2);
  3032. if (data.canConvert<QJsonObject>()) {
  3033. QJsonObject thirdLevelObj = data.toJsonObject();
  3034. if (thirdLevelObj.contains("IsThirdLevel") && thirdLevelObj["IsThirdLevel"].toBool()) {
  3035. QString multiTableName = "";
  3036. if (thirdLevelObj.contains("TableName")) {
  3037. multiTableName = thirdLevelObj["TableName"].toString();
  3038. }
  3039. QList<CONFIG_BASE_STRUCT> buttons;
  3040. int userGrade = 0x1;
  3041. m_sqlOper->GetThirdDirButtons(multiTableName, userGrade, buttons);
  3042. displayThirdLevelButtons(buttons);
  3043. //loadButtonConfigForThirdLevel(thirdLevelObj);
  3044. }
  3045. }
  3046. }
  3047. }
  3048. }
  3049. }
  3050. // 保存展开路径
  3051. void DbTreeViewManager::saveExpandedPaths()
  3052. {
  3053. if (m_currentConfigName == "") {
  3054. qWarning() << "当前配置ID无效,无法保存展开路径。";
  3055. return;
  3056. }
  3057. QSettings settings("RunCloudTech", "David");
  3058. //QString configKey = QString::number(m_currentConfigId);
  3059. settings.beginGroup("TreeViewExpandedState");
  3060. // 保存展开路径
  3061. QString keyExpanded = QString("expandedPaths/%1").arg(m_currentConfigName);
  3062. QStringList expandedList = QStringList(expandedPaths.begin(), expandedPaths.end());
  3063. settings.setValue(keyExpanded, expandedList);
  3064. settings.endGroup();
  3065. qDebug() << "保存展开路径:" << expandedList;
  3066. }
  3067. // 加载展开路径
  3068. void DbTreeViewManager::loadExpandedPaths()
  3069. {
  3070. if (m_currentConfigName == "") {
  3071. qWarning() << "当前配置ID无效,无法加载展开路径。";
  3072. return;
  3073. }
  3074. QSettings settings("RunCloudTech", "David");
  3075. //QString configKey = QString::number(m_currentConfigId);
  3076. settings.beginGroup("TreeViewExpandedState");
  3077. // 读取展开路径
  3078. QString keyExpanded = QString("expandedPaths/%1").arg(m_currentConfigName);
  3079. QStringList loadedExpanded = settings.value(keyExpanded).toStringList();
  3080. settings.endGroup();
  3081. qDebug() << "加载展开路径:" << loadedExpanded;
  3082. m_blockItemChanged = true;
  3083. // 恢复展开路径
  3084. for (const QString &p : loadedExpanded) {
  3085. QStringList path = p.split("/");
  3086. QModelIndex idx = findItemByPath(path);
  3087. if (idx.isValid()) {
  3088. treeViewDown->expand(idx);
  3089. expandedPaths.insert(p);
  3090. qDebug() << "成功恢复展开路径:" << p;
  3091. } else {
  3092. qDebug() << "未找到展开路径部分: " << p;
  3093. }
  3094. }
  3095. m_blockItemChanged = false;
  3096. }
  3097. // 清空历史
  3098. void DbTreeViewManager::clearHistory()
  3099. {
  3100. m_backStack.clear();
  3101. m_forwardStack.clear();
  3102. buttonLeft->setIcon(QIcon(":/images/home_left_hide.png"));
  3103. buttonRight->setIcon(QIcon(":/images/home_right.png"));
  3104. }
  3105. // 获取当前页面的 PageState
  3106. DbTreeViewManager::PageState DbTreeViewManager::getCurrentPageState()
  3107. {
  3108. PageState st;
  3109. st.isThirdLevel = false;
  3110. st.directoryId = -1;
  3111. // 判断是否在第三层窗口中
  3112. if (m_thirdLevelFieldWnd && m_thirdLevelFieldWnd->isVisible()) {
  3113. // 如果此时是在第三层界面,则用当前导航栏 or 目录树的 “选中项” 来获取路径
  3114. QModelIndex curIndex = treeViewDown->currentIndex();
  3115. if (!curIndex.isValid()) {
  3116. st.path << "未知目录";
  3117. } else {
  3118. QStandardItem* item = model->itemFromIndex(curIndex);
  3119. st.path = buildItemPath(item);
  3120. }
  3121. // 如果能拿到 DirectoryId,则置为第三层
  3122. QVariant dataVar = model->itemFromIndex(treeViewDown->currentIndex())->data(Qt::UserRole + 2);
  3123. if (dataVar.canConvert<QJsonObject>()) {
  3124. QJsonObject obj = dataVar.toJsonObject();
  3125. if (obj.contains("IsThirdLevel") && obj["IsThirdLevel"].toBool()) {
  3126. st.isThirdLevel = true;
  3127. st.directoryId = obj.value("Id").toInt(-1);
  3128. st.multiTableName = obj.value("TableName").toString();
  3129. }
  3130. }
  3131. }
  3132. else {
  3133. // 当前是目录树界面,就看 treeView 选中项
  3134. QModelIndex curIndex = treeViewDown->currentIndex();
  3135. if (curIndex.isValid()) {
  3136. QStandardItem* item = model->itemFromIndex(curIndex);
  3137. st.path = buildItemPath(item);
  3138. // 检查是否第三层
  3139. QVariant dataVar = item->data(Qt::UserRole + 2);
  3140. if (dataVar.canConvert<QJsonObject>()) {
  3141. QJsonObject obj = dataVar.toJsonObject();
  3142. if (obj.contains("IsThirdLevel") && obj["IsThirdLevel"].toBool()) {
  3143. st.isThirdLevel = true;
  3144. st.directoryId = obj.value("Id").toInt(-1);
  3145. st.multiTableName = obj.value("TableName").toString();
  3146. }
  3147. }
  3148. }
  3149. }
  3150. return st;
  3151. }
  3152. //加载指定的 PageState:相当于“回到该路径”或“跳转到这个第三层”
  3153. void DbTreeViewManager::loadPageState(const PageState &st, bool isByHistoryNav)
  3154. {
  3155. // 先根据 st.path 找到对应的节点
  3156. QModelIndex idx = findItemByPath(st.path);
  3157. if (!idx.isValid()) {
  3158. qWarning() << "[loadPageState] 未找到路径" << st.path;
  3159. return;
  3160. }
  3161. // 选中该节点
  3162. treeViewDown->setCurrentIndex(idx);
  3163. treeViewDown->scrollTo(idx);
  3164. // 更新导航栏
  3165. updateNavigationBar(idx);
  3166. // 如果是第三层,就加载对应的数据窗口
  3167. if (st.isThirdLevel && st.directoryId != -1) {
  3168. // 如果当前已在一个第三层窗口,先关闭它
  3169. if (m_thirdLevelFieldWnd) {
  3170. m_thirdLevelFieldWnd->close();
  3171. m_thirdLevelFieldWnd->deleteLater();
  3172. m_thirdLevelFieldWnd = nullptr;
  3173. }
  3174. int userPrivilege = 0x1;
  3175. QList<Table_Control_Data> tableControlDatas;
  3176. bool allChangeFlag = false;
  3177. m_sqlOper->GetThirdDirControlData(st.multiTableName, userPrivilege, tableControlDatas, allChangeFlag);
  3178. displayThirdLevelFields(tableControlDatas, allChangeFlag, false); // 传递参数表示 Home 界面
  3179. //QJsonObject data = fetchThirdLevelData(st.directoryId, userPrivilege);
  3180. //displayThirdLevelFields(data, (m_currentConfigName == "Home"));
  3181. } else {
  3182. // 否则,显示目录树
  3183. if (m_thirdLevelFieldWnd) {
  3184. m_thirdLevelFieldWnd->close();
  3185. m_thirdLevelFieldWnd->deleteLater();
  3186. m_thirdLevelFieldWnd = nullptr;
  3187. }
  3188. treeViewDown->show();
  3189. for (auto sep : firstLevelSeparators) {
  3190. if (sep) sep->show();
  3191. }
  3192. }
  3193. if (!isByHistoryNav) {
  3194. m_forwardStack.clear();
  3195. buttonRight->setIcon(QIcon(":/images/home_right.png"));
  3196. // 把新状态压入后退栈
  3197. m_backStack.append(st);
  3198. if(m_backStack.size() >= 2){
  3199. buttonLeft->setIcon(QIcon(":/images/home_left.png"));
  3200. }
  3201. }
  3202. }
  3203. void DbTreeViewManager::loginput(const QString& fieldTableName, const QString& fieldName,const QString& modifies){
  3204. // 获取当前时间并格式化
  3205. QDateTime currentDateTime = QDateTime::currentDateTime();
  3206. QString timestamp = currentDateTime.toString("yyyy-MM-dd hh:mm:ss");
  3207. QString logMessage = QString("%1 :fieldTableName is %2 and fieldName is %3 ,currentvalue is modified %4" ).arg(timestamp).arg(fieldTableName).arg(fieldName).arg(modifies);
  3208. QString pathoflog = "C:\\Users\\Administrator\\Desktop\\qt\\merge\\gujiangdong\\die-bonder-ui0218\\die-bonder-ui\\log/log.txt";
  3209. writeLogToFile(logMessage,pathoflog);
  3210. }
  3211. void DbTreeViewManager::writeLogToFile(const QString& logMessage, const QString& filePath) {
  3212. QFile logFile(filePath);
  3213. if (!logFile.open(QIODevice::Append | QIODevice::Text)) {
  3214. qWarning() << "Cannot open file for writing:" << filePath;
  3215. return;
  3216. }
  3217. QTextStream out(&logFile);
  3218. out << logMessage << "\n";
  3219. logFile.close();
  3220. }