1a9e6e55272cc36ec09c8bfdb6a9be790edeb894
[tagua/yd.git] / src / clock.cpp
blob1a9e6e55272cc36ec09c8bfdb6a9be790edeb894
1 /*
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.
9 */
11 #include "clock.h"
12 #include "constrainedtext.h"
13 #include <math.h>
14 #include <iostream>
16 Clock::Clock(int col, KGameCanvasAbstract* canvas)
17 : ClickableCanvas(canvas)
18 , m_color(col)
19 , m_running(false) {
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);
26 m_background->show();
27 m_caption->show();
28 m_time_label->show();
29 m_player_name->show();
31 setTime(0);
32 setPlayer(Player());
33 m_caption->setText(col == 0 ? "White" : "Black");
34 connect(&m_timer, SIGNAL(timeout()), this, SLOT(tick()));
37 Clock::~Clock() {
38 delete m_background;
39 delete m_caption;
40 delete m_time_label;
41 delete m_player_name;
42 delete m_decs;
45 void Clock::start() {
46 m_running = true;
47 m_time.start();
48 m_timer.start(100);
51 void Clock::stop() {
52 if (m_running) m_total_time -= m_time.elapsed();
53 m_running = false;
54 m_timer.stop();
57 void Clock::activate(bool a) {
58 if(m_active == a)
59 return;
61 m_active = 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);
69 void Clock::tick() {
70 computeTime();
73 void Clock::computeTime() {
74 int time = m_total_time;
75 if (m_running) time -= m_time.elapsed();
77 bool positive;
78 int total_secs;
79 int decs = -1;
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;
88 else {
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;
97 QString timeText;
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;
107 if (!positive)
108 timeText = "-" + timeText;
110 #if 0
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));
116 m_decs->show();
118 else
119 m_decs->hide();
120 #endif
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) {
136 m_total_time = t;
137 tick();
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"));