-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtextrenderer.cpp
More file actions
132 lines (91 loc) · 3.47 KB
/
textrenderer.cpp
File metadata and controls
132 lines (91 loc) · 3.47 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include "textrenderer.h"
TextRenderer::TextRenderer(QWidget *parent, QWidget *mainWidget)
: QTextEdit(parent)
{
this->mainWidget = mainWidget;
this->setReadOnly(true);
textwidth = 80;
limittextwidth = true;
this->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
this->verticalScrollBar()->setStyle(new QCommonStyle());
this->horizontalScrollBar()->setStyle(new QCommonStyle());
this->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
this->setCursorWidth(this->fontMetrics().horizontalAdvance("X"));
this->setTabStopDistance(this->cursorWidth() * 4);
this->setWordWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere);
QTextOption option = this->document()->defaultTextOption();
option.setAlignment(Qt::AlignJustify);
this->document()->setDefaultTextOption(option);
}
/**
* @brief Sets the margins of the contents so that textwidth characters are shown.
* @param textwidth
* @return
*/
int TextRenderer::setTextWidth(const int textwidth) {
int leftMargin = 0;
int rightMargin = 0;
if(limittextwidth) {
this->textwidth = textwidth;
if(textwidth < 10) {
this->textwidth = 10;
}
int globalWidth = this->mainWidget->width();
int globalX = this->mapToGlobal(QPoint(0, 0)).x();
int textwidthPixels = this->fontMetrics().horizontalAdvance(QString(this->textwidth, 'X'));
if(textwidthPixels < this->width()) {
int globalMargin = (globalWidth - textwidthPixels) / 2;
leftMargin = globalMargin - globalX;
if(leftMargin < 0) {
leftMargin = 0;
}
rightMargin = globalMargin - (globalWidth - globalX - this->width());
if(rightMargin < 0) {
rightMargin = 0;
}
if(this->width() < textwidthPixels + leftMargin + rightMargin) {
rightMargin = this->width() - textwidthPixels - leftMargin;
}
}
}
this->setViewportMargins(leftMargin, 0, rightMargin, 0);
return this->textwidth;
}
void TextRenderer::limitTextWidth(const bool limittextwidth) {
this->limittextwidth = limittextwidth;
setTextWidth(textwidth);
}
void TextRenderer::resizeEvent(QResizeEvent *event) {
this->QTextEdit::resizeEvent(event);
this->setTextWidth(textwidth);
}
void TextRenderer::setFont(const QFont &font) {
QFont fontCopy = font;
fontCopy.setPointSize(this->font().pointSize());
this->QWidget::setFont(fontCopy);
this->setTextWidth(textwidth);
this->setCursorWidth(this->fontMetrics().horizontalAdvance("X"));
this->setTabStopDistance(this->cursorWidth() * 4);
}
void TextRenderer::setFontSize(const int fontsize) {
QFont font = this->font();
font.setPointSize(fontsize);
this->QWidget::setFont(font);
this->setTextWidth(textwidth);
this->setCursorWidth(this->fontMetrics().horizontalAdvance("X"));
this->setTabStopDistance(this->cursorWidth() * 4);
}
void TextRenderer::scrollToBlock(const int blockNumber) {
int position = this->document()->findBlockByNumber(blockNumber).position();
if(blockNumber < 0) {
position = 0;
}
if(blockNumber >= this->document()->blockCount()) {
position = this->document()->lastBlock().position();
}
this->moveCursor(QTextCursor::End);
QTextCursor cursor = this->textCursor();
cursor.setPosition(position);
this->setTextCursor(cursor);
this->ensureCursorVisible();
}