diff --git a/src/Gui/DlgExpressionInput.cpp b/src/Gui/DlgExpressionInput.cpp index 99641c2dc..3d3531d94 100644 --- a/src/Gui/DlgExpressionInput.cpp +++ b/src/Gui/DlgExpressionInput.cpp @@ -34,10 +34,6 @@ using namespace App; using namespace Gui::Dialog; -const int DlgExpressionInput::h = 15; -const int DlgExpressionInput::r = 30; -const int DlgExpressionInput::d = 7; - DlgExpressionInput::DlgExpressionInput(const App::ObjectIdentifier & _path, boost::shared_ptr _expression, const Base::Unit & _impliedUnit, QWidget *parent) : QDialog(parent), ui(new Ui::DlgExpressionInput), @@ -45,7 +41,7 @@ DlgExpressionInput::DlgExpressionInput(const App::ObjectIdentifier & _path, boos path(_path), discarded(false), impliedUnit(_impliedUnit), - l(30) + minimumWidth(300) { assert(path.getDocumentObject() != 0); @@ -70,15 +66,9 @@ DlgExpressionInput::DlgExpressionInput(const App::ObjectIdentifier & _path, boos setAttribute(Qt::WA_TranslucentBackground, true); setParent(0); + setWindowFlags(Qt::Popup); + setModal(true); ui->expression->setFocus(); - - QDesktopWidget widget; - setMinimumWidth(widget.availableGeometry(widget.primaryScreen()).width()/2); - -#ifndef FC_DEBUG - ui->parsedExpr->setVisible(false); -#endif - } DlgExpressionInput::~DlgExpressionInput() @@ -86,54 +76,27 @@ DlgExpressionInput::~DlgExpressionInput() delete ui; } -QPoint DlgExpressionInput::tip() const +QPoint DlgExpressionInput::expressionPosition() const { - return QPoint(l - d, 0); -} - -void DlgExpressionInput::setGeometry(int x, int y, int w, int h) -{ - QDesktopWidget widget; - int screenWidth = widget.availableGeometry(widget.primaryScreen()).width(); - - if (x + w > screenWidth) { - l = l + (x + w - screenWidth); - x = screenWidth - w - 10; - } - - QWidget::setGeometry(x, y, w, h); -} - -void DlgExpressionInput::paintEvent(QPaintEvent * event) { - QPainter painter(this); - QPainterPath path; - - path.moveTo(0, h + r / 2); - path.arcTo(QRect(0, h, r, r), 180, -90); - path.lineTo(l, h); - path.lineTo(l - d, 0); - path.lineTo(l + d, h); - path.lineTo(width() - r - 1, h); - path.arcTo(QRect(width() - r - 1, h, r, r), 90, -90); - path.lineTo(width() - 1, height() - r); - path.arcTo(QRect(width() - r - 1, height() - r - 1, r, r), 0, -90); - path.lineTo(r, height() - 1); - path.arcTo(QRect(0, height() - r - 1, r, r), -90, -90); - path.lineTo(0, h + r/2); - - QPen pen(Qt::black); - QBrush brush(QColor(250, 250, 180)); - pen.setWidthF(2.0); - painter.setBrush(brush); - painter.setPen(pen); - - painter.fillPath(path, brush); - painter.drawPath(path); + return QPoint(0, ui->ctrlArea->height()+3); } 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 expr(ExpressionParser::parse(path.getDocumentObject(), text.toUtf8().constData())); if (expr) { @@ -142,15 +105,11 @@ void DlgExpressionInput::textChanged(const QString &text) if (error.size() > 0) throw Base::Exception(error.c_str()); -#ifdef FC_DEBUG - ui->parsedExpr->setText(Base::Tools::fromStdString(expr->toString())); -#endif - std::auto_ptr result(expr->eval()); expression = expr; ui->okBtn->setEnabled(true); - ui->errorMsg->setText(QString::fromUtf8("")); + ui->msg->setText(QString::fromUtf8("")); NumberExpression * n = Base::freecad_dynamic_cast(result.get()); if (n) { @@ -161,19 +120,21 @@ void DlgExpressionInput::textChanged(const QString &text) value.setUnit(impliedUnit); - ui->result->setText(value.getUserString()); + ui->msg->setText(value.getUserString()); } else - ui->result->setText(Base::Tools::fromStdString(result->toString())); + 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->errorMsg->setText(QString::fromUtf8(e.what())); - QPalette p(ui->errorMsg->palette()); + ui->msg->setText(QString::fromUtf8(e.what())); + QPalette p(ui->msg->palette()); p.setColor(QPalette::WindowText, Qt::red); - ui->errorMsg->setPalette(p); + ui->msg->setPalette(p); ui->okBtn->setDisabled(true); - ui->result->setText(QString::fromAscii("--")); } } @@ -183,4 +144,16 @@ void DlgExpressionInput::setDiscarded() reject(); } +void DlgExpressionInput::setExpressionInputSize(int width, int height) { + + if(ui->expression->minimumHeight() < height) + ui->expression->setMinimumHeight(height); + + if(ui->expression->minimumWidth() < width) + ui->expression->setMinimumWidth(width); + + minimumWidth = width; +} + + #include "moc_DlgExpressionInput.cpp" diff --git a/src/Gui/DlgExpressionInput.h b/src/Gui/DlgExpressionInput.h index ba1d80b48..d69cf08e8 100644 --- a/src/Gui/DlgExpressionInput.h +++ b/src/Gui/DlgExpressionInput.h @@ -58,9 +58,8 @@ public: bool discardedFormula() const { return discarded; } - void paintEvent(QPaintEvent *event); - - QPoint tip() const; + QPoint expressionPosition() const; + void setExpressionInputSize(int width, int height); void setGeometry(int x, int y, int w, int h); @@ -75,10 +74,7 @@ private: bool discarded; const Base::Unit impliedUnit; - static const int h; - int l; - static const int r; - static const int d; + int minimumWidth; }; } diff --git a/src/Gui/DlgExpressionInput.ui b/src/Gui/DlgExpressionInput.ui index b7b06f27b..ced996cd3 100644 --- a/src/Gui/DlgExpressionInput.ui +++ b/src/Gui/DlgExpressionInput.ui @@ -6,31 +6,118 @@ 0 0 - 600 - 134 + 414 + 95 - + 0 0 - 600 + 300 0 Formula editor - - - 25 + + + 3 - - + + 0 + + + + + + + + 0 + + + 0 + + + + + true + + + QFrame::StyledPanel + + + QFrame::Raised + + + + 4 + + + 2 + + + + + Result: + + + + + + + + + + + + 255 + 0 + 0 + + + + + + + + + 255 + 0 + 0 + + + + + + + + + 190 + 190 + 190 + + + + + + + + + + + + + + + + + @@ -38,115 +125,92 @@ - 40 + 0 20 + + + + - - - &Discard formula + + + + 0 + 0 + - - false - - - false + + + 10 + 10 + - - - Cancel + + + Qt::Horizontal - - false + + + 0 + 2 + - - - - - - Ok - - - true - - + - - - - - - - - - 255 - 0 - 0 - - - - - - - - - 255 - 0 - 0 - - - - - - - - - 190 - 190 - 190 - - - - - - - - - - + + + + + + 2 + + + + + &Discard + + + false + + + false + + + + + + + Ok + + + + + + + + + Qt::Horizontal + + + + 0 + 20 + + + + + - - - - => - - - - - - - - - - - 50 - 0 - - - - - - - - + Qt::Vertical @@ -154,19 +218,25 @@ 20 - 40 + 0 - - - - - - - + label + expression + result + horizontalSpacer_2 + widget + + ctrlArea + + verticalSpacer + horizontalSpacer + horizontalSpacer_2 + + horizontalSpacer_3 @@ -177,22 +247,6 @@ - - cancelBtn - clicked() - DlgExpressionInput - reject() - - - 399 - 69 - - - 392 - 85 - - - okBtn clicked() @@ -200,12 +254,12 @@ accept() - 505 - 68 + 275 + 94 - 505 - 94 + 310 + 63 diff --git a/src/Gui/QuantitySpinBox.cpp b/src/Gui/QuantitySpinBox.cpp index 697bdc287..66262e81d 100644 --- a/src/Gui/QuantitySpinBox.cpp +++ b/src/Gui/QuantitySpinBox.cpp @@ -496,8 +496,10 @@ void QuantitySpinBox::openFormulaDialog() Q_D(const QuantitySpinBox); Gui::Dialog::DlgExpressionInput box(getPath(), getExpression(), d->unit, this); - QPoint pos = mapToGlobal(QPoint(width() / 2, height() / 2)); - box.setGeometry(pos.x() - box.tip().x(), pos.y() - box.tip().y(), box.width(), box.height()); + QPoint pos = mapToGlobal(QPoint(0,0)); + box.move(pos-box.expressionPosition()); + box.setExpressionInputSize(width(), height()); + if (box.exec() == QDialog::Accepted) setExpression(box.getExpression()); else if (box.discardedFormula())