forked from GSam/Capture2Text
-
Notifications
You must be signed in to change notification settings - Fork 3
/
SampleBox.cpp
129 lines (108 loc) · 3.96 KB
/
SampleBox.cpp
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
/*
Copyright (C) 2010-2017 Christopher Brochtrup
This file is part of Capture2Text.
Capture2Text is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Capture2Text is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Capture2Text. If not, see <http://www.gnu.org/licenses/>.
*/
#include <QPainter>
#include <QFontMetrics>
#include "SampleBox.h"
SampleBox::SampleBox(QWidget *parent)
: QWidget(parent),
previewText("Sample Preview Text"),
textColor(QColor(128, 128, 128, 255)),
backgroundColor(QColor(255, 0, 0 , 100)),
borderColor(QColor(255, 0, 0, 255)),
textFont(QFont("Arial", 16)),
isCaptureBoxSample(true)
{
}
void SampleBox::drawCheckerboard(QPainter &painter)
{
const int squareSize = 20;
// Draw checkerboard
bool isWhite = false;
int numSquaresX = width() / squareSize + 1;
int numSquaresY = height() / squareSize + 1;
for(int y = 0; y < numSquaresY; y++)
{
isWhite = ((y % 2) == 0);
for(int x = 0; x < numSquaresX; x++)
{
QColor color(200, 200, 200, 255);
if(isWhite)
{
color.setRgb(255, 255, 255, 255);
}
painter.fillRect(x * squareSize, y * squareSize,
squareSize, squareSize, color);
isWhite = !isWhite;
}
}
// Draw border
QColor borderColor(128, 128, 128, 255);
QPen pen;
pen.setColor(borderColor);
painter.setPen(pen);
painter.drawRect(0, 0, width() - 1, height() - 1);
}
void SampleBox::paintEvent(QPaintEvent *)
{
const float gapPercent = 0.10f;
QPainter painter(this);
drawCheckerboard(painter);
if(isCaptureBoxSample)
{
// Background
painter.fillRect(width() * gapPercent + 1,
height() * gapPercent + 1,
width() * (1.0f - gapPercent * 2) - 2,
height() * (1.0f - gapPercent * 2) - 2, backgroundColor);
// Border
QPen pen;
pen.setColor(borderColor);
painter.setPen(pen);
painter.drawRect(width() * gapPercent,
height() * gapPercent,
width() * (1.0f - gapPercent * 2) - 1,
height() * (1.0f - gapPercent * 2) - 1);
}
else
{
QFontMetrics metrics(textFont);
int textWidth = qMin(metrics.width(previewText) + horizontalPadding, width());
int textHeight = metrics.height() + verticalPadding;
// Background
painter.fillRect(width() * gapPercent + 1,
height() * gapPercent + 1,
textWidth,
textHeight,
backgroundColor);
// Border
QPen pen;
pen.setColor(borderColor);
painter.setPen(pen);
painter.drawRect(width() * gapPercent,
height() * gapPercent,
textWidth + 1,
textHeight + 1);
// Text
pen.setColor(textColor);
painter.setPen(pen);
painter.setFont(textFont);
painter.drawText(QRect(width() * gapPercent + horizontalPadding / 2,
height() * gapPercent,
textWidth,
textHeight),
Qt::AlignLeft | Qt::AlignVCenter | Qt::TextSingleLine,
previewText);
}
}