修改外部同步器的编辑逻辑、添加‘另存为’ 按钮
This commit is contained in:
+69
-22
@@ -3,7 +3,9 @@
|
||||
#include "MyInputMethod.h"
|
||||
#include <QObject>
|
||||
#include <QSpinBox>
|
||||
#include <QDoubleSpinBox>
|
||||
#include <QSpinBox>
|
||||
#include <QRegularExpression>
|
||||
#include <QDebug>
|
||||
#include <QKeyEvent>
|
||||
|
||||
@@ -52,8 +54,7 @@ DigitalInput::~DigitalInput()
|
||||
void DigitalInput::onCharPressed(){
|
||||
QPushButton * button = qobject_cast<QPushButton *>(sender());
|
||||
QString ch = button->text();
|
||||
|
||||
if (!m_currSpinBox) {
|
||||
if (!m_currSpinBox && !m_currDoubleSpinBox) {
|
||||
ui->traceEdit->insert(ch);
|
||||
return;
|
||||
}
|
||||
@@ -62,34 +63,60 @@ void DigitalInput::onCharPressed(){
|
||||
QString currentText = ui->traceEdit->text();
|
||||
QString newText = currentText.left(cursorPos) + ch + currentText.mid(cursorPos);
|
||||
|
||||
if (newText.isEmpty()) {
|
||||
if (newText.isEmpty()) {
|
||||
ui->traceEdit->insert(ch);
|
||||
return;
|
||||
}
|
||||
|
||||
if(m_currDoubleSpinBox) {
|
||||
// 用正则表达式判定 newText是否是一个浮点数或部分浮点输入。
|
||||
// 允许的示例: 0.2251, 15.335, 15, -924.02, 5. (末尾点为临时状态), .5
|
||||
// 也允许单独的正负号作为正在输入中的状态: "-" 或 "+"
|
||||
QString simulated = newText;
|
||||
|
||||
// Allow single sign while typing
|
||||
if (simulated == "-" || simulated == "+") {
|
||||
ui->traceEdit->insert(ch);
|
||||
return;
|
||||
}
|
||||
|
||||
// Regular expression: optional sign, then either digits with optional dot+digits, or dot+digits, or digits+dot (partial)
|
||||
// 不限制小数位数;最终的最小/最大以及精度由 hideEvent 写回时决定
|
||||
QRegularExpression re("^[+-]?((\\d+(\\.\\d*)?)|(\\.\\d*))$");
|
||||
QRegularExpressionMatch match = re.match(simulated);
|
||||
if (!match.hasMatch()) {
|
||||
// not a valid float format (even partial) -> reject
|
||||
return;
|
||||
}
|
||||
|
||||
// acceptable -> insert
|
||||
ui->traceEdit->insert(ch);
|
||||
return;
|
||||
}
|
||||
|
||||
if(ch == "." && m_currSpinBox) {
|
||||
// SpinBox不能输入 .
|
||||
return;
|
||||
}
|
||||
|
||||
if(ch == "-" && m_currSpinBox->minimum() && currentText.isEmpty()) {
|
||||
ui->traceEdit->insert(ch);
|
||||
return;
|
||||
}
|
||||
|
||||
bool ok = false;
|
||||
int value = newText.toInt(&ok);
|
||||
if (!ok) {
|
||||
ui->traceEdit->insert(ch);
|
||||
if (!ok) {
|
||||
return;
|
||||
}
|
||||
|
||||
int max = m_currSpinBox->maximum();
|
||||
if (value <= max) {
|
||||
if (value <= max && value >= m_currSpinBox->minimum()) {
|
||||
ui->traceEdit->insert(ch);
|
||||
}
|
||||
}
|
||||
|
||||
void DigitalInput::onKeyBackspacePressed(){
|
||||
|
||||
// if(m_currSpinBox && QString::number(m_currSpinBox->value()).size() == 1) {
|
||||
// ui->traceEdit->clear();
|
||||
// }
|
||||
|
||||
// #ifndef Q_OS_WIN32
|
||||
// //QWSServer::sendKeyEvent(0,Qt::Key_Backspace,Qt::NoModifier,true,false); //锟斤拷系统锟斤拷锟酵硷拷锟斤拷锟铰硷拷
|
||||
// MyInputMethod::sendEvent(Qt::Key_Backspace,Qt::NoModifier);
|
||||
// #endif
|
||||
int cursorPos = ui->traceEdit->cursorPosition();
|
||||
if (cursorPos > 0) {
|
||||
QString text = ui->traceEdit->text();
|
||||
@@ -131,12 +158,6 @@ void DigitalInput::onEnterPressed()
|
||||
|
||||
void DigitalInput::onClrPressed()
|
||||
{
|
||||
// if(m_currSpinBox) {
|
||||
// m_currSpinBox->setValue(std::min(m_currSpinBox->maximum(), std::max(m_currSpinBox->minimum(), 0)));
|
||||
// ui->traceEdit->setText(m_currSpinBox->value());
|
||||
// // ui->traceEdit->setFocus();
|
||||
// }
|
||||
|
||||
ui->traceEdit->clear();
|
||||
ui->traceEdit->setFocus();
|
||||
}
|
||||
@@ -147,14 +168,28 @@ void DigitalInput::showEvent(QShowEvent *event)
|
||||
raise();
|
||||
|
||||
auto focusWidget = qApp->focusWidget();
|
||||
|
||||
QDoubleSpinBox* dbox = dynamic_cast<QDoubleSpinBox*>(focusWidget);
|
||||
if (dbox) {
|
||||
m_currDoubleSpinBox = dbox;
|
||||
m_currSpinBox = nullptr;
|
||||
// Show value without padding trailing zeros (avoid decimals() padding)
|
||||
ui->traceEdit->setText(QString::number(dbox->value(), 'g', 15));
|
||||
ui->traceEdit->setFocus();
|
||||
qDebug() << "The traceEdit has connected to the QDoubleSpinBox: " << dbox->objectName();
|
||||
return;
|
||||
}
|
||||
|
||||
QSpinBox* box = dynamic_cast<QSpinBox*>(focusWidget);
|
||||
if(!box) {
|
||||
qWarning() << "focusWidget is not QSpinBox";
|
||||
qWarning() << "focusWidget is not QSpinBox or QDoubleSpinBox";
|
||||
m_currSpinBox = nullptr;
|
||||
m_currDoubleSpinBox = nullptr;
|
||||
return;
|
||||
}
|
||||
|
||||
m_currSpinBox = box;
|
||||
m_currDoubleSpinBox = nullptr;
|
||||
ui->traceEdit->setText(box->text());
|
||||
// focusWidget->clearFocus();
|
||||
ui->traceEdit->setFocus();
|
||||
@@ -167,6 +202,18 @@ void DigitalInput::hideEvent(QHideEvent * event)
|
||||
{
|
||||
QWidget::hideEvent(event);
|
||||
|
||||
if (m_currDoubleSpinBox) {
|
||||
bool ok = false;
|
||||
double value = ui->traceEdit->text().toDouble(&ok);
|
||||
if (!ok) value = m_currDoubleSpinBox->value();
|
||||
double min = m_currDoubleSpinBox->minimum();
|
||||
double max = m_currDoubleSpinBox->maximum();
|
||||
value = qBound(min, value, max);
|
||||
m_currDoubleSpinBox->setValue(value);
|
||||
m_currDoubleSpinBox->clearFocus();
|
||||
return;
|
||||
}
|
||||
|
||||
if(m_currSpinBox) {
|
||||
int value = ui->traceEdit->text().toInt();
|
||||
int min = m_currSpinBox->minimum();
|
||||
|
||||
Reference in New Issue
Block a user