DbTreeViewManager.cpp 165 KB

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