+ 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

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

View File

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

View File

@ -132,17 +132,16 @@ void Placement::slotActiveDocument(const Gui::Document& doc)
documents.insert(doc.getDocument()->getName()); documents.insert(doc.getDocument()->getName());
} }
bool Placement::hasValidInputs() bool Placement::hasValidInputs() const
{ {
QList<Gui::InputField*> sb = this->findChildren<Gui::InputField*>(); QList<Gui::InputField*> sb = this->findChildren<Gui::InputField*>();
for (QList<Gui::InputField*>::iterator it = sb.begin(); it != sb.end(); ++it) { for (QList<Gui::InputField*>::iterator it = sb.begin(); it != sb.end(); ++it) {
if(!(*it)->hasValidInput()) if (!(*it)->hasValidInput())
return false; return false;
} }
return true; return true;
} }
void Placement::revertTransformation() void Placement::revertTransformation()
{ {
for (std::set<std::string>::iterator it = documents.begin(); it != documents.end(); ++it) { 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 applyPlacement(const QString& p, bool incremental);
void revertTransformation(); void revertTransformation();
void slotActiveDocument(const Gui::Document&); void slotActiveDocument(const Gui::Document&);
bool hasValidInputs(); bool hasValidInputs() const;
Q_SIGNALS: Q_SIGNALS:
void placementChanged(const QVariant &, bool, bool); void placementChanged(const QVariant &, bool, bool);