DbTreeViewManager.cpp 178 KB

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