Skip to content

Commit

Permalink
+ fix issue with default value and unit in InputField
Browse files Browse the repository at this point in the history
  • Loading branch information
wwmayer committed Jun 6, 2014
1 parent 2fcce86 commit f0bd78b
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 29 deletions.
68 changes: 45 additions & 23 deletions src/Gui/InputField.cpp
Expand Up @@ -58,13 +58,15 @@ class InputValidator : public QValidator

// --------------------------------------------------------------------

InputField::InputField ( QWidget * parent )
: QLineEdit(parent),
StepSize(1.0),
Maximum(DOUBLE_MAX),
Minimum(-DOUBLE_MAX),
HistorySize(5),
SaveSize(5)
InputField::InputField(QWidget * parent)
: QLineEdit(parent),
actUnitValue(0),
validInput(true),
Maximum(DOUBLE_MAX),
Minimum(-DOUBLE_MAX),
StepSize(1.0),
HistorySize(5),
SaveSize(5)
{
setValidator(new InputValidator(this));
iconLabel = new QLabel(this);
Expand All @@ -82,7 +84,7 @@ InputField::InputField ( QWidget * parent )

this->setContextMenuPolicy(Qt::DefaultContextMenu);

QObject::connect(this, SIGNAL(textChanged (QString)),
QObject::connect(this, SIGNAL(textChanged(QString)),
this, SLOT(newInput(QString)));
}

Expand All @@ -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();
Expand Down Expand Up @@ -169,9 +178,10 @@ void InputField::contextMenuEvent(QContextMenuEvent *event)
void InputField::newInput(const QString & text)
{
Quantity res;
try{
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()));
Expand All @@ -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"));
Expand All @@ -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;
actUnitValue = res.getValue()/dFactor;
actQuantity = res;

// signaling
valueChanged(res);
valueChanged(res.getValue());
Expand Down Expand Up @@ -339,24 +353,21 @@ void InputField::setValue(const Base::Quantity& quant)
if (actQuantity.getValue() < Minimum)
actQuantity.setValue(Minimum);

if (!quant.getUnit().isEmpty())
actUnit = quant.getUnit();
actUnit = quant.getUnit();

double dFactor;
setText(quant.getUserString(dFactor,actUnitStr));
actUnitValue = quant.getValue()/dFactor;
validInput = true;
updateText(quant);
}

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
Expand Down Expand Up @@ -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
Expand All @@ -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());
Expand Down Expand Up @@ -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()) {
Expand Down
4 changes: 3 additions & 1 deletion src/Gui/InputField.h
Expand Up @@ -106,7 +106,7 @@ class GuiExport InputField : public QLineEdit
/// 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())
Expand Down Expand Up @@ -156,13 +156,15 @@ 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);
virtual void resizeEvent(QResizeEvent*);

private:
QPixmap getValidationIcon(const char* name, const QSize& size) const;
void updateText(const Base::Quantity&);

private:
QLabel* iconLabel;
Expand Down
7 changes: 3 additions & 4 deletions src/Gui/Placement.cpp
Expand Up @@ -132,17 +132,16 @@ 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) {
if(!(*it)->hasValidInput())
return false;
if (!(*it)->hasValidInput())
return false;
}
return true;
}


void Placement::revertTransformation()
{
for (std::set<std::string>::iterator it = documents.begin(); it != documents.end(); ++it) {
Expand Down
2 changes: 1 addition & 1 deletion src/Gui/Placement.h
Expand Up @@ -73,7 +73,7 @@ private Q_SLOTS:
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);
Expand Down

0 comments on commit f0bd78b

Please sign in to comment.