204 lines
6.2 KiB
C++
204 lines
6.2 KiB
C++
#include "dialogalgoarg.h"
|
|
#include "ui_dialogalgoarg.h"
|
|
|
|
#include <QCheckBox>
|
|
#include <QDoubleValidator>
|
|
#include <QEvent>
|
|
#include <QFormLayout>
|
|
#include <QGuiApplication>
|
|
#include <QInputMethod>
|
|
#include <QIntValidator>
|
|
#include <QLabel>
|
|
#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();
|
|
});
|
|
}
|
|
}
|
|
|
|
DialogAlgoArg::DialogAlgoArg(QWidget* parent)
|
|
: QDialog(parent)
|
|
, ui(new Ui::DialogAlgoArg)
|
|
{
|
|
ui->setupUi(this);
|
|
setWindowTitle(QStringLiteral("检测参数配置"));
|
|
|
|
// 设置输入验证器(只限制输入格式,不做上下选择)
|
|
ui->edit_score->setValidator(new QDoubleValidator(0.0, 1.0, 2, this));
|
|
ui->edit_nms->setValidator(new QDoubleValidator(0.0, 1.0, 2, this));
|
|
ui->edit_width->setValidator(new QIntValidator(0, 8192, this));
|
|
ui->edit_height->setValidator(new QIntValidator(0, 8192, this));
|
|
ui->edit_modelType->setValidator(new QIntValidator(0, 10, this));
|
|
ui->edit_expectedBoltCount->setValidator(new QIntValidator(1, 64, this));
|
|
setupInputKeyboard();
|
|
createDisplayRows();
|
|
}
|
|
|
|
DialogAlgoArg::~DialogAlgoArg()
|
|
{
|
|
delete ui;
|
|
}
|
|
|
|
void DialogAlgoArg::SetParams(const DroneScrewAlgoUiParams& params)
|
|
{
|
|
m_params = params;
|
|
loadToUi(params);
|
|
}
|
|
|
|
DroneScrewAlgoUiParams DialogAlgoArg::GetParams() const
|
|
{
|
|
return m_params;
|
|
}
|
|
|
|
void DialogAlgoArg::SetDisplayOptions(const DroneScrewDisplayOption& options)
|
|
{
|
|
m_displayOptions = options;
|
|
loadDisplayToUi(options);
|
|
}
|
|
|
|
DroneScrewDisplayOption DialogAlgoArg::GetDisplayOptions() const
|
|
{
|
|
return m_displayOptions;
|
|
}
|
|
|
|
void DialogAlgoArg::loadToUi(const DroneScrewAlgoUiParams& p)
|
|
{
|
|
if (ui->edit_score) ui->edit_score->setText(QString::number(p.scoreThreshold, 'f', 2));
|
|
if (ui->edit_nms) ui->edit_nms->setText(QString::number(p.nmsThreshold, 'f', 2));
|
|
if (ui->edit_width) ui->edit_width->setText(QString::number(p.inputWidth));
|
|
if (ui->edit_height) ui->edit_height->setText(QString::number(p.inputHeight));
|
|
if (ui->edit_modelType) ui->edit_modelType->setText(QString::number(p.modelType));
|
|
if (ui->edit_expectedBoltCount) ui->edit_expectedBoltCount->setText(QString::number(p.expectedBoltCount));
|
|
}
|
|
|
|
DroneScrewAlgoUiParams DialogAlgoArg::collectFromUi() const
|
|
{
|
|
DroneScrewAlgoUiParams p;
|
|
if (ui->edit_score) { bool ok; float v = ui->edit_score->text().toFloat(&ok); if (ok) p.scoreThreshold = v; }
|
|
if (ui->edit_nms) { bool ok; float v = ui->edit_nms->text().toFloat(&ok); if (ok) p.nmsThreshold = v; }
|
|
if (ui->edit_width) { bool ok; int v = ui->edit_width->text().toInt(&ok); if (ok) p.inputWidth = v; }
|
|
if (ui->edit_height) { bool ok; int v = ui->edit_height->text().toInt(&ok); if (ok) p.inputHeight = v; }
|
|
if (ui->edit_modelType) { bool ok; int v = ui->edit_modelType->text().toInt(&ok); if (ok) p.modelType = v; }
|
|
if (ui->edit_expectedBoltCount) { bool ok; int v = ui->edit_expectedBoltCount->text().toInt(&ok); if (ok) p.expectedBoltCount = v; }
|
|
return p;
|
|
}
|
|
|
|
bool DialogAlgoArg::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 DialogAlgoArg::setupInputKeyboard()
|
|
{
|
|
const Qt::InputMethodHints numberHints =
|
|
Qt::ImhPreferNumbers | Qt::ImhFormattedNumbersOnly;
|
|
const Qt::InputMethodHints integerHints =
|
|
Qt::ImhPreferNumbers | Qt::ImhDigitsOnly;
|
|
|
|
SetupKeyboardEdit(ui->edit_score, this, numberHints);
|
|
SetupKeyboardEdit(ui->edit_nms, this, numberHints);
|
|
SetupKeyboardEdit(ui->edit_width, this, integerHints);
|
|
SetupKeyboardEdit(ui->edit_height, this, integerHints);
|
|
SetupKeyboardEdit(ui->edit_modelType, this, integerHints);
|
|
SetupKeyboardEdit(ui->edit_expectedBoltCount, this, integerHints);
|
|
}
|
|
|
|
void DialogAlgoArg::createDisplayRows()
|
|
{
|
|
if (!ui->fl_main)
|
|
return;
|
|
|
|
m_checkDrawBoxes = new QCheckBox(QStringLiteral("显示"), this);
|
|
m_checkDrawBoxes->setObjectName(QStringLiteral("check_drawBoxes"));
|
|
m_checkDrawBoxes->setMinimumHeight(40);
|
|
m_checkDrawBoxes->setStyleSheet(QStringLiteral(
|
|
"QCheckBox { color: rgb(239, 241, 245); font-size: 18px; background: none; }"));
|
|
|
|
ui->fl_main->addRow(new QLabel(QStringLiteral("显示结果框"), this),
|
|
m_checkDrawBoxes);
|
|
}
|
|
|
|
void DialogAlgoArg::loadDisplayToUi(const DroneScrewDisplayOption& options)
|
|
{
|
|
if (m_checkDrawBoxes)
|
|
m_checkDrawBoxes->setChecked(options.drawBoxes);
|
|
}
|
|
|
|
DroneScrewDisplayOption DialogAlgoArg::collectDisplayFromUi() const
|
|
{
|
|
DroneScrewDisplayOption options = m_displayOptions;
|
|
if (m_checkDrawBoxes)
|
|
options.drawBoxes = m_checkDrawBoxes->isChecked();
|
|
return options;
|
|
}
|
|
|
|
void DialogAlgoArg::on_btn_ok_clicked()
|
|
{
|
|
m_params = collectFromUi();
|
|
m_displayOptions = collectDisplayFromUi();
|
|
accept();
|
|
}
|
|
|
|
void DialogAlgoArg::on_btn_cancel_clicked()
|
|
{
|
|
reject();
|
|
}
|
|
|
|
void DialogAlgoArg::on_btn_apply_clicked()
|
|
{
|
|
m_params = collectFromUi();
|
|
m_displayOptions = collectDisplayFromUi();
|
|
}
|
|
|
|
void DialogAlgoArg::on_btn_reset_clicked()
|
|
{
|
|
DroneScrewAlgoUiParams p; // 默认值
|
|
loadToUi(p);
|
|
DroneScrewDisplayOption options = m_displayOptions;
|
|
options.drawBoxes = true;
|
|
loadDisplayToUi(options);
|
|
}
|