From f33ce91c8fba280ea4bd90b9dbbeef8368416738 Mon Sep 17 00:00:00 2001 From: Eivind Kvedalen Date: Mon, 28 Sep 2015 22:10:04 +0200 Subject: [PATCH] QuantitySpinBox: Moved getIcon() and some data members to ExpressionBinding class. Conflicts: src/Gui/QuantitySpinBox.cpp --- src/Gui/ExpressionBinding.cpp | 20 +++++++++ src/Gui/ExpressionBinding.h | 8 +++- src/Gui/QuantitySpinBox.cpp | 83 ++++++++++++----------------------- src/Gui/QuantitySpinBox.h | 1 - 4 files changed, 56 insertions(+), 56 deletions(-) diff --git a/src/Gui/ExpressionBinding.cpp b/src/Gui/ExpressionBinding.cpp index 7c7d54f32b..c6dd086bef 100644 --- a/src/Gui/ExpressionBinding.cpp +++ b/src/Gui/ExpressionBinding.cpp @@ -22,6 +22,8 @@ #include "PreCompiled.h" #ifndef _PreComp_ +# include +# include #endif #include "ExpressionBinding.h" #include "Command.h" @@ -35,6 +37,8 @@ using namespace Gui; using namespace App; ExpressionBinding::ExpressionBinding() + : iconLabel(0) + , iconHeight(-1) { } @@ -104,6 +108,22 @@ std::string ExpressionBinding::getEscapedExpressionString() const return Base::Tools::escapedUnicodeFromUtf8(getExpressionString().c_str()); } +QPixmap ExpressionBinding::getIcon(const char* name, const QSize& size) const +{ + QString key = QString::fromAscii("%1_%2x%3") + .arg(QString::fromAscii(name)) + .arg(size.width()) + .arg(size.height()); + QPixmap icon; + if (QPixmapCache::find(key, icon)) + return icon; + + icon = BitmapFactory().pixmapFromSvg(name, size); + if (!icon.isNull()) + QPixmapCache::insert(key, icon); + return icon; +} + bool ExpressionBinding::apply(const std::string & propName) { if (hasExpression()) { diff --git a/src/Gui/ExpressionBinding.h b/src/Gui/ExpressionBinding.h index 1a70cf8815..7e66e403bf 100644 --- a/src/Gui/ExpressionBinding.h +++ b/src/Gui/ExpressionBinding.h @@ -26,6 +26,7 @@ #include #include #include +#include namespace App { class Expression; @@ -46,6 +47,8 @@ public: virtual bool apply(); bool hasExpression() const; + QPixmap getIcon(const char *name, const QSize &size) const; + protected: const App::ObjectIdentifier & getPath() const { return path; } boost::shared_ptr getExpression() const; @@ -55,7 +58,10 @@ protected: private: App::ObjectIdentifier path; - //boost::shared_ptr expression; +protected: + QLabel* iconLabel; + QPalette defaultPalette; + int iconHeight; }; } diff --git a/src/Gui/QuantitySpinBox.cpp b/src/Gui/QuantitySpinBox.cpp index 54b818ece9..0eac610080 100644 --- a/src/Gui/QuantitySpinBox.cpp +++ b/src/Gui/QuantitySpinBox.cpp @@ -44,6 +44,7 @@ #include #include #include +#include using namespace Gui; using namespace App; @@ -228,9 +229,6 @@ end: double maximum; double minimum; double singleStep; - QPalette defaultPalette; - QLabel* iconLabel; - int iconHeight; }; } @@ -243,56 +241,37 @@ QuantitySpinBox::QuantitySpinBox(QWidget *parent) this->setContextMenuPolicy(Qt::DefaultContextMenu); QObject::connect(lineEdit(), SIGNAL(textChanged(QString)), this, SLOT(userInput(QString))); - d_ptr->defaultPalette = lineEdit()->palette(); + + defaultPalette = lineEdit()->palette(); /* Icon for f(x) */ QFontMetrics fm(lineEdit()->font()); int frameWidth = style()->pixelMetric(QStyle::PM_SpinBoxFrameWidth); - d_ptr->iconHeight = fm.height() - frameWidth; - d_ptr->iconLabel = new ExpressionLabel(lineEdit()); - d_ptr->iconLabel->setCursor(Qt::ArrowCursor); - QPixmap pixmap = getIcon(":/icons/bound-expression-unset.svg", QSize(d_ptr->iconHeight, d_ptr->iconHeight)); - d_ptr->iconLabel->setPixmap(pixmap); - d_ptr->iconLabel->setStyleSheet(QString::fromAscii("QLabel { border: none; padding: 0px; padding-top: %2px; width: %1px; height: %1px }").arg(d_ptr->iconHeight).arg(frameWidth/2)); - d_ptr->iconLabel->hide(); - lineEdit()->setStyleSheet(QString::fromAscii("QLineEdit { padding-right: %1px } ").arg(d_ptr->iconHeight+frameWidth)); + 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::fromAscii("QLabel { border: none; padding: 0px; padding-top: %2px; width: %1px; height: %1px }").arg(iconHeight).arg(frameWidth/2)); + iconLabel->hide(); + lineEdit()->setStyleSheet(QString::fromAscii("QLineEdit { padding-right: %1px } ").arg(iconHeight+frameWidth)); - QObject::connect(d_ptr->iconLabel, SIGNAL(clicked()), this, SLOT(openFormulaDialog())); + QObject::connect(iconLabel, SIGNAL(clicked()), this, SLOT(openFormulaDialog())); } QuantitySpinBox::~QuantitySpinBox() { } -QPixmap QuantitySpinBox::getIcon(const char* name, const QSize& size) const -{ - QString key = QString::fromAscii("%1_%2x%3") - .arg(QString::fromAscii(name)) - .arg(size.width()) - .arg(size.height()); - QPixmap icon; - if (QPixmapCache::find(key, icon)) - return icon; - - icon = BitmapFactory().pixmapFromSvg(name, size); - if (!icon.isNull()) - QPixmapCache::insert(key, icon); - return icon; -} - void QuantitySpinBox::bind(const App::ObjectIdentifier &_path) { - Q_D(QuantitySpinBox); - ExpressionBinding::bind(_path); - d->iconLabel->show(); + iconLabel->show(); } void Gui::QuantitySpinBox::setExpression(boost::shared_ptr expr) { - Q_D(QuantitySpinBox); - Q_ASSERT(isBound()); try { @@ -303,10 +282,9 @@ void Gui::QuantitySpinBox::setExpression(boost::shared_ptr expr) NumberExpression * value = freecad_dynamic_cast(result.get()); if (value) { - lineEdit()->setText(value->getQuantity().getUserString()); + updateText(value->getQuantity()); setReadOnly(true); - QPixmap pixmap = getIcon(":/icons/bound-expression.svg", QSize(d_ptr->iconHeight, d_ptr->iconHeight)); - d_ptr->iconLabel->setPixmap(pixmap); + iconLabel->setPixmap(getIcon(":/icons/bound-expression.svg", QSize(iconHeight, iconHeight))); QPalette p(lineEdit()->palette()); p.setColor(QPalette::Text, Qt::lightGray); @@ -316,21 +294,20 @@ void Gui::QuantitySpinBox::setExpression(boost::shared_ptr expr) } else { setReadOnly(false); - QPixmap pixmap = getIcon(":/icons/bound-expression-unset.svg", QSize(d_ptr->iconHeight, d_ptr->iconHeight)); - d_ptr->iconLabel->setPixmap(pixmap); + iconLabel->setPixmap(getIcon(":/icons/bound-expression-unset.svg", QSize(iconHeight, iconHeight))); QPalette p(lineEdit()->palette()); - p.setColor(QPalette::Active, QPalette::Text, d->defaultPalette.color(QPalette::Text)); + p.setColor(QPalette::Active, QPalette::Text, defaultPalette.color(QPalette::Text)); lineEdit()->setPalette(p); } - d->iconLabel->setToolTip(QString()); + iconLabel->setToolTip(QString()); } catch (const Base::Exception & e) { setReadOnly(true); QPalette p(lineEdit()->palette()); p.setColor(QPalette::Active, QPalette::Text, Qt::red); lineEdit()->setPalette(p); - d->iconLabel->setToolTip(QString::fromAscii(e.what())); + iconLabel->setToolTip(QString::fromAscii(e.what())); } } @@ -351,14 +328,12 @@ bool QuantitySpinBox::apply() void QuantitySpinBox::resizeEvent(QResizeEvent * event) { - Q_D(const QuantitySpinBox); - QAbstractSpinBox::resizeEvent(event); int frameWidth = style()->pixelMetric(QStyle::PM_SpinBoxFrameWidth); - QSize sz = d->iconLabel->sizeHint(); - d->iconLabel->move(lineEdit()->rect().right() - frameWidth - sz.width(), 0); + QSize sz = iconLabel->sizeHint(); + iconLabel->move(lineEdit()->rect().right() - frameWidth - sz.width(), 0); try { if (isBound() && getExpression()) { @@ -367,8 +342,8 @@ void QuantitySpinBox::resizeEvent(QResizeEvent * event) if (value) { setReadOnly(true); - QPixmap pixmap = getIcon(":/icons/bound-expression.svg", QSize(d_ptr->iconHeight, d_ptr->iconHeight)); - d->iconLabel->setPixmap(pixmap); + QPixmap pixmap = getIcon(":/icons/bound-expression.svg", QSize(iconHeight, iconHeight)); + iconLabel->setPixmap(pixmap); QPalette p(lineEdit()->palette()); p.setColor(QPalette::Text, Qt::lightGray); @@ -378,22 +353,22 @@ void QuantitySpinBox::resizeEvent(QResizeEvent * event) } else { setReadOnly(false); - QPixmap pixmap = getIcon(":/icons/bound-expression-unset.svg", QSize(d_ptr->iconHeight, d_ptr->iconHeight)); - d->iconLabel->setPixmap(pixmap); - + QPixmap pixmap = getIcon(":/icons/bound-expression-unset.svg", QSize(iconHeight, iconHeight)); + iconLabel->setPixmap(pixmap); + QPalette p(lineEdit()->palette()); - p.setColor(QPalette::Active, QPalette::Text, d->defaultPalette.color(QPalette::Text)); + p.setColor(QPalette::Active, QPalette::Text, defaultPalette.color(QPalette::Text)); lineEdit()->setPalette(p); } - d->iconLabel->setToolTip(QString()); + iconLabel->setToolTip(QString()); } catch (const Base::Exception & e) { setReadOnly(true); QPalette p(lineEdit()->palette()); p.setColor(QPalette::Active, QPalette::Text, Qt::red); lineEdit()->setPalette(p); - d->iconLabel->setToolTip(QString::fromAscii(e.what())); + iconLabel->setToolTip(QString::fromAscii(e.what())); } } diff --git a/src/Gui/QuantitySpinBox.h b/src/Gui/QuantitySpinBox.h index 8b88cee693..2a6d7d4112 100644 --- a/src/Gui/QuantitySpinBox.h +++ b/src/Gui/QuantitySpinBox.h @@ -124,7 +124,6 @@ protected: private: - QPixmap getIcon(const char *name, const QSize &size) const; void updateText(const Base::Quantity&); Q_SIGNALS: