Removed AlgebraicNotation from the variant API.
[tagua/yd.git] / src / clock.cpp
blob384a69e2f96a1dfb6c4203d548ba15598e1fd76c
1 /*
2 Copyright (c) 2006 Paolo Capriotti <p.capriotti@sns.it>
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"));
173 #if 1-1
174 #include <math.h>
175 #include <iostream>
176 #include <QResizeEvent>
177 #include "clock.h"
178 #include "mastersettings.h"
180 static void setFontSize(int max, int width, const QString& text, QFont& font) {
181 font.setPointSize(max);
182 return; // FIXME
183 while (max >= 8) {
184 QTime tm; tm.start();
185 QFontMetrics metrics(font);
186 int fw = metrics.boundingRect(text).width();
187 std::cout << "font metrics: " << tm.elapsed() << std::endl;
189 if (fw <= width) break;
190 max--;
191 font.setPointSize(max);
195 void Clock::Info::settingsChanged() {
198 void Clock::Info::setup(const Player& player, const QRect& rect, const QString& caption, KGameCanvasAbstract* canvas) {
199 putInCanvas(canvas);
201 m_player = player;
202 m_total_time = 0;
203 m_rect = rect;
205 Settings s_clock = settings().group("clock");
207 QColor framecol(0x60,0x60,0x90);
208 QColor backgroundColor;
209 (s_clock["background"] |= QColor(0xa0,0xf0,0xd0,200)) >> backgroundColor;
210 m_background = new KGameCanvasRectangle(backgroundColor, QSize(m_rect.size()), this);
211 m_frame[0] = new KGameCanvasRectangle(framecol, QSize(m_rect.width()-2,1), this);
212 m_frame[0]->moveTo(1,0);
213 m_frame[1] = new KGameCanvasRectangle(framecol, QSize(m_rect.width()-2,1), this);
214 m_frame[1]->moveTo(0,m_rect.height()-1);
215 m_frame[2] = new KGameCanvasRectangle(framecol, QSize(1,m_rect.height()), this);
216 m_frame[2]->moveTo(0,0);
217 m_frame[3] = new KGameCanvasRectangle(framecol, QSize(1,m_rect.height()), this);
218 m_frame[3]->moveTo(m_rect.width()-1,0);
220 int tempFontSize;
223 QFont captionFont("Bitstream Vera Sans");
224 (s_clock["captionFontSize"] |=
225 static_cast<int>(captionFont.pointSize() * 1.4)) >> tempFontSize;
226 captionFont.setPointSize(tempFontSize);
227 m_caption = new KGameCanvasText(caption, Qt::black, captionFont,
228 KGameCanvasText::HStart, KGameCanvasText::VTop, this);
229 m_caption->show();
233 QFont timeFont("Bitstream Vera Sans");
234 (s_clock["timeFontSize"] |= timeFont.pointSize() * 2) >> tempFontSize;
235 timeFont.setPointSize(tempFontSize);
236 timeFont.setWeight(QFont::Bold);
237 m_time_label = new KGameCanvasText("", Qt::black, timeFont,
238 KGameCanvasText::HStart, KGameCanvasText::VCenter, this);
239 m_time_label->show();
243 QFont decsFont("Bitstream Vera Sans");
244 (s_clock["decsFontSize"] |=
245 static_cast<int>(decsFont.pointSize() * 0.8)) >> tempFontSize;
246 decsFont.setPointSize(tempFontSize);
247 m_decs = new KGameCanvasText("", Qt::black, decsFont,
248 KGameCanvasText::HStart, KGameCanvasText::VBottom, this);
252 QFont playerFont("Bitstream Vera Sans");
253 (s_clock["playerFontSize"] |= playerFont.pointSize()) >> tempFontSize;
254 playerFont.setPointSize(tempFontSize);
255 m_player_name = new KGameCanvasText(playerString(player), Qt::black, playerFont,
256 KGameCanvasText::HStart, KGameCanvasText::VBottom, this);
257 m_player_name->show();
260 computeTime();
261 update();
262 show();
265 void Clock::Info::reload() {
266 Settings s_clock = settings().group("clock");
268 QFont tempFont;
269 QColor backgroundColor;
271 s_clock["background"] >> backgroundColor;
272 m_background->setColor(backgroundColor);
274 tempFont = m_caption->font();
275 tempFont.setPointSize(s_clock["captionFontSize"].value<int>());
276 m_caption->setFont(tempFont);
278 tempFont = m_time_label->font();
279 tempFont.setPointSize(s_clock["timeFontSize"].value<int>());
280 m_time_label->setFont(tempFont);
282 tempFont = m_decs->font();
283 tempFont.setPointSize(s_clock["decsFontSize"].value<int>());
284 m_decs->setFont(tempFont);
286 tempFont = m_player_name->font();
287 tempFont.setPointSize(s_clock["playerFontSize"].value<int>());
288 m_player_name->setFont(tempFont);
291 QString Clock::Info::playerString(const Player& player) const {
292 QString rating = player.rating != -1 ? QString(" (%1)").arg(player.rating) : "";
293 return QString("%1").arg(player.name) + rating;
296 void Clock::Info::setPlayer(const Player& player) {
297 m_player_name->setText(playerString(player));
300 void Clock::Info::setTime(int time) {
301 m_total_time = time;
302 tick();
305 void Clock::Info::resize(const QRect& rect) {
306 m_rect = rect;
307 update();
310 void Clock::Info::update() {
311 m_background->setSize(m_rect.size());
313 m_frame[0]->setSize(QSize(m_rect.width()-2,1));
314 m_frame[0]->moveTo(1,0);
315 m_frame[1]->setSize(QSize(m_rect.width()-2,1));
316 m_frame[1]->moveTo(1,m_rect.height()-1);
317 m_frame[2]->setSize(QSize(1,m_rect.height()));
318 m_frame[2]->moveTo(0,0);
319 m_frame[3]->setSize(QSize(1,m_rect.height()));
320 m_frame[3]->moveTo(m_rect.width()-1,0);
323 /*QFont font = m_caption->font();
324 setFontSize(20, m_rect.width() / 2, m_caption->text(), font);
325 m_caption->setFont(font);*/
326 m_caption->moveTo(QPoint(10, 10));
330 QPoint pos(
331 static_cast<int>(m_rect.width() * 0.5),
332 static_cast<int>(m_rect.height() * 0.5));
333 /*QFont font = m_time_label->font();
334 int width = m_rect.width() - pos.x();
335 setFontSize(22, width,
336 m_time_label->text(), font);
337 m_time_label->setFont(font);*/
338 m_time_label->moveTo(pos);
341 m_player_name->moveTo(QPoint(
342 static_cast<int>(m_rect.width() * 0.05),
343 static_cast<int>(m_rect.height() * 0.8)));
345 moveTo(m_rect.topLeft());
348 void Clock::Info::start() {
349 m_running = true;
350 m_time.start();
353 void Clock::Info::stop() {
354 if (m_running) m_total_time -= m_time.elapsed();
355 m_running = false;
358 void Clock::Info::computeTime() const {
359 int time = m_total_time;
360 if (m_running) time -= m_time.elapsed();
361 bool positive;
362 int total_secs;
363 int decs = -1;
365 if (time > 0 && time < 10000) {
366 int total_decs = static_cast<int>(ceil(time / 100.0));
367 positive = total_decs >= 0;
368 if (!positive) total_decs = -total_decs;
369 decs = total_decs % 10;
370 total_secs = total_decs / 10;
372 else {
373 total_secs = static_cast<int>(ceil(time / 1000.0));
374 positive = total_secs >= 0;
375 if (!positive) total_secs = -total_secs;
379 int secs = total_secs % 60;
380 int mins = total_secs / 60;
381 QString timeText;
384 QString secText = QString::number(secs);
385 if (secText.length() < 2) secText = "0" + secText;
387 QString minText = QString::number(mins);
388 if (minText.length() < 2) minText = "0" + minText;
390 timeText = minText + ":" + secText;
391 if (!positive)
392 timeText = "-" + timeText;
394 if (positive && decs != -1) {
395 int dec = static_cast<int>(ceil(time / 100.0)) % 10;
397 m_decs->moveTo(m_time_label->rect().bottomRight() + QPoint(2, 0));
398 m_decs->setText(":" + QString::number(dec));
399 m_decs->show();
401 else
402 m_decs->hide();
405 m_time_label->setText(timeText);
406 m_time_label->setColor(time <= 0 && m_running ? QColor(200,20,20) : Qt::black);
409 void Clock::Info::tick() {
410 computeTime();
413 void Clock::Info::activate(bool value) {
414 m_background->setVisible(value);
415 for(int i=0;i<4;i++)
416 m_frame[i]->setVisible(value);
418 QColor textcolor = value ? Qt::black : Qt::darkGray;
419 m_caption->setColor(textcolor);
420 m_time_label->setColor(textcolor);
421 m_decs->setColor(textcolor);
422 m_player_name->setColor(textcolor);
425 QRect Clock::Info::eventRect() const {
426 return m_background->rect().translated(pos());
430 Clock::Clock(KGameCanvasAbstract* parent)
431 : ClickableCanvas(parent)
432 , m_running(-1)
433 , m_active(-1) {
434 QTime startup_time; startup_time.start();
435 m_info[0].setup(Player(), QRect(0, 0, 0, 0), "White", this);
436 m_info[1].setup(Player(), QRect(0, 0, 0, 0), "Black", this);
437 connect(&m_timer, SIGNAL(timeout()), this, SLOT(tick()));
440 void Clock::settingsChanged() {
441 for(int i=0;i<2;i++)
442 m_info[i].settingsChanged();
445 void Clock::resize(QSize size) {
446 int baseWidth = (size.width() - 10) / 2;
447 m_info[0].resize(QRect(0, 0, baseWidth, 70));
448 m_info[1].resize(QRect(baseWidth + 10, 0, baseWidth, 70));
451 void Clock::reload() {
452 m_info[0].reload();
453 m_info[1].reload();
456 void Clock::setTime(int index, int value) {
457 Q_ASSERT(index == 0 || index == 1);
459 m_info[index].setTime(value);
462 void Clock::start(int index) {
463 Q_ASSERT(index == 0 || index == 1);
465 m_timer.start(10);
466 m_running = index;
467 m_info[index].start();
468 m_info[1 - index].stop();
471 void Clock::stop() {
472 m_info[0].stop();
473 m_info[1].stop();
474 m_timer.stop();
475 m_running = -1;
478 void Clock::activate(int index) {
479 m_active = index;
480 m_info[0].activate(index == 0);
481 m_info[1].activate(index == 1);
484 void Clock::tick() {
485 if (m_running != -1) {
486 Q_ASSERT(m_running == 0 || m_running == 1);
487 m_info[m_running].tick();
491 void Clock::setPlayers(const Player& white, const Player& black) {
492 m_info[0].setPlayer(white);
493 m_info[1].setPlayer(black);
496 void Clock::onMousePress(const QPoint& pos, int button) {
497 if (button == Qt::LeftButton) {
498 if (m_info[0].eventRect().contains(pos))
499 labelClicked(0);
500 else if (m_info[1].eventRect().contains(pos))
501 labelClicked(1);
505 #endif