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);
    }
}
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();
}
Exemple #3
0
App::DocumentObjectExecReturn * DrawSVGTemplate::execute(void)
{
    std::string templValue = Template.getValue();
    if (templValue.empty())
        return App::DocumentObject::StdReturn;

    Base::FileInfo fi(templValue);
    if (!fi.isReadable()) {
        // non-empty template value, but can't read file
        // if there is a old absolute template file set use a redirect
        fi.setFile(App::Application::getResourceDir() + "Mod/Drawing/Templates/" + fi.fileName());
        // try the redirect
        if (!fi.isReadable()) {
            Base::Console().Log("DrawPage::execute() not able to open %s!\n",Template.getValue());
            std::string error = std::string("Cannot open file ") + Template.getValue();
            return new App::DocumentObjectExecReturn(error);
        }
    }

    if (std::string(PageResult.getValue()).empty())                    //first time through?
        PageResult.setValue(fi.filePath().c_str());

    // open Template file
    string line;
    ifstream inTemplate (fi.filePath().c_str());

    ostringstream copyTemplate;
    string tempendl = "--endOfLine--";

    //inTemplate to copyTemplate
    //remove DrawingContent comment line
    //change line endings
    //capture TitleBlock dimensions
    while (getline(inTemplate,line))
    {
        // copy every line except the DrawingContent comment?
        if(line.find("<!-- DrawingContent -->") == string::npos) {
            // if not -  write through
            copyTemplate << line << tempendl;
        }

        //double t0, t1,t2,t3;
        float t0, t1,t2,t3;
        if(line.find("<!-- Title block") != std::string::npos) {
            (void) sscanf(line.c_str(), "%*s %*s %*s %f %f %f %f", &t0, &t1, &t2, &t3);    //eg "    <!-- Working space 10 10 410 287 -->"
                                                                                           //coverity 151677
            blockDimensions = QRectF(t0, t1, t2 - t0, t3 - t1);
        }

    }
    inTemplate.close();

    string outfragment(copyTemplate.str());
    std::string newfragment = outfragment;

    // update EditableText SVG clauses with Property values
    std::map<std::string, std::string> subs = EditableTexts.getValues();

    if (subs.size() > 0) {
        boost::regex e1 ("<text.*?freecad:editable=\"(.*?)\".*?<tspan.*?>(.*?)</tspan>");
        string::const_iterator begin, end;
        begin = outfragment.begin();
        end = outfragment.end();
        boost::match_results<std::string::const_iterator> what;

        // Find editable texts
        while (boost::regex_search(begin, end, what, e1)) {            //search in outfragment
            // if we have a replacement value for the text we've found
            if (subs.count(what[1].str())) {
                 // change it to specified value
                 boost::regex e2 ("(<text.*?freecad:editable=\"" + what[1].str() + "\".*?<tspan.*?)>(.*?)(</tspan>)");
                 newfragment = boost::regex_replace(newfragment, e2, "$1>" + subs[what[1].str()] + "$3");   //replace in newfragment
            }
            begin = what[0].second;
        }
    }


    // restoring linebreaks and saving the file
    boost::regex e3 ("--endOfLine--");
    string fmt = "\\n";
    outfragment = boost::regex_replace(newfragment, e3, fmt);

    const QString qsOut = QString::fromStdString(outfragment);
    QDomDocument doc(QString::fromLatin1("mydocument"));

    //if (!doc.setContent(&resultFile)) {
    if (!doc.setContent(qsOut)) {
        //setError(); //???? how/when does this get reset?
        std::string errMsg = std::string("Invalid SVG syntax in ") + getNameInDocument() + std::string(" - Check EditableTexts");
        return new App::DocumentObjectExecReturn(errMsg);
    } else {
        // make a temp file for FileIncluded Property
        string tempName = PageResult.getExchangeTempFile();
        ofstream outfinal(tempName.c_str());
        outfinal << outfragment;
        outfinal.close();
        PageResult.setValue(tempName.c_str());
    }

    // Calculate the dimensions of the page and store for retrieval
    // Parse the document XML
    QDomElement docElem = doc.documentElement();

    // Obtain the size of the SVG document by reading the document attributes
    Base::Quantity quantity;

    // Obtain the width
    QString str = docElem.attribute(QString::fromLatin1("width"));
    quantity = Base::Quantity::parse(str);
    quantity.setUnit(Base::Unit::Length);

    Width.setValue(quantity.getValue());

    str = docElem.attribute(QString::fromLatin1("height"));
    quantity = Base::Quantity::parse(str);
    quantity.setUnit(Base::Unit::Length);

    Height.setValue(quantity.getValue());

    bool isLandscape = getWidth() / getHeight() >= 1.;

    Orientation.setValue(isLandscape ? 1 : 0);

    return TechDraw::DrawTemplate::execute();
}
PyObject* SketchObjectPy::getDatum(PyObject *args)
{
    const std::vector<Constraint*>& vals = this->getSketchObjectPtr()->Constraints.getValues();
    Constraint* constr = 0;

    do {
        int index = 0;
        if (PyArg_ParseTuple(args,"i", &index)) {
            if (index < 0 || index >= static_cast<int>(vals.size())) {
                PyErr_SetString(PyExc_IndexError, "index out of range");
                return 0;
            }

            constr = vals[index];
            break;
        }

        PyErr_Clear();
        char* name;
        if (PyArg_ParseTuple(args,"s", &name)) {
            int id = 0;
            for (std::vector<Constraint*>::const_iterator it = vals.begin(); it != vals.end(); ++it, ++id) {
                if (Sketcher::PropertyConstraintList::getConstraintName((*it)->Name, id) == name) {
                    constr = *it;
                    break;
                }
            }

            if (!constr) {
                std::stringstream str;
                str << "Invalid constraint name: '" << name << "'";
                PyErr_SetString(PyExc_NameError, str.str().c_str());
                return 0;
            }
            else {
                break;
            }
        }

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

    ConstraintType type = constr->Type;
    if (type != Distance &&
        type != DistanceX &&
        type != DistanceY &&
        type != Radius &&
        type != Angle) {
        PyErr_SetString(PyExc_TypeError, "Constraint is not a datum");
        return 0;
    }

    Base::Quantity datum;
    datum.setValue(constr->getValue());
    if (type == Angle) {
        datum.setValue(Base::toDegrees<double>(datum.getValue()));
        datum.setUnit(Base::Unit::Angle);
    }
    else {
        datum.setUnit(Base::Unit::Length);
    }

    return new Base::QuantityPy(new Base::Quantity(datum));
}
QVariant PropertyConstraintListItem::value(const App::Property* prop) const
{
    assert(prop && prop->getTypeId().isDerivedFrom(Sketcher::PropertyConstraintList::getClassTypeId()));

    PropertyConstraintListItem* self = const_cast<PropertyConstraintListItem*>(this);

    int id = 1;

    QList<Base::Quantity> quantities;
    QList<Base::Quantity> subquantities;
    bool onlyNamed = true;

    const std::vector< Sketcher::Constraint * > &vals = static_cast<const Sketcher::PropertyConstraintList*>(prop)->getValues();
    for (std::vector< Sketcher::Constraint* >::const_iterator it = vals.begin();it != vals.end(); ++it, ++id) {
        if ((*it)->Type == Sketcher::Distance || // Datum constraint
            (*it)->Type == Sketcher::DistanceX ||
            (*it)->Type == Sketcher::DistanceY ||
            (*it)->Type == Sketcher::Radius ||
            (*it)->Type == Sketcher::Angle ) {

            Base::Quantity quant;
            if ((*it)->Type == Sketcher::Angle ) {
                double datum = Base::toDegrees<double>((*it)->getValue());
                quant.setUnit(Base::Unit::Angle);
                quant.setValue(datum);
            }
            else {
                quant.setUnit(Base::Unit::Length);
                quant.setValue((*it)->getValue());
            }

            quantities.append(quant);

            // Use a 7-bit ASCII string for the internal name.
            // See also comment in PropertyConstraintListItem::initialize()
            QString internalName = QString::fromLatin1("Constraint%1").arg(id);
            PropertyConstraintListItem* self = const_cast<PropertyConstraintListItem*>(this);

            self->blockEvent = true;

            if ((*it)->Name.empty() && !onlyUnnamed) {
                onlyNamed = false;
                subquantities.append(quant);
                PropertyConstraintListItem* unnamednode = static_cast<PropertyConstraintListItem*>(self->child(self->childCount()-1));
                unnamednode->blockEvent = true;
                unnamednode->setProperty(internalName.toLatin1(), QVariant::fromValue<Base::Quantity>(quant));
                unnamednode->blockEvent = false;
            }
            else {
                self->setProperty(internalName.toLatin1(), QVariant::fromValue<Base::Quantity>(quant));
            }

            self->blockEvent = false;
        }
    }

    // The quantities of unnamed constraints are only needed for display purposes inside toString()
    if (!onlyUnnamed && !onlyNamed) {
        self->blockEvent = true;
        self->setProperty("Unnamed", QVariant::fromValue< QList<Base::Quantity> >(subquantities));
        self->blockEvent = false;
    }

    return QVariant::fromValue< QList<Base::Quantity> >(quantities);
}