+ fix issue with default value and unit in InputField

This commit is contained in:
wmayer 2014-06-06 16:17:47 +02:00
parent 2fcce866a7
commit f0bd78bee9
4 changed files with 52 additions and 29 deletions

View File

@ -60,9 +60,11 @@ private:
InputField::InputField(QWidget * parent)
: QLineEdit(parent),
StepSize(1.0),
actUnitValue(0),
validInput(true),
Maximum(DOUBLE_MAX),
Minimum(-DOUBLE_MAX),
StepSize(1.0),
HistorySize(5),
SaveSize(5)
{
@ -106,6 +108,13 @@ QPixmap InputField::getValidationIcon(const char* name, const QSize& size) const
return icon;
}
void InputField::updateText(const Base::Quantity& quant)
{
double dFactor;
QString unit;
setText(quant.getUserString(dFactor,unit));
}
void InputField::resizeEvent(QResizeEvent *)
{
QSize sz = iconLabel->sizeHint();
@ -171,7 +180,8 @@ void InputField::newInput(const QString & text)
Quantity res;
try {
res = Quantity::parse(text);
}catch(Base::Exception &e){
}
catch(Base::Exception &e){
ErrorText = e.what();
this->setToolTip(QString::fromAscii(ErrorText.c_str()));
QPixmap pixmap = getValidationIcon(":/icons/button_invalid.svg", QSize(sizeHint().height(),sizeHint().height()));
@ -181,6 +191,9 @@ void InputField::newInput(const QString & text)
return;
}
if (res.getUnit().isEmpty())
res.setUnit(this->actUnit);
// check if unit fits!
if(!actUnit.isEmpty() && !res.getUnit().isEmpty() && actUnit != res.getUnit()){
this->setToolTip(QString::fromAscii("Wrong unit"));
@ -207,11 +220,12 @@ void InputField::newInput(const QString & text)
}
this->setToolTip(QString::fromAscii(ErrorText.c_str()));
actQuantity = res;
double dFactor;
res.getUserString(dFactor,actUnitStr);
// calculate the number shown
actUnitValue = res.getValue()/dFactor;
actQuantity = res;
// signaling
valueChanged(res);
valueChanged(res.getValue());
@ -339,13 +353,9 @@ void InputField::setValue(const Base::Quantity& quant)
if (actQuantity.getValue() < Minimum)
actQuantity.setValue(Minimum);
if (!quant.getUnit().isEmpty())
actUnit = quant.getUnit();
double dFactor;
setText(quant.getUserString(dFactor,actUnitStr));
actUnitValue = quant.getValue()/dFactor;
validInput = true;
updateText(quant);
}
void InputField::setValue(const double& value)
@ -353,10 +363,11 @@ void InputField::setValue(const double& value)
setValue(Base::Quantity(value, actUnit));
}
void InputField::setUnit(const Base::Unit& unit)
{
actUnit = unit;
actQuantity.setUnit(unit);
updateText(actQuantity);
}
const Base::Unit& InputField::getUnit() const
@ -386,8 +397,10 @@ double InputField::maximum(void)const
void InputField::setMaximum(double m)
{
Maximum = m;
if (actQuantity.getValue() > Maximum)
if (actQuantity.getValue() > Maximum) {
actQuantity.setValue(Maximum);
updateText(actQuantity);
}
}
/// get the value of the minimum property
@ -400,11 +413,13 @@ double InputField::minimum(void)const
void InputField::setMinimum(double m)
{
Minimum = m;
if (actQuantity.getValue() < Minimum)
if (actQuantity.getValue() < Minimum) {
actQuantity.setValue(Minimum);
updateText(actQuantity);
}
}
void InputField::setUnitText(QString str)
void InputField::setUnitText(const QString& str)
{
Base::Quantity quant = Base::Quantity::parse(str);
setUnit(quant.getUnit());
@ -441,6 +456,13 @@ void InputField::selectNumber(void)
setSelection(0,i);
}
void InputField::showEvent(QShowEvent * event)
{
QLineEdit::showEvent(event);
updateText(actQuantity);
}
void InputField::keyPressEvent(QKeyEvent *event)
{
switch (event->key()) {

View File

@ -106,7 +106,7 @@ public:
/// set the value of the minimum property
void setHistorySize(int);
/// set the unit by a string (can be used in the *.ui file)
void setUnitText(QString);
void setUnitText(const QString&);
/// get the unit as a string (can be used in the *.ui file)
QString getUnitText(void);
/// set the number portion selected (use after setValue())
@ -156,6 +156,7 @@ protected Q_SLOTS:
void updateIconLabel(const QString& text);
protected:
virtual void showEvent(QShowEvent * event);
virtual void keyPressEvent(QKeyEvent * event);
virtual void wheelEvent(QWheelEvent * event);
virtual void contextMenuEvent(QContextMenuEvent * event);
@ -163,6 +164,7 @@ protected:
private:
QPixmap getValidationIcon(const char* name, const QSize& size) const;
void updateText(const Base::Quantity&);
private:
QLabel* iconLabel;

View File

@ -132,7 +132,7 @@ void Placement::slotActiveDocument(const Gui::Document& doc)
documents.insert(doc.getDocument()->getName());
}
bool Placement::hasValidInputs()
bool Placement::hasValidInputs() const
{
QList<Gui::InputField*> sb = this->findChildren<Gui::InputField*>();
for (QList<Gui::InputField*>::iterator it = sb.begin(); it != sb.end(); ++it) {
@ -142,7 +142,6 @@ bool Placement::hasValidInputs()
return true;
}
void Placement::revertTransformation()
{
for (std::set<std::string>::iterator it = documents.begin(); it != documents.end(); ++it) {

View File

@ -73,7 +73,7 @@ private:
void applyPlacement(const QString& p, bool incremental);
void revertTransformation();
void slotActiveDocument(const Gui::Document&);
bool hasValidInputs();
bool hasValidInputs() const;
Q_SIGNALS:
void placementChanged(const QVariant &, bool, bool);