1 /*
2 	SPDX-FileCopyrightText: 2009-2021 Graeme Gott <graeme@gottcode.org>
3 
4 	SPDX-License-Identifier: GPL-3.0-or-later
5 */
6 
7 #ifndef TANGLET_CLOCK_H
8 #define TANGLET_CLOCK_H
9 
10 #include <QWidget>
11 class QSettings;
12 class QTimer;
13 
14 /**
15  * @brief The Clock class manages and displays how much time is left.
16  */
17 class Clock : public QWidget
18 {
19 	Q_OBJECT
20 
21 	class Timer;
22 	class AllotmentTimer;
23 	class ClassicTimer;
24 	class DisciplineTimer;
25 	class RefillTimer;
26 	class StaminaTimer;
27 	class StrikeoutTimer;
28 	class TangletTimer;
29 	class UnlimitedTimer;
30 
31 public:
32 	/** The different timer modes that control the clock. */
33 	enum Mode
34 	{
35 		Tanglet,
36 		Classic,
37 		Refill,
38 		Stamina,
39 		Strikeout,
40 		Allotment,
41 		Discipline,
42 		Unlimited,
43 		TotalTimers
44 	};
45 
46 	/**
47 	 * Constructs a clock instance.
48 	 * @param parent the QWidget that manages the clock
49 	 */
50 	explicit Clock(QWidget* parent = nullptr);
51 
52 	/**
53 	 * Destroys the clock.
54 	 */
55 	~Clock();
56 
57 	/**
58 	 * @return the size required to show the clock
59 	 */
60 	QSize sizeHint() const override;
61 
62 	/**
63 	 * @return whether the current timer has finished
64 	 */
65 	bool isFinished() const;
66 
67 	/**
68 	 * Updates the clock based on a correct guess.
69 	 * @param score how many points the guess was worth
70 	 */
71 	void addWord(int score);
72 
73 	/**
74 	 * Updates the clock based on an incorrect guess.
75 	 * @param score how many points the guess was worth
76 	 */
77 	void addIncorrectWord(int score);
78 
79 	/**
80 	 * Pauses or resumes the clock.
81 	 * @param paused pauses the clock if @c true
82 	 */
83 	void setPaused(bool paused);
84 
85 	/**
86 	 * Override the text display of the clock. Used to inform the player a game has ended.
87 	 * @param text the text to display
88 	 */
89 	void setText(const QString& text);
90 
91 	/**
92 	 * Starts the clock.
93 	 */
94 	void start();
95 
96 	/**
97 	 * Stops the clock and resets it to @c 0.
98 	 */
99 	void stop();
100 
101 	/**
102 	 * Loads the clock details.
103 	 * @param game where to read the clock details
104 	 */
105 	void load(const QSettings& game);
106 
107 	/**
108 	 * Saves the clock details.
109 	 * @param game where to store the clock details
110 	 */
111 	void save(QSettings& game);
112 
113 	/**
114 	 * Configures how the clock tracks time.
115 	 * @param timer the timer mode
116 	 */
117 	void setTimer(int timer);
118 
119 	/**
120 	 * @return the current timer mode
121 	 */
122 	int timer() const;
123 
124 	/**
125 	 * Fetch the translated name of a timer.
126 	 * @param timer the timer mode
127 	 * @return translated name of timer
128 	 */
129 	static QString timerToString(int timer);
130 
131 	/**
132 	 * Fetch the translated description of a timer.
133 	 * @param timer the timer mode
134 	 * @return translated description of timer
135 	 */
136 	static QString timerDescription(int timer);
137 
138 	/**
139 	 * Fetch the QSettings group name of a timer.
140 	 * @param timer the timer mode
141 	 * @return the QSettings group name of a timer
142 	 */
143 	static QString timerScoresGroup(int timer);
144 
145 signals:
146 	/**
147 	 * Emitted when the clock reaches @c 0.
148 	 */
149 	void finished();
150 
151 protected:
152 	/**
153 	 * Draws the clock.
154 	 * @param event the QWidget paint event details
155 	 */
156 	void paintEvent(QPaintEvent* event) override;
157 
158 private slots:
159 	/**
160 	 * Calculates the current time remaining. If finished, it calls stop() to clear the time and
161 	 * emits the finished() signal to end the game.
162 	 */
163 	void updateTime();
164 
165 private:
166 	QString m_text; /**< the string to display */
167 	QTimer* m_update; /**< the tick used to update the timer */
168 
169 	Timer* m_timer; /**< the current timer */
170 };
171 
172 #endif // TANGLET_CLOCK_H
173