first take on alternative expression ui input dialog

This commit is contained in:
Stefan Tröger 2015-09-23 23:10:12 +02:00 committed by wmayer
parent 6a93d1392d
commit 96998d390a
4 changed files with 229 additions and 204 deletions

View File

@ -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<const Expression> _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<Expression> 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<Expression> 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<NumberExpression>(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"

View File

@ -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;
};
}

View File

@ -6,31 +6,118 @@
<rect>
<x>0</x>
<y>0</y>
<width>600</width>
<height>134</height>
<width>414</width>
<height>95</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="MinimumExpanding" vsizetype="Preferred">
<sizepolicy hsizetype="Expanding" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>600</width>
<width>300</width>
<height>0</height>
</size>
</property>
<property name="windowTitle">
<string>Formula editor</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<property name="topMargin">
<number>25</number>
<layout class="QVBoxLayout" name="verticalLayout">
<property name="spacing">
<number>3</number>
</property>
<item row="3" column="0" colspan="4">
<layout class="QHBoxLayout" name="horizontalLayout">
<property name="margin">
<number>0</number>
</property>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QWidget" name="widget" native="true">
<layout class="QVBoxLayout" name="verticalLayout_2">
<property name="spacing">
<number>0</number>
</property>
<property name="margin">
<number>0</number>
</property>
<item>
<widget class="QFrame" name="ctrlArea">
<property name="autoFillBackground">
<bool>true</bool>
</property>
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_4">
<property name="spacing">
<number>4</number>
</property>
<property name="margin">
<number>2</number>
</property>
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>Result:</string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="msg">
<property name="palette">
<palette>
<active>
<colorrole role="Text">
<brush brushstyle="SolidPattern">
<color alpha="255">
<red>255</red>
<green>0</green>
<blue>0</blue>
</color>
</brush>
</colorrole>
</active>
<inactive>
<colorrole role="Text">
<brush brushstyle="SolidPattern">
<color alpha="255">
<red>255</red>
<green>0</green>
<blue>0</blue>
</color>
</brush>
</colorrole>
</inactive>
<disabled>
<colorrole role="Text">
<brush brushstyle="SolidPattern">
<color alpha="255">
<red>190</red>
<green>190</green>
<blue>190</blue>
</color>
</brush>
</colorrole>
</disabled>
</palette>
</property>
<property name="text">
<string/>
</property>
</widget>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
@ -38,115 +125,92 @@
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<width>0</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_5">
<item>
<widget class="QPushButton" name="discardBtn">
<property name="text">
<string>&amp;Discard formula</string>
<widget class="Gui::ExpressionLineEdit" name="expression">
<property name="sizePolicy">
<sizepolicy hsizetype="Ignored" vsizetype="Ignored">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="autoDefault">
<bool>false</bool>
</property>
<property name="default">
<bool>false</bool>
<property name="minimumSize">
<size>
<width>10</width>
<height>10</height>
</size>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="cancelBtn">
<property name="text">
<string>Cancel</string>
<spacer name="horizontalSpacer_3">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="autoDefault">
<bool>false</bool>
<property name="sizeHint" stdset="0">
<size>
<width>0</width>
<height>2</height>
</size>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="okBtn">
<property name="text">
<string>Ok</string>
</property>
<property name="default">
<bool>true</bool>
</property>
</widget>
</spacer>
</item>
</layout>
</item>
<item row="2" column="1" colspan="3">
<widget class="QLabel" name="errorMsg">
<property name="palette">
<palette>
<active>
<colorrole role="Text">
<brush brushstyle="SolidPattern">
<color alpha="255">
<red>255</red>
<green>0</green>
<blue>0</blue>
</color>
</brush>
</colorrole>
</active>
<inactive>
<colorrole role="Text">
<brush brushstyle="SolidPattern">
<color alpha="255">
<red>255</red>
<green>0</green>
<blue>0</blue>
</color>
</brush>
</colorrole>
</inactive>
<disabled>
<colorrole role="Text">
<brush brushstyle="SolidPattern">
<color alpha="255">
<red>190</red>
<green>190</green>
<blue>190</blue>
</color>
</brush>
</colorrole>
</disabled>
</palette>
</property>
<property name="text">
<string/>
</property>
</widget>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_3">
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<property name="spacing">
<number>2</number>
</property>
<item>
<widget class="QPushButton" name="discardBtn">
<property name="text">
<string>&amp;Discard</string>
</property>
<property name="autoDefault">
<bool>false</bool>
</property>
<property name="default">
<bool>false</bool>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="okBtn">
<property name="text">
<string>Ok</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>0</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
<item row="0" column="2">
<widget class="QLabel" name="label">
<property name="text">
<string>=&gt;</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="Gui::ExpressionLineEdit" name="expression"/>
</item>
<item row="0" column="3">
<widget class="QLabel" name="result">
<property name="minimumSize">
<size>
<width>50</width>
<height>0</height>
</size>
</property>
<property name="text">
<string/>
</property>
</widget>
</item>
<item row="4" column="1">
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
@ -154,19 +218,25 @@
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
<height>0</height>
</size>
</property>
</spacer>
</item>
<item row="1" column="1">
<widget class="QLabel" name="parsedExpr">
<property name="text">
<string/>
</property>
</widget>
</item>
</layout>
<zorder>label</zorder>
<zorder>expression</zorder>
<zorder>result</zorder>
<zorder>horizontalSpacer_2</zorder>
<zorder>widget</zorder>
<zorder></zorder>
<zorder>ctrlArea</zorder>
<zorder></zorder>
<zorder>verticalSpacer</zorder>
<zorder>horizontalSpacer</zorder>
<zorder>horizontalSpacer_2</zorder>
<zorder></zorder>
<zorder>horizontalSpacer_3</zorder>
</widget>
<customwidgets>
<customwidget>
@ -177,22 +247,6 @@
</customwidgets>
<resources/>
<connections>
<connection>
<sender>cancelBtn</sender>
<signal>clicked()</signal>
<receiver>DlgExpressionInput</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>399</x>
<y>69</y>
</hint>
<hint type="destinationlabel">
<x>392</x>
<y>85</y>
</hint>
</hints>
</connection>
<connection>
<sender>okBtn</sender>
<signal>clicked()</signal>
@ -200,12 +254,12 @@
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>505</x>
<y>68</y>
<x>275</x>
<y>94</y>
</hint>
<hint type="destinationlabel">
<x>505</x>
<y>94</y>
<x>310</x>
<y>63</y>
</hint>
</hints>
</connection>

View File

@ -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())