|
|
#ifndef NMDFNUISUPPORT_H
|
|
|
#define NMDFNUISUPPORT_H
|
|
|
|
|
|
#include "nmDfnUnits.h"
|
|
|
#include "ZxBaseUtil.h"
|
|
|
#include <QComboBox>
|
|
|
#include <QDialogButtonBox>
|
|
|
#include <QGridLayout>
|
|
|
#include <QHeaderView>
|
|
|
#include <QLabel>
|
|
|
#include <QLineEdit>
|
|
|
#include <QPushButton>
|
|
|
#include <QTableView>
|
|
|
#include <QtCore/qnumeric.h>
|
|
|
|
|
|
// DFN 内部的单位与外观约定;所有系数来自框架,绝不改写全局单位配置。
|
|
|
namespace nmDfnUi
|
|
|
{
|
|
|
// 从框架填充单位;只有基准单位时仍可正常使用,但禁用无意义切换。
|
|
|
inline void units(QComboBox* pCombo, const QString& sBase)
|
|
|
{
|
|
|
const QString sUnit = unitName(sBase);
|
|
|
iUnitGroup* pGroup = iUnitHelper::getUnitGroupByUnit(sUnit);
|
|
|
pCombo->clear();
|
|
|
if (pGroup) pCombo->addItems(pGroup->getAllUnitNames());
|
|
|
if (pCombo->findText(sUnit) < 0) pCombo->addItem(sUnit);
|
|
|
pCombo->setCurrentIndex(pCombo->findText(sUnit));
|
|
|
pCombo->setEnabled(pCombo->count() > 1);
|
|
|
pCombo->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
|
|
|
pCombo->setSizeAdjustPolicy(QComboBox::AdjustToContents);
|
|
|
}
|
|
|
// 标准按钮保留主题高度,禁用回车意外触发的自动默认按钮。
|
|
|
inline QPushButton* button(const QString& sText, QWidget* pParent)
|
|
|
{
|
|
|
QPushButton* pButton = new QPushButton(sText, pParent);
|
|
|
pButton->setAutoDefault(false);
|
|
|
return pButton;
|
|
|
}
|
|
|
// 创建统一的确定/取消底栏,翻译由对应窗口提供。
|
|
|
inline QDialogButtonBox* buttons(QWidget* pParent, const QString& sAccept, const QString& sCancel)
|
|
|
{
|
|
|
QDialogButtonBox* pButtons = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel,
|
|
|
Qt::Horizontal, pParent);
|
|
|
pButtons->button(QDialogButtonBox::Ok)->setText(sAccept);
|
|
|
pButtons->button(QDialogButtonBox::Cancel)->setText(sCancel);
|
|
|
pButtons->button(QDialogButtonBox::Ok)->setIcon(zxLoadIcon("OK"));
|
|
|
pButtons->button(QDialogButtonBox::Cancel)->setIcon(zxLoadIcon("Cancel"));
|
|
|
pButtons->button(QDialogButtonBox::Ok)->setAutoDefault(false);
|
|
|
pButtons->button(QDialogButtonBox::Cancel)->setAutoDefault(false);
|
|
|
pButtons->setContentsMargins(4, 5, 4, 5);
|
|
|
return pButtons;
|
|
|
}
|
|
|
// 表单采用与已统一窗口相同的间距和数值/单位宽度比例。
|
|
|
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 void label(QGridLayout* pGrid, int nRow, const QString& sText, QWidget* pParent)
|
|
|
{
|
|
|
QLabel* pLabel = new QLabel(sText, pParent);
|
|
|
pLabel->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
|
|
|
pGrid->addWidget(pLabel, nRow, 0);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 一个独立的数值/单位字段;缓存基准值,显示舍入和反复切换不会降低精度。
|
|
|
class nmDfnValueField
|
|
|
{
|
|
|
public:
|
|
|
// 创建字段并加入表单;Qt 控件由父窗口持有,字段自身只保存状态。
|
|
|
nmDfnValueField(QGridLayout* pGrid, int nRow, const QString& sLabel,
|
|
|
const QString& sBaseUnit, double dValue, QWidget* pParent)
|
|
|
: m_pEdit(new QLineEdit(pParent)), m_pUnit(new QComboBox(pParent)),
|
|
|
m_sBaseUnit(nmDfnUi::unitName(sBaseUnit)), m_sDisplayUnit(m_sBaseUnit), m_dBaseValue(dValue)
|
|
|
{
|
|
|
nmDfnUi::units(m_pUnit, m_sBaseUnit);
|
|
|
nmDfnUi::label(pGrid, nRow, sLabel, pParent);
|
|
|
pGrid->addWidget(m_pEdit, nRow, 1);
|
|
|
pGrid->addWidget(m_pUnit, nRow, 2);
|
|
|
m_sShownText = nmDfnUi::number(dValue);
|
|
|
m_pEdit->setText(m_sShownText);
|
|
|
}
|
|
|
// 暴露控件仅用于信号连接,不转移所有权。
|
|
|
QLineEdit* editor() const { return m_pEdit; }
|
|
|
// 暴露单位框用于识别切换信号。
|
|
|
QComboBox* unitCombo() const { return m_pUnit; }
|
|
|
// 仅在文字真正变化时更新原值,非法输入返回失败。
|
|
|
bool read(double& dValue)
|
|
|
{
|
|
|
if (m_pEdit->text() != m_sShownText) {
|
|
|
bool bOk = false;
|
|
|
const double dDisplay = m_pEdit->text().toDouble(&bOk);
|
|
|
if (!bOk || !nmDfnUi::convert(dDisplay, m_sDisplayUnit, m_sBaseUnit, m_dBaseValue)) return false;
|
|
|
m_sShownText = m_pEdit->text();
|
|
|
}
|
|
|
dValue = m_dBaseValue;
|
|
|
return qIsFinite(dValue);
|
|
|
}
|
|
|
// 按旧单位读取后切换显示;失败时恢复旧单位,保留待修正文字。
|
|
|
void changeUnit()
|
|
|
{
|
|
|
double dBase = 0.0, dDisplay = 0.0;
|
|
|
if (!read(dBase) || !nmDfnUi::convert(dBase, m_sBaseUnit, m_pUnit->currentText(), dDisplay)) {
|
|
|
const bool bBlocked = m_pUnit->blockSignals(true);
|
|
|
m_pUnit->setCurrentIndex(m_pUnit->findText(m_sDisplayUnit));
|
|
|
m_pUnit->blockSignals(bBlocked);
|
|
|
return;
|
|
|
}
|
|
|
m_sDisplayUnit = m_pUnit->currentText();
|
|
|
m_sShownText = nmDfnUi::number(dDisplay);
|
|
|
const bool bBlocked = m_pEdit->blockSignals(true);
|
|
|
m_pEdit->setText(m_sShownText);
|
|
|
m_pEdit->blockSignals(bBlocked);
|
|
|
}
|
|
|
private:
|
|
|
// 字段状态与一组控件一一对应,禁止复制后共享同一组编辑器。
|
|
|
nmDfnValueField(const nmDfnValueField&);
|
|
|
// 禁止赋值,避免基准值缓存与控件归属分离。
|
|
|
nmDfnValueField& operator=(const nmDfnValueField&);
|
|
|
QLineEdit* m_pEdit; // 数字输入框,由父窗口释放。
|
|
|
QComboBox* m_pUnit; // 框架单位框,由父窗口释放。
|
|
|
QString m_sBaseUnit; // 计算使用的基准单位。
|
|
|
QString m_sDisplayUnit; // 上次成功显示的单位,用于解释尚未提交的文字。
|
|
|
QString m_sShownText; // 与基准值对应的显示文本。
|
|
|
double m_dBaseValue; // 完整精度基准值。
|
|
|
};
|
|
|
|
|
|
// 模型驱动表格仅绘制可见数据,统一初始推荐尺寸与拉伸方式。
|
|
|
class nmDfnTableView : public QTableView
|
|
|
{
|
|
|
public:
|
|
|
// 推荐行数仅用于 sizeHint,不限制窗口高度和可查看的数据量。
|
|
|
explicit nmDfnTableView(int nRows, QWidget* pParent)
|
|
|
: QTableView(pParent), m_nPreferredRows(nRows)
|
|
|
{
|
|
|
setAlternatingRowColors(true);
|
|
|
setEditTriggers(QAbstractItemView::NoEditTriggers);
|
|
|
setSelectionBehavior(QAbstractItemView::SelectRows);
|
|
|
setSelectionMode(QAbstractItemView::ExtendedSelection);
|
|
|
horizontalHeader()->setMinimumSectionSize(fontMetrics().width('0') * 12);
|
|
|
horizontalHeader()->setResizeMode(QHeaderView::Stretch);
|
|
|
verticalHeader()->setResizeMode(QHeaderView::Fixed);
|
|
|
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
|
|
|
}
|
|
|
// 字体决定初始宽高,窗口变大时表格随布局拉伸。
|
|
|
QSize sizeHint() const
|
|
|
{
|
|
|
return QSize(fontMetrics().width('0') * 105,
|
|
|
horizontalHeader()->sizeHint().height() + verticalHeader()->defaultSectionSize() * m_nPreferredRows
|
|
|
+ frameWidth() * 2);
|
|
|
}
|
|
|
private:
|
|
|
int m_nPreferredRows; // 推荐可见行数,不是数据截断上限。
|
|
|
};
|
|
|
|
|
|
#endif // NMDFNUISUPPORT_H
|