2 Copyright (c) 2006 Paolo Capriotti <p.capriotti@gmail.com>
3 (c) 2006 Maurizio Monge <maurizio.monge@kdemail.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
12 #include "constrainedtext.h"
16 Clock::Clock(int col
, KGameCanvasAbstract
* canvas
)
17 : ClickableCanvas(canvas
)
21 m_background
= new KGameCanvasPixmap(this);
22 m_caption
= new ConstrainedText(this);
23 m_time_label
= new ConstrainedText(this);
24 m_player_name
= new ConstrainedText(this);
25 m_decs
= new ConstrainedText(this);
30 m_player_name
->show();
34 m_caption
->setText(col
== 0 ? "White" : "Black");
35 connect(&m_timer
, SIGNAL(timeout()), this, SLOT(tick()));
53 if (m_running
) m_total_time
-= m_time
.elapsed();
58 void Clock::activate(bool a
) {
63 m_background
->setPixmap(m_active
? m_active_pixmap
: m_inactive_pixmap
);
65 m_time_label
->setColor(m_active
? m_active_text
: m_inactive_text
);
66 m_player_name
->setColor(m_active
? m_active_text
: m_inactive_text
);
67 m_caption
->setColor(m_active
? m_active_text
: m_inactive_text
);
74 void Clock::computeTime() {
75 int time
= m_total_time
;
76 if (m_running
) time
-= m_time
.elapsed();
82 if (time
> 0 && time
< 10000) {
83 int total_decs
= static_cast<int>(ceil(time
/ 100.0));
84 positive
= total_decs
>= 0;
85 if (!positive
) total_decs
= -total_decs
;
86 decs
= total_decs
% 10;
87 total_secs
= total_decs
/ 10;
90 total_secs
= static_cast<int>(ceil(time
/ 1000.0));
91 positive
= total_secs
>= 0;
92 if (!positive
) total_secs
= -total_secs
;
96 int secs
= total_secs
% 60;
97 int mins
= total_secs
/ 60;
101 QString secText
= QString::number(secs
);
102 if (secText
.length() < 2) secText
= "0" + secText
;
104 QString minText
= QString::number(mins
);
105 if (minText
.length() < 2) minText
= "0" + minText
;
107 timeText
= minText
+ ":" + secText
;
109 timeText
= "-" + timeText
;
112 if (positive
&& decs
!= -1) {
113 int dec
= static_cast<int>(ceil(time
/ 100.0)) % 10;
115 m_decs
->moveTo(m_time_label
->rect().bottomRight() + QPoint(2, 0));
116 m_decs
->setText(":" + QString::number(dec
));
124 m_time_label
->setText(timeText
);
127 QString
Clock::playerString(const Player
& player
) {
128 QString rating
= player
.rating
!= -1 ? QString(" (%1)").arg(player
.rating
) : QString();
129 return QString("%1").arg(player
.name
) + rating
;
132 void Clock::setPlayer(const Player
& player
) {
133 m_player_name
->setText(playerString(player
));
136 void Clock::setTime(int t
) {
141 void Clock::onMousePress(const QPoint
& /*pos*/, int /*button*/) {
144 void Clock::resize(int size
) {
145 m_controls_loader
.setSize(size
);
147 m_height
= (int)m_controls_loader
.getValue
<double>("clock_height");
149 m_active_pixmap
= m_controls_loader
.getValue
<QPixmap
>("clock_active_background");
150 m_inactive_pixmap
= m_controls_loader
.getValue
<QPixmap
>("clock_inactive_background");
152 m_active_text
= m_controls_loader
.getValue
<QBrush
>("clock_active_text").color();
153 m_inactive_text
= m_controls_loader
.getValue
<QBrush
>("clock_inactive_text").color();
155 m_background
->setPixmap(m_active
? m_active_pixmap
: m_inactive_pixmap
);
156 m_background
->moveTo(m_controls_loader
.getValue
<QPointF
>("clock_background_offset").toPoint());
158 m_time_label
->setConstrainRect(m_controls_loader
.getValue
<QRectF
>("clock_time_rect").toRect());
159 m_time_label
->setColor(m_active
? m_active_text
: m_inactive_text
);
161 m_player_name
->setConstrainRect(m_controls_loader
.getValue
<QRectF
>("clock_player_rect").toRect());
162 m_player_name
->setColor(m_active
? m_active_text
: m_inactive_text
);
164 m_caption
->setConstrainRect(m_controls_loader
.getValue
<QRectF
>("clock_caption_rect").toRect());
165 m_caption
->setColor(m_active
? m_active_text
: m_inactive_text
);
168 void Clock::settingsChanged() {
169 m_caption
->setFont(m_controls_loader
.getStaticValue
<QFont
>("clock_caption_font"));
170 m_player_name
->setFont(m_controls_loader
.getStaticValue
<QFont
>("clock_player_font"));
171 m_time_label
->setFont(m_controls_loader
.getStaticValue
<QFont
>("clock_time_font"));