375 lines
7.6 KiB
C++
375 lines
7.6 KiB
C++
#include "Inputter.h"
|
|
#include "ui_Inputter.h"
|
|
|
|
Inputter::Inputter(QWidget *parent) :
|
|
QWidget(parent),
|
|
ui(new Ui::Inputter)
|
|
{
|
|
ui->setupUi(this);
|
|
|
|
//load qss
|
|
QFile file(":/qss/Inputter.qss");
|
|
file.open(QFile::ReadOnly);
|
|
QTextStream filetext(&file);
|
|
QString stylesheet = filetext.readAll();
|
|
this->setStyleSheet(stylesheet);
|
|
file.close();
|
|
|
|
curentStr = "";
|
|
currentValue = 0;
|
|
inputType = type::Normal;
|
|
DecimalPlaces = -1;
|
|
|
|
ui->label->hide();
|
|
ui->lineEdit->setText(curentStr);
|
|
ui->lineEdit->setReadOnly(true);
|
|
|
|
|
|
DoubleClickTimer = new QTimer(this);
|
|
DoubleClickTimer->setInterval(200);//0.2s之内再点击,那就相当于双击
|
|
connect(DoubleClickTimer,SIGNAL(timeout()),
|
|
this,SLOT(DoubleClickTimeout()));
|
|
|
|
}
|
|
|
|
Inputter::~Inputter()
|
|
{
|
|
if(DoubleClickTimer)
|
|
{
|
|
if(DoubleClickTimer->isActive())
|
|
{
|
|
DoubleClickTimer->stop();
|
|
}
|
|
|
|
delete DoubleClickTimer;
|
|
}
|
|
|
|
|
|
delete ui;
|
|
}
|
|
|
|
bool Inputter::event(QEvent *event)
|
|
{
|
|
if(event->type() == QEvent::Leave)
|
|
{
|
|
this->close();
|
|
}
|
|
return QWidget::event(event);
|
|
}
|
|
|
|
void Inputter::setInputType(int Type)
|
|
{
|
|
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;
|
|
}
|
|
}
|
|
|
|
void Inputter::setInitValue(QVariant value)
|
|
{
|
|
curentStr = value.toString();
|
|
ui->lineEdit->setText(curentStr);
|
|
}
|
|
|
|
void Inputter::setDecimalPlaces(int value)
|
|
{
|
|
DecimalPlaces = value;
|
|
}
|
|
|
|
void Inputter::setLabel(QVariant s)
|
|
{
|
|
if(ui->label->isHidden())
|
|
{
|
|
ui->label->show();
|
|
}
|
|
|
|
ui->label->setText(s.toString());
|
|
}
|
|
|
|
|
|
void Inputter::on_NumberClicked(QString str)
|
|
{
|
|
if((!curentStr.contains('n'))&&(!curentStr.contains('a')))
|
|
{
|
|
if(inputType == type::Normal)
|
|
{
|
|
curentStr.append(str);
|
|
ui->lineEdit->setText(curentStr);
|
|
}
|
|
else//检查当前释放点和点连在一起,是否点之间数字大于255
|
|
{
|
|
QString mac = curentStr;
|
|
|
|
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);
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
void Inputter::DoubleClickTimeout()
|
|
{
|
|
DoubleClickTimer->stop();
|
|
}
|
|
|
|
|
|
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()
|
|
{
|
|
if(inputType == type::Normal)
|
|
{
|
|
if(curentStr.size()>0)
|
|
{
|
|
if(curentStr.contains('.'))
|
|
{
|
|
qDebug() << "it is illegal string";
|
|
}
|
|
else
|
|
{
|
|
if(DecimalPlaces != 0)
|
|
{
|
|
on_NumberClicked(ui->pushButton_point->text());
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
qDebug() << "point can't be set in first char";
|
|
}
|
|
}
|
|
else
|
|
{
|
|
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";
|
|
}
|
|
}
|
|
}
|
|
|
|
void Inputter::on_pushButton_0_clicked()
|
|
{
|
|
on_NumberClicked(ui->pushButton_0->text());
|
|
}
|
|
|
|
void Inputter::on_pushButton_minus_clicked()
|
|
{
|
|
if(inputType == type::Normal)
|
|
{
|
|
if((!curentStr.contains('n'))&&(!curentStr.contains('a')))
|
|
{
|
|
if(curentStr.at(0) == '-')
|
|
{
|
|
curentStr = curentStr.mid(1);
|
|
}
|
|
else
|
|
{
|
|
curentStr.insert(0,'-');
|
|
}
|
|
}
|
|
|
|
ui->lineEdit->setText(curentStr);
|
|
}
|
|
}
|
|
|
|
|
|
void Inputter::on_pushButton_del_clicked()
|
|
{
|
|
curentStr = curentStr.mid(0,curentStr.size() - 1);
|
|
ui->lineEdit->setText(curentStr);
|
|
|
|
if(DoubleClickTimer)
|
|
{
|
|
if(DoubleClickTimer->isActive())
|
|
{
|
|
curentStr.clear();
|
|
ui->lineEdit->clear();
|
|
DoubleClickTimer->stop();
|
|
}
|
|
else
|
|
{
|
|
DoubleClickTimer->start();
|
|
}
|
|
}
|
|
}
|
|
|
|
void Inputter::on_pushButton_cancel_clicked()
|
|
{
|
|
this->close();
|
|
}
|
|
|
|
void Inputter::on_pushButton_ok_clicked()
|
|
{
|
|
if(inputType == type::Normal)
|
|
{
|
|
while(curentStr.at(0) == '0')
|
|
{
|
|
if(curentStr.contains('.'))
|
|
{
|
|
if(curentStr.at(1) == '.')
|
|
{
|
|
break;
|
|
}
|
|
else
|
|
{
|
|
curentStr = curentStr.mid(1);
|
|
}
|
|
|
|
}
|
|
else
|
|
{
|
|
if(curentStr.size() > 1)
|
|
{
|
|
curentStr = curentStr.mid(1);
|
|
}
|
|
else
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
if(curentStr.at(curentStr.size()-1) == '.')
|
|
{
|
|
curentStr.append('0');
|
|
}
|
|
|
|
//判断位数
|
|
if(DecimalPlaces == -1)
|
|
{
|
|
curentStr = QString::number(curentStr.toDouble());
|
|
}
|
|
else
|
|
{
|
|
curentStr = QString::number(curentStr.toDouble(),'f',DecimalPlaces);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
//mac模式
|
|
//检查是否填充完成,没有那么就填零,把格式填对
|
|
//curentStr
|
|
|
|
while(curentStr.count('.') < 3)//检查点,如果不够就进行编辑
|
|
{
|
|
if(curentStr.data()[curentStr.size()-1] == '.')
|
|
{
|
|
curentStr.append(QString::number(0));
|
|
}
|
|
else
|
|
{
|
|
curentStr.append('.');
|
|
}
|
|
|
|
if(curentStr.count('.') >= 3)
|
|
{
|
|
break;
|
|
}
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
currentValue = curentStr;
|
|
qDebug() << currentValue;
|
|
emit confirmValue(currentValue);
|
|
this->deleteLater();
|
|
this->close();
|
|
}
|
|
|
|
void Inputter::on_pushButton_nan_clicked()
|
|
{
|
|
curentStr = "nan";
|
|
ui->lineEdit->setText(curentStr);
|
|
}
|
|
|
|
|