-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsmscalablelabel.cpp
More file actions
74 lines (61 loc) · 2.11 KB
/
smscalablelabel.cpp
File metadata and controls
74 lines (61 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include "smscalablelabel.h"
#include <QDebug>
#include <QFontMetrics>
SMScalableLabel::SMScalableLabel(QWidget * parent) : QLabel(parent)
{
}
void SMScalableLabel::setColor(const QColor & c)
{
QString sheet;
QTextStream(&sheet) << "QLabel { color: rgba("
<< c.red() << ","
<< c.green() << ","
<< c.blue() << ","
<< c.alpha() << "); }";
this->setStyleSheet(sheet);
}
void SMScalableLabel::setTextAndScale(const QString & t)
{
QLabel::setText(t);
this->scale();
}
void SMScalableLabel::resizeEvent(QResizeEvent *)
{
this->scale();
}
void SMScalableLabel::scale()
{
// TODO this should take hints as a parameter(s)
QSize currentSize = this->size();
// qDebug() << "Scaling text...";
QFont f = this->font();
QFontMetrics metrics(f);
QRect bb = metrics.boundingRect(this->text());
// qDebug() << "Current font size: " << f.pointSize();
// qDebug() << "Current bounding box: " << bb;
// qDebug() << "Current size: " << currentSize;
if(bb.width() + bb.x() > currentSize.width() ||
bb.height() + bb.y() > currentSize.height()) {
// Need to reduce font
do {
f.setPointSize(f.pointSize()-1);
metrics = QFontMetrics(f);
bb = metrics.boundingRect(this->text());
} while(bb.width() + bb.x() > currentSize.width() ||
bb.height() + bb.y() > currentSize.height());
} else if(bb.width() + bb.x() < currentSize.width() &&
bb.height() + bb.y() < currentSize.height()) {
// Can increase font
do {
f.setPointSize(f.pointSize()+1);
metrics = QFontMetrics(f);
bb = metrics.boundingRect(this->text());
} while(bb.width() + bb.x() < currentSize.width() &&
bb.height() + bb.y() < currentSize.height());
f.setPointSize(f.pointSize()-1);
}
this->setFont(f);
// qDebug() << "New font size: " << f.pointSize();
// metrics = QFontMetrics(f);
// qDebug() << "New bounding box: " << metrics.boundingRect(this->text());
}