StopDelegate: cache SVG in pixmaps, draw direction change indicator

This commit is contained in:
Filippo Gentile 2022-02-11 01:07:55 +01:00
parent 50a2f94233
commit 7fe4e130ea
2 changed files with 73 additions and 21 deletions

View File

@ -3,8 +3,7 @@
#include "stopeditor.h"
#include <QPainter>
#include <QDebug>
#include <QSvgRenderer>
#include "app/session.h"
#include "model/stopmodel.h"
@ -13,12 +12,13 @@
#include <QCoreApplication>
#include <QDebug>
StopDelegate::StopDelegate(sqlite3pp::database &db, QObject *parent) :
QStyledItemDelegate(parent),
mDb(db)
{
renderer = new QSvgRenderer(this);
loadIcon(QCoreApplication::instance()->applicationDirPath() + QStringLiteral("/icons/lightning.svg"));
refreshPixmaps();
}
void StopDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
@ -68,7 +68,7 @@ void StopDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
const double arrX = left + width * (isTransit ? 0.4 : 0.2);
const double depX = left + width * 0.6;
const double transitLineX = left + width * 0.1;
const double transitLineX = left + width * 0.2;
if(item.addHere == 0)
@ -93,6 +93,20 @@ void StopDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
item.departure.toString("HH:mm"));
}
//Check direction
if(item.fromGate.gateConnId && item.toGate.gateConnId
&& item.type != StopType::First && item.type != StopType::Last)
{
//Ignore First and Last stop (sometimes they have fake in/out gates set which might trigger this message)
//Both entry and exit path are set, check direction
if(item.fromGate.stationTrackSide == item.toGate.stationTrackSide)
{
//Train leaves station track from same side of entrance, draw reverse icon
QPointF iconTopLeft(left, bottom - PixHeight);
painter->drawPixmap(iconTopLeft, m_reverseDirPix);
}
}
if(item.type != StopType::Last && item.nextSegment.segmentId)
{
//Last has no next segment so do not draw lightning
@ -109,16 +123,15 @@ void StopDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
if(nextSegmentElectrified != prevSegmentElectrified)
{
//Railway type changed, draw a lightning
QSizeF s = QSizeF(renderer->defaultSize()).scaled(width, height * 0.4, Qt::KeepAspectRatio);
QRectF lightningRect(left, top + height * 0.1, s.width(), s.height());
renderer->render(painter, lightningRect);
QPointF lightningTopLeft(left, top + qMin(5.0, height * 0.1));
painter->drawPixmap(lightningTopLeft, m_lightningPix);
if(!nextSegmentElectrified)
{
//Next railway is not electrified, cross the lightning
//Then keep red pen to draw next segment name
painter->setPen(QPen(Qt::red, 4));
painter->drawLine(lightningRect.topLeft(), lightningRect.bottomRight());
painter->drawLine(lightningTopLeft, lightningTopLeft + QPointF(PixWidth, PixHeight));
}
}
@ -253,11 +266,6 @@ void StopDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewI
editor->setGeometry(option.rect);
}
void StopDelegate::loadIcon(const QString &fileName)
{
renderer->load(fileName);
}
void StopDelegate::onEditorSegmentChosen(StopEditor *editor)
{
if(editor->closeOnSegmentChosen())
@ -266,3 +274,45 @@ void StopDelegate::onEditorSegmentChosen(StopEditor *editor)
emit closeEditor(editor, StopDelegate::EditNextItem);
}
}
void StopDelegate::refreshPixmaps()
{
const QString iconPath = QCoreApplication::instance()->applicationDirPath() + QStringLiteral("/icons");
//Square pixmaps
m_lightningPix = QPixmap(PixWidth, PixHeight);
m_reverseDirPix = QPixmap(PixWidth, PixHeight);
m_lightningPix.fill(Qt::transparent);
m_reverseDirPix.fill(Qt::transparent);
QSvgRenderer mSvg;
QPainter painter;
QRectF iconRect;
//Cache Lightning
mSvg.load(iconPath + QStringLiteral("/lightning.svg"));
//Scale SVG to fit requested size
iconRect.setSize(mSvg.defaultSize().scaled(PixWidth, PixHeight, Qt::KeepAspectRatio));
//Center on pixmap
iconRect.moveTop((PixHeight - iconRect.height()) / 2);
iconRect.moveLeft((PixWidth - iconRect.width()) / 2);
painter.begin(&m_lightningPix);
mSvg.render(&painter, iconRect);
painter.end();
//Cache Reverse Direction
mSvg.load(iconPath + QStringLiteral("/reverse_direction.svg"));
//Scale SVG to fit requested size
iconRect.setSize(mSvg.defaultSize().scaled(PixWidth, PixHeight, Qt::KeepAspectRatio));
//Center on pixmap
iconRect.moveTop((PixHeight - iconRect.height()) / 2);
iconRect.moveLeft((PixWidth - iconRect.width()) / 2);
painter.begin(&m_reverseDirPix);
mSvg.render(&painter, iconRect);
painter.end();
}

View File

@ -3,10 +3,6 @@
#include <QStyledItemDelegate>
#include "utils/types.h"
#include <QSvgRenderer>
namespace sqlite3pp {
class database;
}
@ -41,8 +37,6 @@ public:
void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &) const override;
void loadIcon(const QString& fileName);
signals:
/*!
* \brief popupEditorSegmentCombo
@ -66,12 +60,20 @@ private slots:
void onEditorSegmentChosen(StopEditor *editor);
private:
QSvgRenderer *renderer;
void refreshPixmaps();
private:
sqlite3pp::database &mDb;
QPixmap m_lightningPix;
QPixmap m_reverseDirPix;
static constexpr int NormalStopHeight = 100;
static constexpr int TransitStopHeight = 80;
static constexpr int AddHereHeight = 30;
static constexpr int PixWidth = 35;
static constexpr int PixHeight = PixWidth;
};
#endif // STOPDELEGATE_H