pollymc/launcher/ui/widgets/Common.cpp

34 lines
887 B
C++
Raw Normal View History

#include "Common.h"
// Origin: Qt
// More specifically, this is a trimmed down version on the algorithm in:
// https://code.woboq.org/qt5/qtbase/src/widgets/styles/qcommonstyle.cpp.html#846
QList<std::pair<qreal, QString>> viewItemTextLayout(QTextLayout& textLayout, int lineWidth, qreal& height)
{
QList<std::pair<qreal, QString>> lines;
2018-07-15 18:21:05 +05:30
height = 0;
2018-07-15 18:21:05 +05:30
textLayout.beginLayout();
2018-07-15 18:21:05 +05:30
QString str = textLayout.text();
while (true) {
2018-07-15 18:21:05 +05:30
QTextLine line = textLayout.createLine();
2018-07-15 18:21:05 +05:30
if (!line.isValid())
break;
if (line.textLength() == 0)
break;
2018-07-15 18:21:05 +05:30
line.setLineWidth(lineWidth);
line.setPosition(QPointF(0, height));
2018-07-15 18:21:05 +05:30
height += line.height();
lines.append(std::make_pair(line.naturalTextWidth(), str.mid(line.textStart(), line.textLength())));
2018-07-15 18:21:05 +05:30
}
2018-07-15 18:21:05 +05:30
textLayout.endLayout();
2018-07-15 18:21:05 +05:30
return lines;
}