1 /* ColorCode, a free MasterMind clone with built in solver
2 * Copyright (C) 2009 Dirk Laebisch
3 * http://www.laebisch.com/
4 *
5 * ColorCode 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 3 of the License, or
8 * (at your option) any later version.
9 *
10 * ColorCode is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with ColorCode. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include <QtWidgets>
20 #include "timedisplay.h"
21 #include "gametimer.h"
22
23
TimeDisplay(QGraphicsItem * parent)24 TimeDisplay::TimeDisplay(QGraphicsItem* parent) : QGraphicsTextItem(parent)
25 {
26 setAcceptedMouseButtons(Qt::NoButton);
27 setAcceptHoverEvents(false);
28
29 mFont = QFont("Arial", 18, QFont::Normal, false);
30 mFont.setStyleHint(QFont::SansSerif);
31 mFont.setPixelSize(24);
32 setFont(mFont);
33 setDefaultTextColor(QColor("#d7d8da"));
34
35 mShowTenth = true;
36 mSs = mLastSs = "";
37 mHs = "";
38
39 mInterval = 50;
40 mPaused = false;
41 mStopped = true;
42 mGameTime = 0;
43 mTime = 0;
44
45 mTimer = new GameTimer(this);
46 connect(mTimer, SIGNAL(TimeOutSignal(int)), this, SLOT(DisplayTime(int)));
47 mTimer->Start();
48
49 setTextWidth(440);
50 setScale(0.5);
51 }
52
~TimeDisplay()53 TimeDisplay::~TimeDisplay()
54 {
55 }
56
GetGameTime() const57 int TimeDisplay::GetGameTime() const
58 {
59 return mGameTime;
60 }
61
IsActive() const62 bool TimeDisplay::IsActive() const
63 {
64 return (!mPaused && !mStopped);
65 }
66
IsPaused() const67 bool TimeDisplay::IsPaused() const
68 {
69 return mPaused;
70 }
71
IsStopped() const72 bool TimeDisplay::IsStopped() const
73 {
74 return mPaused;
75 }
76
SetTenth(bool b)77 void TimeDisplay::SetTenth(bool b)
78 {
79 mShowTenth = b;
80 }
81
StartT()82 void TimeDisplay::StartT()
83 {
84 mTime = 0;
85 mT0.start();
86 DisplayTime();
87 mPaused = false;
88 mStopped = false;
89 }
90
PauseT()91 void TimeDisplay::PauseT()
92 {
93 if (!mStopped && !mPaused)
94 {
95 mTime += mT0.restart();
96 DisplayTime();
97 mPaused = true;
98 }
99 }
100
ResumeT()101 void TimeDisplay::ResumeT()
102 {
103 if (mPaused && !mStopped)
104 {
105 mT0.restart();
106 mPaused = false;
107 }
108 }
109
StopT()110 void TimeDisplay::StopT()
111 {
112 if (!mStopped && !mPaused)
113 {
114 mTime += mT0.restart();
115 DisplayTime();
116 mStopped = true;
117 mPaused = false;
118 mTime = 0;
119 }
120 }
121
DisplayTime(int tick)122 void TimeDisplay::DisplayTime(int tick)
123 {
124 if (mPaused || mStopped)
125 {
126 return;
127 }
128
129 mGameTime = mT0.elapsed() + mTime;
130 mT1 = QTime(0, 0, 0, 0);
131 mT1 = mT1.addMSecs(mGameTime);
132
133 if (mShowTenth)
134 {
135 if (tick == 0)
136 {
137 mHs = QString::number((int)(mT1.msec()/10));
138 }
139 else
140 {
141 mHs = QString::number(((int)(mT1.msec()/50))*5);
142 }
143 if (mHs.length() < 2)
144 {
145 mHs.prepend("0");
146 }
147 }
148
149 if (isVisible())
150 {
151 QString tstr;
152 if (!mShowTenth)
153 {
154 mSs = mT1.toString("ss");
155 if (mSs == mLastSs)
156 {
157 return;
158 }
159 tstr = QString("<table align='center' cellpadding='0' cellspacing='0' border='0'><tr><td width='30' align='center'>%1</td><td width='10' align='center'>:</td><td width='30' align='center'>%2</td><td width='10' align='center'>:</td><td width='30' align='center'>%3</td></tr></table>").arg( mT1.toString("hh"), mT1.toString("mm"), mSs);
160 mLastSs = mSs;
161 }
162 else
163 {
164 tstr = QString("<table align='center' cellpadding='0' cellspacing='0' border='0'><tr><td width='30' align='center'>%1</td><td width='10' align='center'>:</td><td width='30' align='center'>%2</td><td width='10' align='center'>:</td><td width='30' align='center'>%3</td><td width='10' align='center'>:</td><td width='30' align='center'>%4</td></tr></table>").arg( mT1.toString("hh"), mT1.toString("mm"), mT1.toString("ss"), mHs);
165 }
166 setHtml(tstr);
167 }
168 }
169