DbTreeViewManager.cpp 175 KB

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