/// sets the field with a quantity
void InputField::setValue(const Base::Quantity& quant)
{
    actQuantity = quant;
    if(!quant.getUnit().isEmpty())
        actUnit = quant.getUnit();

    double dFactor;
    setText(quant.getUserString(dFactor,actUnitStr));
    actUnitValue = quant.getValue()/dFactor;
}
Ejemplo n.º 2
0
QString QuantitySpinBox::textFromValue(const Base::Quantity& value) const
{
    double factor;
    QString unitStr;
    QString str = value.getUserString(factor, unitStr);
    if (qAbs(value.getValue()) >= 1000.0) {
        str.remove(locale().groupSeparator());
    }
    return str;
}
Ejemplo n.º 3
0
void DlgExpressionInput::textChanged(const QString &text)
{
    try {
        //resize the input field according to text size
        QFontMetrics fm(ui->expression->font());
        int width = fm.width(text) + 15;
        if (width < minimumWidth)
            ui->expression->setMinimumWidth(minimumWidth);
        else
            ui->expression->setMinimumWidth(width);
        
        if(this->width() < ui->expression->minimumWidth())
            setMinimumWidth(ui->expression->minimumWidth());

        //now handle expression
        boost::shared_ptr<Expression> expr(ExpressionParser::parse(path.getDocumentObject(), text.toUtf8().constData()));

        if (expr) {
            std::string error = path.getDocumentObject()->ExpressionEngine.validateExpression(path, expr);

            if (error.size() > 0)
                throw Base::Exception(error.c_str());

            std::unique_ptr<Expression> result(expr->eval());

            expression = expr;
            ui->okBtn->setEnabled(true);
            ui->msg->clear();

            NumberExpression * n = Base::freecad_dynamic_cast<NumberExpression>(result.get());
            if (n) {
                Base::Quantity value = n->getQuantity();

                if (!value.getUnit().isEmpty() && value.getUnit() != impliedUnit)
                    throw Base::Exception("Unit mismatch between result and required unit");

                value.setUnit(impliedUnit);

                ui->msg->setText(value.getUserString());
            }
            else
                ui->msg->setText(Base::Tools::fromStdString(result->toString()));

            //set default palette as we may have read text right now
            ui->msg->setPalette(ui->okBtn->palette());
        }
    }
    catch (Base::Exception & e) {
        ui->msg->setText(QString::fromUtf8(e.what()));
        QPalette p(ui->msg->palette());
        p.setColor(QPalette::WindowText, Qt::red);
        ui->msg->setPalette(p);
        ui->okBtn->setDisabled(true);
    }
}
PyObject* SketchObjectPy::setDatum(PyObject *args)
{
    double Datum;
    int    Index;
    PyObject* object;
    Base::Quantity Quantity;
    if (PyArg_ParseTuple(args,"iO!", &Index, &(Base::QuantityPy::Type), &object)) {
        Quantity = *(static_cast<Base::QuantityPy*>(object)->getQuantityPtr());
        if (Quantity.getUnit() == Base::Unit::Angle)
            //Datum = Quantity.getValueAs(Base::Quantity::Radian);
            Datum = Base::toRadians<double>(Quantity.getValue());
        else
            Datum = Quantity.getValue();
    }
    else {
        PyErr_Clear();
        if (!PyArg_ParseTuple(args, "id", &Index, &Datum))
            return 0;
        Quantity.setValue(Datum);
    }

    int err=this->getSketchObjectPtr()->setDatum(Index, Datum);
    if (err) {
        std::stringstream str;
        if (err == -1)
            str << "Invalid constraint index: " << Index;
        else if (err == -3)
            str << "Cannot set the datum because the sketch contains conflicting constraints";
        else if (err == -2)
            str << "Datum " << (const char*)Quantity.getUserString().toUtf8() << " for the constraint with index " << Index << " is invalid";
        else if (err == -4)
            str << "Negative datum values are not valid for the constraint with index " << Index;
        else if (err == -5)
            str << "Zero is not a valid datum for the constraint with index " << Index;
        else
            str << "Unexpected problem at setting datum " << (const char*)Quantity.getUserString().toUtf8() << " for the constraint with index " << Index;
        PyErr_SetString(PyExc_ValueError, str.str().c_str());
        return 0;
    }

    Py_Return;
}
Ejemplo n.º 5
0
std::string  DrawViewDimension::getFormatedValue() const
{
    QString str = QString::fromUtf8(FormatSpec.getStrValue().c_str());
    double val = std::abs(getDimValue());
    //QLocale here(QLocale::German);                                           //for testing
    //here.setNumberOptions(QLocale::OmitGroupSeparator);
    QLocale here = QLocale();                                                  //system locale
    QString valText = here.toString(val, 'f',Precision.getValue());

    Base::Quantity qVal;
    qVal.setValue(val);
    if (Type.isValue("Angle")) {
        qVal.setUnit(Base::Unit::Angle);
    } else {
        qVal.setUnit(Base::Unit::Length);
    }
    QString userStr = qVal.getUserString();
    QStringList userSplit = userStr.split(QString::fromUtf8(" "),QString::SkipEmptyParts);   //break userString into number + UoM
    QString displayText;
    if (!userSplit.isEmpty()) {
       QString unitText = userSplit.back();
       displayText = valText + QString::fromUtf8(" ") + unitText;
    }

    QRegExp rx(QString::fromAscii("%(\\w+)%"));                        //any word bracketed by %
    QStringList list;
    int pos = 0;

    while ((pos = rx.indexIn(str, pos)) != -1) {
        list << rx.cap(0);
        pos += rx.matchedLength();
    }

    for(QStringList::const_iterator it = list.begin(); it != list.end(); ++it) {
        if(*it == QString::fromAscii("%value%")){
            str.replace(*it,displayText);
        } else {                                                       //insert additional placeholder replacement logic here
            str.replace(*it, QString::fromAscii(""));                  //maybe we should just leave what was there?
        }
    }
    return str.toStdString();
}
Ejemplo n.º 6
0
// Gets called after call of 'validateAndInterpret'
void QuantitySpinBox::userInput(const QString & text)
{
    Q_D(QuantitySpinBox);

    QString tmp = text;
    int pos = 0;
    QValidator::State state;
    Base::Quantity res = d->validateAndInterpret(tmp, pos, state);
    if (state == QValidator::Acceptable) {
        d->validInput = true;
        d->validStr = text;
    }
    else if (state == QValidator::Intermediate) {
        tmp = tmp.trimmed();
        tmp += QLatin1Char(' ');
        tmp += d->unitStr;
        Base::Quantity res2 = d->validateAndInterpret(tmp, pos, state);
        if (state == QValidator::Acceptable) {
            d->validInput = true;
            d->validStr = tmp;
            res = res2;
        }
        else {
            d->validInput = false;
            return;
        }
    }
    else {
        d->validInput = false;
        return;
    }

    double factor;
    res.getUserString(factor,d->unitStr);
    d->unitValue = res.getValue()/factor;
    d->quantity = res;

    // signaling
    valueChanged(res);
    valueChanged(res.getValue());
}
Ejemplo n.º 7
0
PyObject* SketchObjectPy::setDatum(PyObject *args)
{
    double Datum;
    int    Index;
    PyObject* object;
    Base::Quantity Quantity;

    do {
        // handle (int,Quantity)
        if (PyArg_ParseTuple(args,"iO!", &Index, &(Base::QuantityPy::Type), &object)) {
            Quantity = *(static_cast<Base::QuantityPy*>(object)->getQuantityPtr());
            if (Quantity.getUnit() == Base::Unit::Angle) {
                Datum = Base::toRadians<double>(Quantity.getValue());
                break;
            }
            else {
                Datum = Quantity.getValue();
                break;
            }
        }

        // handle (int,double)
        PyErr_Clear();
        if (PyArg_ParseTuple(args, "id", &Index, &Datum)) {
            Quantity.setValue(Datum);
            break;
        }

        // handle (string,Quantity)
        char* constrName;
        PyErr_Clear();
        if (PyArg_ParseTuple(args,"sO!", &constrName, &(Base::QuantityPy::Type), &object)) {
            Quantity = *(static_cast<Base::QuantityPy*>(object)->getQuantityPtr());
            if (Quantity.getUnit() == Base::Unit::Angle) {
                Datum = Base::toRadians<double>(Quantity.getValue());
            }
            else {
                Datum = Quantity.getValue();
            }

            int i = 0;
            Index = -1;
            const std::vector<Constraint*>& vals = this->getSketchObjectPtr()->Constraints.getValues();
            for (std::vector<Constraint*>::const_iterator it = vals.begin(); it != vals.end(); ++it, ++i) {
                if ((*it)->Name == constrName) {
                    Index = i;
                    break;
                }
            }

            if (Index >= 0) {
                break;
            }
            else {
                std::stringstream str;
                str << "Invalid constraint name: '" << constrName << "'";
                PyErr_SetString(PyExc_ValueError, str.str().c_str());
                return 0;
            }
        }

        // handle (string,double)
        PyErr_Clear();
        if (PyArg_ParseTuple(args, "sd", &constrName, &Datum)) {
            Quantity.setValue(Datum);
            int i = 0;
            Index = -1;
            const std::vector<Constraint*>& vals = this->getSketchObjectPtr()->Constraints.getValues();
            for (std::vector<Constraint*>::const_iterator it = vals.begin(); it != vals.end(); ++it, ++i) {
                if ((*it)->Name == constrName) {
                    Index = i;
                    break;
                }
            }

            if (Index >= 0) {
                break;
            }
            else {
                std::stringstream str;
                str << "Invalid constraint name: '" << constrName << "'";
                PyErr_SetString(PyExc_ValueError, str.str().c_str());
                return 0;
            }
        }

        // error handling
        PyErr_SetString(PyExc_TypeError, "Wrong arguments");
        return 0;
    }
    while (false);

    int err=this->getSketchObjectPtr()->setDatum(Index, Datum);
    if (err) {
        std::stringstream str;
        if (err == -1)
            str << "Invalid constraint index: " << Index;
        else if (err == -3)
            str << "Cannot set the datum because the sketch contains conflicting constraints";
        else if (err == -2)
            str << "Datum " << (const char*)Quantity.getUserString().toUtf8() << " for the constraint with index " << Index << " is invalid";
        else if (err == -4)
            str << "Negative datum values are not valid for the constraint with index " << Index;
        else if (err == -5)
            str << "Zero is not a valid datum for the constraint with index " << Index;
        else if (err == -6)
            str << "Cannot set the datum because of invalid geometry";
        else
            str << "Unexpected problem at setting datum " << (const char*)Quantity.getUserString().toUtf8() << " for the constraint with index " << Index;
        PyErr_SetString(PyExc_ValueError, str.str().c_str());
        return 0;
    }

    Py_Return;
}