123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215 |
- #include "DbTreeViewManager.h"
- #include "OriginalWnd/OriginalWnd.h"
- #include "OriginalWnd/NonInteractiveCheckDelegate.h"
- #include <QFile>
- #include <QTextStream>
- #include <QDateTime>
- #include "Src/common/JMessageTip.h"
- #include "Src/common/JLogAllOutput.h"
- #include "CViewInterface.h"
- // 构造函数
- DbTreeViewManager::DbTreeViewManager(QWidget* pOriginalWndMenuPage, QWidget* parent)
- : QWidget(parent),
- m_pOriginalWndMenuPage(pOriginalWndMenuPage)
- {
- m_originalWnd = dynamic_cast<OriginalWnd*>(parent);
- Init();
- // 设置模型
- m_pTreeViewDown->setModel(m_pCModel);
- m_pTreeViewDown->setHeaderHidden(true);
- m_pTreeViewDown->setEditTriggers(QAbstractItemView::NoEditTriggers);
- // 设置树视图的几何位置
- m_pTreeViewDown->setGeometry(16, 106, m_pOriginalWndMenuPage->width() - 16, m_pOriginalWndMenuPage->height() - 106);
- // 安装事件过滤器以自定义绘制虚线
- m_pTreeViewDown->viewport()->installEventFilter(this);
- // 创建统一分隔线
- m_pLineFrame1 = createUnifiedSeparator(m_pOriginalWndMenuPage, 2);
- m_pLineFrame1->setGeometry(16, 100, 460, 2);
- // 创建按钮并设置布局
- setupButton();
- // 创建导航栏
- m_pNavigationWidget = new QWidget(m_pOriginalWndMenuPage);
- m_pNavigationWidget->setGeometry(15, 15, 300, 74);
- // 连接目录前的复选框信号与槽
- connect(m_pCModel, &QStandardItemModel::itemChanged, this, &DbTreeViewManager::onItemChanged);
- // 目录树连接点击信号
- connect(m_pTreeViewDown, &QTreeView::clicked, this, &DbTreeViewManager::onTreeViewClicked);
- // 连接目录 expanded 和 collapsed 信号
- connect(m_pTreeViewDown, &QTreeView::expanded, this, [=](const QModelIndex &index) {
- QStandardItem *item = m_pCModel->itemFromIndex(index);
- if (!item) return;
- QStringList path = buildItemPath(item);
- expandedPaths.insert(path.join("/"));
- updateSeparatorLine();
- // 保存展开路径
- saveExpandedPaths();
- });
- connect(m_pTreeViewDown, &QTreeView::collapsed, this, [=](const QModelIndex &index) {
- QStandardItem *item = m_pCModel->itemFromIndex(index);
- if (!item) return;
- QStringList path = buildItemPath(item);
- expandedPaths.remove(path.join("/"));
- updateSeparatorLine();
- // 保存展开路径
- saveExpandedPaths();
- });
- // 所有展开操作完成后更新分隔线
- QTimer::singleShot(0, this, [=]() {
- QStandardItem *rootItem = m_pCModel->invisibleRootItem();
- QStandardItem *thirdItem = findFirstThirdLevelItemDFS(rootItem);
- if (thirdItem)
- {
- QJsonObject thirdLevelObj = thirdItem->data(Qt::UserRole + 2).toJsonObject();
- if (thirdLevelObj.contains("IsThirdLevel") && thirdLevelObj["IsThirdLevel"].toBool())
- {
- QString multiTableName = "";
- if (thirdLevelObj.contains("TableName"))
- {
- multiTableName = thirdLevelObj["TableName"].toString();
- }
- QList<CONFIG_BASE_STRUCT> buttons;
- int userGrade = 0x1;
- m_sqlOper->GetThirdDirButtons(multiTableName, userGrade, buttons);
- displayThirdLevelButtons(buttons);
- // loadButtonConfigForThirdLevel(thirdLevelObj);
- }
- }
- });
- // 应用自定义只读复选框委托
- m_pTreeViewDown->setItemDelegate(new NonInteractiveCheckDelegate(m_pTreeViewDown));
- menuArray[0] = {2, false,-1};
- menuArray[1] = {5, false,-1};
- menuArray[2] = {3, false,-1};
- menuArray[3] = {4, false,-1};
- menuArray[4] = {6, false,-1};
- currentMenuId = -1;
- }
- DbTreeViewManager::~DbTreeViewManager()
- {
-
- }
- void DbTreeViewManager::Init()
- {
- m_pTreeViewDown = new QTreeView(this);
- m_pCModel = new QStandardItemModel(this);
- m_sqlOper = &SqlOperation::GetInstance();
- m_pTreeCViewInterface = ns_module::CViewInterface::GetInstance();
- }
- // 创建横线样式
- QFrame* DbTreeViewManager::createUnifiedSeparator(QWidget *parent, int height)
- {
- QFrame *separator = new QFrame(parent);
- separator->setFrameShape(QFrame::NoFrame); // 移除默认框架
- separator->setFixedHeight(height); // 设置固定高度
- separator->setStyleSheet("background-color: #C7CAEB;");
- separator->hide(); // 初始化为隐藏
- return separator;
- }
- // 目录树样式
- void DbTreeViewManager::applyCustomStyles() {
- m_pTreeViewDown->setStyleSheet(R"(
- /* 设置分支图标 */
- QTreeView::branch:closed:has-children {
- border-image: none;
- image: url(:/images/home_add.png);
- }
- QTreeView::branch:open:has-children {
- border-image: none;
- image: url(:/images/home_minus.png);
- }
- /* 设置多选框 */
- QTreeView::indicator:unchecked {
- image: url(:/images/home_NotSelecte.png);
- }
- QTreeView::indicator:checked {
- image: url(:/images/home_selected.png);
- }
- /* 背景透明,行间距 */
- QTreeView {
- background: transparent;
- border: none;
- }
- /* 设置项目选中的背景色 */
- QTreeView::item:selected {
- background-color: #A9B4FF;
- }
- /* 设置项目的行间距 */
- QTreeView::item {
- padding-top: 5px; /* 上边距 */
- padding-bottom: 5px; /* 下边距 */
- }
- )");
- }
- // 检查权限
- bool DbTreeViewManager::hasPrivilege(const int &UserGrade, const int &userPrivilege)
- {
- bool hasPriv;
- // UserGrade 是以逗号分隔的字符串,如 "1,2,3"
- if(UserGrade==userPrivilege){
- hasPriv = true;
- }else{
- hasPriv = false;
- }
- // qDebug() << "Checking privilege. UserGrade:" << UserGrade << "userPrivilege:" << userPrivilege << "result:" << hasPriv;
- return hasPriv;
- }
- //void DbTreeViewManager::updateDb(const QString &TableName, const int &Id,const QString currentValue){
- // QSqlDatabase db = QSqlDatabase::database();
- // if (!db.isOpen()) {
- // qWarning() << "数据库未打开";
- // }
- // QString sql = QString("UPDATE %1 SET currentValue = :newValue WHERE id = :id").arg(TableName);
- // QSqlQuery query;
- // query.prepare(sql);
- // query.bindValue(":newValue", currentValue);
- // query.bindValue(":id", Id);
- // // 执行更新操作
- // if (!query.exec()) {
- // qWarning() << "Failed to update currentValue:" << query.lastError().text();
- // } else {
- // qDebug() << "currentValue updated successfully.";
- // }
- //}
- void DbTreeViewManager::initializeTree(QString name, const int &userPrivilege)
- {
- QString strLog = "Initializing tree with configId:";
- strLog += name;
- strLog += "and userPrivilege:";
- strLog += userPrivilege;
- JLogAllOutput::cmd_debug(strLog.toStdString());
- m_currentConfigName = name; // 设置当前配置ID
- // 先清空历史
- clearHistory();
- // 保存之前的选中/展开状态
- m_blockItemChanged = true;
- m_isRestoring = true;
- // 清理当前的分隔线
- clearAllSeparators();
- // 清空已有模型数据和相关记录
- m_pCModel->clear();
- firstLevelSeparators.clear();
- expandedPaths.clear();
- // 清理并隐藏三级目录窗口
- clearThirdLevelMenu();
- // **Home 界面逻辑**
- if (name == "Home")
- {
- // 隐藏目录树和分隔线
- m_pTreeViewDown->hide();
- for (auto separator : firstLevelSeparators)
- {
- if (separator)
- {
- separator->hide();
- }
- }
- // 隐藏导航栏
- m_pNavigationWidget->hide();
- // 隐藏按钮
- updateButtonsVisibility(); // 将按钮隐藏
- // 隐藏特定的分隔线 lineFrame1
- if (m_pLineFrame1)
- {
- m_pLineFrame1->hide();
- }
- // 从 BondHead 和 Buttons 表中加载 DirectoryId = 1 的数据
- QList<Table_Control_Data> tableControlDatas;
- bool allChangeFlag = false;
- m_sqlOper->GetThirdDirControlData("HomeProduct", userPrivilege, tableControlDatas, allChangeFlag);
- //QJsonObject data = fetchThirdLevelData(1, userPrivilege);
- displayThirdLevelFields(tableControlDatas, allChangeFlag, true); // 传递参数表示 Home 界面
- // 把当前页面压到 m_backStack 里
- PageState st;
- st.path << "Home";
- st.isThirdLevel = true;
- st.directoryId = 1;
- st.multiTableName = "HomeProduct";
- m_backStack.append(st);
- if(m_backStack.size() >= 2){
- buttonLeft->setIcon(QIcon(":/images/home_left.png"));
- }
- } else {
- // **其他配置逻辑**
- // 设置 treeViewDown 的几何位置
- m_pTreeViewDown->setGeometry(16, 106, m_pOriginalWndMenuPage->width() - 16, m_pOriginalWndMenuPage->height() - 106);
- // 显示目录树和分隔线
- m_pTreeViewDown->show();
- for (auto separator : firstLevelSeparators) {
- if (separator)
- separator->show();
- }
- // 应用自定义样式
- applyCustomStyles();
- // 显示导航栏
- m_pNavigationWidget->show();
- // 显示按钮
- updateButtonsVisibility();
- // 显示特定的分隔线 lineFrame1
- if (m_pLineFrame1) {
- m_pLineFrame1->show();
- }
- // 加载目录数据 并进行权限判断
- QString tableName = "Dir_" + name;
- QList<QJsonObject> directories;
- m_sqlOper->GetDirectories(tableName, userPrivilege, directories);
- //m_sqlOper.loadDirectories(tableName, userPrivilege, directories);
- buildTreeFromDirectories(directories);
- //loadDirectories(configId, userPrivilege);
- //
- // 更新分隔线
- updateSeparatorLine();
- m_blockItemChanged = false;
- m_isRestoring = false;
- // qDebug() << "Tree initialization completed.";
- // 恢复展开路径
- loadExpandedPaths();
- // 恢复复选框状态
- loadCheckedPaths();
- // 自动选中第一个根目录项并更新导航栏
- QStandardItem *rootItem = m_pCModel->invisibleRootItem()->child(0);
- if (rootItem) {
- QModelIndex rootIndex = m_pCModel->indexFromItem(rootItem);
- m_pTreeViewDown->setCurrentIndex(rootIndex);
- updateNavigationBar(rootIndex);
- // 如果当前根目录是三级目录,则加载其字段内容
- QVariant dataVar = rootItem->data(Qt::UserRole + 2).toJsonObject();
- bool isThirdLevel = false;
- int directoryId = -1;
- QString multiTableName = "";
- if (dataVar.canConvert<QJsonObject>()) {
- QJsonObject thirdLevelObj = dataVar.toJsonObject();
- if (thirdLevelObj.contains("IsThirdLevel") && thirdLevelObj["IsThirdLevel"].toBool()) {
- isThirdLevel = true;
- if (thirdLevelObj.contains("Id")) {
- directoryId = thirdLevelObj["Id"].toInt();
- }
- if (thirdLevelObj.contains("TableName"))
- {
- multiTableName = thirdLevelObj["TableName"].toString();
- }
- }
- }
- if (isThirdLevel) {
- if (directoryId == -1) {
- // qWarning() << "无效的 DirectoryId,无法加载第三层目录内容。";
- return;
- }
- // userPrivilege 同样传递
- //QJsonObject data = fetchThirdLevelData(directoryId, userPrivilege);
- QList<Table_Control_Data> tableControlDatas;
- bool allChangeFlag = false;
- m_sqlOper->GetThirdDirControlData(multiTableName, userPrivilege, tableControlDatas, allChangeFlag);
- //QJsonObject data = fetchThirdLevelData(1, userPrivilege);
- displayThirdLevelFields(tableControlDatas, allChangeFlag, false); // 传递参数表示 Home 界面
- //QJsonObject data = m_sqlOper.fetchThirdLevelData(multiTableName, directoryId, userPrivilege);
- //displayThirdLevelFields(data, false); // 传递参数表示非 Home 界面
- // 把当前状态压入 m_backStack
- PageState st;
- st.path = buildItemPath(rootItem);
- st.isThirdLevel = true;
- st.directoryId = directoryId;
- st.multiTableName = multiTableName;
- m_backStack.append(st);
- if(m_backStack.size() >= 2){
- buttonLeft->setIcon(QIcon(":/images/home_left.png"));
- }
- }
- } else {
- // qWarning() << "树形模型中没有根目录项。";
- }
- //寻找第一个三级目录,显示第一个三级目录的按钮
- // 自动找第一个三级目录
- QStandardItem *rootItem1 = m_pCModel->invisibleRootItem();
- QStandardItem *firstThirdItem = findFirstThirdLevelItemDFS(rootItem1);
- if (!firstThirdItem) {
- // qDebug() << "[initializeTree] No third-level item found, do nothing.";
- return;
- }
- // QStandardItem 里获取它的目录信息
- QVariant dataVar = firstThirdItem->data(Qt::UserRole + 2);
- if (!dataVar.canConvert<QJsonObject>()) {
- // qWarning() << "[initializeTree] firstThirdItem->data 无法转换为 QJsonObject";
- return;
- }
- QJsonObject dirObj = dataVar.toJsonObject();
- if (!dirObj.contains("Id")) {
- // qWarning() << "[initializeTree] 第一个三级目录无 Id 字段,无法加载";
- return;
- }
- int directoryId = dirObj["Id"].toInt();
- if (!dirObj.contains("TableName"))
- {
- return;
- }
- QString multiTableName = dirObj["TableName"].toString();
- //根据目录ID找出按钮
- QTimer::singleShot(0, this, &DbTreeViewManager::updateSeparatorLine);
- QList<CONFIG_BASE_STRUCT> buttons;
- m_sqlOper->GetThirdDirButtons(multiTableName, userPrivilege, buttons);
- //QJsonObject data = fetchThirdLevelData(1, userPrivilege);
- displayThirdLevelButtons(buttons); // 传递参数表示 Home 界面
- // 数据库BondHead/Buttons
- //QJsonObject data = fetchThirdLevelData(directoryId, userPrivilege);
- //if (!data.contains("buttons")) {
- // // 第三层没有 buttons 字段
- // qWarning() << "[initializeTree] fetchThirdLevelData: no 'buttons', skip";
- // return;
- //}
- // singleShot 在所有展开操作完成后更新分隔线
-
- // loadButtonConfigForThirdLevel 显示按钮
- //loadButtonConfigForThirdLevel(data);
- }
- }
- // 从数据库加载目录 包含权限判断
- void DbTreeViewManager::loadDirectories(int configId, const int &userPrivilege)
- {
- // qDebug() << "Loading directories for configId:" << configId << "with userPrivilege:" << userPrivilege;
- QSqlDatabase db = QSqlDatabase::database();
- if (!db.isOpen()) {
- qWarning() << "数据库未打开";
- return;
- }
- QSqlQuery query(db);
- query.prepare(R"(
- SELECT Id, Name, ParentId, IsThirdLevel, Separator, UserGrade, Circle
- FROM Directories
- WHERE ConfigId = :configId
- )");
- query.bindValue(":configId", configId);
- if (!query.exec()) {
- qWarning() << "查询 Directories 失败:" << query.lastError().text();
- return;
- }
- // 以 QJsonObject 保存所有目录
- QList<QJsonObject> directories;
- while (query.next()) {
- QJsonObject obj;
- int id = query.value("Id").toInt();
- obj["Id"] = id;
- obj["Name"] = query.value("Name").toString();
- obj["ParentId"] = query.value("ParentId").isNull() ? -1 : query.value("ParentId").toInt();
- obj["IsThirdLevel"] = query.value("IsThirdLevel").toBool();
- obj["Separator"] = query.value("Separator").toInt();
- obj["UserGrade"] = query.value("UserGrade").toString();
- obj["Circle"] = query.value("Circle").toInt();
- qDebug() << "加载目录:"<< obj["Name"].toString() << " ParentId:" << obj["ParentId"].toInt()<< " Separator:" << obj["Separator"].toInt();
- qDebug() << " Circle:" << obj["Circle"].toInt();
- // 权限判断:只有当记录的 UserGrade 中包含当前用户权限时才加入
- bool ok;
- if (hasPrivilege(obj["UserGrade"].toString().toInt(&ok,16), userPrivilege))
- directories.append(obj);
- else
- qDebug() << "目录跳过(权限不足):" << obj["Name"].toString();
- }
- // qDebug() << "Loaded directories count:" << directories.size();
- // 根据目录列表构建树模型
- buildTreeFromDirectories(directories);
- }
- void DbTreeViewManager::buildTreeFromDirectories(const QList<QJsonObject> &directories)
- {
- // 建立 Id->QStandardItem 的映射
- QMap<int, QStandardItem*> idItemMap;
- for (const QJsonObject &dir : directories) {
- QStandardItem *item = new QStandardItem(dir["Name"].toString());
- item->setData(dir["Id"].toInt(), Qt::UserRole + 1); // 保存目录Id
- // 如果是三级目录,将整个 JSON 对象存到 UserRole+2
- if (dir["IsThirdLevel"].toBool()) {
- item->setData(dir, Qt::UserRole + 2);
- } else {
- item->setData(false, Qt::UserRole + 2); // 标记为非三级目录
- }
- item->setCheckable(true);
- // 复选框状态由程序逻辑设置
- item->setCheckState(Qt::Unchecked);
- idItemMap.insert(dir["Id"].toInt(), item);
- }
- // 建立父子关系
- for (const QJsonObject &dir : directories) {
- int parentId = dir["ParentId"].toInt();
- int currentId = dir["Id"].toInt();
- QStandardItem *childItem = idItemMap.value(currentId);
- if (childItem == nullptr)
- {
- qDebug() << "child is nullptr";
- }
- if (parentId == -1) {
- // 说明这是一级目录
- m_pCModel->invisibleRootItem()->appendRow(childItem);
- qDebug() << " !!!!! first :" << "child : " << currentId;
- // 如果一级目录的 "Separator" == 1,则为其创建一条分割线
- if (dir["Separator"].toInt() == 1) {
- QFrame *sep = createUnifiedSeparator(m_pOriginalWndMenuPage, 2);
- sep->hide();
- firstLevelSeparators.insert(childItem, sep);
- }
- } else {
- // 非一级目录 => 子目录
- QStandardItem *parentItem = idItemMap.value(parentId);
- if (parentItem)
- {
- parentItem->appendRow(childItem);
- qDebug() << "parent:" << parentId << "child : " << currentId;
- }
- else
- qWarning() << "无法找到 ParentId:" << parentId << "的父目录项";
- }
- }
- }
- // 查询三级目录下的 BondHead 和 Buttons 数据 均判断权限
- //QJsonObject DbTreeViewManager::fetchThirdLevelData(int directoryId, const int &userPrivilege)
- //{
- // QJsonObject data;
- // QJsonArray fieldsArray;
- // QJsonArray buttonsArray;
- // QString temp;
- // bool allChangeFlag = true; // 默认所有 ChangeFlag 为 true
- //
- // QSqlDatabase db = QSqlDatabase::database();
- // if (!db.isOpen()) {
- // qWarning() << "数据库未打开";
- // return data;
- // }
- // // 查询 TableName 表
- // QSqlQuery queryBondtest(db);
- // queryBondtest.prepare(R"(
- // SELECT TableName
- // FROM Directories
- // WHERE Id = :dirId
- // )");
- // queryBondtest.bindValue(":dirId",directoryId);
- // if (queryBondtest.exec()) {
- // while (queryBondtest.next()) {
- // temp = queryBondtest.value("TableName").toString();
- // }
- // }
- // QStringList tableName = temp.split(",");
- // QString currentTableName;
- // for (const QString &item : tableName) {
- // if (item.isEmpty()) {
- // qDebug() << "Encountered an empty string, stopping traversal.";
- // break; // 遇到空字符串时退出循环
- // }else{
- // currentTableName = item;
- // QString sql = QString("SELECT Id, DirectoryId, Describe, Type, Value,CurrentValue,UpLimit,DownLimit,units, Groupld, UserGrade, ChangeFlag FROM %1 WHERE DirectoryId = :dirId").arg(currentTableName);
- // // 查询 BondHead 表
- // QSqlQuery queryBond(db);
- // queryBond.prepare(sql);
- // queryBond.bindValue(":dirId", directoryId);
- // if (queryBond.exec()) {
- // while (queryBond.next()) {
- // bool ok;
- // int dbUserGrade = queryBond.value("UserGrade").toString().toInt(&ok, 16);
- // // 只有当数据库记录的 UserGrade 包含当前用户权限时才加载该字段
- // if (!hasPrivilege(dbUserGrade, userPrivilege))
- // continue;
- // QJsonObject field;
- // field["Id"] = queryBond.value("Id").toInt();
- // field["DirectoryId"] = queryBond.value("DirectoryId").toInt();
- // field["Describe"] = queryBond.value("Describe").toString();
- // field["Type"] = queryBond.value("Type").toString();
- // field["Value"] = queryBond.value("Value").toString();
- // field["CurrentValue"] = queryBond.value("CurrentValue").toString();
- // field["UpLimit"] = queryBond.value("UpLimit").toString();
- // field["DownLimit"] = queryBond.value("DownLimit").toString();
- // field["units"] = queryBond.value("units").toString();
- // field["Groupld"] = queryBond.value("Groupld").toInt();
- // field["ChangeFlag"] = queryBond.value("ChangeFlag").toInt();
- // field["TableName"] = currentTableName;
- // fieldsArray.append(field);
- //
- // // 如果有任一字段 ChangeFlag 为 0,则 allChangeFlag = false
- // if (queryBond.value("ChangeFlag").toInt() == 0)
- // allChangeFlag = false;
- // }
- // // qDebug() << "Fetched fields:" << fieldsArray;
- // } else {
- // qWarning() << "查询 BondHead 失败:" << queryBond.lastError().text();
- // }
- // }
- // }
- // //QString sql = QString("SELECT Id, DirectoryId, Describe, Type, Value,units, Groupld, UserGrade, ChangeFlag FROM %1 WHERE DirectoryId = :dirId").arg(currentTableName);
- // // // 查询 BondHead 表
- // // QSqlQuery queryBond(db);
- // // queryBond.prepare(sql);
- // // queryBond.bindValue(":dirId", directoryId);
- // // if (queryBond.exec()) {
- // // while (queryBond.next()) {
- // // bool ok;
- // // int dbUserGrade = queryBond.value("UserGrade").toString().toInt(&ok, 16);
- // // // 只有当数据库记录的 UserGrade 包含当前用户权限时才加载该字段
- // // if (!hasPrivilege(dbUserGrade, userPrivilege))
- // // continue;
- // // QJsonObject field;
- // // field["Id"] = queryBond.value("Id").toInt();
- // // field["DirectoryId"] = queryBond.value("DirectoryId").toInt();
- // // field["Describe"] = queryBond.value("Describe").toString();
- // // field["Type"] = queryBond.value("Type").toString();
- // // field["Value"] = queryBond.value("Value").toString();
- // // field["units"] = queryBond.value("units").toString();
- // // field["Groupld"] = queryBond.value("Groupld").toInt();
- // // field["ChangeFlag"] = queryBond.value("ChangeFlag").toInt();
- // // fieldsArray.append(field);
- //
- // // // 如果有任一字段 ChangeFlag 为 0,则 allChangeFlag = false
- // // if (queryBond.value("ChangeFlag").toInt() == 0)
- // // allChangeFlag = false;
- // // }
- // // // qDebug() << "Fetched fields:" << fieldsArray;
- // // } else {
- // // qWarning() << "查询 BondHead 失败:" << queryBond.lastError().text();
- // // }
- //
- // // 查询 Buttons 表
- // QSqlQuery queryBtn(db);
- // queryBtn.prepare(R"(
- // SELECT ButtonId, Icon, Text, Enabled, UserGrade
- // FROM Buttons
- // WHERE DirectoryId = :dirId
- // )");
- // queryBtn.bindValue(":dirId", directoryId);
- // if (queryBtn.exec()) {
- // while (queryBtn.next()) {
- // bool ok;
- // int btnUserGrade = queryBtn.value("UserGrade").toString().toInt(&ok, 16);
- // if (!hasPrivilege(btnUserGrade, userPrivilege))
- // continue;
- // QJsonObject button;
- // button["id"] = queryBtn.value("ButtonId").toString();
- // button["icon"] = queryBtn.value("Icon").toString();
- // button["text"] = queryBtn.value("Text").toString();
- // button["enabled"] = queryBtn.value("Enabled").toBool();
- // buttonsArray.append(button);
- // }
- // // qDebug() << "Fetched buttons:" << buttonsArray;
- // } else {
- // qWarning() << "查询 Buttons 失败:" << queryBtn.lastError().text();
- // }
- //
- // data["fields"] = fieldsArray;
- // data["buttons"] = buttonsArray;
- // data["IsThirdLevel"] = true;
- // data["allChangeFlag"] = allChangeFlag;
- // return data;
- //}
- // 事件过滤器,用于自定义绘制虚线
- bool DbTreeViewManager::eventFilter(QObject *watched, QEvent *event)
- {
- // 拦截 Paint 事件
- if (watched == m_pTreeViewDown->viewport() && event->type() == QEvent::Paint)
- {
- // 进行默认绘制
- bool handled = QWidget::eventFilter(watched, event);
- // 使用 QPainter 叠加画“拐角线”
- QPainter painter(m_pTreeViewDown->viewport());
- if (!painter.isActive()) {
- qWarning() << "Painter not active";
- return handled;
- }
- painter.save();
- painter.setPen(QPen(Qt::gray, 1, Qt::DashLine)); // 灰色、1px 宽、虚线
- // 调用递归函数,绘制所有分支
- paintAllBranches(QModelIndex(), painter);
- painter.restore();
- return handled;
- }
- // 其余事件交给父类默认处理
- return QWidget::eventFilter(watched, event);
- }
- // 递归绘制所有分支
- void DbTreeViewManager::paintAllBranches(const QModelIndex &parentIndex, QPainter &painter)
- {
- int rowCount = m_pCModel->rowCount(parentIndex);
- for(int i = 0; i < rowCount; ++i)
- {
- // 当前子节点
- QModelIndex childIndex = m_pCModel->index(i, 0, parentIndex);
- if (!childIndex.isValid()) continue;
- // 1) 父->子拐角线
- drawParentChildLine(childIndex, painter);
- // 2) 兄弟延续竖线(如果本节点不是最后一个兄弟,就在拐点列画条向下的线)
- if (i < rowCount - 1) {
- drawSiblingLine(childIndex, painter);
- }
- // 3) 递归处理子节点
- paintAllBranches(childIndex, painter);
- }
- }
- // 在“父节点 -> 子节点”间画一条“L”型拐角线,调整横向线段的长度
- void DbTreeViewManager::drawParentChildLine(const QModelIndex &childIndex, QPainter &painter)
- {
- QModelIndex parentIndex = childIndex.parent();
- if (!parentIndex.isValid()) {
- // “顶层节点”定义一个固定的起点 (rootX, rootY)
- int indent = m_pTreeViewDown->indentation();
- int depth = 0; // 顶层节点深度为0
- int branchX = (depth + 1) * indent - indent / 2; // 计算 branchX 基于缩进和深度
- // 定义 rootY 为节点中心 Y
- QRect childRect = m_pTreeViewDown->visualRect(childIndex);
- if (!childRect.isValid())
- return;
- int rootY = childRect.center().y();
- // 定义横向偏移量
- const int hOffset = -20;
- // 绘制竖线
- painter.drawLine(QPoint(branchX, rootY),
- QPoint(branchX, childRect.center().y()));
- // 计算新的横线终点
- int newX = childRect.left() + hOffset;
- // 绘制横线
- painter.drawLine(QPoint(branchX, childRect.center().y()),
- QPoint(newX, childRect.center().y()));
- return;
- }
- QRect parentRect = m_pTreeViewDown->visualRect(parentIndex);
- QRect childRect = m_pTreeViewDown->visualRect(childIndex);
- if (!parentRect.isValid() || !childRect.isValid()) {
- // 父或子超出可视区域
- return;
- }
- int pMidY = parentRect.center().y();
- int cMidY = childRect.center().y();
- // 计算节点深度
- int depth = 0;
- QModelIndex p = parentIndex;
- while (p.isValid()) {
- depth++;
- p = p.parent();
- }
- int indent = m_pTreeViewDown->indentation();
- int branchX = depth * indent - indent / 2;
- // branchX 不超出视图范围
- branchX = std::max(branchX, 0);
- // 定义横向偏移量
- const int hOffset = -15;
- // 绘制竖线
- painter.drawLine(QPoint(branchX, pMidY), QPoint(branchX, cMidY));
- // 计算新的横线终点
- int newX = childRect.left() + hOffset;
- // 绘制横线
- painter.drawLine(QPoint(branchX, cMidY), QPoint(newX, cMidY));
- }
- // 节点下面还有兄弟,则在拐点列那里继续往下画竖线
- void DbTreeViewManager::drawSiblingLine(const QModelIndex &childIndex, QPainter &painter)
- {
- QModelIndex parentIndex = childIndex.parent();
- if (!parentIndex.isValid()) {
- return; // 没有父节点
- }
- // 下一个兄弟
- int row = childIndex.row();
- int lastRow = m_pCModel->rowCount(parentIndex) - 1;
- if (row >= lastRow) {
- return; // 说明是最后一个兄弟,不用画延伸线
- }
- QModelIndex nextSibling = m_pCModel->index(row + 1, 0, parentIndex);
- QRect currRect = m_pTreeViewDown->visualRect(childIndex);
- QRect nextRect = m_pTreeViewDown->visualRect(nextSibling);
- if (!currRect.isValid() || !nextRect.isValid()) {
- return;
- }
- // 计算节点深度
- int depth = 0;
- QModelIndex p = parentIndex;
- while (p.isValid()) {
- depth++;
- p = p.parent();
- }
- int indent = m_pTreeViewDown->indentation();
- int branchX = depth * indent - indent / 2;
- // 确保 branchX 不超出视图范围
- branchX = std::max(branchX, 0);
- // 从当前节点底部向下延伸到下一个兄弟节点顶部
- int startY = currRect.bottom();
- int endY = nextRect.top();
- painter.drawLine(QPoint(branchX, startY), QPoint(branchX, endY));
- }
- // 递归查找第一个三级目录项
- QStandardItem* DbTreeViewManager::findFirstThirdLevelItemDFS(QStandardItem *parentItem)
- {
- if (!parentItem) {
- return nullptr;
- }
- // 遍历当前 parentItem 的所有子
- for (int i = 0; i < parentItem->rowCount(); ++i) {
- QStandardItem *child = parentItem->child(i);
- if (!child) continue;
- // 检查这个子节点是否是三级目录
- QVariant data = child->data(Qt::UserRole + 2);
- if (data.canConvert<QJsonObject>()) {
- QJsonObject obj = data.toJsonObject();
- if (obj.contains("IsThirdLevel") && obj["IsThirdLevel"].toBool()) {
- // 找到第一个 IsThirdLevel = true 的节点就返回
- return child;
- }
- }
- // 如果不是 IsThirdLevel,继续往子节点里找
- QStandardItem* deeperFound = findFirstThirdLevelItemDFS(child);
- if (deeperFound) {
- return deeperFound;
- }
- }
- return nullptr;
- }
- void DbTreeViewManager::onItemChanged(QStandardItem *item)
- {
- if (m_blockItemChanged || m_isRestoring) {
- return; // 防止递归和恢复期间触发
- }
- m_blockItemChanged = true;
- Qt::CheckState state = item->checkState();
- // 更新所有子项的复选框状态
- for (int i = 0; i < item->rowCount(); ++i) {
- QStandardItem *child = item->child(i);
- if (child) {
- child->setCheckState(state);
- // 递归更新子项
- onItemChanged(child);
- }
- }
- // 更新所有父项的复选框状态
- QModelIndex parentIndex = item->index().parent();
- while (parentIndex.isValid()) {
- QStandardItem *parentItem = m_pCModel->itemFromIndex(parentIndex);
- if (!parentItem) break;
- int checkedCount = 0;
- int totalCount = parentItem->rowCount();
- for (int i = 0; i < totalCount; ++i) {
- QStandardItem *child = parentItem->child(i);
- if (child && child->checkState() == Qt::Checked) {
- checkedCount++;
- }
- }
- if (checkedCount == totalCount) {
- parentItem->setCheckState(Qt::Checked);
- }
- else {
- parentItem->setCheckState(Qt::Unchecked);
- }
- parentIndex = parentIndex.parent();
- }
- m_blockItemChanged = false;
- // 记录复选框状态
- saveCheckedPaths();
- }
- // 初始化和设置按钮
- void DbTreeViewManager::setupButton()
- {
- // 创建按钮并设置属性
- auto createButton = [&](QPushButton*& button, const QString& iconPath, const QRect& geometry) {
- button = new QPushButton(this);
- button->setIcon(QIcon(iconPath));
- button->setGeometry(geometry);
- button->setStyleSheet(R"(
- QPushButton {
- position: absolute;
- border-radius: 6px;
- opacity: 1;
- background: #FFFFFF;
- border: 1px solid #BABBDC;
- }
- QPushButton:hover {
- background-color: #A9B4FF; /* 鼠标悬停效果 */
- }
- )");
- button->show();
- };
- // 根据原始代码调整按钮的位置和大小
- createButton(ButtonBack, ":/images/home_openFile.png", QRect(328, 6, 76, 30));
- createButton(buttonUp, ":/images/home_up.png", QRect(408, 6, 36, 30));
- createButton(buttonDown, ":/images/home_down.png", QRect(408, 40, 36, 30));
- createButton(buttonLeft, ":/images/home_left_hide.png", QRect(328, 40, 36, 30));
- createButton(buttonRight, ":/images/home_right.png", QRect(368, 40, 36, 30));
- // 连接按钮的点击信号到相应的槽函数
- connect(ButtonBack, &QPushButton::clicked, this, &DbTreeViewManager::onButtonBackClicked);
- connect(buttonUp, &QPushButton::clicked, this, &DbTreeViewManager::onButtonUpClicked);
- connect(buttonDown, &QPushButton::clicked, this, &DbTreeViewManager::onButtonDownClicked);
- connect(buttonLeft, &QPushButton::clicked, this, &DbTreeViewManager::onButtonLeftClicked);
- connect(buttonRight, &QPushButton::clicked, this, &DbTreeViewManager::onButtonRightClicked);
- }
- void DbTreeViewManager::updateButtonsVisibility()
- {
- if (m_currentConfigName == "Home") {
- // Home 界面,隐藏所有按钮
- ButtonBack->hide();
- buttonUp->hide();
- buttonDown->hide();
- buttonLeft->hide();
- buttonRight->hide();
- }
- else {
- // 其他配置,显示所有按钮
- ButtonBack->show();
- buttonUp->show();
- buttonDown->show();
- buttonLeft->show();
- buttonRight->show();
- }
- }
- QStandardItem* DbTreeViewManager::deepCopyItem(const QStandardItem *item){
- if (!item) {
- return nullptr;
- }
- // 创建一个新的 QStandardItem
- QStandardItem *newItem = new QStandardItem(item->text());
- newItem->setIcon(item->icon());
- newItem->setToolTip(item->toolTip());
- newItem->setEditable(item->isEditable());
- newItem->setCheckable(item->isCheckable());
- newItem->setCheckState(item->checkState());
- // 递归复制子项
- for (int i = 0; i < item->rowCount(); ++i) {
- QStandardItem *childItem = item->child(i);
- QStandardItem *newChildItem = deepCopyItem(childItem);
- newItem->appendRow(newChildItem);
- }
- return newItem;
- }
- void DbTreeViewManager::loadpage(const int &configId){
- for(int i=0;i<5;i++){
- if(configId == menuArray[i].menuId){
- // QJsonObject data = fetchThirdLevelData(menuArray[i].index, 0x1);
- //displayThirdLevelFields(data, false);
- }
- }
- }
- void DbTreeViewManager::onTreeViewClicked(const QModelIndex &index)
- {
- QStandardItem *item = m_pCModel->itemFromIndex(index);
- if (!item) return;
- // 是否第三层,提取第三层的目录ID及目录表格名字
- bool isThird = false;
- int dirId = -1;
- QString strModuleType; // 模组
- //QString dirTableName = "";
- QString mutliTableName = "";
- QVariant dataVar = item->data(Qt::UserRole + 2);
- if (dataVar.canConvert<QJsonObject>()) {
- QJsonObject obj = dataVar.toJsonObject();
- qDebug() << " AAkey :" << obj;
- if (obj.contains("IsThirdLevel") && obj["IsThirdLevel"].toBool()) {
- isThird = true;
- if (obj.contains("Id")) {
- dirId = obj["Id"].toInt();
- }
- if (obj.contains("TableName"))
- {
- mutliTableName = obj["TableName"].toString();
- }
- if (obj.contains("ModuleType"))
- {
- strModuleType = obj["ModuleType"].toString();
- m_strModuleTypeAAA = strModuleType;
- }
- }
- }
- updateNavigationBar(index);
- if (isThird) {
- //!!!修改权限
- int userPrivilege = 0x1; // 示例
- if (dirId < 0) {
- qWarning() << "无效directoryId";
- return;
- }
- if (mutliTableName == "")
- {
- qWarning() << "无效directoryId";
- return;
- }
- QList<Table_Control_Data> tableControlDatas;
- bool allChangeFlag = false;
- m_sqlOper->GetThirdDirControlData(mutliTableName, userPrivilege, tableControlDatas, allChangeFlag);
- displayThirdLevelFields(tableControlDatas, allChangeFlag, false); // 传递参数表示 Home 界面
- //QJsonObject data = m_sqlOper.fetchThirdLevelData(mutliTableName, dirId, userPrivilege);
- //QJsonObject data = fetchThirdLevelData(dirId, userPrivilege);
- //displayThirdLevelFields(data, false);
- for(int i=0;i<5;i++){
- if(currentMenuId == menuArray[i].menuId){
- menuArray[i].isthird = true;
- menuArray[i].index = dirId;
- }
- }
- // 将“新页面”压入后退栈, 并清空前进栈
- PageState st;
- st.path = buildItemPath(item);
- st.isThirdLevel = true;
- st.directoryId = dirId;
- st.multiTableName = mutliTableName;
- m_backStack.append(st);
- if(m_backStack.size() >= 2){
- buttonLeft->setIcon(QIcon(":/images/home_left.png"));
- }
- m_forwardStack.clear();
- buttonRight->setIcon(QIcon(":/images/home_right.png"));
- } else {
- // 不是第三层,只是选中目录
- // 清理第三层窗口
- if (m_thirdLevelFieldWnd) {
- m_thirdLevelFieldWnd->close();
- m_thirdLevelFieldWnd->deleteLater();
- m_thirdLevelFieldWnd = nullptr;
- }
- // 同样更新历史
- PageState st;
- st.path = buildItemPath(item);
- st.isThirdLevel = false;
- st.directoryId = -1;
- st.multiTableName = mutliTableName;
- m_backStack.append(st);
- if(m_backStack.size() >= 2){
- buttonLeft->setIcon(QIcon(":/images/home_left.png"));
- }
- m_forwardStack.clear();
- buttonRight->setIcon(QIcon(":/images/home_right.png"));
- }
- }
- void DbTreeViewManager::onTreeViewClicked_updown(const QModelIndex &index){
- QStandardItem *item = m_pCModel->itemFromIndex(index);
- if (!item) return;
- // 是否第三层
- bool isThird = false;
- int dirId = -1;
- QString multiTableName = "";
- QVariant dataVar = item->data(Qt::UserRole + 2);
- if (dataVar.canConvert<QJsonObject>()) {
- QJsonObject obj = dataVar.toJsonObject();
- if (obj.contains("IsThirdLevel") && obj["IsThirdLevel"].toBool()) {
- isThird = true;
- if (obj.contains("Id")) {
- dirId = obj["Id"].toInt();
- }
- if (obj.contains("TableName"))
- {
- multiTableName = obj["TableName"].toString();
- }
- }
- }
- updateNavigationBar(index);
- if (isThird) {
- int userPrivilege = 0x1; // 示例
- if (dirId < 0) {
- qWarning() << "无效directoryId";
- return;
- }
- if (multiTableName == "")
- {
- qWarning() << "无效multiTableName";
- return;
- }
- QList<Table_Control_Data> tableControlDatas;
- bool allChangeFlag = false;
- m_sqlOper->GetThirdDirControlData(multiTableName, userPrivilege, tableControlDatas, allChangeFlag);
- displayThirdLevelFields(tableControlDatas, allChangeFlag, false); // 传递参数表示 Home 界面
- // QJsonObject data = m_sqlOper.fetchThirdLevelData(multiTableName, dirId, userPrivilege);
- //displayThirdLevelFields(data, false);
- // QJsonObject data = fetchThirdLevelData(dirId, userPrivilege);
- // 将“新页面”压入后退栈, 并清空前进栈
- PageState st;
- st.path = buildItemPath(item);
- st.isThirdLevel = true;
- st.directoryId = dirId;
- st.multiTableName = multiTableName;
- m_backStack.append(st);
- if(m_backStack.size() >= 2){
- buttonLeft->setIcon(QIcon(":/images/home_left.png"));
- }
- m_forwardStack.clear();
- buttonRight->setIcon(QIcon(":/images/home_right.png"));
- } else {
- // 不是第三层,只是选中目录
- // 清理第三层窗口
- if (m_thirdLevelFieldWnd) {
- m_thirdLevelFieldWnd->close();
- m_thirdLevelFieldWnd->deleteLater();
- m_thirdLevelFieldWnd = nullptr;
- }
- // 同样更新历史
- PageState st;
- st.path = buildItemPath(item);
- st.isThirdLevel = false;
- st.directoryId = -1;
- st.multiTableName = multiTableName;
- m_backStack.append(st);
- if(m_backStack.size() >= 2){
- buttonLeft->setIcon(QIcon(":/images/home_left.png"));
- }
- m_forwardStack.clear();
- buttonRight->setIcon(QIcon(":/images/home_right.png"));
- }
- }
- void DbTreeViewManager::updateNavigationBar(const QModelIndex &index)
- {
- QStandardItem *item = m_pCModel->itemFromIndex(index);
- if (!item) {
- qWarning() << "导航栏更新失败:未找到对应项";
- return;
- }
- // 清理现有布局
- if (m_pNavigationWidget->layout()) {
- QLayoutItem *child;
- while ((child = m_pNavigationWidget->layout()->takeAt(0)) != nullptr) {
- if (child->widget()) {
- child->widget()->deleteLater();
- }
- delete child;
- }
- delete m_pNavigationWidget->layout();
- }
- // 构建路径列表,从当前项回溯到根节点
- QList<QStandardItem *> path;
- QStandardItem *temp = item;
- while (temp) {
- path.prepend(temp); // 从根节点开始
- temp = temp->parent();
- }
- QVBoxLayout *newLayout = new QVBoxLayout; // 创建新的导航栏布局
- newLayout->setContentsMargins(0, 0, 3, 3);
- newLayout->setSpacing(0);
- // 确保显示三行
- for (int i = 0; i < 3; ++i) {
- QLabel *label = new QLabel;
- if (i < path.size()) {
- QString text = path[i]->text();
- if (i == 1) text = " " + text; // 二级目录缩进
- if (i == 2) text = " " + text; // 三级目录缩进
- label->setText(text);
- } else {
- label->setText(""); // 填充空白行
- }
- QFont font = label->font(); // 设置字体和样式
- font.setPointSize(14);
- font.setFamily("思源黑体");
- font.setBold(true); // 设置文字为粗体
- font.setLetterSpacing(QFont::PercentageSpacing,105); // 设置字间距100%为默认
- label->setFont(font);
- label->setFixedHeight(m_pNavigationWidget->height() / 3);
- newLayout->addWidget(label);
- }
- // 设置布局并更新导航栏
- m_pNavigationWidget->setLayout(newLayout);
- m_pNavigationWidget->update();
- }
- void DbTreeViewManager::clearAllSeparators()
- {
- // 遍历 firstLevelSeparators 中所有 QFrame,
- // 隐藏后调用 deleteLater()
- for (auto separator : firstLevelSeparators.values()) {
- if(separator) {
- separator->hide();
- separator->deleteLater();
- }
- }
- // 清空映射
- firstLevelSeparators.clear();
- }
- void DbTreeViewManager::updateSeparatorLine()
- {
- // 如果目录树被隐藏,就把分割线都藏起来
- if (!m_pTreeViewDown->isVisible()) {
- for (auto it = firstLevelSeparators.begin(); it != firstLevelSeparators.end(); ++it) {
- QFrame *sep = it.value();
- if (sep) sep->hide();
- }
- return;
- }
- // 遍历所有“一级目录”与其分隔线
- for (auto it = firstLevelSeparators.begin(); it != firstLevelSeparators.end(); ++it) {
- QStandardItem *firstLevelItem = it.key();
- QFrame *separator = it.value();
- if (!firstLevelItem || !separator) continue;
- // 拿到可视区域
- QModelIndex firstLevelIndex = m_pCModel->indexFromItem(firstLevelItem);
- QRect firstLevelRect = m_pTreeViewDown->visualRect(firstLevelIndex);
- // 如果这个一级目录滚动到看不见了,就把分割线也藏起来
- if (!firstLevelRect.isValid()) {
- separator->hide();
- continue;
- }
- // 如果展开并且它有子节点,就找“最后一个可见子项”
- if (m_pTreeViewDown->isExpanded(firstLevelIndex) && firstLevelItem->hasChildren()) {
- QModelIndex lastVisibleChild = findLastVisibleChild(firstLevelIndex);
- if (lastVisibleChild.isValid()) {
- QRect lastChildRect = m_pTreeViewDown->visualRect(lastVisibleChild);
- if (lastChildRect.isValid()) {
- // 将分割线放在最后一个子项下面
- separator->setGeometry(16,lastChildRect.bottom() + 115, m_pOriginalWndMenuPage->width() - 40,1);
- separator->show();
- continue;
- }
- }
- }
- // 如果收起或找不到可见子项,就放在本一级目录下面
- separator->setGeometry(16,firstLevelRect.bottom() + 115, m_pOriginalWndMenuPage->width() - 40,1);
- separator->show();
- }
- }
- QModelIndex DbTreeViewManager::findLastVisibleChild(const QModelIndex &parentIndex) {
- if (!parentIndex.isValid()) return QModelIndex();
- int childCount = m_pCModel->rowCount(parentIndex);
- QModelIndex lastVisible;
- for (int i = childCount - 1; i >= 0; --i) {
- QModelIndex childIndex = m_pCModel->index(i, 0, parentIndex);
- if (!m_pTreeViewDown->isRowHidden(i, parentIndex)) {
- if (m_pTreeViewDown->isExpanded(childIndex) && m_pCModel->rowCount(childIndex) > 0) {
- QModelIndex deeper = findLastVisibleChild(childIndex);
- if (deeper.isValid()) {
- return deeper;
- }
- }
- lastVisible = childIndex;
- break;
- }
- }
- return lastVisible;
- }
- //void DbTreeViewManager::displayThirdLevelFields(const QJsonObject &data, bool isHome)
- //{
- // if (data.isEmpty()) {
- // qWarning() << "字段数据为空,无法显示";
- // return;
- // }
- //
- // if (!data.contains("IsThirdLevel") || !data["IsThirdLevel"].toBool()) {
- // qWarning() << "不是三级目录,跳过按钮加载";
- // return;
- // }
- //
- // bool allChangeFlag = false;
- // if (data.contains("allChangeFlag") && data["allChangeFlag"].isBool()) {
- // allChangeFlag = data["allChangeFlag"].toBool();
- // }
- // else {
- // qWarning() << "数据中缺少 'allChangeFlag' 字段或类型不正确";
- // }
- //
- // // 若已有第三层窗口,先关闭并释放
- // if (m_thirdLevelFieldWnd) {
- // m_thirdLevelFieldWnd->close();
- // m_thirdLevelFieldWnd->deleteLater();
- // m_thirdLevelFieldWnd = nullptr;
- // }
- //
- // // 获取当前选中的目录项,设置状态
- // QModelIndex currentIndex = treeViewDown->currentIndex();
- // if (currentIndex.isValid()) {
- // QStandardItem* currentItem = model->itemFromIndex(currentIndex);
- // if (currentItem) {
- // currentItem->setCheckState(allChangeFlag ? Qt::Checked : Qt::Unchecked);
- // qDebug() << "设置当前目录项复选框状态为:" << (allChangeFlag ? "Checked" : "Unchecked");
- // }
- // }
- // else {
- // qWarning() << "当前没有选中的目录项";
- // }
- //
- // // 隐藏目录树及所有分隔线
- // treeViewDown->hide();
- // for (auto separator : firstLevelSeparators) {
- // if (separator)
- // separator->hide();
- // }
- //
- // // 显示按钮
- // loadButtonConfigForThirdLevel(data);
- //
- // // 清理之前的控件列表
- // m_fieldWidgets.clear();
- //
- // // 创建新的 "字段展示" 窗口,并赋值给 m_thirdLevelFieldWnd
- // m_thirdLevelFieldWnd = new QWidget(widget2);
- // m_thirdLevelFieldWnd->setWindowTitle("字段展示");
- // m_thirdLevelFieldWnd->setStyleSheet("background-color: transparent;");
- // if (isHome) {
- // // Home 界面,填满 widget2
- // m_thirdLevelFieldWnd->setGeometry(0, 0, widget2->width(), widget2->height());
- // }
- // else {
- // // 其他配置界面,设置与 treeViewDown 相同的几何位置
- // m_thirdLevelFieldWnd->setGeometry(treeViewDown->geometry());
- // }
- //
- // // 创建滚动区域
- // QScrollArea *scrollArea = new QScrollArea(m_thirdLevelFieldWnd);
- // scrollArea->setWidgetResizable(true);
- // scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
- //
- // // 创建滚动容器并设置布局
- // QWidget *scrollWidget = new QWidget;
- // scrollWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
- // QVBoxLayout *scrollLayout = new QVBoxLayout(scrollWidget);
- // scrollLayout->setSpacing(10);
- // scrollLayout->setContentsMargins(10, 10, 10, 10);
- //
- //
- // //!!!提成一个类
- // int previousGroupId = -1;
- // // 遍历字段数组
- // if (data.contains("fields") && data["fields"].isArray()) {
- // QJsonArray fields = data["fields"].toArray();
- // for (const QJsonValue &val : fields) {
- // QJsonObject field = val.toObject();
- // int fieldId = field["Id"].toInt();
- // QString fieldName = field["Name"].toString();
- // QString fieldType = field["Type"].toString();
- // QString fieldValue = field["Value"].toString();
- // //QString fieldCurrentValue = field["CurrentValue"].toString();
- // QString fieldTableName = field["TableName"].toString();
- // QString fieldUpLimit = field["UpLimit"].toString();
- // QString fieldDownLimit = field["DownLimit"].toString();
- // QString fieldUnits = field["units"].toString();
- // int groupId = field["Groupld"].toInt();
- //
- // // 如果组变化,插入分隔线
- // if (previousGroupId != -1 && groupId != previousGroupId) {
- // QFrame *separator = createUnifiedSeparator(scrollWidget, 2);
- // QHBoxLayout *separatorLayout = new QHBoxLayout;
- // separatorLayout->setContentsMargins(0, 5, 0, 5);
- // separatorLayout->addWidget(separator);
- // scrollLayout->addLayout(separatorLayout);
- // separator->show();
- // }
- // previousGroupId = groupId;
- //
- // // 每个字段一行
- // QHBoxLayout *fieldLayout = new QHBoxLayout;
- // fieldLayout->setSpacing(5);
- //
- // QLabel *label = new QLabel(fieldName);
- // label->setAlignment(Qt::AlignVCenter | Qt::AlignLeft);
- // label->setFixedHeight(24);
- // label->setMinimumWidth(120);
- // fieldLayout->addWidget(label);
- // fieldLayout->addStretch(1);
- //
- // QWidget *rightWidget = new QWidget;
- // QHBoxLayout *rightLayout = new QHBoxLayout(rightWidget);
- // rightLayout->setContentsMargins(0, 0, 20, 0);
- // rightLayout->setSpacing(5);
- //
- // QWidget *createdWidget = nullptr;
- //
- // if (fieldType == "input") {
- // // 检查 Value 中是否包含逗号分隔(比如 "请输入内容1,分钟")
- // if (fieldUnits!= "") {
- // // // 采用逗号分割,trim去除空白
- // // QStringList parts = fieldValue.split(",", Qt::SkipEmptyParts);
- // // QString leftText = parts.at(0).trimmed();
- // // QString rightText = (parts.size() > 1 ? parts.at(1).trimmed() : QString());
- //
- // // 创建 QLineEdit 显示左边文本
- // QLineEdit *lineEdit = new QLineEdit();
- // if((fieldUpLimit=="")&&(fieldDownLimit=="")){
- // lineEdit->setText(fieldValue);
- // lineEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
- // lineEdit->setFixedSize(105, 28);
- // lineEdit->setAlignment(Qt::AlignLeft);
- // //!!!提到前面
- // lineEdit->setStyleSheet(R"(
- // QLineEdit {
- // background: #FFFFFF;
- // border: 1px solid #BABBDC;
- // border-radius: 6px;
- // padding: 2px 5px;
- // }
- // )");
- // }else{//上下限至少设置了一个值
- // if(fieldValue == ""){
- // lineEdit->setPlaceholderText("请输入数字");
- // }else{
- // lineEdit->setText(fieldValue);
- // }
- //
- //
- // lineEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
- // lineEdit->setFixedSize(105, 28);
- // lineEdit->setAlignment(Qt::AlignLeft);
- // lineEdit->setStyleSheet(R"(
- // QLineEdit {
- // background: #FFFFFF;
- // border: 1px solid #BABBDC;
- // border-radius: 6px;
- // padding: 2px 5px;
- // }
- // )");
- // }
- //
- // // 创建 QLabel 显示右边文本(比如 "分钟")
- // QLabel *unitLabel = new QLabel(fieldUnits);
- // unitLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
- // unitLabel->setFixedHeight(28);
- // unitLabel->setStyleSheet("QLabel { font-size: 14px; }");
- //
- // // 把 QLineEdit 和 QLabel 水平排列,确保 Qlabel 显示在 QLineEdit 右侧
- // QHBoxLayout *inputLayout = new QHBoxLayout;
- // inputLayout->setSpacing(10);
- // inputLayout->setContentsMargins(0,0,0,0);
- // inputLayout->addWidget(lineEdit);
- // inputLayout->addWidget(unitLabel);
- //
- // // 用一个容器部件来装下这个布局
- // QWidget *inputWidget = new QWidget;
- // inputWidget->setLayout(inputLayout);
- // rightLayout->addWidget(inputWidget);
- //
- // // 保存控件指针到 m_fieldWidgets(可选)
- // m_fieldWidgets.append(lineEdit);
- // m_fieldWidgets.append(unitLabel);
- // createdWidget = inputWidget;
- // // // 连接 textChanged 信号到 lambda 函数
- // connect(lineEdit, &QLineEdit::textChanged, [this,lineEdit,fieldTableName,fieldId,fieldUpLimit,fieldDownLimit]() {
- // if((fieldUpLimit!="")&&(fieldDownLimit!="")){
- // int uplimit = fieldUpLimit.toInt();
- // int downlimit = fieldDownLimit.toInt();
- // int inputvalue = (lineEdit->text()).toInt();
- // if((inputvalue<=uplimit)&&(inputvalue>=downlimit)){
- // updateDb(fieldTableName,fieldId,lineEdit->text());
- // }else{
- // lineEdit->setPlaceholderText("超出设定范围,请重新输入");
- // }
- // }else if(fieldDownLimit!=""){
- // int downlimit = fieldDownLimit.toInt();
- // int inputvalue = (lineEdit->text()).toInt();
- // if(inputvalue>=downlimit){
- // updateDb(fieldTableName,fieldId,lineEdit->text());
- // }else{
- // lineEdit->setPlaceholderText("超出设定范围,请重新输入");
- // }
- // }else if(fieldUpLimit!=""){
- // int uplimit = fieldUpLimit.toInt();
- // int inputvalue = (lineEdit->text()).toInt();
- // if(inputvalue<=uplimit){
- // updateDb(fieldTableName,fieldId,lineEdit->text());
- // }else{
- // lineEdit->setPlaceholderText("超出设定范围,请重新输入");
- // }
- // }else{
- // updateDb(fieldTableName,fieldId,lineEdit->text());
- // }
- //
- // });
- // } else {
- // // 如果不包含逗号,按原有逻辑只创建 QLineEdit
- // QLineEdit *lineEdit1 = new QLineEdit(fieldValue);
- // lineEdit1->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
- // lineEdit1->setFixedSize(140, 28);
- // lineEdit1->setAlignment(Qt::AlignLeft);
- // lineEdit1->setStyleSheet(R"(
- // QLineEdit {
- // background: #FFFFFF;
- // border: 1px solid #BABBDC;
- // border-radius: 6px;
- // padding: 2px 5px;
- // }
- // )");
- // rightLayout->addWidget(lineEdit1);
- // createdWidget = lineEdit1;
- // // // 连接 textChanged 信号到 lambda 函数
- // connect(lineEdit1, &QLineEdit::textChanged, [this,lineEdit1,fieldTableName,fieldId]() {
- // updateDb(fieldTableName,fieldId,lineEdit1->text());
- // loginput(fieldTableName,fieldId,lineEdit1->text());
- // });
- // }
- // }
- // else if (fieldType == "radio") {
- // // 用逗号分隔的选项
- // QStringList optionList = fieldValue.split(QRegExp("[,,]"), Qt::SkipEmptyParts);
- // QHBoxLayout *radioLayout = new QHBoxLayout;
- // radioLayout->setSpacing(5);
- // QButtonGroup *radioGroup = new QButtonGroup(rightWidget);
- // for (const QString &optionText : optionList) {
- // QString trimmedOpt = optionText.trimmed();
- // if (trimmedOpt.isEmpty()) continue;
- // QRadioButton *radioButton = new QRadioButton(trimmedOpt);
- // radioGroup->addButton(radioButton);
- // radioLayout->addWidget(radioButton);
- // m_fieldWidgets.append(radioButton);
- // if(fieldValue == trimmedOpt){
- // radioButton->setChecked(true);
- // }
- // }
- // // 使用Lambda函数连接 buttonToggled 信号
- // connect(radioGroup, static_cast<void (QButtonGroup::*)(QAbstractButton *, bool)>(&QButtonGroup::buttonToggled),[this,fieldTableName,fieldId](QAbstractButton *button,bool checked) {
- // if (checked) {
- // // qDebug() << "Button toggled:" << button->text();
- // // 在这里添加你想要执行的操作
- // updateDb(fieldTableName,fieldId,button->text());
- // }
- // });
- // rightLayout->addLayout(radioLayout);
- // }
- // else if (fieldType == "checkbox") {
- // bool isChecked;
- // if(fieldValue == ""){
- // isChecked = (fieldValue == "1");
- // }else{
- // isChecked = (fieldValue == "1");
- // }
- //
- // QCheckBox *checkBox = new QCheckBox;
- // checkBox->setChecked(isChecked);
- // checkBox->setStyleSheet(R"(
- // QCheckBox::indicator {
- // width: 20px;
- // height: 20px;
- // }
- // QCheckBox::indicator:unchecked {
- // background-color: #FFFFFF;
- // border: 1px solid #BABBDC;
- // border-radius: 2px;
- // }
- // QCheckBox::indicator:checked {
- // image: url(:/images/three_Selecte.png);
- // }
- // QCheckBox {
- // spacing: 0px;
- // }
- // )");
- // rightLayout->addWidget(checkBox);
- // createdWidget = checkBox;
- // connect(checkBox, &QCheckBox::stateChanged, [this,fieldTableName,fieldId](int state) {
- // if (state == Qt::Checked) {
- // // qDebug() << "CheckBox is checked"<<fieldId;
- // updateDb(fieldTableName,fieldId,"1");
- // } else if (state == Qt::Unchecked) {
- // // qDebug() << "CheckBox is unchecked"<<fieldId;
- // // 在这里添加你想要执行的操作
- // updateDb(fieldTableName,fieldId,"0");
- // } else if (state == Qt::PartiallyChecked) {
- // qDebug() << "CheckBox is partially checked";
- // // 在这里添加你想要执行的操作
- // }
- // });
- // }
- // else if (fieldType == "ComboBox") {
- // QStringList optionList = fieldValue.split(QRegExp("[,,]"), Qt::SkipEmptyParts);
- // QComboBox *comboBox = new QComboBox;
- // comboBox->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
- // comboBox->setFixedSize(140, 28);
- // comboBox->setStyleSheet(R"(
- // QComboBox {
- // background: #FFFFFF;
- // border: 1px solid #BABBDC;
- // border-radius: 6px;
- // padding: 2px 5px;
- // }
- // QComboBox::drop-down {
- // width: 20px;
- // }
- // )");
- // // int newIndex = -1;
- // for (const QString &opt : optionList) {
- // QString trimmedOpt = opt.trimmed();
- // if (!trimmedOpt.isEmpty())
- // comboBox->addItem(trimmedOpt);
- // }
- // if(fieldValue == ""){
- // comboBox->setCurrentIndex(0);
- // }else{
- // comboBox->setCurrentIndex(fieldValue.toInt());
- // }
- // rightLayout->addWidget(comboBox);
- // createdWidget = comboBox;
- // // 使用Lambda函数连接 currentIndexChanged 信号
- // connect(comboBox, QOverload<int>::of(&QComboBox::currentIndexChanged), [this,comboBox,fieldTableName,fieldId](int index) {
- // QString selectedText = comboBox->itemText(index);
- // QString indexString = QString::number(index);
- // // qDebug() << "ComboBox index changed to:" << index << "Text:" << selectedText;
- // updateDb(fieldTableName,fieldId,indexString);
- // // 在这里添加你想要执行的操作
- // });
- // }
- // else if (fieldType == "time") {
- // QTimeEdit *timeEdit = new QTimeEdit;
- // timeEdit->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
- // timeEdit->setFixedSize(120, 28);
- // timeEdit->setStyleSheet(R"(
- // QTimeEdit {
- // background: #FFFFFF;
- // border: 1px solid #BABBDC;
- // border-radius: 6px;
- // padding: 2px 5px;
- // }
- // )");
- // timeEdit->setDisplayFormat("HH:mm:ss");
- // timeEdit->setTime(QTime::fromString(fieldValue, "HH:mm:ss"));
- // rightLayout->addWidget(timeEdit);
- // createdWidget = timeEdit;
- // }
- // else if (fieldType == "switch") {
- // QWidget *switchContainer = new QWidget;
- // QHBoxLayout *switchLayout = new QHBoxLayout(switchContainer);
- // switchLayout->setSpacing(5);
- // switchLayout->setContentsMargins(0, 0, 0, 0);
- // QCheckBox *switchBox = new QCheckBox;
- // switchBox->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
- // switchBox->setFixedSize(30, 30);
- // switchBox->setStyleSheet(R"(
- // QCheckBox::indicator {
- // width: 30px;
- // height: 30px;
- // }
- // QCheckBox::indicator:unchecked {
- // background-color: #BABBDC;
- // border-radius: 6px;
- // }
- // QCheckBox::indicator:checked {
- // background-color: #4CAF50;
- // border-radius: 6px;
- // }
- // )");
- // QString switchValue;
- //
- // if(fieldValue ==""){
- // switchValue = fieldValue.toLower();
- // }else{
- // switchValue = fieldValue.toLower();
- // }
- // if (switchValue == "on")
- // switchBox->setChecked(true);
- // else
- // switchBox->setChecked(false);
- // QLabel *switchLabel = new QLabel(switchBox->isChecked() ? "开" : "关");
- // switchLabel->setAlignment(Qt::AlignVCenter | Qt::AlignLeft);
- // switchLabel->setStyleSheet("font-size: 14px;");
- // connect(switchBox, &QCheckBox::stateChanged, [fieldTableName,fieldId,switchLabel, this](int state){
- // if (state == Qt::Checked){
- // switchLabel->setText("开");
- // updateDb(fieldTableName,fieldId,"on");
- // }else{
- // switchLabel->setText("关");
- // updateDb(fieldTableName,fieldId,"off");
- // }
- // });
- // switchLayout->addWidget(switchBox);
- // switchLayout->addWidget(switchLabel);
- // rightLayout->addWidget(switchContainer);
- // createdWidget = switchBox;
- // }
- // else if (fieldType == "combined") {
- // // 用逗号分隔
- // QStringList parts = fieldValue.split(QRegExp("[,,]"), Qt::SkipEmptyParts);
- // QString inputText = (parts.size() >= 1 ? parts.at(0).trimmed() : QString());
- // QString button1Text = (parts.size() >= 2 ? parts.at(1).trimmed() : QStringLiteral("获取"));
- // QString button2Text = (parts.size() >= 3 ? parts.at(2).trimmed() : QStringLiteral("设置"));
- //
- // QLineEdit *comboInput = new QLineEdit(inputText);
- // comboInput->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
- // comboInput->setFixedHeight(28);
- // comboInput->setStyleSheet(R"(
- // QLineEdit {
- // background: #FFFFFF;
- // border: 1px solid #BABBDC;
- // border-radius: 5px;
- // padding: 2px 5px;
- // }
- // )");
- // m_fieldWidgets.append(comboInput);
- //
- // QWidget *combinedWidget = new QWidget;
- // QHBoxLayout *combinedLayout = new QHBoxLayout(combinedWidget);
- // combinedLayout->setSpacing(5);
- // combinedLayout->setContentsMargins(0, 0, 0, 0);
- //
- // QPushButton *btnGet = new QPushButton(button1Text, combinedWidget);
- // btnGet->setFixedSize(80, 28);
- // btnGet->setStyleSheet(R"(
- // QPushButton {
- // background: #FFFFFF;
- // border: 1px solid #BABBDC;
- // border-radius: 5px;
- // }
- // QPushButton:hover {
- // background-color: #F0F0F0;
- // }
- // )");
- // m_fieldWidgets.append(btnGet);
- //
- // QPushButton *btnSet = new QPushButton(button2Text, combinedWidget);
- // btnSet->setFixedSize(80, 28);
- // btnSet->setStyleSheet(R"(
- // QPushButton {
- // background: #FFFFFF;
- // border: 1px solid #BABBDC;
- // border-radius: 5px;
- // }
- // QPushButton:hover {
- // background-color: #F0F0F0;
- // }
- // )");
- // m_fieldWidgets.append(btnSet);
- //
- // combinedLayout->addWidget(comboInput);
- // combinedLayout->addWidget(btnGet);
- // combinedLayout->addWidget(btnSet);
- // combinedLayout->addStretch();
- // rightLayout->addWidget(combinedWidget);
- // }
- // else if (fieldType == "Text")
- // {
- // QLabel* label = new QLabel();
- // label->setText(fieldValue);
- // label->setAlignment(Qt::AlignVCenter | Qt::AlignLeft);
- // label->setFixedHeight(24);
- // label->setMinimumWidth(120);
- // rightLayout->addWidget(label);
- //
- // } else
- // {
- // qWarning() << "未知字段类型:" << fieldType;
- // }
- //
- // if (createdWidget)
- // m_fieldWidgets.append(createdWidget);
- //
- // fieldLayout->addWidget(rightWidget);
- // scrollLayout->addLayout(fieldLayout);
- // }
- // }
- //
- // scrollLayout->addStretch();
- //
- // scrollArea->setWidget(scrollWidget);
- //
- // QVBoxLayout *mainLayout = new QVBoxLayout(m_thirdLevelFieldWnd);
- // mainLayout->setContentsMargins(0, 0, 0, 0);
- // mainLayout->addWidget(scrollArea);
- //
- // m_thirdLevelFieldWnd->show();
- //}
- QString extractBeforeComma(const QString& input)
- {
- int commaPos = input.indexOf(',');
- if (commaPos != -1) {
- return input.left(commaPos);
- }
- return input;
- }
- void DbTreeViewManager::displayThirdLevelFields(const QList<Table_Control_Data>& data, bool allChangeFlag, bool isHome)
- {
- if (data.isEmpty())
- {
- qWarning() << "字段数据为空,无法显示";
- return;
- }
- //遍历得到结果allChangeFalg
- // 若已有第三层窗口,先关闭并释放
- if (m_thirdLevelFieldWnd)
- {
- m_thirdLevelFieldWnd->close();
- m_thirdLevelFieldWnd->deleteLater();
- m_thirdLevelFieldWnd = nullptr;
- }
- // 获取当前选中的目录项,设置状态
- QModelIndex currentIndex = m_pTreeViewDown->currentIndex();
- if (currentIndex.isValid())
- {
- QStandardItem* currentItem = m_pCModel->itemFromIndex(currentIndex);
- if (currentItem)
- {
- currentItem->setCheckState(allChangeFlag ? Qt::Checked : Qt::Unchecked);
- qDebug() << "设置当前目录项复选框状态为:" << (allChangeFlag ? "Checked" : "Unchecked");
- }
- } else
- {
- qWarning() << "当前没有选中的目录项";
- }
- // 隐藏目录树及所有分隔线
- m_pTreeViewDown->hide();
- for (auto separator : firstLevelSeparators)
- {
- if (separator)
- separator->hide();
- }
- // 清理之前的控件列表
- m_fieldWidgets.clear();
- // 创建新的 "字段展示" 窗口,并赋值给 m_thirdLevelFieldWnd
- m_thirdLevelFieldWnd = new QWidget(m_pOriginalWndMenuPage);
- m_thirdLevelFieldWnd->setWindowTitle("字段展示");
- m_thirdLevelFieldWnd->setStyleSheet("background-color: transparent;");
- if (isHome)
- {
- // Home 界面,填满 widget2
- m_thirdLevelFieldWnd->setGeometry(0, 0, m_pOriginalWndMenuPage->width(), m_pOriginalWndMenuPage->height());
- } else
- {
- // 其他配置界面,设置与 treeViewDown 相同的几何位置
- m_thirdLevelFieldWnd->setGeometry(m_pTreeViewDown->geometry());
- }
- // 创建滚动区域
- QScrollArea* scrollArea = new QScrollArea(m_thirdLevelFieldWnd);
- scrollArea->setWidgetResizable(true);
- scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
- // 创建滚动容器并设置布局
- QWidget* scrollWidget = new QWidget;
- scrollWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
- QVBoxLayout* scrollLayout = new QVBoxLayout(scrollWidget);
- scrollLayout->setSpacing(10);
- scrollLayout->setContentsMargins(10, 10, 10, 10);
- QHBoxLayout* fieldLayout = new QHBoxLayout;
- fieldLayout->setSpacing(5);
- QPushButton* btnSave = new QPushButton(QStringLiteral("保存"));
- btnSave->setFixedSize(80, 28);
- btnSave->setStyleSheet(R"(
- QPushButton {
- background: #FFFFFF;
- border: 1px solid #BABBDC;
- border-radius: 5px;
- }
- QPushButton:hover {
- background-color: #F0F0F0;
- }
- )");
- m_fieldWidgets.append(btnSave);
- connect(btnSave, &QPushButton::clicked, this, [=]() {
- if (modifiedLabels.isEmpty()) {
- QMessageBox::information(this, "提示", "没有任何组件被修改!");
- return;
- }
- int ret = QMessageBox::question(this, "保存确认", "检测到修改内容,是否保存?",
- QMessageBox::Yes | QMessageBox::No);
- if (ret == QMessageBox::Yes) {
- emit saveClicked();
- }
- });
- fieldLayout->addWidget(btnSave);
- scrollLayout->addLayout(fieldLayout);
- QFrame* separator = createUnifiedSeparator(scrollWidget, 2);
- QHBoxLayout* separatorLayout = new QHBoxLayout;
- separatorLayout->setContentsMargins(0, 5, 0, 5);
- separatorLayout->addWidget(separator);
- scrollLayout->addLayout(separatorLayout);
- separator->show();
- //不同表格的全部插进来
- int previousGroupId = -1;
- QList<CONFIG_BASE_STRUCT> buttonControls;
- for (const Table_Control_Data& tableData : data)
- {
- //遍历控件list
- for (const CONFIG_BASE_STRUCT& control : tableData.controlDataList)
- {
- //判断特殊的按钮控件
- if (control.strType == "Button")
- {
- buttonControls.append(control);
- }
- else
- {
- //先创建label
- //int fieldId = control.iId;
- QString fieldName = QString::fromStdString(control.strName);
- QString fieldDescribe = QString::fromStdString(control.strDescribe);
- QString fieldType = QString::fromStdString(control.strType);
- QString fieldValue = QString::fromStdString(control.strValue);
- QString fieldDefult = QString::fromStdString(control.strDefult);
- QString fieldUpLimit = QString::fromStdString(control.strUpperLimit);
- QString fieldDownLimit = QString::fromStdString(control.strLowerLimit);
- QString fieldUnits = QString::fromStdString(control.strUnit);
- QString strAxisType = QString::fromStdString(control.strAxisType);
- QString strstrModuleType = m_strModuleTypeAAA;
- int groupId = control.iGroupId;
- QString fieldTableName = extractBeforeComma(tableData.tableName);
- // 如果组变化,插入分隔线
- if (previousGroupId != -1 && groupId != previousGroupId)
- {
- QFrame* separator = createUnifiedSeparator(scrollWidget, 2);
- QHBoxLayout* separatorLayout = new QHBoxLayout;
- separatorLayout->setContentsMargins(0, 5, 0, 5);
- separatorLayout->addWidget(separator);
- scrollLayout->addLayout(separatorLayout);
- separator->show();
- }
- previousGroupId = groupId;
- // 每个字段一行
- QHBoxLayout* fieldLayout = new QHBoxLayout;
- fieldLayout->setSpacing(5);
- QLabel* label = new QLabel(fieldDescribe);
- label->setAlignment(Qt::AlignVCenter | Qt::AlignLeft);
- label->setFixedHeight(24);
- label->setMinimumWidth(120);
- fieldLayout->addWidget(label);
- fieldLayout->addStretch(1);
- QWidget* rightWidget = new QWidget;
- QHBoxLayout* rightLayout = new QHBoxLayout(rightWidget);
- rightLayout->setContentsMargins(0, 0, 20, 0);
- rightLayout->setSpacing(5);
- QWidget* createdWidget = nullptr;
- if (fieldType == "input" || fieldType == "LONG" || fieldType == "STRING")
- {
- // 检查 Value 中是否包含逗号分隔(比如 "请输入内容1,分钟")
- if (fieldUnits != "")
- {
- // // 采用逗号分割,trim去除空白
- // QStringList parts = fieldValue.split(",", Qt::SkipEmptyParts);
- // QString leftText = parts.at(0).trimmed();
- // QString rightText = (parts.size() > 1 ? parts.at(1).trimmed() : QString());
- // 创建 QLineEdit 显示左边文本
- QLineEdit* lineEdit = new QLineEdit();
- if ((fieldUpLimit == "") && (fieldDownLimit == ""))
- {
- lineEdit->setText(fieldValue);
- lineEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
- lineEdit->setFixedSize(105, 28);
- lineEdit->setAlignment(Qt::AlignLeft);
- //!!!提到前面
- lineEdit->setStyleSheet(R"(
- QLineEdit {
- background: #FFFFFF;
- border: 1px solid #BABBDC;
- border-radius: 6px;
- padding: 2px 5px;
- }
- )");
- } else
- {//上下限至少设置了一个值
- if (fieldValue == "")
- {
- lineEdit->setPlaceholderText(tr("Please input number","请输入数字"));
- } else
- {
- lineEdit->setText(fieldValue);
- }
- lineEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
- lineEdit->setFixedSize(105, 28);
- lineEdit->setAlignment(Qt::AlignLeft);
- lineEdit->setStyleSheet(R"(
- QLineEdit {
- background: #FFFFFF;
- border: 1px solid #BABBDC;
- border-radius: 6px;
- padding: 2px 5px;
- }
- )");
- }
- // 创建 QLabel 显示右边文本(比如 "分钟")
- QLabel* unitLabel = new QLabel(fieldUnits);
- unitLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
- unitLabel->setFixedHeight(28);
- unitLabel->setStyleSheet("QLabel { font-size: 14px; }");
- // 把 QLineEdit 和 QLabel 水平排列,确保 Qlabel 显示在 QLineEdit 右侧
- QHBoxLayout* inputLayout = new QHBoxLayout;
- inputLayout->setSpacing(10);
- inputLayout->setContentsMargins(0, 0, 0, 0);
- inputLayout->addWidget(lineEdit);
- inputLayout->addWidget(unitLabel);
- // 用一个容器部件来装下这个布局
- QWidget* inputWidget = new QWidget;
- inputWidget->setLayout(inputLayout);
- rightLayout->addWidget(inputWidget);
- // 保存控件指针到 m_fieldWidgets(可选)
- m_fieldWidgets.append(lineEdit);
- m_fieldWidgets.append(unitLabel);
- createdWidget = inputWidget;
- // // 连接 textChanged 信号到 lambda 函数
- //connect(lineEdit, &QLineEdit::textChanged, [this, lineEdit, fieldTableName, fieldName, fieldUpLimit, fieldDownLimit]() {
- // if ((fieldUpLimit != "") && (fieldDownLimit != ""))
- // {
- // int uplimit = fieldUpLimit.toInt();
- // int downlimit = fieldDownLimit.toInt();
- // int inputvalue = (lineEdit->text()).toInt();
- // if ((inputvalue <= uplimit) && (inputvalue >= downlimit))
- // {
- // m_sqlOper->updateControlData(fieldTableName, fieldName, lineEdit->text());
- // //updateDb(fieldTableName, fieldId, lineEdit->text());
- // } else
- // {
- // lineEdit->setPlaceholderText(tr("超出设定范围,请重新输入"));
- // }
- // } else if (fieldDownLimit != "")
- // {
- // int downlimit = fieldDownLimit.toInt();
- // int inputvalue = (lineEdit->text()).toInt();
- // if (inputvalue >= downlimit)
- // {
- // m_sqlOper->updateControlData(fieldTableName, fieldName, lineEdit->text());
- // //updateDb(fieldTableName, fieldId, lineEdit->text());
- // } else
- // {
- // lineEdit->setPlaceholderText(tr("超出设定范围,请重新输入"));
- // }
- // } else if (fieldUpLimit != "")
- // {
- // int uplimit = fieldUpLimit.toInt();
- // int inputvalue = (lineEdit->text()).toInt();
- // if (inputvalue <= uplimit)
- // {
- // m_sqlOper->updateControlData(fieldTableName, fieldName, lineEdit->text());
- // //updateDb(fieldTableName, fieldId, lineEdit->text());
- // } else
- // {
- // lineEdit->setPlaceholderText(tr("超出设定范围,请重新输入"));
- // }
- // } else
- // {
- // m_sqlOper->updateControlData(fieldTableName, fieldName, lineEdit->text());
- // //updateDb(fieldTableName, fieldId, lineEdit->text());
- // }
- // });
- connect(lineEdit, &QLineEdit::textChanged, [this, lineEdit, fieldUpLimit, fieldDownLimit, label]() {
- if ((fieldUpLimit != "") && (fieldDownLimit != ""))
- {
- int uplimit = fieldUpLimit.toInt();
- int downlimit = fieldDownLimit.toInt();
- int inputvalue = (lineEdit->text()).toInt();
- if ((inputvalue <= uplimit) && (inputvalue >= downlimit))
- {
- label->setProperty("value", lineEdit->text());
- label->setStyleSheet("color: #5c63be;");
- modifiedLabels.insert(label);
- // m_sqlOper->updateControlData(fieldTableName, fieldName, lineEdit->text());
- //updateDb(fieldTableName, fieldId, lineEdit->text());
- }
- else
- {
- lineEdit->setPlaceholderText(tr("out of range,please input again","超出设定范围,请重新输入"));
- }
- }
- else if (fieldDownLimit != "")
- {
- int downlimit = fieldDownLimit.toInt();
- int inputvalue = (lineEdit->text()).toInt();
- if (inputvalue >= downlimit)
- {
- label->setProperty("value", lineEdit->text());
- label->setStyleSheet("color: #5c63be;");
- modifiedLabels.insert(label);
- // m_sqlOper->updateControlData(fieldTableName, fieldName, lineEdit->text());
- //updateDb(fieldTableName, fieldId, lineEdit->text());
- }
- else
- {
- lineEdit->setPlaceholderText(tr("out of range,please input again","超出设定范围,请重新输入"));
- }
- }
- else if (fieldUpLimit != "")
- {
- int uplimit = fieldUpLimit.toInt();
- int inputvalue = (lineEdit->text()).toInt();
- if (inputvalue <= uplimit)
- {
- label->setProperty("value", lineEdit->text());
- label->setStyleSheet("color: #5c63be;");
- modifiedLabels.insert(label);
- // m_sqlOper->updateControlData(fieldTableName, fieldName, lineEdit->text());
- //updateDb(fieldTableName, fieldId, lineEdit->text());
- }
- else
- {
- lineEdit->setPlaceholderText(tr("out of range,please input again","超出设定范围,请重新输入"));
- }
- }
- else
- {
- label->setProperty("value", lineEdit->text());
- label->setStyleSheet("color: #5c63be;");
- modifiedLabels.insert(label);
- // m_sqlOper->updateControlData(fieldTableName, fieldName, lineEdit->text());
- //updateDb(fieldTableName, fieldId, lineEdit->text());
- }
- });
- connect(this, &DbTreeViewManager::saveClicked, [this, fieldTableName, fieldName, label]() {
- if (modifiedLabels.contains(label)) {
- m_sqlOper->updateControlData(fieldTableName, fieldName, label->property("value").toString());
- label->setStyleSheet("color: black;");
- modifiedLabels.remove(label);
- }
- });
- QPushButton* btnDefault = new QPushButton(QStringLiteral("默认"));
- btnDefault->setFixedSize(60, 28);
- btnDefault->setStyleSheet(R"(
- QPushButton {
- background: #FFFFFF;
- border: 1px solid #BABBDC;
- border-radius: 5px;
- }
- QPushButton:hover {
- background-color: #F0F0F0;
- }
- )");
- rightLayout->addWidget(btnDefault);
- connect(btnDefault, &QPushButton::clicked, [this, fieldDefult, lineEdit]() {
- if (!fieldDefult.isEmpty()) {
- lineEdit->setText(fieldDefult);
- }
- });
- }
- else
- {
- // 如果不包含逗号,按原有逻辑只创建 QLineEdit
- QLineEdit* lineEdit1 = new QLineEdit(fieldValue);
- lineEdit1->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
- lineEdit1->setFixedSize(140, 28);
- lineEdit1->setAlignment(Qt::AlignLeft);
- lineEdit1->setStyleSheet(R"(
- QLineEdit {
- background: #FFFFFF;
- border: 1px solid #BABBDC;
- border-radius: 6px;
- padding: 2px 5px;
- }
- )");
- rightLayout->addWidget(lineEdit1);
- createdWidget = lineEdit1;
- // // 连接 textChanged 信号到 lambda 函数
- //connect(lineEdit1, &QLineEdit::textChanged, [this, lineEdit1, fieldTableName, fieldName]() {
- // m_sqlOper->updateControlData(fieldTableName, fieldName, lineEdit1->text());
- // //updateDb(fieldTableName, fieldId, lineEdit1->text());
- // loginput(fieldTableName, fieldName, lineEdit1->text());
- // });
- connect(lineEdit1, &QLineEdit::textChanged, [this, lineEdit1, label]() {
- label->setProperty("value", lineEdit1->text());
- label->setStyleSheet("color: #5c63be;");
- modifiedLabels.insert(label);
- // m_sqlOper->updateControlData(fieldTableName, fieldName, lineEdit1->text());
- //updateDb(fieldTableName, fieldId, lineEdit1->text());
- // loginput(fieldTableName, fieldName, lineEdit1->text());
- });
- connect(this, &DbTreeViewManager::saveClicked, [this, fieldTableName, fieldName, label]() {
- if (modifiedLabels.contains(label)) {
- m_sqlOper->updateControlData(fieldTableName, fieldName, label->property("value").toString());
- loginput(fieldTableName, fieldName, label->property("value").toString());
- label->setStyleSheet("color: black;");
- modifiedLabels.remove(label);
- }
- });
- QPushButton* btnDefault = new QPushButton(QStringLiteral("默认"));
- btnDefault->setFixedSize(60, 28);
- btnDefault->setStyleSheet(R"(
- QPushButton {
- background: #FFFFFF;
- border: 1px solid #BABBDC;
- border-radius: 5px;
- }
- QPushButton:hover {
- background-color: #F0F0F0;
- }
- )");
- rightLayout->addWidget(btnDefault);
- connect(btnDefault, &QPushButton::clicked, [this, fieldDefult, lineEdit1]() {
- if (!fieldDefult.isEmpty()) {
- lineEdit1->setText(fieldDefult);
- }
- });
- }
- }
- else if (fieldType == "radio")
- {
- // 用逗号分隔的选项
- QStringList optionList = fieldUnits.split(QRegExp("[,,]"), Qt::SkipEmptyParts);
- QHBoxLayout* radioLayout = new QHBoxLayout;
- radioLayout->setSpacing(5);
- QButtonGroup* radioGroup = new QButtonGroup(rightWidget);
- for (const QString& optionText : optionList)
- {
- QString trimmedOpt = optionText.trimmed();
- if (trimmedOpt.isEmpty()) continue;
- QRadioButton* radioButton = new QRadioButton(trimmedOpt);
- radioGroup->addButton(radioButton);
- radioLayout->addWidget(radioButton);
- m_fieldWidgets.append(radioButton);
- if (fieldValue == trimmedOpt)
- {
- radioButton->setChecked(true);
- }
- }
- // 使用Lambda函数连接 buttonToggled 信号
- //connect(radioGroup, static_cast<void (QButtonGroup::*)(QAbstractButton*, bool)>(&QButtonGroup::buttonToggled), [this, fieldTableName, fieldName](QAbstractButton* button, bool checked) {
- // if (checked)
- // {
- // // qDebug() << "Button toggled:" << button->text();
- // // 在这里添加你想要执行的操作
- // m_sqlOper->updateControlData(fieldTableName, fieldName, button->text());
- // //updateDb(fieldTableName, fieldId, button->text());
- // }
- // });
- connect(radioGroup, static_cast<void (QButtonGroup::*)(QAbstractButton*, bool)>(&QButtonGroup::buttonToggled), [this, label](QAbstractButton* button, bool checked) {
- if (checked)
- {
- label->setProperty("value", button->text());
- label->setStyleSheet("color: #5c63be;");
- modifiedLabels.insert(label);
- // qDebug() << "Button toggled:" << button->text();
- // 在这里添加你想要执行的操作
- // m_sqlOper->updateControlData(fieldTableName, fieldName, button->text());
- //updateDb(fieldTableName, fieldId, button->text());
- }
- });
- connect(this, &DbTreeViewManager::saveClicked, [this, fieldTableName, fieldName, label]() {
- if (modifiedLabels.contains(label)) {
- m_sqlOper->updateControlData(fieldTableName, fieldName, label->property("value").toString());
- label->setStyleSheet("color: black;");
- modifiedLabels.remove(label);
- }
- });
- rightLayout->addLayout(radioLayout);
- QPushButton* btnDefault = new QPushButton(QStringLiteral("默认"));
- btnDefault->setFixedSize(60, 28);
- btnDefault->setStyleSheet(R"(
- QPushButton {
- background: #FFFFFF;
- border: 1px solid #BABBDC;
- border-radius: 5px;
- }
- QPushButton:hover {
- background-color: #F0F0F0;
- }
- )");
- rightLayout->addWidget(btnDefault);
- connect(btnDefault, &QPushButton::clicked, [this, fieldDefult, radioGroup]() {
- if (!fieldDefult.isEmpty()) {
- for (QAbstractButton* button : radioGroup->buttons()) {
- if (button->text() == fieldDefult) {
- button->setChecked(true);
- break;
- }
- }
- }
- });
- }
- else if (fieldType == "checkbox" || fieldType == "BOOL")
- {
- bool isChecked;
- if (fieldValue == "")
- {
- isChecked = (fieldValue == "1");
- } else
- {
- isChecked = (fieldValue == "1");
- }
- QCheckBox* checkBox = new QCheckBox;
- checkBox->setChecked(isChecked);
- checkBox->setStyleSheet(R"(
- QCheckBox::indicator {
- width: 20px;
- height: 20px;
- }
- QCheckBox::indicator:unchecked {
- background-color: #FFFFFF;
- border: 1px solid #BABBDC;
- border-radius: 2px;
- }
- QCheckBox::indicator:checked {
- image: url(:/images/three_Selecte.png);
- }
- QCheckBox {
- spacing: 0px;
- }
- )");
- rightLayout->addWidget(checkBox);
- createdWidget = checkBox;
- //connect(checkBox, &QCheckBox::stateChanged, [this, fieldTableName, fieldName](int state) {
- // if (state == Qt::Checked)
- // {
- // // qDebug() << "CheckBox is checked"<<fieldId;
- // m_sqlOper->updateControlData(fieldTableName, fieldName, "1");
- // //updateDb(fieldTableName, fieldId, "1");
- // } else if (state == Qt::Unchecked)
- // {
- // // qDebug() << "CheckBox is unchecked"<<fieldId;
- // // 在这里添加你想要执行的操作
- // m_sqlOper->updateControlData(fieldTableName, fieldName, "0");
- // //updateDb(fieldTableName, fieldId, "0");
- // } else if (state == Qt::PartiallyChecked)
- // {
- // qDebug() << "CheckBox is partially checked";
- // // 在这里添加你想要执行的操作
- // }
- // });
- connect(checkBox, &QCheckBox::stateChanged, [this, label](int state) {
- if (state == Qt::Checked)
- {
- // qDebug() << "CheckBox is checked"<<fieldId;
- // m_sqlOper->updateControlData(fieldTableName, fieldName, "1");
- //updateDb(fieldTableName, fieldId, "1");
- label->setProperty("value", "1");
- label->setStyleSheet("color: #5c63be;");
- modifiedLabels.insert(label);
- }
- else if (state == Qt::Unchecked)
- {
- // qDebug() << "CheckBox is unchecked"<<fieldId;
- // 在这里添加你想要执行的操作
- // m_sqlOper->updateControlData(fieldTableName, fieldName, "0");
- //updateDb(fieldTableName, fieldId, "0");
- label->setProperty("value", "0");
- label->setStyleSheet("color: #5c63be;");
- modifiedLabels.insert(label);
- }
- else if (state == Qt::PartiallyChecked)
- {
- qDebug() << "CheckBox is partially checked";
- // 在这里添加你想要执行的操作
- }
- });
- connect(this, &DbTreeViewManager::saveClicked, [this, fieldTableName, fieldName, label]() {
- if (modifiedLabels.contains(label)) {
- m_sqlOper->updateControlData(fieldTableName, fieldName, label->property("value").toString());
- label->setStyleSheet("color: black;");
- modifiedLabels.remove(label);
- }
- });
- QPushButton* btnDefault = new QPushButton(QStringLiteral("默认"));
- btnDefault->setFixedSize(60, 28);
- btnDefault->setStyleSheet(R"(
- QPushButton {
- background: #FFFFFF;
- border: 1px solid #BABBDC;
- border-radius: 5px;
- }
- QPushButton:hover {
- background-color: #F0F0F0;
- }
- )");
- rightLayout->addWidget(btnDefault);
- connect(btnDefault, &QPushButton::clicked, [this, fieldDefult, checkBox]() {
- if (!fieldDefult.isEmpty()) {
- if (fieldDefult == "1") {
- checkBox->setChecked(true);
- }
- else {
- checkBox->setChecked(false);
- }
- }
- });
- }
- else if (fieldType == "ComboBox")
- {
- //QStringList optionList = fieldValue.split(QRegExp("[,,]"), Qt::SkipEmptyParts);
- QJsonArray jsonArray;
- QJsonParseError parseError;
- QJsonDocument jsonDoc = QJsonDocument::fromJson(fieldUnits.toUtf8(), &parseError);
- if (!jsonDoc.isNull() && jsonDoc.isArray()) {
- jsonArray = jsonDoc.array();
- }
- QComboBox* comboBox = new QComboBox;
- comboBox->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
- comboBox->setFixedSize(140, 28);
- comboBox->setStyleSheet(R"(
- QComboBox {
- background: #FFFFFF;
- border: 1px solid #BABBDC;
- border-radius: 6px;
- padding: 2px 5px;
- }
- QComboBox::drop-down {
- width: 20px;
- }
- )");
- // int newIndex = -1;
- /*for (const QString& opt : optionList)
- {
- QString trimmedOpt = opt.trimmed();
- if (!trimmedOpt.isEmpty())
- comboBox->addItem(trimmedOpt);
- }
- if (fieldValue == "")
- {
- comboBox->setCurrentIndex(0);
- } else
- {
- comboBox->setCurrentIndex(fieldValue.toInt());
- }*/
- int matchedIndex = -1;
- int currentIndex = 0;
- for (const QJsonValue& val : jsonArray) {
- QJsonObject obj = val.toObject();
- QString key = obj["key"].toString();
- QString valueStr = QString::number(obj["value"].toInt()); // 统一转字符串对比
- comboBox->addItem(key);
- if (valueStr == fieldValue.trimmed()) {
- matchedIndex = currentIndex;
- }
- currentIndex++;
- }
- comboBox->setCurrentIndex(matchedIndex != -1 ? matchedIndex : 0);
- rightLayout->addWidget(comboBox);
- createdWidget = comboBox;
- // 使用Lambda函数连接 currentIndexChanged 信号
- //connect(comboBox, QOverload<int>::of(&QComboBox::currentIndexChanged), [this, comboBox, fieldTableName, fieldName](int index) {
- // QString selectedText = comboBox->itemText(index);
- // QString indexString = QString::number(index);
- // // qDebug() << "ComboBox index changed to:" << index << "Text:" << selectedText;
- // m_sqlOper->updateControlData(fieldTableName, fieldName, indexString);
- // //updateDb(fieldTableName, fieldId, indexString);
- // // 在这里添加你想要执行的操作
- // });
- connect(comboBox, QOverload<int>::of(&QComboBox::currentIndexChanged),
- [this, comboBox, jsonArray, label](int index) {
- QString selectedText = comboBox->itemText(index);
- QString valueString;
- for (const QJsonValue& val : jsonArray) {
- QJsonObject obj = val.toObject();
- if (obj["key"].toString() == selectedText) {
- valueString = QString::number(obj["value"].toInt());
- break;
- }
- }
- // m_sqlOper->updateControlData(fieldTableName, fieldName, valueString);
- label->setProperty("value", valueString);
- label->setStyleSheet("color: #5c63be;");
- modifiedLabels.insert(label);
- });
- connect(this, &DbTreeViewManager::saveClicked, [this, fieldTableName, fieldName, label]() {
- if (modifiedLabels.contains(label)) {
- m_sqlOper->updateControlData(fieldTableName, fieldName, label->property("value").toString());
- label->setStyleSheet("color: black;");
- modifiedLabels.remove(label);
- }
- });
- QPushButton* btnDefault = new QPushButton(QStringLiteral("默认"));
- btnDefault->setFixedSize(60, 28);
- btnDefault->setStyleSheet(R"(
- QPushButton {
- background: #FFFFFF;
- border: 1px solid #BABBDC;
- border-radius: 5px;
- }
- QPushButton:hover {
- background-color: #F0F0F0;
- }
- )");
- rightLayout->addWidget(btnDefault);
- connect(btnDefault, &QPushButton::clicked, [this, fieldDefult, comboBox, jsonArray]() {
- if (!fieldDefult.isEmpty()) {
- int matchedIndex = -1;
- int currentIndex = 0;
- for (const QJsonValue& val : jsonArray) {
- QJsonObject obj = val.toObject();
- QString key = obj["key"].toString();
- QString valueStr = QString::number(obj["value"].toInt());
- if (valueStr == fieldDefult.trimmed()) {
- matchedIndex = currentIndex;
- break;
- }
- currentIndex++;
- }
- comboBox->setCurrentIndex(matchedIndex != -1 ? matchedIndex : 0);
- }
- });
- } else if (fieldType == "time")
- {
- QTimeEdit* timeEdit = new QTimeEdit;
- timeEdit->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
- timeEdit->setFixedSize(120, 28);
- timeEdit->setStyleSheet(R"(
- QTimeEdit {
- background: #FFFFFF;
- border: 1px solid #BABBDC;
- border-radius: 6px;
- padding: 2px 5px;
- }
- )");
- timeEdit->setDisplayFormat("HH:mm:ss");
- timeEdit->setTime(QTime::fromString(fieldValue, "HH:mm:ss"));
- rightLayout->addWidget(timeEdit);
- createdWidget = timeEdit;
- connect(timeEdit, &QTimeEdit::timeChanged, [this, timeEdit, label]() {
- QString timeString = timeEdit->time().toString("HH:mm:ss");
- label->setProperty("value", timeString);
- label->setStyleSheet("color: #5c63be;");
- modifiedLabels.insert(label);
- });
- connect(this, &DbTreeViewManager::saveClicked, [this, fieldTableName, fieldName, label]() {
- if (modifiedLabels.contains(label)) {
- m_sqlOper->updateControlData(fieldTableName, fieldName, label->property("value").toString());
- label->setStyleSheet("color: black;");
- modifiedLabels.remove(label);
- }
- });
- QPushButton* btnDefault = new QPushButton(QStringLiteral("默认"));
- btnDefault->setFixedSize(60, 28);
- btnDefault->setStyleSheet(R"(
- QPushButton {
- background: #FFFFFF;
- border: 1px solid #BABBDC;
- border-radius: 5px;
- }
- QPushButton:hover {
- background-color: #F0F0F0;
- }
- )");
- rightLayout->addWidget(btnDefault);
- connect(btnDefault, &QPushButton::clicked, [this, fieldDefult, timeEdit]() {
- if (!fieldDefult.isEmpty()) {
- timeEdit->setTime(QTime::fromString(fieldDefult, "HH:mm:ss"));
- }
- });
- } else if (fieldType == "switch")
- {
- QWidget* switchContainer = new QWidget;
- QHBoxLayout* switchLayout = new QHBoxLayout(switchContainer);
- switchLayout->setSpacing(5);
- switchLayout->setContentsMargins(0, 0, 0, 0);
- QCheckBox* switchBox = new QCheckBox;
- switchBox->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
- switchBox->setFixedSize(30, 30);
- switchBox->setStyleSheet(R"(
- QCheckBox::indicator {
- width: 30px;
- height: 30px;
- }
- QCheckBox::indicator:unchecked {
- background-color: #BABBDC;
- border-radius: 6px;
- }
- QCheckBox::indicator:checked {
- background-color: #4CAF50;
- border-radius: 6px;
- }
- )");
- QString switchValue;
- if (fieldValue == "")
- {
- switchValue = fieldValue.toLower();
- } else
- {
- switchValue = fieldValue.toLower();
- }
- if (switchValue == "on")
- switchBox->setChecked(true);
- else
- switchBox->setChecked(false);
- QLabel* switchLabel = new QLabel(switchBox->isChecked() ? tr("Open","开") :tr("Close","关"));
- switchLabel->setAlignment(Qt::AlignVCenter | Qt::AlignLeft);
- switchLabel->setStyleSheet("font-size: 14px;");
- //connect(switchBox, &QCheckBox::stateChanged, [fieldTableName, fieldName, switchLabel, this](int state) {
- // if (state == Qt::Checked)
- // {
- // switchLabel->setText(tr("开"));
- // m_sqlOper->updateControlData(fieldTableName, fieldName, "on");
- // //updateDb(fieldTableName, fieldId, "on");
- // } else
- // {
- // switchLabel->setText(tr("关"));
- // m_sqlOper->updateControlData(fieldTableName, fieldName, "off");
- // //updateDb(fieldTableName, fieldId, "off");
- // }
- // });
- connect(switchBox, &QCheckBox::stateChanged, [switchLabel, this, label](int state) {
- if (state == Qt::Checked)
- {
- switchLabel->setText(tr("Open","开"));
- label->setProperty("value", "on");
- label->setStyleSheet("color: #5c63be;");
- modifiedLabels.insert(label);
- // m_sqlOper->updateControlData(fieldTableName, fieldName, "on");
- //updateDb(fieldTableName, fieldId, "on");
- }
- else
- {
- switchLabel->setText(tr("Close","关"));
- label->setProperty("value", "off");
- label->setStyleSheet("color: #5c63be;");
- modifiedLabels.insert(label);
- // m_sqlOper->updateControlData(fieldTableName, fieldName, "off");
- //updateDb(fieldTableName, fieldId, "off");
- }
- });
- connect(this, &DbTreeViewManager::saveClicked, [this, fieldTableName, fieldName, label]() {
- if (modifiedLabels.contains(label)) {
- m_sqlOper->updateControlData(fieldTableName, fieldName, label->property("value").toString());
- label->setStyleSheet("color: black;");
- modifiedLabels.remove(label);
- }
- });
- switchLayout->addWidget(switchBox);
- switchLayout->addWidget(switchLabel);
- rightLayout->addWidget(switchContainer);
- createdWidget = switchBox;
- QPushButton* btnDefault = new QPushButton(QStringLiteral("默认"));
- btnDefault->setFixedSize(60, 28);
- btnDefault->setStyleSheet(R"(
- QPushButton {
- background: #FFFFFF;
- border: 1px solid #BABBDC;
- border-radius: 5px;
- }
- QPushButton:hover {
- background-color: #F0F0F0;
- }
- )");
- rightLayout->addWidget(btnDefault);
- connect(btnDefault, &QPushButton::clicked, [this, fieldDefult, switchBox, switchLabel]() {
- if (!fieldDefult.isEmpty()) {
- if (fieldDefult == "on") {
- switchBox->setChecked(true);
- switchLabel->setText(tr("Open", "开"));
- }
- else {
- switchBox->setChecked(false);
- switchLabel->setText(tr("Close", "关"));
- }
- }
- });
- }
- else if (fieldType == "combined" || fieldType == "POSITION")
- {
- // 用逗号分隔
- QStringList parts = fieldValue.split(QRegExp("[,,]"), Qt::SkipEmptyParts);
- QString inputText = (parts.size() >= 1 ? parts.at(0).trimmed() : QString());
- QString button1Text = (parts.size() >= 2 ? parts.at(1).trimmed() : QStringLiteral("获取"));
- QString button2Text = (parts.size() >= 3 ? parts.at(2).trimmed() : QStringLiteral("设置"));
- QLineEdit* comboInput = new QLineEdit(inputText);
- QRegularExpression regExp("^-?\\d*\\.?\\d*"); // 匹配整数/浮点数(含负数)
- QRegularExpressionValidator* validator = new QRegularExpressionValidator(regExp, this);
- comboInput->setValidator(validator);
- QString strID = fieldTableName;
- strID += "_";
- strID += fieldName;
- strID += +"_";
- strID += fieldDescribe;
- comboInput->setProperty("Input", strID);
- comboInput->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
- comboInput->setFixedHeight(28);
- comboInput->setStyleSheet(R"(
- QLineEdit {
- background: #FFFFFF;
- border: 1px solid #BABBDC;
- border-radius: 5px;
- padding: 2px 5px;
- }
- )");
- m_fieldWidgets.append(comboInput);
- QWidget* combinedWidget = new QWidget;
- QHBoxLayout* combinedLayout = new QHBoxLayout(combinedWidget);
- combinedLayout->setSpacing(5);
- combinedLayout->setContentsMargins(0, 0, 0, 0);
- QPushButton* btnGet = new QPushButton(button1Text, combinedWidget);
- QString strIDGet = fieldTableName;
- strIDGet += "_";
- strIDGet += fieldName;
- strIDGet += +"_";
- strIDGet += fieldDescribe;
- btnGet->setProperty("Get", strIDGet);
- btnGet->setFixedSize(80, 28);
- btnGet->setStyleSheet(R"(
- QPushButton {
- background: #FFFFFF;
- border: 1px solid #BABBDC;
- border-radius: 5px;
- }
- QPushButton:hover {
- background-color: #F0F0F0;
- }
- )");
- connect(btnGet, &QPushButton::clicked, this, [this, comboInput, strstrModuleType, btnGet, strAxisType,fieldTableName, fieldName]()
- {
- double strNum = comboInput->text().toDouble();
- //qDebug() << "AAA:" << strstrModuleType << strAxisType <<" " << fieldTableName << fieldName;
- //
- if (m_pTreeCViewInterface)
- {
- m_pTreeCViewInterface->GetAxisPosition(strstrModuleType.toStdString(),
- strAxisType.toStdString(), strNum);
- }
-
- comboInput->text() = strNum;
- //QPushButton* senderBtn = qobject_cast<QPushButton*>(sender());
- //if (senderBtn) {
- // QString strOnclick = senderBtn->property("Get").toString();
- // qDebug() << "Clicked ID:" << strOnclick;
- //}
- // JMessageTip::Message_question(m_veCombinedCont[0].pLineC->text());
- });
- m_fieldWidgets.append(btnGet);
- QPushButton* btnSet = new QPushButton(button2Text, combinedWidget);
- QString strIDSet = fieldTableName;
- strIDSet += "_";
- strIDSet += fieldName;
- strIDSet += +"_";
- strIDSet += fieldDescribe;
- btnSet->setProperty("set", strIDSet);
- btnSet->setFixedSize(80, 28);
- btnSet->setStyleSheet(R"(
- QPushButton {
- background: #FFFFFF;
- border: 1px solid #BABBDC;
- border-radius: 5px;
- }
- QPushButton:hover {
- background-color: #F0F0F0;
- }
- )");
- connect(btnSet, &QPushButton::clicked, this, [this, comboInput, strstrModuleType, btnGet, strAxisType, fieldTableName, fieldName]()
- {
- QPushButton* senderBtn = qobject_cast<QPushButton*>(sender());
- if (senderBtn) {
- QString strOnclick = senderBtn->property("set").toString();
- qDebug() << "btnSet ID:" << strOnclick;
- }
- double strNum = comboInput->text().toDouble();
- if (m_pTreeCViewInterface)
- {
- m_pTreeCViewInterface->ModuleMoveTo(strstrModuleType.toStdString(),
- strAxisType.toStdString(), strNum);
- }
- //JMessageTip::Message_question(m_veCombinedCont[0].pLineC->text());
- });
- m_fieldWidgets.append(btnSet);
- combinedLayout->addWidget(comboInput);
- combinedLayout->addWidget(btnGet);
- combinedLayout->addWidget(btnSet);
- combinedLayout->addStretch();
- rightLayout->addWidget(combinedWidget);
- // 压入内存
- ST_COMBINED_CONT _a;
- _a.pGetBut = btnGet;
- _a.pSetBut = btnSet;
- _a.pLineC = comboInput;
- _a.strConfigurationsName = fieldTableName;
- _a.strfieldName = fieldName;
- _a.strfieldDescribe = fieldDescribe;
- m_veCombinedCont.push_back(_a);
- connect(comboInput, &QLineEdit::textChanged, [this, comboInput, label]() {
- label->setProperty("value", comboInput->text());
- label->setStyleSheet("color: #5c63be;");
- modifiedLabels.insert(label);
- });
- connect(this, &DbTreeViewManager::saveClicked, [this, fieldTableName, fieldName, label]() {
- if (modifiedLabels.contains(label)) {
- m_sqlOper->updateControlData(fieldTableName, fieldName, label->property("value").toString());
- label->setStyleSheet("color: black;");
- modifiedLabels.remove(label);
- }
- });
- QPushButton* btnDefault = new QPushButton(QStringLiteral("默认"));
- btnDefault->setFixedSize(60, 28);
- btnDefault->setStyleSheet(R"(
- QPushButton {
- background: #FFFFFF;
- border: 1px solid #BABBDC;
- border-radius: 5px;
- }
- QPushButton:hover {
- background-color: #F0F0F0;
- }
- )");
- rightLayout->addWidget(btnDefault);
- connect(btnDefault, &QPushButton::clicked, [this, fieldDefult, comboInput]() {
- if (!fieldDefult.isEmpty()) {
- comboInput->setText(fieldDefult);
- }
- });
- } else if (fieldType == "Text")
- {
- QLabel* label = new QLabel();
- label->setText(fieldValue);
- label->setAlignment(Qt::AlignVCenter | Qt::AlignLeft);
- label->setFixedHeight(24);
- label->setMinimumWidth(120);
- rightLayout->addWidget(label);
- } else
- {
- qWarning() << "未知字段类型:" << fieldType;
- }
- if (createdWidget)
- m_fieldWidgets.append(createdWidget);
- fieldLayout->addWidget(rightWidget);
- scrollLayout->addLayout(fieldLayout);
- }
- }
- }
- scrollLayout->addStretch();
- scrollArea->setWidget(scrollWidget);
- QVBoxLayout* mainLayout = new QVBoxLayout(m_thirdLevelFieldWnd);
- mainLayout->setContentsMargins(0, 0, 0, 0);
- mainLayout->addWidget(scrollArea);
- m_thirdLevelFieldWnd->show();
- // 显示按钮
- displayThirdLevelButtons(buttonControls);
- //loadButtonConfigForThirdLevel(data);
- }
- void DbTreeViewManager::updateParentCheckState(QStandardItem *item)
- {
- if (!item)
- return;
- QStandardItem *parentItem = item->parent();
- if (!parentItem)
- return;
- bool allChecked = true;
- bool noneChecked = true;
- for (int i = 0; i < parentItem->rowCount(); ++i) {
- QStandardItem *child = parentItem->child(i);
- if (child->checkState() != Qt::Checked) {
- allChecked = false;
- }
- if (child->checkState() == Qt::Checked) {
- noneChecked = false;
- }
- }
- if (allChecked) {
- parentItem->setCheckState(Qt::Checked);
- } else {
- parentItem->setCheckState(Qt::Unchecked);
- }
- if (noneChecked) {
- qDebug() << "noneChecked状态为:" << noneChecked;
- }
- // 递归更新上一级
- updateParentCheckState(parentItem);
- }
- void DbTreeViewManager::onButtonBackClicked()
- {
- if (m_thirdLevelFieldWnd) {
- // 当前在第三层目录,返回到上一层目录
- qDebug() << "当前在第三层目录,准备返回上一层。";
- for(int i=0;i<5;i++){
- if(currentMenuId == menuArray[i].menuId){
- menuArray[i].isthird = false;
- }
- }
- // 关闭并销毁字段展示窗口
- m_thirdLevelFieldWnd->close();
- m_thirdLevelFieldWnd->deleteLater();
- m_thirdLevelFieldWnd = nullptr;
- // 显示目录树和分隔线
- m_pTreeViewDown->show();
- for (auto separator : firstLevelSeparators) {
- if (separator)
- separator->show();
- }
- // 获取当前导航路径
- QStringList path;
- QLayout* layout = m_pNavigationWidget->layout();
- if (!layout) {
- qWarning() << "导航栏没有布局,无法获取路径。";
- return;
- }
- for (int i = 0; i < layout->count(); ++i) {
- QLayoutItem* item = layout->itemAt(i);
- if (item && item->widget()) {
- QLabel* label = qobject_cast<QLabel*>(item->widget());
- if (label) {
- QString text = label->text().trimmed();
- if (!text.isEmpty()) {
- path << text;
- }
- }
- }
- }
- if (path.isEmpty()) {
- qWarning() << "导航路径为空,无法返回上一层。";
- return;
- }
- // 移除最后一个目录名称以返回上一层
- path.removeLast();
- if (path.isEmpty()) {
- qDebug() << "已经在根目录,无法返回。";
- return;
- }
- // 根据新的路径查找对应的 QModelIndex
- QModelIndex parentIndex = findItemByPath(path);
- if (!parentIndex.isValid()) {
- qWarning() << "无法找到路径对应的目录项:" << path;
- return;
- }
- // 选中父目录项
- m_pTreeViewDown->setCurrentIndex(parentIndex);
- m_pTreeViewDown->scrollTo(parentIndex);
- // 更新导航栏
- updateNavigationBar(parentIndex);
- // 检查父目录是否为第三层目录
- QStandardItem* parentItem = m_pCModel->itemFromIndex(parentIndex);
- if (!parentItem) {
- qWarning() << "父目录项无效。";
- return;
- }
- QVariant dataVar = parentItem->data(Qt::UserRole + 2);
- bool isThirdLevel = false;
- int directoryId = -1;
- QString multiTableName = "";
- if (dataVar.canConvert<QJsonObject>()) {
- QJsonObject dataObj = dataVar.toJsonObject();
- if (dataObj.contains("IsThirdLevel") && dataObj["IsThirdLevel"].toBool()) {
- isThirdLevel = true;
- if (dataObj.contains("Id")) {
- directoryId = dataObj["Id"].toInt();
- }
- if (dataObj.contains("TableName"))
- {
- multiTableName = dataObj["TableName"].toString();
- }
- }
- }
- if (isThirdLevel) {
- if (directoryId == -1) {
- qWarning() << "无效的 DirectoryId,无法加载第三层目录内容。";
- return;
- }
- int userPrivilege = 0x1; // 当前用户的权限等级
- QList<Table_Control_Data> tableControlDatas;
- bool allChangeFlag = false;
- m_sqlOper->GetThirdDirControlData(multiTableName, userPrivilege, tableControlDatas, allChangeFlag);
- displayThirdLevelFields(tableControlDatas, allChangeFlag, false); // 传递参数表示 Home 界面
- // 获取第三层目录的数据
- //m_sqlOper->GetThirdDirControlData();
- //QJsonObject data = fetchThirdLevelData(directoryId, userPrivilege);
- // 显示字段展示窗口
- //displayThirdLevelFields(data, false); // 传递参数表示非 Home 界面
- } else {
- // 父目录不是第三层目录,确保目录树和分隔线可见
- m_pTreeViewDown->show();
- for (auto separator : firstLevelSeparators) {
- if (separator)
- separator->show();
- }
- }
- return;
- }
- // 如果不在第三层目录,则在目录树中返回上一层
- qDebug() << "当前不在第三层目录,尝试在目录树中返回上一层。";
- QModelIndex currentIndex = m_pTreeViewDown->currentIndex();
- if (!currentIndex.isValid()) {
- qWarning() << "当前没有选中的目录项,无法返回。";
- return;
- }
- QStandardItem* currentItem = m_pCModel->itemFromIndex(currentIndex);
- if (!currentItem) {
- qWarning() << "当前选中的目录项无效。";
- return;
- }
- QStandardItem* parentItem = currentItem->parent();
- if (!parentItem) {
- qDebug() << "当前已经在根目录,无法返回。";
- return;
- }
- QModelIndex parentIndex = m_pCModel->indexFromItem(parentItem);
- if (!parentIndex.isValid()) {
- qWarning() << "父目录的 QModelIndex 无效。";
- return;
- }
- // 选中父目录项
- m_pTreeViewDown->setCurrentIndex(parentIndex);
- m_pTreeViewDown->scrollTo(parentIndex);
- // 更新导航栏
- updateNavigationBar(parentIndex);
- // 检查父目录是否为第三层目录
- QVariant dataVar = parentItem->data(Qt::UserRole + 2);
- bool isThirdLevel = false;
- int directoryId = -1;
- QString multiTableName = "";
- if (dataVar.canConvert<QJsonObject>()) {
- QJsonObject dataObj = dataVar.toJsonObject();
- if (dataObj.contains("IsThirdLevel") && dataObj["IsThirdLevel"].toBool()) {
- isThirdLevel = true;
- if (dataObj.contains("Id")) {
- directoryId = dataObj["Id"].toInt();
- }
- if (dataObj.contains("TableName"))
- {
- multiTableName = dataObj["TableName"].toString();
- }
- }
- }
- if (isThirdLevel) {
- if (directoryId == -1) {
- qWarning() << "无效的 DirectoryId,无法加载第三层目录内容。";
- return;
- }
- int userPrivilege = 0x1; // 根据实际情况获取当前用户的权限等级
- QList<Table_Control_Data> tableControlDatas;
- bool allChangeFlag = false;
- m_sqlOper->GetThirdDirControlData(multiTableName, userPrivilege, tableControlDatas, allChangeFlag);
- displayThirdLevelFields(tableControlDatas, allChangeFlag, false); // 传递参数表示 Home 界面
- //QJsonObject data = fetchThirdLevelData(directoryId, userPrivilege);
- //displayThirdLevelFields(data, false); // 传递参数表示非 Home 界面
- }
- }
- QList<QStandardItem*> DbTreeViewManager::collectAllThirdLevelItems()
- {
- QList<QStandardItem*> result;
- // 从根节点开始递归搜集
- QStandardItem *root = m_pCModel->invisibleRootItem();
- if (!root) return result;
- std::function<void(QStandardItem*)> dfsCollectThirdLevel;
- dfsCollectThirdLevel = [&](QStandardItem* parentItem)
- {
- if (!parentItem) return;
- int rowCount = parentItem->rowCount();
- for (int i = 0; i < rowCount; ++i) {
- QStandardItem *child = parentItem->child(i);
- if (!child) continue;
- // 判断是否是第三层
- QVariant dataVar = child->data(Qt::UserRole + 2);
- if (dataVar.canConvert<QJsonObject>()) {
- QJsonObject obj = dataVar.toJsonObject();
- if (obj.contains("IsThirdLevel") && obj["IsThirdLevel"].toBool()) {
- // 收集这个第三层节点
- result.append(child);
- }
- }
- // 即使不是第三层,也要继续往下找子节点
- dfsCollectThirdLevel(child);
- }
- };
- dfsCollectThirdLevel(root);
- return result;
- }
- void DbTreeViewManager::onButtonDownClicked()
- {
- // 1) 收集所有 第三层 节点
- QList<QStandardItem*> thirdLevelList = collectAllThirdLevelItems();
- if (thirdLevelList.isEmpty()) {
- qDebug() << "[onButtonDownClicked] 没有任何三级目录,无法遍历。";
- return;
- }
- // 2) 找到 当前选中的节点 在 thirdLevelList 里的索引
- QModelIndex curIndex = m_pTreeViewDown->currentIndex();
- if (!curIndex.isValid()) {
- qDebug() << "[onButtonDownClicked] 当前没有选中节点,默认跳到第一个三级目录。";
- // 如果想默认跳到第一个
- QStandardItem *firstItem = thirdLevelList.first();
- QModelIndex idx = m_pCModel->indexFromItem(firstItem);
- m_pTreeViewDown->setCurrentIndex(idx);
- // 模拟点击一下
- onTreeViewClicked_updown(idx);
- // 保存复选框状态
- saveCheckedPaths();
- // 保存展开路径
- saveExpandedPaths();
- return;
- }
- QStandardItem *curItem = m_pCModel->itemFromIndex(curIndex);
- if (!curItem) {
- qWarning() << "[onButtonDownClicked] currentItem 无效。";
- return;
- }
- // 若不是三级目录,就先找它最近的父级是不是三级目录,否则当做没找到。
- int currentPos = -1;
- {
- // 在 thirdLevelList 里找到 curItem
- currentPos = thirdLevelList.indexOf(curItem);
- if (currentPos < 0) {
- qDebug() << "[onButtonDownClicked] 当前节点不是三级目录,尝试找父级是不是三级目录…";
- // 向上找父节点是否在 thirdLevelList 里
- QStandardItem *temp = curItem->parent();
- while (temp) {
- int pos = thirdLevelList.indexOf(temp);
- if (pos >= 0) {
- currentPos = pos;
- break;
- }
- temp = temp->parent();
- }
- if (currentPos < 0) {
- qDebug() << "还是找不到,说明当前目录不属于任何三级目录,直接跳到第一个";
- QStandardItem *firstItem = thirdLevelList.first();
- QModelIndex idx = m_pCModel->indexFromItem(firstItem);
- m_pTreeViewDown->setCurrentIndex(idx);
- onTreeViewClicked_updown(idx);
- // 保存复选框状态
- saveCheckedPaths();
- // 保存展开路径
- saveExpandedPaths();
- return;
- }
- }
- }
- // 尝试下一个
- int nextPos = currentPos + 1;
- if (nextPos >= thirdLevelList.size()) {
- qDebug() << "[onButtonDownClicked] 已经是最后一个三级目录了,无法再向下。";
- return;
- }
- // 跳到下一个
- QStandardItem *nextItem = thirdLevelList[nextPos];
- QModelIndex nextIndex = m_pCModel->indexFromItem(nextItem);
- m_pTreeViewDown->setCurrentIndex(nextIndex);
- // 相当于模拟点击
- onTreeViewClicked_updown(nextIndex);
- // 保存复选框状态
- saveCheckedPaths();
- // 保存展开路径
- saveExpandedPaths();
- }
- void DbTreeViewManager::onButtonUpClicked()
- {
- // 1) 收集所有 第三层 节点
- QList<QStandardItem*> thirdLevelList = collectAllThirdLevelItems();
- if (thirdLevelList.isEmpty()) {
- qDebug() << "[onButtonUpClicked] 没有任何三级目录,无法遍历。";
- return;
- }
- // 2) 找到 当前选中的节点
- QModelIndex curIndex = m_pTreeViewDown->currentIndex();
- if (!curIndex.isValid()) {
- qDebug() << "[onButtonUpClicked] 当前无选中节点,默认跳到最后一个三级目录。";
- // 跳到最后一个
- QStandardItem *lastItem = thirdLevelList.last();
- QModelIndex idx = m_pCModel->indexFromItem(lastItem);
- m_pTreeViewDown->setCurrentIndex(idx);
- // 模拟点击
- onTreeViewClicked_updown(idx);
- // 保存复选框状态
- saveCheckedPaths();
- // 保存展开路径
- saveExpandedPaths();
- return;
- }
- QStandardItem *curItem = m_pCModel->itemFromIndex(curIndex);
- if (!curItem) {
- qWarning() << "[onButtonUpClicked] currentItem 无效。";
- return;
- }
- // 如果当前节点不是三级目录, 向上查找
- int currentPos = thirdLevelList.indexOf(curItem);
- if (currentPos < 0) {
- qDebug() << "[onButtonUpClicked] 当前节点不是三级目录,尝试找父级是不是三级目录。";
- QStandardItem *temp = curItem->parent();
- while (temp) {
- int pos = thirdLevelList.indexOf(temp);
- if (pos >= 0) {
- currentPos = pos;
- break;
- }
- temp = temp->parent();
- }
- if (currentPos < 0) {
- qDebug() << "找不到任何三级目录,直接跳到最后一个。";
- QStandardItem *lastItem = thirdLevelList.last();
- QModelIndex idx = m_pCModel->indexFromItem(lastItem);
- m_pTreeViewDown->setCurrentIndex(idx);
- onTreeViewClicked_updown(idx);
- // 保存复选框状态
- saveCheckedPaths();
- // 保存展开路径
- saveExpandedPaths();
- return;
- }
- }
- // 尝试上一个
- int prevPos = currentPos - 1;
- if (prevPos < 0) {
- qDebug() << "[onButtonUpClicked] 已经是第一个三级目录了,无法再向上。";
- return;
- }
- // 跳到上一个
- QStandardItem *prevItem = thirdLevelList[prevPos];
- QModelIndex prevIndex = m_pCModel->indexFromItem(prevItem);
- m_pTreeViewDown->setCurrentIndex(prevIndex);
- // 模拟点击
- onTreeViewClicked_updown(prevIndex);
- // 保存复选框状态
- saveCheckedPaths();
- // 保存展开路径
- saveExpandedPaths();
- }
- void DbTreeViewManager::onButtonRightClicked()
- {
- if (m_forwardStack.isEmpty()) {
- qDebug() << "[Forward] 已经没有可以前进的页面";
- buttonRight->setIcon(QIcon(":/images/home_right.png"));
- return;
- }
- // 1. 弹出 forwardStack 的最后一个
- PageState st = m_forwardStack.takeLast();
- // 2. 把当前页面 push 到 backStack
- PageState cur = getCurrentPageState();
- m_backStack.append(cur);
- if(m_backStack.size() >= 2){
- buttonLeft->setIcon(QIcon(":/images/home_left.png"));
- }
- // 3. 加载 st
- loadPageState(st, true);
- }
- void DbTreeViewManager::onButtonLeftClicked()
- {
- if (m_backStack.size() < 2) {
- qDebug() << "[Back] 已经无法再后退";
- buttonLeft->setIcon(QIcon(":/images/home_left_hide.png"));
- return;
- }
- // 1. 先把当前页面(相当于 m_backStack 最后一个)弹出,放到 forwardStack
- PageState cur = m_backStack.takeLast();
- if(m_backStack.size() >= 2){
- buttonLeft->setIcon(QIcon(":/images/home_left.png"));
- }else{
- buttonLeft->setIcon(QIcon(":/images/home_left_hide.png"));
- }
- m_forwardStack.append(cur);
- buttonRight->setIcon(QIcon(":/images/home_right_show.png"));
- // 2. 拿到新的 m_backStack 末尾(即上一次看的页面)
- const PageState &prev = m_backStack.last();
- // 3. 加载 prev
- loadPageState(prev, true );
- }
- // 构建节点的完整路径
- QStringList DbTreeViewManager::buildItemPath(QStandardItem *item)
- {
- QStringList path;
- QStandardItem *currentItem = item;
- while (currentItem) {
- path.prepend(currentItem->text());
- currentItem = currentItem->parent();
- }
- return path;
- }
- // 根据路径查找对应的节点
- QModelIndex DbTreeViewManager::findItemByPath(const QStringList &path)
- {
- if (path.isEmpty()) return QModelIndex();
- QStandardItem *currentItem = m_pCModel->invisibleRootItem();
- QModelIndex currentIndex;
- for (const QString &part : path) {
- bool found = false;
- for (int i = 0; i < currentItem->rowCount(); ++i) {
- QStandardItem *child = currentItem->child(i);
- if (child == nullptr)
- {
- qDebug() << "child is nullptr";
- }
- if (child->text() == part) {
- currentIndex = m_pCModel->indexFromItem(child);
- currentItem = child;
- found = true;
- qDebug() << "找到路径部分:" << part;
- break;
- }
- }
- if (!found) {
- qWarning() << "路径部分未找到:" << part;
- return QModelIndex();
- }
- }
- return currentIndex;
- }
- void DbTreeViewManager::setCheckedPaths(const QStringList &checkedPathsList)
- {
- m_blockItemChanged = true;
- for (const QString &pathStr : checkedPathsList) {
- QStringList path = pathStr.split("/");
- QModelIndex idx = findItemByPath(path);
- if (idx.isValid()) {
- QStandardItem *item = m_pCModel->itemFromIndex(idx);
- if (item) {
- item->setCheckState(Qt::Checked);
- qDebug() << " Setting item:" << item->text() << "to Checked";
- }
- } else {
- qDebug() << "Path not found:" << pathStr;
- }
- }
- m_blockItemChanged = false;
- }
- // 递归收集被选中的路径
- QStringList DbTreeViewManager::collectCheckedPathsRecursive(QStandardItem *item, QStringList path)
- {
- QStringList checkedList;
- if (!item) {
- item = m_pCModel->invisibleRootItem();
- }
- for (int i = 0; i < item->rowCount(); ++i) {
- QStandardItem *child = item->child(i);
- if (child) {
- QStringList currentPath = path;
- currentPath << child->text();
- if (child->checkState() == Qt::Checked) {
- checkedList << currentPath.join("/");
- }
- // 递归收集子项
- checkedList << collectCheckedPathsRecursive(child, currentPath);
- }
- }
- return checkedList;
- }
- // 收集所有被选中的复选框路径
- QStringList DbTreeViewManager::collectCheckedPaths()
- {
- return collectCheckedPathsRecursive(m_pCModel->invisibleRootItem(), QStringList());
- }
- // 更新父项的复选框状态
- void DbTreeViewManager::updateParentItems(QStandardItem *parentItem)
- {
- if (!parentItem)
- return;
- int checkedCount = 0;
- int totalCount = parentItem->rowCount();
- qDebug() << "Updating child items of:" << parentItem->text();
- for (int i = 0; i < totalCount; ++i)
- {
- QStandardItem *child = parentItem->child(i);
- if (child && child->checkState() == Qt::Checked)
- {
- checkedCount++;
- }
- }
- if (checkedCount == totalCount)
- {
- parentItem->setCheckState(Qt::Checked);
- }
- else
- {
- parentItem->setCheckState(Qt::Unchecked);
- }
- // 递归更新上层父项
- updateParentItems(parentItem->parent());
- }
- // 加载并显示三级目录对应按钮配置信息
- //void DbTreeViewManager::loadButtonConfigForThirdLevel(const QJsonObject &thirdLevelObj)
- //{
- // if (!m_originalWnd) {
- // qWarning() << "OriginalWnd 指针为空,无法加载按钮配置";
- // return;
- // }
- //
- // if (!thirdLevelObj.contains("buttons")) {
- // qWarning() << "三级目录配置中不包含 'buttons' 字段";
- // return;
- // }
- //
- // QJsonArray buttonsArray = thirdLevelObj.value("buttons").toArray();
- //
- // // 获取 widget_left
- // QWidget* widgetLeft = m_originalWnd->getWidgetLeft();
- // if (!widgetLeft) {
- // qWarning() << "无法访问 widget_left";
- // return;
- // }
- //
- // // 清空 widget_left 中由 loadButtonConfigForThirdLevel 创建的按钮
- // QList<QPushButton*> existingButtons = widgetLeft->findChildren<QPushButton*>();
- // for (QPushButton* button : existingButtons) {
- // if (button->objectName().startsWith("thirdLevelBtn_")) { // 仅删除特定按钮
- // button->deleteLater();
- // }
- // }
- //
- // // 使用绝对定位创建按钮
- // for (int i = 0; i < buttonsArray.size() && i < 12; ++i) {
- // QJsonObject buttonObj = buttonsArray[i].toObject();
- // QString buttonId = buttonObj.value("id").toString();
- // QString buttonIcon = buttonObj.value("icon").toString();
- // QString buttonText = buttonObj.value("text").toString();
- // bool isEnabled = buttonObj.value("enabled").toBool();
- //
- // // 创建按钮
- // QPushButton *button = new QPushButton(widgetLeft);
- // button->setObjectName("thirdLevelBtn_" + buttonId); // 设置带前缀的对象名称
- // connect(button, &QPushButton::clicked, this, []() {
- // JMessageTip::Message_question("Q111");
- // });
- //
- // // 设置按钮的样式,调整图标和文本的位置
- // button->setStyleSheet(R"(
- // QPushButton {
- // position: absolute;
- // border-radius: 6px;
- // opacity: 1;
- // background: #CBD0FF;
- // border: none;
- // }
- // QPushButton:hover {
- // background-color: #A9B4FF; /* 鼠标悬停效果 */
- // }
- // )");
- //
- // // 设置按钮的位置和大小
- // int x = 16;
- // int y = 245 + i * (48 + 13); // 第一个按钮 y=245,后续每个按钮间隔13px
- // button->setGeometry(x, y, 158, 48);
- //
- // // 设置按钮的可见性,根据 "enabled" 字段显示或隐藏按钮
- // button->setVisible(isEnabled);
- //
- // // 创建图标标签
- // QLabel *iconLabel = new QLabel(button);
- // iconLabel->setPixmap(QIcon(buttonIcon).pixmap(16, 16));
- // iconLabel->setGeometry(10, 16, 16, 16); // 图标距离左边10px,顶部16px
- // iconLabel->setFixedSize(16, 16);
- // iconLabel->setStyleSheet("background-color: transparent;");
- // iconLabel->setVisible(isEnabled); // 根据按钮的可见性设置图标的可见性
- //
- // // 创建文本标签
- // QLabel *textLabel = new QLabel(buttonText, button);
- // textLabel->setGeometry(34, 0, 90, 48); // 文本距离左边34px
- // textLabel->setWordWrap(true); // 允许换行
- // textLabel->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
- // textLabel->setStyleSheet(R"(
- // QLabel {
- // background: transparent;
- // font-family: "思源黑体";
- // font-size: 14px;
- // font-weight: 500;
- // color: #4E51CE;
- // }
- // )");
- //
- // textLabel->setVisible(isEnabled); // 根据按钮的可见性设置文本的可见性
- //
- // // 创建 F1-F12 标签
- // QString fLabelText = QString("F%1").arg(i + 1); // F1, F2, ..., F12
- // QLabel *fLabel = new QLabel(fLabelText, button);
- // fLabel->setFixedSize(21, 16); // 设置大小为21x16
- // fLabel->setAlignment(Qt::AlignCenter);
- // fLabel->setStyleSheet(R"(
- // QLabel {
- // background-color: transparent;
- // color: #2A7ED8;
- // font-size: 12px;
- // font-weight: bold;
- // }
- // )");
- //
- // // 设置标签的位置
- // int fX = 134;
- // int fY = 2;
- // fLabel->setGeometry(fX, fY, 14, 16);
- // fLabel->setVisible(isEnabled); // 根据按钮的可见性设置标签的可见性
- //
- // // 将按钮、图标、文本和标签添加到控件列表
- // m_fieldWidgets.append(button);
- // m_fieldWidgets.append(iconLabel);
- // m_fieldWidgets.append(textLabel);
- // m_fieldWidgets.append(fLabel);
- // }
- //}
- void DbTreeViewManager::displayThirdLevelButtons(const QList<CONFIG_BASE_STRUCT>& buttons)
- {
- if (!m_originalWnd)
- {
- qWarning() << "OriginalWnd 指针为空,无法加载按钮配置";
- return;
- }
- // 获取 widget_left
- QWidget* widgetLeft = m_originalWnd->getWidgetLeft();
- if (!widgetLeft)
- {
- qWarning() << "无法访问 widget_left";
- return;
- }
- // 清空 widget_left 中由 loadButtonConfigForThirdLevel 创建的按钮
- QList<QPushButton*> existingButtons = widgetLeft->findChildren<QPushButton*>();
- for (QPushButton* button : existingButtons)
- {
- QString objName = button->objectName();
- if (button->objectName().startsWith("thirdLevelBtn_F"))
- { // 仅删除特定按钮
- button->deleteLater();
- }
- }
- // 使用绝对定位创建按钮
- for (int i = 0; i < buttons.size() && i < 12; ++i)
- {
- int buttonId = buttons[i].iGroupId;
- QString buttonIcon = QString::fromStdString(buttons[i].sIcon);
- QString buttonText = QString::fromStdString(buttons[i].strName);
- QString strCallFun = QString::fromStdString(buttons[i].strCallFun);
- bool isEnabled = buttons[i].bEnable;
- //for (int i = 0; i < buttonsArray.size() && i < 12; ++i) {
- //QJsonObject buttonObj = buttonsArray[i].toObject();
- //QString buttonId = buttonObj.value("id").toString();
- //QString strNumID = buttonId; // 不改变原来的意思
- //strNumID.remove('F');
- //QString strDirectoryId = buttonObj.value("DirectoryId").toString();
- //QString buttonIcon = buttonObj.value("icon").toString();
- //QString buttonText = buttonObj.value("text").toString();
- //QString strCallFun = buttonObj.value("CallFun").toString();
- //bool isEnabled = buttonObj.value("enabled").toBool();
- // 创建按钮
- QPushButton *button = new QPushButton(widgetLeft);
- QString ObjectName = "thirdLevelBtn_F" + QString::number(buttonId);
- button->setObjectName(ObjectName); // 设置带前缀的对象名称
- ObjectName = button->objectName();
- connect(button, &QPushButton::clicked, this, [&, strCallFun, buttonId]()
- {
- ns_module::ST_BUTTON_FUN callFun = {};
- //callFun.DirectoryId = strDirectoryId.toInt();
- callFun.strCallFun = strCallFun.toStdString();
- callFun.nId = buttonId;
- emit RunFunSignals(callFun);
- });
- // 设置按钮的样式,调整图标和文本的位置
- button->setStyleSheet(R"(
- QPushButton {
- position: absolute;
- border-radius: 6px;
- opacity: 1;
- background: #CBD0FF;
- border: none;
- }
- QPushButton:hover {
- background-color: #A9B4FF; /* 鼠标悬停效果 */
- }
- )");
- if (buttonId > 12)
- {
- qWarning() << "buttonId 超过最大ID";
- continue;
- }
- // 设置按钮的位置和大小
- int x = 16;
- int y = 245 + (buttonId - 1) * (48 + 13); // 第一个按钮 y=245,后续每个按钮间隔13px
- button->setGeometry(x, y, 158, 48);
- // 设置按钮的可见性,根据 "enabled" 字段显示或隐藏按钮
- button->setVisible(isEnabled);
- // 创建图标标签
- QLabel* iconLabel = new QLabel(button);
- iconLabel->setPixmap(QIcon(buttonIcon).pixmap(16, 16));
- iconLabel->setGeometry(10, 16, 16, 16); // 图标距离左边10px,顶部16px
- iconLabel->setFixedSize(16, 16);
- iconLabel->setStyleSheet("background-color: transparent;");
- iconLabel->setVisible(isEnabled); // 根据按钮的可见性设置图标的可见性
- // 创建文本标签
- QLabel* textLabel = new QLabel(buttonText, button);
- textLabel->setGeometry(34, 0, 90, 48); // 文本距离左边34px
- textLabel->setWordWrap(true); // 允许换行
- textLabel->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
- textLabel->setStyleSheet(R"(
- QLabel {
- background: transparent;
- font-family: "思源黑体";
- font-size: 14px;
- font-weight: 500;
- color: #4E51CE;
- }
- )");
- textLabel->setVisible(isEnabled); // 根据按钮的可见性设置文本的可见性
- // 创建 F1-F12 标签
- QString fLabelText = QString("F%1").arg(buttonId); // F1, F2, ..., F12
- QLabel* fLabel = new QLabel(fLabelText, button);
- fLabel->setFixedSize(21, 16); // 设置大小为21x16
- fLabel->setAlignment(Qt::AlignCenter);
- fLabel->setStyleSheet(R"(
- QLabel {
- background-color: transparent;
- color: #2A7ED8;
- font-size: 12px;
- font-weight: bold;
- }
- )");
- // 设置标签的位置
- int fX = 134;
- int fY = 2;
- fLabel->setGeometry(fX, fY, 14, 16);
- fLabel->setVisible(isEnabled); // 根据按钮的可见性设置标签的可见性
- // 将按钮、图标、文本和标签添加到控件列表
- m_fieldWidgets.append(button);
- m_fieldWidgets.append(iconLabel);
- m_fieldWidgets.append(textLabel);
- m_fieldWidgets.append(fLabel);
- }
- }
- // 清理并隐藏三级目录菜单内容
- void DbTreeViewManager::clearThirdLevelMenu()
- {
- // 遍历所有子控件,找到标题为 "字段展示" 的窗口并关闭
- foreach (QObject *child, m_pOriginalWndMenuPage->children()) {
- QWidget *childWidget = qobject_cast<QWidget*>(child);
- if (childWidget && childWidget->windowTitle() == "字段展示") {
- qDebug() << "关闭现有的字段展示窗口";
- childWidget->close();
- }
- }
- // 显示主目录树和分隔线
- m_pTreeViewDown->show();
- for (auto separator : firstLevelSeparators) {
- separator->show();
- }
- }
- // 保存选中路径
- void DbTreeViewManager::saveCheckedPaths()
- {
- if (m_currentConfigName == "") {
- qWarning() << "当前配置ID无效,无法保存复选框状态。";
- return;
- }
- QSettings settings("RunCloudTech", "David");
- //QString configKey = QString::number(m_currentConfigId);
- settings.beginGroup("TreeViewCheckedState");
- // 保存选中路径
- QString keyChecked = QString("checkedPaths/%1").arg(m_currentConfigName);
- QStringList checkedList = collectCheckedPaths();
- settings.setValue(keyChecked, checkedList);
- settings.endGroup();
- qDebug() << "保存复选框状态路径:" << checkedList;
- }
- // 加载选中路径
- void DbTreeViewManager::loadCheckedPaths()
- {
- if (m_currentConfigName == "") {
- qWarning() << "当前配置ID无效,无法加载复选框状态。";
- return;
- }
- QSettings settings("RunCloudTech", "David");
- //QString configKey = QString::number(m_currentConfigId);
- settings.beginGroup("TreeViewCheckedState");
- // 读取选中路径
- QString keyChecked = QString("checkedPaths/%1").arg(m_currentConfigName);
- QStringList loadedChecked = settings.value(keyChecked).toStringList();
- settings.endGroup();
- qDebug() << "加载复选框状态路径:" << loadedChecked;
- m_blockItemChanged = true;
- setCheckedPaths(loadedChecked);
- m_blockItemChanged = false;
- // 更新导航栏
- if (!loadedChecked.isEmpty()) {
- QString lastPathStr = loadedChecked.last();
- QStringList lastPath = lastPathStr.split("/");
- QModelIndex lastIdx = findItemByPath(lastPath);
- if (lastIdx.isValid()) {
- m_pTreeViewDown->setCurrentIndex(lastIdx);
- updateNavigationBar(lastIdx);
- }
- } else {
- // 如果没有加载到任何路径,自动选择第一个目录
- QStandardItem *rootItem = m_pCModel->invisibleRootItem();
- if (rootItem->rowCount() > 0) {
- QModelIndex firstIndex = m_pCModel->index(0, 0, QModelIndex());
- if (firstIndex.isValid()) {
- m_pTreeViewDown->setCurrentIndex(firstIndex);
- m_pTreeViewDown->expand(firstIndex); // 展开第一个目录
- QStandardItem *firstItem = m_pCModel->itemFromIndex(firstIndex);
- QVariant data = firstItem->data(Qt::UserRole + 2);
- if (data.canConvert<QJsonObject>()) {
- QJsonObject thirdLevelObj = data.toJsonObject();
- if (thirdLevelObj.contains("IsThirdLevel") && thirdLevelObj["IsThirdLevel"].toBool()) {
- QString multiTableName = "";
- if (thirdLevelObj.contains("TableName")) {
- multiTableName = thirdLevelObj["TableName"].toString();
- }
- QList<CONFIG_BASE_STRUCT> buttons;
- int userGrade = 0x1;
- m_sqlOper->GetThirdDirButtons(multiTableName, userGrade, buttons);
- displayThirdLevelButtons(buttons);
- //loadButtonConfigForThirdLevel(thirdLevelObj);
- }
- }
- }
- }
- }
- }
- // 保存展开路径
- void DbTreeViewManager::saveExpandedPaths()
- {
- if (m_currentConfigName == "") {
- qWarning() << "当前配置ID无效,无法保存展开路径。";
- return;
- }
- QSettings settings("RunCloudTech", "David");
- //QString configKey = QString::number(m_currentConfigId);
- settings.beginGroup("TreeViewExpandedState");
- // 保存展开路径
- QString keyExpanded = QString("expandedPaths/%1").arg(m_currentConfigName);
- QStringList expandedList = QStringList(expandedPaths.begin(), expandedPaths.end());
- settings.setValue(keyExpanded, expandedList);
- settings.endGroup();
- qDebug() << "保存展开路径:" << expandedList;
- }
- // 加载展开路径
- void DbTreeViewManager::loadExpandedPaths()
- {
- if (m_currentConfigName == "") {
- qWarning() << "当前配置ID无效,无法加载展开路径。";
- return;
- }
- QSettings settings("RunCloudTech", "David");
- //QString configKey = QString::number(m_currentConfigId);
- settings.beginGroup("TreeViewExpandedState");
- // 读取展开路径
- QString keyExpanded = QString("expandedPaths/%1").arg(m_currentConfigName);
- QStringList loadedExpanded = settings.value(keyExpanded).toStringList();
- settings.endGroup();
- qDebug() << "加载展开路径:" << loadedExpanded;
- m_blockItemChanged = true;
- // 恢复展开路径
- for (const QString &p : loadedExpanded) {
- QStringList path = p.split("/");
- QModelIndex idx = findItemByPath(path);
- if (idx.isValid()) {
- m_pTreeViewDown->expand(idx);
- expandedPaths.insert(p);
- qDebug() << "成功恢复展开路径:" << p;
- } else {
- qDebug() << "未找到展开路径部分: " << p;
- }
- }
- m_blockItemChanged = false;
- }
- // 清空历史
- void DbTreeViewManager::clearHistory()
- {
- m_backStack.clear();
- m_forwardStack.clear();
- buttonLeft->setIcon(QIcon(":/images/home_left_hide.png"));
- buttonRight->setIcon(QIcon(":/images/home_right.png"));
- }
- // 获取当前页面的 PageState
- DbTreeViewManager::PageState DbTreeViewManager::getCurrentPageState()
- {
- PageState st;
- st.isThirdLevel = false;
- st.directoryId = -1;
- // 判断是否在第三层窗口中
- if (m_thirdLevelFieldWnd && m_thirdLevelFieldWnd->isVisible()) {
- // 如果此时是在第三层界面,则用当前导航栏 or 目录树的 “选中项” 来获取路径
- QModelIndex curIndex = m_pTreeViewDown->currentIndex();
- if (!curIndex.isValid()) {
- st.path << "未知目录";
- } else {
- QStandardItem* item = m_pCModel->itemFromIndex(curIndex);
- st.path = buildItemPath(item);
- }
- // 如果能拿到 DirectoryId,则置为第三层
- QVariant dataVar = m_pCModel->itemFromIndex(m_pTreeViewDown->currentIndex())->data(Qt::UserRole + 2);
- if (dataVar.canConvert<QJsonObject>()) {
- QJsonObject obj = dataVar.toJsonObject();
- if (obj.contains("IsThirdLevel") && obj["IsThirdLevel"].toBool()) {
- st.isThirdLevel = true;
- st.directoryId = obj.value("Id").toInt(-1);
- st.multiTableName = obj.value("TableName").toString();
- }
- }
- }
- else {
- // 当前是目录树界面,就看 treeView 选中项
- QModelIndex curIndex = m_pTreeViewDown->currentIndex();
- if (curIndex.isValid()) {
- QStandardItem* item = m_pCModel->itemFromIndex(curIndex);
- st.path = buildItemPath(item);
- // 检查是否第三层
- QVariant dataVar = item->data(Qt::UserRole + 2);
- if (dataVar.canConvert<QJsonObject>()) {
- QJsonObject obj = dataVar.toJsonObject();
- if (obj.contains("IsThirdLevel") && obj["IsThirdLevel"].toBool()) {
- st.isThirdLevel = true;
- st.directoryId = obj.value("Id").toInt(-1);
- st.multiTableName = obj.value("TableName").toString();
- }
- }
- }
- }
- return st;
- }
- //加载指定的 PageState:相当于“回到该路径”或“跳转到这个第三层”
- void DbTreeViewManager::loadPageState(const PageState &st, bool isByHistoryNav)
- {
- // 先根据 st.path 找到对应的节点
- QModelIndex idx = findItemByPath(st.path);
- if (!idx.isValid()) {
- qWarning() << "[loadPageState] 未找到路径" << st.path;
- return;
- }
- // 选中该节点
- m_pTreeViewDown->setCurrentIndex(idx);
- m_pTreeViewDown->scrollTo(idx);
- // 更新导航栏
- updateNavigationBar(idx);
- // 如果是第三层,就加载对应的数据窗口
- if (st.isThirdLevel && st.directoryId != -1) {
- // 如果当前已在一个第三层窗口,先关闭它
- if (m_thirdLevelFieldWnd) {
- m_thirdLevelFieldWnd->close();
- m_thirdLevelFieldWnd->deleteLater();
- m_thirdLevelFieldWnd = nullptr;
- }
- int userPrivilege = 0x1;
- QList<Table_Control_Data> tableControlDatas;
- bool allChangeFlag = false;
- m_sqlOper->GetThirdDirControlData(st.multiTableName, userPrivilege, tableControlDatas, allChangeFlag);
- displayThirdLevelFields(tableControlDatas, allChangeFlag, false); // 传递参数表示 Home 界面
- //QJsonObject data = fetchThirdLevelData(st.directoryId, userPrivilege);
- //displayThirdLevelFields(data, (m_currentConfigName == "Home"));
- } else {
- // 否则,显示目录树
- if (m_thirdLevelFieldWnd) {
- m_thirdLevelFieldWnd->close();
- m_thirdLevelFieldWnd->deleteLater();
- m_thirdLevelFieldWnd = nullptr;
- }
- m_pTreeViewDown->show();
- for (auto sep : firstLevelSeparators) {
- if (sep) sep->show();
- }
- }
- if (!isByHistoryNav) {
- m_forwardStack.clear();
- buttonRight->setIcon(QIcon(":/images/home_right.png"));
- // 把新状态压入后退栈
- m_backStack.append(st);
- if(m_backStack.size() >= 2){
- buttonLeft->setIcon(QIcon(":/images/home_left.png"));
- }
- }
- }
- void DbTreeViewManager::loginput(const QString& fieldTableName, const QString& fieldName,const QString& modifies){
- // 获取当前时间并格式化
- QDateTime currentDateTime = QDateTime::currentDateTime();
- QString timestamp = currentDateTime.toString("yyyy-MM-dd hh:mm:ss");
- QString logMessage = QString("%1 :fieldTableName is %2 and fieldName is %3 ,currentvalue is modified %4" ).arg(timestamp).arg(fieldTableName).arg(fieldName).arg(modifies);
- QString pathoflog = "C:\\Users\\Administrator\\Desktop\\qt\\merge\\gujiangdong\\die-bonder-ui0218\\die-bonder-ui\\log/log.txt";
- writeLogToFile(logMessage,pathoflog);
- }
- void DbTreeViewManager::writeLogToFile(const QString& logMessage, const QString& filePath) {
- QFile logFile(filePath);
- if (!logFile.open(QIODevice::Append | QIODevice::Text)) {
- qWarning() << "Cannot open file for writing:" << filePath;
- return;
- }
- QTextStream out(&logFile);
- out << logMessage << "\n";
- logFile.close();
- }
|