Address some valgrind-detected problems, flag others for later.
[tagua/yd.git] / src / clock.cpp
blobef5cf8fefc7513bf13c59ff859eb94eb6e687094
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_active(false) {
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);
27 m_background->show();
28 m_caption->show();
29 m_time_label->show();
30 m_player_name->show();
32 setTime(0);
33 setPlayer(Player());
34 m_caption->setText(col == 0 ? "White" : "Black");
35 connect(&m_timer, SIGNAL(timeout()), this, SLOT(tick()));
38 Clock::~Clock() {
39 delete m_background;
40 delete m_caption;
41 delete m_time_label;
42 delete m_player_name;
43 delete m_decs;
46 void Clock::start() {
47 m_running = true;
48 m_time.start();
49 m_timer.start(100);
52 void Clock::stop() {
53 if (m_running) m_total_time -= m_time.elapsed();
54 m_running = false;
55 m_timer.stop();
58 void Clock::activate(bool a) {
59 if(m_active == a)
60 return;
62 m_active = 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);
70 void Clock::tick() {
71 computeTime();
74 void Clock::computeTime() {
75 int time = m_total_time;
76 if (m_running) time -= m_time.elapsed();
78 bool positive;
79 int total_secs;
80 int decs = -1;
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;
89 else {
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;
98 QString timeText;
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;
108 if (!positive)
109 timeText = "-" + timeText;
111 #if 0
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));
117 m_decs->show();
119 else
120 m_decs->hide();
121 #endif
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) {
137 m_total_time = t;
138 tick();
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"));