DbTreeViewManager.cpp 175 KB

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