1
0
mirror of https://github.com/KDE/latte-dock.git synced 2025-02-13 21:57:40 +03:00

screendialog:proper metrics for screens labels

This commit is contained in:
Michail Vourlakos 2021-06-05 13:33:01 +03:00
parent c9c310b19a
commit c951fd6f60
3 changed files with 41 additions and 9 deletions

View File

@ -123,6 +123,34 @@ void drawFormattedText(QPainter *painter, const QStyleOptionMenuItem &option, co
drawFormattedText(painter, option, option.text, Qt::AlignLeft, textOpacity);
}
QRect remainedFromFormattedText(const QStyleOption &option, const QString &text, Qt::AlignmentFlag alignment)
{
QString css = QString("body {}");
QTextDocument doc;
doc.setDefaultStyleSheet(css);
doc.setHtml("<body>" + text + "</body>");
//we need an offset to be in the same vertical center of TextEdit
int textWidth = doc.size().width() + MARGIN;
Qt::AlignmentFlag curalign = alignment;
if (qApp->layoutDirection() == Qt::LeftToRight || (curalign == Qt::AlignHCenter)) {
curalign = alignment;
} else {
curalign = alignment == Qt::AlignLeft ? Qt::AlignRight : Qt::AlignLeft;
}
if (alignment == Qt::AlignHCenter) {
return option.rect;
} else if (curalign == Qt::AlignRight) {
return QRect(option.rect.x(), option.rect.y(), option.rect.width() - textWidth, option.rect.height());
} else {
return QRect(option.rect.x() + textWidth, option.rect.y(), option.rect.width() - textWidth, option.rect.height());
}
}
void drawFormattedText(QPainter *painter, const QStyleOption &option, const QString &text, Qt::AlignmentFlag alignment, const float textOpacity)
{
painter->save();

View File

@ -29,6 +29,7 @@ Qt::AlignmentFlag horizontalAlignment(Qt::Alignment alignments);
//! now they are not present to current list
QStringList subtracted(const QStringList &original, const QStringList &current);
QRect remainedFromFormattedText(const QStyleOption &option, const QString &text, Qt::AlignmentFlag alignment = Qt::AlignLeft);
void drawFormattedText(QPainter *painter, const QStyleOptionViewItem &option, const float textOpacity = 1.0);
void drawFormattedText(QPainter *painter, const QStyleOptionMenuItem &option, const float textOpacity = 1.0);
void drawFormattedText(QPainter *painter, const QStyleOption &option, const QString &text, Qt::AlignmentFlag alignment = Qt::AlignLeft, const float textOpacity = 1.0);

View File

@ -87,15 +87,6 @@ void CheckBox::paint(QPainter *painter, const QStyleOptionViewItem &option, cons
Latte::drawScreen(painter, adjustedOption, screen.geometry, maxiconsize);
adjustedOption.rect = remainedrect;
//! screen name
adjustedOption.text = screen.name;
if (isActive) {
adjustedOption.text = "<b>" + adjustedOption.text + "</b>";
}
Latte::drawFormattedText(painter, adjustedOption);
//! screen id
adjustedOption.text = "{" + screen.id + "}";
@ -104,6 +95,18 @@ void CheckBox::paint(QPainter *painter, const QStyleOptionViewItem &option, cons
}
adjustedOption.displayAlignment = Qt::AlignRight;
remainedrect = remainedFromFormattedText(adjustedOption, adjustedOption.text, Qt::AlignRight);
Latte::drawFormattedText(painter, adjustedOption);
adjustedOption.rect = remainedrect;
//! screen name
adjustedOption.text = screen.name;
if (isActive) {
adjustedOption.text = "<b>" + adjustedOption.text + "</b>";
}
adjustedOption.displayAlignment = Qt::AlignLeft;
Latte::drawFormattedText(painter, adjustedOption);
}