1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2024-11-28 05:34:47 +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;
_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())),

View File

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

View File

@ -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<KV>::const_iterator i;
QFontMetrics fm(_font);
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->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<KV>::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<KV>::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);

View File

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

View File

@ -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);

View File

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

View File

@ -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);

View File

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