-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.cpp
More file actions
52 lines (43 loc) · 1.5 KB
/
utils.cpp
File metadata and controls
52 lines (43 loc) · 1.5 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
#include "utils.h"
#include <QPainter>
#include <QString>
#include <QFontMetrics>
#include <QSize>
namespace utils{
namespace paint{
void drawTextAt(QPainter* painter, const QString &text, int x, int y, TextPosition pos)
{
QFontMetrics fm(painter->font());
QSize bound = fm.size(0, text);
//qDebug() << fm.leading() << fm.ascent() << fm.descent() << fm.lineSpacing() << fm.lineWidth() << fm.strikeOutPos() << fm.overlinePos() << fm.underlinePos();
int dx = fm.leading() - fm.minLeftBearing();
int dy = fm.lineWidth() + fm.descent();
switch(pos){
case TEXT_POS_TOP_LEFT:
painter->drawText(x - bound.width() - dx, y - dy, text);
break;
case TEXT_POS_TOP_RIGHT:
painter->drawText(x + dx, y - dy, text);
break;
case TEXT_POS_BOTTOM_LEFT:
painter->drawText(x - bound.width() - dx, y + bound.height() - dy, text);
break;
case TEXT_POS_BOTTOM_RIGHT:
painter->drawText(x + dx, y + bound.height() - dy, text);
break;
case TEXT_POS_TOP_CENTER:
painter->drawText(x + (dx - bound.width()) / 2, y - dy, text);
break;
case TEXT_POS_BOTTOM_CENTER:
painter->drawText(x + (dx - bound.width()) / 2, y + bound.height() - dy, text);
break;
case TEXT_POS_LEFT_CENTER:
painter->drawText(x - bound.width() - dx, y + (bound.height() - dy) / 2, text);
break;
case TEXT_POS_RIGHT_CENTER:
painter->drawText(x + dx, y + (bound.height() - dy) / 2, text);
break;
}
}
}
}