[games/chessament] src: Show time control format
Manuel Alcaraz Zambrano <[email protected]>
| Newsgroups | gmane.comp.kde.cvs |
|---|---|
| Message-ID | <[email protected]> |
Git commit 478f939c942196c666f8715fc8cbbdb32d9a52d9 by Manuel Alcaraz Zambrano.
Committed on 28/07/2026 at 14:17.
Pushed by manuelal into branch 'master'.
Show time control format
M +6 -0 src/qml/TournamentSettingsFormat.qml
M +22 -0 src/timecontrolmodel.cpp
M +5 -1 src/timecontrolmodel.h
M +26 -0 src/tournament/timecontrol.cpp
M +10 -0 src/tournament/timecontrol.h
https://invent.kde.org/games/chessament/-/commit/478f939c942196c666f8715fc8cbbdb32d9a52d9
diff --git a/src/qml/TournamentSettingsFormat.qml b/src/qml/TournamentSettingsFormat.qml
index fab475f..2f606f5 100644
--- a/src/qml/TournamentSettingsFormat.qml
+++ b/src/qml/TournamentSettingsFormat.qml
@@ -82,6 +82,12 @@ FormCard.FormCardPage {
count: timeControlRepeater.count
}
}
+
+ FormCard.FormDelegateSeparator {}
+
+ FormCard.FormTextDelegate {
+ text: timeControlModel.format
+ }
}
FormCard.FormHeader {
diff --git a/src/timecontrolmodel.cpp b/src/timecontrolmodel.cpp
index d069095..e94d17a 100644
--- a/src/timecontrolmodel.cpp
+++ b/src/timecontrolmodel.cpp
@@ -22,6 +22,27 @@ void TimeControlModel::setTournament(Tournament *tournament)
}
m_tournament = tournament;
Q_EMIT tournamentChanged();
+ Q_EMIT formatChanged();
+}
+
+QString TimeControlModel::format() const
+{
+ if (m_tournament == nullptr) {
+ return {};
+ }
+
+ const auto format = m_tournament->timeControl().format();
+
+ switch (format) {
+ case TimeControl::Format::Blitz:
+ return i18nc("@info", "The time control is Blitz.");
+ case TimeControl::Format::Rapid:
+ return i18nc("@info", "The time control is Rapid.");
+ case TimeControl::Format::Classical:
+ return i18nc("@info", "The time control is Classical.");
+ }
+
+ Q_UNREACHABLE();
}
int TimeControlModel::rowCount(const QModelIndex &parent) const
@@ -76,6 +97,7 @@ bool TimeControlModel::setData(const QModelIndex &index, const QVariant &value,
m_tournament->saveTimeControl();
Q_EMIT dataChanged(this->index(index.row()), this->index(index.row()));
+ Q_EMIT formatChanged();
return true;
}
diff --git a/src/timecontrolmodel.h b/src/timecontrolmodel.h
index 48a389e..fb270f9 100644
--- a/src/timecontrolmodel.h
+++ b/src/timecontrolmodel.h
@@ -14,6 +14,7 @@ class TimeControlModel : public QAbstractListModel
QML_ELEMENT
Q_PROPERTY(Tournament *tournament READ tournament WRITE setTournament NOTIFY tournamentChanged)
+ Q_PROPERTY(QString format READ format NOTIFY formatChanged)
public:
enum Roles {
@@ -27,6 +28,8 @@ public:
Tournament *tournament();
+ [[nodiscard]] QString format() const;
+
[[nodiscard]] int rowCount(const QModelIndex &parent = {}) const override;
[[nodiscard]] QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
@@ -40,8 +43,9 @@ public Q_SLOTS:
Q_SIGNALS:
void tournamentChanged();
+ void formatChanged();
void errorOcurred(const QString &error);
private:
- Tournament *m_tournament;
+ Tournament *m_tournament = nullptr;
};
diff --git a/src/tournament/timecontrol.cpp b/src/tournament/timecontrol.cpp
index ea0bef5..2f32803 100644
--- a/src/tournament/timecontrol.cpp
+++ b/src/tournament/timecontrol.cpp
@@ -5,7 +5,9 @@
#include "utils.h"
#include <QJsonArray>
+#include <chrono>
+using namespace std::chrono_literals;
using namespace Qt::StringLiterals;
TimeControlPeriod::TimeControlPeriod(std::optional<int> moves, int time, int increment)
@@ -157,6 +159,30 @@ void TimeControl::removePeriod(int index)
value = periods;
}
+std::chrono::seconds TimeControl::durationPerPlayer() const
+{
+ std::chrono::seconds duration{};
+
+ for (const auto period : periods()) {
+ duration += std::chrono::seconds(period.time() + (period.increment() * 60));
+ }
+
+ return duration;
+}
+
+TimeControl::Format TimeControl::format() const
+{
+ const auto duration = durationPerPlayer();
+
+ if (duration <= 10min) {
+ return TimeControl::Format::Blitz;
+ }
+ if (duration < 60min) {
+ return TimeControl::Format::Rapid;
+ }
+ return TimeControl::Format::Classical;
+}
+
QJsonObject TimeControl::json()
{
return m_json;
diff --git a/src/tournament/timecontrol.h b/src/tournament/timecontrol.h
index d034f8e..eea418b 100644
--- a/src/tournament/timecontrol.h
+++ b/src/tournament/timecontrol.h
@@ -32,6 +32,12 @@ private:
};
struct TimeControl {
+ enum class Format {
+ Blitz,
+ Rapid,
+ Classical,
+ };
+
explicit TimeControl() = default;
explicit TimeControl(std::initializer_list<TimeControlPeriod> periods);
@@ -46,6 +52,10 @@ struct TimeControl {
void removePeriod(int index);
+ [[nodiscard]] std::chrono::seconds durationPerPlayer() const;
+
+ [[nodiscard]] TimeControl::Format format() const;
+
QJsonObject json();
[[nodiscard]] QString toTrf() const;