You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nmWTAI-Platform/Src/nmNum/nmSubWxs/nmWxGeoRefDlg.cpp

367 lines
16 KiB
C++

#include "nmWxGeoRefDlg.h"
#include "nmUtmProjection.h"
#include "iUnitGroup.h"
#include "iUnitHelper.h"
#include "ZxBaseUtil.h"
#include <QComboBox>
#include <QDialogButtonBox>
#include <QGridLayout>
#include <QGroupBox>
#include <QHBoxLayout>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QVBoxLayout>
#include <QtCore/qnumeric.h>
namespace
{
// 创建与模型位置、测量工具一致的三列坐标网格,布局所有权交给分组控件。
QGridLayout* createCoordinateGrid(QGroupBox* pGroup)
{
pGroup->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
QGridLayout* pGrid = new QGridLayout(pGroup);
pGrid->setContentsMargins(8, 15, 8, 10);
pGrid->setHorizontalSpacing(6);
pGrid->setVerticalSpacing(6);
pGrid->setColumnStretch(1, 8);
pGrid->setColumnStretch(2, 5);
return pGrid;
}
}
nmWxGeoRefDlg::nmWxGeoRefDlg(const nmDataGeoRef& oData, QWidget* pParent)
: iDlgBase(pParent), m_oGeoRefData(oData),
m_pLengthUnitGroup(iUnitHelper::getUnitGroupByUnit("m")),
m_eLastInput(NoInput), m_bHasCoordinates(false), m_bInputValid(false),
m_bRefreshing(false), m_bChanged(false), m_pUtmZoneComboBox(nullptr), m_pOkButton(nullptr)
{
for (int nField = 0; nField < FieldCount; ++nField) {
m_dBaseValues[nField] = 0.0;
m_pCoordinateEdits[nField] = nullptr;
m_pLengthUnits[nField] = nullptr;
m_sDisplayUnits[nField] = nField < Easting ? "o" : "m";
}
setWindowTitle(tr("Geo referencing"));
setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
initUI();
loadDataToUI();
adjustSize(); // 初始尺寸来自字体、主题与布局,允许后续拉伸。
}
void nmWxGeoRefDlg::initUI()
{
QVBoxLayout* pMain = new QVBoxLayout(this);
pMain->setContentsMargins(12, 10, 12, 0);
pMain->setSpacing(10);
QGroupBox* pLocationGroup = new QGroupBox(tr("Location"), this);
QGridLayout* pLocationGrid = createCoordinateGrid(pLocationGroup);
const QString sDegree = QString(" (%1)").arg(QChar(0x00b0));
createCoordinateField(pLocationGrid, 0, Longitude, tr("Longitude") + sDegree, "geoLongitude");
createCoordinateField(pLocationGrid, 1, Latitude, tr("Latitude") + sDegree, "geoLatitude");
pMain->addWidget(pLocationGroup);
QGroupBox* pProjectionGroup = new QGroupBox(tr("Zone"), this);
QGridLayout* pProjectionGrid = createCoordinateGrid(pProjectionGroup);
QLabel* pZoneLabel = new QLabel(tr("UTM zone"), pProjectionGroup);
pZoneLabel->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
pProjectionGrid->addWidget(pZoneLabel, 0, 0);
m_pUtmZoneComboBox = new QComboBox(pProjectionGroup);
m_pUtmZoneComboBox->setObjectName("geoUtmZone");
m_pUtmZoneComboBox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
m_pUtmZoneComboBox->setSizeAdjustPolicy(QComboBox::AdjustToContents);
// 分带属于坐标标识,不是长度单位;选项沿用数据模型,过滤不存在的特殊带。
const QStringList listZones = m_oGeoRefData.getUtmZone().getListValueSelections();
foreach (const QString& sZone, listZones) {
int nZone = 0;
char cBand = 0;
if (nmUtmProjection::parseZone(sZone, nZone, cBand)) m_pUtmZoneComboBox->addItem(sZone);
}
m_pUtmZoneComboBox->setCurrentIndex(-1);
pProjectionGrid->addWidget(m_pUtmZoneComboBox, 0, 1, 1, 2);
createCoordinateField(pProjectionGrid, 1, Easting, tr("Easting"), "geoEasting");
createCoordinateField(pProjectionGrid, 2, Northing, tr("Northing"), "geoNorthing");
pMain->addWidget(pProjectionGroup);
// 两个分组按实际标签宽度对齐;数值与单位按 8:5 横向分配剩余宽度。
int nLabelWidth = 0;
const QList<QLabel*> listLabels = findChildren<QLabel*>();
foreach (QLabel* pLabel, listLabels) nLabelWidth = qMax(nLabelWidth, pLabel->sizeHint().width());
pLocationGrid->setColumnMinimumWidth(0, nLabelWidth);
pProjectionGrid->setColumnMinimumWidth(0, nLabelWidth);
pMain->addStretch(1); // 纵向余量留在内容与按钮之间,字段本身不被拉高。
QWidget* pFooter = new QWidget(this);
pFooter->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
QHBoxLayout* pFooterLayout = new QHBoxLayout(pFooter);
pFooterLayout->setContentsMargins(4, 5, 4, 5);
pFooterLayout->setSpacing(8);
pFooterLayout->addStretch();
QDialogButtonBox* pButtons = new QDialogButtonBox(
QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, pFooter);
m_pOkButton = pButtons->button(QDialogButtonBox::Ok);
m_pOkButton->setText(tr("OK"));
m_pOkButton->setIcon(zxLoadIcon("OK"));
m_pOkButton->setDefault(true);
QPushButton* pCancel = pButtons->button(QDialogButtonBox::Cancel);
pCancel->setText(tr("Cancel"));
pCancel->setIcon(zxLoadIcon("Cancel"));
pCancel->setAutoDefault(false);
pFooterLayout->addWidget(pButtons);
pMain->addWidget(pFooter);
connect(pButtons, SIGNAL(accepted()), this, SLOT(accept()));
connect(pButtons, SIGNAL(rejected()), this, SLOT(reject()));
connect(m_pUtmZoneComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(onUtmZoneChanged(int)));
}
void nmWxGeoRefDlg::createCoordinateField(QGridLayout* pGrid, int nRow, CoordinateField eField,
const QString& sLabel, const QString& sObjectName)
{
QWidget* pParent = pGrid->parentWidget();
QLabel* pLabel = new QLabel(sLabel, pParent);
pLabel->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
pLabel->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
pGrid->addWidget(pLabel, nRow, 0);
QLineEdit* pEdit = new QLineEdit(pParent);
pEdit->setObjectName(sObjectName);
pEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
m_pCoordinateEdits[eField] = pEdit;
pGrid->addWidget(pEdit, nRow, 1, 1, eField < Easting ? 2 : 1);
connect(pEdit, SIGNAL(textEdited(QString)), this, SLOT(onCoordinateEdited(QString)));
if (eField < Easting) return;
QComboBox* pUnit = new QComboBox(pParent);
pUnit->setObjectName(sObjectName + "Unit");
pUnit->setSizeAdjustPolicy(QComboBox::AdjustToContents);
pUnit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
if (m_pLengthUnitGroup) pUnit->addItems(m_pLengthUnitGroup->getAllUnitNames());
pUnit->setCurrentIndex(pUnit->findText("m"));
m_pLengthUnits[eField] = pUnit;
pGrid->addWidget(pUnit, nRow, 2);
connect(pUnit, SIGNAL(currentIndexChanged(int)), this, SLOT(onLengthUnitChanged(int)));
}
bool nmWxGeoRefDlg::unitsAvailable() const
{
return m_pLengthUnitGroup && m_pLengthUnits[Easting]->findText("m") >= 0
&& m_pLengthUnits[Northing]->findText("m") >= 0;
}
QString nmWxGeoRefDlg::displayValue(CoordinateField eField, double dValue, const QString& sUnit) const
{
int nDigits = 6; // 经纬度保持六位小数;投影坐标使用框架目标单位的显示精度。
if (eField >= Easting && (!m_pLengthUnitGroup
|| !m_pLengthUnitGroup->convert("m", dValue, sUnit, dValue, nDigits))) return QString();
if (!qIsFinite(dValue)) return QString();
const QString sText = nDigits >= 0 ? QString::number(dValue, 'f', qMin(nDigits, 15))
: QString::number(dValue, 'g', 12);
return dValue != 0.0 && sText.toDouble() == 0.0 ? QString::number(dValue, 'g', 12) : sText;
}
void nmWxGeoRefDlg::loadDataToUI()
{
m_bRefreshing = true;
const QVariant values[FieldCount] = {
m_oGeoRefData.getLongitude().getValue(), m_oGeoRefData.getLatitude().getValue(),
m_oGeoRefData.getEasting().getValue(), m_oGeoRefData.getNorthing().getValue()
};
bool bComplete = true; // 空值不能被 toDouble 默认转零后当成合法参考点。
for (int nField = 0; nField < FieldCount; ++nField) {
bool bNumber = false;
const double dValue = values[nField].toDouble(&bNumber);
bNumber = bNumber && qIsFinite(dValue);
bComplete = bComplete && bNumber;
m_dBaseValues[nField] = bNumber ? dValue : 0.0;
m_sShownTexts[nField] = bNumber
? displayValue(static_cast<CoordinateField>(nField), dValue, m_sDisplayUnits[nField])
: values[nField].toString();
m_pCoordinateEdits[nField]->setText(m_sShownTexts[nField]);
}
m_sUtmZone = m_oGeoRefData.getUtmZone().getValue().toString().trimmed();
m_pUtmZoneComboBox->setCurrentIndex(m_pUtmZoneComboBox->findText(m_sUtmZone));
int nZone = 0;
char cBand = 0;
m_bHasCoordinates = bComplete && nmUtmProjection::parseZone(m_sUtmZone, nZone, cBand)
&& m_pUtmZoneComboBox->currentIndex() >= 0
&& nmUtmProjection::isValidGeographic(m_dBaseValues[Latitude], m_dBaseValues[Longitude])
&& nmUtmProjection::isValidProjected(m_dBaseValues[Easting], m_dBaseValues[Northing]);
m_bInputValid = m_bHasCoordinates;
// 加载时不重新投影或写回舍入值,避免仅打开旧工程就移动参考点。
m_bRefreshing = false;
updateActions();
}
bool nmWxGeoRefDlg::readField(CoordinateField eField, double& dValue) const
{
const QString sText = m_pCoordinateEdits[eField]->text();
if (m_bHasCoordinates && sText == m_sShownTexts[eField]) {
dValue = m_dBaseValues[eField];
return qIsFinite(dValue);
}
bool bNumber = false;
dValue = sText.trimmed().toDouble(&bNumber);
if (!bNumber || !qIsFinite(dValue)) return false;
if (eField < Easting) return true;
int nDigits = -1; // 读取原始换算结果,不按显示小数位舍入。
return m_pLengthUnitGroup
&& m_pLengthUnitGroup->convert(m_sDisplayUnits[eField], dValue, "m", dValue, nDigits)
&& qIsFinite(dValue);
}
bool nmWxGeoRefDlg::updateCoordinates(InputSource eSource)
{
double dLongitude = 0.0, dLatitude = 0.0; // 待提交的地理坐标,单位为度。
double dEasting = 0.0, dNorthing = 0.0; // 待提交的投影坐标,单位为米。
QString sZone = m_pUtmZoneComboBox->currentText();
bool bValid = unitsAvailable();
if (eSource == GeographicInput) {
bValid = bValid && readField(Longitude, dLongitude) && readField(Latitude, dLatitude)
&& nmUtmProjection::geographicToUtm(dLatitude, dLongitude, sZone, dEasting, dNorthing);
} else if (eSource == ProjectedInput) {
int nZone = 0;
char cBand = 0;
bValid = bValid && nmUtmProjection::parseZone(sZone, nZone, cBand)
&& readField(Easting, dEasting) && readField(Northing, dNorthing)
&& nmUtmProjection::utmToGeographic(nZone, cBand, dEasting, dNorthing, dLatitude, dLongitude);
if (bValid) {
// 字母表示纬度带,不仅是南北半球;北向坐标跨带后同步更新字母。
sZone = nmUtmProjection::zoneText(nZone, nmUtmProjection::latitudeBand(dLatitude));
bValid = nmUtmProjection::parseZone(sZone, nZone, cBand);
}
} else {
return m_bInputValid;
}
bValid = bValid && m_pUtmZoneComboBox->findText(sZone) >= 0;
m_bInputValid = bValid;
if (bValid) applyCoordinates(dLongitude, dLatitude, dEasting, dNorthing, sZone, eSource);
updateActions();
return bValid;
}
void nmWxGeoRefDlg::applyCoordinates(double dLongitude, double dLatitude, double dEasting,
double dNorthing, const QString& sZone, InputSource eSource)
{
m_bRefreshing = true;
m_dBaseValues[Longitude] = dLongitude;
m_dBaseValues[Latitude] = dLatitude;
m_dBaseValues[Easting] = dEasting;
m_dBaseValues[Northing] = dNorthing;
m_sUtmZone = sZone;
m_pUtmZoneComboBox->setCurrentIndex(m_pUtmZoneComboBox->findText(sZone));
for (int nField = 0; nField < FieldCount; ++nField) {
// 正在输入的一组保留原文和光标;另一组只展示成功转换后的坐标。
const bool bSourceField = (eSource == GeographicInput && nField < Easting)
|| (eSource == ProjectedInput && nField >= Easting);
if (!bSourceField) m_pCoordinateEdits[nField]->setText(displayValue(
static_cast<CoordinateField>(nField), m_dBaseValues[nField], m_sDisplayUnits[nField]));
m_sShownTexts[nField] = m_pCoordinateEdits[nField]->text();
}
m_bHasCoordinates = true;
m_bInputValid = true;
m_bChanged = true;
m_bRefreshing = false;
}
void nmWxGeoRefDlg::onCoordinateEdited(const QString&)
{
if (m_bRefreshing) return;
for (int nField = 0; nField < FieldCount; ++nField) {
if (sender() != m_pCoordinateEdits[nField]) continue;
m_eLastInput = nField < Easting ? GeographicInput : ProjectedInput;
updateCoordinates(m_eLastInput);
return;
}
}
void nmWxGeoRefDlg::onLengthUnitChanged(int)
{
if (m_bRefreshing) return;
for (int nField = Easting; nField < FieldCount; ++nField) {
QComboBox* pUnit = m_pLengthUnits[nField];
if (sender() != pUnit) continue;
const QString sUnit = pUnit->currentText();
const QString sText = displayValue(static_cast<CoordinateField>(nField), m_dBaseValues[nField], sUnit);
m_bRefreshing = true;
if (!m_bInputValid || sText.isEmpty()) {
pUnit->setCurrentIndex(pUnit->findText(m_sDisplayUnits[nField]));
} else {
// 切换单位只刷新显示;不经过坐标反算,不改变工程值和修改标记。
m_sDisplayUnits[nField] = sUnit;
m_sShownTexts[nField] = sText;
m_pCoordinateEdits[nField]->setText(sText);
}
m_bRefreshing = false;
return;
}
}
void nmWxGeoRefDlg::onUtmZoneChanged(int nIndex)
{
if (m_bRefreshing || nIndex < 0) return;
int nZone = 0;
char cBand = 0;
const QString sSelected = m_pUtmZoneComboBox->itemText(nIndex);
double dLatitude = 0.0, dLongitude = 0.0, dEasting = 0.0, dNorthing = 0.0;
QString sZone;
if (!nmUtmProjection::parseZone(sSelected, nZone, cBand)) return;
if (!nmUtmProjection::zoneCenter(nZone, cBand, dLatitude, dLongitude)) return;
// 沿用选带后填入区域中心的交互;四个值由同一次正投影产生,避免默认坐标相互矛盾。
m_bInputValid = nmUtmProjection::geographicToUtm(dLatitude, dLongitude, sZone, dEasting, dNorthing)
&& sZone == sSelected;
if (m_bInputValid) {
m_eLastInput = NoInput;
applyCoordinates(dLongitude, dLatitude, dEasting, dNorthing, sZone, NoInput);
}
updateActions();
}
void nmWxGeoRefDlg::updateActions()
{
const bool bUnits = unitsAvailable();
// 尚未设置参考点时可直接输入经纬度,也可以先选 UTM 带再修改投影坐标。
// 某一组输入不完整时,暂时锁住另一组,避免切换编辑来源时丢弃未修正的文本。
const bool bGeographic = bUnits && (m_bInputValid || m_eLastInput != ProjectedInput);
const bool bProjected = bUnits && m_pUtmZoneComboBox->currentIndex() >= 0
&& (m_bInputValid || m_eLastInput != GeographicInput);
m_pCoordinateEdits[Longitude]->setEnabled(bGeographic);
m_pCoordinateEdits[Latitude]->setEnabled(bGeographic);
m_pCoordinateEdits[Easting]->setEnabled(bProjected);
m_pCoordinateEdits[Northing]->setEnabled(bProjected);
m_pLengthUnits[Easting]->setEnabled(bUnits && m_bInputValid);
m_pLengthUnits[Northing]->setEnabled(bUnits && m_bInputValid);
m_pUtmZoneComboBox->setEnabled(bUnits);
m_pOkButton->setEnabled(bUnits && m_bInputValid);
}
const nmDataGeoRef& nmWxGeoRefDlg::geoRefData() const
{
return m_oGeoRefData;
}
bool nmWxGeoRefDlg::hasChanges() const
{
return m_bChanged;
}
void nmWxGeoRefDlg::accept()
{
// 文本编辑即时检查,提交仍再次兜底;回车也不能绕过无效输入和单位服务检查。
if (!unitsAvailable() || !updateCoordinates(m_eLastInput)) return;
if (m_bChanged) {
m_oGeoRefData.getLongitude().setValue(m_dBaseValues[Longitude]);
m_oGeoRefData.getLatitude().setValue(m_dBaseValues[Latitude]);
m_oGeoRefData.getEasting().setValue(m_dBaseValues[Easting]);
m_oGeoRefData.getNorthing().setValue(m_dBaseValues[Northing]);
m_oGeoRefData.getUtmZone().setValue(m_sUtmZone);
// 持久化仍采用度和米;显示单位仅在本次窗口中使用,不改变旧工程的读取约定。
m_oGeoRefData.getLongitude().setUnit("o");
m_oGeoRefData.getLatitude().setUnit("o");
m_oGeoRefData.getEasting().setUnit("m");
m_oGeoRefData.getNorthing().setUnit("m");
}
iDlgBase::accept();
}