From 1762d2fc7de99e38477a517c9b379447fd00b968 Mon Sep 17 00:00:00 2001 From: Leland Liu <14846866+lelandliu@users.noreply.github.com> Date: Tue, 9 Jul 2019 22:23:24 -0400 Subject: [PATCH] Added total playtime --- .gitignore | 1 + api/logic/InstanceList.cpp | 18 +++++++++++++++++- api/logic/InstanceList.h | 4 ++++ application/MainWindow.cpp | 20 +++++++++++++++++++- application/MainWindow.h | 2 ++ 5 files changed, 43 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 496c382e3..e11168c3c 100644 --- a/.gitignore +++ b/.gitignore @@ -16,6 +16,7 @@ CMakeLists.txt.user.* /.settings /.idea cmake-build-*/ +Debug # Build dirs build diff --git a/api/logic/InstanceList.cpp b/api/logic/InstanceList.cpp index 02fae6ac5..cb38853b5 100644 --- a/api/logic/InstanceList.cpp +++ b/api/logic/InstanceList.cpp @@ -387,9 +387,19 @@ InstanceList::InstListError InstanceList::loadList() add(newList); } m_dirty = false; + updateTotalPlayTime(); return NoError; } +void InstanceList::updateTotalPlayTime() +{ + totalPlayTime = 0; + for(auto const& itr : m_instances) + { + totalPlayTime += itr.get()->totalTimePlayed(); + } +} + void InstanceList::saveNow() { for(auto & item: m_instances) @@ -475,6 +485,7 @@ void InstanceList::propertiesChanged(BaseInstance *inst) if (i != -1) { emit dataChanged(index(i), index(i)); + updateTotalPlayTime(); } } @@ -848,4 +859,9 @@ bool InstanceList::destroyStagingPath(const QString& keyPath) return FS::deletePath(keyPath); } -#include "InstanceList.moc" \ No newline at end of file +int InstanceList::getTotalPlayTime() { + updateTotalPlayTime(); + return totalPlayTime; +} + +#include "InstanceList.moc" diff --git a/api/logic/InstanceList.h b/api/logic/InstanceList.h index 8215cb665..56ee3be44 100644 --- a/api/logic/InstanceList.h +++ b/api/logic/InstanceList.h @@ -128,6 +128,8 @@ public: */ bool destroyStagingPath(const QString & keyPath); + int getTotalPlayTime(); + signals: void dataIsInvalid(); void instancesChanged(); @@ -145,6 +147,7 @@ private slots: private: int getInstIndex(BaseInstance *inst) const; + void updateTotalPlayTime(); void suspendWatch(); void resumeWatch(); void add(const QList &list); @@ -155,6 +158,7 @@ private: private: int m_watchLevel = 0; + int totalPlayTime = 0; bool m_dirty = false; QList m_instances; QSet m_groupNameCache; diff --git a/application/MainWindow.cpp b/application/MainWindow.cpp index 13a7c7ae9..1720ca89a 100644 --- a/application/MainWindow.cpp +++ b/application/MainWindow.cpp @@ -259,6 +259,7 @@ public: actionAddInstance.setTooltipId(QT_TRANSLATE_NOOP("MainWindow", "Add a new instance.")); all_actions.append(&actionAddInstance); mainToolBar->addAction(actionAddInstance); + actionAddInstance.setTextId(QT_TRANSLATE_NOOP("MainWindow", "A")); mainToolBar->addSeparator(); @@ -724,8 +725,10 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new MainWindow connect(MMC, &MultiMC::globalSettingsClosed, this, &MainWindow::globalSettingsClosed); m_statusLeft = new QLabel(tr("No instance selected"), this); + m_statusCenter = new QLabel(tr("Total playtime: 0s."), this); m_statusRight = new ServerStatus(this); statusBar()->addPermanentWidget(m_statusLeft, 1); + statusBar()->addPermanentWidget(m_statusCenter, 1); statusBar()->addPermanentWidget(m_statusRight, 0); // Add "manage accounts" button, right align @@ -1327,7 +1330,6 @@ void MainWindow::setCatBackground(bool enabled) { QDateTime now = QDateTime::currentDateTime(); QDateTime xmas(QDate(now.date().year(), 12, 25), QTime(0, 0)); - ; QString cat = (non_stupid_abs(now.daysTo(xmas)) <= 4) ? "catmas" : "kitteh"; view->setStyleSheet(QString(R"( GroupView @@ -1526,6 +1528,7 @@ void MainWindow::setSelectedInstanceById(const QString &id) { QModelIndex selectionIndex = proxymodel->mapFromSource(index); view->selectionModel()->setCurrentIndex(selectionIndex, QItemSelectionModel::ClearAndSelect); + updateStatusCenter(); } } @@ -1854,6 +1857,7 @@ void MainWindow::instanceChanged(const QModelIndex ¤t, const QModelIndex & ui->actionExportInstance->setEnabled(m_selectedInstance->canExport()); ui->renameButton->setText(m_selectedInstance->name()); m_statusLeft->setText(m_selectedInstance->getStatusbarDescription()); + updateStatusCenter(); updateInstanceToolIcon(m_selectedInstance->iconKey()); updateToolsMenu(); @@ -1932,3 +1936,17 @@ void MainWindow::checkInstancePathForProblems() warning.exec(); } } + +void MainWindow::updateStatusCenter() +{ + int timeplayed = MMC->instances()->getTotalPlayTime(); + int minutes = timeplayed / 60; + int hours = minutes / 60; + int seconds = timeplayed % 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)); +} diff --git a/application/MainWindow.h b/application/MainWindow.h index 08c6b9692..c992ab94e 100644 --- a/application/MainWindow.h +++ b/application/MainWindow.h @@ -194,6 +194,7 @@ private: void setCatBackground(bool enabled); void updateInstanceToolIcon(QString new_icon); void setSelectedInstanceById(const QString &id); + void updateStatusCenter(); void runModalTask(Task *task); void instanceFromInstanceTask(InstanceTask *task); @@ -207,6 +208,7 @@ private: InstanceProxyModel *proxymodel = nullptr; QToolButton *newsLabel = nullptr; QLabel *m_statusLeft = nullptr; + QLabel *m_statusCenter = nullptr; ServerStatus *m_statusRight = nullptr; QMenu *accountMenu = nullptr; QToolButton *accountMenuButton = nullptr;