DbTreeViewManager.cpp 165 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215
  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. firstLevelSeparators.clear();
  190. expandedPaths.clear();
  191. // 清理并隐藏三级目录窗口
  192. clearThirdLevelMenu();
  193. // **Home 界面逻辑**
  194. if (name == "Home")
  195. {
  196. // 隐藏目录树和分隔线
  197. m_pTreeViewDown->hide();
  198. for (auto separator : firstLevelSeparators)
  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<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 : firstLevelSeparators) {
  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<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. firstLevelSeparators.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 dirTableName = "";
  883. QString mutliTableName = "";
  884. QVariant dataVar = item->data(Qt::UserRole + 2);
  885. if (dataVar.canConvert<QJsonObject>()) {
  886. QJsonObject obj = dataVar.toJsonObject();
  887. qDebug() << " AAkey :" << obj;
  888. if (obj.contains("IsThirdLevel") && obj["IsThirdLevel"].toBool()) {
  889. isThird = true;
  890. if (obj.contains("Id")) {
  891. dirId = obj["Id"].toInt();
  892. }
  893. if (obj.contains("TableName"))
  894. {
  895. mutliTableName = obj["TableName"].toString();
  896. }
  897. if (obj.contains("ModuleType"))
  898. {
  899. strModuleType = obj["ModuleType"].toString();
  900. m_strModuleTypeAAA = strModuleType;
  901. }
  902. }
  903. }
  904. updateNavigationBar(index);
  905. if (isThird) {
  906. //!!!修改权限
  907. int userPrivilege = 0x1; // 示例
  908. if (dirId < 0) {
  909. qWarning() << "无效directoryId";
  910. return;
  911. }
  912. if (mutliTableName == "")
  913. {
  914. qWarning() << "无效directoryId";
  915. return;
  916. }
  917. QList<Table_Control_Data> tableControlDatas;
  918. bool allChangeFlag = false;
  919. m_sqlOper->GetThirdDirControlData(mutliTableName, userPrivilege, tableControlDatas, allChangeFlag);
  920. displayThirdLevelFields(tableControlDatas, allChangeFlag, false); // 传递参数表示 Home 界面
  921. //QJsonObject data = m_sqlOper.fetchThirdLevelData(mutliTableName, dirId, userPrivilege);
  922. //QJsonObject data = fetchThirdLevelData(dirId, userPrivilege);
  923. //displayThirdLevelFields(data, false);
  924. for(int i=0;i<5;i++){
  925. if(currentMenuId == menuArray[i].menuId){
  926. menuArray[i].isthird = true;
  927. menuArray[i].index = dirId;
  928. }
  929. }
  930. // 将“新页面”压入后退栈, 并清空前进栈
  931. PageState st;
  932. st.path = buildItemPath(item);
  933. st.isThirdLevel = true;
  934. st.directoryId = dirId;
  935. st.multiTableName = mutliTableName;
  936. m_backStack.append(st);
  937. if(m_backStack.size() >= 2){
  938. buttonLeft->setIcon(QIcon(":/images/home_left.png"));
  939. }
  940. m_forwardStack.clear();
  941. buttonRight->setIcon(QIcon(":/images/home_right.png"));
  942. } else {
  943. // 不是第三层,只是选中目录
  944. // 清理第三层窗口
  945. if (m_thirdLevelFieldWnd) {
  946. m_thirdLevelFieldWnd->close();
  947. m_thirdLevelFieldWnd->deleteLater();
  948. m_thirdLevelFieldWnd = nullptr;
  949. }
  950. // 同样更新历史
  951. PageState st;
  952. st.path = buildItemPath(item);
  953. st.isThirdLevel = false;
  954. st.directoryId = -1;
  955. st.multiTableName = mutliTableName;
  956. m_backStack.append(st);
  957. if(m_backStack.size() >= 2){
  958. buttonLeft->setIcon(QIcon(":/images/home_left.png"));
  959. }
  960. m_forwardStack.clear();
  961. buttonRight->setIcon(QIcon(":/images/home_right.png"));
  962. }
  963. }
  964. void DbTreeViewManager::onTreeViewClicked_updown(const QModelIndex &index){
  965. QStandardItem *item = m_pCModel->itemFromIndex(index);
  966. if (!item) return;
  967. // 是否第三层
  968. bool isThird = false;
  969. int dirId = -1;
  970. QString multiTableName = "";
  971. QVariant dataVar = item->data(Qt::UserRole + 2);
  972. if (dataVar.canConvert<QJsonObject>()) {
  973. QJsonObject obj = dataVar.toJsonObject();
  974. if (obj.contains("IsThirdLevel") && obj["IsThirdLevel"].toBool()) {
  975. isThird = true;
  976. if (obj.contains("Id")) {
  977. dirId = obj["Id"].toInt();
  978. }
  979. if (obj.contains("TableName"))
  980. {
  981. multiTableName = obj["TableName"].toString();
  982. }
  983. }
  984. }
  985. updateNavigationBar(index);
  986. if (isThird) {
  987. int userPrivilege = 0x1; // 示例
  988. if (dirId < 0) {
  989. qWarning() << "无效directoryId";
  990. return;
  991. }
  992. if (multiTableName == "")
  993. {
  994. qWarning() << "无效multiTableName";
  995. return;
  996. }
  997. QList<Table_Control_Data> tableControlDatas;
  998. bool allChangeFlag = false;
  999. m_sqlOper->GetThirdDirControlData(multiTableName, userPrivilege, tableControlDatas, allChangeFlag);
  1000. displayThirdLevelFields(tableControlDatas, allChangeFlag, false); // 传递参数表示 Home 界面
  1001. // QJsonObject data = m_sqlOper.fetchThirdLevelData(multiTableName, dirId, userPrivilege);
  1002. //displayThirdLevelFields(data, false);
  1003. // QJsonObject data = fetchThirdLevelData(dirId, userPrivilege);
  1004. // 将“新页面”压入后退栈, 并清空前进栈
  1005. PageState st;
  1006. st.path = buildItemPath(item);
  1007. st.isThirdLevel = true;
  1008. st.directoryId = dirId;
  1009. st.multiTableName = multiTableName;
  1010. m_backStack.append(st);
  1011. if(m_backStack.size() >= 2){
  1012. buttonLeft->setIcon(QIcon(":/images/home_left.png"));
  1013. }
  1014. m_forwardStack.clear();
  1015. buttonRight->setIcon(QIcon(":/images/home_right.png"));
  1016. } else {
  1017. // 不是第三层,只是选中目录
  1018. // 清理第三层窗口
  1019. if (m_thirdLevelFieldWnd) {
  1020. m_thirdLevelFieldWnd->close();
  1021. m_thirdLevelFieldWnd->deleteLater();
  1022. m_thirdLevelFieldWnd = nullptr;
  1023. }
  1024. // 同样更新历史
  1025. PageState st;
  1026. st.path = buildItemPath(item);
  1027. st.isThirdLevel = false;
  1028. st.directoryId = -1;
  1029. st.multiTableName = multiTableName;
  1030. m_backStack.append(st);
  1031. if(m_backStack.size() >= 2){
  1032. buttonLeft->setIcon(QIcon(":/images/home_left.png"));
  1033. }
  1034. m_forwardStack.clear();
  1035. buttonRight->setIcon(QIcon(":/images/home_right.png"));
  1036. }
  1037. }
  1038. void DbTreeViewManager::updateNavigationBar(const QModelIndex &index)
  1039. {
  1040. QStandardItem *item = m_pCModel->itemFromIndex(index);
  1041. if (!item) {
  1042. qWarning() << "导航栏更新失败:未找到对应项";
  1043. return;
  1044. }
  1045. // 清理现有布局
  1046. if (m_pNavigationWidget->layout()) {
  1047. QLayoutItem *child;
  1048. while ((child = m_pNavigationWidget->layout()->takeAt(0)) != nullptr) {
  1049. if (child->widget()) {
  1050. child->widget()->deleteLater();
  1051. }
  1052. delete child;
  1053. }
  1054. delete m_pNavigationWidget->layout();
  1055. }
  1056. // 构建路径列表,从当前项回溯到根节点
  1057. QList<QStandardItem *> path;
  1058. QStandardItem *temp = item;
  1059. while (temp) {
  1060. path.prepend(temp); // 从根节点开始
  1061. temp = temp->parent();
  1062. }
  1063. QVBoxLayout *newLayout = new QVBoxLayout; // 创建新的导航栏布局
  1064. newLayout->setContentsMargins(0, 0, 3, 3);
  1065. newLayout->setSpacing(0);
  1066. // 确保显示三行
  1067. for (int i = 0; i < 3; ++i) {
  1068. QLabel *label = new QLabel;
  1069. if (i < path.size()) {
  1070. QString text = path[i]->text();
  1071. if (i == 1) text = " " + text; // 二级目录缩进
  1072. if (i == 2) text = " " + text; // 三级目录缩进
  1073. label->setText(text);
  1074. } else {
  1075. label->setText(""); // 填充空白行
  1076. }
  1077. QFont font = label->font(); // 设置字体和样式
  1078. font.setPointSize(14);
  1079. font.setFamily("思源黑体");
  1080. font.setBold(true); // 设置文字为粗体
  1081. font.setLetterSpacing(QFont::PercentageSpacing,105); // 设置字间距100%为默认
  1082. label->setFont(font);
  1083. label->setFixedHeight(m_pNavigationWidget->height() / 3);
  1084. newLayout->addWidget(label);
  1085. }
  1086. // 设置布局并更新导航栏
  1087. m_pNavigationWidget->setLayout(newLayout);
  1088. m_pNavigationWidget->update();
  1089. }
  1090. void DbTreeViewManager::clearAllSeparators()
  1091. {
  1092. // 遍历 firstLevelSeparators 中所有 QFrame,
  1093. // 隐藏后调用 deleteLater()
  1094. for (auto separator : firstLevelSeparators.values()) {
  1095. if(separator) {
  1096. separator->hide();
  1097. separator->deleteLater();
  1098. }
  1099. }
  1100. // 清空映射
  1101. firstLevelSeparators.clear();
  1102. }
  1103. void DbTreeViewManager::updateSeparatorLine()
  1104. {
  1105. // 如果目录树被隐藏,就把分割线都藏起来
  1106. if (!m_pTreeViewDown->isVisible()) {
  1107. for (auto it = firstLevelSeparators.begin(); it != firstLevelSeparators.end(); ++it) {
  1108. QFrame *sep = it.value();
  1109. if (sep) sep->hide();
  1110. }
  1111. return;
  1112. }
  1113. // 遍历所有“一级目录”与其分隔线
  1114. for (auto it = firstLevelSeparators.begin(); it != firstLevelSeparators.end(); ++it) {
  1115. QStandardItem *firstLevelItem = it.key();
  1116. QFrame *separator = it.value();
  1117. if (!firstLevelItem || !separator) continue;
  1118. // 拿到可视区域
  1119. QModelIndex firstLevelIndex = m_pCModel->indexFromItem(firstLevelItem);
  1120. QRect firstLevelRect = m_pTreeViewDown->visualRect(firstLevelIndex);
  1121. // 如果这个一级目录滚动到看不见了,就把分割线也藏起来
  1122. if (!firstLevelRect.isValid()) {
  1123. separator->hide();
  1124. continue;
  1125. }
  1126. // 如果展开并且它有子节点,就找“最后一个可见子项”
  1127. if (m_pTreeViewDown->isExpanded(firstLevelIndex) && firstLevelItem->hasChildren()) {
  1128. QModelIndex lastVisibleChild = findLastVisibleChild(firstLevelIndex);
  1129. if (lastVisibleChild.isValid()) {
  1130. QRect lastChildRect = m_pTreeViewDown->visualRect(lastVisibleChild);
  1131. if (lastChildRect.isValid()) {
  1132. // 将分割线放在最后一个子项下面
  1133. separator->setGeometry(16,lastChildRect.bottom() + 115, m_pOriginalWndMenuPage->width() - 40,1);
  1134. separator->show();
  1135. continue;
  1136. }
  1137. }
  1138. }
  1139. // 如果收起或找不到可见子项,就放在本一级目录下面
  1140. separator->setGeometry(16,firstLevelRect.bottom() + 115, m_pOriginalWndMenuPage->width() - 40,1);
  1141. separator->show();
  1142. }
  1143. }
  1144. QModelIndex DbTreeViewManager::findLastVisibleChild(const QModelIndex &parentIndex) {
  1145. if (!parentIndex.isValid()) return QModelIndex();
  1146. int childCount = m_pCModel->rowCount(parentIndex);
  1147. QModelIndex lastVisible;
  1148. for (int i = childCount - 1; i >= 0; --i) {
  1149. QModelIndex childIndex = m_pCModel->index(i, 0, parentIndex);
  1150. if (!m_pTreeViewDown->isRowHidden(i, parentIndex)) {
  1151. if (m_pTreeViewDown->isExpanded(childIndex) && m_pCModel->rowCount(childIndex) > 0) {
  1152. QModelIndex deeper = findLastVisibleChild(childIndex);
  1153. if (deeper.isValid()) {
  1154. return deeper;
  1155. }
  1156. }
  1157. lastVisible = childIndex;
  1158. break;
  1159. }
  1160. }
  1161. return lastVisible;
  1162. }
  1163. //void DbTreeViewManager::displayThirdLevelFields(const QJsonObject &data, bool isHome)
  1164. //{
  1165. // if (data.isEmpty()) {
  1166. // qWarning() << "字段数据为空,无法显示";
  1167. // return;
  1168. // }
  1169. //
  1170. // if (!data.contains("IsThirdLevel") || !data["IsThirdLevel"].toBool()) {
  1171. // qWarning() << "不是三级目录,跳过按钮加载";
  1172. // return;
  1173. // }
  1174. //
  1175. // bool allChangeFlag = false;
  1176. // if (data.contains("allChangeFlag") && data["allChangeFlag"].isBool()) {
  1177. // allChangeFlag = data["allChangeFlag"].toBool();
  1178. // }
  1179. // else {
  1180. // qWarning() << "数据中缺少 'allChangeFlag' 字段或类型不正确";
  1181. // }
  1182. //
  1183. // // 若已有第三层窗口,先关闭并释放
  1184. // if (m_thirdLevelFieldWnd) {
  1185. // m_thirdLevelFieldWnd->close();
  1186. // m_thirdLevelFieldWnd->deleteLater();
  1187. // m_thirdLevelFieldWnd = nullptr;
  1188. // }
  1189. //
  1190. // // 获取当前选中的目录项,设置状态
  1191. // QModelIndex currentIndex = treeViewDown->currentIndex();
  1192. // if (currentIndex.isValid()) {
  1193. // QStandardItem* currentItem = model->itemFromIndex(currentIndex);
  1194. // if (currentItem) {
  1195. // currentItem->setCheckState(allChangeFlag ? Qt::Checked : Qt::Unchecked);
  1196. // qDebug() << "设置当前目录项复选框状态为:" << (allChangeFlag ? "Checked" : "Unchecked");
  1197. // }
  1198. // }
  1199. // else {
  1200. // qWarning() << "当前没有选中的目录项";
  1201. // }
  1202. //
  1203. // // 隐藏目录树及所有分隔线
  1204. // treeViewDown->hide();
  1205. // for (auto separator : firstLevelSeparators) {
  1206. // if (separator)
  1207. // separator->hide();
  1208. // }
  1209. //
  1210. // // 显示按钮
  1211. // loadButtonConfigForThirdLevel(data);
  1212. //
  1213. // // 清理之前的控件列表
  1214. // m_fieldWidgets.clear();
  1215. //
  1216. // // 创建新的 "字段展示" 窗口,并赋值给 m_thirdLevelFieldWnd
  1217. // m_thirdLevelFieldWnd = new QWidget(widget2);
  1218. // m_thirdLevelFieldWnd->setWindowTitle("字段展示");
  1219. // m_thirdLevelFieldWnd->setStyleSheet("background-color: transparent;");
  1220. // if (isHome) {
  1221. // // Home 界面,填满 widget2
  1222. // m_thirdLevelFieldWnd->setGeometry(0, 0, widget2->width(), widget2->height());
  1223. // }
  1224. // else {
  1225. // // 其他配置界面,设置与 treeViewDown 相同的几何位置
  1226. // m_thirdLevelFieldWnd->setGeometry(treeViewDown->geometry());
  1227. // }
  1228. //
  1229. // // 创建滚动区域
  1230. // QScrollArea *scrollArea = new QScrollArea(m_thirdLevelFieldWnd);
  1231. // scrollArea->setWidgetResizable(true);
  1232. // scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
  1233. //
  1234. // // 创建滚动容器并设置布局
  1235. // QWidget *scrollWidget = new QWidget;
  1236. // scrollWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
  1237. // QVBoxLayout *scrollLayout = new QVBoxLayout(scrollWidget);
  1238. // scrollLayout->setSpacing(10);
  1239. // scrollLayout->setContentsMargins(10, 10, 10, 10);
  1240. //
  1241. //
  1242. // //!!!提成一个类
  1243. // int previousGroupId = -1;
  1244. // // 遍历字段数组
  1245. // if (data.contains("fields") && data["fields"].isArray()) {
  1246. // QJsonArray fields = data["fields"].toArray();
  1247. // for (const QJsonValue &val : fields) {
  1248. // QJsonObject field = val.toObject();
  1249. // int fieldId = field["Id"].toInt();
  1250. // QString fieldName = field["Name"].toString();
  1251. // QString fieldType = field["Type"].toString();
  1252. // QString fieldValue = field["Value"].toString();
  1253. // //QString fieldCurrentValue = field["CurrentValue"].toString();
  1254. // QString fieldTableName = field["TableName"].toString();
  1255. // QString fieldUpLimit = field["UpLimit"].toString();
  1256. // QString fieldDownLimit = field["DownLimit"].toString();
  1257. // QString fieldUnits = field["units"].toString();
  1258. // int groupId = field["Groupld"].toInt();
  1259. //
  1260. // // 如果组变化,插入分隔线
  1261. // if (previousGroupId != -1 && groupId != previousGroupId) {
  1262. // QFrame *separator = createUnifiedSeparator(scrollWidget, 2);
  1263. // QHBoxLayout *separatorLayout = new QHBoxLayout;
  1264. // separatorLayout->setContentsMargins(0, 5, 0, 5);
  1265. // separatorLayout->addWidget(separator);
  1266. // scrollLayout->addLayout(separatorLayout);
  1267. // separator->show();
  1268. // }
  1269. // previousGroupId = groupId;
  1270. //
  1271. // // 每个字段一行
  1272. // QHBoxLayout *fieldLayout = new QHBoxLayout;
  1273. // fieldLayout->setSpacing(5);
  1274. //
  1275. // QLabel *label = new QLabel(fieldName);
  1276. // label->setAlignment(Qt::AlignVCenter | Qt::AlignLeft);
  1277. // label->setFixedHeight(24);
  1278. // label->setMinimumWidth(120);
  1279. // fieldLayout->addWidget(label);
  1280. // fieldLayout->addStretch(1);
  1281. //
  1282. // QWidget *rightWidget = new QWidget;
  1283. // QHBoxLayout *rightLayout = new QHBoxLayout(rightWidget);
  1284. // rightLayout->setContentsMargins(0, 0, 20, 0);
  1285. // rightLayout->setSpacing(5);
  1286. //
  1287. // QWidget *createdWidget = nullptr;
  1288. //
  1289. // if (fieldType == "input") {
  1290. // // 检查 Value 中是否包含逗号分隔(比如 "请输入内容1,分钟")
  1291. // if (fieldUnits!= "") {
  1292. // // // 采用逗号分割,trim去除空白
  1293. // // QStringList parts = fieldValue.split(",", Qt::SkipEmptyParts);
  1294. // // QString leftText = parts.at(0).trimmed();
  1295. // // QString rightText = (parts.size() > 1 ? parts.at(1).trimmed() : QString());
  1296. //
  1297. // // 创建 QLineEdit 显示左边文本
  1298. // QLineEdit *lineEdit = new QLineEdit();
  1299. // if((fieldUpLimit=="")&&(fieldDownLimit=="")){
  1300. // lineEdit->setText(fieldValue);
  1301. // lineEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
  1302. // lineEdit->setFixedSize(105, 28);
  1303. // lineEdit->setAlignment(Qt::AlignLeft);
  1304. // //!!!提到前面
  1305. // lineEdit->setStyleSheet(R"(
  1306. // QLineEdit {
  1307. // background: #FFFFFF;
  1308. // border: 1px solid #BABBDC;
  1309. // border-radius: 6px;
  1310. // padding: 2px 5px;
  1311. // }
  1312. // )");
  1313. // }else{//上下限至少设置了一个值
  1314. // if(fieldValue == ""){
  1315. // lineEdit->setPlaceholderText("请输入数字");
  1316. // }else{
  1317. // lineEdit->setText(fieldValue);
  1318. // }
  1319. //
  1320. //
  1321. // lineEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
  1322. // lineEdit->setFixedSize(105, 28);
  1323. // lineEdit->setAlignment(Qt::AlignLeft);
  1324. // lineEdit->setStyleSheet(R"(
  1325. // QLineEdit {
  1326. // background: #FFFFFF;
  1327. // border: 1px solid #BABBDC;
  1328. // border-radius: 6px;
  1329. // padding: 2px 5px;
  1330. // }
  1331. // )");
  1332. // }
  1333. //
  1334. // // 创建 QLabel 显示右边文本(比如 "分钟")
  1335. // QLabel *unitLabel = new QLabel(fieldUnits);
  1336. // unitLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
  1337. // unitLabel->setFixedHeight(28);
  1338. // unitLabel->setStyleSheet("QLabel { font-size: 14px; }");
  1339. //
  1340. // // 把 QLineEdit 和 QLabel 水平排列,确保 Qlabel 显示在 QLineEdit 右侧
  1341. // QHBoxLayout *inputLayout = new QHBoxLayout;
  1342. // inputLayout->setSpacing(10);
  1343. // inputLayout->setContentsMargins(0,0,0,0);
  1344. // inputLayout->addWidget(lineEdit);
  1345. // inputLayout->addWidget(unitLabel);
  1346. //
  1347. // // 用一个容器部件来装下这个布局
  1348. // QWidget *inputWidget = new QWidget;
  1349. // inputWidget->setLayout(inputLayout);
  1350. // rightLayout->addWidget(inputWidget);
  1351. //
  1352. // // 保存控件指针到 m_fieldWidgets(可选)
  1353. // m_fieldWidgets.append(lineEdit);
  1354. // m_fieldWidgets.append(unitLabel);
  1355. // createdWidget = inputWidget;
  1356. // // // 连接 textChanged 信号到 lambda 函数
  1357. // connect(lineEdit, &QLineEdit::textChanged, [this,lineEdit,fieldTableName,fieldId,fieldUpLimit,fieldDownLimit]() {
  1358. // if((fieldUpLimit!="")&&(fieldDownLimit!="")){
  1359. // int uplimit = fieldUpLimit.toInt();
  1360. // int downlimit = fieldDownLimit.toInt();
  1361. // int inputvalue = (lineEdit->text()).toInt();
  1362. // if((inputvalue<=uplimit)&&(inputvalue>=downlimit)){
  1363. // updateDb(fieldTableName,fieldId,lineEdit->text());
  1364. // }else{
  1365. // lineEdit->setPlaceholderText("超出设定范围,请重新输入");
  1366. // }
  1367. // }else if(fieldDownLimit!=""){
  1368. // int downlimit = fieldDownLimit.toInt();
  1369. // int inputvalue = (lineEdit->text()).toInt();
  1370. // if(inputvalue>=downlimit){
  1371. // updateDb(fieldTableName,fieldId,lineEdit->text());
  1372. // }else{
  1373. // lineEdit->setPlaceholderText("超出设定范围,请重新输入");
  1374. // }
  1375. // }else if(fieldUpLimit!=""){
  1376. // int uplimit = fieldUpLimit.toInt();
  1377. // int inputvalue = (lineEdit->text()).toInt();
  1378. // if(inputvalue<=uplimit){
  1379. // updateDb(fieldTableName,fieldId,lineEdit->text());
  1380. // }else{
  1381. // lineEdit->setPlaceholderText("超出设定范围,请重新输入");
  1382. // }
  1383. // }else{
  1384. // updateDb(fieldTableName,fieldId,lineEdit->text());
  1385. // }
  1386. //
  1387. // });
  1388. // } else {
  1389. // // 如果不包含逗号,按原有逻辑只创建 QLineEdit
  1390. // QLineEdit *lineEdit1 = new QLineEdit(fieldValue);
  1391. // lineEdit1->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
  1392. // lineEdit1->setFixedSize(140, 28);
  1393. // lineEdit1->setAlignment(Qt::AlignLeft);
  1394. // lineEdit1->setStyleSheet(R"(
  1395. // QLineEdit {
  1396. // background: #FFFFFF;
  1397. // border: 1px solid #BABBDC;
  1398. // border-radius: 6px;
  1399. // padding: 2px 5px;
  1400. // }
  1401. // )");
  1402. // rightLayout->addWidget(lineEdit1);
  1403. // createdWidget = lineEdit1;
  1404. // // // 连接 textChanged 信号到 lambda 函数
  1405. // connect(lineEdit1, &QLineEdit::textChanged, [this,lineEdit1,fieldTableName,fieldId]() {
  1406. // updateDb(fieldTableName,fieldId,lineEdit1->text());
  1407. // loginput(fieldTableName,fieldId,lineEdit1->text());
  1408. // });
  1409. // }
  1410. // }
  1411. // else if (fieldType == "radio") {
  1412. // // 用逗号分隔的选项
  1413. // QStringList optionList = fieldValue.split(QRegExp("[,,]"), Qt::SkipEmptyParts);
  1414. // QHBoxLayout *radioLayout = new QHBoxLayout;
  1415. // radioLayout->setSpacing(5);
  1416. // QButtonGroup *radioGroup = new QButtonGroup(rightWidget);
  1417. // for (const QString &optionText : optionList) {
  1418. // QString trimmedOpt = optionText.trimmed();
  1419. // if (trimmedOpt.isEmpty()) continue;
  1420. // QRadioButton *radioButton = new QRadioButton(trimmedOpt);
  1421. // radioGroup->addButton(radioButton);
  1422. // radioLayout->addWidget(radioButton);
  1423. // m_fieldWidgets.append(radioButton);
  1424. // if(fieldValue == trimmedOpt){
  1425. // radioButton->setChecked(true);
  1426. // }
  1427. // }
  1428. // // 使用Lambda函数连接 buttonToggled 信号
  1429. // connect(radioGroup, static_cast<void (QButtonGroup::*)(QAbstractButton *, bool)>(&QButtonGroup::buttonToggled),[this,fieldTableName,fieldId](QAbstractButton *button,bool checked) {
  1430. // if (checked) {
  1431. // // qDebug() << "Button toggled:" << button->text();
  1432. // // 在这里添加你想要执行的操作
  1433. // updateDb(fieldTableName,fieldId,button->text());
  1434. // }
  1435. // });
  1436. // rightLayout->addLayout(radioLayout);
  1437. // }
  1438. // else if (fieldType == "checkbox") {
  1439. // bool isChecked;
  1440. // if(fieldValue == ""){
  1441. // isChecked = (fieldValue == "1");
  1442. // }else{
  1443. // isChecked = (fieldValue == "1");
  1444. // }
  1445. //
  1446. // QCheckBox *checkBox = new QCheckBox;
  1447. // checkBox->setChecked(isChecked);
  1448. // checkBox->setStyleSheet(R"(
  1449. // QCheckBox::indicator {
  1450. // width: 20px;
  1451. // height: 20px;
  1452. // }
  1453. // QCheckBox::indicator:unchecked {
  1454. // background-color: #FFFFFF;
  1455. // border: 1px solid #BABBDC;
  1456. // border-radius: 2px;
  1457. // }
  1458. // QCheckBox::indicator:checked {
  1459. // image: url(:/images/three_Selecte.png);
  1460. // }
  1461. // QCheckBox {
  1462. // spacing: 0px;
  1463. // }
  1464. // )");
  1465. // rightLayout->addWidget(checkBox);
  1466. // createdWidget = checkBox;
  1467. // connect(checkBox, &QCheckBox::stateChanged, [this,fieldTableName,fieldId](int state) {
  1468. // if (state == Qt::Checked) {
  1469. // // qDebug() << "CheckBox is checked"<<fieldId;
  1470. // updateDb(fieldTableName,fieldId,"1");
  1471. // } else if (state == Qt::Unchecked) {
  1472. // // qDebug() << "CheckBox is unchecked"<<fieldId;
  1473. // // 在这里添加你想要执行的操作
  1474. // updateDb(fieldTableName,fieldId,"0");
  1475. // } else if (state == Qt::PartiallyChecked) {
  1476. // qDebug() << "CheckBox is partially checked";
  1477. // // 在这里添加你想要执行的操作
  1478. // }
  1479. // });
  1480. // }
  1481. // else if (fieldType == "ComboBox") {
  1482. // QStringList optionList = fieldValue.split(QRegExp("[,,]"), Qt::SkipEmptyParts);
  1483. // QComboBox *comboBox = new QComboBox;
  1484. // comboBox->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
  1485. // comboBox->setFixedSize(140, 28);
  1486. // comboBox->setStyleSheet(R"(
  1487. // QComboBox {
  1488. // background: #FFFFFF;
  1489. // border: 1px solid #BABBDC;
  1490. // border-radius: 6px;
  1491. // padding: 2px 5px;
  1492. // }
  1493. // QComboBox::drop-down {
  1494. // width: 20px;
  1495. // }
  1496. // )");
  1497. // // int newIndex = -1;
  1498. // for (const QString &opt : optionList) {
  1499. // QString trimmedOpt = opt.trimmed();
  1500. // if (!trimmedOpt.isEmpty())
  1501. // comboBox->addItem(trimmedOpt);
  1502. // }
  1503. // if(fieldValue == ""){
  1504. // comboBox->setCurrentIndex(0);
  1505. // }else{
  1506. // comboBox->setCurrentIndex(fieldValue.toInt());
  1507. // }
  1508. // rightLayout->addWidget(comboBox);
  1509. // createdWidget = comboBox;
  1510. // // 使用Lambda函数连接 currentIndexChanged 信号
  1511. // connect(comboBox, QOverload<int>::of(&QComboBox::currentIndexChanged), [this,comboBox,fieldTableName,fieldId](int index) {
  1512. // QString selectedText = comboBox->itemText(index);
  1513. // QString indexString = QString::number(index);
  1514. // // qDebug() << "ComboBox index changed to:" << index << "Text:" << selectedText;
  1515. // updateDb(fieldTableName,fieldId,indexString);
  1516. // // 在这里添加你想要执行的操作
  1517. // });
  1518. // }
  1519. // else if (fieldType == "time") {
  1520. // QTimeEdit *timeEdit = new QTimeEdit;
  1521. // timeEdit->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
  1522. // timeEdit->setFixedSize(120, 28);
  1523. // timeEdit->setStyleSheet(R"(
  1524. // QTimeEdit {
  1525. // background: #FFFFFF;
  1526. // border: 1px solid #BABBDC;
  1527. // border-radius: 6px;
  1528. // padding: 2px 5px;
  1529. // }
  1530. // )");
  1531. // timeEdit->setDisplayFormat("HH:mm:ss");
  1532. // timeEdit->setTime(QTime::fromString(fieldValue, "HH:mm:ss"));
  1533. // rightLayout->addWidget(timeEdit);
  1534. // createdWidget = timeEdit;
  1535. // }
  1536. // else if (fieldType == "switch") {
  1537. // QWidget *switchContainer = new QWidget;
  1538. // QHBoxLayout *switchLayout = new QHBoxLayout(switchContainer);
  1539. // switchLayout->setSpacing(5);
  1540. // switchLayout->setContentsMargins(0, 0, 0, 0);
  1541. // QCheckBox *switchBox = new QCheckBox;
  1542. // switchBox->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
  1543. // switchBox->setFixedSize(30, 30);
  1544. // switchBox->setStyleSheet(R"(
  1545. // QCheckBox::indicator {
  1546. // width: 30px;
  1547. // height: 30px;
  1548. // }
  1549. // QCheckBox::indicator:unchecked {
  1550. // background-color: #BABBDC;
  1551. // border-radius: 6px;
  1552. // }
  1553. // QCheckBox::indicator:checked {
  1554. // background-color: #4CAF50;
  1555. // border-radius: 6px;
  1556. // }
  1557. // )");
  1558. // QString switchValue;
  1559. //
  1560. // if(fieldValue ==""){
  1561. // switchValue = fieldValue.toLower();
  1562. // }else{
  1563. // switchValue = fieldValue.toLower();
  1564. // }
  1565. // if (switchValue == "on")
  1566. // switchBox->setChecked(true);
  1567. // else
  1568. // switchBox->setChecked(false);
  1569. // QLabel *switchLabel = new QLabel(switchBox->isChecked() ? "开" : "关");
  1570. // switchLabel->setAlignment(Qt::AlignVCenter | Qt::AlignLeft);
  1571. // switchLabel->setStyleSheet("font-size: 14px;");
  1572. // connect(switchBox, &QCheckBox::stateChanged, [fieldTableName,fieldId,switchLabel, this](int state){
  1573. // if (state == Qt::Checked){
  1574. // switchLabel->setText("开");
  1575. // updateDb(fieldTableName,fieldId,"on");
  1576. // }else{
  1577. // switchLabel->setText("关");
  1578. // updateDb(fieldTableName,fieldId,"off");
  1579. // }
  1580. // });
  1581. // switchLayout->addWidget(switchBox);
  1582. // switchLayout->addWidget(switchLabel);
  1583. // rightLayout->addWidget(switchContainer);
  1584. // createdWidget = switchBox;
  1585. // }
  1586. // else if (fieldType == "combined") {
  1587. // // 用逗号分隔
  1588. // QStringList parts = fieldValue.split(QRegExp("[,,]"), Qt::SkipEmptyParts);
  1589. // QString inputText = (parts.size() >= 1 ? parts.at(0).trimmed() : QString());
  1590. // QString button1Text = (parts.size() >= 2 ? parts.at(1).trimmed() : QStringLiteral("获取"));
  1591. // QString button2Text = (parts.size() >= 3 ? parts.at(2).trimmed() : QStringLiteral("设置"));
  1592. //
  1593. // QLineEdit *comboInput = new QLineEdit(inputText);
  1594. // comboInput->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
  1595. // comboInput->setFixedHeight(28);
  1596. // comboInput->setStyleSheet(R"(
  1597. // QLineEdit {
  1598. // background: #FFFFFF;
  1599. // border: 1px solid #BABBDC;
  1600. // border-radius: 5px;
  1601. // padding: 2px 5px;
  1602. // }
  1603. // )");
  1604. // m_fieldWidgets.append(comboInput);
  1605. //
  1606. // QWidget *combinedWidget = new QWidget;
  1607. // QHBoxLayout *combinedLayout = new QHBoxLayout(combinedWidget);
  1608. // combinedLayout->setSpacing(5);
  1609. // combinedLayout->setContentsMargins(0, 0, 0, 0);
  1610. //
  1611. // QPushButton *btnGet = new QPushButton(button1Text, combinedWidget);
  1612. // btnGet->setFixedSize(80, 28);
  1613. // btnGet->setStyleSheet(R"(
  1614. // QPushButton {
  1615. // background: #FFFFFF;
  1616. // border: 1px solid #BABBDC;
  1617. // border-radius: 5px;
  1618. // }
  1619. // QPushButton:hover {
  1620. // background-color: #F0F0F0;
  1621. // }
  1622. // )");
  1623. // m_fieldWidgets.append(btnGet);
  1624. //
  1625. // QPushButton *btnSet = new QPushButton(button2Text, combinedWidget);
  1626. // btnSet->setFixedSize(80, 28);
  1627. // btnSet->setStyleSheet(R"(
  1628. // QPushButton {
  1629. // background: #FFFFFF;
  1630. // border: 1px solid #BABBDC;
  1631. // border-radius: 5px;
  1632. // }
  1633. // QPushButton:hover {
  1634. // background-color: #F0F0F0;
  1635. // }
  1636. // )");
  1637. // m_fieldWidgets.append(btnSet);
  1638. //
  1639. // combinedLayout->addWidget(comboInput);
  1640. // combinedLayout->addWidget(btnGet);
  1641. // combinedLayout->addWidget(btnSet);
  1642. // combinedLayout->addStretch();
  1643. // rightLayout->addWidget(combinedWidget);
  1644. // }
  1645. // else if (fieldType == "Text")
  1646. // {
  1647. // QLabel* label = new QLabel();
  1648. // label->setText(fieldValue);
  1649. // label->setAlignment(Qt::AlignVCenter | Qt::AlignLeft);
  1650. // label->setFixedHeight(24);
  1651. // label->setMinimumWidth(120);
  1652. // rightLayout->addWidget(label);
  1653. //
  1654. // } else
  1655. // {
  1656. // qWarning() << "未知字段类型:" << fieldType;
  1657. // }
  1658. //
  1659. // if (createdWidget)
  1660. // m_fieldWidgets.append(createdWidget);
  1661. //
  1662. // fieldLayout->addWidget(rightWidget);
  1663. // scrollLayout->addLayout(fieldLayout);
  1664. // }
  1665. // }
  1666. //
  1667. // scrollLayout->addStretch();
  1668. //
  1669. // scrollArea->setWidget(scrollWidget);
  1670. //
  1671. // QVBoxLayout *mainLayout = new QVBoxLayout(m_thirdLevelFieldWnd);
  1672. // mainLayout->setContentsMargins(0, 0, 0, 0);
  1673. // mainLayout->addWidget(scrollArea);
  1674. //
  1675. // m_thirdLevelFieldWnd->show();
  1676. //}
  1677. QString extractBeforeComma(const QString& input)
  1678. {
  1679. int commaPos = input.indexOf(',');
  1680. if (commaPos != -1) {
  1681. return input.left(commaPos);
  1682. }
  1683. return input;
  1684. }
  1685. void DbTreeViewManager::displayThirdLevelFields(const QList<Table_Control_Data>& data, bool allChangeFlag, bool isHome)
  1686. {
  1687. if (data.isEmpty())
  1688. {
  1689. qWarning() << "字段数据为空,无法显示";
  1690. return;
  1691. }
  1692. //遍历得到结果allChangeFalg
  1693. // 若已有第三层窗口,先关闭并释放
  1694. if (m_thirdLevelFieldWnd)
  1695. {
  1696. m_thirdLevelFieldWnd->close();
  1697. m_thirdLevelFieldWnd->deleteLater();
  1698. m_thirdLevelFieldWnd = nullptr;
  1699. }
  1700. // 获取当前选中的目录项,设置状态
  1701. QModelIndex currentIndex = m_pTreeViewDown->currentIndex();
  1702. if (currentIndex.isValid())
  1703. {
  1704. QStandardItem* currentItem = m_pCModel->itemFromIndex(currentIndex);
  1705. if (currentItem)
  1706. {
  1707. currentItem->setCheckState(allChangeFlag ? Qt::Checked : Qt::Unchecked);
  1708. qDebug() << "设置当前目录项复选框状态为:" << (allChangeFlag ? "Checked" : "Unchecked");
  1709. }
  1710. } else
  1711. {
  1712. qWarning() << "当前没有选中的目录项";
  1713. }
  1714. // 隐藏目录树及所有分隔线
  1715. m_pTreeViewDown->hide();
  1716. for (auto separator : firstLevelSeparators)
  1717. {
  1718. if (separator)
  1719. separator->hide();
  1720. }
  1721. // 清理之前的控件列表
  1722. m_fieldWidgets.clear();
  1723. // 创建新的 "字段展示" 窗口,并赋值给 m_thirdLevelFieldWnd
  1724. m_thirdLevelFieldWnd = new QWidget(m_pOriginalWndMenuPage);
  1725. m_thirdLevelFieldWnd->setWindowTitle("字段展示");
  1726. m_thirdLevelFieldWnd->setStyleSheet("background-color: transparent;");
  1727. if (isHome)
  1728. {
  1729. // Home 界面,填满 widget2
  1730. m_thirdLevelFieldWnd->setGeometry(0, 0, m_pOriginalWndMenuPage->width(), m_pOriginalWndMenuPage->height());
  1731. } else
  1732. {
  1733. // 其他配置界面,设置与 treeViewDown 相同的几何位置
  1734. m_thirdLevelFieldWnd->setGeometry(m_pTreeViewDown->geometry());
  1735. }
  1736. // 创建滚动区域
  1737. QScrollArea* scrollArea = new QScrollArea(m_thirdLevelFieldWnd);
  1738. scrollArea->setWidgetResizable(true);
  1739. scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
  1740. // 创建滚动容器并设置布局
  1741. QWidget* scrollWidget = new QWidget;
  1742. scrollWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
  1743. QVBoxLayout* scrollLayout = new QVBoxLayout(scrollWidget);
  1744. scrollLayout->setSpacing(10);
  1745. scrollLayout->setContentsMargins(10, 10, 10, 10);
  1746. QHBoxLayout* fieldLayout = new QHBoxLayout;
  1747. fieldLayout->setSpacing(5);
  1748. QPushButton* btnSave = new QPushButton(QStringLiteral("保存"));
  1749. btnSave->setFixedSize(80, 28);
  1750. btnSave->setStyleSheet(R"(
  1751. QPushButton {
  1752. background: #FFFFFF;
  1753. border: 1px solid #BABBDC;
  1754. border-radius: 5px;
  1755. }
  1756. QPushButton:hover {
  1757. background-color: #F0F0F0;
  1758. }
  1759. )");
  1760. m_fieldWidgets.append(btnSave);
  1761. connect(btnSave, &QPushButton::clicked, this, [=]() {
  1762. if (modifiedLabels.isEmpty()) {
  1763. QMessageBox::information(this, "提示", "没有任何组件被修改!");
  1764. return;
  1765. }
  1766. int ret = QMessageBox::question(this, "保存确认", "检测到修改内容,是否保存?",
  1767. QMessageBox::Yes | QMessageBox::No);
  1768. if (ret == QMessageBox::Yes) {
  1769. emit saveClicked();
  1770. }
  1771. });
  1772. fieldLayout->addWidget(btnSave);
  1773. scrollLayout->addLayout(fieldLayout);
  1774. QFrame* separator = createUnifiedSeparator(scrollWidget, 2);
  1775. QHBoxLayout* separatorLayout = new QHBoxLayout;
  1776. separatorLayout->setContentsMargins(0, 5, 0, 5);
  1777. separatorLayout->addWidget(separator);
  1778. scrollLayout->addLayout(separatorLayout);
  1779. separator->show();
  1780. //不同表格的全部插进来
  1781. int previousGroupId = -1;
  1782. QList<CONFIG_BASE_STRUCT> buttonControls;
  1783. for (const Table_Control_Data& tableData : data)
  1784. {
  1785. //遍历控件list
  1786. for (const CONFIG_BASE_STRUCT& control : tableData.controlDataList)
  1787. {
  1788. //判断特殊的按钮控件
  1789. if (control.strType == "Button")
  1790. {
  1791. buttonControls.append(control);
  1792. }
  1793. else
  1794. {
  1795. //先创建label
  1796. //int fieldId = control.iId;
  1797. QString fieldName = QString::fromStdString(control.strName);
  1798. QString fieldDescribe = QString::fromStdString(control.strDescribe);
  1799. QString fieldType = QString::fromStdString(control.strType);
  1800. QString fieldValue = QString::fromStdString(control.strValue);
  1801. QString fieldDefult = QString::fromStdString(control.strDefult);
  1802. QString fieldUpLimit = QString::fromStdString(control.strUpperLimit);
  1803. QString fieldDownLimit = QString::fromStdString(control.strLowerLimit);
  1804. QString fieldUnits = QString::fromStdString(control.strUnit);
  1805. QString strAxisType = QString::fromStdString(control.strAxisType);
  1806. QString strstrModuleType = m_strModuleTypeAAA;
  1807. int groupId = control.iGroupId;
  1808. QString fieldTableName = extractBeforeComma(tableData.tableName);
  1809. // 如果组变化,插入分隔线
  1810. if (previousGroupId != -1 && groupId != previousGroupId)
  1811. {
  1812. QFrame* separator = createUnifiedSeparator(scrollWidget, 2);
  1813. QHBoxLayout* separatorLayout = new QHBoxLayout;
  1814. separatorLayout->setContentsMargins(0, 5, 0, 5);
  1815. separatorLayout->addWidget(separator);
  1816. scrollLayout->addLayout(separatorLayout);
  1817. separator->show();
  1818. }
  1819. previousGroupId = groupId;
  1820. // 每个字段一行
  1821. QHBoxLayout* fieldLayout = new QHBoxLayout;
  1822. fieldLayout->setSpacing(5);
  1823. QLabel* label = new QLabel(fieldDescribe);
  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 : firstLevelSeparators) {
  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<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 : firstLevelSeparators) {
  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<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. // 清理并隐藏三级目录菜单内容
  3489. void DbTreeViewManager::clearThirdLevelMenu()
  3490. {
  3491. // 遍历所有子控件,找到标题为 "字段展示" 的窗口并关闭
  3492. foreach (QObject *child, m_pOriginalWndMenuPage->children()) {
  3493. QWidget *childWidget = qobject_cast<QWidget*>(child);
  3494. if (childWidget && childWidget->windowTitle() == "字段展示") {
  3495. qDebug() << "关闭现有的字段展示窗口";
  3496. childWidget->close();
  3497. }
  3498. }
  3499. // 显示主目录树和分隔线
  3500. m_pTreeViewDown->show();
  3501. for (auto separator : firstLevelSeparators) {
  3502. separator->show();
  3503. }
  3504. }
  3505. // 保存选中路径
  3506. void DbTreeViewManager::saveCheckedPaths()
  3507. {
  3508. if (m_currentConfigName == "") {
  3509. qWarning() << "当前配置ID无效,无法保存复选框状态。";
  3510. return;
  3511. }
  3512. QSettings settings("RunCloudTech", "David");
  3513. //QString configKey = QString::number(m_currentConfigId);
  3514. settings.beginGroup("TreeViewCheckedState");
  3515. // 保存选中路径
  3516. QString keyChecked = QString("checkedPaths/%1").arg(m_currentConfigName);
  3517. QStringList checkedList = collectCheckedPaths();
  3518. settings.setValue(keyChecked, checkedList);
  3519. settings.endGroup();
  3520. qDebug() << "保存复选框状态路径:" << checkedList;
  3521. }
  3522. // 加载选中路径
  3523. void DbTreeViewManager::loadCheckedPaths()
  3524. {
  3525. if (m_currentConfigName == "") {
  3526. qWarning() << "当前配置ID无效,无法加载复选框状态。";
  3527. return;
  3528. }
  3529. QSettings settings("RunCloudTech", "David");
  3530. //QString configKey = QString::number(m_currentConfigId);
  3531. settings.beginGroup("TreeViewCheckedState");
  3532. // 读取选中路径
  3533. QString keyChecked = QString("checkedPaths/%1").arg(m_currentConfigName);
  3534. QStringList loadedChecked = settings.value(keyChecked).toStringList();
  3535. settings.endGroup();
  3536. qDebug() << "加载复选框状态路径:" << loadedChecked;
  3537. m_blockItemChanged = true;
  3538. setCheckedPaths(loadedChecked);
  3539. m_blockItemChanged = false;
  3540. // 更新导航栏
  3541. if (!loadedChecked.isEmpty()) {
  3542. QString lastPathStr = loadedChecked.last();
  3543. QStringList lastPath = lastPathStr.split("/");
  3544. QModelIndex lastIdx = findItemByPath(lastPath);
  3545. if (lastIdx.isValid()) {
  3546. m_pTreeViewDown->setCurrentIndex(lastIdx);
  3547. updateNavigationBar(lastIdx);
  3548. }
  3549. } else {
  3550. // 如果没有加载到任何路径,自动选择第一个目录
  3551. QStandardItem *rootItem = m_pCModel->invisibleRootItem();
  3552. if (rootItem->rowCount() > 0) {
  3553. QModelIndex firstIndex = m_pCModel->index(0, 0, QModelIndex());
  3554. if (firstIndex.isValid()) {
  3555. m_pTreeViewDown->setCurrentIndex(firstIndex);
  3556. m_pTreeViewDown->expand(firstIndex); // 展开第一个目录
  3557. QStandardItem *firstItem = m_pCModel->itemFromIndex(firstIndex);
  3558. QVariant data = firstItem->data(Qt::UserRole + 2);
  3559. if (data.canConvert<QJsonObject>()) {
  3560. QJsonObject thirdLevelObj = data.toJsonObject();
  3561. if (thirdLevelObj.contains("IsThirdLevel") && thirdLevelObj["IsThirdLevel"].toBool()) {
  3562. QString multiTableName = "";
  3563. if (thirdLevelObj.contains("TableName")) {
  3564. multiTableName = thirdLevelObj["TableName"].toString();
  3565. }
  3566. QList<CONFIG_BASE_STRUCT> buttons;
  3567. int userGrade = 0x1;
  3568. m_sqlOper->GetThirdDirButtons(multiTableName, userGrade, buttons);
  3569. displayThirdLevelButtons(buttons);
  3570. //loadButtonConfigForThirdLevel(thirdLevelObj);
  3571. }
  3572. }
  3573. }
  3574. }
  3575. }
  3576. }
  3577. // 保存展开路径
  3578. void DbTreeViewManager::saveExpandedPaths()
  3579. {
  3580. if (m_currentConfigName == "") {
  3581. qWarning() << "当前配置ID无效,无法保存展开路径。";
  3582. return;
  3583. }
  3584. QSettings settings("RunCloudTech", "David");
  3585. //QString configKey = QString::number(m_currentConfigId);
  3586. settings.beginGroup("TreeViewExpandedState");
  3587. // 保存展开路径
  3588. QString keyExpanded = QString("expandedPaths/%1").arg(m_currentConfigName);
  3589. QStringList expandedList = QStringList(expandedPaths.begin(), expandedPaths.end());
  3590. settings.setValue(keyExpanded, expandedList);
  3591. settings.endGroup();
  3592. qDebug() << "保存展开路径:" << expandedList;
  3593. }
  3594. // 加载展开路径
  3595. void DbTreeViewManager::loadExpandedPaths()
  3596. {
  3597. if (m_currentConfigName == "") {
  3598. qWarning() << "当前配置ID无效,无法加载展开路径。";
  3599. return;
  3600. }
  3601. QSettings settings("RunCloudTech", "David");
  3602. //QString configKey = QString::number(m_currentConfigId);
  3603. settings.beginGroup("TreeViewExpandedState");
  3604. // 读取展开路径
  3605. QString keyExpanded = QString("expandedPaths/%1").arg(m_currentConfigName);
  3606. QStringList loadedExpanded = settings.value(keyExpanded).toStringList();
  3607. settings.endGroup();
  3608. qDebug() << "加载展开路径:" << loadedExpanded;
  3609. m_blockItemChanged = true;
  3610. // 恢复展开路径
  3611. for (const QString &p : loadedExpanded) {
  3612. QStringList path = p.split("/");
  3613. QModelIndex idx = findItemByPath(path);
  3614. if (idx.isValid()) {
  3615. m_pTreeViewDown->expand(idx);
  3616. expandedPaths.insert(p);
  3617. qDebug() << "成功恢复展开路径:" << p;
  3618. } else {
  3619. qDebug() << "未找到展开路径部分: " << p;
  3620. }
  3621. }
  3622. m_blockItemChanged = false;
  3623. }
  3624. // 清空历史
  3625. void DbTreeViewManager::clearHistory()
  3626. {
  3627. m_backStack.clear();
  3628. m_forwardStack.clear();
  3629. buttonLeft->setIcon(QIcon(":/images/home_left_hide.png"));
  3630. buttonRight->setIcon(QIcon(":/images/home_right.png"));
  3631. }
  3632. // 获取当前页面的 PageState
  3633. DbTreeViewManager::PageState DbTreeViewManager::getCurrentPageState()
  3634. {
  3635. PageState st;
  3636. st.isThirdLevel = false;
  3637. st.directoryId = -1;
  3638. // 判断是否在第三层窗口中
  3639. if (m_thirdLevelFieldWnd && m_thirdLevelFieldWnd->isVisible()) {
  3640. // 如果此时是在第三层界面,则用当前导航栏 or 目录树的 “选中项” 来获取路径
  3641. QModelIndex curIndex = m_pTreeViewDown->currentIndex();
  3642. if (!curIndex.isValid()) {
  3643. st.path << "未知目录";
  3644. } else {
  3645. QStandardItem* item = m_pCModel->itemFromIndex(curIndex);
  3646. st.path = buildItemPath(item);
  3647. }
  3648. // 如果能拿到 DirectoryId,则置为第三层
  3649. QVariant dataVar = m_pCModel->itemFromIndex(m_pTreeViewDown->currentIndex())->data(Qt::UserRole + 2);
  3650. if (dataVar.canConvert<QJsonObject>()) {
  3651. QJsonObject obj = dataVar.toJsonObject();
  3652. if (obj.contains("IsThirdLevel") && obj["IsThirdLevel"].toBool()) {
  3653. st.isThirdLevel = true;
  3654. st.directoryId = obj.value("Id").toInt(-1);
  3655. st.multiTableName = obj.value("TableName").toString();
  3656. }
  3657. }
  3658. }
  3659. else {
  3660. // 当前是目录树界面,就看 treeView 选中项
  3661. QModelIndex curIndex = m_pTreeViewDown->currentIndex();
  3662. if (curIndex.isValid()) {
  3663. QStandardItem* item = m_pCModel->itemFromIndex(curIndex);
  3664. st.path = buildItemPath(item);
  3665. // 检查是否第三层
  3666. QVariant dataVar = item->data(Qt::UserRole + 2);
  3667. if (dataVar.canConvert<QJsonObject>()) {
  3668. QJsonObject obj = dataVar.toJsonObject();
  3669. if (obj.contains("IsThirdLevel") && obj["IsThirdLevel"].toBool()) {
  3670. st.isThirdLevel = true;
  3671. st.directoryId = obj.value("Id").toInt(-1);
  3672. st.multiTableName = obj.value("TableName").toString();
  3673. }
  3674. }
  3675. }
  3676. }
  3677. return st;
  3678. }
  3679. //加载指定的 PageState:相当于“回到该路径”或“跳转到这个第三层”
  3680. void DbTreeViewManager::loadPageState(const PageState &st, bool isByHistoryNav)
  3681. {
  3682. // 先根据 st.path 找到对应的节点
  3683. QModelIndex idx = findItemByPath(st.path);
  3684. if (!idx.isValid()) {
  3685. qWarning() << "[loadPageState] 未找到路径" << st.path;
  3686. return;
  3687. }
  3688. // 选中该节点
  3689. m_pTreeViewDown->setCurrentIndex(idx);
  3690. m_pTreeViewDown->scrollTo(idx);
  3691. // 更新导航栏
  3692. updateNavigationBar(idx);
  3693. // 如果是第三层,就加载对应的数据窗口
  3694. if (st.isThirdLevel && st.directoryId != -1) {
  3695. // 如果当前已在一个第三层窗口,先关闭它
  3696. if (m_thirdLevelFieldWnd) {
  3697. m_thirdLevelFieldWnd->close();
  3698. m_thirdLevelFieldWnd->deleteLater();
  3699. m_thirdLevelFieldWnd = nullptr;
  3700. }
  3701. int userPrivilege = 0x1;
  3702. QList<Table_Control_Data> tableControlDatas;
  3703. bool allChangeFlag = false;
  3704. m_sqlOper->GetThirdDirControlData(st.multiTableName, userPrivilege, tableControlDatas, allChangeFlag);
  3705. displayThirdLevelFields(tableControlDatas, allChangeFlag, false); // 传递参数表示 Home 界面
  3706. //QJsonObject data = fetchThirdLevelData(st.directoryId, userPrivilege);
  3707. //displayThirdLevelFields(data, (m_currentConfigName == "Home"));
  3708. } else {
  3709. // 否则,显示目录树
  3710. if (m_thirdLevelFieldWnd) {
  3711. m_thirdLevelFieldWnd->close();
  3712. m_thirdLevelFieldWnd->deleteLater();
  3713. m_thirdLevelFieldWnd = nullptr;
  3714. }
  3715. m_pTreeViewDown->show();
  3716. for (auto sep : firstLevelSeparators) {
  3717. if (sep) sep->show();
  3718. }
  3719. }
  3720. if (!isByHistoryNav) {
  3721. m_forwardStack.clear();
  3722. buttonRight->setIcon(QIcon(":/images/home_right.png"));
  3723. // 把新状态压入后退栈
  3724. m_backStack.append(st);
  3725. if(m_backStack.size() >= 2){
  3726. buttonLeft->setIcon(QIcon(":/images/home_left.png"));
  3727. }
  3728. }
  3729. }
  3730. void DbTreeViewManager::loginput(const QString& fieldTableName, const QString& fieldName,const QString& modifies){
  3731. // 获取当前时间并格式化
  3732. QDateTime currentDateTime = QDateTime::currentDateTime();
  3733. QString timestamp = currentDateTime.toString("yyyy-MM-dd hh:mm:ss");
  3734. QString logMessage = QString("%1 :fieldTableName is %2 and fieldName is %3 ,currentvalue is modified %4" ).arg(timestamp).arg(fieldTableName).arg(fieldName).arg(modifies);
  3735. QString pathoflog = "C:\\Users\\Administrator\\Desktop\\qt\\merge\\gujiangdong\\die-bonder-ui0218\\die-bonder-ui\\log/log.txt";
  3736. writeLogToFile(logMessage,pathoflog);
  3737. }
  3738. void DbTreeViewManager::writeLogToFile(const QString& logMessage, const QString& filePath) {
  3739. QFile logFile(filePath);
  3740. if (!logFile.open(QIODevice::Append | QIODevice::Text)) {
  3741. qWarning() << "Cannot open file for writing:" << filePath;
  3742. return;
  3743. }
  3744. QTextStream out(&logFile);
  3745. out << logMessage << "\n";
  3746. logFile.close();
  3747. }