-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClockWidget.cpp
More file actions
164 lines (143 loc) · 4.45 KB
/
Copy pathClockWidget.cpp
File metadata and controls
164 lines (143 loc) · 4.45 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include "ClockWidget.h"
#include <QPainter>
#include <QGraphicsBlurEffect>
#include <QGraphicsTextItem>
#include <QTime>
#include <math.h>
CClockWidget::CClockWidget(QWidget *parent) :
QWidget(parent)
{
strDate = QDate::currentDate().toString("ddd, dd MMMM ");
strTime = QTime::currentTime().toString("hh:mm");
clockTimer = startTimer(1000);
ShadeColor=0xFFFFFF;
pBackbufferImage=NULL;
TimeFont = font();
TimeFont.setPixelSize(56);
FillShadeMatrix(shadeMatrix8,8);
FillShadeMatrix(shadeMatrix4,4);
}
void CClockWidget::FillShadeMatrix(int* shadeMatrix,int radius)
{
//-F- Filling in coeffs
double maxRad = sqrt(radius*radius+radius*radius);
double norm = 0;
int lineSize = radius*2+1;
for(int y=-radius;y<=radius;y++)
{
for(int x=-radius;x<=radius;x++)
{
double rad = sqrt(x*x+y*y);
if(rad)
{
shadeMatrix[x+radius+(y+radius)*lineSize] = (int)(((1-rad/maxRad)*0.9+0.1)*65536);
norm+= shadeMatrix[x+radius+(y+radius)*lineSize];
}
else
{
shadeMatrix[x+radius+(y+radius)*lineSize] =0;
}
}
}
norm = 65536*2/norm;
for(int y=-radius;y<=radius;y++)
{
for(int x=-radius;x<=radius;x++)
{
shadeMatrix[x+radius+(y+radius)*lineSize]*=norm;
}
}
}
CClockWidget::~CClockWidget()
{
killTimer(clockTimer);
delete pBackbufferImage;
}
void CClockWidget::paintEvent(QPaintEvent *)
{
QPainter painter(this);
painter.drawImage(0,0,*pBackbufferImage);
}
void CClockWidget::DrawBackbuffer()
{
if(!pBackbufferImage)
{
pBackbufferImage = new QImage(size(),QImage::Format_ARGB32);
}
pBackbufferImage->fill(0xFFFFFF);
QImage textImage(pBackbufferImage->size(),pBackbufferImage->format());
textImage.fill(0);
QPainter textPainter(&textImage);
QFontMetrics fm(font());
int fontHeight = fm.height();
textPainter.setPen(TextColor);
textPainter.setFont(TimeFont);
textPainter.drawText(0,0,width(),height(),Qt::AlignHCenter|Qt::AlignTop,strTime);
QFontMetrics fm2(TimeFont);
CreateShade(&textImage,pBackbufferImage,QRect(0,0,width(),fm2.size(Qt::TextSingleLine,strTime).height()+8),shadeMatrix8,8);
QFont fnt = font();
fnt.setBold(true);
textPainter.setFont(fnt);
textPainter.drawText(0,height()-fontHeight-4,width()-4,fontHeight,Qt::AlignRight,strDate);
CreateShade(&textImage,pBackbufferImage,QRect(0,height()-fontHeight-4,width(),fontHeight+4),shadeMatrix4,4);
QPainter bbPainter(pBackbufferImage);
bbPainter.drawImage(0,0,textImage);
update();
}
void CClockWidget::CreateShade(QImage *pSrcImage, QImage* pShadeImage,QRect rect,int* shadeMatrix,int radius)
{
int lineSize = radius*2+1;
unsigned int shadeTransparentColor = ShadeColor.rgb() &0xFFFFFF;
for(int y=rect.top();y<rect.bottom();y++)
{
for(int x=rect.left();x<rect.right();x++)
{
int topDX = x>=pShadeImage->width()-radius ? pShadeImage->width()-x-1 : radius;
int topDY = y>=pShadeImage->height()-radius ? pShadeImage->height()-y-1 : radius;
int startDX = x<radius ? -x : -radius;
int startDY = y<radius ? -y : -radius;
int alpha=0;
for(int dy=startDY;dy<=topDY;dy++)
{
const uchar* pixelData = pSrcImage->scanLine(y+dy);
for(int dx=startDX;dx<=topDX;dx++)
{
if(pixelData[4*(x+dx)]<0x40)
{
alpha+=shadeMatrix[dx+radius+(dy+radius)*lineSize]*pixelData[4*(x+dx)+3];
}
}
}
alpha/=65536;
if(alpha>255)
{
alpha=255;
}
if(alpha)
{
pShadeImage->setPixel(x,y,alpha<<24|shadeTransparentColor);
}
}
}
}
void CClockWidget::timerEvent(QTimerEvent * event)
{
if(event->timerId() == clockTimer)
{
QString strCurDate = QDate::currentDate().toString("dddd,dd MMMM ");
if(strDate != strCurDate)
{
strDate = strCurDate;
}
QTime time = QTime::currentTime();
if(strTime != time.toString("hh:mm"))
{
strTime = time.toString("hh:mm");
DrawBackbuffer();
}
}
}
void CClockWidget::showEvent(QShowEvent *)
{
DrawBackbuffer();
}