1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434 |
- #include "treeviewmanager.h"
- #include "OriginalWnd.h"
- TreeViewManager::TreeViewManager(OriginalWnd* originalWnd, QWidget *widget2, QWidget *parent)
- : QWidget(parent),
- widget2(widget2),
- treeViewDown(new QTreeView(widget2)),
- m_originalWnd(originalWnd),
- navigationWidget(nullptr),
- buttonOpenFile(nullptr),
- buttonUp(nullptr),
- buttonDown(nullptr),
- buttonLeft(nullptr),
- buttonRight(nullptr),
- restoring(false)
- {
- if (!widget2) {
- qWarning() << "TreeViewManager: widget2 未初始化";
- return;
- }
-
- downModel = new QStandardItemModel(this);
- treeViewDown->setModel(downModel);
-
- treeViewDown->viewport()->installEventFilter(this);
-
- applyCustomStyles();
- treeViewDown->setHeaderHidden(true);
- treeViewDown->setGeometry(16, 106, widget2->width()-16, widget2->height() - 106);
- treeViewDown->setEditTriggers(QAbstractItemView::NoEditTriggers);
- treeViewDown->show();
-
- QFrame *lineFrame1 = createUnifiedSeparator(widget2, 2);
- lineFrame1->setGeometry(16, 89, 428, 2);
- lineFrame1->show();
- qDebug() << "widget2 geometry:" << widget2->geometry();
- qDebug() << "treeViewDown geometry:" << treeViewDown->geometry();
-
- setupButton();
-
- navigationWidget = new QWidget(widget2);
- navigationWidget->setGeometry(15, 15, 300, 74);
- navigationLayout = new QVBoxLayout(navigationWidget);
- navigationLayout->setContentsMargins(0, 0, 0, 0);
- navigationLayout->setSpacing(0);
- navigationWidget->show();
-
- updateNavigationWidgetGeometry();
-
- connect(downModel, &QStandardItemModel::itemChanged, this, &TreeViewManager::onItemChanged);
-
- connect(treeViewDown, &QTreeView::clicked, this, [=](const QModelIndex &index) {
- QStandardItem *item = downModel->itemFromIndex(index);
- if (!item) return;
-
- if (!restoring) {
- QStringList path = buildItemPath(item);
- addVisitedPath(path);
- }
- QVariant data = item->data(Qt::UserRole);
- if (data.canConvert<QJsonObject>()) {
- QJsonObject fields = data.toJsonObject();
- if (fields.contains("isThirdLevel") && fields["isThirdLevel"].toBool()) {
- qDebug() << "加载三级目录字段内容:" << fields;
-
- updateNavigationBar(index);
-
- displayThirdLevelFields(fields);
- treeViewDown->hide();
-
- updateSeparatorLine();
- return;
- }
- }
-
- updateNavigationBar(index);
- qDebug() << "更新导航栏,目录项:" << item->text();
- });
-
- connect(treeViewDown, &QTreeView::expanded, this, [=](const QModelIndex &index) {
- QStandardItem *item = downModel->itemFromIndex(index);
- if (!item) return;
- QStringList path = buildItemPath(item);
- addExpandedPath(path);
- updateSeparatorLine();
- });
- connect(treeViewDown, &QTreeView::collapsed, this, [=](const QModelIndex &index) {
- QStandardItem *item = downModel->itemFromIndex(index);
- if (!item) return;
- QStringList path = buildItemPath(item);
- removeExpandedPath(path);
- updateSeparatorLine();
- });
-
- loadVisitedPaths();
- QTimer::singleShot(0, this, [=]() {
- QStandardItem *rootItem = downModel->invisibleRootItem();
- QStandardItem *thirdItem = findFirstThirdLevelItemDFS(rootItem);
- if (thirdItem) {
- QJsonObject thirdLevelObj = thirdItem->data(Qt::UserRole).toJsonObject();
- if (thirdLevelObj.contains("isThirdLevel") && thirdLevelObj["isThirdLevel"].toBool()) {
- loadButtonConfigForThirdLevel(thirdLevelObj);
- }
- }
- });
- connect(buttonOpenFile, &QPushButton::clicked, this, &TreeViewManager::onButtonOpenFileClicked);
- connect(buttonUp, &QPushButton::clicked, this, &TreeViewManager::onButtonUpClicked);
- connect(buttonDown, &QPushButton::clicked, this, &TreeViewManager::onButtonDownClicked);
- connect(buttonLeft, &QPushButton::clicked, this, &TreeViewManager::onButtonLeftClicked);
- connect(buttonRight, &QPushButton::clicked, this, &TreeViewManager::onButtonRightClicked);
-
- QTimer::singleShot(0, this, &TreeViewManager::updateSeparatorLine);
- }
- TreeViewManager::~TreeViewManager()
- {
-
- saveVisitedPaths();
- }
- void TreeViewManager::clearAllSeparators()
- {
-
- for (auto separator : firstLevelSeparators.values()) {
- if (separator) {
- separator->hide();
- separator->deleteLater();
- }
- }
- firstLevelSeparators.clear();
- }
- QStandardItem* TreeViewManager::findFirstThirdLevelItemDFS(QStandardItem *parentItem)
- {
- if (!parentItem) {
- return nullptr;
- }
-
- for (int i = 0; i < parentItem->rowCount(); ++i) {
- QStandardItem *child = parentItem->child(i);
- if (!child) continue;
-
- QVariant data = child->data(Qt::UserRole);
- if (data.canConvert<QJsonObject>()) {
- QJsonObject obj = data.toJsonObject();
- if (obj.contains("isThirdLevel") && obj["isThirdLevel"].toBool()) {
-
- return child;
- }
- }
-
- QStandardItem* deeperFound = findFirstThirdLevelItemDFS(child);
- if (deeperFound) {
- return deeperFound;
- }
- }
- return nullptr;
- }
- void TreeViewManager::updateChildItems(QStandardItem *parentItem, Qt::CheckState state)
- {
- if (!parentItem)
- return;
- qDebug() << "Updating child items of:" << parentItem->text() << "to state:" << state;
- for (int i = 0; i < parentItem->rowCount(); ++i)
- {
- QStandardItem *child = parentItem->child(i);
- if (child)
- {
- child->setCheckState(state);
-
- updateChildItems(child, state);
- }
- }
- }
- QStringList TreeViewManager::collectCheckedPathsRecursive(QStandardItem *item, QStringList path)
- {
- QStringList checkedList;
- if (!item) {
- item = downModel->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 TreeViewManager::collectCheckedPaths()
- {
- return collectCheckedPathsRecursive(downModel->invisibleRootItem(), QStringList());
- }
- void TreeViewManager::setCheckedPaths(const QStringList &checkedPathsList)
- {
- qDebug() << "Setting checked paths:" << checkedPathsList;
- m_blockItemChanged = true;
- qDebug() << "setCheckedPaths: m_blockItemChanged set to true";
- for (const QString &pathStr : checkedPathsList) {
- QStringList path = pathStr.split("/");
- QModelIndex idx = findItemByPath(path);
- if (idx.isValid()) {
- QStandardItem *item = downModel->itemFromIndex(idx);
- if (item) {
- qDebug() << " - Setting item:" << item->text() << "to Checked";
- item->setCheckState(Qt::Checked);
- visitedPaths.insert(pathStr);
- qDebug() << "Added visited path:" << pathStr;
- }
- } else {
- qDebug() << "Path not found:" << pathStr;
- }
- }
- m_blockItemChanged = false;
- qDebug() << "setCheckedPaths: m_blockItemChanged set to false";
- }
- void TreeViewManager::saveCheckedPaths()
- {
- if (currentConfigFilePath.isEmpty()) {
- qWarning() << "当前配置文件路径为空,无法保存复选框状态。";
- return;
- }
- QSettings settings("RunCloudTech", "David");
- QString configKey = currentConfigKey(currentConfigFilePath);
- settings.beginGroup("TreeViewCheckedState");
-
- QString keyChecked = QString("checkedPaths/%1").arg(configKey);
- QStringList checkedList = collectCheckedPaths();
- settings.setValue(keyChecked, checkedList);
- settings.endGroup();
- qDebug() << "保存复选框状态路径:" << checkedList;
- }
- void TreeViewManager::loadCheckedPaths()
- {
- if (currentConfigFilePath.isEmpty()) {
- qWarning() << "当前配置文件路径为空,无法加载复选框状态。";
- return;
- }
- QSettings settings("RunCloudTech", "David");
- QString configKey = currentConfigKey(currentConfigFilePath);
- settings.beginGroup("TreeViewCheckedState");
-
- QString keyChecked = QString("checkedPaths/%1").arg(configKey);
- QStringList loadedChecked = settings.value(keyChecked).toStringList();
- settings.endGroup();
- qDebug() << "加载复选框状态路径:" << loadedChecked;
- m_blockItemChanged = true;
- qDebug() << "loadCheckedPaths: m_blockItemChanged set to true";
- setCheckedPaths(loadedChecked);
- m_blockItemChanged = false;
- qDebug() << "loadCheckedPaths: m_blockItemChanged set to false";
- }
- void TreeViewManager::onItemChanged(QStandardItem *item)
- {
- qDebug() << "onItemChanged called for item:" << item->text();
- qDebug() << "Flags - m_blockItemChanged:" << m_blockItemChanged << ", restoring:" << restoring;
- if (m_blockItemChanged || restoring) {
- qDebug() << "Exiting onItemChanged due to flags.";
- return;
- }
- m_blockItemChanged = true;
- qDebug() << "Processing item:" << item->text();
- Qt::CheckState state = item->checkState();
-
- updateChildItems(item, state);
-
- updateParentItems(item->parent());
- m_blockItemChanged = false;
-
- if (!restoring) {
- QStringList path = buildItemPath(item);
- if (state == Qt::Checked) {
- addVisitedPath(path);
- } else {
-
- QString joinedPath = path.join("/");
- if (visitedPaths.contains(joinedPath)) {
- visitedPaths.remove(joinedPath);
- qDebug() << "移除选中路径:" << joinedPath;
- }
- }
-
- saveCheckedPaths();
- }
- }
- void TreeViewManager::updateParentItems(QStandardItem *parentItem)
- {
- if (!parentItem)
- return;
- int checkedCount = 0;
- int totalCount = parentItem->rowCount();
- qDebug() << "Updating child items of:" ;
- 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());
- }
- QFrame* TreeViewManager::createUnifiedSeparator(QWidget *parent, int height)
- {
- QFrame *separator = new QFrame(parent);
- separator->setFrameShape(QFrame::NoFrame);
- separator->setFixedHeight(height);
- separator->setStyleSheet("background-color: #C7CAEB;");
- separator->hide();
- qDebug() << "创建统一分隔符 QFrame,父级:" << parent;
- return separator;
- }
- void TreeViewManager::updateSeparatorLine() {
-
- if (!treeViewDown->isVisible()) {
-
- for (auto separator : firstLevelSeparators) {
- if (separator)
- separator->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 = downModel->indexFromItem(firstLevelItem);
- QRect firstLevelRect = treeViewDown->visualRect(firstLevelIndex);
- if (!firstLevelRect.isValid()) {
- separator->hide();
- continue;
- }
- if (treeViewDown->isExpanded(firstLevelIndex) && firstLevelItem->hasChildren()) {
-
- QModelIndex lastVisibleChild = findLastVisibleChild(firstLevelIndex);
- if (lastVisibleChild.isValid()) {
- QRect lastChildRect = treeViewDown->visualRect(lastVisibleChild);
- if (lastChildRect.isValid()) {
-
- separator->setGeometry(16, lastChildRect.bottom() + 105, widget2->width() - 32, 2);
- separator->show();
- } else {
-
- separator->setGeometry(16, firstLevelRect.bottom() + 105, widget2->width() - 32, 2);
- separator->show();
- }
- } else {
-
- separator->setGeometry(16, firstLevelRect.bottom() + 105, widget2->width() - 32, 2);
- separator->show();
- }
- } else {
-
- separator->setGeometry(16, firstLevelRect.bottom() + 105, widget2->width() - 32, 2);
- separator->show();
- }
- }
- }
- QModelIndex TreeViewManager::findLastVisibleChild(const QModelIndex &parentIndex) {
- if (!parentIndex.isValid()) return QModelIndex();
- int childCount = downModel->rowCount(parentIndex);
- QModelIndex lastVisibleChild;
- for (int i = childCount - 1; i >= 0; --i) {
- QModelIndex childIndex = downModel->index(i, 0, parentIndex);
- if (!treeViewDown->isRowHidden(i, parentIndex)) {
- if (treeViewDown->isExpanded(childIndex) && downModel->rowCount(childIndex) > 0) {
- QModelIndex deeperChild = findLastVisibleChild(childIndex);
- if (deeperChild.isValid()) {
- return deeperChild;
- }
- }
- lastVisibleChild = childIndex;
- break;
- }
- }
- return lastVisibleChild;
- }
- QString TreeViewManager::currentConfigKey(const QString &configFilePath)
- {
-
- QFileInfo fileInfo(configFilePath);
- QString key = fileInfo.fileName();
- qDebug() << "Generated config key:" << key;
- return key;
- }
- void TreeViewManager::saveVisitedPaths()
- {
- if (currentConfigFilePath.isEmpty()) {
- qWarning() << "当前配置文件路径为空,无法保存复选框状态。";
- return;
- }
- QSettings settings("RunCloudTech", "David");
- QString configKey = currentConfigKey(currentConfigFilePath);
- settings.beginGroup("TreeViewState");
-
- QString keyVisited = QString("visitedPaths/%1").arg(configKey);
- QStringList visitedList(visitedPaths.begin(), visitedPaths.end());
- settings.setValue(keyVisited, visitedList);
-
- QString keyExpanded = QString("expandedPaths/%1").arg(configKey);
- QStringList expandedList(expandedPaths.begin(), expandedPaths.end());
- settings.setValue(keyExpanded, expandedList);
- settings.endGroup();
- qDebug() << "保存选中路径:" << visitedList;
- qDebug() << "保存展开路径:" << expandedList;
-
- saveCheckedPaths();
- }
- void TreeViewManager::loadVisitedPaths()
- {
- if (currentConfigFilePath.isEmpty()) {
- qWarning() << "当前配置文件路径为空,无法加载访问路径。";
- return;
- }
- QSettings settings("RunCloudTech", "David");
- QString configKey = currentConfigKey(currentConfigFilePath);
- settings.beginGroup("TreeViewState");
-
- QString keyVisited = QString("visitedPaths/%1").arg(configKey);
- QStringList loadedVisited = settings.value(keyVisited).toStringList();
-
- QString keyExpanded = QString("expandedPaths/%1").arg(configKey);
- QStringList loadedExpanded = settings.value(keyExpanded).toStringList();
- settings.endGroup();
- qDebug() << "加载选中路径:" << loadedVisited;
- qDebug() << "加载展开路径:" << loadedExpanded;
- restoring = true;
-
- for (const QString &p : loadedExpanded) {
- QStringList path = p.split("/");
- QModelIndex idx = findItemByPath(path);
- if (idx.isValid()) {
- treeViewDown->expand(idx);
- expandedPaths.insert(p);
- qDebug() << "成功恢复展开路径:" << p;
- } else {
- qDebug() << "未找到展开路径部分: " << p;
- }
- }
-
- for (const QString &p : loadedVisited) {
- QStringList path = p.split("/");
- QModelIndex idx = findItemByPath(path);
- if (idx.isValid()) {
- QStandardItem *item = downModel->itemFromIndex(idx);
- if (item) {
- item->setCheckState(Qt::Checked);
- visitedPaths.insert(p);
- qDebug() << "成功恢复选中路径:" << p;
- }
- } else {
- qDebug() << "未找到选中路径部分: " << p;
- }
- }
-
- loadCheckedPaths();
- restoring = false;
-
- if (!loadedVisited.isEmpty()) {
- QString lastPathStr = loadedVisited.last();
- QStringList lastPath = lastPathStr.split("/");
- QModelIndex lastIdx = findItemByPath(lastPath);
- if (lastIdx.isValid()) {
- treeViewDown->setCurrentIndex(lastIdx);
- updateNavigationBar(lastIdx);
- }
- } else {
-
- QStandardItem *rootItem = downModel->invisibleRootItem();
- if (rootItem->rowCount() > 0) {
- QModelIndex firstIndex = downModel->index(0, 0, QModelIndex());
- if (firstIndex.isValid()) {
- treeViewDown->setCurrentIndex(firstIndex);
- updateNavigationBar(firstIndex);
- treeViewDown->expand(firstIndex);
- QStandardItem *firstItem = downModel->itemFromIndex(firstIndex);
- QVariant data = firstItem->data(Qt::UserRole);
- if (data.canConvert<QJsonObject>()) {
- QJsonObject thirdLevelObj = data.toJsonObject();
- if (thirdLevelObj.contains("isThirdLevel") && thirdLevelObj["isThirdLevel"].toBool()) {
- loadButtonConfigForThirdLevel(thirdLevelObj);
- }
- }
- }
- }
- }
-
- QTimer::singleShot(0, this, &TreeViewManager::updateSeparatorLine);
- }
- void TreeViewManager::addVisitedPath(const QStringList &path)
- {
- QString joined = path.join("/");
- if (!visitedPaths.contains(joined)) {
- visitedPaths.insert(joined);
- qDebug() << "记录[选中]路径:" << joined;
- }
- }
- void TreeViewManager::addExpandedPath(const QStringList &path)
- {
- QString joined = path.join("/");
- if (!expandedPaths.contains(joined)) {
- expandedPaths.insert(joined);
- qDebug() << "记录[展开]路径:" << joined;
- }
- }
- void TreeViewManager::removeExpandedPath(const QStringList &path)
- {
- QString joinedPath = path.join("/");
- if (expandedPaths.contains(joinedPath)) {
- expandedPaths.remove(joinedPath);
- qDebug() << "移除展开路径:" << joinedPath;
- }
- }
- QStringList TreeViewManager::buildItemPath(QStandardItem *item)
- {
- QStringList path;
- QStandardItem *currentItem = item;
- while (currentItem) {
- path.prepend(currentItem->text());
- currentItem = currentItem->parent();
- }
- return path;
- }
- QModelIndex TreeViewManager::findItemByPath(const QStringList &path)
- {
- if (path.isEmpty()) return QModelIndex();
- QStandardItem *currentItem = downModel->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->text() == part) {
- currentIndex = downModel->indexFromItem(child);
- currentItem = child;
- found = true;
- break;
- }
- }
- if (!found) {
- qWarning() << "未找到路径部分:" << part;
- return QModelIndex();
- }
- }
- return currentIndex;
- }
- bool TreeViewManager::eventFilter(QObject *watched, QEvent *event)
- {
-
- if (watched == treeViewDown->viewport() && event->type() == QEvent::Paint)
- {
-
-
- bool handled = QWidget::eventFilter(watched, event);
-
- QPainter painter(treeViewDown->viewport());
- if (!painter.isActive()) {
- qWarning() << "Painter not active";
- return handled;
- }
- painter.save();
- painter.setPen(QPen(Qt::gray, 1, Qt::DashLine));
-
- paintAllBranches(QModelIndex(), painter);
- painter.restore();
-
- return handled;
- }
-
- return QWidget::eventFilter(watched, event);
- }
- void TreeViewManager::paintAllBranches(const QModelIndex &parentIndex, QPainter &painter)
- {
- int rowCount = downModel->rowCount(parentIndex);
- for(int i = 0; i < rowCount; ++i)
- {
-
- QModelIndex childIndex = downModel->index(i, 0, parentIndex);
- if (!childIndex.isValid()) continue;
-
- drawParentChildLine(childIndex, painter);
-
- if (i < rowCount - 1) {
- drawSiblingLine(childIndex, painter);
- }
-
- paintAllBranches(childIndex, painter);
- }
- }
- void TreeViewManager::drawParentChildLine(const QModelIndex &childIndex, QPainter &painter)
- {
- QModelIndex parentIndex = childIndex.parent();
- if (!parentIndex.isValid()) {
-
-
- int indent = treeViewDown->indentation();
- int depth = 0;
- int branchX = (depth + 1) * indent - indent / 2;
-
- QRect childRect = treeViewDown->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 = treeViewDown->visualRect(parentIndex);
- QRect childRect = treeViewDown->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 = treeViewDown->indentation();
- int branchX = depth * indent - indent / 2;
-
- 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 TreeViewManager::drawSiblingLine(const QModelIndex &childIndex, QPainter &painter)
- {
- QModelIndex parentIndex = childIndex.parent();
- if (!parentIndex.isValid()) {
- return;
- }
-
- int row = childIndex.row();
- int lastRow = downModel->rowCount(parentIndex) - 1;
- if (row >= lastRow) {
- return;
- }
- QModelIndex nextSibling = downModel->index(row + 1, 0, parentIndex);
- QRect currRect = treeViewDown->visualRect(childIndex);
- QRect nextRect = treeViewDown->visualRect(nextSibling);
- if (!currRect.isValid() || !nextRect.isValid()) {
- return;
- }
-
- int depth = 0;
- QModelIndex p = parentIndex;
- while (p.isValid()) {
- depth++;
- p = p.parent();
- }
- int indent = treeViewDown->indentation();
- int branchX = depth * indent - indent / 2;
-
- branchX = std::max(branchX, 0);
-
- int startY = currRect.bottom();
- int endY = nextRect.top();
- painter.drawLine(QPoint(branchX, startY), QPoint(branchX, endY));
- }
- void TreeViewManager::preloadAllConfigs(const QStringList &configPaths)
- {
- m_configCache.clear();
- for (const QString &path : configPaths) {
- QFile file(path);
- if (!file.exists()) {
- qWarning() << "preloadAllConfigs: JSON 文件不存在:" << path;
- continue;
- }
- if (!file.open(QIODevice::ReadOnly)) {
- qWarning() << "preloadAllConfigs: 无法打开 JSON 文件:" << path;
- continue;
- }
- QByteArray data = file.readAll();
- file.close();
- QJsonDocument doc = QJsonDocument::fromJson(data);
- if (doc.isNull() || !doc.isObject()) {
- qWarning() << "preloadAllConfigs: JSON 文件格式错误:" << path;
- continue;
- }
-
- QString key = QFileInfo(path).fileName();
- m_configCache.insert(key, doc);
- qDebug() << "preloadAllConfigs: 已缓存配置文件" << key;
- }
- }
- void TreeViewManager::switchConfig(const QString &configKey)
- {
- qDebug() << "切换配置文件到:" << configKey;
-
- saveVisitedPaths();
-
- if (!m_configCache.contains(configKey)) {
- qWarning() << "switchConfig: 未找到 configKey =" << configKey << ",无法切换";
- return;
- }
-
- QJsonDocument doc = m_configCache.value(configKey);
-
- clearAllSeparators();
-
- clearThirdLevelMenu();
-
- loadJsonDoc(doc, configKey);
- }
- void TreeViewManager::clearThirdLevelMenu()
- {
- qDebug() << "清理并隐藏三级目录菜单内容";
-
- foreach (QObject *child, widget2->children()) {
- QWidget *childWidget = qobject_cast<QWidget*>(child);
- if (childWidget && childWidget->windowTitle() == "字段展示") {
- qDebug() << "关闭现有的字段展示窗口";
- childWidget->close();
- }
- }
-
- treeViewDown->show();
- for (auto separator : firstLevelSeparators) {
- separator->show();
- }
- }
- void TreeViewManager::loadJsonDoc(const QJsonDocument &doc, const QString &configFilePath)
- {
-
- currentConfigFilePath = configFilePath;
- m_blockItemChanged = true;
- qDebug() << "Loading JSON config, m_blockItemChanged set to true";
-
- downModel->clear();
- firstLevelSeparators.clear();
- visitedPaths.clear();
- expandedPaths.clear();
-
- if (!doc.isObject()) {
- qWarning() << "loadJsonDoc: doc 不是对象结构";
- return;
- }
- QJsonObject rootObj = doc.object();
- buildTree(rootObj, downModel->invisibleRootItem());
-
-
-
- loadVisitedPaths();
- m_blockItemChanged = false;
- qDebug() << "Finished loading JSON config, m_blockItemChanged set to false";
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- qDebug() << "[DEBUG] loadJsonDoc 完成,对应 configKey =" << configFilePath;
- }
- void TreeViewManager::applyCustomStyles() {
- treeViewDown->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; /* 下边距 */
- }
- )");
- }
- void TreeViewManager::loadTreeData(const QJsonDocument &doc)
- {
- if (!doc.isObject()) {
- qWarning() << "无效的 JSON 结构";
- return;
- }
- qDebug() << "加载的 JSON 数据:" << doc.toJson();
- buildTree(doc.object(), downModel->invisibleRootItem());
- }
- void TreeViewManager::buildTree(const QJsonObject &jsonObj, QStandardItem *parent)
- {
- for (auto it = jsonObj.begin(); it != jsonObj.end(); ++it) {
-
- if (it.key() == "isThirdLevel" || it.key() == "separator" || it.key() == "buttons") {
- continue;
- }
- QStandardItem *item = new QStandardItem(it.key());
- item->setCheckable(true);
- item->setCheckState(Qt::Unchecked);
- parent->appendRow(item);
- qDebug() << "创建目录项:" << it.key();
- if (it.value().isObject()) {
- QJsonObject childObj = it.value().toObject();
- if (childObj.contains("isThirdLevel") && childObj["isThirdLevel"].toBool()) {
-
- item->setData(childObj, Qt::UserRole);
- qDebug() << "目录项为三级目录:" << it.key();
- }
- else {
-
- buildTree(childObj, item);
- }
- }
-
- if (parent == downModel->invisibleRootItem()) {
- QJsonObject currentObj = it.value().toObject();
- bool hasSeparator = currentObj.contains("separator") && currentObj["separator"].toBool();
- if (hasSeparator) {
- QFrame *separator = createUnifiedSeparator(widget2, 2);
- separator->hide();
- firstLevelSeparators.insert(item, separator);
- qDebug() << "为一级目录创建统一分隔线:" << it.key();
- }
- }
- }
-
- if (parent != downModel->invisibleRootItem()) {
- updateParentItems(parent);
- }
- }
- void TreeViewManager::updateNavigationWidgetGeometry()
- {
-
- int navWidth = 300;
- int navHeight = 74;
-
- int navLeft = 15;
- int navTop = 15;
-
- navigationWidget->setGeometry(navLeft, navTop, navWidth, navHeight);
-
- navigationWidget->update();
- qDebug() << "Updated navigationWidget geometry:"
- << navigationWidget->geometry();
- }
- void TreeViewManager::updateNavigationBar(const QModelIndex &index)
- {
-
- if (!index.isValid()) {
- qWarning() << "updateNavigationBar(): 当前 index 无效,尝试从 QSettings 恢复上次导航";
- QSettings settings("RunCloudTech", "David");
- settings.beginGroup("TreeViewNav");
- QStringList lastNavPath = settings.value("lastNavPath").toStringList();
- settings.endGroup();
- if (!lastNavPath.isEmpty()) {
-
- QModelIndex savedIndex = findItemByPath(lastNavPath);
- if (savedIndex.isValid()) {
- qDebug() << "成功从 QSettings 恢复导航路径:" << lastNavPath
- << ",对应项:" << downModel->itemFromIndex(savedIndex)->text();
-
- updateNavigationBar(savedIndex);
- } else {
- qWarning() << "QSettings 里存的路径无法找到对应节点,无法恢复。";
- }
- } else {
- qWarning() << "QSettings 里没有存任何导航路径,或为空。";
- }
- return;
- }
- QStandardItem *item = downModel->itemFromIndex(index);
- if (!item) {
- qWarning() << "导航栏更新失败:未找到对应项";
- return;
- }
- qDebug() << "导航栏更新,目录项:" << item->text();
-
- if (navigationWidget->layout()) {
- QLayoutItem *child;
- while ((child = navigationWidget->layout()->takeAt(0)) != nullptr) {
- if (child->widget()) {
- child->widget()->deleteLater();
- }
- delete child;
- }
- delete navigationWidget->layout();
- }
-
- QList<QStandardItem *> path;
- QStandardItem *temp = item;
- while (temp) {
- if (temp->text() != "isThirdLevel") {
- path.prepend(temp);
- }
- temp = temp->parent();
- }
- qDebug() << "导航路径:" << [path]() {
- QStringList pathNames;
- for (QStandardItem *p : path) {
- pathNames.append(p->text());
- }
- return pathNames.join(" -> ");
- }();
-
- QVBoxLayout *newLayout = new QVBoxLayout;
- newLayout->setContentsMargins(0, 0, 0, 0);
- 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(12);
- label->setFont(font);
- label->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
- label->setFixedHeight(navigationWidget->height() / 3);
- newLayout->addWidget(label);
- }
-
- navigationWidget->setLayout(newLayout);
- navigationWidget->update();
- qDebug() << "导航栏更新完成:" << path.size() << "项";
-
- QStringList pathStrings;
- for (QStandardItem *p : path) {
- pathStrings << p->text();
- }
- QSettings settings("RunCloudTech", "David");
- settings.beginGroup("TreeViewNav");
- settings.setValue("lastNavPath", pathStrings);
- settings.endGroup();
- qDebug() << "已将当前导航路径写入 QSettings:" << pathStrings;
- }
- void TreeViewManager::loadButtonConfigForThirdLevel(const QJsonObject &thirdLevelObj)
- {
- if (!m_originalWnd) {
- qWarning() << "OriginalWnd 指针为空,无法加载按钮配置";
- return;
- }
- if (!thirdLevelObj.contains("buttons")) {
- qWarning() << "三级目录配置中不包含 'buttons' 字段";
- return;
- }
- QJsonArray buttonsArray = thirdLevelObj.value("buttons").toArray();
- if (buttonsArray.size() != 12) {
- qWarning() << "按钮数量不是12个,实际数量:" << buttonsArray.size();
-
- }
-
- QWidget* widgetLeft = m_originalWnd->getWidgetLeft();
- if (!widgetLeft) {
- qWarning() << "无法访问 widget_left";
- return;
- }
-
- 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);
-
- 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);
- button->setGeometry(x, y, 158, 48);
-
- button->setVisible(isEnabled);
-
- QLabel *iconLabel = new QLabel(button);
- iconLabel->setPixmap(QIcon(buttonIcon).pixmap(16, 16));
- iconLabel->setGeometry(10, 16, 16, 16);
- iconLabel->setFixedSize(16, 16);
- iconLabel->setStyleSheet("background-color: transparent;");
- iconLabel->setVisible(isEnabled);
-
- QLabel *textLabel = new QLabel(buttonText, button);
- textLabel->setGeometry(34, 0, 90, 48);
- 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);
-
- QString fLabelText = QString("F%1").arg(i + 1);
- QLabel *fLabel = new QLabel(fLabelText, button);
- fLabel->setFixedSize(21, 16);
- 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);
- qDebug() << "创建按钮:" << buttonId << ", 文本:" << buttonText << ", 图标:" << buttonIcon << ", 启用:" << isEnabled;
- }
- }
- void TreeViewManager::displayThirdLevelFields(const QJsonObject &fields)
- {
- if (fields.isEmpty()) {
- qWarning() << "字段数据为空,无法显示";
- return;
- }
- qDebug() << "显示的字段数据:" << fields;
-
- if (!fields.contains("isThirdLevel") || !fields["isThirdLevel"].toBool()) {
- qWarning() << "不是三级目录,跳过按钮加载";
- return;
- }
-
- treeViewDown->hide();
-
- for (auto separator : firstLevelSeparators) {
- separator->hide();
- }
-
- loadButtonConfigForThirdLevel(fields);
-
- foreach (QObject *child, widget2->children()) {
- QWidget *childWidget = qobject_cast<QWidget*>(child);
- if (childWidget && childWidget->windowTitle() == "字段展示") {
- qDebug() << "字段窗口已存在,关闭旧窗口";
- childWidget->close();
- }
- }
-
- QWidget *fieldWindow = new QWidget(widget2);
- fieldWindow->setWindowTitle("字段展示");
- fieldWindow->setGeometry(treeViewDown->geometry());
-
- QScrollArea *scrollArea = new QScrollArea(fieldWindow);
- scrollArea->setWidgetResizable(true);
- scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
- qDebug() << "QScrollArea 已创建,水平滚动条策略设置为 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);
-
- for (auto it = fields.begin(); it != fields.end(); ++it) {
- QString fieldName = it.key();
- if (fieldName == "isThirdLevel") {
- continue;
- }
- if (fieldName == "buttons") {
- continue;
- }
- QJsonObject fieldConfig = it.value().toObject();
- QString fieldType = fieldConfig["type"].toString();
- qDebug() << "处理字段:" << fieldName << "类型:" << fieldType;
-
- 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, 0, 0);
- rightLayout->setSpacing(5);
- if (fieldType == "input") {
-
- QLineEdit *lineEdit = new QLineEdit(fieldConfig["value"].toString());
- lineEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
- lineEdit->setFixedHeight(28);
- lineEdit->setAlignment(Qt::AlignLeft);
- lineEdit->setStyleSheet(R"(
- QLineEdit {
- background: #FFFFFF;
- border: 1px solid #BABBDC;
- border-radius: 6px;
- padding: 2px 5px;
- }
- )");
- rightLayout->addWidget(lineEdit);
- qDebug() << "添加输入框:" << fieldName;
- } else if (fieldType == "radio") {
-
- QHBoxLayout *radioLayout = new QHBoxLayout;
- radioLayout->setSpacing(5);
- QButtonGroup *radioGroup = new QButtonGroup(rightWidget);
- QString currentValue = fieldConfig["value"].toString();
- QJsonArray options = fieldConfig["options"].toArray();
- for (const QJsonValue &option : options) {
- QRadioButton *radioButton = new QRadioButton(option.toString());
-
- if (option.toString() == currentValue) {
- radioButton->setChecked(true);
- }
- radioGroup->addButton(radioButton);
- radioLayout->addWidget(radioButton);
- qDebug() << "添加单选按钮:" << option.toString();
- }
- rightLayout->addLayout(radioLayout);
- } else if (fieldType == "checkbox") {
-
- QCheckBox *checkBox = new QCheckBox;
- checkBox->setChecked(fieldConfig["value"].toBool());
-
- checkBox->setStyleSheet(R"(
- QCheckBox::indicator {
- width: 20px;
- height: 20px;
- }
- QCheckBox::indicator:unchecked {
- background-color: #FFFFFF;
- border: 1px solid #4E51CE;
- border-radius: 4px;
- }
- QCheckBox::indicator:checked {
- image: url(:/images/check_selected.png);
- border: 1px solid #FFFFFF;
- border-radius: 4px;
- }
- QCheckBox {
- /* 取消显示文本 */
- spacing: 0px;
- }
- )");
-
- rightLayout->addWidget(checkBox);
- qDebug() << "添加复选框:" << fieldName;
- } else if (fieldType == "dropdown") {
-
- QComboBox *comboBox = new QComboBox;
- comboBox->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
- comboBox->setFixedSize(120, 28);
- comboBox->setStyleSheet(R"(
- QComboBox {
- background: #FFFFFF;
- border: 1px solid #BABBDC;
- border-radius: 6px;
- padding: 2px 5px;
- }
- QComboBox::drop-down {
- width: 20px;
- }
- )");
- QJsonArray options = fieldConfig["options"].toArray();
- for (const QJsonValue &option : options) {
- comboBox->addItem(option.toString());
- }
- comboBox->setCurrentText(fieldConfig["value"].toString());
- rightLayout->addWidget(comboBox);
- qDebug() << "添加下拉框:" << fieldName;
- } 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(fieldConfig["value"].toString(), "HH:mm:ss"));
- rightLayout->addWidget(timeEdit);
- qDebug() << "添加时间选择框:" << fieldName;
- } 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 = fieldConfig["value"].toString();
- 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, [switchLabel](int state){
- if (state == Qt::Checked) {
- switchLabel->setText("开");
- } else {
- switchLabel->setText("关");
- }
- });
- switchLayout->addWidget(switchBox);
- switchLayout->addWidget(switchLabel);
- rightLayout->addWidget(switchContainer);
- qDebug() << "添加开关控件和标签:" << fieldName;
- } else if (fieldType == "combined") {
-
- QJsonObject combinedValue = fieldConfig["value"].toObject();
-
- if (!combinedValue.contains("inputValue") ||
- !combinedValue.contains("moduleButton") ||
- !combinedValue.contains("axisButton")) {
- qWarning() << "组合控件的 value 字段不完整:" << fieldName;
- continue;
- }
- QLineEdit *comboInput = new QLineEdit(combinedValue["inputValue"].toString());
- 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;
- }
- )");
- qDebug() << "添加组合控件的输入框:" << fieldName;
-
- QWidget *combinedWidget = new QWidget;
- QHBoxLayout *combinedLayout = new QHBoxLayout(combinedWidget);
- combinedLayout->setSpacing(5);
- combinedLayout->setContentsMargins(0, 0, 0, 0);
-
- QToolButton *moduleButton = new QToolButton(combinedWidget);
- moduleButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
- moduleButton->setFixedSize(80, 28);
- moduleButton->setStyleSheet(R"(
- QToolButton {
- background: #FFFFFF;
- border: 1px solid #BABBDC;
- border-radius: 5px;
- padding: 0px; /* 移除默认内边距 */
- text-align: center; /* 使文本居中 */
- }
- QToolButton::menu-indicator {
- image: none;
- }
- QToolButton::checked {
- background: #4CAF50; /* 高亮颜色 */
- }
- /* 强制文本居中 */
- QToolButton::hover {
- qproperty-textAlignment: 'AlignCenter';
- }
- )");
- moduleButton->setText("模组");
- moduleButton->setCheckable(true);
- moduleButton->setPopupMode(QToolButton::InstantPopup);
-
- QToolButton *axisButton = new QToolButton(combinedWidget);
- axisButton->setObjectName("axisButton");
- axisButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
- axisButton->setFixedSize(80, 28);
- axisButton->setStyleSheet(R"(
- QToolButton {
- background: #FFFFFF;
- border: 1px solid #BABBDC;
- border-radius: 5px;
- padding: 0px; /* 移除默认内边距 */
- text-align: center; /* 使文本居中 */
- }
- QToolButton::menu-indicator {
- image: none;
- }
- QToolButton::checked {
- background: #4CAF50; /* 高亮颜色 */
- }
- /* 强制文本居中 */
- QToolButton::hover {
- qproperty-textAlignment: 'AlignCenter';
- }
- )");
- axisButton->setText("轴向");
- axisButton->setCheckable(true);
- axisButton->setPopupMode(QToolButton::InstantPopup);
- axisButton->setEnabled(false);
-
- QMenu *axisMenu = new QMenu(axisButton);
- axisMenu->setStyleSheet(R"(
- QMenu {
- background-color: white;
- color: black;
- border: 1px solid #BABBDC;
- }
- QMenu::item:selected {
- background-color: #4CAF50; /* 选中时背景颜色 */
- color: white; /* 选中时字体颜色 */
- }
- )");
- QJsonObject axisMap = fieldConfig["axisMap"].toObject();
-
- axisButton->setMenu(axisMenu);
- qDebug() << "初始化轴向菜单为空:" << fieldName;
-
- connect(axisMenu, &QMenu::triggered, this, [axisButton](QAction *action){
- axisButton->setText(action->text());
- axisButton->setChecked(true);
- qDebug() << "轴向选择:" << action->text();
- });
-
- QMenu *moduleMenu = new QMenu(moduleButton);
- moduleMenu->setStyleSheet(R"(
- QMenu {
- background-color: white;
- color: black;
- border: 1px solid #BABBDC;
- }
- QMenu::item:selected {
- background-color: #4CAF50; /* 选中时背景颜色 */
- color: white; /* 选中时字体颜色 */
- }
- )");
- QJsonArray moduleOptions = fieldConfig["moduleOptions"].toArray();
- for (const QJsonValue &mod : moduleOptions) {
- QAction *action = moduleMenu->addAction(mod.toString());
-
- connect(action, &QAction::triggered, this, [moduleButton, action, fieldConfig,
- combinedWidget, fieldName,
- axisButton, axisMenu, axisMap]() {
- moduleButton->setText(action->text());
- moduleButton->setChecked(true);
- qDebug() << "模组选择:" << action->text();
-
- moduleButton->setStyleSheet("background: #4CAF50;");
- axisButton->setStyleSheet("background: #4CAF50;");
- qDebug() << "组合控件颜色已切换为高亮颜色";
-
- axisButton->setEnabled(true);
-
- axisMenu->clear();
- if (axisMap.contains(action->text())) {
- QJsonArray axes = axisMap[action->text()].toArray();
- for (const QJsonValue &axis : axes) {
- axisMenu->addAction(axis.toString());
- }
- qDebug() << "轴向菜单已根据模组选择动态填充";
- } else {
- qDebug() << "没有找到对应模组的轴向选项";
- }
-
- int timeoutSec = fieldConfig["timeout"].toInt(180);
- QTimer *timer = new QTimer(combinedWidget);
- timer->setSingleShot(true);
- connect(timer, &QTimer::timeout, combinedWidget, [moduleButton, axisButton]() {
-
- moduleButton->setStyleSheet("background: #FFFFFF;");
- axisButton->setStyleSheet("background: #FFFFFF;");
- qDebug() << "组合控件颜色已切换为默认(超时)";
- });
- timer->start(timeoutSec * 1000);
- qDebug() << "启动定时器," << timeoutSec << "秒后更改组合控件颜色";
- });
- }
- moduleButton->setMenu(moduleMenu);
- qDebug() << "添加组合控件的模组按钮:" << fieldName;
-
- combinedLayout->addWidget(comboInput);
- combinedLayout->addWidget(moduleButton);
- combinedLayout->addWidget(axisButton);
- combinedLayout->addStretch();
- rightLayout->addWidget(combinedWidget);
- qDebug() << "组合控件已添加到布局:" << fieldName;
- } else {
- qWarning() << "未知字段类型:" << fieldType;
- }
-
- fieldLayout->addWidget(rightWidget);
- qDebug() << "将 rightWidget 添加到 fieldLayout";
-
- scrollLayout->addLayout(fieldLayout);
- qDebug() << "添加字段布局:" << fieldName;
- if (fieldConfig.contains("separator") && fieldConfig["separator"].toBool()) {
- QFrame *separator = createUnifiedSeparator(scrollWidget, 2);
- QHBoxLayout *separatorLayout = new QHBoxLayout;
- separatorLayout->setContentsMargins(0, 5, 0, 5);
- separatorLayout->addWidget(separator);
- scrollLayout->addLayout(separatorLayout);
- separator->show();
- qDebug() << "添加统一的动态分割线,并已显示";
- }
- }
-
- scrollLayout->addStretch();
- qDebug() << "添加弹性伸缩以填充底部空隙";
-
- scrollArea->setWidget(scrollWidget);
- qDebug() << "滚动容器已设置到 QScrollArea";
-
- QVBoxLayout *mainLayout = new QVBoxLayout(fieldWindow);
- mainLayout->setContentsMargins(0, 0, 0, 0);
- mainLayout->addWidget(scrollArea);
- qDebug() << "主布局已设置到 fieldWindow";
-
- fieldWindow->show();
- qDebug() << "三级目录字段窗口已打开";
- }
- void TreeViewManager::setupButton()
- {
- buttonOpenFile = new QPushButton(widget2);
- buttonUp = new QPushButton(widget2);
- buttonDown = new QPushButton(widget2);
- buttonLeft = new QPushButton(widget2);
- buttonRight = new QPushButton(widget2);
-
- buttonOpenFile->setParent(widget2);
- buttonOpenFile->setMaximumSize(76, 30);
- buttonOpenFile->setIcon(QIcon(":/images/home_openFile.png"));
- buttonOpenFile->setText("");
- buttonOpenFile->setGeometry(328, 16, 76, 30);
-
- buttonUp->setParent(widget2);
- buttonUp->setMaximumSize(36, 30);
- buttonUp->setIcon(QIcon(":/images/home_up.png"));
- buttonUp->setText("");
- buttonUp->setGeometry(408, 16, 36, 30);
- buttonDown->setParent(widget2);
- buttonDown->setMaximumSize(36, 30);
- buttonDown->setIcon(QIcon(":/images/home_down.png"));
- buttonDown->setText("");
- buttonDown->setGeometry(408, 50, 36, 30);
- buttonLeft->setParent(widget2);
- buttonLeft->setMaximumSize(36, 30);
- buttonLeft->setIcon(QIcon(":/images/home_left.png"));
- buttonLeft->setText("");
- buttonLeft->setGeometry(328, 50, 36, 30);
- buttonRight->setParent(widget2);
- buttonRight->setMaximumSize(36, 30);
- buttonRight->setIcon(QIcon(":/images/home_right.png"));
- buttonRight->setText("");
- buttonRight->setGeometry(368, 50, 36, 30);
-
- buttonOpenFile->show();
- buttonUp->show();
- buttonDown->show();
- buttonLeft->show();
- buttonRight->show();
- }
- void TreeViewManager::onButtonOpenFileClicked()
- {
- foreach (QObject *child, widget2->children()) {
- QWidget *childWidget = qobject_cast<QWidget *>(child);
- if (childWidget && childWidget->windowTitle() == "字段展示") {
- qDebug() << "关闭字段窗口:" << childWidget;
- childWidget->close();
- }
- }
- treeViewDown->show();
- treeViewDown->collapseAll();
- treeViewDown->clearSelection();
-
- QStandardItem *rootItem = downModel->invisibleRootItem();
- if (rootItem) {
- for (int i = 0; i < rootItem->rowCount(); ++i) {
- QStandardItem *childItem = rootItem->child(i);
- if (childItem) {
- treeViewDown->expand(downModel->indexFromItem(childItem));
- qDebug() << "展开一级目录:" << childItem->text();
- }
- }
- }
-
- updateNavigationBar(QModelIndex());
-
- QTimer::singleShot(0, this, &TreeViewManager::updateSeparatorLine);
- qDebug() << "成功返回到一级菜单";
- }
- void TreeViewManager::onButtonUpClicked()
- {
- qDebug() << "TreeViewManager: 向上遍历所有目录";
-
- QModelIndex currentIndex = treeViewDown->currentIndex();
- if (!currentIndex.isValid()) {
- qDebug() << "当前无有效索引,从最后一个根节点开始遍历";
-
- int lastRootRow = downModel->rowCount() - 1;
- if (lastRootRow >= 0) {
- currentIndex = downModel->index(lastRootRow, 0);
- treeViewDown->setCurrentIndex(currentIndex);
- updateNavigationBar(currentIndex);
- qDebug() << "当前目录:" << downModel->itemFromIndex(currentIndex)->text();
- }
- return;
- }
- QStandardItem *currentItem = downModel->itemFromIndex(currentIndex);
- if (!currentItem) {
- qWarning() << "无法获取当前目录项";
- return;
- }
-
- QModelIndex previousSiblingIndex = currentIndex.siblingAtRow(currentIndex.row() - 1);
- if (previousSiblingIndex.isValid()) {
-
- qDebug() << "当前目录有上一个同级节点:" << downModel->itemFromIndex(previousSiblingIndex)->text();
- QModelIndex lastChildIndex = getLastChildIndex(previousSiblingIndex);
- if (lastChildIndex.isValid()) {
- treeViewDown->setCurrentIndex(lastChildIndex);
- updateNavigationBar(lastChildIndex);
- qDebug() << "进入上一个同级节点的最后一个子节点:" << downModel->itemFromIndex(lastChildIndex)->text();
- } else {
- treeViewDown->setCurrentIndex(previousSiblingIndex);
- updateNavigationBar(previousSiblingIndex);
- qDebug() << "进入上一个同级节点:" << downModel->itemFromIndex(previousSiblingIndex)->text();
- }
- return;
- }
-
- QModelIndex parentIndex = currentIndex.parent();
- if (parentIndex.isValid()) {
- treeViewDown->setCurrentIndex(parentIndex);
- updateNavigationBar(parentIndex);
- qDebug() << "返回到父节点:" << downModel->itemFromIndex(parentIndex)->text();
- return;
- }
-
- qDebug() << "已经到达根目录顶部,无法继续向上遍历";
- }
- QModelIndex TreeViewManager::getLastChildIndex(const QModelIndex &parentIndex)
- {
- if (!parentIndex.isValid() || downModel->rowCount(parentIndex) == 0) {
- return QModelIndex();
- }
-
- int lastRow = downModel->rowCount(parentIndex) - 1;
- QModelIndex lastChildIndex = downModel->index(lastRow, 0, parentIndex);
-
- QModelIndex lastGrandChildIndex = getLastChildIndex(lastChildIndex);
- return lastGrandChildIndex.isValid() ? lastGrandChildIndex : lastChildIndex;
- }
- void TreeViewManager::onButtonDownClicked()
- {
- qDebug() << "TreeViewManager: 向下遍历所有目录";
-
- QModelIndex currentIndex = treeViewDown->currentIndex();
- if (!currentIndex.isValid()) {
- qDebug() << "当前无有效索引,从第一个根节点开始遍历";
- currentIndex = downModel->index(0, 0);
- treeViewDown->setCurrentIndex(currentIndex);
- updateNavigationBar(currentIndex);
- qDebug() << "当前目录:" << downModel->itemFromIndex(currentIndex)->text();
- return;
- }
- QStandardItem *currentItem = downModel->itemFromIndex(currentIndex);
- if (!currentItem) {
- qWarning() << "无法获取当前目录项";
- return;
- }
-
- if (currentItem->hasChildren()) {
- qDebug() << "当前目录有子目录,进入第一个子目录:" << currentItem->text();
- treeViewDown->expand(currentIndex);
- QModelIndex childIndex = downModel->index(0, 0, currentIndex);
- if (childIndex.isValid()) {
- treeViewDown->setCurrentIndex(childIndex);
- updateNavigationBar(childIndex);
- qDebug() << "进入子目录:" << downModel->itemFromIndex(childIndex)->text();
- return;
- } else {
- qWarning() << "当前目录有子节点但无法获取";
- }
- }
-
- QModelIndex nextSiblingIndex = currentIndex.siblingAtRow(currentIndex.row() + 1);
- while (!nextSiblingIndex.isValid()) {
-
- QModelIndex parentIndex = currentIndex.parent();
- if (!parentIndex.isValid()) {
- qDebug() << "已到达最底层目录,从第一个根节点重新开始遍历";
- nextSiblingIndex = downModel->index(0, 0);
- break;
- }
- nextSiblingIndex = parentIndex.siblingAtRow(parentIndex.row() + 1);
- currentIndex = parentIndex;
- }
-
- if (nextSiblingIndex.isValid()) {
- treeViewDown->setCurrentIndex(nextSiblingIndex);
- updateNavigationBar(nextSiblingIndex);
- qDebug() << "进入同级或上层同级目录:" << downModel->itemFromIndex(nextSiblingIndex)->text();
- } else {
- qWarning() << "无法找到下一个目录节点";
- }
- }
- QModelIndex TreeViewManager::getNextIndex(const QModelIndex ¤tIndex)
- {
-
- QModelIndex nextIndex = currentIndex.sibling(currentIndex.row() + 1, 0);
- if (nextIndex.isValid()) {
- return nextIndex;
- }
-
- QModelIndex parentIndex = currentIndex.parent();
- while (parentIndex.isValid()) {
- QModelIndex nextParentSibling = parentIndex.sibling(parentIndex.row() + 1, 0);
- if (nextParentSibling.isValid()) {
- return nextParentSibling;
- }
- parentIndex = parentIndex.parent();
- }
-
- return QModelIndex();
- }
- void TreeViewManager::onButtonLeftClicked()
- {
- qDebug() << "TreeViewManager: 返回上一级目录";
-
- bool isFieldWindowClosed = false;
-
- QTimer::singleShot(0, this, &TreeViewManager::updateSeparatorLine);
- foreach (QObject *child, widget2->children()) {
- QWidget *childWidget = qobject_cast<QWidget *>(child);
- if (childWidget && childWidget->windowTitle() == "字段展示") {
- qDebug() << "关闭三级目录窗口并返回到三级目录";
- childWidget->close();
- isFieldWindowClosed = true;
- }
- }
- if (isFieldWindowClosed) {
-
- treeViewDown->show();
- QModelIndex currentIndex = treeViewDown->currentIndex();
- if (currentIndex.isValid()) {
- updateNavigationBar(currentIndex);
- qDebug() << "返回到三级目录索引:" << downModel->itemFromIndex(currentIndex)->text();
- } else {
- qWarning() << "当前索引无效,无法更新导航栏";
- }
-
- QModelIndex parentIndex = currentIndex.parent();
- if (parentIndex.isValid()) {
- treeViewDown->setCurrentIndex(parentIndex);
- treeViewDown->expand(parentIndex);
- updateNavigationBar(parentIndex);
- QStandardItem *parentItem = downModel->itemFromIndex(parentIndex);
- if (parentItem) {
- qDebug() << "返回上一级目录:" << parentItem->text();
- } else {
- qWarning() << "未找到父目录项";
- }
- } else {
- qWarning() << "当前节点没有父节点";
- }
- return;
- }
-
- QModelIndex currentIndex = treeViewDown->currentIndex();
- if (!currentIndex.isValid()) {
- qDebug() << "当前无有效索引,无法返回";
- return;
- }
-
- QModelIndex parentIndex = currentIndex.parent();
- if (parentIndex.isValid()) {
-
- treeViewDown->setCurrentIndex(parentIndex);
- treeViewDown->expand(parentIndex);
- updateNavigationBar(parentIndex);
- QStandardItem *parentItem = downModel->itemFromIndex(parentIndex);
- if (parentItem) {
- qDebug() << "返回上一级目录:" << parentItem->text();
- } else {
- qWarning() << "未找到父目录项";
- }
- } else {
-
- QModelIndex firstIndex = downModel->index(0, 0);
- if (firstIndex.isValid()) {
- treeViewDown->setCurrentIndex(firstIndex);
- treeViewDown->expand(firstIndex);
- updateNavigationBar(firstIndex);
- QStandardItem *firstItem = downModel->itemFromIndex(firstIndex);
- if (firstItem) {
- qDebug() << "返回到一级目录的第一行:" << firstItem->text();
- } else {
- qWarning() << "未找到一级目录的第一行";
- }
- } else {
- qWarning() << "无法找到任何一级目录";
- }
- }
- }
- void TreeViewManager::onButtonRightClicked()
- {
- qDebug() << "TreeViewManager: 进入下一级目录";
-
- QModelIndex currentIndex = treeViewDown->currentIndex();
- if (!currentIndex.isValid()) {
- qDebug() << "当前无有效索引,自动从第一个根节点开始";
- currentIndex = downModel->index(0, 0);
- }
- QStandardItem *currentItem = downModel->itemFromIndex(currentIndex);
- if (!currentItem) {
- qWarning() << "无法获取当前目录项";
- return;
- }
-
- if (currentItem->hasChildren()) {
- qDebug() << "当前目录有子目录,展开并进入第一个子目录:" << currentItem->text();
-
- treeViewDown->expand(currentIndex);
- QModelIndex childIndex = downModel->index(0, 0, currentIndex);
- if (childIndex.isValid()) {
- treeViewDown->setCurrentIndex(childIndex);
- updateNavigationBar(childIndex);
- qDebug() << "进入子目录:" << downModel->itemFromIndex(childIndex)->text();
- return;
- } else {
- qWarning() << "展开失败:未找到子节点";
- return;
- }
- }
-
- QModelIndex nextSiblingIndex = currentIndex.siblingAtRow(currentIndex.row() + 1);
- while (!nextSiblingIndex.isValid()) {
-
- QModelIndex parentIndex = currentIndex.parent();
- if (!parentIndex.isValid()) {
- qDebug() << "已到达最底层目录,没有更多的下一级节点";
- return;
- }
- nextSiblingIndex = parentIndex.siblingAtRow(parentIndex.row() + 1);
- currentIndex = parentIndex;
- }
-
- treeViewDown->setCurrentIndex(nextSiblingIndex);
- updateNavigationBar(nextSiblingIndex);
- qDebug() << "进入同级目录:" << downModel->itemFromIndex(nextSiblingIndex)->text();
- }
|