From d727feb8060f4eed57f3e36f5abfdb874efa0442 Mon Sep 17 00:00:00 2001 From: baldurk Date: Tue, 15 Dec 2020 14:40:45 +0000 Subject: [PATCH] Improve handling of multiline richresourcetext documents --- qrenderdoc/Code/QRDUtils.cpp | 41 +++++++++++++++++++++++-- qrenderdoc/Code/QRDUtils.h | 5 +-- qrenderdoc/Widgets/Extended/RDLabel.cpp | 3 ++ 3 files changed, 44 insertions(+), 5 deletions(-) diff --git a/qrenderdoc/Code/QRDUtils.cpp b/qrenderdoc/Code/QRDUtils.cpp index f4c27d490..ae94a2531 100644 --- a/qrenderdoc/Code/QRDUtils.cpp +++ b/qrenderdoc/Code/QRDUtils.cpp @@ -196,6 +196,7 @@ struct RichResourceText // the ideal width for the document int idealWidth = 0; + int numLines = 1; // cache the context once we've obtained it. ICaptureContext *ctxptr = NULL; @@ -231,6 +232,8 @@ struct RichResourceText text.clear(); + numLines = 1; + for(const QVariant &v : fragments) { if(v.userType() == qMetaTypeId()) @@ -248,11 +251,23 @@ struct RichResourceText } else { - html += lit("%1").arg(v.toString().toHtmlEscaped()); + QString htmlfrag = v.toString().toHtmlEscaped(); + int newlines = htmlfrag.count(QLatin1Char('\n')); + htmlfrag.replace(lit(" "), lit(" ")); + htmlfrag.replace(lit("\n"), lit("").arg(htmlfrag); text += v.toString(); - // this only generates one block + numLines += newlines; + + // this generates one block at least fragmentIndexFromBlockIndex.push_back(i); + for(int l = 0; l < newlines; l++) + { + fragmentIndexFromBlockIndex.push_back(i); + fragmentIndexFromBlockIndex.push_back(i); + } } i++; @@ -693,6 +708,24 @@ int RichResourceTextWidthHint(const QWidget *owner, const QFont &font, const QVa return linkedText->idealWidth; } +int RichResourceTextHeightHint(const QWidget *owner, const QFont &font, const QVariant &var) +{ + QFontMetrics metrics(font); + + if(var.userType() == qMetaTypeId()) + { + RichResourceTextPtr linkedText = var.value(); + + static const int margin = RichResourceTextMargin; + + linkedText->cacheDocument(owner); + + return linkedText->numLines * (metrics.lineSpacing() + margin * 2); + } + + return metrics.height(); +} + bool RichResourceTextMouseEvent(const QWidget *owner, const QVariant &var, QRect rect, const QFont &font, QMouseEvent *event) { @@ -935,7 +968,9 @@ QSize RichTextViewDelegate::sizeHint(const QStyleOptionViewItem &option, const Q QVariant v = index.data(); if(RichResourceTextCheck(v)) - return QSize(RichResourceTextWidthHint(m_widget, option.font, v), option.fontMetrics.height()); + return QSize( + RichResourceTextWidthHint(m_widget, option.font, v), + qMax(RichResourceTextHeightHint(m_widget, option.font, v), option.fontMetrics.height())); } return ForwardingDelegate::sizeHint(option, index); diff --git a/qrenderdoc/Code/QRDUtils.h b/qrenderdoc/Code/QRDUtils.h index 36aa7b8fa..466429a66 100644 --- a/qrenderdoc/Code/QRDUtils.h +++ b/qrenderdoc/Code/QRDUtils.h @@ -201,9 +201,10 @@ void RichResourceTextPaint(const QWidget *owner, QPainter *painter, QRect rect, QPalette palette, QStyle::State state, QPoint mousePos, const QVariant &var); -// Gives the width for a size hint for the rich text (since it might be larger than the original -// text) +// Gives the width/height for a size hint for the rich text (since it might be larger than the +// original text) int RichResourceTextWidthHint(const QWidget *owner, const QFont &font, const QVariant &var); +int RichResourceTextHeightHint(const QWidget *owner, const QFont &font, const QVariant &var); // Handle a mouse event on some rich resource text. // Returns true if the event is processed - for mouse move events, this means that the mouse is over diff --git a/qrenderdoc/Widgets/Extended/RDLabel.cpp b/qrenderdoc/Widgets/Extended/RDLabel.cpp index 0113be9ef..649ed397d 100644 --- a/qrenderdoc/Widgets/Extended/RDLabel.cpp +++ b/qrenderdoc/Widgets/Extended/RDLabel.cpp @@ -41,9 +41,12 @@ void RDLabel::modifySizeHint(QSize &sz) const sz.setWidth(sz.width() - contentsMargins().left() - contentsMargins().right()); if(m_variant.isValid()) + { sz.setWidth(qMax(RichResourceTextWidthHint(this, font(), m_variant) + contentsMargins().left() + contentsMargins().right() + margin() * 2, sz.width())); + sz.setHeight(qMax(RichResourceTextHeightHint(this, font(), m_variant), sz.height())); + } } QSize RDLabel::sizeHint() const
")); + + html += lit("%1