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
)
20 m_background
= new KGameCanvasPixmap(this);
21 m_caption
= new ConstrainedText(this);
22 m_time_label
= new ConstrainedText(this);
23 m_player_name
= new ConstrainedText(this);
24 m_decs
= new ConstrainedText(this);
29 m_player_name
->show();
33 m_caption
->setText(col
== 0 ? "White" : "Black");
34 connect(&m_timer
, SIGNAL(timeout()), this, SLOT(tick()));
52 if (m_running
) m_total_time
-= m_time
.elapsed();
57 void Clock::activate(bool a
) {
62 m_background
->setPixmap(m_active
? m_active_pixmap
: m_inactive_pixmap
);
64 m_time_label
->setColor(m_active
? m_active_text
: m_inactive_text
);
65 m_player_name
->setColor(m_active
? m_active_text
: m_inactive_text
);
66 m_caption
->setColor(m_active
? m_active_text
: m_inactive_text
);
73 void Clock::computeTime() {
74 int time
= m_total_time
;
75 if (m_running
) time
-= m_time
.elapsed();
81 if (time
> 0 && time
< 10000) {
82 int total_decs
= static_cast<int>(ceil(time
/ 100.0));
83 positive
= total_decs
>= 0;
84 if (!positive
) total_decs
= -total_decs
;
85 decs
= total_decs
% 10;
86 total_secs
= total_decs
/ 10;
89 total_secs
= static_cast<int>(ceil(time
/ 1000.0));
90 positive
= total_secs
>= 0;
91 if (!positive
) total_secs
= -total_secs
;
95 int secs
= total_secs
% 60;
96 int mins
= total_secs
/ 60;
100 QString secText
= QString::number(secs
);
101 if (secText
.length() < 2) secText
= "0" + secText
;
103 QString minText
= QString::number(mins
);
104 if (minText
.length() < 2) minText
= "0" + minText
;
106 timeText
= minText
+ ":" + secText
;
108 timeText
= "-" + timeText
;
111 if (positive
&& decs
!= -1) {
112 int dec
= static_cast<int>(ceil(time
/ 100.0)) % 10;
114 m_decs
->moveTo(m_time_label
->rect().bottomRight() + QPoint(2, 0));
115 m_decs
->setText(":" + QString::number(dec
));
123 m_time_label
->setText(timeText
);
126 QString
Clock::playerString(const Player
& player
) {
127 QString rating
= player
.rating
!= -1 ? QString(" (%1)").arg(player
.rating
) : QString();
128 return QString("%1").arg(player
.name
) + rating
;
131 void Clock::setPlayer(const Player
& player
) {
132 m_player_name
->setText(playerString(player
));
135 void Clock::setTime(int t
) {
140 void Clock::onMousePress(const QPoint
& /*pos*/, int /*button*/) {
143 void Clock::resize(int size
) {
144 m_controls_loader
.setSize(size
);
146 m_height
= (int)m_controls_loader
.getValue
<double>("clock_height");
148 m_active_pixmap
= m_controls_loader
.getValue
<QPixmap
>("clock_active_background");
149 m_inactive_pixmap
= m_controls_loader
.getValue
<QPixmap
>("clock_inactive_background");
151 m_active_text
= m_controls_loader
.getValue
<QBrush
>("clock_active_text").color();
152 m_inactive_text
= m_controls_loader
.getValue
<QBrush
>("clock_inactive_text").color();
154 m_background
->setPixmap(m_active
? m_active_pixmap
: m_inactive_pixmap
);
155 m_background
->moveTo(m_controls_loader
.getValue
<QPointF
>("clock_background_offset").toPoint());
157 m_time_label
->setConstrainRect(m_controls_loader
.getValue
<QRectF
>("clock_time_rect").toRect());
158 m_time_label
->setColor(m_active
? m_active_text
: m_inactive_text
);
160 m_player_name
->setConstrainRect(m_controls_loader
.getValue
<QRectF
>("clock_player_rect").toRect());
161 m_player_name
->setColor(m_active
? m_active_text
: m_inactive_text
);
163 m_caption
->setConstrainRect(m_controls_loader
.getValue
<QRectF
>("clock_caption_rect").toRect());
164 m_caption
->setColor(m_active
? m_active_text
: m_inactive_text
);
167 void Clock::settingsChanged() {
168 m_caption
->setFont(m_controls_loader
.getStaticValue
<QFont
>("clock_caption_font"));
169 m_player_name
->setFont(m_controls_loader
.getStaticValue
<QFont
>("clock_player_font"));
170 m_time_label
->setFont(m_controls_loader
.getStaticValue
<QFont
>("clock_time_font"));