Skip to content

Commit

Permalink
Gui: QuantitySpinBox inherits from ExpressionSpinBox to reduce code d…
Browse files Browse the repository at this point in the history
…uplication
  • Loading branch information
wwmayer committed Apr 2, 2021
1 parent e151f19 commit 5d49bf7
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 140 deletions.
144 changes: 16 additions & 128 deletions src/Gui/QuantitySpinBox.cpp
Expand Up @@ -290,7 +290,7 @@ class QuantitySpinBoxPrivate

QuantitySpinBox::QuantitySpinBox(QWidget *parent)
: QAbstractSpinBox(parent),
ExpressionBinding(),
ExpressionSpinBox(this),
d_ptr(new QuantitySpinBoxPrivate())
{
d_ptr->locale = locale();
Expand All @@ -300,17 +300,13 @@ QuantitySpinBox::QuantitySpinBox(QWidget *parent)
QObject::connect(this, SIGNAL(editingFinished()),
this, SLOT(handlePendingEmit()));

makeLabel(lineEdit());

// When a style sheet is set the text margins for top/bottom must be set to avoid to squash the widget
#ifndef Q_OS_MAC
lineEdit()->setTextMargins(0, 2, 0, 2);
#else
// https://forum.freecadweb.org/viewtopic.php?f=8&t=50615
lineEdit()->setTextMargins(0, 2, 0, 0);
#endif

QObject::connect(iconLabel, SIGNAL(clicked()), this, SLOT(openFormulaDialog()));
}

QuantitySpinBox::~QuantitySpinBox()
Expand All @@ -324,22 +320,6 @@ void QuantitySpinBox::bind(const App::ObjectIdentifier &_path)
iconLabel->show();
}

void Gui::QuantitySpinBox::setExpression(std::shared_ptr<Expression> expr)
{
Q_ASSERT(isBound());

try {
ExpressionBinding::setExpression(expr);
}
catch (const Base::Exception & e) {
setReadOnly(true);
QPalette p(lineEdit()->palette());
p.setColor(QPalette::Active, QPalette::Text, Qt::red);
lineEdit()->setPalette(p);
iconLabel->setToolTip(QString::fromLatin1(e.what()));
}
}

QString QuantitySpinBox::boundToName() const
{
if (isBound()) {
Expand Down Expand Up @@ -412,44 +392,12 @@ QString Gui::QuantitySpinBox::expressionText() const
return QString();
}


void Gui::QuantitySpinBox::onChange()
void Gui::QuantitySpinBox::setNumberExpression(App::NumberExpression* expr)
{
Q_ASSERT(isBound());

if (getExpression()) {
std::unique_ptr<Expression> result(getExpression()->eval());
NumberExpression * value = freecad_dynamic_cast<NumberExpression>(result.get());

if (value) {
std::stringstream s;
s << value->getValue();

lineEdit()->setText(getUserString(value->getQuantity()));
handlePendingEmit();

setReadOnly(true);
QPixmap pixmap = getIcon(":/icons/bound-expression.svg", QSize(iconHeight, iconHeight));
iconLabel->setPixmap(pixmap);

QPalette p(lineEdit()->palette());
p.setColor(QPalette::Text, Qt::lightGray);
lineEdit()->setPalette(p);
}
iconLabel->setExpressionText(Base::Tools::fromStdString(getExpression()->toString()));
}
else {
setReadOnly(false);
QPixmap pixmap = getIcon(":/icons/bound-expression-unset.svg", QSize(iconHeight, iconHeight));
iconLabel->setPixmap(pixmap);
QPalette p(lineEdit()->palette());
p.setColor(QPalette::Active, QPalette::Text, defaultPalette.color(QPalette::Text));
lineEdit()->setPalette(p);
iconLabel->setExpressionText(QString());
}
lineEdit()->setText(getUserString(expr->getQuantity()));
handlePendingEmit();
}


bool QuantitySpinBox::apply(const std::string & propName)
{
if (!ExpressionBinding::apply(propName)) {
Expand Down Expand Up @@ -480,70 +428,20 @@ bool QuantitySpinBox::apply(const std::string & propName)
void QuantitySpinBox::resizeEvent(QResizeEvent * event)
{
QAbstractSpinBox::resizeEvent(event);

int frameWidth = style()->pixelMetric(QStyle::PM_SpinBoxFrameWidth);

QSize sz = iconLabel->sizeHint();
iconLabel->move(lineEdit()->rect().right() - frameWidth - sz.width(), 0);

try {
if (isBound() && getExpression()) {
std::unique_ptr<Expression> result(getExpression()->eval());
NumberExpression * value = freecad_dynamic_cast<NumberExpression>(result.get());

if (value) {
setReadOnly(true);
QPixmap pixmap = getIcon(":/icons/bound-expression.svg", QSize(iconHeight, iconHeight));
iconLabel->setPixmap(pixmap);

QPalette p(lineEdit()->palette());
p.setColor(QPalette::Text, Qt::lightGray);
lineEdit()->setPalette(p);
}
iconLabel->setExpressionText(Base::Tools::fromStdString(getExpression()->toString()));
}
else {
setReadOnly(false);
QPixmap pixmap = getIcon(":/icons/bound-expression-unset.svg", QSize(iconHeight, iconHeight));
iconLabel->setPixmap(pixmap);

QPalette p(lineEdit()->palette());
p.setColor(QPalette::Active, QPalette::Text, defaultPalette.color(QPalette::Text));
lineEdit()->setPalette(p);
iconLabel->setExpressionText(QString());
}
}
catch (const Base::Exception & e) {
setReadOnly(true);
QPalette p(lineEdit()->palette());
p.setColor(QPalette::Active, QPalette::Text, Qt::red);
lineEdit()->setPalette(p);
iconLabel->setToolTip(QString::fromLatin1(e.what()));
}

resizeWidget();
}

void Gui::QuantitySpinBox::keyPressEvent(QKeyEvent *event)
{
if (event->text() == QString::fromUtf8("=") && isBound())
openFormulaDialog();
else
if (!handleKeyEvent(event->text()))
QAbstractSpinBox::keyPressEvent(event);
}

void Gui::QuantitySpinBox::paintEvent(QPaintEvent*)
{
QStyleOptionSpinBox opt;
initStyleOption(&opt);
if (hasExpression()) {
opt.activeSubControls &= ~QStyle::SC_SpinBoxUp;
opt.activeSubControls &= ~QStyle::SC_SpinBoxDown;
opt.state &= ~QStyle::State_Active;
opt.stepEnabled = StepNone;
}

QStylePainter p(this);
p.drawComplexControl(QStyle::CC_SpinBox, opt);
drawControl(opt);
}

void QuantitySpinBox::updateText(const Quantity &quant)
Expand Down Expand Up @@ -629,7 +527,15 @@ void QuantitySpinBox::openFormulaDialog()

Q_D(const QuantitySpinBox);
Gui::Dialog::DlgExpressionInput* box = new Gui::Dialog::DlgExpressionInput(getPath(), getExpression(), d->unit, this);
connect(box, SIGNAL(finished(int)), this, SLOT(finishFormulaDialog()));
QObject::connect(box, &Gui::Dialog::DlgExpressionInput::finished, [=]() {
if (box->result() == QDialog::Accepted)
setExpression(box->getExpression());
else if (box->discardedFormula())
setExpression(std::shared_ptr<Expression>());

box->deleteLater();
Q_EMIT showFormulaDialog(false);
});
box->show();

QPoint pos = mapToGlobal(QPoint(0,0));
Expand All @@ -639,24 +545,6 @@ void QuantitySpinBox::openFormulaDialog()
Q_EMIT showFormulaDialog(true);
}

void QuantitySpinBox::finishFormulaDialog()
{
Gui::Dialog::DlgExpressionInput* box = qobject_cast<Gui::Dialog::DlgExpressionInput*>(sender());
if (!box) {
qWarning() << "Sender is not a Gui::Dialog::DlgExpressionInput";
return;
}

if (box->result() == QDialog::Accepted)
setExpression(box->getExpression());
else if (box->discardedFormula())
setExpression(std::shared_ptr<Expression>());

box->deleteLater();

Q_EMIT showFormulaDialog(false);
}

void QuantitySpinBox::handlePendingEmit()
{
updateFromCache(true);
Expand Down
16 changes: 5 additions & 11 deletions src/Gui/QuantitySpinBox.h
Expand Up @@ -24,10 +24,9 @@
#ifndef GUI_QUANTITYSPINBOX_H
#define GUI_QUANTITYSPINBOX_H

#include <QAbstractSpinBox>
#include <Base/UnitsSchema.h>
#include <Gui/MetaTypes.h>
#include "ExpressionBinding.h"
#include <Gui/SpinBox.h>

#ifdef Q_MOC_RUN
Q_DECLARE_METATYPE(Base::Quantity)
Expand All @@ -36,7 +35,7 @@ Q_DECLARE_METATYPE(Base::Quantity)
namespace Gui {

class QuantitySpinBoxPrivate;
class GuiExport QuantitySpinBox : public QAbstractSpinBox, public ExpressionBinding
class GuiExport QuantitySpinBox : public QAbstractSpinBox, public ExpressionSpinBox
{
Q_OBJECT

Expand All @@ -50,8 +49,6 @@ class GuiExport QuantitySpinBox : public QAbstractSpinBox, public ExpressionBind
Q_PROPERTY(QString expression READ expressionText)

public:
using ExpressionBinding::apply;

explicit QuantitySpinBox(QWidget *parent = 0);
virtual ~QuantitySpinBox();

Expand Down Expand Up @@ -128,9 +125,10 @@ class GuiExport QuantitySpinBox : public QAbstractSpinBox, public ExpressionBind
QSize minimumSizeHint() const;
bool event(QEvent *event);

void setExpression(std::shared_ptr<App::Expression> expr);
void setNumberExpression(App::NumberExpression*);
void bind(const App::ObjectIdentifier &_path);
bool apply(const std::string &propName);
using ExpressionSpinBox::apply;

public Q_SLOTS:
/// Sets the field with a quantity
Expand All @@ -140,14 +138,10 @@ public Q_SLOTS:

protected Q_SLOTS:
void userInput(const QString & text);
void openFormulaDialog();
void finishFormulaDialog();
void handlePendingEmit();

//get notified on expression change
virtual void onChange();

protected:
virtual void openFormulaDialog();
virtual StepEnabled stepEnabled() const;
virtual void showEvent(QShowEvent * event);
virtual void hideEvent(QHideEvent * event);
Expand Down
2 changes: 2 additions & 0 deletions src/Gui/SpinBox.cpp
Expand Up @@ -89,6 +89,8 @@ void ExpressionSpinBox::setExpression(std::shared_ptr<Expression> expr)

void ExpressionSpinBox::onChange()
{
Q_ASSERT(isBound());

if (getExpression()) {
std::unique_ptr<Expression> result(getExpression()->eval());
NumberExpression * value = freecad_dynamic_cast<NumberExpression>(result.get());
Expand Down
2 changes: 1 addition & 1 deletion src/Gui/SpinBox.h
Expand Up @@ -51,7 +51,7 @@ class GuiExport ExpressionSpinBox : public ExpressionBinding
void resizeWidget();

bool handleKeyEvent(const QString&);
void openFormulaDialog();
virtual void openFormulaDialog();

void drawControl(QStyleOptionSpinBox&);

Expand Down

0 comments on commit 5d49bf7

Please sign in to comment.