From 0636f93e1688e73f0af7f59e2596f5b658be314f Mon Sep 17 00:00:00 2001 From: inix Date: Fri, 10 Oct 2025 07:00:26 +0200 Subject: [PATCH] move set_play_time dialog to its own class/ui file --- src/yuzu/main.cpp | 76 ++---------------- src/yuzu/set_play_time_dialog.cpp | 49 ++++++++++++ src/yuzu/set_play_time_dialog.h | 27 +++++++ src/yuzu/set_play_time_dialog.ui | 123 ++++++++++++++++++++++++++++++ 4 files changed, 205 insertions(+), 70 deletions(-) create mode 100644 src/yuzu/set_play_time_dialog.cpp create mode 100644 src/yuzu/set_play_time_dialog.h create mode 100644 src/yuzu/set_play_time_dialog.ui diff --git a/src/yuzu/main.cpp b/src/yuzu/main.cpp index 2111060a6f..a3d6e0a085 100644 --- a/src/yuzu/main.cpp +++ b/src/yuzu/main.cpp @@ -15,6 +15,8 @@ #include #include +#include "set_play_time_dialog.h" + #ifdef __APPLE__ #include // for chdir #endif @@ -2637,84 +2639,18 @@ void GMainWindow::OnGameListRemoveFile(u64 program_id, QtCommon::Game::GameListR } void GMainWindow::OnGameListSetPlayTime(u64 program_id) { - QDialog dialog(this); - dialog.setWindowTitle(tr("Set Play Time Data")); - dialog.setModal(true); - - auto* layout = new QVBoxLayout(&dialog); - auto* input_layout = new QHBoxLayout(); - auto* hours_edit = new QLineEdit(&dialog); - auto* minutes_edit = new QLineEdit(&dialog); - auto* seconds_edit = new QLineEdit(&dialog); - - hours_edit->setValidator(new QIntValidator(0, 9999, hours_edit)); - minutes_edit->setValidator(new QIntValidator(0, 59, minutes_edit)); - seconds_edit->setValidator(new QIntValidator(0, 59, seconds_edit)); - - using QtCommon::PlayTimeManager::TimeUnit; const u64 current_play_time = play_time_manager->GetPlayTime(program_id); - auto getTimeUnit = [current_play_time](TimeUnit unit) { - return QtCommon::PlayTimeManager::GetPlayTimeUnit(current_play_time, unit); - }; - hours_edit->setText(getTimeUnit(TimeUnit::Hours)); - minutes_edit->setText(getTimeUnit(TimeUnit::Minutes)); - seconds_edit->setText(getTimeUnit(TimeUnit::Seconds)); + SetPlayTimeDialog dialog(this, current_play_time); - input_layout->addWidget(new QLabel(tr("Hours:"), &dialog)); - input_layout->addWidget(hours_edit); - input_layout->addWidget(new QLabel(tr("Minutes:"), &dialog)); - input_layout->addWidget(minutes_edit); - input_layout->addWidget(new QLabel(tr("Seconds:"), &dialog)); - input_layout->addWidget(seconds_edit); - - layout->addLayout(input_layout); - - auto* error_label = new QLabel(&dialog); - error_label->setStyleSheet(QStringLiteral("QLabel { color : red; }")); - error_label->setVisible(false); - layout->addWidget(error_label); - - auto* button_box = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, &dialog); - layout->addWidget(button_box); - - connect(button_box, &QDialogButtonBox::accepted, [&]() { - const int hours = hours_edit->text().toInt(); - const int minutes = minutes_edit->text().toInt(); - const int seconds = seconds_edit->text().toInt(); - - if (hours < 0 || hours > 9999) { - error_label->setText(tr("Hours must be between 0 and 9999.")); - error_label->setVisible(true); - return; - } - - if (minutes < 0 || minutes > 59) { - error_label->setText(tr("Minutes must be between 0 and 59.")); - error_label->setVisible(true); - return; - } - - if (seconds < 0 || seconds > 59) { - error_label->setText(tr("Seconds must be between 0 and 59.")); - error_label->setVisible(true); - return; - } - - u64 total_seconds = static_cast(hours) * 3600 + static_cast(minutes) * 60 + static_cast(seconds); + if (dialog.exec() == QDialog::Accepted) { + const u64 total_seconds = dialog.GetTotalSeconds(); play_time_manager->SetPlayTime(program_id, total_seconds); game_list->PopulateAsync(UISettings::values.game_dirs); - - dialog.accept(); - }); - - connect(button_box, &QDialogButtonBox::rejected, &dialog, &QDialog::reject); - - dialog.exec(); + } } - void GMainWindow::OnGameListRemovePlayTimeData(u64 program_id) { if (QMessageBox::question(this, tr("Remove Play Time Data"), tr("Reset play time?"), QMessageBox::Yes | QMessageBox::No, diff --git a/src/yuzu/set_play_time_dialog.cpp b/src/yuzu/set_play_time_dialog.cpp new file mode 100644 index 0000000000..c0f1f0be22 --- /dev/null +++ b/src/yuzu/set_play_time_dialog.cpp @@ -0,0 +1,49 @@ +// SPDX-FileCopyrightText: Copyright 2025 Eden Emulator Project +// SPDX-License-Identifier: GPL-3.0-or-later + +#include "yuzu/set_play_time_dialog.h" +#include "frontend_common/play_time_manager.h" +#include "ui_set_play_time_dialog.h" + +SetPlayTimeDialog::SetPlayTimeDialog(QWidget* parent, u64 current_play_time) + : QDialog(parent), ui{std::make_unique()} { + ui->setupUi(this); + + ui->hoursSpinBox->setValue( + QString::fromStdString(PlayTime::PlayTimeManager::GetPlayTimeHours(current_play_time)).toInt()); + ui->minutesSpinBox->setValue( + QString::fromStdString(PlayTime::PlayTimeManager::GetPlayTimeMinutes(current_play_time)).toInt()); + ui->secondsSpinBox->setValue( + QString::fromStdString(PlayTime::PlayTimeManager::GetPlayTimeSeconds(current_play_time)).toInt()); + + connect(ui->hoursSpinBox, QOverload::of(&QSpinBox::valueChanged), this, + &SetPlayTimeDialog::OnValueChanged); + connect(ui->minutesSpinBox, QOverload::of(&QSpinBox::valueChanged), this, + &SetPlayTimeDialog::OnValueChanged); + connect(ui->secondsSpinBox, QOverload::of(&QSpinBox::valueChanged), this, + &SetPlayTimeDialog::OnValueChanged); +} + +SetPlayTimeDialog::~SetPlayTimeDialog() = default; + +u64 SetPlayTimeDialog::GetTotalSeconds() const { + const u64 hours = static_cast(ui->hoursSpinBox->value()); + const u64 minutes = static_cast(ui->minutesSpinBox->value()); + const u64 seconds = static_cast(ui->secondsSpinBox->value()); + + return hours * 3600 + minutes * 60 + seconds; +} + +void SetPlayTimeDialog::OnValueChanged() { + if (ui->errorLabel->isVisible()) { + ui->errorLabel->setVisible(false); + } + + const u64 total_seconds = GetTotalSeconds(); + constexpr u64 max_reasonable_time = 9999ULL * 3600; + + if (total_seconds > max_reasonable_time) { + ui->errorLabel->setText(tr("Total play time reached maximum.")); + ui->errorLabel->setVisible(true); + } +} diff --git a/src/yuzu/set_play_time_dialog.h b/src/yuzu/set_play_time_dialog.h new file mode 100644 index 0000000000..75513539e5 --- /dev/null +++ b/src/yuzu/set_play_time_dialog.h @@ -0,0 +1,27 @@ +// SPDX-FileCopyrightText: Copyright 2025 Eden Emulator Project +// SPDX-License-Identifier: GPL-3.0-or-later + +#pragma once + +#include +#include +#include "common/common_types.h" + +namespace Ui { +class SetPlayTimeDialog; +} + +class SetPlayTimeDialog : public QDialog { + Q_OBJECT + +public: + explicit SetPlayTimeDialog(QWidget* parent, u64 current_play_time); + ~SetPlayTimeDialog() override; + + u64 GetTotalSeconds() const; + +private: + void OnValueChanged(); + + std::unique_ptr ui; +}; diff --git a/src/yuzu/set_play_time_dialog.ui b/src/yuzu/set_play_time_dialog.ui new file mode 100644 index 0000000000..dca1c7f1a7 --- /dev/null +++ b/src/yuzu/set_play_time_dialog.ui @@ -0,0 +1,123 @@ + + + SetPlayTimeDialog + + + + 0 + 0 + 400 + 150 + + + + Set Play Time Data + + + true + + + + + + + + Hours: + + + + + + + 9999 + + + + + + + Minutes: + + + + + + + 59 + + + + + + + Seconds: + + + + + + + 59 + + + + + + + + + QLabel { color : red; } + + + + + + false + + + + + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok + + + + + + + + + buttonBox + accepted() + SetPlayTimeDialog + accept() + + + 199 + 129 + + + 199 + 74 + + + + + buttonBox + rejected() + SetPlayTimeDialog + reject() + + + 199 + 129 + + + 199 + 74 + + + + +