This repository was archived by the owner on May 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlightcycle.cpp
More file actions
129 lines (124 loc) · 3.19 KB
/
Copy pathlightcycle.cpp
File metadata and controls
129 lines (124 loc) · 3.19 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
#include "lightcycle.h"
LightCycle::LightCycle(QWidget *parent) :
QWidget(parent)
{
pixmap = QPixmap(800,SEGMENT_HEIGHT);
image = pixmap.toImage();
column.resize(SEGMENT_HEIGHT);
startTimer(100);
startColumn=0;
setText("----");
ncols=1;
}
void LightCycle::setText(QString text){
QColor color;
QPainter p;
int textWidthinPixels;
//qDebug() << "texto = " << text ;
if(text.size() == 0)
text = QString("Desconhecido");
text+=" ";
QFont font;
font.setStyleHint(QFont::Courier);
font.setWeight(QFont::Light);
font.setKerning(false);
font.setPixelSize(SEGMENT_HEIGHT);
QFontMetrics fm(font);
textWidthinPixels = fm.width(text);
pixmap = QPixmap(textWidthinPixels,SEGMENT_HEIGHT);
//qDebug() << pixmap.rect();
p.begin(&pixmap);
font.setStyleStrategy(QFont::NoAntialias);
p.setFont(font);
pen.setStyle(Qt::NoPen);
p.setPen(pen);
p.setBrush(Qt::white);
p.drawRect(pixmap.rect());
p.setBrush(Qt::black);
pen.setStyle(Qt::SolidLine);
pen.setColor(Qt::black);
p.setPen(pen);
p.drawText(pixmap.rect(),Qt::AlignVCenter|Qt::AlignLeft,text);
p.end();
image = pixmap.toImage();
matrix.clear();
//qDebug() << image.size();
for(int i=0; i<image.width(); i++){
for(int j=0; j<SEGMENT_HEIGHT; j++){
color = image.pixel(i,j);
//qDebug() << color.value();
// checks if the color is white (background color)
if(color.value()!=255)
column[j] = true;
else
column[j] = false;
}
matrix.push_back(column);
}
// std::ofstream fout;
repaint();
}
void LightCycle::resizeEvent(QResizeEvent *e){
Q_UNUSED(e);
lightDiameter = height()/SEGMENT_HEIGHT;
if(lightDiameter <= 1)
lightDiameter = 2;
ncols = width()/lightDiameter;
//qDebug() << ncols;
if(ncols > image.width()){
for(int i=0; i<SEGMENT_HEIGHT; i++){
column[i] = false;
}
for(int i=0; i<ncols-matrix.size(); i++){
matrix.push_back(column);
}
}
else{
matrix.resize(image.width());
}
//qDebug() << ncols << "/" << matrix.size();
}
void LightCycle::timerEvent(QTimerEvent *e){
Q_UNUSED(e);
/*
QRect rect;
QVector<QRgb> column(SEGMENT_HEIGHT);
for(int i=0; i<SEGMENT_HEIGHT; i++){
column[i] = image.pixel(0,i);
}
pixmap.scroll(-1,0,pixmap.rect());
image=pixmap.toImage();
for(int i=0; i<SEGMENT_HEIGHT; i++){
image.setPixel(pixmap.width()-1,i,column[i]);
}
pixmap = pixmap.fromImage(image);*/
startColumn++;
if(startColumn > matrix.size())
startColumn=0;
repaint();
}
void LightCycle::paintEvent(QPaintEvent *e){
Q_UNUSED(e);
int index;
QColor color;
QPainter p(this);
p.setBrush(Qt::white);
p.setPen(Qt::NoPen);
p.setRenderHint(QPainter::Antialiasing);
p.setPen(Qt::white);
p.drawRect(rect());
for(int i=0; i<ncols; i++){
index = (i+startColumn)%matrix.size();
// qDebug() << "index=" << index << "/" << matrix.size();
for(int j=0; j<SEGMENT_HEIGHT; j++){
if(matrix[index][j] == true){
p.setBrush(Qt::blue);
p.drawEllipse(lightDiameter*i,lightDiameter*j,lightDiameter,lightDiameter);
}
else{
p.setBrush(Qt::lightGray);
p.drawEllipse(lightDiameter*i,lightDiameter*j,lightDiameter,lightDiameter);
}
}
}
}