197 lines
4.4 KiB
C++
Executable File
197 lines
4.4 KiB
C++
Executable File
|
|
|
|
|
|
#include "lts_coord_widget.h"
|
|
#include "lts_coord_widget_p.h"
|
|
|
|
#include <LtsCoordTrans/lts_coord_trans.h>
|
|
#include <QToolButton>
|
|
|
|
namespace LtsCoordWidget
|
|
{
|
|
|
|
LtsCoordWidget::LtsCoordWidget(DisplayType dt, QWidget *parent)
|
|
: QWidget(parent)
|
|
, m_displayType(dt)
|
|
{
|
|
setFocusPolicy(Qt::StrongFocus);
|
|
rebuildUi();
|
|
}
|
|
|
|
void LtsCoordWidget::setCoordinate(const QPointF &geoCoord, bool bUseAsDefaultValue)
|
|
{
|
|
if (m_coordinate != geoCoord)
|
|
emit coordinateChanged(geoCoord);
|
|
if (bUseAsDefaultValue)
|
|
m_defaultCoordinate = geoCoord;
|
|
if (m_inputWidgetHandler)
|
|
m_inputWidgetHandler->setCoordinate(geoCoord);
|
|
}
|
|
|
|
void LtsCoordWidget::clear()
|
|
{
|
|
if (m_inputWidgetHandler)
|
|
m_inputWidgetHandler->clear();
|
|
}
|
|
|
|
void LtsCoordWidget::setCoordinate(double lon, double lat, bool bUseAsDefaultValue)
|
|
{
|
|
setCoordinate(QPointF(lon, lat), bUseAsDefaultValue);
|
|
}
|
|
|
|
QPointF LtsCoordWidget::coordinate() const
|
|
{
|
|
if (!m_inputWidgetHandler)
|
|
return QPointF();
|
|
return m_inputWidgetHandler->coordinate();
|
|
}
|
|
|
|
|
|
LtsCoordWidget::DisplayType LtsCoordWidget::displayType() const
|
|
{
|
|
return m_displayType;
|
|
}
|
|
|
|
void LtsCoordWidget::setDisplayType(LtsCoordWidget::DisplayType dt)
|
|
{
|
|
if (m_displayType == dt)
|
|
return;
|
|
m_displayType = dt;
|
|
rebuildUi();
|
|
}
|
|
|
|
bool LtsCoordWidget::withResetButton() const
|
|
{
|
|
return m_bWithResetButton;
|
|
}
|
|
|
|
void LtsCoordWidget::setWithResetButton(bool bWith)
|
|
{
|
|
if (m_bWithResetButton == bWith)
|
|
return;
|
|
m_bWithResetButton = bWith;
|
|
if (m_inputWidgetHandler)
|
|
m_inputWidgetHandler->resetButton()->setVisible(m_bWithResetButton);
|
|
}
|
|
|
|
bool LtsCoordWidget::showToolTip() const
|
|
{
|
|
return m_bShowToolTip;
|
|
}
|
|
|
|
void LtsCoordWidget::setShowToolTip(bool bShow)
|
|
{
|
|
if (m_bShowToolTip == bShow)
|
|
return;
|
|
m_bShowToolTip = bShow;
|
|
|
|
updateToolTip();
|
|
}
|
|
|
|
bool LtsCoordWidget::mgrsSquareEditable() const
|
|
{
|
|
return m_bMgrsSquareEditable;
|
|
}
|
|
|
|
void LtsCoordWidget::setMgrsSquareEditable(bool bEditable)
|
|
{
|
|
if (m_bMgrsSquareEditable == bEditable)
|
|
return;
|
|
m_bMgrsSquareEditable = bEditable;
|
|
if (m_inputWidgetHandler)
|
|
m_inputWidgetHandler->setSquaresEditable(bEditable);
|
|
}
|
|
|
|
void LtsCoordWidget::setWithSurroundingMgrsSquares(bool bEnable)
|
|
{
|
|
if (m_inputWidgetHandler)
|
|
m_inputWidgetHandler->setWithSurroundingSquare(bEnable);
|
|
}
|
|
|
|
void LtsCoordWidget::setMgrsSquares(const QVector<LtsCoordTrans::UtmCoordinate> &geoCoords)
|
|
{
|
|
if (m_inputWidgetHandler)
|
|
m_inputWidgetHandler->setMgrsSquares(geoCoords);
|
|
}
|
|
|
|
|
|
bool LtsCoordWidget::event(QEvent *event)
|
|
{
|
|
if (event->type() == QEvent::ToolTip)
|
|
updateToolTip();
|
|
return QWidget::event(event);
|
|
}
|
|
|
|
|
|
void LtsCoordWidget::rebuildUi()
|
|
{
|
|
|
|
|
|
|
|
|
|
if (m_inputWidgetHandler)
|
|
{
|
|
m_inputWidgetHandler->cleanup();
|
|
delete m_inputWidgetHandler;
|
|
delete layout();
|
|
}
|
|
|
|
switch (m_displayType)
|
|
{
|
|
case DisplayType::MGRS5:
|
|
m_inputWidgetHandler = new MgrsEdit(5, this);
|
|
break;
|
|
case DisplayType::MGRS7:
|
|
m_inputWidgetHandler = new MgrsEdit(7, this);
|
|
break;
|
|
case DisplayType::UTM:
|
|
|
|
qt_assert("DisplayType::UTM not yet implemented", __FILE__, __LINE__);
|
|
break;
|
|
case DisplayType::WGS84:
|
|
m_inputWidgetHandler = new Wgs84Edit(this);
|
|
break;
|
|
}
|
|
if (!m_inputWidgetHandler)
|
|
return;
|
|
|
|
m_inputWidgetHandler->setSquaresEditable(m_bMgrsSquareEditable);
|
|
m_inputWidgetHandler->resetButton()->setVisible(m_bWithResetButton);
|
|
connect(m_inputWidgetHandler->resetButton(), &QToolButton::clicked,
|
|
this, &LtsCoordWidget::onResetToDefaultCoordinate);
|
|
connect(m_inputWidgetHandler, &LtsCoordInputWidgetHandler::coordinateChanged,
|
|
this, &LtsCoordWidget::coordinateChanged);
|
|
connect(m_inputWidgetHandler, &LtsCoordInputWidgetHandler::invalidCoordinate,
|
|
this, &LtsCoordWidget::invalidCoordinate);
|
|
if (!m_defaultCoordinate.isNull())
|
|
m_inputWidgetHandler->setCoordinate(m_defaultCoordinate);
|
|
}
|
|
|
|
void LtsCoordWidget::updateToolTip()
|
|
{
|
|
QString tt;
|
|
if (m_bShowToolTip)
|
|
{
|
|
const QPointF p(coordinate());
|
|
if (!p.isNull())
|
|
{
|
|
const QString mgrs = LtsCoordTrans::mgrsFromGeographic(p.x(), p.y());
|
|
const QPointF p2(p * LtsCoordTrans::rad2deg());
|
|
tt = tr("MGRS: %1\nlat/lon: %2/%3").arg(mgrs).arg(p2.y(), 0, 'f', 10).arg(p2.x(), 0, 'f', 10);
|
|
}
|
|
}
|
|
setToolTip(tt);
|
|
}
|
|
|
|
|
|
void LtsCoordWidget::onResetToDefaultCoordinate()
|
|
{
|
|
if (!m_inputWidgetHandler)
|
|
return;
|
|
if (m_defaultCoordinate.isNull())
|
|
m_inputWidgetHandler->clear();
|
|
m_inputWidgetHandler->setCoordinate(m_defaultCoordinate);
|
|
}
|
|
|
|
}
|