#include "nmWxDisplaySettings.h" #include "nmGuiPlot.h" #include "nmPlotGraphicBinder.h" #include "nmDataAnalyzeManager.h" #include "nmDataFault.h" #include "nmDataFracture.h" #include "nmDataOutline.h" #include "nmDataRegion.h" #include "nmDataRegionMark.h" #include "nmObjBase.h" #include "nmObjLineFault.h" #include "nmObjLineCrack.h" #include "nmObjPointWell.h" #include "nmObjRegion.h" #include "nmObjRegionMark.h" #include "ZxBaseUtil.h" #include #include #include #include #include #include #include #include #include #include #include namespace { // 树的建议大小随字体变化,布局仍允许横向、纵向拉伸及滚动。 class nmDisplayTreeWidget : public QTreeWidget { public: // 沿用 Qt 原生绘制、键盘操作和复选框语义。 explicit nmDisplayTreeWidget(QWidget* pParent) : QTreeWidget(pParent) {} // 初始高度覆盖常见分类,避免大量空白或打开后只能看到少数几行。 virtual QSize sizeHint() const { return QSize(fontMetrics().height() * 22, fontMetrics().height() * 20); } }; // 图片采用隐式共享,缓存标识足以识别本次会话中是否发生替换,不逐像素比较大图。 bool sameImage(const BackgroundImageInfo& oLeft, const BackgroundImageInfo& oRight) { return oLeft.bIsVisible == oRight.bIsVisible && oLeft.sFilePath == oRight.sFilePath && oLeft.objImage.cacheKey() == oRight.objImage.cacheKey(); } // 与已统一的参数窗口共用边距、按钮图标和原生按钮排列。 QDialogButtonBox* createButtons(QWidget* pParent, QVBoxLayout* pMain, QPushButton** ppApplyButton = NULL) { QWidget* pFooter = new QWidget(pParent); pFooter->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed); QHBoxLayout* pLayout = new QHBoxLayout(pFooter); pLayout->setContentsMargins(4, 5, 4, 5); // 三个按钮整体靠右,应用紧邻确定左侧;伸缩空间统一留在按钮组前方。 pLayout->addStretch(); if(ppApplyButton != NULL) { *ppApplyButton = new QPushButton( QCoreApplication::translate("nmWxDisplaySettings", "Apply"), pFooter); (*ppApplyButton)->setAutoDefault(false); pLayout->addWidget(*ppApplyButton); } QDialogButtonBox* pButtons = new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, pFooter); QPushButton* pOk = pButtons->button(QDialogButtonBox::Ok); pOk->setText(QCoreApplication::translate("nmWxDisplaySettings", "OK")); pOk->setIcon(zxLoadIcon("OK")); pOk->setDefault(true); QPushButton* pCancel = pButtons->button(QDialogButtonBox::Cancel); pCancel->setText(QCoreApplication::translate("nmWxDisplaySettings", "Cancel")); pCancel->setIcon(zxLoadIcon("Cancel")); pCancel->setAutoDefault(false); pLayout->addWidget(pButtons); pMain->addWidget(pFooter); return pButtons; } } // 会话层只负责对象身份、工作副本和提交,不持有树控件,不按名称或翻译文字定位对象。 class nmDisplaySettingsSession { public: // 分类值只用于会话与界面的对应,不写入工程文件。 enum Category { Contour, Faults, Images, Wells, Fractures, Regions, RegionMarks, CategoryCount }; struct Entry { Category eCategory; // 所属显示分类 QString sName; // 显示名称,允许重名 QPointer pGraphic; // 非拥有引用,图元删除后自动置空 QPointer pData; // 对应业务对象,检测工程加载后的数据替换 bool bOriginalVisible; // 打开窗口或上次应用后的实际状态 }; // 从指定地图读取快照;不调用会修改 Manager 的显示设置刷新接口。 explicit nmDisplaySettingsSession(nmGuiPlot* pMap) : m_pMap(pMap), m_pManager(pMap != NULL ? pMap->boundDataManager() : NULL) { if(m_pManager.isNull() || m_pMap.isNull() || m_pMap->m_pPlot == NULL || m_pMap->graphicBinder() == NULL) return; m_oImage = m_oOriginalImage = m_pManager->getBackgroundImageInfo(); const QVector vecGraphics = pMap->getAllPlots(); foreach(nmObjBase* pGraphic, vecGraphics) { Category eCategory = Contour; QObject* pData = dataFor(pGraphic, eCategory); if(pData == NULL) continue; // 测量线、比例尺线等临时工具不进入模型显示树。 Entry oEntry; oEntry.eCategory = eCategory; oEntry.sName = pGraphic->getName(); oEntry.pGraphic = pGraphic; oEntry.pData = pData; oEntry.bOriginalVisible = pGraphic->isVisible(); m_vecEntries.append(oEntry); } } // 查找当前绑定关系;使用指针身份避免同名裂缝或重名区域相互覆盖。 QObject* dataFor(nmObjBase* pGraphic, Category& eCategory) const { if(pGraphic == NULL || m_pMap.isNull()) return NULL; nmPlotGraphicBinder* pBinder = m_pMap->graphicBinder(); if(pBinder == NULL) return NULL; if(nmDataOutline* pData = pBinder->getOutlineDataFor(pGraphic)) { eCategory = Contour; return pData; } if(nmObjLineFault* pFault = qobject_cast(pGraphic)) { eCategory = Faults; return pBinder->getFaultDataFor(pFault); } if(nmObjLineCrack* pCrack = qobject_cast(pGraphic)) { eCategory = Fractures; return pBinder->getCrackDataFor(pCrack); } if(nmObjPointWell* pWell = qobject_cast(pGraphic)) { eCategory = Wells; return pBinder->getWellDataFor(pWell); } if(nmObjRegion* pRegion = qobject_cast(pGraphic)) { eCategory = Regions; return pBinder->getRegionDataFor(pRegion); } if(nmObjRegionMark* pMark = qobject_cast(pGraphic)) { eCategory = RegionMarks; return pBinder->getRegionMarkDataFor(pMark); } return NULL; } // 检查打开期间的分析切换、对象重建和外部显隐修改,过期会话不得覆盖当前状态。 bool isCurrent() const { if(m_pMap.isNull() || m_pManager.isNull() || m_pMap->m_pPlot == NULL || m_pMap->boundDataManager() != m_pManager.data() || !sameImage(m_oOriginalImage, m_pManager->getBackgroundImageInfo())) return false; for(int nIndex = 0; nIndex < m_vecEntries.size(); ++nIndex) { const Entry& oEntry = m_vecEntries[nIndex]; Category eCategory = oEntry.eCategory; if(oEntry.pGraphic.isNull() || oEntry.pData.isNull() || dataFor(oEntry.pGraphic, eCategory) != oEntry.pData.data() || eCategory != oEntry.eCategory || oEntry.pGraphic->isVisible() != oEntry.bOriginalVisible) return false; } return true; } // 先验证全部对象并处理可能失败的图片加载,再提交不会失败的显隐状态。 bool apply(const QVector& vecVisible, QString& sError) { if(!isCurrent() || vecVisible.size() != m_vecEntries.size()) { sError = QCoreApplication::translate("nmWxDisplaySettings", "The map has changed. Reopen display settings."); return false; } if(!sameImage(m_oImage, m_oOriginalImage) && !m_pMap->applyBackgroundImage(m_oImage, sError)) return false; bool bChanged = !sameImage(m_oImage, m_oOriginalImage); for(int nIndex = 0; nIndex < m_vecEntries.size(); ++nIndex) { Entry& oEntry = m_vecEntries[nIndex]; if(vecVisible[nIndex] == oEntry.bOriginalVisible) continue; oEntry.pGraphic->setVisible(vecVisible[nIndex]); // Binder 同步业务对象的可见性。 oEntry.bOriginalVisible = vecVisible[nIndex]; bChanged = true; } m_oImage = m_oOriginalImage = m_pManager->getBackgroundImageInfo(); if(bChanged) { m_pMap->setModified(true); m_pMap->update(); } return true; } QPointer m_pMap; // 会话所属地图,不拥有对象 QPointer m_pManager; // 会话所属分析,不访问全局当前实例 QVector m_vecEntries; // 图元快照,顺序与界面节点对应 BackgroundImageInfo m_oImage; // 尚未提交的图片工作副本 BackgroundImageInfo m_oOriginalImage; // 上次提交后的图片,用于变更和冲突判断 }; // 直接在预览区绘制原图,避免 QLabel 的大尺寸 pixmap 反过来撑大窗口。 class nmReservoirMapPreview : public QWidget { public: // 初始尺寸随字体变化,实际绘图区由布局分配。 explicit nmReservoirMapPreview(QWidget* pParent) : QWidget(pParent) { setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); } // 保存共享图片并请求重绘,窗口缩放不会修改工程图片分辨率。 void setImage(const QImage& oImage) { m_oImage = oImage; update(); } // 建议预览尺寸不构成固定宽高限制。 virtual QSize sizeHint() const { return QSize(fontMetrics().height() * 26, fontMetrics().height() * 16); } // 缩小窗口时仍保留可辨认的预览区域。 virtual QSize minimumSizeHint() const { return QSize(fontMetrics().height() * 12, fontMetrics().height() * 8); } protected: // 保持原图宽高比居中绘制,未导入时显示简洁空状态。 virtual void paintEvent(QPaintEvent*) { QPainter oPainter(this); oPainter.fillRect(rect(), palette().brush(QPalette::Base)); if(m_oImage.isNull()) { oPainter.setPen(palette().color(QPalette::Text)); oPainter.drawText(rect(), Qt::AlignCenter, QCoreApplication::translate( "nmWxReservoirMapSettings", "No reservoir map")); return; } QSize oSize = m_oImage.size(); oSize.scale(contentsRect().size(), Qt::KeepAspectRatio); QRect oTarget(QPoint(0, 0), oSize); oTarget.moveCenter(contentsRect().center()); oPainter.setRenderHint(QPainter::SmoothPixmapTransform); oPainter.drawImage(oTarget, m_oImage); } private: QImage m_oImage; // 原图共享副本,绘制时缩放,不生成额外的全尺寸 pixmap }; nmWxReservoirMapSettings::nmWxReservoirMapSettings(const BackgroundImageInfo& oImage, QWidget* pParent) : iDlgBase(pParent), m_pImage(new BackgroundImageInfo(oImage)) { setWindowTitle(tr("Reservoir map settings")); setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint); QVBoxLayout* pMain = new QVBoxLayout(this); pMain->setContentsMargins(12, 10, 12, 0); pMain->setSpacing(10); m_pPreview = new nmReservoirMapPreview(this); pMain->addWidget(m_pPreview, 1); QHBoxLayout* pPathRow = new QHBoxLayout; pPathRow->addWidget(new QLabel(tr("Source file"), this)); m_pSourcePath = new QLineEdit(this); m_pSourcePath->setReadOnly(true); m_pSourcePath->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); pPathRow->addWidget(m_pSourcePath, 1); pMain->addLayout(pPathRow); QHBoxLayout* pActions = new QHBoxLayout; m_pImportButton = new QPushButton(this); m_pRemoveButton = new QPushButton(tr("Remove"), this); m_pImportButton->setAutoDefault(false); m_pRemoveButton->setAutoDefault(false); pActions->addWidget(m_pImportButton, 1); pActions->addWidget(m_pRemoveButton, 1); pMain->addLayout(pActions); QDialogButtonBox* pButtons = createButtons(this, pMain); connect(m_pImportButton, SIGNAL(clicked()), this, SLOT(onImportClicked())); connect(m_pRemoveButton, SIGNAL(clicked()), this, SLOT(onRemoveClicked())); connect(pButtons, SIGNAL(accepted()), this, SLOT(accept())); connect(pButtons, SIGNAL(rejected()), this, SLOT(reject())); refreshImage(); adjustSize(); } nmWxReservoirMapSettings::~nmWxReservoirMapSettings() { delete m_pImage; } const BackgroundImageInfo& nmWxReservoirMapSettings::imageInfo() const { return *m_pImage; } void nmWxReservoirMapSettings::onImportClicked() { if(nmGuiPlot::chooseBackgroundImage(this, *m_pImage)) refreshImage(); } void nmWxReservoirMapSettings::onRemoveClicked() { *m_pImage = BackgroundImageInfo(); refreshImage(); } void nmWxReservoirMapSettings::refreshImage() { const bool bHasImage = !m_pImage->objImage.isNull(); m_pPreview->setImage(m_pImage->objImage); m_pSourcePath->setText(m_pImage->sFilePath); m_pSourcePath->setToolTip(m_pImage->sFilePath); m_pSourcePath->setCursorPosition(0); m_pImportButton->setText(bHasImage ? tr("Replace") : tr("Import")); m_pRemoveButton->setEnabled(bHasImage); } nmWxDisplaySettings::nmWxDisplaySettings(nmGuiPlot* pMap, QWidget* pParent) : iDlgBase(pParent), m_pSession(new nmDisplaySettingsSession(pMap)), m_pImageItem(NULL) { setWindowTitle(tr("Display settings")); setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint); QVBoxLayout* pMain = new QVBoxLayout(this); pMain->setContentsMargins(12, 10, 12, 0); pMain->setSpacing(10); m_pTreeWidget = new nmDisplayTreeWidget(this); m_pTreeWidget->setHeaderHidden(true); m_pTreeWidget->setColumnCount(1); m_pTreeWidget->setUniformRowHeights(true); m_pTreeWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); pMain->addWidget(m_pTreeWidget, 1); populateTree(); QPushButton* pImageSettings = new QPushButton(tr("Reservoir map settings"), this); pImageSettings->setAutoDefault(false); pMain->addWidget(pImageSettings); QPushButton* pApplyButton = NULL; QDialogButtonBox* pButtons = createButtons(this, pMain, &pApplyButton); connect(pImageSettings, SIGNAL(clicked()), this, SLOT(onImageSettingsClicked())); connect(pApplyButton, SIGNAL(clicked()), this, SLOT(onApplyClicked())); connect(pButtons, SIGNAL(accepted()), this, SLOT(accept())); connect(pButtons, SIGNAL(rejected()), this, SLOT(reject())); const bool bAvailable = m_pSession->isCurrent(); m_pTreeWidget->setEnabled(bAvailable); pImageSettings->setEnabled(bAvailable); pApplyButton->setEnabled(bAvailable); pButtons->button(QDialogButtonBox::Ok)->setEnabled(bAvailable); if(pMap != NULL) connect(pMap, SIGNAL(destroyed()), this, SLOT(reject())); if(!m_pSession->m_pManager.isNull()) connect(m_pSession->m_pManager.data(), SIGNAL(destroyed()), this, SLOT(reject())); adjustSize(); } nmWxDisplaySettings::~nmWxDisplaySettings() { delete m_pSession; } void nmWxDisplaySettings::populateTree() { QTreeWidgetItem* aCategories[nmDisplaySettingsSession::CategoryCount]; const QStringList listNames = QStringList() << tr("Contour") << tr("Faults") << tr("Images") << tr("Wells") << tr("Fractures") << tr("Regions") << tr("RegionMarks"); for(int nCategory = 0; nCategory < nmDisplaySettingsSession::CategoryCount; ++nCategory) { QTreeWidgetItem* pItem = new QTreeWidgetItem(m_pTreeWidget); pItem->setText(0, listNames[nCategory]); pItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable); pItem->setCheckState(0, Qt::Unchecked); aCategories[nCategory] = pItem; } for(int nIndex = 0; nIndex < m_pSession->m_vecEntries.size(); ++nIndex) { const nmDisplaySettingsSession::Entry& oEntry = m_pSession->m_vecEntries[nIndex]; QTreeWidgetItem* pCategory = aCategories[oEntry.eCategory]; QTreeWidgetItem* pItem = pCategory; if(oEntry.eCategory != nmDisplaySettingsSession::Contour) { pCategory->setFlags(pCategory->flags() | Qt::ItemIsTristate); pItem = new QTreeWidgetItem(pCategory); pItem->setText(0, oEntry.sName); pItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable); } pItem->setCheckState(0, oEntry.bOriginalVisible ? Qt::Checked : Qt::Unchecked); m_vecEntryItems.append(pItem); } // 无对象的分类仍展示,但不提供没有实际作用的勾选操作。 for(int nCategory = 0; nCategory < nmDisplaySettingsSession::CategoryCount; ++nCategory) { QTreeWidgetItem* pItem = aCategories[nCategory]; if(pItem->childCount() == 0 && !m_vecEntryItems.contains(pItem)) pItem->setDisabled(true); } m_pImageItem = aCategories[nmDisplaySettingsSession::Images]; m_pImageItem->setDisabled(m_pSession->m_oImage.objImage.isNull()); m_pImageItem->setCheckState(0, m_pSession->m_oImage.bIsVisible ? Qt::Checked : Qt::Unchecked); m_pTreeWidget->expandAll(); } void nmWxDisplaySettings::onImageSettingsClicked() { m_pSession->m_oImage.bIsVisible = m_pImageItem->checkState(0) == Qt::Checked; nmWxReservoirMapSettings oDialog(m_pSession->m_oImage, this); if(oDialog.exec() != QDialog::Accepted) return; m_pSession->m_oImage = oDialog.imageInfo(); m_pImageItem->setDisabled(m_pSession->m_oImage.objImage.isNull()); m_pImageItem->setCheckState(0, m_pSession->m_oImage.bIsVisible ? Qt::Checked : Qt::Unchecked); } bool nmWxDisplaySettings::applyChanges() { QVector vecVisible; for(int nIndex = 0; nIndex < m_vecEntryItems.size(); ++nIndex) vecVisible.append(m_vecEntryItems[nIndex]->checkState(0) == Qt::Checked); m_pSession->m_oImage.bIsVisible = !m_pSession->m_oImage.objImage.isNull() && m_pImageItem->checkState(0) == Qt::Checked; QString sError; if(m_pSession->apply(vecVisible, sError)) return true; QMessageBox::warning(this, tr("Display settings"), sError); return false; } void nmWxDisplaySettings::onApplyClicked() { applyChanges(); } void nmWxDisplaySettings::accept() { if(applyChanges()) iDlgBase::accept(); }