123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133 |
- #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);
- applyCustomStyles();
- treeViewDown->setHeaderHidden(true);
- treeViewDown->setGeometry(16, 106, widget2->width()-16, widget2->height() - 106);
- treeViewDown->setEditTriggers(QAbstractItemView::NoEditTriggers);
- treeViewDown->viewport()->installEventFilter(this);
- QFrame *lineFrame1 = createUnifiedSeparator(widget2, 2);
- lineFrame1->setGeometry(16, 89, 428, 2);
- lineFrame1->show();
- setupButton();
- navigationWidget = new QWidget(widget2);
- navigationWidget->setGeometry(15, 15, 300, 74);
- loadVisitedPaths();
-
- 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()) {
- updateNavigationBar(index);
- displayThirdLevelFields(fields);
- treeViewDown->hide();
- updateSeparatorLine();
- return;
- }
- }
- updateNavigationBar(index);
-
- });
-
- 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();
- });
-
- 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);
- }
- TreeViewManager::~TreeViewManager()
- {
- }
- 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;
-
- 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)
- {
-
- m_blockItemChanged = 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;
-
- }
- 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;
-
- setCheckedPaths(loadedChecked);
- m_blockItemChanged = false;
-
- }
- void TreeViewManager::onItemChanged(QStandardItem *item)
- {
-
-
- 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();
-
- 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();
-
- m_fieldWidgets.clear();
- m_currentFieldIndex = -1;
-
- if (!m_configCache.contains(configKey)) {
- qWarning() << "switchConfig: 未找到 configKey =" << configKey << ",无法切换";
- return;
- }
-
- QJsonDocument doc = m_configCache.value(configKey);
-
- clearAllSeparators();
-
- clearThirdLevelMenu();
-
- loadJsonDoc(doc, configKey);
- }
- void TreeViewManager::clearThirdLevelMenu()
- {
-
- 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;
-
-
- 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;
-
- }
- 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::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);
-
- if (it.value().isObject()) {
- QJsonObject childObj = it.value().toObject();
- if (childObj.contains("isThirdLevel") && childObj["isThirdLevel"].toBool()) {
-
- item->setData(childObj, Qt::UserRole);
-
- }
- 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);
-
- }
- }
- }
-
- if (parent != downModel->invisibleRootItem()) {
- updateParentItems(parent);
- }
- }
- void TreeViewManager::updateNavigationBar(const QModelIndex &index)
- {
-
- if (!index.isValid()) {
-
- 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;
- }
-
-
- 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, 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);
- label->setFont(font);
- label->setFixedHeight(navigationWidget->height() / 3);
- newLayout->addWidget(label);
- }
-
- navigationWidget->setLayout(newLayout);
- navigationWidget->update();
-
-
- QStringList pathStrings;
- for (QStandardItem *p : path) {
- pathStrings << p->text();
- }
- QSettings settings("RunCloudTech", "David");
- settings.beginGroup("TreeViewNav");
- settings.setValue("lastNavPath", pathStrings);
- settings.endGroup();
-
- }
- 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();
-
- 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);
-
- }
- }
- void TreeViewManager::displayThirdLevelFields(const QJsonObject &fields)
- {
- if (fields.isEmpty()) {
- qWarning() << "字段数据为空,无法显示";
- return;
- }
-
-
- if (!fields.contains("isThirdLevel") || !fields["isThirdLevel"].toBool()) {
- qWarning() << "不是三级目录,跳过按钮加载";
- return;
- }
-
- treeViewDown->hide();
-
- for (auto separator : firstLevelSeparators) {
- separator->hide();
- }
-
- loadButtonConfigForThirdLevel(fields);
-
- m_fieldWidgets.clear();
- m_currentFieldIndex = -1;
-
- foreach (QObject *child, widget2->children()) {
- QWidget *childWidget = qobject_cast<QWidget*>(child);
- if (childWidget && childWidget->windowTitle() == "字段展示") {
-
- 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);
-
-
- 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();
-
-
- 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") {
-
- QLineEdit *lineEdit = new QLineEdit(fieldConfig["value"].toString());
- lineEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
- lineEdit->setFixedSize(120, 28);
- lineEdit->setAlignment(Qt::AlignLeft);
- lineEdit->setStyleSheet(R"(
- QLineEdit {
- background: #FFFFFF;
- border: 1px solid #BABBDC;
- border-radius: 6px;
- padding: 2px 5px;
- }
- )");
- rightLayout->addWidget(lineEdit);
- createdWidget = lineEdit;
-
- } 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);
- m_fieldWidgets.append(radioButton);
-
- }
- 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 #BABBDC;
- border-radius: 2px;
- }
- QCheckBox::indicator:checked {
- image: url(:/images/three_Selecte.png);
- }
- QCheckBox {
- /* 取消显示文本 */
- spacing: 0px;
- }
- )");
- rightLayout->addWidget(checkBox);
- createdWidget = checkBox;
-
- } 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);
- createdWidget = comboBox;
-
- } 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);
- 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 = 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);
- createdWidget = switchBox;
-
- } else if (fieldType == "combined") {
-
- QJsonObject combinedValue = fieldConfig["value"].toObject();
-
- if (!combinedValue.contains("inputValue") ||
- !combinedValue.contains("moduleButton") ||
- !combinedValue.contains("axisButton")) {
-
- 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;
- }
- )");
- m_fieldWidgets.append(comboInput);
-
-
- 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);
- m_fieldWidgets.append(moduleButton);
-
- 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);
- m_fieldWidgets.append(axisButton);
-
- 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);
-
-
- connect(axisMenu, &QMenu::triggered, this, [axisButton](QAction *action){
- axisButton->setText(action->text());
- axisButton->setChecked(true);
-
- });
-
- 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);
-
-
- moduleButton->setStyleSheet("background: #4CAF50;");
- axisButton->setStyleSheet("background: #4CAF50;");
-
-
- 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());
- }
-
- } else {
-
- }
-
- 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;");
-
- });
- timer->start(timeoutSec * 1000);
-
- });
- }
- moduleButton->setMenu(moduleMenu);
-
-
- combinedLayout->addWidget(comboInput);
- combinedLayout->addWidget(moduleButton);
- combinedLayout->addWidget(axisButton);
- combinedLayout->addStretch();
- rightLayout->addWidget(combinedWidget);
-
- } else {
- qWarning() << "未知字段类型:" << fieldType;
- }
-
- if (createdWidget) {
- m_fieldWidgets.append(createdWidget);
- }
-
- fieldLayout->addWidget(rightWidget);
-
-
- scrollLayout->addLayout(fieldLayout);
-
- 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();
-
- }
- }
-
- scrollLayout->addStretch();
-
- scrollArea->setWidget(scrollWidget);
-
- QVBoxLayout *mainLayout = new QVBoxLayout(fieldWindow);
- mainLayout->setContentsMargins(0, 0, 0, 0);
- mainLayout->addWidget(scrollArea);
-
- fieldWindow->show();
-
- }
- void TreeViewManager::onButtonUpClicked()
- {
- if (m_fieldWidgets.isEmpty()) {
-
-
- return;
- }
- if (m_currentFieldIndex <= 0) {
-
- return;
- }
-
- m_currentFieldIndex--;
- QWidget *widgetToFocus = m_fieldWidgets[m_currentFieldIndex];
- if (widgetToFocus) {
- widgetToFocus->setFocus();
- }
- }
- void TreeViewManager::onButtonDownClicked()
- {
- if (m_fieldWidgets.isEmpty()) {
-
-
- return;
- }
- if (m_currentFieldIndex < 0) {
-
- m_currentFieldIndex = 0;
- }
- else if (m_currentFieldIndex >= m_fieldWidgets.size() - 1) {
-
- return;
- }
- else {
-
- m_currentFieldIndex++;
- }
- QWidget *widgetToFocus = m_fieldWidgets[m_currentFieldIndex];
- if (widgetToFocus) {
- widgetToFocus->setFocus();
- }
- }
- void TreeViewManager::setupButton()
- {
-
- auto createButton = [&](QPushButton*& button, QWidget* parent, const QString& iconPath, const QRect& geometry) {
- button = new QPushButton(parent);
- 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(buttonOpenFile, widget2, ":/images/home_openFile.png", QRect(328, 16, 76, 30));
- createButton(buttonUp, widget2, ":/images/home_up.png", QRect(408, 16, 36, 30));
- createButton(buttonDown, widget2, ":/images/home_down.png", QRect(408, 50, 36, 30));
- createButton(buttonLeft, widget2, ":/images/home_left.png", QRect(328, 50, 36, 30));
- createButton(buttonRight, widget2, ":/images/home_right.png", QRect(368, 50, 36, 30));
- }
- void TreeViewManager::onButtonOpenFileClicked()
- {
- foreach (QObject *child, widget2->children()) {
- QWidget *childWidget = qobject_cast<QWidget *>(child);
- if (childWidget && childWidget->windowTitle() == "字段展示") {
-
- childWidget->close();
- }
- }
- treeViewDown->show();
- treeViewDown->collapseAll();
- treeViewDown->clearSelection();
-
- m_fieldWidgets.clear();
- m_currentFieldIndex = -1;
-
- 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));
-
- }
- }
- }
-
- updateNavigationBar(QModelIndex());
-
- QTimer::singleShot(0, this, &TreeViewManager::updateSeparatorLine);
-
- }
- void TreeViewManager::onButtonLeftClicked()
- {
-
-
- bool isFieldWindowClosed = false;
-
- QTimer::singleShot(0, this, &TreeViewManager::updateSeparatorLine);
- foreach (QObject *child, widget2->children()) {
- QWidget *childWidget = qobject_cast<QWidget *>(child);
- if (childWidget && childWidget->windowTitle() == "字段展示") {
-
- childWidget->close();
- isFieldWindowClosed = true;
- }
- }
- if (isFieldWindowClosed) {
-
- treeViewDown->show();
- QModelIndex currentIndex = treeViewDown->currentIndex();
- if (currentIndex.isValid()) {
- updateNavigationBar(currentIndex);
-
- } else {
- qWarning() << "当前索引无效,无法更新导航栏";
- }
-
- m_fieldWidgets.clear();
- m_currentFieldIndex = -1;
-
- 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) {
-
- } 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()
- {
-
-
- QModelIndex currentIndex = treeViewDown->currentIndex();
- if (!currentIndex.isValid()) {
-
- currentIndex = downModel->index(0, 0);
- }
- QStandardItem *currentItem = downModel->itemFromIndex(currentIndex);
- if (!currentItem) {
- qWarning() << "无法获取当前目录项";
- return;
- }
-
- if (currentItem->hasChildren()) {
-
-
- treeViewDown->expand(currentIndex);
- QModelIndex childIndex = downModel->index(0, 0, currentIndex);
- if (childIndex.isValid()) {
- treeViewDown->setCurrentIndex(childIndex);
- updateNavigationBar(childIndex);
-
- return;
- } else {
- qWarning() << "展开失败:未找到子节点";
- return;
- }
- }
-
- QModelIndex nextSiblingIndex = currentIndex.siblingAtRow(currentIndex.row() + 1);
- while (!nextSiblingIndex.isValid()) {
-
- QModelIndex parentIndex = currentIndex.parent();
- if (!parentIndex.isValid()) {
-
- return;
- }
- nextSiblingIndex = parentIndex.siblingAtRow(parentIndex.row() + 1);
- currentIndex = parentIndex;
- }
-
- treeViewDown->setCurrentIndex(nextSiblingIndex);
- updateNavigationBar(nextSiblingIndex);
-
- }
|