DbTreeViewManager.cpp 164 KB

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