183 lines
5.0 KiB
C++
183 lines
5.0 KiB
C++
#include "KnuthPlassItem.h"
|
|
|
|
#include <QPainter>
|
|
|
|
#include "BookImageResolver.h"
|
|
|
|
BookImageResolver *KnuthPlassItem::s_imageResolver = nullptr;
|
|
|
|
KnuthPlassItem::KnuthPlassItem(QQuickItem *parent)
|
|
: QQuickPaintedItem(parent)
|
|
{
|
|
setAntialiasing(true);
|
|
setRenderTarget(QQuickPaintedItem::Image);
|
|
}
|
|
|
|
void KnuthPlassItem::setImageResolver(BookImageResolver *resolver)
|
|
{
|
|
s_imageResolver = resolver;
|
|
}
|
|
|
|
void KnuthPlassItem::setLayout(const QVariant &val)
|
|
{
|
|
m_layoutVariant = val;
|
|
if (val.canConvert<ChunkLayout>()) {
|
|
m_layout = val.value<ChunkLayout>();
|
|
} else {
|
|
m_layout = {};
|
|
}
|
|
setImplicitHeight(m_layout.totalHeight);
|
|
emit layoutChanged();
|
|
update();
|
|
}
|
|
|
|
void KnuthPlassItem::setTextColor(const QColor &c)
|
|
{
|
|
if (m_textColor != c) {
|
|
m_textColor = c;
|
|
emit colorsChanged();
|
|
update();
|
|
}
|
|
}
|
|
|
|
void KnuthPlassItem::setLinkColor(const QColor &c)
|
|
{
|
|
if (m_linkColor != c) {
|
|
m_linkColor = c;
|
|
emit colorsChanged();
|
|
update();
|
|
}
|
|
}
|
|
|
|
void KnuthPlassItem::setDimColor(const QColor &c)
|
|
{
|
|
if (m_dimColor != c) {
|
|
m_dimColor = c;
|
|
emit colorsChanged();
|
|
update();
|
|
}
|
|
}
|
|
|
|
void KnuthPlassItem::setHorizontalMargin(qreal m)
|
|
{
|
|
if (!qFuzzyCompare(m_horizontalMargin, m)) {
|
|
m_horizontalMargin = m;
|
|
emit horizontalMarginChanged();
|
|
update();
|
|
}
|
|
}
|
|
|
|
void KnuthPlassItem::geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry)
|
|
{
|
|
QQuickPaintedItem::geometryChange(newGeometry, oldGeometry);
|
|
if (newGeometry.size() != oldGeometry.size()) {
|
|
update();
|
|
}
|
|
}
|
|
|
|
void KnuthPlassItem::paint(QPainter *painter)
|
|
{
|
|
if (!painter)
|
|
return;
|
|
|
|
painter->setRenderHint(QPainter::Antialiasing, true);
|
|
painter->setRenderHint(QPainter::TextAntialiasing, true);
|
|
painter->setRenderHint(QPainter::SmoothPixmapTransform, true);
|
|
|
|
const qreal offX = m_horizontalMargin;
|
|
|
|
// 1. Draw block decorations (backgrounds & borders)
|
|
for (const auto &dec : m_layout.decorations) {
|
|
QRectF r = dec.rect;
|
|
r.translate(offX, 0);
|
|
|
|
if (dec.background.isValid()) {
|
|
painter->fillRect(r, dec.background);
|
|
}
|
|
|
|
QColor borderColor = dec.borderColor.isValid() ? dec.borderColor : m_dimColor;
|
|
if (dec.borderLeft > 0) {
|
|
painter->fillRect(QRectF(r.left(), r.top(), dec.borderLeft, r.height()), borderColor);
|
|
}
|
|
if (dec.borderTop > 0) {
|
|
painter->fillRect(QRectF(r.left(), r.top(), r.width(), dec.borderTop), borderColor);
|
|
}
|
|
if (dec.borderBottom > 0) {
|
|
painter->fillRect(QRectF(r.left(), r.bottom() - dec.borderBottom, r.width(), dec.borderBottom), borderColor);
|
|
}
|
|
}
|
|
|
|
// 2. Draw horizontal rules (<hr>)
|
|
for (const auto &rule : m_layout.rules) {
|
|
QRectF r = rule.rect;
|
|
r.translate(offX, 0);
|
|
painter->fillRect(r, rule.color.isValid() ? rule.color : m_dimColor);
|
|
}
|
|
|
|
// 3. Draw text runs
|
|
QFont currentFont;
|
|
QColor currentPenColor;
|
|
bool hasFont = false;
|
|
bool hasPen = false;
|
|
|
|
for (const auto &line : m_layout.lines) {
|
|
for (const auto &run : line.runs) {
|
|
QRectF r = run.rect;
|
|
r.translate(offX, 0);
|
|
|
|
// Highlight background
|
|
if (run.background.isValid()) {
|
|
painter->fillRect(r, run.background);
|
|
}
|
|
|
|
// Word text
|
|
QColor fg = run.color.isValid() ? run.color : (run.link ? m_linkColor : m_textColor);
|
|
if (!hasPen || currentPenColor != fg) {
|
|
painter->setPen(fg);
|
|
currentPenColor = fg;
|
|
hasPen = true;
|
|
}
|
|
if (!hasFont || currentFont != run.font) {
|
|
painter->setFont(run.font);
|
|
currentFont = run.font;
|
|
hasFont = true;
|
|
}
|
|
|
|
painter->drawText(QPointF(r.left(), run.baselineY), run.text);
|
|
|
|
// Text decorations
|
|
if (run.underline) {
|
|
qreal lineY = run.baselineY + 2.0;
|
|
painter->drawLine(QPointF(r.left(), lineY), QPointF(r.right(), lineY));
|
|
}
|
|
if (run.strikeOut) {
|
|
qreal lineY = run.baselineY - (run.font.pixelSize() * 0.35);
|
|
painter->drawLine(QPointF(r.left(), lineY), QPointF(r.right(), lineY));
|
|
}
|
|
if (run.overline) {
|
|
qreal lineY = run.baselineY - (run.font.pixelSize() * 0.85);
|
|
painter->drawLine(QPointF(r.left(), lineY), QPointF(r.right(), lineY));
|
|
}
|
|
}
|
|
}
|
|
|
|
// 4. Draw images
|
|
for (const auto &imgItem : m_layout.images) {
|
|
QRectF r = imgItem.targetRect;
|
|
r.translate(offX, 0);
|
|
|
|
QImage img;
|
|
if (s_imageResolver) {
|
|
img = s_imageResolver->resolveImage(imgItem.url);
|
|
}
|
|
if (!img.isNull()) {
|
|
painter->drawImage(r, img);
|
|
} else {
|
|
// Placeholder rect
|
|
painter->setPen(m_dimColor);
|
|
painter->drawRect(r);
|
|
}
|
|
}
|
|
}
|
|
|