KDEGames

kgamehighscoredialog.h
1/*
2 SPDX-FileCopyrightText: 1998 Sandro Sigala <ssigala@globalnet.it>
3 SPDX-FileCopyrightText: 2001 Waldo Bastian <bastian@kde.org>
4 SPDX-FileCopyrightText: 2007 Matt Williams <matt@milliams.com>
5
6 SPDX-License-Identifier: ICS
7*/
8
9#ifndef KGAMEHIGHSCOREDIALOG_H
10#define KGAMEHIGHSCOREDIALOG_H
11
12// own
13#include <kdegames_export.h>
14// Qt
15#include <QDialog>
16#include <QMap>
17// Std
18#include <memory>
19
20class KGameDifficulty;
21
22/**
23 * \class KGameHighScoreDialog kgamehighscoredialog.h <KGameHighScoreDialog>
24 *
25 * @short A simple high score implementation
26 *
27 * This class can be used both for displaying the current high scores
28 * and also for adding new highscores. It is the recommended way of
29 * implementing a simple highscore table.
30 *
31 * To display the current highscores it is simply a case of creating
32 * a KGameHighScoreDialog object and calling exec(). This example code will
33 * display the Name and Score (the score is always added automatically
34 * unless hidden @ref hideField since it is used for sorting) of the
35 * top 10 players:
36 * \code
37 * KGameHighScoreDialog ksdialog(this);
38 * ksdialog.exec();
39 * \endcode
40 *
41 * To add a new highscore, e.g. at the end of a game you simply create an
42 * object with the @ref Fields you want to write (i.e. KGameHighScoreDialog::Name |
43 * KGameHighScoreDialog::Score), call addScore and then (optionally) display
44 * the dialog.
45 * This code will allow you to add a highscore with a Name and Score
46 * field. If it's the first time a player has a score on the table, they
47 * will be prompted for their name but subsequent times they will have
48 * their name filled in automatically.
49 * \code
50 * KGameHighScoreDialog ksdialog(this);
51 * ksdialog.addScore(playersScore);
52 * ksdialog.exec();
53 * \endcode
54 *
55 * Or if you want to fill the name in from the code you can pass a default
56 * name by doing
57 * \code
58 * KGameHighScoreDialog::FieldInfo scoreInfo;
59 * scoreInfo[KGameHighScoreDialog::Name] = "Matt";
60 * scoreInfo[KGameHighScoreDialog::Score].setNum(playersScore);
61 * ksdialog.addScore(scoreInfo);
62 * \endcode
63 *
64 * If you want to add an extra field (e.g. the number of moves taken) then
65 * do
66 * \code
67 * KGameHighScoreDialog::FieldInfo scoreInfo;
68 * scoreInfo[KGameHighScoreDialog::Name] = "Matt";
69 * scoreInfo[KGameHighScoreDialog::Score].setNum(playersScore);
70 *
71 * ksdialog.addField(KGameHighScoreDialog::Custom1, "Num of Moves", "moves");
72 * scoreInfo[KGameHighScoreDialog::Custom1].setNum(42);
73 *
74 * ksdialog.addScore(scoreInfo);
75 * \endcode
76 * You can define up to 5 Custom fields.
77 * @author Matt Williams <matt@milliams.com>
78 */
79class KDEGAMES_EXPORT KGameHighScoreDialog : public QDialog
80{
81 Q_OBJECT
82
83public:
84 /// Highscore fields
85 enum Fields {
86 Name = 1 << 0,
87 Level = 1 << 1,
88 Date = 1 << 2,
89 Time = 1 << 3,
90 Score = 1 << 4,
91
92 Custom1 = 1 << 10, ///< Field for custom information
93 Custom2 = 1 << 11,
94 Custom3 = 1 << 12,
95 Custom4 = 1 << 13,
96 Custom5 = 1 << 14,
97
98 Max = 1 << 30 ///< Only for setting a maximum
99 };
100
101 /// Flags for setting preferences for adding scores
103 AskName = 0x1, /**< Promt the player for their name */
104 LessIsMore = 0x2 /**< A lower numerical score means higher placing on the table */
105 };
106 /**
107 * Stores a combination of #AddScoreFlag values.
108 */
109 Q_DECLARE_FLAGS(AddScoreFlags, AddScoreFlag)
110
112
113 /**
114 * @param fields Bitwise OR of the @ref Fields that should be listed (Score is always present)
115 * @param parent passed to parent QWidget constructor.
116 */
117 explicit KGameHighScoreDialog(int fields = Name, QWidget *parent = nullptr);
118
119 ~KGameHighScoreDialog() override;
120
121 /**
122 * The group name must be passed though i18n() in order for the
123 * group name to be translated. i.e.
124 * \code ksdialog.setConfigGroup(qMakePair(QByteArrayLiteral("Easy"), i18n("Easy"))); \endcode
125 * If you set a group, it will be prefixed in the config file by
126 * 'KHighscore_' otherwise the group will simply be 'KHighscore'.
127 *
128 * @param group to use for reading/writing highscores from/to.
129 */
130 void setConfigGroup(const QPair<QByteArray, QString> &group);
131
132 /**
133 * You must add the translations of all group names to the dialog. This
134 * is best done by passing the name through i18n().
135 * The group set through setConfigGroup(const QPair<QByteArray, QString>& group)
136 * will be added automatically
137 *
138 * @param group the translated group name
139 */
140 void addLocalizedConfigGroupName(const QPair<QByteArray, QString> &group);
141
142 /**
143 * You must add the translations of all group names to the dialog. This
144 * is best done by passing the name through i18n().
145 * The group set through setConfigGroup(const QPair<QByteArray, QString>& group)
146 * will be added automatically.
147 *
148 * This function can be used directly with KGameDifficulty::localizedLevelStrings().
149 *
150 * @param groups the list of translated group names
151 */
152 void addLocalizedConfigGroupNames(const QMap<QByteArray, QString> &groups);
153
154 /**
155 * Hide some config groups so that they are not shown on the dialog
156 * (but are still stored in the configuration file).
157 * \code
158 * ksdialog.setHiddenConfigGroups(QList<QByteArray>{QByteArrayLiteral("Very Easy"), QByteArrayLiteral("Easy")});
159 * \endcode
160 *
161 * @param hiddenGroups the list of group names you want to hide
162 *
163 * @since KDE 4.6
164 */
165 void setHiddenConfigGroups(const QList<QByteArray> &hiddenGroups);
166
167 /**
168 * It is a good idea giving config group weights, otherwise tabs
169 * get ordered by their tab name that is not probably what you want.
170 *
171 * This function can be used directly with KGameDifficulty::levelWeights().
172 *
173 * @param weights the list of untranslated group weights
174 *
175 * @since KDE 4.2
176 */
177 void setConfigGroupWeights(const QMap<int, QByteArray> &weights);
178
179 /**
180 * @param comment to add when showing high-scores.
181 * The comment is only used once.
182 */
183 void setComment(const QString &comment);
184
185 /**
186 * Define an extra FieldInfo entry.
187 * @param field id of this field @ref Fields e.g. KGameHighScoreDialog::Custom1
188 * @param header text shown in the header in the dialog for this field. e.g. "Number of Moves"
189 * @param key unique key used to store this field. e.g. "moves"
190 */
191 void addField(int field, const QString &header, const QString &key);
192
193 /**
194 * Hide a field so that it is not shown on the table (but is still stored in the configuration file).
195 * @param field id of this field @ref Fields e.g. KGameHighScoreDialog::Score
196 */
197 void hideField(int field);
198
199 /**
200 * Adds a new score to the list.
201 *
202 * @param newInfo info about the score.
203 * @param flags set whether the user should be prompted for their name and how the scores should be sorted
204 *
205 * @returns The highscore position if the score was good enough to
206 * make it into the list (1 being topscore) or 0 otherwise.
207 */
208 int addScore(const FieldInfo &newInfo = FieldInfo(), AddScoreFlags flags = {});
209
210 /**
211 * Convenience function for ease of use.
212 *
213 * @param newScore the score of the player.
214 * @param flags set whether the user should be prompted for their name and how the scores should be sorted
215 *
216 * @returns The highscore position if the score was good enough to
217 * make it into the list (1 being topscore) or 0 otherwise.
218 */
219 int addScore(int newScore, AddScoreFlags flags = {});
220
221 /**
222 * @returns the current best score in the group
223 */
224 int highScore();
225
226 /**
227 * Assume that config groups (incl. current selection) are equal to
228 * difficulty levels, and initialize them. This is usually equal to the
229 * following code using KGameDifficulty:
230 * @code
231 * addLocalizedConfigGroupNames(KGameDifficulty::localizedLevelStrings());
232 * setConfigGroupWeights(KGameDifficulty::levelWeights());
233 * setConfigGroup(KGameDifficulty::localizedLevelString());
234 * @endcode
235 */
236 void initFromDifficulty(const KGameDifficulty *difficulty, bool setConfigGroup = true);
237
238 /// Display the dialog as non-modal
239 virtual void show();
240 /// Display the dialog as modal
241 int exec() override;
242
243private Q_SLOTS:
244 void slotGotReturn();
245 void slotGotName();
246 void slotForgetScore();
247
248private:
249 void keyPressEvent(QKeyEvent *ev) override;
250
251private:
252 friend class KGameHighScoreDialogPrivate;
253 std::unique_ptr<class KGameHighScoreDialogPrivate> const d_ptr;
254 Q_DECLARE_PRIVATE(KGameHighScoreDialog)
255};
256
257Q_DECLARE_OPERATORS_FOR_FLAGS(KGameHighScoreDialog::AddScoreFlags)
258
259#endif // KGAMEHIGHSCOREDIALOG_H
KGameDifficulty manages difficulty levels of a game in a standard way.
A simple high score implementation.
AddScoreFlag
Flags for setting preferences for adding scores.
virtual int exec()
virtual void keyPressEvent(QKeyEvent *e) override
Q_SLOTSQ_SLOTS
void show()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:13:43 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.