summaryrefslogtreecommitdiff
path: root/src/mainwindow.cpp
diff options
context:
space:
mode:
authorAl <26797547+Al12rs@users.noreply.github.com>2020-06-07 12:21:41 -0700
committerGitHub <noreply@github.com>2020-06-07 12:21:41 -0700
commitec5a78cefa610df153063c3cbfcc42b36998a542 (patch)
tree0345d3543d85d0aaf93b6c1b1761b8c2dc554ebd /src/mainwindow.cpp
parent8b667fe69a268c7d183f60dd43ec95dbd976c47b (diff)
parent6d7c0866859a0fe27a8055068552470b67d680e3 (diff)
Merge pull request #1112 from Al12rs/updateProblemsButton
Move Problems Checks on a thread and avoid redundant calls
Diffstat (limited to 'src/mainwindow.cpp')
-rw-r--r--src/mainwindow.cpp61
1 files changed, 39 insertions, 22 deletions
diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp
index 477cbc70..c3a19bf4 100644
--- a/src/mainwindow.cpp
+++ b/src/mainwindow.cpp
@@ -266,6 +266,8 @@ MainWindow::MainWindow(Settings &settings
, m_LinkToolbar(nullptr)
, m_LinkDesktop(nullptr)
, m_LinkStartMenu(nullptr)
+ , m_NumberOfProblems(0)
+ , m_ProblemsCheckRequired(false)
{
// disables incredibly slow menu fade in effect that looks and feels like crap.
// this was only happening to users with the windows
@@ -400,7 +402,7 @@ MainWindow::MainWindow(Settings &settings
ui->bossButton->setToolTip(tr("There is no supported sort mechanism for this game. You will probably have to use a third-party tool."));
}
- connect(&m_PluginContainer, SIGNAL(diagnosisUpdate()), this, SLOT(updateProblemsButton()));
+ connect(&m_PluginContainer, SIGNAL(diagnosisUpdate()), this, SLOT(scheduleCheckForProblems()));
connect(ui->savegameList, SIGNAL(itemEntered(QListWidgetItem*)), this, SLOT(saveSelectionChanged(QListWidgetItem*)));
@@ -473,7 +475,8 @@ MainWindow::MainWindow(Settings &settings
setFilterShortcuts(ui->downloadView, ui->downloadFilterEdit);
m_UpdateProblemsTimer.setSingleShot(true);
- connect(&m_UpdateProblemsTimer, SIGNAL(timeout()), this, SLOT(updateProblemsButton()));
+ connect(&m_UpdateProblemsTimer, &QTimer::timeout, this, &MainWindow::checkForProblemsAsync);
+ connect(this, &MainWindow::checkForProblemsDone, this, &MainWindow::updateProblemsButton, Qt::ConnectionType::QueuedConnection);
m_SaveMetaTimer.setSingleShot(false);
connect(&m_SaveMetaTimer, SIGNAL(timeout()), this, SLOT(saveModMetas()));
@@ -1000,21 +1003,19 @@ void MainWindow::on_centralWidget_customContextMenuRequested(const QPoint &pos)
createPopupMenu()->exec(ui->centralWidget->mapToGlobal(pos));
}
-void MainWindow::scheduleUpdateButton()
+void MainWindow::scheduleCheckForProblems()
{
if (!m_UpdateProblemsTimer.isActive()) {
- m_UpdateProblemsTimer.start(1000);
+ m_UpdateProblemsTimer.start(500);
}
}
void MainWindow::updateProblemsButton()
{
- TimeThis tt("MainWindow::updateProblemsButton()");
-
// if the current stylesheet doesn't provide an icon, this is used instead
const char* DefaultIconName = ":/MO/gui/warning";
- const std::size_t numProblems = checkForProblems();
+ const std::size_t numProblems = m_NumberOfProblems;
// original icon without a count painted on it
const QIcon original = m_originalNotificationIcon.isNull() ?
@@ -1097,19 +1098,34 @@ bool MainWindow::errorReported(QString &logFile)
return false;
}
+QFuture<void> MainWindow::checkForProblemsAsync() {
+ return QtConcurrent::run([this]() {
+ checkForProblemsImpl();
+ });
+}
-size_t MainWindow::checkForProblems()
+void MainWindow::checkForProblemsImpl()
{
- size_t numProblems = 0;
- for (QObject *pluginObj : m_PluginContainer.plugins<QObject>()) {
- IPlugin *plugin = qobject_cast<IPlugin*>(pluginObj);
- if (plugin == nullptr || plugin->isActive()) {
- IPluginDiagnose *diagnose = qobject_cast<IPluginDiagnose*>(pluginObj);
- if (diagnose != nullptr)
- numProblems += diagnose->activeProblems().size();
+ m_ProblemsCheckRequired = true;
+
+ std::scoped_lock lk(m_CheckForProblemsMutex);
+
+ // another thread might already have checked while this one was waiting on the lock
+ if (m_ProblemsCheckRequired) {
+ m_ProblemsCheckRequired = false;
+ TimeThis tt("MainWindow::checkForProblemsImpl()");
+ size_t numProblems = 0;
+ for (QObject *pluginObj : m_PluginContainer.plugins<QObject>()) {
+ IPlugin *plugin = qobject_cast<IPlugin*>(pluginObj);
+ if (plugin == nullptr || plugin->isActive()) {
+ IPluginDiagnose *diagnose = qobject_cast<IPluginDiagnose*>(pluginObj);
+ if (diagnose != nullptr)
+ numProblems += diagnose->activeProblems().size();
+ }
}
+ m_NumberOfProblems = numProblems;
+ emit checkForProblemsDone();
}
- return numProblems;
}
void MainWindow::about()
@@ -1353,7 +1369,7 @@ void MainWindow::showEvent(QShowEvent *event)
m_OrganizerCore.settings().nexus().registerAsNXMHandler(false);
m_WasVisible = true;
- updateProblemsButton();
+ updateProblemsButton();
}
}
@@ -2456,7 +2472,7 @@ void MainWindow::directory_refreshed()
{
// some problem-reports may rely on the virtual directory tree so they need to be updated
// now
- updateProblemsButton();
+ scheduleCheckForProblems();
//Some better check for the current tab is needed.
if (ui->tabWidget->currentIndex() == 2) {
@@ -3782,7 +3798,7 @@ void MainWindow::clearOverwrite()
for (auto f : overwriteDir.entryList(QDir::AllDirs | QDir::Files | QDir::NoDotAndDotDot))
delList.push_back(overwriteDir.absoluteFilePath(f));
if (shellDelete(delList, true)) {
- updateProblemsButton();
+ scheduleCheckForProblems();
m_OrganizerCore.refreshModList();
} else {
const auto e = GetLastError();
@@ -5162,7 +5178,6 @@ void MainWindow::languageChange(const QString &newLanguage)
createHelpMenu();
updateDownloadView();
- updateProblemsButton();
QMenu *listOptionsMenu = new QMenu(ui->listOptionsBtn);
initModListContextMenu(listOptionsMenu);
@@ -5869,12 +5884,14 @@ void MainWindow::on_bsaList_itemChanged(QTreeWidgetItem*, int)
void MainWindow::on_actionNotifications_triggered()
{
- updateProblemsButton();
+ auto future = checkForProblemsAsync();
+
+ future.waitForFinished();
ProblemsDialog problems(m_PluginContainer.plugins<QObject>(), this);
problems.exec();
- updateProblemsButton();
+ scheduleCheckForProblems();
}
void MainWindow::on_actionChange_Game_triggered()