Skip to content

Commit

Permalink
Gui: Change label type to ExpressionLabel in ExpressionBinding
Browse files Browse the repository at this point in the history
* Add function ExpressionBinding::makeLabel() and use it in subclasses to reduce code duplication
  • Loading branch information
wwmayer committed Apr 2, 2021
1 parent 64fb634 commit 75bf85e
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 100 deletions.
21 changes: 21 additions & 0 deletions src/Gui/ExpressionBinding.cpp
Expand Up @@ -22,9 +22,12 @@

#include "PreCompiled.h"
#ifndef _PreComp_
# include <QLineEdit>
# include <QPixmapCache>
# include <QStyle>
#endif
#include "ExpressionBinding.h"
#include "QuantitySpinBox_p.h"
#include "BitmapFactory.h"
#include "Command.h"
#include <App/Expression.h>
Expand Down Expand Up @@ -266,3 +269,21 @@ void ExpressionBinding::objectDeleted(const App::DocumentObject& obj)
unbind();
}
}

void ExpressionBinding::makeLabel(QLineEdit* le)
{
defaultPalette = le->palette();

/* Icon for f(x) */
QFontMetrics fm(le->font());
int frameWidth = le->style()->pixelMetric(QStyle::PM_SpinBoxFrameWidth);
iconHeight = fm.height() - frameWidth;
iconLabel = new ExpressionLabel(le);
iconLabel->setCursor(Qt::ArrowCursor);
QPixmap pixmap = getIcon(":/icons/bound-expression-unset.svg", QSize(iconHeight, iconHeight));
iconLabel->setPixmap(pixmap);
iconLabel->setStyleSheet(QString::fromLatin1("QLabel { border: none; padding: 0px; padding-top: %2px; width: %1px; height: %1px }").arg(iconHeight).arg(frameWidth/2));
iconLabel->hide();
iconLabel->setExpressionText(QString());
le->setStyleSheet(QString::fromLatin1("QLineEdit { padding-right: %1px } ").arg(iconHeight+frameWidth));
}
7 changes: 6 additions & 1 deletion src/Gui/ExpressionBinding.h
Expand Up @@ -33,6 +33,9 @@ namespace App {
class Expression;
}

class ExpressionLabel;
class QLineEdit;

namespace Gui {

class GuiExport ExpressionBinding
Expand Down Expand Up @@ -66,12 +69,14 @@ class GuiExport ExpressionBinding
//gets called when the bound expression is changed, either by this binding or any external action
virtual void onChange() {}

void makeLabel(QLineEdit* parent);

private:
App::ObjectIdentifier path;
std::shared_ptr<App::Expression> lastExpression;

protected:
QLabel* iconLabel;
ExpressionLabel* iconLabel;
QPalette defaultPalette;
int iconHeight;

Expand Down
3 changes: 2 additions & 1 deletion src/Gui/InputField.cpp
Expand Up @@ -40,6 +40,7 @@
#include "Command.h"
#include "InputField.h"
#include "BitmapFactory.h"
#include "QuantitySpinBox_p.h"
#include "propertyeditor/PropertyItem.h"

using namespace Gui;
Expand Down Expand Up @@ -78,7 +79,7 @@ InputField::InputField(QWidget * parent)
{
setValidator(new InputValidator(this));
setFocusPolicy(Qt::WheelFocus);
iconLabel = new QLabel(this);
iconLabel = new ExpressionLabel(this);
iconLabel->setCursor(Qt::ArrowCursor);
QPixmap pixmap = getValidationIcon(":/icons/button_valid.svg", QSize(sizeHint().height(),sizeHint().height()));
iconLabel->setPixmap(pixmap);
Expand Down
22 changes: 5 additions & 17 deletions src/Gui/QuantitySpinBox.cpp
Expand Up @@ -300,20 +300,8 @@ QuantitySpinBox::QuantitySpinBox(QWidget *parent)
QObject::connect(this, SIGNAL(editingFinished()),
this, SLOT(handlePendingEmit()));

defaultPalette = lineEdit()->palette();
makeLabel(lineEdit());

/* Icon for f(x) */
QFontMetrics fm(lineEdit()->font());
int frameWidth = style()->pixelMetric(QStyle::PM_SpinBoxFrameWidth);
iconHeight = fm.height() - frameWidth;
iconLabel = new ExpressionLabel(lineEdit());
iconLabel->setCursor(Qt::ArrowCursor);
QPixmap pixmap = getIcon(":/icons/bound-expression-unset.svg", QSize(iconHeight, iconHeight));
iconLabel->setPixmap(pixmap);
iconLabel->setStyleSheet(QString::fromLatin1("QLabel { border: none; padding: 0px; padding-top: %2px; width: %1px; height: %1px }").arg(iconHeight).arg(frameWidth/2));
iconLabel->hide();
static_cast<ExpressionLabel *>(iconLabel)->setExpressionText(QString());
lineEdit()->setStyleSheet(QString::fromLatin1("QLineEdit { padding-right: %1px } ").arg(iconHeight+frameWidth));
// 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);
Expand Down Expand Up @@ -448,7 +436,7 @@ void Gui::QuantitySpinBox::onChange()
p.setColor(QPalette::Text, Qt::lightGray);
lineEdit()->setPalette(p);
}
static_cast<ExpressionLabel*>(iconLabel)->setExpressionText(Base::Tools::fromStdString(getExpression()->toString()));
iconLabel->setExpressionText(Base::Tools::fromStdString(getExpression()->toString()));
}
else {
setReadOnly(false);
Expand All @@ -457,7 +445,7 @@ void Gui::QuantitySpinBox::onChange()
QPalette p(lineEdit()->palette());
p.setColor(QPalette::Active, QPalette::Text, defaultPalette.color(QPalette::Text));
lineEdit()->setPalette(p);
static_cast<ExpressionLabel*>(iconLabel)->setExpressionText(QString());
iconLabel->setExpressionText(QString());
}
}

Expand Down Expand Up @@ -512,7 +500,7 @@ void QuantitySpinBox::resizeEvent(QResizeEvent * event)
p.setColor(QPalette::Text, Qt::lightGray);
lineEdit()->setPalette(p);
}
static_cast<ExpressionLabel*>(iconLabel)->setExpressionText(Base::Tools::fromStdString(getExpression()->toString()));
iconLabel->setExpressionText(Base::Tools::fromStdString(getExpression()->toString()));
}
else {
setReadOnly(false);
Expand All @@ -522,7 +510,7 @@ void QuantitySpinBox::resizeEvent(QResizeEvent * event)
QPalette p(lineEdit()->palette());
p.setColor(QPalette::Active, QPalette::Text, defaultPalette.color(QPalette::Text));
lineEdit()->setPalette(p);
static_cast<ExpressionLabel*>(iconLabel)->setExpressionText(QString());
iconLabel->setExpressionText(QString());
}
}
catch (const Base::Exception & e) {
Expand Down
1 change: 1 addition & 0 deletions src/Gui/QuantitySpinBox_p.h
Expand Up @@ -24,6 +24,7 @@
#define QUANTITYSPINBOX_P_H

#include <QLabel>
#include <QMouseEvent>

class ExpressionLabel : public QLabel
{
Expand Down
79 changes: 19 additions & 60 deletions src/Gui/SpinBox.cpp
Expand Up @@ -151,20 +151,7 @@ UIntSpinBox::UIntSpinBox (QWidget* parent)
setValue(0);
updateValidator();

defaultPalette = lineEdit()->palette();

/* Icon for f(x) */
QFontMetrics fm(lineEdit()->font());
int frameWidth = style()->pixelMetric(QStyle::PM_SpinBoxFrameWidth);
iconHeight = fm.height() - frameWidth;
iconLabel = new ExpressionLabel(lineEdit());
iconLabel->setCursor(Qt::ArrowCursor);
QPixmap pixmap = getIcon(":/icons/bound-expression-unset.svg", QSize(iconHeight, iconHeight));
iconLabel->setPixmap(pixmap);
iconLabel->setStyleSheet(QString::fromLatin1("QLabel { border: none; padding: 0px; padding-top: %2px; width: %1px; height: %1px }").arg(iconHeight).arg(frameWidth/2));
iconLabel->hide();
static_cast<ExpressionLabel*>(iconLabel)->setExpressionText(QString());
lineEdit()->setStyleSheet(QString::fromLatin1("QLineEdit { padding-right: %1px } ").arg(iconHeight+frameWidth));
makeLabel(lineEdit());

QObject::connect(iconLabel, SIGNAL(clicked()), this, SLOT(openFormulaDialog()));
}
Expand Down Expand Up @@ -296,15 +283,15 @@ void UIntSpinBox::onChange() {
p.setColor(QPalette::Text, Qt::lightGray);
lineEdit()->setPalette(p);
}
static_cast<ExpressionLabel*>(iconLabel)->setExpressionText(Base::Tools::fromStdString(getExpression()->toString()));
iconLabel->setExpressionText(Base::Tools::fromStdString(getExpression()->toString()));
}
else {
setReadOnly(false);
iconLabel->setPixmap(getIcon(":/icons/bound-expression-unset.svg", QSize(iconHeight, iconHeight)));
QPalette p(lineEdit()->palette());
p.setColor(QPalette::Active, QPalette::Text, defaultPalette.color(QPalette::Text));
lineEdit()->setPalette(p);
static_cast<ExpressionLabel*>(iconLabel)->setExpressionText(QString());
iconLabel->setExpressionText(QString());
}
}

Expand Down Expand Up @@ -347,7 +334,7 @@ void UIntSpinBox::resizeEvent(QResizeEvent * event)
p.setColor(QPalette::Text, Qt::lightGray);
lineEdit()->setPalette(p);
}
static_cast<ExpressionLabel*>(iconLabel)->setExpressionText(Base::Tools::fromStdString(getExpression()->toString()));
iconLabel->setExpressionText(Base::Tools::fromStdString(getExpression()->toString()));
}
else {
setReadOnly(false);
Expand All @@ -357,7 +344,7 @@ void UIntSpinBox::resizeEvent(QResizeEvent * event)
QPalette p(lineEdit()->palette());
p.setColor(QPalette::Active, QPalette::Text, defaultPalette.color(QPalette::Text));
lineEdit()->setPalette(p);
static_cast<ExpressionLabel*>(iconLabel)->setExpressionText(QString());
iconLabel->setExpressionText(QString());
}
}
catch (const Base::Exception & e) {
Expand Down Expand Up @@ -430,23 +417,9 @@ void UIntSpinBox::paintEvent(QPaintEvent*)

// ----------------------------------------------------------------------------

IntSpinBox::IntSpinBox(QWidget* parent) : QSpinBox(parent) {

defaultPalette = lineEdit()->palette();

/* Icon for f(x) */
QFontMetrics fm(lineEdit()->font());
int frameWidth = style()->pixelMetric(QStyle::PM_SpinBoxFrameWidth);
iconHeight = fm.height() - frameWidth;
iconLabel = new ExpressionLabel(lineEdit());
iconLabel->setCursor(Qt::ArrowCursor);
QPixmap pixmap = getIcon(":/icons/bound-expression-unset.svg", QSize(iconHeight, iconHeight));
iconLabel->setPixmap(pixmap);
iconLabel->setStyleSheet(QString::fromLatin1("QLabel { border: none; padding: 0px; padding-top: %2px; width: %1px; height: %1px }").arg(iconHeight).arg(frameWidth/2));
iconLabel->hide();
static_cast<ExpressionLabel*>(iconLabel)->setExpressionText(QString());
lineEdit()->setStyleSheet(QString::fromLatin1("QLineEdit { padding-right: %1px } ").arg(iconHeight+frameWidth));

IntSpinBox::IntSpinBox(QWidget* parent) : QSpinBox(parent)
{
makeLabel(lineEdit());
QObject::connect(iconLabel, SIGNAL(clicked()), this, SLOT(openFormulaDialog()));
}

Expand Down Expand Up @@ -506,15 +479,15 @@ void IntSpinBox::onChange() {
p.setColor(QPalette::Text, Qt::lightGray);
lineEdit()->setPalette(p);
}
static_cast<ExpressionLabel*>(iconLabel)->setExpressionText(Base::Tools::fromStdString(getExpression()->toString()));
iconLabel->setExpressionText(Base::Tools::fromStdString(getExpression()->toString()));
}
else {
setReadOnly(false);
iconLabel->setPixmap(getIcon(":/icons/bound-expression-unset.svg", QSize(iconHeight, iconHeight)));
QPalette p(lineEdit()->palette());
p.setColor(QPalette::Active, QPalette::Text, defaultPalette.color(QPalette::Text));
lineEdit()->setPalette(p);
static_cast<ExpressionLabel*>(iconLabel)->setExpressionText(QString());
iconLabel->setExpressionText(QString());
}
}

Expand All @@ -541,7 +514,7 @@ void IntSpinBox::resizeEvent(QResizeEvent * event)
p.setColor(QPalette::Text, Qt::lightGray);
lineEdit()->setPalette(p);
}
static_cast<ExpressionLabel*>(iconLabel)->setExpressionText(Base::Tools::fromStdString(getExpression()->toString()));
iconLabel->setExpressionText(Base::Tools::fromStdString(getExpression()->toString()));
}
else {
setReadOnly(false);
Expand All @@ -551,7 +524,7 @@ void IntSpinBox::resizeEvent(QResizeEvent * event)
QPalette p(lineEdit()->palette());
p.setColor(QPalette::Active, QPalette::Text, defaultPalette.color(QPalette::Text));
lineEdit()->setPalette(p);
static_cast<ExpressionLabel*>(iconLabel)->setExpressionText(QString());
iconLabel->setExpressionText(QString());
}
}
catch (const Base::Exception & e) {
Expand Down Expand Up @@ -624,23 +597,9 @@ void IntSpinBox::paintEvent(QPaintEvent*)

// ----------------------------------------------------------------------------

DoubleSpinBox::DoubleSpinBox(QWidget* parent): QDoubleSpinBox(parent) {

defaultPalette = lineEdit()->palette();

/* Icon for f(x) */
QFontMetrics fm(lineEdit()->font());
int frameWidth = style()->pixelMetric(QStyle::PM_SpinBoxFrameWidth);
iconHeight = fm.height() - frameWidth;
iconLabel = new ExpressionLabel(lineEdit());
iconLabel->setCursor(Qt::ArrowCursor);
QPixmap pixmap = getIcon(":/icons/bound-expression-unset.svg", QSize(iconHeight, iconHeight));
iconLabel->setPixmap(pixmap);
iconLabel->setStyleSheet(QString::fromLatin1("QLabel { border: none; padding: 0px; padding-top: %2px; width: %1px; height: %1px }").arg(iconHeight).arg(frameWidth/2));
iconLabel->hide();
static_cast<ExpressionLabel*>(iconLabel)->setExpressionText(QString());
lineEdit()->setStyleSheet(QString::fromLatin1("QLineEdit { padding-right: %1px } ").arg(iconHeight+frameWidth));

DoubleSpinBox::DoubleSpinBox(QWidget* parent): QDoubleSpinBox(parent)
{
makeLabel(lineEdit());
QObject::connect(iconLabel, SIGNAL(clicked()), this, SLOT(openFormulaDialog()));
}

Expand Down Expand Up @@ -700,15 +659,15 @@ void DoubleSpinBox::onChange() {
p.setColor(QPalette::Text, Qt::lightGray);
lineEdit()->setPalette(p);
}
static_cast<ExpressionLabel*>(iconLabel)->setExpressionText(Base::Tools::fromStdString(getExpression()->toString()));
iconLabel->setExpressionText(Base::Tools::fromStdString(getExpression()->toString()));
}
else {
setReadOnly(false);
iconLabel->setPixmap(getIcon(":/icons/bound-expression-unset.svg", QSize(iconHeight, iconHeight)));
QPalette p(lineEdit()->palette());
p.setColor(QPalette::Active, QPalette::Text, defaultPalette.color(QPalette::Text));
lineEdit()->setPalette(p);
static_cast<ExpressionLabel*>(iconLabel)->setExpressionText(QString());
iconLabel->setExpressionText(QString());
}
}

Expand All @@ -735,7 +694,7 @@ void DoubleSpinBox::resizeEvent(QResizeEvent * event)
p.setColor(QPalette::Text, Qt::lightGray);
lineEdit()->setPalette(p);
}
static_cast<ExpressionLabel*>(iconLabel)->setExpressionText(Base::Tools::fromStdString(getExpression()->toString()));
iconLabel->setExpressionText(Base::Tools::fromStdString(getExpression()->toString()));
}
else {
setReadOnly(false);
Expand All @@ -745,7 +704,7 @@ void DoubleSpinBox::resizeEvent(QResizeEvent * event)
QPalette p(lineEdit()->palette());
p.setColor(QPalette::Active, QPalette::Text, defaultPalette.color(QPalette::Text));
lineEdit()->setPalette(p);
static_cast<ExpressionLabel*>(iconLabel)->setExpressionText(QString());
iconLabel->setExpressionText(QString());
}
}
catch (const Base::Exception & e) {
Expand Down

0 comments on commit 75bf85e

Please sign in to comment.