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.
13 #include <QResizeEvent>
17 static void setFontSize(int max
, int width
, const QString
& text
, QFont
& font
) {
18 font
.setPointSize(max
);
22 QFontMetrics
metrics(font
);
23 int fw
= metrics
.boundingRect(text
).width();
24 std::cout
<< "font metrics: " << tm
.elapsed() << std::endl
;
26 if (fw
<= width
) break;
28 font
.setPointSize(max
);
32 void Clock::Info::settingsChanged() {
35 void Clock::Info::setup(const Player
& player
, const QRect
& rect
, const QString
& caption
, KGameCanvasAbstract
* canvas
) {
42 Settings s_clock
= settings
.group("clock");
44 QColor
framecol(0x60,0x60,0x90);
45 QColor backgroundColor
;
46 (s_clock
["background"] |= QColor(0xa0,0xf0,0xd0,200)) >> backgroundColor
;
47 m_background
= new KGameCanvasRectangle(backgroundColor
, QSize(m_rect
.size()), this);
48 m_frame
[0] = new KGameCanvasRectangle(framecol
, QSize(m_rect
.width()-2,1), this);
49 m_frame
[0]->moveTo(1,0);
50 m_frame
[1] = new KGameCanvasRectangle(framecol
, QSize(m_rect
.width()-2,1), this);
51 m_frame
[1]->moveTo(0,m_rect
.height()-1);
52 m_frame
[2] = new KGameCanvasRectangle(framecol
, QSize(1,m_rect
.height()), this);
53 m_frame
[2]->moveTo(0,0);
54 m_frame
[3] = new KGameCanvasRectangle(framecol
, QSize(1,m_rect
.height()), this);
55 m_frame
[3]->moveTo(m_rect
.width()-1,0);
60 QFont
captionFont("Bitstream Vera Sans");
61 (s_clock
["captionFontSize"] |=
62 static_cast<int>(captionFont
.pointSize() * 1.4)) >> tempFontSize
;
63 captionFont
.setPointSize(tempFontSize
);
64 m_caption
= new KGameCanvasText(caption
, Qt::black
, captionFont
,
65 KGameCanvasText::HStart
, KGameCanvasText::VTop
, this);
70 QFont
timeFont("Bitstream Vera Sans");
71 (s_clock
["timeFontSize"] |= timeFont
.pointSize() * 2) >> tempFontSize
;
72 timeFont
.setPointSize(tempFontSize
);
73 timeFont
.setWeight(QFont::Bold
);
74 m_time_label
= new KGameCanvasText("", Qt::black
, timeFont
,
75 KGameCanvasText::HStart
, KGameCanvasText::VCenter
, this);
80 QFont
decsFont("Bitstream Vera Sans");
81 (s_clock
["decsFontSize"] |=
82 static_cast<int>(decsFont
.pointSize() * 0.8)) >> tempFontSize
;
83 decsFont
.setPointSize(tempFontSize
);
84 m_decs
= new KGameCanvasText("", Qt::black
, decsFont
,
85 KGameCanvasText::HStart
, KGameCanvasText::VBottom
, this);
89 QFont
playerFont("Bitstream Vera Sans");
90 (s_clock
["playerFontSize"] |= playerFont
.pointSize()) >> tempFontSize
;
91 playerFont
.setPointSize(tempFontSize
);
92 m_player_name
= new KGameCanvasText(playerString(player
), Qt::black
, playerFont
,
93 KGameCanvasText::HStart
, KGameCanvasText::VBottom
, this);
94 m_player_name
->show();
102 void Clock::Info::reload() {
103 Settings s_clock
= settings
.group("clock");
106 QColor backgroundColor
;
108 s_clock
["background"] >> backgroundColor
;
109 m_background
->setColor(backgroundColor
);
111 tempFont
= m_caption
->font();
112 tempFont
.setPointSize(s_clock
["captionFontSize"].value
<int>());
113 m_caption
->setFont(tempFont
);
115 tempFont
= m_time_label
->font();
116 tempFont
.setPointSize(s_clock
["timeFontSize"].value
<int>());
117 m_time_label
->setFont(tempFont
);
119 tempFont
= m_decs
->font();
120 tempFont
.setPointSize(s_clock
["decsFontSize"].value
<int>());
121 m_decs
->setFont(tempFont
);
123 tempFont
= m_player_name
->font();
124 tempFont
.setPointSize(s_clock
["playerFontSize"].value
<int>());
125 m_player_name
->setFont(tempFont
);
128 QString
Clock::Info::playerString(const Player
& player
) const {
129 QString rating
= player
.rating
!= -1 ? QString(" (%1)").arg(player
.rating
) : "";
130 return QString("%1").arg(player
.name
) + rating
;
133 void Clock::Info::setPlayer(const Player
& player
) {
134 m_player_name
->setText(playerString(player
));
137 void Clock::Info::setTime(int time
) {
142 void Clock::Info::resize(const QRect
& rect
) {
147 void Clock::Info::update() {
148 m_background
->setSize(m_rect
.size());
150 m_frame
[0]->setSize(QSize(m_rect
.width()-2,1));
151 m_frame
[0]->moveTo(1,0);
152 m_frame
[1]->setSize(QSize(m_rect
.width()-2,1));
153 m_frame
[1]->moveTo(1,m_rect
.height()-1);
154 m_frame
[2]->setSize(QSize(1,m_rect
.height()));
155 m_frame
[2]->moveTo(0,0);
156 m_frame
[3]->setSize(QSize(1,m_rect
.height()));
157 m_frame
[3]->moveTo(m_rect
.width()-1,0);
160 /*QFont font = m_caption->font();
161 setFontSize(20, m_rect.width() / 2, m_caption->text(), font);
162 m_caption->setFont(font);*/
163 m_caption
->moveTo(QPoint(10, 10));
168 static_cast<int>(m_rect
.width() * 0.5),
169 static_cast<int>(m_rect
.height() * 0.5));
170 /*QFont font = m_time_label->font();
171 int width = m_rect.width() - pos.x();
172 setFontSize(22, width,
173 m_time_label->text(), font);
174 m_time_label->setFont(font);*/
175 m_time_label
->moveTo(pos
);
178 m_player_name
->moveTo(QPoint(
179 static_cast<int>(m_rect
.width() * 0.05),
180 static_cast<int>(m_rect
.height() * 0.8)));
182 moveTo(m_rect
.topLeft());
185 void Clock::Info::start() {
190 void Clock::Info::stop() {
191 if (m_running
) m_total_time
-= m_time
.elapsed();
195 void Clock::Info::computeTime() const {
196 int time
= m_total_time
;
197 if (m_running
) time
-= m_time
.elapsed();
202 if (time
> 0 && time
< 10000) {
203 int total_decs
= static_cast<int>(ceil(time
/ 100.0));
204 positive
= total_decs
>= 0;
205 if (!positive
) total_decs
= -total_decs
;
206 decs
= total_decs
% 10;
207 total_secs
= total_decs
/ 10;
210 total_secs
= static_cast<int>(ceil(time
/ 1000.0));
211 positive
= total_secs
>= 0;
212 if (!positive
) total_secs
= -total_secs
;
216 int secs
= total_secs
% 60;
217 int mins
= total_secs
/ 60;
221 QString secText
= QString::number(secs
);
222 if (secText
.length() < 2) secText
= "0" + secText
;
224 QString minText
= QString::number(mins
);
225 if (minText
.length() < 2) minText
= "0" + minText
;
227 timeText
= minText
+ ":" + secText
;
229 timeText
= "-" + timeText
;
231 if (positive
&& decs
!= -1) {
232 int dec
= static_cast<int>(ceil(time
/ 100.0)) % 10;
234 m_decs
->moveTo(m_time_label
->rect().bottomRight() + QPoint(2, 0));
235 m_decs
->setText(":" + QString::number(dec
));
242 m_time_label
->setText(timeText
);
243 m_time_label
->setColor(time
<= 0 && m_running
? QColor(200,20,20) : Qt::black
);
246 void Clock::Info::tick() {
250 void Clock::Info::activate(bool value
) {
251 m_background
->setVisible(value
);
253 m_frame
[i
]->setVisible(value
);
255 QColor textcolor
= value
? Qt::black
: Qt::darkGray
;
256 m_caption
->setColor(textcolor
);
257 m_time_label
->setColor(textcolor
);
258 m_decs
->setColor(textcolor
);
259 m_player_name
->setColor(textcolor
);
262 QRect
Clock::Info::eventRect() const {
263 return m_background
->rect().translated(pos());
267 Clock::Clock(KGameCanvasAbstract
* parent
)
268 : ClickableCanvas(parent
)
271 QTime startup_time
; startup_time
.start();
272 m_info
[0].setup(Player(), QRect(0, 0, 0, 0), "White", this);
273 m_info
[1].setup(Player(), QRect(0, 0, 0, 0), "Black", this);
274 connect(&m_timer
, SIGNAL(timeout()), this, SLOT(tick()));
277 void Clock::settingsChanged() {
279 m_info
[i
].settingsChanged();
282 void Clock::resize(QSize size
) {
283 int baseWidth
= (size
.width() - 10) / 2;
284 m_info
[0].resize(QRect(0, 0, baseWidth
, 70));
285 m_info
[1].resize(QRect(baseWidth
+ 10, 0, baseWidth
, 70));
288 void Clock::reload() {
293 void Clock::setTime(int index
, int value
) {
294 Q_ASSERT(index
== 0 || index
== 1);
296 m_info
[index
].setTime(value
);
299 void Clock::start(int index
) {
300 Q_ASSERT(index
== 0 || index
== 1);
304 m_info
[index
].start();
305 m_info
[1 - index
].stop();
315 void Clock::activate(int index
) {
317 m_info
[0].activate(index
== 0);
318 m_info
[1].activate(index
== 1);
322 if (m_running
!= -1) {
323 Q_ASSERT(m_running
== 0 || m_running
== 1);
324 m_info
[m_running
].tick();
328 void Clock::setPlayers(const Player
& white
, const Player
& black
) {
329 m_info
[0].setPlayer(white
);
330 m_info
[1].setPlayer(black
);
333 void Clock::onMousePress(const QPoint
& pos
, int button
) {
334 if (button
== Qt::LeftButton
) {
335 if (m_info
[0].eventRect().contains(pos
))
336 emit
labelClicked(0);
337 else if (m_info
[1].eventRect().contains(pos
))
338 emit
labelClicked(1);