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.
map-display/maplocationdialog.cpp

315 lines
10 KiB
C++

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include "maplocationdialog.h"
#include "MapEditor/UGMapEditorWnd.h"
#include "qdebug.h"
#include "qregularexpression.h"
#include "ui_maplocationdialog.h"
#include "angle.h"
#include <QRegularExpressionValidator>
#include "messagetips.h"
MapLocationDialog::MapLocationDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::MapLocationDialog)
{
ui->setupUi(this);
this->setWindowTitle("地图定位");
this->resize(600,400);
this->setWindowFlags(Qt::WindowCloseButtonHint); //关闭右上角帮助(?)按钮。
showAltitudeSetting(false);
this->is3DMap = false;
// ui->latitudeText->setTextMargins(10,0,10,0);
// ui->longitudeText->setTextMargins(10,0,10,0);
// ui->latitudeText->setInputMask("a09x000000x;");
// ui->longitudeText->setInputMask("a009x000000x;");
ui->longitudeText->setPlaceholderText("121.506377°(-180° to 180°)");
ui->latitudeText->setPlaceholderText("31.2451056°(-90° to 90°)");
ui->longitudeText->setText("121.506377°");
ui->latitudeText->setText("31.245105°");
//设置纬度输入范围()
QRegularExpression latRe("^(\\- |\\+)?([0-8]?\\d{1}\\.\\d*|90\\.0*|[0-8]?\\d{1}|90)[s\\-,;°]?$");
ui->latitudeText->setValidator(new QRegularExpressionValidator(latRe));
//设置经度输入范围()
QRegularExpression lonRe("^(\\- |\\+)?(((\\d|[1-9]\\d|1[0-7]\\d|0{1,3})\\.\\d*)|(\\d|[1-9]\\d|1[0-7]\\d|0{1,3})|180\\.0*|180)[s\\-,;°]?$");
ui->longitudeText->setValidator(new QRegularExpressionValidator(lonRe));
}
MapLocationDialog::~MapLocationDialog()
{
delete ui;
}
void MapLocationDialog::setMap(UGMapEditorWnd* pEditorWnd)
{
this->pEditorWnd = pEditorWnd;
this->is3DMap = false;
}
void MapLocationDialog::setScene(SceneControl *pSceneControl)
{
this->pSceneControl = pSceneControl;
showAltitudeSetting(true);
this->is3DMap = true;
}
void MapLocationDialog::on_acceptBtn_clicked()
{
QString lon = ui->longitudeText->text().remove(QRegExp("\\s"));
QString lat = ui->latitudeText->text().remove(QRegExp("\\s"));
if(lon.isEmpty() || lat.isEmpty())
{
QString str = "经度或纬度缺失 ";
MessageTips *mMessageTips = new MessageTips(str,this);
mMessageTips->show();
this->reject();
return;
}
//获取经纬度
QStringList lonList;
QStringList latList;
lonList = transformAngle(lon);
latList = transformAngle(lat);
Angle angle;
double dlon;
double dlat;
if(ui->checkBox->isChecked())
{
angle.DmstoDeg(lonList[0].toInt(),lonList[1].toInt(),lonList[2].toDouble(),dlon);
angle.DmstoDeg(latList[0].toInt(),latList[1].toInt(),latList[2].toDouble(),dlat);
}
else
{
dlon = lonList[0].toDouble();
dlat = latList[0].toDouble();
}
//定位
if(is3DMap)
{//三维场景
double altitude = ui->altitudeSpinBox->value();
double height = pSceneControl->GetScene3D()->GetHeight(dlon,dlat);
int offset;
if(height<=100)
offset = 10;
else
offset = height*0.1;
if((altitude-height)<offset && height>=0)
{
altitude = height + offset;
}
UGCameraState cs;
cs.dLat = UGMathEngine::DegreesToRadians(dlat);
cs.dLon = UGMathEngine::DegreesToRadians(dlon);
cs.dAltitude = altitude;
// cs.dDistance = 1000;
cs.dTilt = UGMathEngine::DegreesToRadians(80);
pSceneControl->GetCameraWorld()->FlyToCamera(cs,3000);
}
else
{//二维地图定位
//设置地图中心点,并刷新地图
// UGMapEditorWnd* pEditorWnd = (UGMapEditorWnd*)pMap->GetMapEditWnd();
pEditorWnd->SetCenter(UGPoint2D(dlon,dlat));
pEditorWnd->SetScale(1/20000.0);
pEditorWnd->Refresh();
}
this->accept();
}
void MapLocationDialog::on_cancelBtn_clicked()
{
this->close();
}
QStringList MapLocationDialog::transformAngle(QString angle)
{//提取字符串中的数字
QRegularExpression re("(\\-)?\\d+\\.?\\d*");
QRegularExpressionMatchIterator i = re.globalMatch(angle);
QStringList angleList;
while(i.hasNext())
{
QRegularExpressionMatch match = i.next();
angleList<< match.captured(0);
}
return angleList;
}
bool MapLocationDialog::isValidedDeg(QString str,int type)
{
// QRegularExpressionValidator re("^[NS](\\d{1,3}\\.?\\d{0,6})°$");
if(type == 1) //lat -90.0+90.0
{
QRegularExpression latRe("^(\\- |\\+)?([0-8]?\\d{1}\\.\\d*|90\\.0*|[0-8]?\\d{1}|90)°$");
QRegularExpressionValidator vad(latRe,0);
int pos = 0;
int suc = vad.validate(str,pos);
if(suc == 2) return true;
else return false;
}
if(type == 2) //lon -180 至 180
{
QRegularExpression lonRe("^(\\- |\\+)?(((\\d|[1-9]\\d|1[0-7]\\d|0{1,3})\\.\\d*)|(\\d|[1-9]\\d|1[0-7]\\d|0{1,3})|180\\.0*|180)°$");
QRegularExpressionValidator vad(lonRe,0);
int pos = 0;
int suc = vad.validate(str,pos);
if(suc == 2) return true;
else return false;
}
}
bool MapLocationDialog::isValidedDms(QString str,int type)
{// 度分秒形式
if(type == 1) //lat -90.0+90.0
{
QRegularExpression latRe("^(\\- |\\+)?((\\d|[1-8]\\d)[s\\-,;°](\\d|[0-5]\\d)[s\\-,;](\\d|[0-5]\\d)(\\.\\d*)?[s\\-,;\""”秒]?$");
QRegularExpressionValidator vad(latRe,0);
int pos = 0;
int suc = vad.validate(str,pos);
if(suc == 2) return true;
else return false;
}
if(type == 2) //lon -180 至 180
{
QRegularExpression lonRe("^(\\- |\\+)?((\\d|[1-9]\\d|1[0-7]\\d)[s\\-,;°](\\d|[0-5]\\d)[s\\-,;](\\d|[0-5]\\d)(\\.\\d*)?[s\\-,;\""”秒]?$");
QRegularExpressionValidator vad(lonRe,0);
int pos = 0;
int suc = vad.validate(str,pos);
if(suc == 2) return true;
else return false;
}
}
//设置高度设置显示
void MapLocationDialog::showAltitudeSetting(bool isShow)
{
if(isShow)
{
ui->label_3->setHidden(false);
ui->altitudeSpinBox->setHidden(false);//默认二维地图模式下隐藏高度选项
ui->altitudeSpinBox->setMaximum(8849);
ui->altitudeSpinBox->setMinimum(-416);
ui->altitudeSpinBox->setSuffix(" m");
}
else
{
ui->label_3->setHidden(true);
ui->altitudeSpinBox->setHidden(true);//默认二维地图模式下隐藏高度选项
}
}
void MapLocationDialog::on_checkBox_stateChanged(int arg1)
{
QString lon = ui->longitudeText->text().remove(QRegExp("\\s"));
QString lat = ui->latitudeText->text().remove(QRegExp("\\s"));
QStringList lonList;
QStringList latList;
if(!lon.isEmpty())
{
lonList = transformAngle(lon);
}
if(!lat.isEmpty())
{
latList = transformAngle(lat);
}
if(arg1 == 2) // 度分秒显示
{
// ui->latitudeText->setInputMask("\\N09°09'09x0000\";");
// ui->longitudeText->setInputMask("\\E09°09'09x0000\";");
//设置纬度输入范围()
QRegularExpression latRe("^(\\- |\\+)?((\\d|[1-8]\\d)[s\\-,;°](\\d|[0-5]\\d)[s\\-,;](\\d|[0-5]\\d)(\\.\\d*)?[s\\-,;\""”秒]?$");
ui->latitudeText->setValidator(new QRegularExpressionValidator(latRe));
//设置经度输入范围()
QRegularExpression lonRe("^(\\- |\\+)?((\\d|[1-9]\\d|1[0-7]\\d)[s\\-,;°](\\d|[0-5]\\d)[s\\-,;](\\d|[0-5]\\d)(\\.\\d*)?[s\\-,;\""”秒]?$");
ui->longitudeText->setValidator(new QRegularExpressionValidator(lonRe));
ui->longitudeText->setPlaceholderText("121°30'22.9572\"(-180° to 180°)");
ui->latitudeText->setPlaceholderText("31°14'42.3801\"(-90° to 90°)");
// if(lon.isEmpty()) ui->longitudeText->setText("E121°30'22.9572\"");
// if(lat.isEmpty()) ui->latitudeText->setText("N31°14'42.38016\"");
Angle angle;
if(!lonList.isEmpty())
{
int deg;
int min;
double sec;
angle.DegtoDms(lonList[0].toDouble(),deg,min,sec);
QString text = QString::number(deg) + "°" +
QString::number(min) + "'" +
QString::number(sec,'f',4) + "\"";
ui->longitudeText->setText(text);
}
if(!latList.isEmpty())
{
int deg;
int min;
double sec;
angle.DegtoDms(latList[0].toDouble(),deg,min,sec);
QString text = QString::number(deg) + "°" +
QString::number(min) + "'" +
QString::number(sec,'f',4) + "\"";
ui->latitudeText->setText(text);
}
}
else // 度显示
{
// ui->latitudeText->setInputMask("\\N09x000000°;");
// ui->longitudeText->setInputMask("\\E009x000000°;");
//设置纬度输入范围()
QRegularExpression latRe("^(\\- |\\+)?([0-8]?\\d{1}\\.\\d*|90\\.0*|[0-8]?\\d{1}|90)[s\\-,;°]?$");
ui->latitudeText->setValidator(new QRegularExpressionValidator(latRe));
//设置经度输入范围()
QRegularExpression lonRe("^(\\- |\\+)?(((\\d|[1-9]\\d|1[0-7]\\d|0{1,3})\\.\\d*)|(\\d|[1-9]\\d|1[0-7]\\d|0{1,3})|180\\.0*|180)[s\\-,;°]?$");
ui->longitudeText->setValidator(new QRegularExpressionValidator(lonRe));
ui->longitudeText->setPlaceholderText("121.506377°(-180° to 180°)");
ui->latitudeText->setPlaceholderText("31.245105°(-90° to 90°)");
// if(lon.isEmpty()) ui->longitudeText->setText("E121.506377°");
// if(lat.isEmpty()) ui->latitudeText->setText("N31.245105°");
Angle angle;
if(!lonList.isEmpty())
{
double deg;
angle.DmstoDeg(lonList[0].toInt(),lonList[1].toInt(),lonList[2].toDouble(),deg);
QString text = QString::number(deg,'f',6) + "°";
ui->longitudeText->setText(text);
}
if(!latList.isEmpty())
{
double deg;
angle.DmstoDeg(latList[0].toInt(),latList[1].toInt(),latList[2].toDouble(),deg);
QString text = QString::number(deg,'f',6) + "°";
ui->latitudeText->setText(text);
}
}
}
void MapLocationDialog::on_longitudeText_editingFinished()
{
// qDebug()<<"********************************************";
}