DbTreeViewManager.cpp 164 KB

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