Files
gcs-nf/App/ComponentUI/Inputter/Inputter.cpp
T

451 lines
9.3 KiB
C++
Raw Normal View History

#include "Inputter.h"
2020-05-06 19:17:40 +08:00
#include "ui_Inputter.h"
Inputter::Inputter(QWidget *parent) :
QWidget(parent),
ui(new Ui::Inputter)
{
ui->setupUi(this);
//load qss
2020-05-09 14:46:07 +08:00
QFile file(":/qss/Inputter.qss");
file.open(QFile::ReadOnly);
QTextStream filetext(&file);
QString stylesheet = filetext.readAll();
this->setStyleSheet(stylesheet);
file.close();
2020-05-06 19:17:40 +08:00
2020-11-19 15:37:11 +08:00
this->setFocusPolicy(Qt::WheelFocus);
this->setFocus();
ui->lineEdit->setFocusPolicy(Qt::WheelFocus);
ui->lineEdit->setFocus();
2020-05-06 19:17:40 +08:00
curentStr = "";
currentValue = 0;
2020-08-10 22:42:59 +08:00
inputType = type::Normal;
2020-05-07 19:14:44 +08:00
DecimalPlaces = -1;
2020-05-06 19:17:40 +08:00
2020-12-09 17:29:28 +08:00
ui->label_decription->hide();
2020-05-06 19:17:40 +08:00
ui->lineEdit->setText(curentStr);
2020-05-08 11:29:50 +08:00
ui->lineEdit->setReadOnly(true);
2020-08-10 22:42:59 +08:00
DoubleClickTimer = new QTimer(this);
2020-08-11 16:48:15 +08:00
DoubleClickTimer->setInterval(200);//0.2s之内再点击,那就相当于双击
2020-08-10 22:42:59 +08:00
connect(DoubleClickTimer,SIGNAL(timeout()),
this,SLOT(DoubleClickTimeout()));
2020-05-06 19:17:40 +08:00
}
Inputter::~Inputter()
{
2020-08-11 16:48:15 +08:00
if(DoubleClickTimer)
{
if(DoubleClickTimer->isActive())
{
DoubleClickTimer->stop();
}
delete DoubleClickTimer;
}
2020-05-06 19:17:40 +08:00
delete ui;
}
bool Inputter::event(QEvent *event)
{
2021-01-10 18:49:21 +08:00
//qDebug() << event;
2020-11-21 18:19:28 +08:00
if(event->type() == QEvent::Leave)
{
//this->close();
}
return QWidget::event(event);
}
2020-11-19 15:37:11 +08:00
2020-11-21 18:19:28 +08:00
void Inputter::keyReleaseEvent(QKeyEvent *event) //键盘按下事件
2020-11-19 15:37:11 +08:00
{
qInfo() << "inputter key" << event;
//qDebug() << event;
switch(event->key())
{
case Qt::Key_0:
on_pushButton_0_clicked();
break;
case Qt::Key_1:
on_pushButton_1_clicked();
break;
case Qt::Key_2:
on_pushButton_2_clicked();
break;
case Qt::Key_3:
on_pushButton_3_clicked();
break;
case Qt::Key_4:
on_pushButton_4_clicked();
break;
case Qt::Key_5:
on_pushButton_5_clicked();
break;
case Qt::Key_6:
on_pushButton_6_clicked();
break;
case Qt::Key_7:
on_pushButton_7_clicked();
break;
case Qt::Key_8:
on_pushButton_8_clicked();
break;
case Qt::Key_9:
on_pushButton_9_clicked();
break;
case Qt::Key_Period:
on_pushButton_point_clicked();
break;
case Qt::Key_Minus:
on_pushButton_minus_clicked();
break;
case Qt::Key_Delete:
{
curentStr = curentStr.mid(0,curentStr.size() - 1);
ui->lineEdit->setText(curentStr);
}
2020-11-19 15:37:11 +08:00
break;
case Qt::Key_Backspace:
{
curentStr = curentStr.mid(0,curentStr.size() - 1);
ui->lineEdit->setText(curentStr);
}
2020-11-19 15:37:11 +08:00
break;
case Qt::Key_Escape:
on_pushButton_cancel_clicked();
break;
case Qt::Key_Return:
on_pushButton_ok_clicked();
break;
}
}
void Inputter::setInputType(int Type)
{
2020-08-10 22:42:59 +08:00
switch (Type) {
default:
case 0:
inputType = type::Normal;
break;
case 1:
inputType = type::MAC;
ui->pushButton_nan->setEnabled(false);
ui->pushButton_minus->setEnabled(false);
break;
}
}
2020-05-07 19:14:44 +08:00
void Inputter::setInitValue(QVariant value)
{
curentStr = value.toString();
ui->lineEdit->setText(curentStr);
}
void Inputter::setDecimalPlaces(int value)
{
DecimalPlaces = value;
}
2020-10-12 14:39:16 +08:00
void Inputter::setLabel(QVariant s)
{
2020-12-09 17:29:28 +08:00
if(ui->label_decription->isHidden())
2020-10-12 14:39:16 +08:00
{
2020-12-09 17:29:28 +08:00
ui->label_decription->show();
2020-10-12 14:39:16 +08:00
}
2020-12-09 17:29:28 +08:00
ui->label_decription->setText(s.toString());
2020-10-12 14:39:16 +08:00
}
2020-05-06 19:17:40 +08:00
void Inputter::on_NumberClicked(QString str)
{
2020-08-10 22:42:59 +08:00
if((!curentStr.contains('n'))&&(!curentStr.contains('a')))
{
if(inputType == type::Normal)
{
curentStr.append(str);
ui->lineEdit->setText(curentStr);
}
else//检查当前释放点和点连在一起,是否点之间数字大于255
{
QString mac = curentStr;
2020-05-06 19:17:40 +08:00
2020-08-10 22:42:59 +08:00
int index;
if(mac.contains('.'))
{
index = mac.lastIndexOf('.')+1;
}
else
{
index = 0;
}
mac = mac.mid(index);
mac.append(str);
if(mac.toInt() <= 255)
{
//如果上一个是0,这个也是0,那么就不用连接
if(mac != 0)
{
curentStr.append(str);
ui->lineEdit->setText(curentStr);
}
}
else
{
if(curentStr.count('.') < 3)
{
curentStr.append('.');
curentStr.append(str);
ui->lineEdit->setText(curentStr);
}
}
}
}
2020-05-06 19:17:40 +08:00
}
2020-08-10 22:42:59 +08:00
void Inputter::DoubleClickTimeout()
{
DoubleClickTimer->stop();
}
2020-05-06 19:17:40 +08:00
void Inputter::on_pushButton_1_clicked()
{
on_NumberClicked(ui->pushButton_1->text());
}
void Inputter::on_pushButton_2_clicked()
{
on_NumberClicked(ui->pushButton_2->text());
}
void Inputter::on_pushButton_3_clicked()
{
on_NumberClicked(ui->pushButton_3->text());
}
void Inputter::on_pushButton_4_clicked()
{
on_NumberClicked(ui->pushButton_4->text());
}
void Inputter::on_pushButton_5_clicked()
{
on_NumberClicked(ui->pushButton_5->text());
}
void Inputter::on_pushButton_6_clicked()
{
on_NumberClicked(ui->pushButton_6->text());
}
void Inputter::on_pushButton_7_clicked()
{
on_NumberClicked(ui->pushButton_7->text());
}
void Inputter::on_pushButton_8_clicked()
{
on_NumberClicked(ui->pushButton_8->text());
}
void Inputter::on_pushButton_9_clicked()
{
on_NumberClicked(ui->pushButton_9->text());
}
void Inputter::on_pushButton_point_clicked()
{
2020-08-10 22:42:59 +08:00
if(inputType == type::Normal)
2020-05-06 19:17:40 +08:00
{
if(curentStr.size()>0)
2020-05-06 19:17:40 +08:00
{
if(curentStr.contains('.'))
{
qDebug() << "it is illegal string";
}
else
{
2020-05-19 19:02:32 +08:00
if(DecimalPlaces != 0)
{
on_NumberClicked(ui->pushButton_point->text());
}
}
2020-05-06 19:17:40 +08:00
}
else
{
qDebug() << "point can't be set in first char";
2020-05-06 19:17:40 +08:00
}
}
else
{
2020-08-10 22:42:59 +08:00
if(curentStr.size()>0)
{
if((curentStr.data()[curentStr.size() -1]!= '.')&&(curentStr.count('.')<3))//数量小于3
{
on_NumberClicked(ui->pushButton_point->text());
}
}
else
{
qDebug() << "point can't be set in first char";
}
2020-05-06 19:17:40 +08:00
}
}
void Inputter::on_pushButton_0_clicked()
{
on_NumberClicked(ui->pushButton_0->text());
}
2020-06-06 16:08:11 +08:00
void Inputter::on_pushButton_minus_clicked()
{
2020-08-10 22:42:59 +08:00
if(inputType == type::Normal)
2020-06-06 16:08:11 +08:00
{
2020-08-10 22:42:59 +08:00
if((!curentStr.contains('n'))&&(!curentStr.contains('a')))
2020-07-24 16:46:08 +08:00
{
2020-08-10 22:42:59 +08:00
if(curentStr.at(0) == '-')
{
curentStr = curentStr.mid(1);
}
else
{
curentStr.insert(0,'-');
}
2020-07-24 16:46:08 +08:00
}
2020-06-06 16:08:11 +08:00
2020-07-24 16:46:08 +08:00
ui->lineEdit->setText(curentStr);
}
2020-06-06 16:08:11 +08:00
}
2020-05-06 19:17:40 +08:00
void Inputter::on_pushButton_del_clicked()
{
curentStr = curentStr.mid(0,curentStr.size() - 1);
ui->lineEdit->setText(curentStr);
2020-08-10 22:42:59 +08:00
if(DoubleClickTimer)
{
if(DoubleClickTimer->isActive())
{
curentStr.clear();
ui->lineEdit->clear();
DoubleClickTimer->stop();
}
else
{
DoubleClickTimer->start();
}
}
2020-05-06 19:17:40 +08:00
}
void Inputter::on_pushButton_cancel_clicked()
{
this->close();
}
void Inputter::on_pushButton_ok_clicked()
{
2020-08-10 22:42:59 +08:00
if(inputType == type::Normal)
2020-05-07 19:14:44 +08:00
{
while(curentStr.at(0) == '0')
2020-05-07 19:14:44 +08:00
{
if(curentStr.contains('.'))
2020-05-07 19:14:44 +08:00
{
if(curentStr.at(1) == '.')
{
break;
}
else
{
curentStr = curentStr.mid(1);
}
2020-05-07 19:14:44 +08:00
}
else
{
if(curentStr.size() > 1)
{
curentStr = curentStr.mid(1);
}
else
{
break;
}
2020-05-07 19:14:44 +08:00
}
}
if(curentStr.at(curentStr.size()-1) == '.')
2020-05-07 19:14:44 +08:00
{
curentStr.append('0');
2020-05-07 19:14:44 +08:00
}
//判断位数
2020-06-08 17:54:17 +08:00
if(DecimalPlaces == -1)
{
curentStr = QString::number(curentStr.toDouble());
}
else
{
curentStr = QString::number(curentStr.toDouble(),'f',DecimalPlaces);
}
}
else
2020-05-07 19:14:44 +08:00
{
//mac模式
2020-08-10 22:42:59 +08:00
//检查是否填充完成,没有那么就填零,把格式填对
//curentStr
while(curentStr.count('.') < 3)//检查点,如果不够就进行编辑
{
if(curentStr.data()[curentStr.size()-1] == '.')
{
curentStr.append(QString::number(0));
}
else
{
curentStr.append('.');
}
if(curentStr.count('.') >= 3)
{
break;
}
}
2020-06-13 21:23:53 +08:00
2020-05-07 19:14:44 +08:00
}
2020-05-06 19:17:40 +08:00
currentValue = curentStr;
qDebug() << currentValue;
emit confirmValue(currentValue);
this->deleteLater();
this->close();
}
2020-05-07 19:14:44 +08:00
void Inputter::on_pushButton_nan_clicked()
{
curentStr = "nan";
ui->lineEdit->setText(curentStr);
}
2020-06-06 16:08:11 +08:00