DbTreeViewManager.cpp 166 KB

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