File: timedisplay.cpp

package info (click to toggle)
colorcode 0.8.5-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 1,224 kB
  • sloc: cpp: 8,331; makefile: 57
file content (168 lines) | stat: -rw-r--r-- 4,081 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/* ColorCode, a free MasterMind clone with built in solver
 * Copyright (C) 2009  Dirk Laebisch
 * http://www.laebisch.com/
 *
 * ColorCode 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 3 of the License, or
 * (at your option) any later version.
 *
 * ColorCode is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with ColorCode. If not, see <http://www.gnu.org/licenses/>.
*/

#include <QtWidgets>
#include "timedisplay.h"
#include "gametimer.h"


TimeDisplay::TimeDisplay(QGraphicsItem* parent) : QGraphicsTextItem(parent)
{
    setAcceptedMouseButtons(Qt::NoButton);
    setAcceptHoverEvents(false);

    mFont = QFont("Arial", 18, QFont::Normal, false);
    mFont.setStyleHint(QFont::SansSerif);
    mFont.setPixelSize(24);
    setFont(mFont);
    setDefaultTextColor(QColor("#d7d8da"));

    mShowTenth = true;
    mSs = mLastSs = "";
    mHs = "";

    mInterval = 50;
    mPaused = false;
    mStopped = true;
    mGameTime = 0;
    mTime = 0;

    mTimer = new GameTimer(this);
    connect(mTimer, SIGNAL(TimeOutSignal(int)), this, SLOT(DisplayTime(int)));
    mTimer->Start();

    setTextWidth(440);
    setScale(0.5);
}

TimeDisplay::~TimeDisplay()
{
}

int TimeDisplay::GetGameTime() const
{
    return mGameTime;
}

bool TimeDisplay::IsActive() const
{
    return (!mPaused && !mStopped);
}

bool TimeDisplay::IsPaused() const
{
    return mPaused;
}

bool TimeDisplay::IsStopped() const
{
    return mPaused;
}

void TimeDisplay::SetTenth(bool b)
{
    mShowTenth = b;
}

void TimeDisplay::StartT()
{
    mTime = 0;
    mT0.start();
    DisplayTime();
    mPaused = false;
    mStopped = false;
}

void TimeDisplay::PauseT()
{
    if (!mStopped && !mPaused)
    {
        mTime += mT0.restart();
        DisplayTime();
        mPaused = true;
    }
}

void TimeDisplay::ResumeT()
{
    if (mPaused && !mStopped)
    {
        mT0.restart();
        mPaused = false;
    }
}

void TimeDisplay::StopT()
{
    if (!mStopped && !mPaused)
    {
        mTime += mT0.restart();
        DisplayTime();
        mStopped = true;
        mPaused = false;
        mTime = 0;
    }
}

void TimeDisplay::DisplayTime(int tick)
{
    if (mPaused || mStopped)
    {
        return;
    }

    mGameTime = mT0.elapsed() + mTime;
    mT1 = QTime(0, 0, 0, 0);
    mT1 = mT1.addMSecs(mGameTime);

    if (mShowTenth)
    {
        if (tick == 0)
        {
            mHs = QString::number((int)(mT1.msec()/10));
        }
        else
        {
            mHs = QString::number(((int)(mT1.msec()/50))*5);
        }
        if (mHs.length() < 2)
        {
            mHs.prepend("0");
        }
    }

    if (isVisible())
    {
        QString tstr;
        if (!mShowTenth)
        {
            mSs = mT1.toString("ss");
            if (mSs == mLastSs)
            {
                return;
            }
            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);
            mLastSs = mSs;
        }
        else
        {
            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);
        }
        setHtml(tstr);
    }
}