DbTreeViewManager.cpp 141 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738
  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. //不同表格的全部插进来
  1747. int previousGroupId = -1;
  1748. QList<CONFIG_BASE_STRUCT> buttonControls;
  1749. for (const Table_Control_Data& tableData : data)
  1750. {
  1751. //遍历控件list
  1752. for (const CONFIG_BASE_STRUCT& control : tableData.controlDataList)
  1753. {
  1754. //判断特殊的按钮控件
  1755. if (control.strType == "Button")
  1756. {
  1757. buttonControls.append(control);
  1758. }
  1759. else
  1760. {
  1761. //先创建label
  1762. //int fieldId = control.iId;
  1763. QString fieldName = QString::fromStdString(control.strName);
  1764. QString fieldDescribe = QString::fromStdString(control.strDescribe);
  1765. QString fieldType = QString::fromStdString(control.strType);
  1766. QString fieldValue = QString::fromStdString(control.strValue);
  1767. QString fieldUpLimit = QString::fromStdString(control.strUpperLimit);
  1768. QString fieldDownLimit = QString::fromStdString(control.strLowerLimit);
  1769. QString fieldUnits = QString::fromStdString(control.strUnit);
  1770. QString strAxisType = QString::fromStdString(control.strAxisType);
  1771. QString strstrModuleType = m_strModuleTypeAAA;
  1772. int groupId = control.iGroupId;
  1773. QString fieldTableName = extractBeforeComma(tableData.tableName);
  1774. // 如果组变化,插入分隔线
  1775. if (previousGroupId != -1 && groupId != previousGroupId)
  1776. {
  1777. QFrame* separator = createUnifiedSeparator(scrollWidget, 2);
  1778. QHBoxLayout* separatorLayout = new QHBoxLayout;
  1779. separatorLayout->setContentsMargins(0, 5, 0, 5);
  1780. separatorLayout->addWidget(separator);
  1781. scrollLayout->addLayout(separatorLayout);
  1782. separator->show();
  1783. }
  1784. previousGroupId = groupId;
  1785. // 每个字段一行
  1786. QHBoxLayout* fieldLayout = new QHBoxLayout;
  1787. fieldLayout->setSpacing(5);
  1788. QLabel* label = new QLabel(fieldDescribe);
  1789. label->setAlignment(Qt::AlignVCenter | Qt::AlignLeft);
  1790. label->setFixedHeight(24);
  1791. label->setMinimumWidth(120);
  1792. fieldLayout->addWidget(label);
  1793. fieldLayout->addStretch(1);
  1794. QWidget* rightWidget = new QWidget;
  1795. QHBoxLayout* rightLayout = new QHBoxLayout(rightWidget);
  1796. rightLayout->setContentsMargins(0, 0, 20, 0);
  1797. rightLayout->setSpacing(5);
  1798. QWidget* createdWidget = nullptr;
  1799. if (fieldType == "input" || fieldType == "LONG" || fieldType == "STRING")
  1800. {
  1801. // 检查 Value 中是否包含逗号分隔(比如 "请输入内容1,分钟")
  1802. if (fieldUnits != "")
  1803. {
  1804. // // 采用逗号分割,trim去除空白
  1805. // QStringList parts = fieldValue.split(",", Qt::SkipEmptyParts);
  1806. // QString leftText = parts.at(0).trimmed();
  1807. // QString rightText = (parts.size() > 1 ? parts.at(1).trimmed() : QString());
  1808. // 创建 QLineEdit 显示左边文本
  1809. QLineEdit* lineEdit = new QLineEdit();
  1810. if ((fieldUpLimit == "") && (fieldDownLimit == ""))
  1811. {
  1812. lineEdit->setText(fieldValue);
  1813. lineEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
  1814. lineEdit->setFixedSize(105, 28);
  1815. lineEdit->setAlignment(Qt::AlignLeft);
  1816. //!!!提到前面
  1817. lineEdit->setStyleSheet(R"(
  1818. QLineEdit {
  1819. background: #FFFFFF;
  1820. border: 1px solid #BABBDC;
  1821. border-radius: 6px;
  1822. padding: 2px 5px;
  1823. }
  1824. )");
  1825. } else
  1826. {//上下限至少设置了一个值
  1827. if (fieldValue == "")
  1828. {
  1829. lineEdit->setPlaceholderText(tr("请输入数字"));
  1830. } else
  1831. {
  1832. lineEdit->setText(fieldValue);
  1833. }
  1834. lineEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
  1835. lineEdit->setFixedSize(105, 28);
  1836. lineEdit->setAlignment(Qt::AlignLeft);
  1837. lineEdit->setStyleSheet(R"(
  1838. QLineEdit {
  1839. background: #FFFFFF;
  1840. border: 1px solid #BABBDC;
  1841. border-radius: 6px;
  1842. padding: 2px 5px;
  1843. }
  1844. )");
  1845. }
  1846. // 创建 QLabel 显示右边文本(比如 "分钟")
  1847. QLabel* unitLabel = new QLabel(fieldUnits);
  1848. unitLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
  1849. unitLabel->setFixedHeight(28);
  1850. unitLabel->setStyleSheet("QLabel { font-size: 14px; }");
  1851. // 把 QLineEdit 和 QLabel 水平排列,确保 Qlabel 显示在 QLineEdit 右侧
  1852. QHBoxLayout* inputLayout = new QHBoxLayout;
  1853. inputLayout->setSpacing(10);
  1854. inputLayout->setContentsMargins(0, 0, 0, 0);
  1855. inputLayout->addWidget(lineEdit);
  1856. inputLayout->addWidget(unitLabel);
  1857. // 用一个容器部件来装下这个布局
  1858. QWidget* inputWidget = new QWidget;
  1859. inputWidget->setLayout(inputLayout);
  1860. rightLayout->addWidget(inputWidget);
  1861. // 保存控件指针到 m_fieldWidgets(可选)
  1862. m_fieldWidgets.append(lineEdit);
  1863. m_fieldWidgets.append(unitLabel);
  1864. createdWidget = inputWidget;
  1865. // // 连接 textChanged 信号到 lambda 函数
  1866. connect(lineEdit, &QLineEdit::textChanged, [this, lineEdit, fieldTableName, fieldName, fieldUpLimit, fieldDownLimit]() {
  1867. if ((fieldUpLimit != "") && (fieldDownLimit != ""))
  1868. {
  1869. int uplimit = fieldUpLimit.toInt();
  1870. int downlimit = fieldDownLimit.toInt();
  1871. int inputvalue = (lineEdit->text()).toInt();
  1872. if ((inputvalue <= uplimit) && (inputvalue >= downlimit))
  1873. {
  1874. m_sqlOper->updateControlData(fieldTableName, fieldName, lineEdit->text());
  1875. //updateDb(fieldTableName, fieldId, lineEdit->text());
  1876. } else
  1877. {
  1878. lineEdit->setPlaceholderText(tr("超出设定范围,请重新输入"));
  1879. }
  1880. } else if (fieldDownLimit != "")
  1881. {
  1882. int downlimit = fieldDownLimit.toInt();
  1883. int inputvalue = (lineEdit->text()).toInt();
  1884. if (inputvalue >= downlimit)
  1885. {
  1886. m_sqlOper->updateControlData(fieldTableName, fieldName, lineEdit->text());
  1887. //updateDb(fieldTableName, fieldId, lineEdit->text());
  1888. } else
  1889. {
  1890. lineEdit->setPlaceholderText(tr("超出设定范围,请重新输入"));
  1891. }
  1892. } else if (fieldUpLimit != "")
  1893. {
  1894. int uplimit = fieldUpLimit.toInt();
  1895. int inputvalue = (lineEdit->text()).toInt();
  1896. if (inputvalue <= uplimit)
  1897. {
  1898. m_sqlOper->updateControlData(fieldTableName, fieldName, lineEdit->text());
  1899. //updateDb(fieldTableName, fieldId, lineEdit->text());
  1900. } else
  1901. {
  1902. lineEdit->setPlaceholderText(tr("超出设定范围,请重新输入"));
  1903. }
  1904. } else
  1905. {
  1906. m_sqlOper->updateControlData(fieldTableName, fieldName, lineEdit->text());
  1907. //updateDb(fieldTableName, fieldId, lineEdit->text());
  1908. }
  1909. });
  1910. }
  1911. else
  1912. {
  1913. // 如果不包含逗号,按原有逻辑只创建 QLineEdit
  1914. QLineEdit* lineEdit1 = new QLineEdit(fieldValue);
  1915. lineEdit1->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
  1916. lineEdit1->setFixedSize(140, 28);
  1917. lineEdit1->setAlignment(Qt::AlignLeft);
  1918. lineEdit1->setStyleSheet(R"(
  1919. QLineEdit {
  1920. background: #FFFFFF;
  1921. border: 1px solid #BABBDC;
  1922. border-radius: 6px;
  1923. padding: 2px 5px;
  1924. }
  1925. )");
  1926. rightLayout->addWidget(lineEdit1);
  1927. createdWidget = lineEdit1;
  1928. // // 连接 textChanged 信号到 lambda 函数
  1929. connect(lineEdit1, &QLineEdit::textChanged, [this, lineEdit1, fieldTableName, fieldName]() {
  1930. m_sqlOper->updateControlData(fieldTableName, fieldName, lineEdit1->text());
  1931. //updateDb(fieldTableName, fieldId, lineEdit1->text());
  1932. loginput(fieldTableName, fieldName, lineEdit1->text());
  1933. });
  1934. }
  1935. }
  1936. else if (fieldType == "radio")
  1937. {
  1938. // 用逗号分隔的选项
  1939. QStringList optionList = fieldUnits.split(QRegExp("[,,]"), Qt::SkipEmptyParts);
  1940. QHBoxLayout* radioLayout = new QHBoxLayout;
  1941. radioLayout->setSpacing(5);
  1942. QButtonGroup* radioGroup = new QButtonGroup(rightWidget);
  1943. for (const QString& optionText : optionList)
  1944. {
  1945. QString trimmedOpt = optionText.trimmed();
  1946. if (trimmedOpt.isEmpty()) continue;
  1947. QRadioButton* radioButton = new QRadioButton(trimmedOpt);
  1948. radioGroup->addButton(radioButton);
  1949. radioLayout->addWidget(radioButton);
  1950. m_fieldWidgets.append(radioButton);
  1951. if (fieldValue == trimmedOpt)
  1952. {
  1953. radioButton->setChecked(true);
  1954. }
  1955. }
  1956. // 使用Lambda函数连接 buttonToggled 信号
  1957. connect(radioGroup, static_cast<void (QButtonGroup::*)(QAbstractButton*, bool)>(&QButtonGroup::buttonToggled), [this, fieldTableName, fieldName](QAbstractButton* button, bool checked) {
  1958. if (checked)
  1959. {
  1960. // qDebug() << "Button toggled:" << button->text();
  1961. // 在这里添加你想要执行的操作
  1962. m_sqlOper->updateControlData(fieldTableName, fieldName, button->text());
  1963. //updateDb(fieldTableName, fieldId, button->text());
  1964. }
  1965. });
  1966. rightLayout->addLayout(radioLayout);
  1967. }
  1968. else if (fieldType == "checkbox" || fieldType == "BOOL")
  1969. {
  1970. bool isChecked;
  1971. if (fieldValue == "")
  1972. {
  1973. isChecked = (fieldValue == "1");
  1974. } else
  1975. {
  1976. isChecked = (fieldValue == "1");
  1977. }
  1978. QCheckBox* checkBox = new QCheckBox;
  1979. checkBox->setChecked(isChecked);
  1980. checkBox->setStyleSheet(R"(
  1981. QCheckBox::indicator {
  1982. width: 20px;
  1983. height: 20px;
  1984. }
  1985. QCheckBox::indicator:unchecked {
  1986. background-color: #FFFFFF;
  1987. border: 1px solid #BABBDC;
  1988. border-radius: 2px;
  1989. }
  1990. QCheckBox::indicator:checked {
  1991. image: url(:/images/three_Selecte.png);
  1992. }
  1993. QCheckBox {
  1994. spacing: 0px;
  1995. }
  1996. )");
  1997. rightLayout->addWidget(checkBox);
  1998. createdWidget = checkBox;
  1999. connect(checkBox, &QCheckBox::stateChanged, [this, fieldTableName, fieldName](int state) {
  2000. if (state == Qt::Checked)
  2001. {
  2002. // qDebug() << "CheckBox is checked"<<fieldId;
  2003. m_sqlOper->updateControlData(fieldTableName, fieldName, "1");
  2004. //updateDb(fieldTableName, fieldId, "1");
  2005. } else if (state == Qt::Unchecked)
  2006. {
  2007. // qDebug() << "CheckBox is unchecked"<<fieldId;
  2008. // 在这里添加你想要执行的操作
  2009. m_sqlOper->updateControlData(fieldTableName, fieldName, "0");
  2010. //updateDb(fieldTableName, fieldId, "0");
  2011. } else if (state == Qt::PartiallyChecked)
  2012. {
  2013. qDebug() << "CheckBox is partially checked";
  2014. // 在这里添加你想要执行的操作
  2015. }
  2016. });
  2017. }
  2018. else if (fieldType == "ComboBox")
  2019. {
  2020. QStringList optionList = fieldValue.split(QRegExp("[,,]"), Qt::SkipEmptyParts);
  2021. QComboBox* comboBox = new QComboBox;
  2022. comboBox->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
  2023. comboBox->setFixedSize(140, 28);
  2024. comboBox->setStyleSheet(R"(
  2025. QComboBox {
  2026. background: #FFFFFF;
  2027. border: 1px solid #BABBDC;
  2028. border-radius: 6px;
  2029. padding: 2px 5px;
  2030. }
  2031. QComboBox::drop-down {
  2032. width: 20px;
  2033. }
  2034. )");
  2035. // int newIndex = -1;
  2036. for (const QString& opt : optionList)
  2037. {
  2038. QString trimmedOpt = opt.trimmed();
  2039. if (!trimmedOpt.isEmpty())
  2040. comboBox->addItem(trimmedOpt);
  2041. }
  2042. if (fieldValue == "")
  2043. {
  2044. comboBox->setCurrentIndex(0);
  2045. } else
  2046. {
  2047. comboBox->setCurrentIndex(fieldValue.toInt());
  2048. }
  2049. rightLayout->addWidget(comboBox);
  2050. createdWidget = comboBox;
  2051. // 使用Lambda函数连接 currentIndexChanged 信号
  2052. connect(comboBox, QOverload<int>::of(&QComboBox::currentIndexChanged), [this, comboBox, fieldTableName, fieldName](int index) {
  2053. QString selectedText = comboBox->itemText(index);
  2054. QString indexString = QString::number(index);
  2055. // qDebug() << "ComboBox index changed to:" << index << "Text:" << selectedText;
  2056. m_sqlOper->updateControlData(fieldTableName, fieldName, indexString);
  2057. //updateDb(fieldTableName, fieldId, indexString);
  2058. // 在这里添加你想要执行的操作
  2059. });
  2060. } else if (fieldType == "time")
  2061. {
  2062. QTimeEdit* timeEdit = new QTimeEdit;
  2063. timeEdit->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
  2064. timeEdit->setFixedSize(120, 28);
  2065. timeEdit->setStyleSheet(R"(
  2066. QTimeEdit {
  2067. background: #FFFFFF;
  2068. border: 1px solid #BABBDC;
  2069. border-radius: 6px;
  2070. padding: 2px 5px;
  2071. }
  2072. )");
  2073. timeEdit->setDisplayFormat("HH:mm:ss");
  2074. timeEdit->setTime(QTime::fromString(fieldValue, "HH:mm:ss"));
  2075. rightLayout->addWidget(timeEdit);
  2076. createdWidget = timeEdit;
  2077. } else if (fieldType == "switch")
  2078. {
  2079. QWidget* switchContainer = new QWidget;
  2080. QHBoxLayout* switchLayout = new QHBoxLayout(switchContainer);
  2081. switchLayout->setSpacing(5);
  2082. switchLayout->setContentsMargins(0, 0, 0, 0);
  2083. QCheckBox* switchBox = new QCheckBox;
  2084. switchBox->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
  2085. switchBox->setFixedSize(30, 30);
  2086. switchBox->setStyleSheet(R"(
  2087. QCheckBox::indicator {
  2088. width: 30px;
  2089. height: 30px;
  2090. }
  2091. QCheckBox::indicator:unchecked {
  2092. background-color: #BABBDC;
  2093. border-radius: 6px;
  2094. }
  2095. QCheckBox::indicator:checked {
  2096. background-color: #4CAF50;
  2097. border-radius: 6px;
  2098. }
  2099. )");
  2100. QString switchValue;
  2101. if (fieldValue == "")
  2102. {
  2103. switchValue = fieldValue.toLower();
  2104. } else
  2105. {
  2106. switchValue = fieldValue.toLower();
  2107. }
  2108. if (switchValue == "on")
  2109. switchBox->setChecked(true);
  2110. else
  2111. switchBox->setChecked(false);
  2112. QLabel* switchLabel = new QLabel(switchBox->isChecked() ? tr("开") :tr("关"));
  2113. switchLabel->setAlignment(Qt::AlignVCenter | Qt::AlignLeft);
  2114. switchLabel->setStyleSheet("font-size: 14px;");
  2115. connect(switchBox, &QCheckBox::stateChanged, [fieldTableName, fieldName, switchLabel, this](int state) {
  2116. if (state == Qt::Checked)
  2117. {
  2118. switchLabel->setText(tr("开"));
  2119. m_sqlOper->updateControlData(fieldTableName, fieldName, "on");
  2120. //updateDb(fieldTableName, fieldId, "on");
  2121. } else
  2122. {
  2123. switchLabel->setText(tr("关"));
  2124. m_sqlOper->updateControlData(fieldTableName, fieldName, "off");
  2125. //updateDb(fieldTableName, fieldId, "off");
  2126. }
  2127. });
  2128. switchLayout->addWidget(switchBox);
  2129. switchLayout->addWidget(switchLabel);
  2130. rightLayout->addWidget(switchContainer);
  2131. createdWidget = switchBox;
  2132. }
  2133. else if (fieldType == "combined" || fieldType == "POSITION")
  2134. {
  2135. // 用逗号分隔
  2136. QStringList parts = fieldValue.split(QRegExp("[,,]"), Qt::SkipEmptyParts);
  2137. QString inputText = (parts.size() >= 1 ? parts.at(0).trimmed() : QString());
  2138. QString button1Text = (parts.size() >= 2 ? parts.at(1).trimmed() : QStringLiteral("获取"));
  2139. QString button2Text = (parts.size() >= 3 ? parts.at(2).trimmed() : QStringLiteral("设置"));
  2140. QLineEdit* comboInput = new QLineEdit(inputText);
  2141. QRegularExpression regExp("^-?\\d*\\.?\\d*"); // 匹配整数/浮点数(含负数)
  2142. QRegularExpressionValidator* validator = new QRegularExpressionValidator(regExp, this);
  2143. comboInput->setValidator(validator);
  2144. QString strID = fieldTableName;
  2145. strID += "_";
  2146. strID += fieldName;
  2147. strID += +"_";
  2148. strID += fieldDescribe;
  2149. comboInput->setProperty("Input", strID);
  2150. comboInput->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
  2151. comboInput->setFixedHeight(28);
  2152. comboInput->setStyleSheet(R"(
  2153. QLineEdit {
  2154. background: #FFFFFF;
  2155. border: 1px solid #BABBDC;
  2156. border-radius: 5px;
  2157. padding: 2px 5px;
  2158. }
  2159. )");
  2160. m_fieldWidgets.append(comboInput);
  2161. QWidget* combinedWidget = new QWidget;
  2162. QHBoxLayout* combinedLayout = new QHBoxLayout(combinedWidget);
  2163. combinedLayout->setSpacing(5);
  2164. combinedLayout->setContentsMargins(0, 0, 0, 0);
  2165. QPushButton* btnGet = new QPushButton(button1Text, combinedWidget);
  2166. QString strIDGet = fieldTableName;
  2167. strIDGet += "_";
  2168. strIDGet += fieldName;
  2169. strIDGet += +"_";
  2170. strIDGet += fieldDescribe;
  2171. btnGet->setProperty("Get", strIDGet);
  2172. btnGet->setFixedSize(80, 28);
  2173. btnGet->setStyleSheet(R"(
  2174. QPushButton {
  2175. background: #FFFFFF;
  2176. border: 1px solid #BABBDC;
  2177. border-radius: 5px;
  2178. }
  2179. QPushButton:hover {
  2180. background-color: #F0F0F0;
  2181. }
  2182. )");
  2183. connect(btnGet, &QPushButton::clicked, this, [this, comboInput, strstrModuleType, btnGet, strAxisType,fieldTableName, fieldName]()
  2184. {
  2185. double strNum = comboInput->text().toDouble();
  2186. //qDebug() << "AAA:" << strstrModuleType << strAxisType <<" " << fieldTableName << fieldName;
  2187. //
  2188. if (m_pTreeCViewInterface)
  2189. {
  2190. m_pTreeCViewInterface->GetAxisPosition(strstrModuleType.toStdString(),
  2191. strAxisType.toStdString(), strNum);
  2192. }
  2193. comboInput->text() = strNum;
  2194. //QPushButton* senderBtn = qobject_cast<QPushButton*>(sender());
  2195. //if (senderBtn) {
  2196. // QString strOnclick = senderBtn->property("Get").toString();
  2197. // qDebug() << "Clicked ID:" << strOnclick;
  2198. //}
  2199. // JMessageTip::Message_question(m_veCombinedCont[0].pLineC->text());
  2200. });
  2201. m_fieldWidgets.append(btnGet);
  2202. QPushButton* btnSet = new QPushButton(button2Text, combinedWidget);
  2203. QString strIDSet = fieldTableName;
  2204. strIDSet += "_";
  2205. strIDSet += fieldName;
  2206. strIDSet += +"_";
  2207. strIDSet += fieldDescribe;
  2208. btnSet->setProperty("set", strIDSet);
  2209. btnSet->setFixedSize(80, 28);
  2210. btnSet->setStyleSheet(R"(
  2211. QPushButton {
  2212. background: #FFFFFF;
  2213. border: 1px solid #BABBDC;
  2214. border-radius: 5px;
  2215. }
  2216. QPushButton:hover {
  2217. background-color: #F0F0F0;
  2218. }
  2219. )");
  2220. connect(btnSet, &QPushButton::clicked, this, [this, comboInput, strstrModuleType, btnGet, strAxisType, fieldTableName, fieldName]()
  2221. {
  2222. QPushButton* senderBtn = qobject_cast<QPushButton*>(sender());
  2223. if (senderBtn) {
  2224. QString strOnclick = senderBtn->property("set").toString();
  2225. qDebug() << "btnSet ID:" << strOnclick;
  2226. }
  2227. double strNum = comboInput->text().toDouble();
  2228. if (m_pTreeCViewInterface)
  2229. {
  2230. m_pTreeCViewInterface->ModuleMoveTo(strstrModuleType.toStdString(),
  2231. strAxisType.toStdString(), strNum);
  2232. }
  2233. //JMessageTip::Message_question(m_veCombinedCont[0].pLineC->text());
  2234. });
  2235. m_fieldWidgets.append(btnSet);
  2236. combinedLayout->addWidget(comboInput);
  2237. combinedLayout->addWidget(btnGet);
  2238. combinedLayout->addWidget(btnSet);
  2239. combinedLayout->addStretch();
  2240. rightLayout->addWidget(combinedWidget);
  2241. // 压入内存
  2242. ST_COMBINED_CONT _a;
  2243. _a.pGetBut = btnGet;
  2244. _a.pSetBut = btnSet;
  2245. _a.pLineC = comboInput;
  2246. _a.strConfigurationsName = fieldTableName;
  2247. _a.strfieldName = fieldName;
  2248. _a.strfieldDescribe = fieldDescribe;
  2249. m_veCombinedCont.push_back(_a);
  2250. } else if (fieldType == "Text")
  2251. {
  2252. QLabel* label = new QLabel();
  2253. label->setText(fieldValue);
  2254. label->setAlignment(Qt::AlignVCenter | Qt::AlignLeft);
  2255. label->setFixedHeight(24);
  2256. label->setMinimumWidth(120);
  2257. rightLayout->addWidget(label);
  2258. } else
  2259. {
  2260. qWarning() << "未知字段类型:" << fieldType;
  2261. }
  2262. if (createdWidget)
  2263. m_fieldWidgets.append(createdWidget);
  2264. fieldLayout->addWidget(rightWidget);
  2265. scrollLayout->addLayout(fieldLayout);
  2266. }
  2267. }
  2268. }
  2269. scrollLayout->addStretch();
  2270. scrollArea->setWidget(scrollWidget);
  2271. QVBoxLayout* mainLayout = new QVBoxLayout(m_thirdLevelFieldWnd);
  2272. mainLayout->setContentsMargins(0, 0, 0, 0);
  2273. mainLayout->addWidget(scrollArea);
  2274. m_thirdLevelFieldWnd->show();
  2275. // 显示按钮
  2276. displayThirdLevelButtons(buttonControls);
  2277. //loadButtonConfigForThirdLevel(data);
  2278. }
  2279. void DbTreeViewManager::updateParentCheckState(QStandardItem *item)
  2280. {
  2281. if (!item)
  2282. return;
  2283. QStandardItem *parentItem = item->parent();
  2284. if (!parentItem)
  2285. return;
  2286. bool allChecked = true;
  2287. bool noneChecked = true;
  2288. for (int i = 0; i < parentItem->rowCount(); ++i) {
  2289. QStandardItem *child = parentItem->child(i);
  2290. if (child->checkState() != Qt::Checked) {
  2291. allChecked = false;
  2292. }
  2293. if (child->checkState() == Qt::Checked) {
  2294. noneChecked = false;
  2295. }
  2296. }
  2297. if (allChecked) {
  2298. parentItem->setCheckState(Qt::Checked);
  2299. } else {
  2300. parentItem->setCheckState(Qt::Unchecked);
  2301. }
  2302. if (noneChecked) {
  2303. qDebug() << "noneChecked状态为:" << noneChecked;
  2304. }
  2305. // 递归更新上一级
  2306. updateParentCheckState(parentItem);
  2307. }
  2308. void DbTreeViewManager::onButtonBackClicked()
  2309. {
  2310. if (m_thirdLevelFieldWnd) {
  2311. // 当前在第三层目录,返回到上一层目录
  2312. qDebug() << "当前在第三层目录,准备返回上一层。";
  2313. for(int i=0;i<5;i++){
  2314. if(currentMenuId == menuArray[i].menuId){
  2315. menuArray[i].isthird = false;
  2316. }
  2317. }
  2318. // 关闭并销毁字段展示窗口
  2319. m_thirdLevelFieldWnd->close();
  2320. m_thirdLevelFieldWnd->deleteLater();
  2321. m_thirdLevelFieldWnd = nullptr;
  2322. // 显示目录树和分隔线
  2323. m_pTreeViewDown->show();
  2324. for (auto separator : firstLevelSeparators) {
  2325. if (separator)
  2326. separator->show();
  2327. }
  2328. // 获取当前导航路径
  2329. QStringList path;
  2330. QLayout* layout = m_pNavigationWidget->layout();
  2331. if (!layout) {
  2332. qWarning() << "导航栏没有布局,无法获取路径。";
  2333. return;
  2334. }
  2335. for (int i = 0; i < layout->count(); ++i) {
  2336. QLayoutItem* item = layout->itemAt(i);
  2337. if (item && item->widget()) {
  2338. QLabel* label = qobject_cast<QLabel*>(item->widget());
  2339. if (label) {
  2340. QString text = label->text().trimmed();
  2341. if (!text.isEmpty()) {
  2342. path << text;
  2343. }
  2344. }
  2345. }
  2346. }
  2347. if (path.isEmpty()) {
  2348. qWarning() << "导航路径为空,无法返回上一层。";
  2349. return;
  2350. }
  2351. // 移除最后一个目录名称以返回上一层
  2352. path.removeLast();
  2353. if (path.isEmpty()) {
  2354. qDebug() << "已经在根目录,无法返回。";
  2355. return;
  2356. }
  2357. // 根据新的路径查找对应的 QModelIndex
  2358. QModelIndex parentIndex = findItemByPath(path);
  2359. if (!parentIndex.isValid()) {
  2360. qWarning() << "无法找到路径对应的目录项:" << path;
  2361. return;
  2362. }
  2363. // 选中父目录项
  2364. m_pTreeViewDown->setCurrentIndex(parentIndex);
  2365. m_pTreeViewDown->scrollTo(parentIndex);
  2366. // 更新导航栏
  2367. updateNavigationBar(parentIndex);
  2368. // 检查父目录是否为第三层目录
  2369. QStandardItem* parentItem = m_pCModel->itemFromIndex(parentIndex);
  2370. if (!parentItem) {
  2371. qWarning() << "父目录项无效。";
  2372. return;
  2373. }
  2374. QVariant dataVar = parentItem->data(Qt::UserRole + 2);
  2375. bool isThirdLevel = false;
  2376. int directoryId = -1;
  2377. QString multiTableName = "";
  2378. if (dataVar.canConvert<QJsonObject>()) {
  2379. QJsonObject dataObj = dataVar.toJsonObject();
  2380. if (dataObj.contains("IsThirdLevel") && dataObj["IsThirdLevel"].toBool()) {
  2381. isThirdLevel = true;
  2382. if (dataObj.contains("Id")) {
  2383. directoryId = dataObj["Id"].toInt();
  2384. }
  2385. if (dataObj.contains("TableName"))
  2386. {
  2387. multiTableName = dataObj["TableName"].toString();
  2388. }
  2389. }
  2390. }
  2391. if (isThirdLevel) {
  2392. if (directoryId == -1) {
  2393. qWarning() << "无效的 DirectoryId,无法加载第三层目录内容。";
  2394. return;
  2395. }
  2396. int userPrivilege = 0x1; // 当前用户的权限等级
  2397. QList<Table_Control_Data> tableControlDatas;
  2398. bool allChangeFlag = false;
  2399. m_sqlOper->GetThirdDirControlData(multiTableName, userPrivilege, tableControlDatas, allChangeFlag);
  2400. displayThirdLevelFields(tableControlDatas, allChangeFlag, false); // 传递参数表示 Home 界面
  2401. // 获取第三层目录的数据
  2402. //m_sqlOper->GetThirdDirControlData();
  2403. //QJsonObject data = fetchThirdLevelData(directoryId, userPrivilege);
  2404. // 显示字段展示窗口
  2405. //displayThirdLevelFields(data, false); // 传递参数表示非 Home 界面
  2406. } else {
  2407. // 父目录不是第三层目录,确保目录树和分隔线可见
  2408. m_pTreeViewDown->show();
  2409. for (auto separator : firstLevelSeparators) {
  2410. if (separator)
  2411. separator->show();
  2412. }
  2413. }
  2414. return;
  2415. }
  2416. // 如果不在第三层目录,则在目录树中返回上一层
  2417. qDebug() << "当前不在第三层目录,尝试在目录树中返回上一层。";
  2418. QModelIndex currentIndex = m_pTreeViewDown->currentIndex();
  2419. if (!currentIndex.isValid()) {
  2420. qWarning() << "当前没有选中的目录项,无法返回。";
  2421. return;
  2422. }
  2423. QStandardItem* currentItem = m_pCModel->itemFromIndex(currentIndex);
  2424. if (!currentItem) {
  2425. qWarning() << "当前选中的目录项无效。";
  2426. return;
  2427. }
  2428. QStandardItem* parentItem = currentItem->parent();
  2429. if (!parentItem) {
  2430. qDebug() << "当前已经在根目录,无法返回。";
  2431. return;
  2432. }
  2433. QModelIndex parentIndex = m_pCModel->indexFromItem(parentItem);
  2434. if (!parentIndex.isValid()) {
  2435. qWarning() << "父目录的 QModelIndex 无效。";
  2436. return;
  2437. }
  2438. // 选中父目录项
  2439. m_pTreeViewDown->setCurrentIndex(parentIndex);
  2440. m_pTreeViewDown->scrollTo(parentIndex);
  2441. // 更新导航栏
  2442. updateNavigationBar(parentIndex);
  2443. // 检查父目录是否为第三层目录
  2444. QVariant dataVar = parentItem->data(Qt::UserRole + 2);
  2445. bool isThirdLevel = false;
  2446. int directoryId = -1;
  2447. QString multiTableName = "";
  2448. if (dataVar.canConvert<QJsonObject>()) {
  2449. QJsonObject dataObj = dataVar.toJsonObject();
  2450. if (dataObj.contains("IsThirdLevel") && dataObj["IsThirdLevel"].toBool()) {
  2451. isThirdLevel = true;
  2452. if (dataObj.contains("Id")) {
  2453. directoryId = dataObj["Id"].toInt();
  2454. }
  2455. if (dataObj.contains("TableName"))
  2456. {
  2457. multiTableName = dataObj["TableName"].toString();
  2458. }
  2459. }
  2460. }
  2461. if (isThirdLevel) {
  2462. if (directoryId == -1) {
  2463. qWarning() << "无效的 DirectoryId,无法加载第三层目录内容。";
  2464. return;
  2465. }
  2466. int userPrivilege = 0x1; // 根据实际情况获取当前用户的权限等级
  2467. QList<Table_Control_Data> tableControlDatas;
  2468. bool allChangeFlag = false;
  2469. m_sqlOper->GetThirdDirControlData(multiTableName, userPrivilege, tableControlDatas, allChangeFlag);
  2470. displayThirdLevelFields(tableControlDatas, allChangeFlag, false); // 传递参数表示 Home 界面
  2471. //QJsonObject data = fetchThirdLevelData(directoryId, userPrivilege);
  2472. //displayThirdLevelFields(data, false); // 传递参数表示非 Home 界面
  2473. }
  2474. }
  2475. QList<QStandardItem*> DbTreeViewManager::collectAllThirdLevelItems()
  2476. {
  2477. QList<QStandardItem*> result;
  2478. // 从根节点开始递归搜集
  2479. QStandardItem *root = m_pCModel->invisibleRootItem();
  2480. if (!root) return result;
  2481. std::function<void(QStandardItem*)> dfsCollectThirdLevel;
  2482. dfsCollectThirdLevel = [&](QStandardItem* parentItem)
  2483. {
  2484. if (!parentItem) return;
  2485. int rowCount = parentItem->rowCount();
  2486. for (int i = 0; i < rowCount; ++i) {
  2487. QStandardItem *child = parentItem->child(i);
  2488. if (!child) continue;
  2489. // 判断是否是第三层
  2490. QVariant dataVar = child->data(Qt::UserRole + 2);
  2491. if (dataVar.canConvert<QJsonObject>()) {
  2492. QJsonObject obj = dataVar.toJsonObject();
  2493. if (obj.contains("IsThirdLevel") && obj["IsThirdLevel"].toBool()) {
  2494. // 收集这个第三层节点
  2495. result.append(child);
  2496. }
  2497. }
  2498. // 即使不是第三层,也要继续往下找子节点
  2499. dfsCollectThirdLevel(child);
  2500. }
  2501. };
  2502. dfsCollectThirdLevel(root);
  2503. return result;
  2504. }
  2505. void DbTreeViewManager::onButtonDownClicked()
  2506. {
  2507. // 1) 收集所有 第三层 节点
  2508. QList<QStandardItem*> thirdLevelList = collectAllThirdLevelItems();
  2509. if (thirdLevelList.isEmpty()) {
  2510. qDebug() << "[onButtonDownClicked] 没有任何三级目录,无法遍历。";
  2511. return;
  2512. }
  2513. // 2) 找到 当前选中的节点 在 thirdLevelList 里的索引
  2514. QModelIndex curIndex = m_pTreeViewDown->currentIndex();
  2515. if (!curIndex.isValid()) {
  2516. qDebug() << "[onButtonDownClicked] 当前没有选中节点,默认跳到第一个三级目录。";
  2517. // 如果想默认跳到第一个
  2518. QStandardItem *firstItem = thirdLevelList.first();
  2519. QModelIndex idx = m_pCModel->indexFromItem(firstItem);
  2520. m_pTreeViewDown->setCurrentIndex(idx);
  2521. // 模拟点击一下
  2522. onTreeViewClicked_updown(idx);
  2523. // 保存复选框状态
  2524. saveCheckedPaths();
  2525. // 保存展开路径
  2526. saveExpandedPaths();
  2527. return;
  2528. }
  2529. QStandardItem *curItem = m_pCModel->itemFromIndex(curIndex);
  2530. if (!curItem) {
  2531. qWarning() << "[onButtonDownClicked] currentItem 无效。";
  2532. return;
  2533. }
  2534. // 若不是三级目录,就先找它最近的父级是不是三级目录,否则当做没找到。
  2535. int currentPos = -1;
  2536. {
  2537. // 在 thirdLevelList 里找到 curItem
  2538. currentPos = thirdLevelList.indexOf(curItem);
  2539. if (currentPos < 0) {
  2540. qDebug() << "[onButtonDownClicked] 当前节点不是三级目录,尝试找父级是不是三级目录…";
  2541. // 向上找父节点是否在 thirdLevelList 里
  2542. QStandardItem *temp = curItem->parent();
  2543. while (temp) {
  2544. int pos = thirdLevelList.indexOf(temp);
  2545. if (pos >= 0) {
  2546. currentPos = pos;
  2547. break;
  2548. }
  2549. temp = temp->parent();
  2550. }
  2551. if (currentPos < 0) {
  2552. qDebug() << "还是找不到,说明当前目录不属于任何三级目录,直接跳到第一个";
  2553. QStandardItem *firstItem = thirdLevelList.first();
  2554. QModelIndex idx = m_pCModel->indexFromItem(firstItem);
  2555. m_pTreeViewDown->setCurrentIndex(idx);
  2556. onTreeViewClicked_updown(idx);
  2557. // 保存复选框状态
  2558. saveCheckedPaths();
  2559. // 保存展开路径
  2560. saveExpandedPaths();
  2561. return;
  2562. }
  2563. }
  2564. }
  2565. // 尝试下一个
  2566. int nextPos = currentPos + 1;
  2567. if (nextPos >= thirdLevelList.size()) {
  2568. qDebug() << "[onButtonDownClicked] 已经是最后一个三级目录了,无法再向下。";
  2569. return;
  2570. }
  2571. // 跳到下一个
  2572. QStandardItem *nextItem = thirdLevelList[nextPos];
  2573. QModelIndex nextIndex = m_pCModel->indexFromItem(nextItem);
  2574. m_pTreeViewDown->setCurrentIndex(nextIndex);
  2575. // 相当于模拟点击
  2576. onTreeViewClicked_updown(nextIndex);
  2577. // 保存复选框状态
  2578. saveCheckedPaths();
  2579. // 保存展开路径
  2580. saveExpandedPaths();
  2581. }
  2582. void DbTreeViewManager::onButtonUpClicked()
  2583. {
  2584. // 1) 收集所有 第三层 节点
  2585. QList<QStandardItem*> thirdLevelList = collectAllThirdLevelItems();
  2586. if (thirdLevelList.isEmpty()) {
  2587. qDebug() << "[onButtonUpClicked] 没有任何三级目录,无法遍历。";
  2588. return;
  2589. }
  2590. // 2) 找到 当前选中的节点
  2591. QModelIndex curIndex = m_pTreeViewDown->currentIndex();
  2592. if (!curIndex.isValid()) {
  2593. qDebug() << "[onButtonUpClicked] 当前无选中节点,默认跳到最后一个三级目录。";
  2594. // 跳到最后一个
  2595. QStandardItem *lastItem = thirdLevelList.last();
  2596. QModelIndex idx = m_pCModel->indexFromItem(lastItem);
  2597. m_pTreeViewDown->setCurrentIndex(idx);
  2598. // 模拟点击
  2599. onTreeViewClicked_updown(idx);
  2600. // 保存复选框状态
  2601. saveCheckedPaths();
  2602. // 保存展开路径
  2603. saveExpandedPaths();
  2604. return;
  2605. }
  2606. QStandardItem *curItem = m_pCModel->itemFromIndex(curIndex);
  2607. if (!curItem) {
  2608. qWarning() << "[onButtonUpClicked] currentItem 无效。";
  2609. return;
  2610. }
  2611. // 如果当前节点不是三级目录, 向上查找
  2612. int currentPos = thirdLevelList.indexOf(curItem);
  2613. if (currentPos < 0) {
  2614. qDebug() << "[onButtonUpClicked] 当前节点不是三级目录,尝试找父级是不是三级目录。";
  2615. QStandardItem *temp = curItem->parent();
  2616. while (temp) {
  2617. int pos = thirdLevelList.indexOf(temp);
  2618. if (pos >= 0) {
  2619. currentPos = pos;
  2620. break;
  2621. }
  2622. temp = temp->parent();
  2623. }
  2624. if (currentPos < 0) {
  2625. qDebug() << "找不到任何三级目录,直接跳到最后一个。";
  2626. QStandardItem *lastItem = thirdLevelList.last();
  2627. QModelIndex idx = m_pCModel->indexFromItem(lastItem);
  2628. m_pTreeViewDown->setCurrentIndex(idx);
  2629. onTreeViewClicked_updown(idx);
  2630. // 保存复选框状态
  2631. saveCheckedPaths();
  2632. // 保存展开路径
  2633. saveExpandedPaths();
  2634. return;
  2635. }
  2636. }
  2637. // 尝试上一个
  2638. int prevPos = currentPos - 1;
  2639. if (prevPos < 0) {
  2640. qDebug() << "[onButtonUpClicked] 已经是第一个三级目录了,无法再向上。";
  2641. return;
  2642. }
  2643. // 跳到上一个
  2644. QStandardItem *prevItem = thirdLevelList[prevPos];
  2645. QModelIndex prevIndex = m_pCModel->indexFromItem(prevItem);
  2646. m_pTreeViewDown->setCurrentIndex(prevIndex);
  2647. // 模拟点击
  2648. onTreeViewClicked_updown(prevIndex);
  2649. // 保存复选框状态
  2650. saveCheckedPaths();
  2651. // 保存展开路径
  2652. saveExpandedPaths();
  2653. }
  2654. void DbTreeViewManager::onButtonRightClicked()
  2655. {
  2656. if (m_forwardStack.isEmpty()) {
  2657. qDebug() << "[Forward] 已经没有可以前进的页面";
  2658. buttonRight->setIcon(QIcon(":/images/home_right.png"));
  2659. return;
  2660. }
  2661. // 1. 弹出 forwardStack 的最后一个
  2662. PageState st = m_forwardStack.takeLast();
  2663. // 2. 把当前页面 push 到 backStack
  2664. PageState cur = getCurrentPageState();
  2665. m_backStack.append(cur);
  2666. if(m_backStack.size() >= 2){
  2667. buttonLeft->setIcon(QIcon(":/images/home_left.png"));
  2668. }
  2669. // 3. 加载 st
  2670. loadPageState(st, true);
  2671. }
  2672. void DbTreeViewManager::onButtonLeftClicked()
  2673. {
  2674. if (m_backStack.size() < 2) {
  2675. qDebug() << "[Back] 已经无法再后退";
  2676. buttonLeft->setIcon(QIcon(":/images/home_left_hide.png"));
  2677. return;
  2678. }
  2679. // 1. 先把当前页面(相当于 m_backStack 最后一个)弹出,放到 forwardStack
  2680. PageState cur = m_backStack.takeLast();
  2681. if(m_backStack.size() >= 2){
  2682. buttonLeft->setIcon(QIcon(":/images/home_left.png"));
  2683. }else{
  2684. buttonLeft->setIcon(QIcon(":/images/home_left_hide.png"));
  2685. }
  2686. m_forwardStack.append(cur);
  2687. buttonRight->setIcon(QIcon(":/images/home_right_show.png"));
  2688. // 2. 拿到新的 m_backStack 末尾(即上一次看的页面)
  2689. const PageState &prev = m_backStack.last();
  2690. // 3. 加载 prev
  2691. loadPageState(prev, true );
  2692. }
  2693. // 构建节点的完整路径
  2694. QStringList DbTreeViewManager::buildItemPath(QStandardItem *item)
  2695. {
  2696. QStringList path;
  2697. QStandardItem *currentItem = item;
  2698. while (currentItem) {
  2699. path.prepend(currentItem->text());
  2700. currentItem = currentItem->parent();
  2701. }
  2702. return path;
  2703. }
  2704. // 根据路径查找对应的节点
  2705. QModelIndex DbTreeViewManager::findItemByPath(const QStringList &path)
  2706. {
  2707. if (path.isEmpty()) return QModelIndex();
  2708. QStandardItem *currentItem = m_pCModel->invisibleRootItem();
  2709. QModelIndex currentIndex;
  2710. for (const QString &part : path) {
  2711. bool found = false;
  2712. for (int i = 0; i < currentItem->rowCount(); ++i) {
  2713. QStandardItem *child = currentItem->child(i);
  2714. if (child == nullptr)
  2715. {
  2716. qDebug() << "child is nullptr";
  2717. }
  2718. if (child->text() == part) {
  2719. currentIndex = m_pCModel->indexFromItem(child);
  2720. currentItem = child;
  2721. found = true;
  2722. qDebug() << "找到路径部分:" << part;
  2723. break;
  2724. }
  2725. }
  2726. if (!found) {
  2727. qWarning() << "路径部分未找到:" << part;
  2728. return QModelIndex();
  2729. }
  2730. }
  2731. return currentIndex;
  2732. }
  2733. void DbTreeViewManager::setCheckedPaths(const QStringList &checkedPathsList)
  2734. {
  2735. m_blockItemChanged = true;
  2736. for (const QString &pathStr : checkedPathsList) {
  2737. QStringList path = pathStr.split("/");
  2738. QModelIndex idx = findItemByPath(path);
  2739. if (idx.isValid()) {
  2740. QStandardItem *item = m_pCModel->itemFromIndex(idx);
  2741. if (item) {
  2742. item->setCheckState(Qt::Checked);
  2743. qDebug() << " Setting item:" << item->text() << "to Checked";
  2744. }
  2745. } else {
  2746. qDebug() << "Path not found:" << pathStr;
  2747. }
  2748. }
  2749. m_blockItemChanged = false;
  2750. }
  2751. // 递归收集被选中的路径
  2752. QStringList DbTreeViewManager::collectCheckedPathsRecursive(QStandardItem *item, QStringList path)
  2753. {
  2754. QStringList checkedList;
  2755. if (!item) {
  2756. item = m_pCModel->invisibleRootItem();
  2757. }
  2758. for (int i = 0; i < item->rowCount(); ++i) {
  2759. QStandardItem *child = item->child(i);
  2760. if (child) {
  2761. QStringList currentPath = path;
  2762. currentPath << child->text();
  2763. if (child->checkState() == Qt::Checked) {
  2764. checkedList << currentPath.join("/");
  2765. }
  2766. // 递归收集子项
  2767. checkedList << collectCheckedPathsRecursive(child, currentPath);
  2768. }
  2769. }
  2770. return checkedList;
  2771. }
  2772. // 收集所有被选中的复选框路径
  2773. QStringList DbTreeViewManager::collectCheckedPaths()
  2774. {
  2775. return collectCheckedPathsRecursive(m_pCModel->invisibleRootItem(), QStringList());
  2776. }
  2777. // 更新父项的复选框状态
  2778. void DbTreeViewManager::updateParentItems(QStandardItem *parentItem)
  2779. {
  2780. if (!parentItem)
  2781. return;
  2782. int checkedCount = 0;
  2783. int totalCount = parentItem->rowCount();
  2784. qDebug() << "Updating child items of:" << parentItem->text();
  2785. for (int i = 0; i < totalCount; ++i)
  2786. {
  2787. QStandardItem *child = parentItem->child(i);
  2788. if (child && child->checkState() == Qt::Checked)
  2789. {
  2790. checkedCount++;
  2791. }
  2792. }
  2793. if (checkedCount == totalCount)
  2794. {
  2795. parentItem->setCheckState(Qt::Checked);
  2796. }
  2797. else
  2798. {
  2799. parentItem->setCheckState(Qt::Unchecked);
  2800. }
  2801. // 递归更新上层父项
  2802. updateParentItems(parentItem->parent());
  2803. }
  2804. // 加载并显示三级目录对应按钮配置信息
  2805. //void DbTreeViewManager::loadButtonConfigForThirdLevel(const QJsonObject &thirdLevelObj)
  2806. //{
  2807. // if (!m_originalWnd) {
  2808. // qWarning() << "OriginalWnd 指针为空,无法加载按钮配置";
  2809. // return;
  2810. // }
  2811. //
  2812. // if (!thirdLevelObj.contains("buttons")) {
  2813. // qWarning() << "三级目录配置中不包含 'buttons' 字段";
  2814. // return;
  2815. // }
  2816. //
  2817. // QJsonArray buttonsArray = thirdLevelObj.value("buttons").toArray();
  2818. //
  2819. // // 获取 widget_left
  2820. // QWidget* widgetLeft = m_originalWnd->getWidgetLeft();
  2821. // if (!widgetLeft) {
  2822. // qWarning() << "无法访问 widget_left";
  2823. // return;
  2824. // }
  2825. //
  2826. // // 清空 widget_left 中由 loadButtonConfigForThirdLevel 创建的按钮
  2827. // QList<QPushButton*> existingButtons = widgetLeft->findChildren<QPushButton*>();
  2828. // for (QPushButton* button : existingButtons) {
  2829. // if (button->objectName().startsWith("thirdLevelBtn_")) { // 仅删除特定按钮
  2830. // button->deleteLater();
  2831. // }
  2832. // }
  2833. //
  2834. // // 使用绝对定位创建按钮
  2835. // for (int i = 0; i < buttonsArray.size() && i < 12; ++i) {
  2836. // QJsonObject buttonObj = buttonsArray[i].toObject();
  2837. // QString buttonId = buttonObj.value("id").toString();
  2838. // QString buttonIcon = buttonObj.value("icon").toString();
  2839. // QString buttonText = buttonObj.value("text").toString();
  2840. // bool isEnabled = buttonObj.value("enabled").toBool();
  2841. //
  2842. // // 创建按钮
  2843. // QPushButton *button = new QPushButton(widgetLeft);
  2844. // button->setObjectName("thirdLevelBtn_" + buttonId); // 设置带前缀的对象名称
  2845. // connect(button, &QPushButton::clicked, this, []() {
  2846. // JMessageTip::Message_question("Q111");
  2847. // });
  2848. //
  2849. // // 设置按钮的样式,调整图标和文本的位置
  2850. // button->setStyleSheet(R"(
  2851. // QPushButton {
  2852. // position: absolute;
  2853. // border-radius: 6px;
  2854. // opacity: 1;
  2855. // background: #CBD0FF;
  2856. // border: none;
  2857. // }
  2858. // QPushButton:hover {
  2859. // background-color: #A9B4FF; /* 鼠标悬停效果 */
  2860. // }
  2861. // )");
  2862. //
  2863. // // 设置按钮的位置和大小
  2864. // int x = 16;
  2865. // int y = 245 + i * (48 + 13); // 第一个按钮 y=245,后续每个按钮间隔13px
  2866. // button->setGeometry(x, y, 158, 48);
  2867. //
  2868. // // 设置按钮的可见性,根据 "enabled" 字段显示或隐藏按钮
  2869. // button->setVisible(isEnabled);
  2870. //
  2871. // // 创建图标标签
  2872. // QLabel *iconLabel = new QLabel(button);
  2873. // iconLabel->setPixmap(QIcon(buttonIcon).pixmap(16, 16));
  2874. // iconLabel->setGeometry(10, 16, 16, 16); // 图标距离左边10px,顶部16px
  2875. // iconLabel->setFixedSize(16, 16);
  2876. // iconLabel->setStyleSheet("background-color: transparent;");
  2877. // iconLabel->setVisible(isEnabled); // 根据按钮的可见性设置图标的可见性
  2878. //
  2879. // // 创建文本标签
  2880. // QLabel *textLabel = new QLabel(buttonText, button);
  2881. // textLabel->setGeometry(34, 0, 90, 48); // 文本距离左边34px
  2882. // textLabel->setWordWrap(true); // 允许换行
  2883. // textLabel->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
  2884. // textLabel->setStyleSheet(R"(
  2885. // QLabel {
  2886. // background: transparent;
  2887. // font-family: "思源黑体";
  2888. // font-size: 14px;
  2889. // font-weight: 500;
  2890. // color: #4E51CE;
  2891. // }
  2892. // )");
  2893. //
  2894. // textLabel->setVisible(isEnabled); // 根据按钮的可见性设置文本的可见性
  2895. //
  2896. // // 创建 F1-F12 标签
  2897. // QString fLabelText = QString("F%1").arg(i + 1); // F1, F2, ..., F12
  2898. // QLabel *fLabel = new QLabel(fLabelText, button);
  2899. // fLabel->setFixedSize(21, 16); // 设置大小为21x16
  2900. // fLabel->setAlignment(Qt::AlignCenter);
  2901. // fLabel->setStyleSheet(R"(
  2902. // QLabel {
  2903. // background-color: transparent;
  2904. // color: #2A7ED8;
  2905. // font-size: 12px;
  2906. // font-weight: bold;
  2907. // }
  2908. // )");
  2909. //
  2910. // // 设置标签的位置
  2911. // int fX = 134;
  2912. // int fY = 2;
  2913. // fLabel->setGeometry(fX, fY, 14, 16);
  2914. // fLabel->setVisible(isEnabled); // 根据按钮的可见性设置标签的可见性
  2915. //
  2916. // // 将按钮、图标、文本和标签添加到控件列表
  2917. // m_fieldWidgets.append(button);
  2918. // m_fieldWidgets.append(iconLabel);
  2919. // m_fieldWidgets.append(textLabel);
  2920. // m_fieldWidgets.append(fLabel);
  2921. // }
  2922. //}
  2923. void DbTreeViewManager::displayThirdLevelButtons(const QList<CONFIG_BASE_STRUCT>& buttons)
  2924. {
  2925. if (!m_originalWnd)
  2926. {
  2927. qWarning() << "OriginalWnd 指针为空,无法加载按钮配置";
  2928. return;
  2929. }
  2930. // 获取 widget_left
  2931. QWidget* widgetLeft = m_originalWnd->getWidgetLeft();
  2932. if (!widgetLeft)
  2933. {
  2934. qWarning() << "无法访问 widget_left";
  2935. return;
  2936. }
  2937. // 清空 widget_left 中由 loadButtonConfigForThirdLevel 创建的按钮
  2938. QList<QPushButton*> existingButtons = widgetLeft->findChildren<QPushButton*>();
  2939. for (QPushButton* button : existingButtons)
  2940. {
  2941. QString objName = button->objectName();
  2942. if (button->objectName().startsWith("thirdLevelBtn_F"))
  2943. { // 仅删除特定按钮
  2944. button->deleteLater();
  2945. }
  2946. }
  2947. // 使用绝对定位创建按钮
  2948. for (int i = 0; i < buttons.size() && i < 12; ++i)
  2949. {
  2950. int buttonId = buttons[i].iGroupId;
  2951. QString buttonIcon = QString::fromStdString(buttons[i].sIcon);
  2952. QString buttonText = QString::fromStdString(buttons[i].strName);
  2953. QString strCallFun = QString::fromStdString(buttons[i].strCallFun);
  2954. bool isEnabled = buttons[i].bEnable;
  2955. //for (int i = 0; i < buttonsArray.size() && i < 12; ++i) {
  2956. //QJsonObject buttonObj = buttonsArray[i].toObject();
  2957. //QString buttonId = buttonObj.value("id").toString();
  2958. //QString strNumID = buttonId; // 不改变原来的意思
  2959. //strNumID.remove('F');
  2960. //QString strDirectoryId = buttonObj.value("DirectoryId").toString();
  2961. //QString buttonIcon = buttonObj.value("icon").toString();
  2962. //QString buttonText = buttonObj.value("text").toString();
  2963. //QString strCallFun = buttonObj.value("CallFun").toString();
  2964. //bool isEnabled = buttonObj.value("enabled").toBool();
  2965. // 创建按钮
  2966. QPushButton *button = new QPushButton(widgetLeft);
  2967. QString ObjectName = "thirdLevelBtn_F" + QString::number(buttonId);
  2968. button->setObjectName(ObjectName); // 设置带前缀的对象名称
  2969. ObjectName = button->objectName();
  2970. connect(button, &QPushButton::clicked, this, [&, strCallFun, buttonId]()
  2971. {
  2972. ns_module::ST_BUTTON_FUN callFun = {};
  2973. //callFun.DirectoryId = strDirectoryId.toInt();
  2974. callFun.strCallFun = strCallFun.toStdString();
  2975. callFun.nId = buttonId;
  2976. emit RunFunSignals(callFun);
  2977. });
  2978. // 设置按钮的样式,调整图标和文本的位置
  2979. button->setStyleSheet(R"(
  2980. QPushButton {
  2981. position: absolute;
  2982. border-radius: 6px;
  2983. opacity: 1;
  2984. background: #CBD0FF;
  2985. border: none;
  2986. }
  2987. QPushButton:hover {
  2988. background-color: #A9B4FF; /* 鼠标悬停效果 */
  2989. }
  2990. )");
  2991. if (buttonId > 12)
  2992. {
  2993. qWarning() << "buttonId 超过最大ID";
  2994. continue;
  2995. }
  2996. // 设置按钮的位置和大小
  2997. int x = 16;
  2998. int y = 245 + (buttonId - 1) * (48 + 13); // 第一个按钮 y=245,后续每个按钮间隔13px
  2999. button->setGeometry(x, y, 158, 48);
  3000. // 设置按钮的可见性,根据 "enabled" 字段显示或隐藏按钮
  3001. button->setVisible(isEnabled);
  3002. // 创建图标标签
  3003. QLabel* iconLabel = new QLabel(button);
  3004. iconLabel->setPixmap(QIcon(buttonIcon).pixmap(16, 16));
  3005. iconLabel->setGeometry(10, 16, 16, 16); // 图标距离左边10px,顶部16px
  3006. iconLabel->setFixedSize(16, 16);
  3007. iconLabel->setStyleSheet("background-color: transparent;");
  3008. iconLabel->setVisible(isEnabled); // 根据按钮的可见性设置图标的可见性
  3009. // 创建文本标签
  3010. QLabel* textLabel = new QLabel(buttonText, button);
  3011. textLabel->setGeometry(34, 0, 90, 48); // 文本距离左边34px
  3012. textLabel->setWordWrap(true); // 允许换行
  3013. textLabel->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
  3014. textLabel->setStyleSheet(R"(
  3015. QLabel {
  3016. background: transparent;
  3017. font-family: "思源黑体";
  3018. font-size: 14px;
  3019. font-weight: 500;
  3020. color: #4E51CE;
  3021. }
  3022. )");
  3023. textLabel->setVisible(isEnabled); // 根据按钮的可见性设置文本的可见性
  3024. // 创建 F1-F12 标签
  3025. QString fLabelText = QString("F%1").arg(buttonId); // F1, F2, ..., F12
  3026. QLabel* fLabel = new QLabel(fLabelText, button);
  3027. fLabel->setFixedSize(21, 16); // 设置大小为21x16
  3028. fLabel->setAlignment(Qt::AlignCenter);
  3029. fLabel->setStyleSheet(R"(
  3030. QLabel {
  3031. background-color: transparent;
  3032. color: #2A7ED8;
  3033. font-size: 12px;
  3034. font-weight: bold;
  3035. }
  3036. )");
  3037. // 设置标签的位置
  3038. int fX = 134;
  3039. int fY = 2;
  3040. fLabel->setGeometry(fX, fY, 14, 16);
  3041. fLabel->setVisible(isEnabled); // 根据按钮的可见性设置标签的可见性
  3042. // 将按钮、图标、文本和标签添加到控件列表
  3043. m_fieldWidgets.append(button);
  3044. m_fieldWidgets.append(iconLabel);
  3045. m_fieldWidgets.append(textLabel);
  3046. m_fieldWidgets.append(fLabel);
  3047. }
  3048. }
  3049. // 清理并隐藏三级目录菜单内容
  3050. void DbTreeViewManager::clearThirdLevelMenu()
  3051. {
  3052. // 遍历所有子控件,找到标题为 "字段展示" 的窗口并关闭
  3053. foreach (QObject *child, m_pOriginalWndMenuPage->children()) {
  3054. QWidget *childWidget = qobject_cast<QWidget*>(child);
  3055. if (childWidget && childWidget->windowTitle() == "字段展示") {
  3056. qDebug() << "关闭现有的字段展示窗口";
  3057. childWidget->close();
  3058. }
  3059. }
  3060. // 显示主目录树和分隔线
  3061. m_pTreeViewDown->show();
  3062. for (auto separator : firstLevelSeparators) {
  3063. separator->show();
  3064. }
  3065. }
  3066. // 保存选中路径
  3067. void DbTreeViewManager::saveCheckedPaths()
  3068. {
  3069. if (m_currentConfigName == "") {
  3070. qWarning() << "当前配置ID无效,无法保存复选框状态。";
  3071. return;
  3072. }
  3073. QSettings settings("RunCloudTech", "David");
  3074. //QString configKey = QString::number(m_currentConfigId);
  3075. settings.beginGroup("TreeViewCheckedState");
  3076. // 保存选中路径
  3077. QString keyChecked = QString("checkedPaths/%1").arg(m_currentConfigName);
  3078. QStringList checkedList = collectCheckedPaths();
  3079. settings.setValue(keyChecked, checkedList);
  3080. settings.endGroup();
  3081. qDebug() << "保存复选框状态路径:" << checkedList;
  3082. }
  3083. // 加载选中路径
  3084. void DbTreeViewManager::loadCheckedPaths()
  3085. {
  3086. if (m_currentConfigName == "") {
  3087. qWarning() << "当前配置ID无效,无法加载复选框状态。";
  3088. return;
  3089. }
  3090. QSettings settings("RunCloudTech", "David");
  3091. //QString configKey = QString::number(m_currentConfigId);
  3092. settings.beginGroup("TreeViewCheckedState");
  3093. // 读取选中路径
  3094. QString keyChecked = QString("checkedPaths/%1").arg(m_currentConfigName);
  3095. QStringList loadedChecked = settings.value(keyChecked).toStringList();
  3096. settings.endGroup();
  3097. qDebug() << "加载复选框状态路径:" << loadedChecked;
  3098. m_blockItemChanged = true;
  3099. setCheckedPaths(loadedChecked);
  3100. m_blockItemChanged = false;
  3101. // 更新导航栏
  3102. if (!loadedChecked.isEmpty()) {
  3103. QString lastPathStr = loadedChecked.last();
  3104. QStringList lastPath = lastPathStr.split("/");
  3105. QModelIndex lastIdx = findItemByPath(lastPath);
  3106. if (lastIdx.isValid()) {
  3107. m_pTreeViewDown->setCurrentIndex(lastIdx);
  3108. updateNavigationBar(lastIdx);
  3109. }
  3110. } else {
  3111. // 如果没有加载到任何路径,自动选择第一个目录
  3112. QStandardItem *rootItem = m_pCModel->invisibleRootItem();
  3113. if (rootItem->rowCount() > 0) {
  3114. QModelIndex firstIndex = m_pCModel->index(0, 0, QModelIndex());
  3115. if (firstIndex.isValid()) {
  3116. m_pTreeViewDown->setCurrentIndex(firstIndex);
  3117. m_pTreeViewDown->expand(firstIndex); // 展开第一个目录
  3118. QStandardItem *firstItem = m_pCModel->itemFromIndex(firstIndex);
  3119. QVariant data = firstItem->data(Qt::UserRole + 2);
  3120. if (data.canConvert<QJsonObject>()) {
  3121. QJsonObject thirdLevelObj = data.toJsonObject();
  3122. if (thirdLevelObj.contains("IsThirdLevel") && thirdLevelObj["IsThirdLevel"].toBool()) {
  3123. QString multiTableName = "";
  3124. if (thirdLevelObj.contains("TableName")) {
  3125. multiTableName = thirdLevelObj["TableName"].toString();
  3126. }
  3127. QList<CONFIG_BASE_STRUCT> buttons;
  3128. int userGrade = 0x1;
  3129. m_sqlOper->GetThirdDirButtons(multiTableName, userGrade, buttons);
  3130. displayThirdLevelButtons(buttons);
  3131. //loadButtonConfigForThirdLevel(thirdLevelObj);
  3132. }
  3133. }
  3134. }
  3135. }
  3136. }
  3137. }
  3138. // 保存展开路径
  3139. void DbTreeViewManager::saveExpandedPaths()
  3140. {
  3141. if (m_currentConfigName == "") {
  3142. qWarning() << "当前配置ID无效,无法保存展开路径。";
  3143. return;
  3144. }
  3145. QSettings settings("RunCloudTech", "David");
  3146. //QString configKey = QString::number(m_currentConfigId);
  3147. settings.beginGroup("TreeViewExpandedState");
  3148. // 保存展开路径
  3149. QString keyExpanded = QString("expandedPaths/%1").arg(m_currentConfigName);
  3150. QStringList expandedList = QStringList(expandedPaths.begin(), expandedPaths.end());
  3151. settings.setValue(keyExpanded, expandedList);
  3152. settings.endGroup();
  3153. qDebug() << "保存展开路径:" << expandedList;
  3154. }
  3155. // 加载展开路径
  3156. void DbTreeViewManager::loadExpandedPaths()
  3157. {
  3158. if (m_currentConfigName == "") {
  3159. qWarning() << "当前配置ID无效,无法加载展开路径。";
  3160. return;
  3161. }
  3162. QSettings settings("RunCloudTech", "David");
  3163. //QString configKey = QString::number(m_currentConfigId);
  3164. settings.beginGroup("TreeViewExpandedState");
  3165. // 读取展开路径
  3166. QString keyExpanded = QString("expandedPaths/%1").arg(m_currentConfigName);
  3167. QStringList loadedExpanded = settings.value(keyExpanded).toStringList();
  3168. settings.endGroup();
  3169. qDebug() << "加载展开路径:" << loadedExpanded;
  3170. m_blockItemChanged = true;
  3171. // 恢复展开路径
  3172. for (const QString &p : loadedExpanded) {
  3173. QStringList path = p.split("/");
  3174. QModelIndex idx = findItemByPath(path);
  3175. if (idx.isValid()) {
  3176. m_pTreeViewDown->expand(idx);
  3177. expandedPaths.insert(p);
  3178. qDebug() << "成功恢复展开路径:" << p;
  3179. } else {
  3180. qDebug() << "未找到展开路径部分: " << p;
  3181. }
  3182. }
  3183. m_blockItemChanged = false;
  3184. }
  3185. // 清空历史
  3186. void DbTreeViewManager::clearHistory()
  3187. {
  3188. m_backStack.clear();
  3189. m_forwardStack.clear();
  3190. buttonLeft->setIcon(QIcon(":/images/home_left_hide.png"));
  3191. buttonRight->setIcon(QIcon(":/images/home_right.png"));
  3192. }
  3193. // 获取当前页面的 PageState
  3194. DbTreeViewManager::PageState DbTreeViewManager::getCurrentPageState()
  3195. {
  3196. PageState st;
  3197. st.isThirdLevel = false;
  3198. st.directoryId = -1;
  3199. // 判断是否在第三层窗口中
  3200. if (m_thirdLevelFieldWnd && m_thirdLevelFieldWnd->isVisible()) {
  3201. // 如果此时是在第三层界面,则用当前导航栏 or 目录树的 “选中项” 来获取路径
  3202. QModelIndex curIndex = m_pTreeViewDown->currentIndex();
  3203. if (!curIndex.isValid()) {
  3204. st.path << "未知目录";
  3205. } else {
  3206. QStandardItem* item = m_pCModel->itemFromIndex(curIndex);
  3207. st.path = buildItemPath(item);
  3208. }
  3209. // 如果能拿到 DirectoryId,则置为第三层
  3210. QVariant dataVar = m_pCModel->itemFromIndex(m_pTreeViewDown->currentIndex())->data(Qt::UserRole + 2);
  3211. if (dataVar.canConvert<QJsonObject>()) {
  3212. QJsonObject obj = dataVar.toJsonObject();
  3213. if (obj.contains("IsThirdLevel") && obj["IsThirdLevel"].toBool()) {
  3214. st.isThirdLevel = true;
  3215. st.directoryId = obj.value("Id").toInt(-1);
  3216. st.multiTableName = obj.value("TableName").toString();
  3217. }
  3218. }
  3219. }
  3220. else {
  3221. // 当前是目录树界面,就看 treeView 选中项
  3222. QModelIndex curIndex = m_pTreeViewDown->currentIndex();
  3223. if (curIndex.isValid()) {
  3224. QStandardItem* item = m_pCModel->itemFromIndex(curIndex);
  3225. st.path = buildItemPath(item);
  3226. // 检查是否第三层
  3227. QVariant dataVar = item->data(Qt::UserRole + 2);
  3228. if (dataVar.canConvert<QJsonObject>()) {
  3229. QJsonObject obj = dataVar.toJsonObject();
  3230. if (obj.contains("IsThirdLevel") && obj["IsThirdLevel"].toBool()) {
  3231. st.isThirdLevel = true;
  3232. st.directoryId = obj.value("Id").toInt(-1);
  3233. st.multiTableName = obj.value("TableName").toString();
  3234. }
  3235. }
  3236. }
  3237. }
  3238. return st;
  3239. }
  3240. //加载指定的 PageState:相当于“回到该路径”或“跳转到这个第三层”
  3241. void DbTreeViewManager::loadPageState(const PageState &st, bool isByHistoryNav)
  3242. {
  3243. // 先根据 st.path 找到对应的节点
  3244. QModelIndex idx = findItemByPath(st.path);
  3245. if (!idx.isValid()) {
  3246. qWarning() << "[loadPageState] 未找到路径" << st.path;
  3247. return;
  3248. }
  3249. // 选中该节点
  3250. m_pTreeViewDown->setCurrentIndex(idx);
  3251. m_pTreeViewDown->scrollTo(idx);
  3252. // 更新导航栏
  3253. updateNavigationBar(idx);
  3254. // 如果是第三层,就加载对应的数据窗口
  3255. if (st.isThirdLevel && st.directoryId != -1) {
  3256. // 如果当前已在一个第三层窗口,先关闭它
  3257. if (m_thirdLevelFieldWnd) {
  3258. m_thirdLevelFieldWnd->close();
  3259. m_thirdLevelFieldWnd->deleteLater();
  3260. m_thirdLevelFieldWnd = nullptr;
  3261. }
  3262. int userPrivilege = 0x1;
  3263. QList<Table_Control_Data> tableControlDatas;
  3264. bool allChangeFlag = false;
  3265. m_sqlOper->GetThirdDirControlData(st.multiTableName, userPrivilege, tableControlDatas, allChangeFlag);
  3266. displayThirdLevelFields(tableControlDatas, allChangeFlag, false); // 传递参数表示 Home 界面
  3267. //QJsonObject data = fetchThirdLevelData(st.directoryId, userPrivilege);
  3268. //displayThirdLevelFields(data, (m_currentConfigName == "Home"));
  3269. } else {
  3270. // 否则,显示目录树
  3271. if (m_thirdLevelFieldWnd) {
  3272. m_thirdLevelFieldWnd->close();
  3273. m_thirdLevelFieldWnd->deleteLater();
  3274. m_thirdLevelFieldWnd = nullptr;
  3275. }
  3276. m_pTreeViewDown->show();
  3277. for (auto sep : firstLevelSeparators) {
  3278. if (sep) sep->show();
  3279. }
  3280. }
  3281. if (!isByHistoryNav) {
  3282. m_forwardStack.clear();
  3283. buttonRight->setIcon(QIcon(":/images/home_right.png"));
  3284. // 把新状态压入后退栈
  3285. m_backStack.append(st);
  3286. if(m_backStack.size() >= 2){
  3287. buttonLeft->setIcon(QIcon(":/images/home_left.png"));
  3288. }
  3289. }
  3290. }
  3291. void DbTreeViewManager::loginput(const QString& fieldTableName, const QString& fieldName,const QString& modifies){
  3292. // 获取当前时间并格式化
  3293. QDateTime currentDateTime = QDateTime::currentDateTime();
  3294. QString timestamp = currentDateTime.toString("yyyy-MM-dd hh:mm:ss");
  3295. QString logMessage = QString("%1 :fieldTableName is %2 and fieldName is %3 ,currentvalue is modified %4" ).arg(timestamp).arg(fieldTableName).arg(fieldName).arg(modifies);
  3296. QString pathoflog = "C:\\Users\\Administrator\\Desktop\\qt\\merge\\gujiangdong\\die-bonder-ui0218\\die-bonder-ui\\log/log.txt";
  3297. writeLogToFile(logMessage,pathoflog);
  3298. }
  3299. void DbTreeViewManager::writeLogToFile(const QString& logMessage, const QString& filePath) {
  3300. QFile logFile(filePath);
  3301. if (!logFile.open(QIODevice::Append | QIODevice::Text)) {
  3302. qWarning() << "Cannot open file for writing:" << filePath;
  3303. return;
  3304. }
  3305. QTextStream out(&logFile);
  3306. out << logMessage << "\n";
  3307. logFile.close();
  3308. }