-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimepanel.cpp
83 lines (73 loc) · 1.86 KB
/
timepanel.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
#include "timepanel.h"
#include <QGraphicsItem>
#include <QPainter>
#include "game.h"
extern Game * game;
TimePanel::TimePanel(QGraphicsItem *parent) : QGraphicsItem(parent)
{
QObject::connect(&timerCount,&QTimer::timeout,[this](){
if(isStart){
secCount ++;
}
update();
});
timerCount.start(1000);
}
//倒计时开始/结束
void TimePanel::start()
{
timer.restart();
isStart = true;
}
void TimePanel::stop()
{
isStart = false;
secCount = 0;
}
//绘制相关
void TimePanel::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
painter->setRenderHint(QPainter::Antialiasing,true);
QPen pen(Qt::black);
auto font = painter->font();
font.setBold(true);
font.setPixelSize(20);
painter->setFont(font);
painter->setPen(pen);
painter->drawRect(QRect(0,0,80,80));
painter->drawText(QPoint(0,20),role);
if(countdown){
if(isStart){
if(secCount < 90){
painter->drawText(QPoint(10,40),QString::number(90 - secCount).append(" S"));
}
else{
if(role == "white"){
game->gameover("black");
}
else if(role == "black"){
game->gameover("white");
}
painter->drawText(QPoint(10,40),"0 S");
}
}
else{
painter->drawText(QPoint(10,40),"0 S");
}
}
else{
painter->drawText(QPoint(10,40),QString::number(secCount).append(" S"));
}
}
void TimePanel::setRole(const QString &role)
{
this->role = role;
}
QRectF TimePanel::boundingRect() const
{
return QRectF(0, 0, 100, 100);
}
void TimePanel::setCountDown(bool enable)
{
countdown = enable;
}