From b399ef9f5fac8327e6e37f6caf106b7706590386 Mon Sep 17 00:00:00 2001 From: Paolo Capriotti Date: Fri, 17 Aug 2007 12:21:22 +0200 Subject: [PATCH] Fixed ticket #30. --- src/chesstable.cpp | 7 + src/clock.cpp | 677 ++++++++++++++--------------------------------------- src/clock.h | 81 ------- 3 files changed, 179 insertions(+), 586 deletions(-) rewrite src/clock.cpp (64%) diff --git a/src/chesstable.cpp b/src/chesstable.cpp index 1dfcb5c..c931db3 100644 --- a/src/chesstable.cpp +++ b/src/chesstable.cpp @@ -183,9 +183,11 @@ void ChessTable::layout(bool force_reload) { m_clocks[x]->resize(clock_size); m_clocks[x]->moveTo(clock0_position.x(), clock0_position.y()); + std::cout << "moving clock " << x << " to " << clock0_position.y() << std::endl; m_clocks[!x]->resize(clock_size); m_clocks[!x]->moveTo(clock1_position.x(), clock1_position.y()); + std::cout << "moving clock " << !x << " to " << clock1_position.y() << std::endl; m_pools[x]->m_flipped = false; m_pools[x]->onResize(pool_piece_size, force_reload); @@ -276,6 +278,11 @@ void ChessTable::flip() { int delta = qAbs(m_pools[0]->pos().y() - m_pools[1]->pos().y()); for(int i=0;i<2;i++) m_pools[i]->flipAndMoveBy( QPoint(0, delta) ); + + // flip clocks + QPoint pos = m_clocks[0]->pos(); + m_clocks[0]->moveTo(m_clocks[1]->pos()); + m_clocks[1]->moveTo(pos); } void ChessTable::flip(bool flipped) { diff --git a/src/clock.cpp b/src/clock.cpp dissimilarity index 64% index 7b8ca4e..1a9e6e5 100644 --- a/src/clock.cpp +++ b/src/clock.cpp @@ -1,505 +1,172 @@ -/* - Copyright (c) 2006 Paolo Capriotti - (c) 2006 Maurizio Monge - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. -*/ - -#include "clock.h" -#include "constrainedtext.h" -#include -#include - -Clock::Clock(int col, KGameCanvasAbstract* canvas) -: ClickableCanvas(canvas) -, m_color(col) -, m_running(false) { - m_background = new KGameCanvasPixmap(this); - m_caption = new ConstrainedText(this); - m_time_label = new ConstrainedText(this); - m_player_name = new ConstrainedText(this); - m_decs = new ConstrainedText(this); - - m_background->show(); - m_caption->show(); - m_time_label->show(); - m_player_name->show(); - - setTime(0); - setPlayer(Player()); - m_caption->setText(col == 0 ? "White" : "Black"); - connect(&m_timer, SIGNAL(timeout()), this, SLOT(tick())); -} - -Clock::~Clock() { - delete m_background; - delete m_caption; - delete m_time_label; - delete m_player_name; - delete m_decs; -} - -void Clock::start() { - m_running = true; - m_time.start(); - m_timer.start(100); -} - -void Clock::stop() { - if (m_running) m_total_time -= m_time.elapsed(); - m_running = false; - m_timer.stop(); -} - -void Clock::activate(bool a) { - if(m_active == a) - return; - - m_active = a; - m_background->setPixmap(m_active ? m_active_pixmap : m_inactive_pixmap); - - m_time_label->setColor(m_active ? m_active_text : m_inactive_text); - m_player_name->setColor(m_active ? m_active_text : m_inactive_text); - m_caption->setColor(m_active ? m_active_text : m_inactive_text); -} - -void Clock::tick() { - computeTime(); -} - -void Clock::computeTime() { - int time = m_total_time; - if (m_running) time -= m_time.elapsed(); - - bool positive; - int total_secs; - int decs = -1; - - if (time > 0 && time < 10000) { - int total_decs = static_cast(ceil(time / 100.0)); - positive = total_decs >= 0; - if (!positive) total_decs = -total_decs; - decs = total_decs % 10; - total_secs = total_decs / 10; - } - else { - total_secs = static_cast(ceil(time / 1000.0)); - positive = total_secs >= 0; - if (!positive) total_secs = -total_secs; - } - - - int secs = total_secs % 60; - int mins = total_secs / 60; - QString timeText; - - { - QString secText = QString::number(secs); - if (secText.length() < 2) secText = "0" + secText; - - QString minText = QString::number(mins); - if (minText.length() < 2) minText = "0" + minText; - - timeText = minText + ":" + secText; - if (!positive) - timeText = "-" + timeText; - -#if 0 - if (positive && decs != -1) { - int dec = static_cast(ceil(time / 100.0)) % 10; - - m_decs->moveTo(m_time_label->rect().bottomRight() + QPoint(2, 0)); - m_decs->setText(":" + QString::number(dec)); - m_decs->show(); - } - else - m_decs->hide(); -#endif - } - - m_time_label->setText(timeText); -} - -QString Clock::playerString(const Player& player) { - QString rating = player.rating != -1 ? QString(" (%1)").arg(player.rating) : QString(); - return QString("%1").arg(player.name) + rating; -} - -void Clock::setPlayer(const Player& player) { - m_player_name->setText(playerString(player)); -} - -void Clock::setTime(int t) { - m_total_time = t; - tick(); -} - -void Clock::onMousePress(const QPoint& /*pos*/, int /*button*/) { -} - -void Clock::resize(int size) { - m_controls_loader.setSize(size); - - m_height = (int)m_controls_loader.getValue("clock_height"); - - m_active_pixmap = m_controls_loader.getValue("clock_active_background"); - m_inactive_pixmap = m_controls_loader.getValue("clock_inactive_background"); - - m_active_text = m_controls_loader.getValue("clock_active_text").color(); - m_inactive_text = m_controls_loader.getValue("clock_inactive_text").color(); - - m_background->setPixmap(m_active ? m_active_pixmap : m_inactive_pixmap); - m_background->moveTo(m_controls_loader.getValue("clock_background_offset").toPoint()); - - m_time_label->setConstrainRect(m_controls_loader.getValue("clock_time_rect").toRect()); - m_time_label->setColor(m_active ? m_active_text : m_inactive_text); - - m_player_name->setConstrainRect(m_controls_loader.getValue("clock_player_rect").toRect()); - m_player_name->setColor(m_active ? m_active_text : m_inactive_text); - - m_caption->setConstrainRect(m_controls_loader.getValue("clock_caption_rect").toRect()); - m_caption->setColor(m_active ? m_active_text : m_inactive_text); -} - -void Clock::settingsChanged() { - m_caption->setFont(m_controls_loader.getStaticValue("clock_caption_font")); - m_player_name->setFont(m_controls_loader.getStaticValue("clock_player_font")); - m_time_label->setFont(m_controls_loader.getStaticValue("clock_time_font")); -} - -#if 1-1 -#include -#include -#include -#include "clock.h" -#include "mastersettings.h" - -static void setFontSize(int max, int width, const QString& text, QFont& font) { - font.setPointSize(max); - return; // FIXME - while (max >= 8) { - QTime tm; tm.start(); - QFontMetrics metrics(font); - int fw = metrics.boundingRect(text).width(); - std::cout << "font metrics: " << tm.elapsed() << std::endl; - - if (fw <= width) break; - max--; - font.setPointSize(max); - } -} - -void Clock::Info::settingsChanged() { -} - -void Clock::Info::setup(const Player& player, const QRect& rect, const QString& caption, KGameCanvasAbstract* canvas) { - putInCanvas(canvas); - - m_player = player; - m_total_time = 0; - m_rect = rect; - - Settings s_clock = settings().group("clock"); - - QColor framecol(0x60,0x60,0x90); - QColor backgroundColor; - (s_clock["background"] |= QColor(0xa0,0xf0,0xd0,200)) >> backgroundColor; - m_background = new KGameCanvasRectangle(backgroundColor, QSize(m_rect.size()), this); - m_frame[0] = new KGameCanvasRectangle(framecol, QSize(m_rect.width()-2,1), this); - m_frame[0]->moveTo(1,0); - m_frame[1] = new KGameCanvasRectangle(framecol, QSize(m_rect.width()-2,1), this); - m_frame[1]->moveTo(0,m_rect.height()-1); - m_frame[2] = new KGameCanvasRectangle(framecol, QSize(1,m_rect.height()), this); - m_frame[2]->moveTo(0,0); - m_frame[3] = new KGameCanvasRectangle(framecol, QSize(1,m_rect.height()), this); - m_frame[3]->moveTo(m_rect.width()-1,0); - - int tempFontSize; - - { - QFont captionFont("Bitstream Vera Sans"); - (s_clock["captionFontSize"] |= - static_cast(captionFont.pointSize() * 1.4)) >> tempFontSize; - captionFont.setPointSize(tempFontSize); - m_caption = new KGameCanvasText(caption, Qt::black, captionFont, - KGameCanvasText::HStart, KGameCanvasText::VTop, this); - m_caption->show(); - } - - { - QFont timeFont("Bitstream Vera Sans"); - (s_clock["timeFontSize"] |= timeFont.pointSize() * 2) >> tempFontSize; - timeFont.setPointSize(tempFontSize); - timeFont.setWeight(QFont::Bold); - m_time_label = new KGameCanvasText("", Qt::black, timeFont, - KGameCanvasText::HStart, KGameCanvasText::VCenter, this); - m_time_label->show(); - } - - { - QFont decsFont("Bitstream Vera Sans"); - (s_clock["decsFontSize"] |= - static_cast(decsFont.pointSize() * 0.8)) >> tempFontSize; - decsFont.setPointSize(tempFontSize); - m_decs = new KGameCanvasText("", Qt::black, decsFont, - KGameCanvasText::HStart, KGameCanvasText::VBottom, this); - } - - { - QFont playerFont("Bitstream Vera Sans"); - (s_clock["playerFontSize"] |= playerFont.pointSize()) >> tempFontSize; - playerFont.setPointSize(tempFontSize); - m_player_name = new KGameCanvasText(playerString(player), Qt::black, playerFont, - KGameCanvasText::HStart, KGameCanvasText::VBottom, this); - m_player_name->show(); - } - - computeTime(); - update(); - show(); -} - -void Clock::Info::reload() { - Settings s_clock = settings().group("clock"); - - QFont tempFont; - QColor backgroundColor; - - s_clock["background"] >> backgroundColor; - m_background->setColor(backgroundColor); - - tempFont = m_caption->font(); - tempFont.setPointSize(s_clock["captionFontSize"].value()); - m_caption->setFont(tempFont); - - tempFont = m_time_label->font(); - tempFont.setPointSize(s_clock["timeFontSize"].value()); - m_time_label->setFont(tempFont); - - tempFont = m_decs->font(); - tempFont.setPointSize(s_clock["decsFontSize"].value()); - m_decs->setFont(tempFont); - - tempFont = m_player_name->font(); - tempFont.setPointSize(s_clock["playerFontSize"].value()); - m_player_name->setFont(tempFont); -} - -QString Clock::Info::playerString(const Player& player) const { - QString rating = player.rating != -1 ? QString(" (%1)").arg(player.rating) : ""; - return QString("%1").arg(player.name) + rating; -} - -void Clock::Info::setPlayer(const Player& player) { - m_player_name->setText(playerString(player)); -} - -void Clock::Info::setTime(int time) { - m_total_time = time; - tick(); -} - -void Clock::Info::resize(const QRect& rect) { - m_rect = rect; - update(); -} - -void Clock::Info::update() { - m_background->setSize(m_rect.size()); - - m_frame[0]->setSize(QSize(m_rect.width()-2,1)); - m_frame[0]->moveTo(1,0); - m_frame[1]->setSize(QSize(m_rect.width()-2,1)); - m_frame[1]->moveTo(1,m_rect.height()-1); - m_frame[2]->setSize(QSize(1,m_rect.height())); - m_frame[2]->moveTo(0,0); - m_frame[3]->setSize(QSize(1,m_rect.height())); - m_frame[3]->moveTo(m_rect.width()-1,0); - - { - /*QFont font = m_caption->font(); - setFontSize(20, m_rect.width() / 2, m_caption->text(), font); - m_caption->setFont(font);*/ - m_caption->moveTo(QPoint(10, 10)); - } - - { - QPoint pos( - static_cast(m_rect.width() * 0.5), - static_cast(m_rect.height() * 0.5)); - /*QFont font = m_time_label->font(); - int width = m_rect.width() - pos.x(); - setFontSize(22, width, - m_time_label->text(), font); - m_time_label->setFont(font);*/ - m_time_label->moveTo(pos); - } - - m_player_name->moveTo(QPoint( - static_cast(m_rect.width() * 0.05), - static_cast(m_rect.height() * 0.8))); - - moveTo(m_rect.topLeft()); -} - -void Clock::Info::start() { - m_running = true; - m_time.start(); -} - -void Clock::Info::stop() { - if (m_running) m_total_time -= m_time.elapsed(); - m_running = false; -} - -void Clock::Info::computeTime() const { - int time = m_total_time; - if (m_running) time -= m_time.elapsed(); - bool positive; - int total_secs; - int decs = -1; - - if (time > 0 && time < 10000) { - int total_decs = static_cast(ceil(time / 100.0)); - positive = total_decs >= 0; - if (!positive) total_decs = -total_decs; - decs = total_decs % 10; - total_secs = total_decs / 10; - } - else { - total_secs = static_cast(ceil(time / 1000.0)); - positive = total_secs >= 0; - if (!positive) total_secs = -total_secs; - } - - - int secs = total_secs % 60; - int mins = total_secs / 60; - QString timeText; - - { - QString secText = QString::number(secs); - if (secText.length() < 2) secText = "0" + secText; - - QString minText = QString::number(mins); - if (minText.length() < 2) minText = "0" + minText; - - timeText = minText + ":" + secText; - if (!positive) - timeText = "-" + timeText; - - if (positive && decs != -1) { - int dec = static_cast(ceil(time / 100.0)) % 10; - - m_decs->moveTo(m_time_label->rect().bottomRight() + QPoint(2, 0)); - m_decs->setText(":" + QString::number(dec)); - m_decs->show(); - } - else - m_decs->hide(); - } - - m_time_label->setText(timeText); - m_time_label->setColor(time <= 0 && m_running ? QColor(200,20,20) : Qt::black); -} - -void Clock::Info::tick() { - computeTime(); -} - -void Clock::Info::activate(bool value) { - m_background->setVisible(value); - for(int i=0;i<4;i++) - m_frame[i]->setVisible(value); - - QColor textcolor = value ? Qt::black : Qt::darkGray; - m_caption->setColor(textcolor); - m_time_label->setColor(textcolor); - m_decs->setColor(textcolor); - m_player_name->setColor(textcolor); -} - -QRect Clock::Info::eventRect() const { - return m_background->rect().translated(pos()); -} - - -Clock::Clock(KGameCanvasAbstract* parent) -: ClickableCanvas(parent) -, m_running(-1) -, m_active(-1) { - QTime startup_time; startup_time.start(); - m_info[0].setup(Player(), QRect(0, 0, 0, 0), "White", this); - m_info[1].setup(Player(), QRect(0, 0, 0, 0), "Black", this); - connect(&m_timer, SIGNAL(timeout()), this, SLOT(tick())); -} - -void Clock::settingsChanged() { - for(int i=0;i<2;i++) - m_info[i].settingsChanged(); -} - -void Clock::resize(QSize size) { - int baseWidth = (size.width() - 10) / 2; - m_info[0].resize(QRect(0, 0, baseWidth, 70)); - m_info[1].resize(QRect(baseWidth + 10, 0, baseWidth, 70)); -} - -void Clock::reload() { - m_info[0].reload(); - m_info[1].reload(); -} - -void Clock::setTime(int index, int value) { - Q_ASSERT(index == 0 || index == 1); - - m_info[index].setTime(value); -} - -void Clock::start(int index) { - Q_ASSERT(index == 0 || index == 1); - - m_timer.start(10); - m_running = index; - m_info[index].start(); - m_info[1 - index].stop(); -} - -void Clock::stop() { - m_info[0].stop(); - m_info[1].stop(); - m_timer.stop(); - m_running = -1; -} - -void Clock::activate(int index) { - m_active = index; - m_info[0].activate(index == 0); - m_info[1].activate(index == 1); -} - -void Clock::tick() { - if (m_running != -1) { - Q_ASSERT(m_running == 0 || m_running == 1); - m_info[m_running].tick(); - } -} - -void Clock::setPlayers(const Player& white, const Player& black) { - m_info[0].setPlayer(white); - m_info[1].setPlayer(black); -} - -void Clock::onMousePress(const QPoint& pos, int button) { - if (button == Qt::LeftButton) { - if (m_info[0].eventRect().contains(pos)) - labelClicked(0); - else if (m_info[1].eventRect().contains(pos)) - labelClicked(1); - } -} - -#endif +/* + Copyright (c) 2006 Paolo Capriotti + (c) 2006 Maurizio Monge + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. +*/ + +#include "clock.h" +#include "constrainedtext.h" +#include +#include + +Clock::Clock(int col, KGameCanvasAbstract* canvas) +: ClickableCanvas(canvas) +, m_color(col) +, m_running(false) { + m_background = new KGameCanvasPixmap(this); + m_caption = new ConstrainedText(this); + m_time_label = new ConstrainedText(this); + m_player_name = new ConstrainedText(this); + m_decs = new ConstrainedText(this); + + m_background->show(); + m_caption->show(); + m_time_label->show(); + m_player_name->show(); + + setTime(0); + setPlayer(Player()); + m_caption->setText(col == 0 ? "White" : "Black"); + connect(&m_timer, SIGNAL(timeout()), this, SLOT(tick())); +} + +Clock::~Clock() { + delete m_background; + delete m_caption; + delete m_time_label; + delete m_player_name; + delete m_decs; +} + +void Clock::start() { + m_running = true; + m_time.start(); + m_timer.start(100); +} + +void Clock::stop() { + if (m_running) m_total_time -= m_time.elapsed(); + m_running = false; + m_timer.stop(); +} + +void Clock::activate(bool a) { + if(m_active == a) + return; + + m_active = a; + m_background->setPixmap(m_active ? m_active_pixmap : m_inactive_pixmap); + + m_time_label->setColor(m_active ? m_active_text : m_inactive_text); + m_player_name->setColor(m_active ? m_active_text : m_inactive_text); + m_caption->setColor(m_active ? m_active_text : m_inactive_text); +} + +void Clock::tick() { + computeTime(); +} + +void Clock::computeTime() { + int time = m_total_time; + if (m_running) time -= m_time.elapsed(); + + bool positive; + int total_secs; + int decs = -1; + + if (time > 0 && time < 10000) { + int total_decs = static_cast(ceil(time / 100.0)); + positive = total_decs >= 0; + if (!positive) total_decs = -total_decs; + decs = total_decs % 10; + total_secs = total_decs / 10; + } + else { + total_secs = static_cast(ceil(time / 1000.0)); + positive = total_secs >= 0; + if (!positive) total_secs = -total_secs; + } + + + int secs = total_secs % 60; + int mins = total_secs / 60; + QString timeText; + + { + QString secText = QString::number(secs); + if (secText.length() < 2) secText = "0" + secText; + + QString minText = QString::number(mins); + if (minText.length() < 2) minText = "0" + minText; + + timeText = minText + ":" + secText; + if (!positive) + timeText = "-" + timeText; + +#if 0 + if (positive && decs != -1) { + int dec = static_cast(ceil(time / 100.0)) % 10; + + m_decs->moveTo(m_time_label->rect().bottomRight() + QPoint(2, 0)); + m_decs->setText(":" + QString::number(dec)); + m_decs->show(); + } + else + m_decs->hide(); +#endif + } + + m_time_label->setText(timeText); +} + +QString Clock::playerString(const Player& player) { + QString rating = player.rating != -1 ? QString(" (%1)").arg(player.rating) : QString(); + return QString("%1").arg(player.name) + rating; +} + +void Clock::setPlayer(const Player& player) { + m_player_name->setText(playerString(player)); +} + +void Clock::setTime(int t) { + m_total_time = t; + tick(); +} + +void Clock::onMousePress(const QPoint& /*pos*/, int /*button*/) { +} + +void Clock::resize(int size) { + m_controls_loader.setSize(size); + + m_height = (int)m_controls_loader.getValue("clock_height"); + + m_active_pixmap = m_controls_loader.getValue("clock_active_background"); + m_inactive_pixmap = m_controls_loader.getValue("clock_inactive_background"); + + m_active_text = m_controls_loader.getValue("clock_active_text").color(); + m_inactive_text = m_controls_loader.getValue("clock_inactive_text").color(); + + m_background->setPixmap(m_active ? m_active_pixmap : m_inactive_pixmap); + m_background->moveTo(m_controls_loader.getValue("clock_background_offset").toPoint()); + + m_time_label->setConstrainRect(m_controls_loader.getValue("clock_time_rect").toRect()); + m_time_label->setColor(m_active ? m_active_text : m_inactive_text); + + m_player_name->setConstrainRect(m_controls_loader.getValue("clock_player_rect").toRect()); + m_player_name->setColor(m_active ? m_active_text : m_inactive_text); + + m_caption->setConstrainRect(m_controls_loader.getValue("clock_caption_rect").toRect()); + m_caption->setColor(m_active ? m_active_text : m_inactive_text); +} + +void Clock::settingsChanged() { + m_caption->setFont(m_controls_loader.getStaticValue("clock_caption_font")); + m_player_name->setFont(m_controls_loader.getStaticValue("clock_player_font")); + m_time_label->setFont(m_controls_loader.getStaticValue("clock_time_font")); +} + diff --git a/src/clock.h b/src/clock.h index 2b19f9a..00c52f8 100644 --- a/src/clock.h +++ b/src/clock.h @@ -86,85 +86,4 @@ Q_SIGNALS: void labelClicked(int); }; - -#if 1-1 - -class Clock : public QObject, public ClickableCanvas { -Q_OBJECT - - /** - * Structure containing information for a player. - */ - class Info : public KGameCanvasGroup { - Player m_player; - QTime m_time; - bool m_running; - int m_total_time; - KGameCanvasRectangle* m_background; - KGameCanvasRectangle* m_frame[4]; - KGameCanvasText* m_caption; - KGameCanvasText* m_time_label; - KGameCanvasText* m_player_name; - KGameCanvasText* m_decs; - QRect m_rect; - - void computeTime() const; - void update(); - public: - Info() - : m_player(QString(), 0) - , m_running(false) - , m_rect(0, 0, 0, 0) { } - - void setup(const Player& player, const QRect& p, - const QString& caption, KGameCanvasAbstract* canvas); - void setTime(int); - void start(); - void stop(); - void activate(bool); - void tick(); - - QString playerString(const Player& player) const; - void setPlayer(const Player& player); - - void resize(const QRect& rect); - void reload(); - QRect eventRect() const; - - /** changed settings handler */ - virtual void settingsChanged(); - }; - - Info m_info[2]; - QTimer m_timer; - int m_running; - int m_active; -public: - Clock(KGameCanvasAbstract* parent); - - void setTime(int index, int value); - void start(int index); - void stop(); - void activate(int index); - - void setPlayers(const Player& white, const Player& black); - - bool running() const { return m_running != -1; } - void resize(QSize size); - void reload(); - - virtual void onMousePress(const QPoint& pos, int button); - virtual void onMouseRelease(const QPoint& /*pos*/, int /*button*/) { } - virtual void onMouseMove(const QPoint& /*pos*/, int /*button*/) { } - - /** changed settings handler */ - virtual void settingsChanged(); -private Q_SLOTS: - void tick(); -Q_SIGNALS: - void labelClicked(int); -}; - -#endif - #endif // CLOCK_H -- 2.11.4.GIT