148 lines
4.0 KiB
C++
148 lines
4.0 KiB
C++
#include "dialogcamerasetting.h"
|
||
#include "ui_dialogcamerasetting.h"
|
||
|
||
#include <QDoubleValidator>
|
||
#include <QEvent>
|
||
#include <QGuiApplication>
|
||
#include <QInputMethod>
|
||
#include <QLineEdit>
|
||
#include <QTimer>
|
||
|
||
namespace
|
||
{
|
||
void SetupKeyboardEdit(QLineEdit* edit, QObject* filter, Qt::InputMethodHints hints)
|
||
{
|
||
if (!edit)
|
||
return;
|
||
|
||
edit->setAttribute(Qt::WA_InputMethodEnabled, true);
|
||
edit->setInputMethodHints(hints);
|
||
edit->installEventFilter(filter);
|
||
}
|
||
|
||
void ShowSystemKeyboard(QWidget* focusWidget)
|
||
{
|
||
if (!focusWidget)
|
||
return;
|
||
|
||
focusWidget->setFocus(Qt::MouseFocusReason);
|
||
|
||
QInputMethod* inputMethod = QGuiApplication::inputMethod();
|
||
if (inputMethod)
|
||
inputMethod->show();
|
||
|
||
QTimer::singleShot(0, focusWidget, []() {
|
||
QInputMethod* delayedInputMethod = QGuiApplication::inputMethod();
|
||
if (delayedInputMethod)
|
||
delayedInputMethod->show();
|
||
});
|
||
}
|
||
}
|
||
|
||
DialogCameraSetting::DialogCameraSetting(QWidget* parent)
|
||
: QDialog(parent)
|
||
, ui(new Ui::DialogCameraSetting)
|
||
{
|
||
ui->setupUi(this);
|
||
setWindowTitle(QStringLiteral("相机设置"));
|
||
|
||
// 输入校验:曝光为微秒,增益为倍数
|
||
ui->edit_exposure->setValidator(new QDoubleValidator(1.0, 1000000.0, 1, this));
|
||
ui->edit_gain->setValidator(new QDoubleValidator(0.0, 100.0, 2, this));
|
||
setupInputKeyboard();
|
||
}
|
||
|
||
DialogCameraSetting::~DialogCameraSetting()
|
||
{
|
||
delete ui;
|
||
}
|
||
|
||
void DialogCameraSetting::SetParams(const QString& serverIp, const DroneScrewCameraParams& cam)
|
||
{
|
||
m_serverIp = serverIp;
|
||
m_camera = cam;
|
||
loadToUi(serverIp, cam);
|
||
}
|
||
|
||
QString DialogCameraSetting::GetServerIp() const
|
||
{
|
||
return m_serverIp;
|
||
}
|
||
|
||
DroneScrewCameraParams DialogCameraSetting::GetCameraParams() const
|
||
{
|
||
return m_camera;
|
||
}
|
||
|
||
void DialogCameraSetting::loadToUi(const QString& serverIp, const DroneScrewCameraParams& cam)
|
||
{
|
||
if (ui->edit_ip) ui->edit_ip->setText(serverIp);
|
||
if (ui->edit_exposure) ui->edit_exposure->setText(QString::number(cam.exposure, 'f', 1));
|
||
if (ui->edit_gain) ui->edit_gain->setText(QString::number(cam.gain, 'f', 2));
|
||
}
|
||
|
||
void DialogCameraSetting::collectFromUi()
|
||
{
|
||
if (ui->edit_ip) m_serverIp = ui->edit_ip->text().trimmed();
|
||
if (ui->edit_exposure) { bool ok; double v = ui->edit_exposure->text().toDouble(&ok); if (ok) m_camera.exposure = v; }
|
||
if (ui->edit_gain) { bool ok; double v = ui->edit_gain->text().toDouble(&ok); if (ok) m_camera.gain = v; }
|
||
}
|
||
|
||
bool DialogCameraSetting::eventFilter(QObject* watched, QEvent* event)
|
||
{
|
||
QLineEdit* edit = qobject_cast<QLineEdit*>(watched);
|
||
if (edit)
|
||
{
|
||
switch (event->type())
|
||
{
|
||
case QEvent::FocusIn:
|
||
case QEvent::MouseButtonPress:
|
||
case QEvent::MouseButtonRelease:
|
||
case QEvent::TouchEnd:
|
||
ShowSystemKeyboard(edit);
|
||
break;
|
||
default:
|
||
break;
|
||
}
|
||
}
|
||
|
||
return QDialog::eventFilter(watched, event);
|
||
}
|
||
|
||
void DialogCameraSetting::setupInputKeyboard()
|
||
{
|
||
const Qt::InputMethodHints ipHints =
|
||
Qt::ImhPreferNumbers | Qt::ImhNoPredictiveText;
|
||
const Qt::InputMethodHints numberHints =
|
||
Qt::ImhPreferNumbers | Qt::ImhFormattedNumbersOnly;
|
||
|
||
SetupKeyboardEdit(ui->edit_ip, this, ipHints);
|
||
SetupKeyboardEdit(ui->edit_exposure, this, numberHints);
|
||
SetupKeyboardEdit(ui->edit_gain, this, numberHints);
|
||
}
|
||
|
||
void DialogCameraSetting::on_btn_ok_clicked()
|
||
{
|
||
collectFromUi();
|
||
accept();
|
||
}
|
||
|
||
void DialogCameraSetting::on_btn_cancel_clicked()
|
||
{
|
||
reject();
|
||
}
|
||
|
||
void DialogCameraSetting::on_btn_apply_clicked()
|
||
{
|
||
collectFromUi();
|
||
emit applyRequested(m_serverIp, m_camera);
|
||
}
|
||
|
||
void DialogCameraSetting::on_btn_reset_clicked()
|
||
{
|
||
// 仅把曝光/增益重置为默认值,Server IP 是用户特意配置的连接项,保持不动
|
||
DroneScrewCameraParams def;
|
||
if (ui->edit_exposure) ui->edit_exposure->setText(QString::number(def.exposure, 'f', 1));
|
||
if (ui->edit_gain) ui->edit_gain->setText(QString::number(def.gain, 'f', 2));
|
||
}
|