You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
AppFlow/FITK_Kernel/FITKEasyParam/FITKWidgetSciNotation.cpp

123 lines
3.4 KiB
C++

#include "FITKWidgetSciNotation.h"
#include <QTimer>
#include <QHelpEvent>
#include <QToolTip>
namespace Core
{
FITKWidgetSciNotation::FITKWidgetSciNotation(QWidget* parent) :
QLineEdit(parent)
{
setAttribute(Qt::WA_AlwaysShowToolTips);
//文本编辑控件编辑完成信号连接
connect(this, SIGNAL(editingFinished()), this, SLOT(slot_textChanged()));
connect(this, &FITKWidgetSciNotation::textEdited, this, &FITKWidgetSciNotation::slot_textEdited);
connect(this, &FITKWidgetSciNotation::textChanged, this, &FITKWidgetSciNotation::slot_textEdited);
}
FITKWidgetSciNotation::~FITKWidgetSciNotation()
{
}
void FITKWidgetSciNotation::setValue(double val)
{
// 保存数值。
_value = val;
// 更新文本。
disconnect(this, &FITKWidgetSciNotation::textChanged, this, &FITKWidgetSciNotation::slot_textEdited);
setText(QString("%1").arg(val));
connect(this, &FITKWidgetSciNotation::textChanged, this, &FITKWidgetSciNotation::slot_textEdited);
}
double FITKWidgetSciNotation::value()
{
return _value;
}
void FITKWidgetSciNotation::setRange(double* range)
{
_range[0] = range[0];
_range[1] = range[1];
}
bool FITKWidgetSciNotation::isValidText(QString message, double & value)
{
bool result = {};
//判断是否为空
if (message.isEmpty())return false;
//获取第一个字符串
QString firstChart = message.mid(0, 1);
//判断数据是否为负数qstring::toDouble不支持负数
if (firstChart == "-"){
QString stringData = message.remove(0, 1);
stringData.toDouble(&result);
if (result == true){
double doubleData = stringData.toDouble();
//数据取反
value = doubleData * (-1);
return true;
}
else{
return false;
}
}
else{
message.toDouble(&result);
if (result == true){
value = message.toDouble(&result);
return true;
}
else{
return false;
}
}
}
QString FITKWidgetSciNotation::getCurrentValidText()
{
return _currentValidText;
}
bool FITKWidgetSciNotation::getCurrentValidValue(double & value)
{
return isValidText(_currentValidText, value);
}
void FITKWidgetSciNotation::setCurrentValidValue(double value)
{
_currentValidText = QString::number(value);
setText(_currentValidText);
}
void FITKWidgetSciNotation::slot_textChanged()
{
bool result = true;
double value;
//判断文本是否是有效的科学计数法
result = isValidText(this->text(), value);
if (result == true){
//判断数据是否超出范围
if (value < _range[0] || value>_range[1]) {
this->setText(_currentValidText);
return;
}
_currentValidText = this->text();
emit dataChanged();
}
else{
this->setText(_currentValidText);
}
}
void FITKWidgetSciNotation::slot_textEdited(const QString & text)
{
_value = text.toDouble();
}
}