175 lines
5.7 KiB
C++
175 lines
5.7 KiB
C++
#include "DigitalInput.h"
|
|
#include "ui_digitalinput.h"
|
|
#include "MyInputMethod.h"
|
|
#include <QObject>
|
|
#include <QSpinBox>
|
|
#include <QSpinBox>
|
|
#include <QDebug>
|
|
#include <QKeyEvent>
|
|
|
|
DigitalInput::DigitalInput(MyInputMethod *myInputMethod,QWidget *parent) :
|
|
QWidget(parent,(Qt::FramelessWindowHint )),
|
|
ui(new Ui::DigitalInput)
|
|
{
|
|
ui->setupUi(this);
|
|
this->myInputMethod = myInputMethod;
|
|
|
|
connect(ui->Key_0,SIGNAL(pressed()),this,SLOT(onCharPressed()));
|
|
connect(ui->Key_1,SIGNAL(pressed()),this,SLOT(onCharPressed()));
|
|
connect(ui->Key_2,SIGNAL(pressed()),this,SLOT(onCharPressed()));
|
|
connect(ui->Key_3,SIGNAL(pressed()),this,SLOT(onCharPressed()));
|
|
connect(ui->Key_4,SIGNAL(pressed()),this,SLOT(onCharPressed()));
|
|
connect(ui->Key_5,SIGNAL(pressed()),this,SLOT(onCharPressed()));
|
|
connect(ui->Key_6,SIGNAL(pressed()),this,SLOT(onCharPressed()));
|
|
connect(ui->Key_7,SIGNAL(pressed()),this,SLOT(onCharPressed()));
|
|
connect(ui->Key_8,SIGNAL(pressed()),this,SLOT(onCharPressed()));
|
|
connect(ui->Key_9,SIGNAL(pressed()),this,SLOT(onCharPressed()));
|
|
connect(ui->Key_pluse,SIGNAL(pressed()),this,SLOT(onCharPressed()));
|
|
connect(ui->Key_backspace,SIGNAL(pressed()),this,SLOT(onKeyBackspacePressed()));
|
|
// connect(ui->Key_selectAll,SIGNAL(pressed()),this,SLOT(onKeySelectAllPressed()));
|
|
// connect(ui->Key_up,SIGNAL(pressed()),this,SLOT(onKeyUpPressed()));
|
|
// connect(ui->Key_down,SIGNAL(pressed()),this,SLOT(onKeyDownPressed()));
|
|
|
|
connect(ui->enterBtn, &QPushButton::clicked, this, &DigitalInput::onEnterPressed);
|
|
connect(ui->clrBtn, &QPushButton::clicked, this, &DigitalInput::onClrPressed);
|
|
|
|
// 所有键盘按钮不接收焦点,避免点击按钮时 traceEdit 丢失焦点
|
|
QList<QPushButton*> btns = findChildren<QPushButton*>();
|
|
for (QPushButton* btn : btns) {
|
|
btn->setFocusPolicy(Qt::NoFocus);
|
|
}
|
|
|
|
ui->traceEdit->setFocusPolicy(Qt::StrongFocus);
|
|
}
|
|
|
|
DigitalInput::~DigitalInput()
|
|
{
|
|
delete ui;
|
|
}
|
|
|
|
|
|
void DigitalInput::onCharPressed(){
|
|
QPushButton * button = qobject_cast<QPushButton *>(sender());
|
|
QString ch = button->text();
|
|
|
|
if (!m_currSpinBox) {
|
|
ui->traceEdit->insert(ch);
|
|
return;
|
|
}
|
|
|
|
int cursorPos = ui->traceEdit->cursorPosition();
|
|
QString currentText = ui->traceEdit->text();
|
|
QString newText = currentText.left(cursorPos) + ch + currentText.mid(cursorPos);
|
|
|
|
if (newText.isEmpty()) {
|
|
ui->traceEdit->insert(ch);
|
|
return;
|
|
}
|
|
|
|
bool ok = false;
|
|
int value = newText.toInt(&ok);
|
|
if (!ok) {
|
|
ui->traceEdit->insert(ch);
|
|
return;
|
|
}
|
|
|
|
int max = m_currSpinBox->maximum();
|
|
if (value <= max) {
|
|
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();
|
|
text.remove(cursorPos - 1, 1);
|
|
ui->traceEdit->setText(text);
|
|
ui->traceEdit->setCursorPosition(cursorPos - 1);
|
|
}
|
|
}
|
|
|
|
void DigitalInput::onKeySelectAllPressed(){
|
|
#ifndef Q_OS_WIN32
|
|
//QWSServer::sendKeyEvent(0,Qt::Key_A,Qt::ControlModifier,true,false); //锟斤拷系统锟斤拷锟酵硷拷锟斤拷锟铰硷拷
|
|
//if(!init)
|
|
// QWSServer::sendKeyEvent(0,Qt::Key_A,Qt::ControlModifier,true,false); //锟斤拷系统锟斤拷锟酵硷拷锟斤拷锟铰硷拷
|
|
//init = 1;
|
|
|
|
MyInputMethod::sendEvent(Qt::Key_A, Qt::ControlModifier);
|
|
#endif
|
|
|
|
|
|
}
|
|
void DigitalInput::onKeyUpPressed(){
|
|
#ifndef Q_OS_WIN32
|
|
//QWSServer::sendKeyEvent(0,Qt::Key_Up,Qt::NoModifier,true,false); //锟斤拷系统锟斤拷锟酵硷拷锟斤拷锟铰硷拷
|
|
MyInputMethod::sendEvent(Qt::Key_Up,Qt::NoModifier);
|
|
#endif
|
|
}
|
|
void DigitalInput::onKeyDownPressed(){
|
|
#ifndef Q_OS_WIN32
|
|
//QWSServer::sendKeyEvent(0,Qt::Key_Down,Qt::NoModifier,true,false); //锟斤拷系统锟斤拷锟酵硷拷锟斤拷锟铰硷拷
|
|
MyInputMethod::sendEvent(Qt::Key_Down,Qt::NoModifier);
|
|
#endif
|
|
}
|
|
|
|
void DigitalInput::onEnterPressed()
|
|
{
|
|
hide();
|
|
}
|
|
|
|
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();
|
|
}
|
|
|
|
void DigitalInput::showEvent(QShowEvent *event)
|
|
{
|
|
QWidget::showEvent(event);
|
|
auto focusWidget = qApp->focusWidget();
|
|
QSpinBox* box = dynamic_cast<QSpinBox*>(focusWidget);
|
|
if(!box) {
|
|
qWarning() << "focusWidget is not QSpinBox";
|
|
m_currSpinBox = nullptr;
|
|
return;
|
|
}
|
|
|
|
m_currSpinBox = box;
|
|
ui->traceEdit->setText(box->text());
|
|
// focusWidget->clearFocus();
|
|
ui->traceEdit->setFocus();
|
|
|
|
// connect(box, QOverload<const QString&>::of(&QSpinBox::valueChanged), ui->traceEdit, &QLineEdit::setText);
|
|
qDebug() << "The traceEdit has connected to the QSpinBox: " << box->objectName();
|
|
}
|
|
|
|
void DigitalInput::hideEvent(QHideEvent * event)
|
|
{
|
|
QWidget::hideEvent(event);
|
|
|
|
if(m_currSpinBox) {
|
|
int value = ui->traceEdit->text().toInt();
|
|
int min = m_currSpinBox->minimum();
|
|
int max = m_currSpinBox->maximum();
|
|
value = qBound(min, value, max);
|
|
m_currSpinBox->setValue(value);
|
|
m_currSpinBox->clearFocus();
|
|
}
|
|
} |