Skip to content

Commit

Permalink
Added total playtime
Browse files Browse the repository at this point in the history
  • Loading branch information
enncoded authored and peterix committed Jul 22, 2021
1 parent 8ea500d commit 1762d2f
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ CMakeLists.txt.user.*
/.settings
/.idea
cmake-build-*/
Debug

# Build dirs
build
Expand Down
18 changes: 17 additions & 1 deletion api/logic/InstanceList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -475,6 +485,7 @@ void InstanceList::propertiesChanged(BaseInstance *inst)
if (i != -1)
{
emit dataChanged(index(i), index(i));
updateTotalPlayTime();
}
}

Expand Down Expand Up @@ -848,4 +859,9 @@ bool InstanceList::destroyStagingPath(const QString& keyPath)
return FS::deletePath(keyPath);
}

#include "InstanceList.moc"
int InstanceList::getTotalPlayTime() {
updateTotalPlayTime();
return totalPlayTime;
}

#include "InstanceList.moc"
4 changes: 4 additions & 0 deletions api/logic/InstanceList.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ class MULTIMC_LOGIC_EXPORT InstanceList : public QAbstractListModel
*/
bool destroyStagingPath(const QString & keyPath);

int getTotalPlayTime();

signals:
void dataIsInvalid();
void instancesChanged();
Expand All @@ -145,6 +147,7 @@ private slots:

private:
int getInstIndex(BaseInstance *inst) const;
void updateTotalPlayTime();
void suspendWatch();
void resumeWatch();
void add(const QList<InstancePtr> &list);
Expand All @@ -155,6 +158,7 @@ private slots:

private:
int m_watchLevel = 0;
int totalPlayTime = 0;
bool m_dirty = false;
QList<InstancePtr> m_instances;
QSet<QString> m_groupNameCache;
Expand Down
20 changes: 19 additions & 1 deletion application/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ class MainWindow::Ui
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();

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -1526,6 +1528,7 @@ void MainWindow::setSelectedInstanceById(const QString &id)
{
QModelIndex selectionIndex = proxymodel->mapFromSource(index);
view->selectionModel()->setCurrentIndex(selectionIndex, QItemSelectionModel::ClearAndSelect);
updateStatusCenter();
}
}

Expand Down Expand Up @@ -1854,6 +1857,7 @@ void MainWindow::instanceChanged(const QModelIndex &current, 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();
Expand Down Expand Up @@ -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));
}
2 changes: 2 additions & 0 deletions application/MainWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ private slots:
void setCatBackground(bool enabled);
void updateInstanceToolIcon(QString new_icon);
void setSelectedInstanceById(const QString &id);
void updateStatusCenter();

void runModalTask(Task *task);
void instanceFromInstanceTask(InstanceTask *task);
Expand All @@ -207,6 +208,7 @@ private slots:
InstanceProxyModel *proxymodel = nullptr;
QToolButton *newsLabel = nullptr;
QLabel *m_statusLeft = nullptr;
QLabel *m_statusCenter = nullptr;
ServerStatus *m_statusRight = nullptr;
QMenu *accountMenu = nullptr;
QToolButton *accountMenuButton = nullptr;
Expand Down

0 comments on commit 1762d2f

Please sign in to comment.