1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2024-11-28 13:41:16 +01:00

Optimization/code cleanup

This commit is contained in:
Martin Tůma 2018-05-18 01:38:33 +02:00
parent 33919c501c
commit 650eb1c302
8 changed files with 44 additions and 58 deletions

View File

@ -36,6 +36,9 @@ AxisItem::AxisItem(Type type, QGraphicsItem *parent) : QGraphicsItem(parent)
_type = type; _type = type;
_size = 0; _size = 0;
_font.setPixelSize(FONT_SIZE);
_font.setFamily(FONT_FAMILY);
#ifndef Q_OS_MAC #ifndef Q_OS_MAC
setCacheMode(QGraphicsItem::DeviceCoordinateCache); setCacheMode(QGraphicsItem::DeviceCoordinateCache);
#endif // Q_OS_MAC #endif // Q_OS_MAC
@ -67,10 +70,7 @@ void AxisItem::setLabel(const QString& label)
void AxisItem::updateBoundingRect() void AxisItem::updateBoundingRect()
{ {
QFont font; QFontMetrics fm(_font);
font.setPixelSize(FONT_SIZE);
font.setFamily(FONT_FAMILY);
QFontMetrics fm(font);
QRect ss, es, ls; QRect ss, es, ls;
struct Label l; struct Label l;
@ -107,29 +107,22 @@ void AxisItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
{ {
Q_UNUSED(option); Q_UNUSED(option);
Q_UNUSED(widget); Q_UNUSED(widget);
QFont font; QFontMetrics fm(_font);
font.setPixelSize(FONT_SIZE); QRect ls(fm.tightBoundingRect(_label));
font.setFamily(FONT_FAMILY);
QFontMetrics fm(font);
QRect ts, ls;
struct Label l;
qreal range = _range.size(); qreal range = _range.size();
qreal val; QRect ts;
QPen pen = QPen(Qt::black, AXIS_WIDTH);
painter->setRenderHint(QPainter::Antialiasing, false); painter->setRenderHint(QPainter::Antialiasing, false);
painter->setFont(font); painter->setFont(_font);
painter->setPen(pen); painter->setPen(QPen(Qt::black, AXIS_WIDTH));
ls = fm.tightBoundingRect(_label);
if (_type == X) { if (_type == X) {
painter->drawLine(0, 0, _size, 0); 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++) { 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); QString str = QString::number(val);
painter->drawLine((_size/range) * (val - _range.min()), TICK/2, painter->drawLine((_size/range) * (val - _range.min()), TICK/2,
@ -144,10 +137,10 @@ void AxisItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
} else { } else {
painter->drawLine(0, 0, 0, -_size); painter->drawLine(0, 0, 0, -_size);
l = label(_range.min(), _range.max(), YTICKS); Label l = label(_range.min(), _range.max(), YTICKS);
int mtw = 0; int mtw = 0;
for (int i = 0; i < ((l.max - l.min) / l.d) + 1; i++) { 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); QString str = QString::number(val);
painter->drawLine(TICK/2, -((_size/range) * (val - _range.min())), painter->drawLine(TICK/2, -((_size/range) * (val - _range.min())),

View File

@ -30,6 +30,7 @@ private:
qreal _size; qreal _size;
QString _label; QString _label;
QRectF _boundingRect; QRectF _boundingRect;
QFont _font;
}; };
#endif // AXISITEM_H #endif // AXISITEM_H

View File

@ -7,6 +7,9 @@
InfoItem::InfoItem(QGraphicsItem *parent) : QGraphicsItem(parent) InfoItem::InfoItem(QGraphicsItem *parent) : QGraphicsItem(parent)
{ {
_font.setPixelSize(FONT_SIZE);
_font.setFamily(FONT_FAMILY);
#ifndef Q_OS_MAC #ifndef Q_OS_MAC
setCacheMode(QGraphicsItem::DeviceCoordinateCache); setCacheMode(QGraphicsItem::DeviceCoordinateCache);
#endif // Q_OS_MAC #endif // Q_OS_MAC
@ -14,16 +17,14 @@ InfoItem::InfoItem(QGraphicsItem *parent) : QGraphicsItem(parent)
void InfoItem::updateBoundingRect() void InfoItem::updateBoundingRect()
{ {
QFont font; QFontMetrics fm(_font);
font.setPixelSize(FONT_SIZE);
font.setFamily(FONT_FAMILY);
QFontMetrics fm(font);
QList<KV>::const_iterator i;
qreal width = 0; qreal width = 0;
for (i = _list.constBegin(); i != _list.constEnd(); i++) { for (QList<KV>::const_iterator i = _list.constBegin();
i != _list.constEnd(); i++) {
width += fm.width(i->key + ": "); 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()); _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(option);
Q_UNUSED(widget); Q_UNUSED(widget);
QFont font; QFontMetrics fm(_font);
font.setPixelSize(FONT_SIZE);
font.setFamily(FONT_FAMILY);
painter->setFont(font);
QFontMetrics fm(font);
QList<KV>::const_iterator i;
int width = 0; int width = 0;
painter->setFont(_font);
painter->setRenderHint(QPainter::Antialiasing, false); painter->setRenderHint(QPainter::Antialiasing, false);
for (i = _list.constBegin(); i != _list.constEnd(); i++) { for (QList<KV>::const_iterator i = _list.constBegin();
i != _list.constEnd(); i++) {
painter->drawText(width, fm.height() - fm.descent(), i->key + ": "); painter->drawText(width, fm.height() - fm.descent(), i->key + ": ");
width += fm.width(i->key + ": "); width += fm.width(i->key + ": ");
painter->drawText(width, fm.height() - fm.descent(), i->value); 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) { if (i != _list.constEnd() - 1) {
painter->save(); painter->save();
painter->setPen(Qt::gray); painter->setPen(Qt::gray);

View File

@ -33,6 +33,7 @@ private:
QList<KV> _list; QList<KV> _list;
QRectF _boundingRect; QRectF _boundingRect;
QFont _font;
}; };
#endif // INFOITEM_H #endif // INFOITEM_H

View File

@ -18,6 +18,9 @@ ScaleItem::ScaleItem(QGraphicsItem *parent) : QGraphicsItem(parent)
_res = 1.0; _res = 1.0;
_digitalZoom = 0; _digitalZoom = 0;
_font.setPixelSize(FONT_SIZE);
_font.setFamily(FONT_FAMILY);
#ifndef Q_OS_MAC #ifndef Q_OS_MAC
setCacheMode(QGraphicsItem::DeviceCoordinateCache); setCacheMode(QGraphicsItem::DeviceCoordinateCache);
#endif // Q_OS_MAC #endif // Q_OS_MAC
@ -25,10 +28,7 @@ ScaleItem::ScaleItem(QGraphicsItem *parent) : QGraphicsItem(parent)
void ScaleItem::updateBoundingRect() void ScaleItem::updateBoundingRect()
{ {
QFont font; QFontMetrics fm(_font);
font.setPixelSize(FONT_SIZE);
font.setFamily(FONT_FAMILY);
QFontMetrics fm(font);
QRect ss, es, us; QRect ss, es, us;
ss = fm.tightBoundingRect(QString::number(0)); ss = fm.tightBoundingRect(QString::number(0));
@ -45,17 +45,13 @@ void ScaleItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
{ {
Q_UNUSED(option); Q_UNUSED(option);
Q_UNUSED(widget); Q_UNUSED(widget);
QFont font; QFontMetrics fm(_font);
font.setPixelSize(FONT_SIZE);
font.setFamily(FONT_FAMILY);
QFontMetrics fm(font);
QRect br; QRect br;
QPen pen = QPen(Qt::black, BORDER_WIDTH);
painter->setRenderHint(QPainter::Antialiasing, false); painter->setRenderHint(QPainter::Antialiasing, false);
painter->setFont(font); painter->setFont(_font);
painter->setPen(pen); painter->setPen(QPen(Qt::black, BORDER_WIDTH));
for (int i = 0; i <= SEGMENTS; i++) { for (int i = 0; i <= SEGMENTS; i++) {
QString label = QString::number(_length * i); QString label = QString::number(_length * i);

View File

@ -27,10 +27,9 @@ private:
qreal _length; qreal _length;
Units _units; Units _units;
bool _scale; bool _scale;
qreal _digitalZoom; qreal _digitalZoom;
QRectF _boundingRect; QRectF _boundingRect;
QFont _font;
}; };
#endif // SCALEITEM_H #endif // SCALEITEM_H

View File

@ -9,14 +9,14 @@ SliderInfoItem::SliderInfoItem(QGraphicsItem *parent) : QGraphicsItem(parent)
{ {
_side = Right; _side = Right;
_color = Qt::red; _color = Qt::red;
_font.setPixelSize(FONT_SIZE);
_font.setFamily(FONT_FAMILY);
} }
void SliderInfoItem::updateBoundingRect() void SliderInfoItem::updateBoundingRect()
{ {
QFont font; QFontMetrics fm(_font);
font.setPixelSize(FONT_SIZE);
font.setFamily(FONT_FAMILY);
QFontMetrics fm(font);
qreal width = qMax(fm.width(_x), fm.width(_y)); qreal width = qMax(fm.width(_x), fm.width(_y));
qreal height = 2 * fm.height() - 2*fm.descent(); qreal height = 2 * fm.height() - 2*fm.descent();
@ -31,10 +31,7 @@ void SliderInfoItem::paint(QPainter *painter, const QStyleOptionGraphicsItem
{ {
Q_UNUSED(option); Q_UNUSED(option);
Q_UNUSED(widget); Q_UNUSED(widget);
QFont font; QFontMetrics fm(_font);
font.setPixelSize(FONT_SIZE);
font.setFamily(FONT_FAMILY);
QFontMetrics fm(font);
QRectF rx, ry; QRectF rx, ry;
@ -57,7 +54,7 @@ void SliderInfoItem::paint(QPainter *painter, const QStyleOptionGraphicsItem
painter->drawRect(rx); painter->drawRect(rx);
painter->setBrush(Qt::NoBrush); painter->setBrush(Qt::NoBrush);
painter->setFont(font); painter->setFont(_font);
painter->setRenderHint(QPainter::Antialiasing, false); painter->setRenderHint(QPainter::Antialiasing, false);
painter->setPen(_color); painter->setPen(_color);

View File

@ -25,6 +25,7 @@ private:
QString _x, _y; QString _x, _y;
QRectF _boundingRect; QRectF _boundingRect;
QColor _color; QColor _color;
QFont _font;
}; };
#endif // SLIDERINFOITEM_H #endif // SLIDERINFOITEM_H