|
|
#ifndef NMWELLBORESTORAGEUISUPPORT_H
|
|
|
#define NMWELLBORESTORAGEUISUPPORT_H
|
|
|
|
|
|
#include "nmParameterField.h"
|
|
|
#include "iUnitGroup.h"
|
|
|
#include "iUnitHelper.h"
|
|
|
#include "ZxBaseUtil.h"
|
|
|
|
|
|
#include <QAbstractItemModel>
|
|
|
#include <QByteArray>
|
|
|
#include <QComboBox>
|
|
|
#include <QDialogButtonBox>
|
|
|
#include <QGridLayout>
|
|
|
#include <QHeaderView>
|
|
|
#include <QLabel>
|
|
|
#include <QKeyEvent>
|
|
|
#include <QLineEdit>
|
|
|
#include <QPersistentModelIndex>
|
|
|
#include <QPointer>
|
|
|
#include <QPushButton>
|
|
|
#include <QStyledItemDelegate>
|
|
|
#include <QTableWidget>
|
|
|
#include <QtCore/qnumeric.h>
|
|
|
|
|
|
// 仅供井筒储集计算器及其子窗口共用,不依赖井、分析管理器或求解器。
|
|
|
namespace nmWellboreStorageUi
|
|
|
{
|
|
|
// 由框架单位表处理上标别名及换算;失败时不返回带错误单位的原始数值。
|
|
|
inline bool convert(double dValue, const QString& sFrom, const QString& sTo, double& dResult)
|
|
|
{
|
|
|
if (!qIsFinite(dValue)) return false;
|
|
|
const QString sSource = nmParameterField::resolveUnitSpelling(sFrom);
|
|
|
const QString sTarget = nmParameterField::resolveUnitSpelling(sTo);
|
|
|
if (sSource == sTarget) {
|
|
|
dResult = dValue;
|
|
|
return true;
|
|
|
}
|
|
|
iUnitGroup* pGroup = iUnitHelper::getUnitGroupByUnit(sSource);
|
|
|
int nDigits = 6;
|
|
|
return pGroup && pGroup->indexOf(sTarget) >= 0
|
|
|
&& pGroup->convert(sSource, dValue, sTarget, dResult, nDigits) && qIsFinite(dResult);
|
|
|
}
|
|
|
|
|
|
// 自动回显最多六位有效数字并省略尾零;极小值使用科学计数法,避免显示成零。
|
|
|
// 仅缩短显示文字,计算和回填使用完整精度缓存;不截断用户正在输入的数字。
|
|
|
inline QString number(double dValue)
|
|
|
{
|
|
|
return qIsFinite(dValue) ? QString::number(dValue, 'g', 6) : QString();
|
|
|
}
|
|
|
|
|
|
// 统一主题高度和横向伸展策略;单位选项完全来自框架,缺失时仅保留基准单位。
|
|
|
inline QString populateUnits(QComboBox* pCombo, const QString& sBaseUnit)
|
|
|
{
|
|
|
const QString sUnit = nmParameterField::resolveUnitSpelling(sBaseUnit);
|
|
|
iUnitGroup* pGroup = iUnitHelper::getUnitGroupByUnit(sUnit);
|
|
|
const bool bBlocked = pCombo->blockSignals(true);
|
|
|
pCombo->clear();
|
|
|
if (pGroup) pCombo->addItems(pGroup->getAllUnitNames());
|
|
|
if (pCombo->count() == 0) pCombo->addItem(sUnit);
|
|
|
pCombo->setCurrentIndex(pCombo->findText(sUnit));
|
|
|
pCombo->setEnabled(pGroup && pCombo->count() > 1);
|
|
|
pCombo->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
|
|
|
pCombo->setSizeAdjustPolicy(QComboBox::AdjustToContents);
|
|
|
pCombo->blockSignals(bBlocked);
|
|
|
return sUnit;
|
|
|
}
|
|
|
|
|
|
// 单位切换失败时恢复旧选择,保留用户尚未修正的输入,避免递归触发切换槽。
|
|
|
inline void restoreUnit(QComboBox* pCombo, const QString& sUnit)
|
|
|
{
|
|
|
const bool bBlocked = pCombo->blockSignals(true);
|
|
|
pCombo->setCurrentIndex(pCombo->findText(sUnit));
|
|
|
pCombo->blockSignals(bBlocked);
|
|
|
}
|
|
|
|
|
|
// 参数名称右对齐,字段和单位按 8:5 分配宽度;控件高度由字体与主题决定。
|
|
|
inline QGridLayout* form(QWidget* pParent)
|
|
|
{
|
|
|
pParent->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
|
|
|
QGridLayout* pGrid = new QGridLayout(pParent);
|
|
|
pGrid->setContentsMargins(8, 15, 8, 10);
|
|
|
pGrid->setHorizontalSpacing(6);
|
|
|
pGrid->setVerticalSpacing(6);
|
|
|
pGrid->setColumnStretch(1, 8);
|
|
|
pGrid->setColumnStretch(2, 5);
|
|
|
return pGrid;
|
|
|
}
|
|
|
|
|
|
// 标准确定/取消按钮,文本由调用窗口翻译,避免子窗口依赖彼此的翻译上下文。
|
|
|
inline QDialogButtonBox* buttons(QWidget* pParent, const QString& sOk, const QString& sCancel)
|
|
|
{
|
|
|
QDialogButtonBox* pButtons = new QDialogButtonBox(
|
|
|
QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, pParent);
|
|
|
pButtons->button(QDialogButtonBox::Ok)->setText(sOk);
|
|
|
pButtons->button(QDialogButtonBox::Ok)->setIcon(zxLoadIcon("OK"));
|
|
|
pButtons->button(QDialogButtonBox::Cancel)->setText(sCancel);
|
|
|
pButtons->button(QDialogButtonBox::Cancel)->setIcon(zxLoadIcon("Cancel"));
|
|
|
pButtons->button(QDialogButtonBox::Ok)->setAutoDefault(false);
|
|
|
pButtons->button(QDialogButtonBox::Cancel)->setAutoDefault(false);
|
|
|
pButtons->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
|
|
|
pButtons->setContentsMargins(4, 5, 4, 5);
|
|
|
return pButtons;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 一个数值/单位字段,缓存业务基准值;控件由窗口拥有,字段对象跟随父窗口释放。
|
|
|
class nmWellboreStorageUnitField : public QObject
|
|
|
{
|
|
|
public:
|
|
|
// 创建框架单位下拉框及输入框;只读字段用于计算结果。
|
|
|
nmWellboreStorageUnitField(QWidget* pParent, const QString& sBaseUnit, bool bReadOnly = false)
|
|
|
: QObject(pParent), m_pEdit(new QLineEdit(pParent)), m_pUnitCombo(new QComboBox(pParent)),
|
|
|
m_sBaseUnit(nmWellboreStorageUi::populateUnits(m_pUnitCombo, sBaseUnit)),
|
|
|
m_sDisplayUnit(m_sBaseUnit), m_dBaseValue(qQNaN())
|
|
|
{
|
|
|
m_pEdit->setReadOnly(bReadOnly);
|
|
|
m_pEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
|
|
|
}
|
|
|
// 将完整字段加入表单,不设置固定像素宽高。
|
|
|
void addTo(QGridLayout* pGrid, int nRow, const QString& sLabel)
|
|
|
{
|
|
|
QLabel* pLabel = new QLabel(sLabel, m_pEdit->parentWidget());
|
|
|
pLabel->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
|
|
|
pGrid->addWidget(pLabel, nRow, 0);
|
|
|
pGrid->addWidget(m_pEdit, nRow, 1);
|
|
|
pGrid->addWidget(m_pUnitCombo, nRow, 2);
|
|
|
}
|
|
|
// 返回编辑器供窗口连接输入变化信号,不转移所有权。
|
|
|
QLineEdit* editor() const { return m_pEdit; }
|
|
|
// 返回单位框供窗口连接切换信号,不转移所有权。
|
|
|
QComboBox* unitCombo() const { return m_pUnitCombo; }
|
|
|
// 未修改显示文本时直接读取原值;非法文本返回失败,不恢复或复用旧结果。
|
|
|
bool read(double& dValue)
|
|
|
{
|
|
|
if (m_pEdit->text() != m_sShownText) {
|
|
|
bool bOk = false;
|
|
|
const double dDisplay = m_pEdit->text().toDouble(&bOk);
|
|
|
double dBase = 0.0;
|
|
|
if (!bOk || !nmWellboreStorageUi::convert(dDisplay, m_sDisplayUnit, m_sBaseUnit, dBase))
|
|
|
return false;
|
|
|
m_dBaseValue = dBase;
|
|
|
m_sShownText = m_pEdit->text();
|
|
|
}
|
|
|
dValue = m_dBaseValue;
|
|
|
return qIsFinite(dValue);
|
|
|
}
|
|
|
// 写入基准值并刷新显示;失败则清空结果,避免继续显示过期数值。
|
|
|
bool setValue(double dValue)
|
|
|
{
|
|
|
double dDisplay = 0.0;
|
|
|
if (!nmWellboreStorageUi::convert(dValue, m_sBaseUnit, m_sDisplayUnit, dDisplay)) {
|
|
|
clear();
|
|
|
return false;
|
|
|
}
|
|
|
m_dBaseValue = dValue;
|
|
|
showValue(dDisplay);
|
|
|
return true;
|
|
|
}
|
|
|
// 先按旧单位收齐文本,再按候选单位显示;任何失败均恢复旧单位选择。
|
|
|
bool changeUnit()
|
|
|
{
|
|
|
const QString sNext = m_pUnitCombo->currentText();
|
|
|
double dBase = 0.0, dDisplay = 0.0;
|
|
|
// 无有效结果时仍允许选择显示单位,后续计算成功后按新单位回显。
|
|
|
if (m_pEdit->isReadOnly() && !qIsFinite(m_dBaseValue)) {
|
|
|
if (!nmWellboreStorageUi::convert(0.0, m_sBaseUnit, sNext, dDisplay)) {
|
|
|
nmWellboreStorageUi::restoreUnit(m_pUnitCombo, m_sDisplayUnit);
|
|
|
return false;
|
|
|
}
|
|
|
m_sDisplayUnit = sNext;
|
|
|
return true;
|
|
|
}
|
|
|
if (!read(dBase) || !nmWellboreStorageUi::convert(dBase, m_sBaseUnit, sNext, dDisplay)) {
|
|
|
nmWellboreStorageUi::restoreUnit(m_pUnitCombo, m_sDisplayUnit);
|
|
|
return false;
|
|
|
}
|
|
|
m_sDisplayUnit = sNext;
|
|
|
showValue(dDisplay);
|
|
|
return true;
|
|
|
}
|
|
|
// 清空无效结果及原值缓存,之后不能从旧文字获得可回填数值。
|
|
|
void clear()
|
|
|
{
|
|
|
m_dBaseValue = qQNaN();
|
|
|
m_sShownText.clear();
|
|
|
const bool bBlocked = m_pEdit->blockSignals(true);
|
|
|
m_pEdit->clear();
|
|
|
m_pEdit->blockSignals(bBlocked);
|
|
|
}
|
|
|
private:
|
|
|
// 程序回显屏蔽 textChanged;由业务窗口在操作结束时统一重新计算。
|
|
|
void showValue(double dDisplay)
|
|
|
{
|
|
|
m_sShownText = nmWellboreStorageUi::number(dDisplay);
|
|
|
const bool bBlocked = m_pEdit->blockSignals(true);
|
|
|
m_pEdit->setText(m_sShownText);
|
|
|
m_pEdit->blockSignals(bBlocked);
|
|
|
}
|
|
|
QLineEdit* m_pEdit; // 数值编辑器,Qt 父控件负责释放。
|
|
|
QComboBox* m_pUnitCombo; // 框架单位列表,Qt 父控件负责释放。
|
|
|
QString m_sBaseUnit; // 计算和跨窗口传值使用的基准单位。
|
|
|
QString m_sDisplayUnit; // 上次成功切换的单位,用于解释尚未提交的文字。
|
|
|
QString m_sShownText; // 与缓存原值对应的显示文字,避免单位往返损失精度。
|
|
|
double m_dBaseValue; // 完整精度的业务基准值,NaN 表示当前没有有效结果。
|
|
|
};
|
|
|
|
|
|
// 体积表格的编辑委托,负责在单位切换、删行和确定前提交尚未失焦的单元格。
|
|
|
class nmWellboreStorageCellDelegate : public QStyledItemDelegate
|
|
|
{
|
|
|
public:
|
|
|
// 委托由表格拥有,不持有业务数据。
|
|
|
explicit nmWellboreStorageCellDelegate(QObject* pParent) : QStyledItemDelegate(pParent), m_bRestoring(false) {}
|
|
|
// 创建文本编辑器,支持科学计数法且不截断原有精度。
|
|
|
QWidget* createEditor(QWidget* pParent, const QStyleOptionViewItem&, const QModelIndex& oIndex) const
|
|
|
{
|
|
|
m_pEditor = new QLineEdit(pParent);
|
|
|
m_oIndex = oIndex;
|
|
|
m_sOriginalText = oIndex.data(Qt::EditRole).toString();
|
|
|
m_oOriginalBaseValue = oIndex.data(Qt::UserRole);
|
|
|
if (m_pEditReceiver)
|
|
|
QObject::connect(m_pEditor, SIGNAL(textEdited(QString)), m_pEditReceiver, m_sEditSlot.constData());
|
|
|
return m_pEditor;
|
|
|
}
|
|
|
// 记录初始文本,仅进入编辑再离开不反向覆盖基准值。
|
|
|
void setEditorData(QWidget* pEditor, const QModelIndex& oIndex) const
|
|
|
{
|
|
|
QLineEdit* pLine = static_cast<QLineEdit*>(pEditor);
|
|
|
const QString sText = oIndex.data(Qt::EditRole).toString();
|
|
|
// 即时提交引起的 model 更新不能重置用户的光标和选区。
|
|
|
if (pLine->property("initialText").isValid() && pLine->text() == sText) return;
|
|
|
pLine->setText(sText);
|
|
|
pLine->setProperty("initialText", sText);
|
|
|
pLine->selectAll();
|
|
|
}
|
|
|
// 真正修改后交由窗口解析;非法文本保留供用户修正。
|
|
|
void setModelData(QWidget* pEditor, QAbstractItemModel* pModel, const QModelIndex& oIndex) const
|
|
|
{
|
|
|
QLineEdit* pLine = static_cast<QLineEdit*>(pEditor);
|
|
|
if (pLine->text() != pLine->property("initialText").toString())
|
|
|
pModel->setData(oIndex, pLine->text(), Qt::EditRole);
|
|
|
pLine->setProperty("initialText", pLine->text());
|
|
|
}
|
|
|
// Esc 保留表格原有的撤销单元格语义,并恢复完整精度基准值。
|
|
|
bool eventFilter(QObject* pObject, QEvent* pEvent)
|
|
|
{
|
|
|
if (pObject == m_pEditor && pEvent->type() == QEvent::KeyPress
|
|
|
&& static_cast<QKeyEvent*>(pEvent)->key() == Qt::Key_Escape && m_oIndex.isValid()) {
|
|
|
m_bRestoring = true;
|
|
|
m_pEditor->setText(m_sOriginalText);
|
|
|
emit commitData(m_pEditor);
|
|
|
// 即使用户重新输入了相同显示文本,也要恢复编辑前的未舍入原值。
|
|
|
QAbstractItemModel* pModel = const_cast<QAbstractItemModel*>(m_oIndex.model());
|
|
|
pModel->setData(m_oIndex, m_oOriginalBaseValue, Qt::UserRole);
|
|
|
m_bRestoring = false;
|
|
|
}
|
|
|
return QStyledItemDelegate::eventFilter(pObject, pEvent);
|
|
|
}
|
|
|
// 在撤销通知期间向业务窗口提供原始缓存,避免再次解析舍入显示文字。
|
|
|
bool restoredBaseValue(QVariant& oValue) const
|
|
|
{
|
|
|
oValue = m_oOriginalBaseValue;
|
|
|
return m_bRestoring;
|
|
|
}
|
|
|
// 将输入变化交给业务窗口即时重算,使最后一个单元格填完后即可点击确定。
|
|
|
void setEditReceiver(QObject* pReceiver, const char* pSlot)
|
|
|
{
|
|
|
m_pEditReceiver = pReceiver;
|
|
|
m_sEditSlot = pSlot;
|
|
|
}
|
|
|
// 即时写入当前文字但保留编辑器、光标及选区,不打断连续输入。
|
|
|
void commit()
|
|
|
{
|
|
|
if (m_pEditor && m_pEditor->isVisible() && m_oIndex.isValid()) emit commitData(m_pEditor);
|
|
|
}
|
|
|
// Qt 4 关闭编辑器后延迟删除,检查可见性和索引以免重复提交旧编辑器。
|
|
|
void finish()
|
|
|
{
|
|
|
if (!m_pEditor || !m_pEditor->isVisible() || !m_oIndex.isValid()) return;
|
|
|
QLineEdit* pEditor = m_pEditor;
|
|
|
emit commitData(pEditor);
|
|
|
emit closeEditor(pEditor, QAbstractItemDelegate::NoHint);
|
|
|
m_pEditor = nullptr;
|
|
|
}
|
|
|
private:
|
|
|
mutable QPointer<QLineEdit> m_pEditor; // 当前活动编辑器的弱引用。
|
|
|
mutable QPersistentModelIndex m_oIndex; // 当前编辑位置,结构变化时可检测失效。
|
|
|
QPointer<QObject> m_pEditReceiver; // 业务窗口弱引用,负责即时计算及确定按钮状态。
|
|
|
QByteArray m_sEditSlot; // 业务窗口接收输入变化的槽签名。
|
|
|
mutable QString m_sOriginalText; // 本次进入编辑时的显示文本,供 Esc 撤销。
|
|
|
mutable QVariant m_oOriginalBaseValue; // 本次进入编辑时的完整精度基准值。
|
|
|
bool m_bRestoring; // 撤销单元格期间通知业务层直接恢复基准缓存。
|
|
|
};
|
|
|
|
|
|
// 两个子窗口共用的主题表格,单位行不作为数据行,纵向空间交给表格伸展。
|
|
|
class nmWellboreStorageTable : public QTableWidget
|
|
|
{
|
|
|
public:
|
|
|
// 预留单位行;调用方按用途指定推荐数据行数和每列字符数,仅用于初始尺寸。
|
|
|
nmWellboreStorageTable(int nColumns, QWidget* pParent, int nPreferredRows, int nColumnCharacters)
|
|
|
: QTableWidget(1, nColumns, pParent), m_pDelegate(new nmWellboreStorageCellDelegate(this)),
|
|
|
m_nPreferredRows(qMax(1, nPreferredRows)), m_nColumnCharacters(qMax(1, nColumnCharacters))
|
|
|
{
|
|
|
setItemDelegate(m_pDelegate);
|
|
|
setAlternatingRowColors(true);
|
|
|
setSelectionBehavior(QAbstractItemView::SelectRows);
|
|
|
setSelectionMode(QAbstractItemView::SingleSelection);
|
|
|
horizontalHeader()->setResizeMode(QHeaderView::Stretch);
|
|
|
verticalHeader()->setResizeMode(QHeaderView::ResizeToContents);
|
|
|
setVerticalHeaderItem(0, new QTableWidgetItem(QString()));
|
|
|
}
|
|
|
// 按字体和推荐行数计算初始尺寸;仅提供布局建议,实际宽高仍可随窗口伸展。
|
|
|
QSize sizeHint() const
|
|
|
{
|
|
|
// 有数据时使用实际行高,避免主题行高与默认行高不同导致可见行数偏差。
|
|
|
const int nDataRowHeight = rowCount() > 1 ? rowHeight(1) : verticalHeader()->defaultSectionSize();
|
|
|
return QSize(fontMetrics().width(QLatin1Char('0')) * m_nColumnCharacters * columnCount()
|
|
|
+ verticalHeader()->sizeHint().width() + frameWidth() * 2,
|
|
|
horizontalHeader()->sizeHint().height() + rowHeight(0)
|
|
|
+ nDataRowHeight * m_nPreferredRows + frameWidth() * 2);
|
|
|
}
|
|
|
// 指定即时编辑通知的接收者,规格只读表不需要设置。
|
|
|
void setEditReceiver(QObject* pReceiver, const char* pSlot) { m_pDelegate->setEditReceiver(pReceiver, pSlot); }
|
|
|
// 撤销单元格时读取编辑前的基准缓存,正常编辑返回 false。
|
|
|
bool restoredBaseValue(QVariant& oValue) const { return m_pDelegate->restoredBaseValue(oValue); }
|
|
|
// 即时提交输入并保留编辑器,供体积窗口更新当前结果。
|
|
|
void commitEditing() { m_pDelegate->commit(); }
|
|
|
// 将表格活动编辑写回旧单位上下文,再执行后续业务操作。
|
|
|
void finishEditing() { m_pDelegate->finish(); }
|
|
|
private:
|
|
|
nmWellboreStorageCellDelegate* m_pDelegate; // 表格持有的文本编辑委托。
|
|
|
int m_nPreferredRows; // 初始推荐展示的数据行数,不含第 0 行单位行。
|
|
|
int m_nColumnCharacters; // 每列推荐容纳的字符数,结合当前字体计算列宽。
|
|
|
};
|
|
|
|
|
|
#endif // NMWELLBORESTORAGEUISUPPORT_H
|