DbTreeViewManager.cpp 176 KB

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