DbTreeViewManager.cpp 176 KB

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