Reserve code block headers and share Copy button geometry
This commit is contained in:
1 parent
ffcb02354f
commit
f7e82c04a7
2 files changed
+17
No files matched your search
@@ -5,6 +5,7 @@
|
||||
#include <algorithm>
|
||||
#include <climits>
|
||||
#include <QCache>
|
||||
#include <QCoreApplication>
|
||||
#include <QFontDatabase>
|
||||
#include <QFontMetricsF>
|
||||
#include <QRegularExpression>
|
||||
@@ -211,11 +212,24 @@ qreal layoutFormattedMessage(QTextLayout& layout, const MessageText& message, in
|
||||
option.setTabStopDistance(4 * QFontMetricsF(mono).horizontalAdvance(' ')); layout.setTextOption(option);
|
||||
layout.beginLayout();
|
||||
qreal height = 0;
|
||||
int block = 0;
|
||||
while (true) {
|
||||
auto line = layout.createLine();
|
||||
if (!line.isValid()) break;
|
||||
if (block < message.code.size() && message.code[block].start == line.textStart()) {
|
||||
height += QFontMetricsF(layout.font()).height() + 8; ++block;
|
||||
}
|
||||
line.setLineWidth(qMax(1, width)); line.setPosition(QPointF(0, height)); height += line.height();
|
||||
}
|
||||
layout.endLayout();
|
||||
return qMax(height, QFontMetricsF(layout.font()).height());
|
||||
}
|
||||
|
||||
QRectF messageCodeCopyRect(const QTextLayout& layout, const MessageCodeBlock& block, int width) {
|
||||
const auto line = layout.lineForTextPosition(block.start);
|
||||
if (!line.isValid()) return {};
|
||||
const QFontMetricsF metrics(layout.font());
|
||||
const qreal height = metrics.height() + 8;
|
||||
const qreal buttonWidth = qMin(qreal(width), metrics.horizontalAdvance(QCoreApplication::translate("ChatGui", "Copy")) + 16);
|
||||
return QRectF(width - buttonWidth, line.y() - height + 2, buttonWidth, height - 4);
|
||||
}
|
||||
@@ -34,3 +34,6 @@ QSharedPointer<const MessageText> formattedMessageText(const QModelIndex& index)
|
||||
|
||||
// Настроить layout с уже заданным базовым font; width > 0, dark выбирает цвета. Возвращает высоту строк.
|
||||
qreal layoutFormattedMessage(QTextLayout& layout, const MessageText& message, int width, bool dark);
|
||||
|
||||
// Прямоугольник Copy в координатах готового layout; пустой, если у блока нет строки.
|
||||
QRectF messageCodeCopyRect(const QTextLayout& layout, const MessageCodeBlock& block, int width);
|
||||
Reference in new issue
Block a user