DbTreeViewManager.cpp 176 KB

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