Merge pull request #4173 from jamierocks/common-time-duration-format

NOISSUE Use common duration format for global and instances
This commit is contained in:
Petr Mrázek 2021-10-24 01:11:52 +02:00 committed by GitHub
commit 98887911c1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 43 additions and 36 deletions

View File

@ -84,6 +84,10 @@ set(CORE_SOURCES
# A Recursive file system watcher # A Recursive file system watcher
RecursiveFileSystemWatcher.h RecursiveFileSystemWatcher.h
RecursiveFileSystemWatcher.cpp RecursiveFileSystemWatcher.cpp
# Time
MMCTime.h
MMCTime.cpp
) )
add_unit_test(FileSystem add_unit_test(FileSystem

21
launcher/MMCTime.cpp Normal file
View File

@ -0,0 +1,21 @@
#include <MMCTime.h>
#include <QObject>
QString Time::prettifyDuration(int64_t duration) {
int seconds = (int) (duration % 60);
duration /= 60;
int minutes = (int) (duration % 60);
duration /= 60;
int hours = (int) (duration % 24);
int days = (int) (duration / 24);
if((hours == 0)&&(days == 0))
{
return QObject::tr("%1m %2s").arg(minutes).arg(seconds);
}
if (days == 0)
{
return QObject::tr("%1h %2m").arg(hours).arg(minutes);
}
return QObject::tr("%1d %2h %3m").arg(days).arg(hours).arg(minutes);
}

9
launcher/MMCTime.h Normal file
View File

@ -0,0 +1,9 @@
#pragma once
#include <QString>
namespace Time {
QString prettifyDuration(int64_t duration);
}

View File

@ -88,6 +88,7 @@
#include "UpdateController.h" #include "UpdateController.h"
#include "KonamiCode.h" #include "KonamiCode.h"
#include <InstanceCopyTask.h> #include <InstanceCopyTask.h>
#include "MMCTime.h"
namespace { namespace {
QString profileInUseFilter(const QString & profile, bool used) QString profileInUseFilter(const QString & profile, bool used)
@ -747,7 +748,7 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new MainWindow
connect(LAUNCHER, &Launcher::globalSettingsClosed, this, &MainWindow::globalSettingsClosed); connect(LAUNCHER, &Launcher::globalSettingsClosed, this, &MainWindow::globalSettingsClosed);
m_statusLeft = new QLabel(tr("No instance selected"), this); m_statusLeft = new QLabel(tr("No instance selected"), this);
m_statusCenter = new QLabel(tr("Total playtime: 0s."), this); m_statusCenter = new QLabel(tr("Total playtime: 0s"), this);
statusBar()->addPermanentWidget(m_statusLeft, 1); statusBar()->addPermanentWidget(m_statusLeft, 1);
statusBar()->addPermanentWidget(m_statusCenter, 0); statusBar()->addPermanentWidget(m_statusCenter, 0);
@ -1926,15 +1927,8 @@ void MainWindow::checkInstancePathForProblems()
void MainWindow::updateStatusCenter() void MainWindow::updateStatusCenter()
{ {
int timeplayed = LAUNCHER->instances()->getTotalPlayTime(); int timePlayed = LAUNCHER->instances()->getTotalPlayTime();
int minutesTotal = timeplayed / 60; if (timePlayed > 0) {
int seconds = timeplayed % 60; m_statusCenter->setText(tr("Total playtime: %1").arg(Time::prettifyDuration(timePlayed)));
int minutes = minutesTotal % 60; }
int hours = minutesTotal / 60;
if(hours != 0)
m_statusCenter->setText(tr("Total playtime: %1h %2m %3s").arg(hours).arg(minutes).arg(seconds));
else if(minutes != 0)
m_statusCenter->setText(tr("Total playtime: %1m %2s").arg(minutes).arg(seconds));
else if(seconds != 0)
m_statusCenter->setText(tr("Total playtime: %1s").arg(seconds));
} }

View File

@ -10,6 +10,7 @@
#include <pathmatcher/MultiMatcher.h> #include <pathmatcher/MultiMatcher.h>
#include <FileSystem.h> #include <FileSystem.h>
#include <java/JavaVersion.h> #include <java/JavaVersion.h>
#include "MMCTime.h"
#include "launch/LaunchTask.h" #include "launch/LaunchTask.h"
#include "launch/steps/LookupServerAddress.h" #include "launch/steps/LookupServerAddress.h"
@ -766,25 +767,6 @@ QString MinecraftInstance::getLogFileRoot()
return gameRoot(); return gameRoot();
} }
QString MinecraftInstance::prettifyTimeDuration(int64_t duration)
{
int seconds = (int) (duration % 60);
duration /= 60;
int minutes = (int) (duration % 60);
duration /= 60;
int hours = (int) (duration % 24);
int days = (int) (duration / 24);
if((hours == 0)&&(days == 0))
{
return tr("%1m %2s").arg(minutes).arg(seconds);
}
if (days == 0)
{
return tr("%1h %2m").arg(hours).arg(minutes);
}
return tr("%1d %2h %3m").arg(days).arg(hours).arg(minutes);
}
QString MinecraftInstance::getStatusbarDescription() QString MinecraftInstance::getStatusbarDescription()
{ {
QStringList traits; QStringList traits;
@ -798,11 +780,11 @@ QString MinecraftInstance::getStatusbarDescription()
if(m_settings->get("ShowGameTime").toBool()) if(m_settings->get("ShowGameTime").toBool())
{ {
if (lastTimePlayed() > 0) { if (lastTimePlayed() > 0) {
description.append(tr(", last played for %1").arg(prettifyTimeDuration(lastTimePlayed()))); description.append(tr(", last played for %1").arg(Time::prettifyDuration(lastTimePlayed())));
} }
if (totalTimePlayed() > 0) { if (totalTimePlayed() > 0) {
description.append(tr(", total played for %1").arg(prettifyTimeDuration(totalTimePlayed()))); description.append(tr(", total played for %1").arg(Time::prettifyDuration(totalTimePlayed())));
} }
} }
if(hasCrashed()) if(hasCrashed())

View File

@ -118,9 +118,6 @@ protected:
QStringList validLaunchMethods(); QStringList validLaunchMethods();
QString launchMethod(); QString launchMethod();
private:
QString prettifyTimeDuration(int64_t duration);
protected: // data protected: // data
std::shared_ptr<PackProfile> m_components; std::shared_ptr<PackProfile> m_components;
mutable std::shared_ptr<ModFolderModel> m_loader_mod_list; mutable std::shared_ptr<ModFolderModel> m_loader_mod_list;