DbTreeViewManager.cpp 164 KB

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