From 650eb1c30251f560b0320c95cd47a9375f0525ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20T=C5=AFma?= Date: Fri, 18 May 2018 01:38:33 +0200 Subject: [PATCH] Optimization/code cleanup --- src/GUI/axisitem.cpp | 33 +++++++++++++-------------------- src/GUI/axisitem.h | 1 + src/GUI/infoitem.cpp | 30 ++++++++++++++---------------- src/GUI/infoitem.h | 1 + src/GUI/scaleitem.cpp | 18 +++++++----------- src/GUI/scaleitem.h | 3 +-- src/GUI/sliderinfoitem.cpp | 15 ++++++--------- src/GUI/sliderinfoitem.h | 1 + 8 files changed, 44 insertions(+), 58 deletions(-) diff --git a/src/GUI/axisitem.cpp b/src/GUI/axisitem.cpp index 5e927623..98c5819d 100644 --- a/src/GUI/axisitem.cpp +++ b/src/GUI/axisitem.cpp @@ -36,6 +36,9 @@ AxisItem::AxisItem(Type type, QGraphicsItem *parent) : QGraphicsItem(parent) _type = type; _size = 0; + _font.setPixelSize(FONT_SIZE); + _font.setFamily(FONT_FAMILY); + #ifndef Q_OS_MAC setCacheMode(QGraphicsItem::DeviceCoordinateCache); #endif // Q_OS_MAC @@ -67,10 +70,7 @@ void AxisItem::setLabel(const QString& label) void AxisItem::updateBoundingRect() { - QFont font; - font.setPixelSize(FONT_SIZE); - font.setFamily(FONT_FAMILY); - QFontMetrics fm(font); + QFontMetrics fm(_font); QRect ss, es, ls; struct Label l; @@ -107,29 +107,22 @@ void AxisItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, { Q_UNUSED(option); Q_UNUSED(widget); - QFont font; - font.setPixelSize(FONT_SIZE); - font.setFamily(FONT_FAMILY); - QFontMetrics fm(font); - QRect ts, ls; - struct Label l; + QFontMetrics fm(_font); + QRect ls(fm.tightBoundingRect(_label)); qreal range = _range.size(); - qreal val; - QPen pen = QPen(Qt::black, AXIS_WIDTH); + QRect ts; painter->setRenderHint(QPainter::Antialiasing, false); - painter->setFont(font); - painter->setPen(pen); - - ls = fm.tightBoundingRect(_label); + painter->setFont(_font); + painter->setPen(QPen(Qt::black, AXIS_WIDTH)); if (_type == X) { painter->drawLine(0, 0, _size, 0); - l = label(_range.min(), _range.max(), XTICKS); + Label l = label(_range.min(), _range.max(), XTICKS); for (int i = 0; i < ((l.max - l.min) / l.d) + 1; i++) { - val = l.min + i * l.d; + qreal val = l.min + i * l.d; QString str = QString::number(val); painter->drawLine((_size/range) * (val - _range.min()), TICK/2, @@ -144,10 +137,10 @@ void AxisItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, } else { painter->drawLine(0, 0, 0, -_size); - l = label(_range.min(), _range.max(), YTICKS); + Label l = label(_range.min(), _range.max(), YTICKS); int mtw = 0; for (int i = 0; i < ((l.max - l.min) / l.d) + 1; i++) { - val = l.min + i * l.d; + qreal val = l.min + i * l.d; QString str = QString::number(val); painter->drawLine(TICK/2, -((_size/range) * (val - _range.min())), diff --git a/src/GUI/axisitem.h b/src/GUI/axisitem.h index e302cb0e..c1f94bec 100644 --- a/src/GUI/axisitem.h +++ b/src/GUI/axisitem.h @@ -30,6 +30,7 @@ private: qreal _size; QString _label; QRectF _boundingRect; + QFont _font; }; #endif // AXISITEM_H diff --git a/src/GUI/infoitem.cpp b/src/GUI/infoitem.cpp index 45a3410c..0a204bea 100644 --- a/src/GUI/infoitem.cpp +++ b/src/GUI/infoitem.cpp @@ -7,6 +7,9 @@ InfoItem::InfoItem(QGraphicsItem *parent) : QGraphicsItem(parent) { + _font.setPixelSize(FONT_SIZE); + _font.setFamily(FONT_FAMILY); + #ifndef Q_OS_MAC setCacheMode(QGraphicsItem::DeviceCoordinateCache); #endif // Q_OS_MAC @@ -14,16 +17,14 @@ InfoItem::InfoItem(QGraphicsItem *parent) : QGraphicsItem(parent) void InfoItem::updateBoundingRect() { - QFont font; - font.setPixelSize(FONT_SIZE); - font.setFamily(FONT_FAMILY); - QFontMetrics fm(font); - QList::const_iterator i; + QFontMetrics fm(_font); qreal width = 0; - for (i = _list.constBegin(); i != _list.constEnd(); i++) { + for (QList::const_iterator i = _list.constBegin(); + i != _list.constEnd(); i++) { width += fm.width(i->key + ": "); - width += fm.width(i->value) + ((i == _list.constEnd() - 1) ? 0 : PADDING); + width += fm.width(i->value) + ((i == _list.constEnd() - 1) + ? 0 : PADDING); } _boundingRect = QRectF(0, 0, width, _list.isEmpty() ? 0 : fm.height()); @@ -34,22 +35,19 @@ void InfoItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, { Q_UNUSED(option); Q_UNUSED(widget); - QFont font; - font.setPixelSize(FONT_SIZE); - font.setFamily(FONT_FAMILY); - painter->setFont(font); - QFontMetrics fm(font); - QList::const_iterator i; + QFontMetrics fm(_font); int width = 0; - + painter->setFont(_font); painter->setRenderHint(QPainter::Antialiasing, false); - for (i = _list.constBegin(); i != _list.constEnd(); i++) { + for (QList::const_iterator i = _list.constBegin(); + i != _list.constEnd(); i++) { painter->drawText(width, fm.height() - fm.descent(), i->key + ": "); width += fm.width(i->key + ": "); painter->drawText(width, fm.height() - fm.descent(), i->value); - width += fm.width(i->value) + ((i == _list.constEnd() - 1) ? 0 : PADDING); + width += fm.width(i->value) + ((i == _list.constEnd() - 1) + ? 0 : PADDING); if (i != _list.constEnd() - 1) { painter->save(); painter->setPen(Qt::gray); diff --git a/src/GUI/infoitem.h b/src/GUI/infoitem.h index f99ad9b7..48a9662a 100644 --- a/src/GUI/infoitem.h +++ b/src/GUI/infoitem.h @@ -33,6 +33,7 @@ private: QList _list; QRectF _boundingRect; + QFont _font; }; #endif // INFOITEM_H diff --git a/src/GUI/scaleitem.cpp b/src/GUI/scaleitem.cpp index 55f17f27..11031b6f 100644 --- a/src/GUI/scaleitem.cpp +++ b/src/GUI/scaleitem.cpp @@ -18,6 +18,9 @@ ScaleItem::ScaleItem(QGraphicsItem *parent) : QGraphicsItem(parent) _res = 1.0; _digitalZoom = 0; + _font.setPixelSize(FONT_SIZE); + _font.setFamily(FONT_FAMILY); + #ifndef Q_OS_MAC setCacheMode(QGraphicsItem::DeviceCoordinateCache); #endif // Q_OS_MAC @@ -25,10 +28,7 @@ ScaleItem::ScaleItem(QGraphicsItem *parent) : QGraphicsItem(parent) void ScaleItem::updateBoundingRect() { - QFont font; - font.setPixelSize(FONT_SIZE); - font.setFamily(FONT_FAMILY); - QFontMetrics fm(font); + QFontMetrics fm(_font); QRect ss, es, us; ss = fm.tightBoundingRect(QString::number(0)); @@ -45,17 +45,13 @@ void ScaleItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, { Q_UNUSED(option); Q_UNUSED(widget); - QFont font; - font.setPixelSize(FONT_SIZE); - font.setFamily(FONT_FAMILY); - QFontMetrics fm(font); + QFontMetrics fm(_font); QRect br; - QPen pen = QPen(Qt::black, BORDER_WIDTH); painter->setRenderHint(QPainter::Antialiasing, false); - painter->setFont(font); - painter->setPen(pen); + painter->setFont(_font); + painter->setPen(QPen(Qt::black, BORDER_WIDTH)); for (int i = 0; i <= SEGMENTS; i++) { QString label = QString::number(_length * i); diff --git a/src/GUI/scaleitem.h b/src/GUI/scaleitem.h index 405bc020..3c3ab11d 100644 --- a/src/GUI/scaleitem.h +++ b/src/GUI/scaleitem.h @@ -27,10 +27,9 @@ private: qreal _length; Units _units; bool _scale; - qreal _digitalZoom; - QRectF _boundingRect; + QFont _font; }; #endif // SCALEITEM_H diff --git a/src/GUI/sliderinfoitem.cpp b/src/GUI/sliderinfoitem.cpp index 833d089c..e0c770ad 100644 --- a/src/GUI/sliderinfoitem.cpp +++ b/src/GUI/sliderinfoitem.cpp @@ -9,14 +9,14 @@ SliderInfoItem::SliderInfoItem(QGraphicsItem *parent) : QGraphicsItem(parent) { _side = Right; _color = Qt::red; + + _font.setPixelSize(FONT_SIZE); + _font.setFamily(FONT_FAMILY); } void SliderInfoItem::updateBoundingRect() { - QFont font; - font.setPixelSize(FONT_SIZE); - font.setFamily(FONT_FAMILY); - QFontMetrics fm(font); + QFontMetrics fm(_font); qreal width = qMax(fm.width(_x), fm.width(_y)); qreal height = 2 * fm.height() - 2*fm.descent(); @@ -31,10 +31,7 @@ void SliderInfoItem::paint(QPainter *painter, const QStyleOptionGraphicsItem { Q_UNUSED(option); Q_UNUSED(widget); - QFont font; - font.setPixelSize(FONT_SIZE); - font.setFamily(FONT_FAMILY); - QFontMetrics fm(font); + QFontMetrics fm(_font); QRectF rx, ry; @@ -57,7 +54,7 @@ void SliderInfoItem::paint(QPainter *painter, const QStyleOptionGraphicsItem painter->drawRect(rx); painter->setBrush(Qt::NoBrush); - painter->setFont(font); + painter->setFont(_font); painter->setRenderHint(QPainter::Antialiasing, false); painter->setPen(_color); diff --git a/src/GUI/sliderinfoitem.h b/src/GUI/sliderinfoitem.h index ddcb32fe..b9595b05 100644 --- a/src/GUI/sliderinfoitem.h +++ b/src/GUI/sliderinfoitem.h @@ -25,6 +25,7 @@ private: QString _x, _y; QRectF _boundingRect; QColor _color; + QFont _font; }; #endif // SLIDERINFOITEM_H