Qt: stop all async refresh threads on close

This commit is contained in:
Megamouse 2022-12-22 10:40:33 +01:00
parent 96c6dc01eb
commit b2c4fed39c
5 changed files with 39 additions and 83 deletions

View File

@ -139,17 +139,8 @@ game_list_frame::game_list_frame(std::shared_ptr<gui_settings> gui_settings, std
connect(&m_refresh_watcher, &QFutureWatcher<void>::finished, this, &game_list_frame::OnRefreshFinished);
connect(&m_refresh_watcher, &QFutureWatcher<void>::canceled, this, [this]()
{
if (m_size_watcher.isRunning())
{
m_size_watcher.cancel();
m_size_watcher.waitForFinished();
}
if (m_repaint_watcher.isRunning())
{
m_repaint_watcher.cancel();
m_repaint_watcher.waitForFinished();
}
gui::utils::stop_future_watcher(m_size_watcher, true);
gui::utils::stop_future_watcher(m_repaint_watcher, true);
m_path_list.clear();
m_game_data.clear();
@ -266,6 +257,10 @@ void game_list_frame::LoadSettings()
game_list_frame::~game_list_frame()
{
gui::utils::stop_future_watcher(m_size_watcher, true);
gui::utils::stop_future_watcher(m_repaint_watcher, true);
gui::utils::stop_future_watcher(m_refresh_watcher, true);
SaveSettings();
}
@ -434,26 +429,9 @@ std::string game_list_frame::GetDataDirBySerial(const std::string& serial)
void game_list_frame::Refresh(const bool from_drive, const bool scroll_after)
{
if (m_size_watcher.isRunning())
{
m_size_watcher.cancel();
m_size_watcher.waitForFinished();
}
if (m_repaint_watcher.isRunning())
{
m_repaint_watcher.cancel();
m_repaint_watcher.waitForFinished();
}
if (m_refresh_watcher.isRunning())
{
if (from_drive)
{
m_refresh_watcher.cancel();
}
m_refresh_watcher.waitForFinished();
}
gui::utils::stop_future_watcher(m_size_watcher, true);
gui::utils::stop_future_watcher(m_repaint_watcher, true);
gui::utils::stop_future_watcher(m_refresh_watcher, from_drive);
if (from_drive)
{
@ -729,17 +707,8 @@ void game_list_frame::Refresh(const bool from_drive, const bool scroll_after)
void game_list_frame::OnRefreshFinished()
{
if (m_size_watcher.isRunning())
{
m_size_watcher.cancel();
m_size_watcher.waitForFinished();
}
if (m_repaint_watcher.isRunning())
{
m_repaint_watcher.cancel();
m_repaint_watcher.waitForFinished();
}
gui::utils::stop_future_watcher(m_size_watcher, true);
gui::utils::stop_future_watcher(m_repaint_watcher, true);
for (auto&& g : m_games.pop_all())
{
@ -2319,11 +2288,7 @@ void game_list_frame::ResizeIcons(const int& slider_pos)
void game_list_frame::RepaintIcons(const bool& from_settings)
{
if (m_repaint_watcher.isRunning())
{
m_repaint_watcher.cancel();
m_repaint_watcher.waitForFinished();
}
gui::utils::stop_future_watcher(m_repaint_watcher, true);
if (from_settings)
{

View File

@ -11,6 +11,7 @@
#include <QHeaderView>
#include <QTreeWidgetItem>
#include <QPainter>
#include <QFutureWatcher>
#include <string>
#include <map>
@ -127,5 +128,18 @@ namespace gui
// Convert an arbitrary count of bytes to a readable format using global units (KB, MB...)
QString format_byte_size(usz size);
template <typename T>
void stop_future_watcher(QFutureWatcher<T>& watcher, bool cancel)
{
if (watcher.isRunning())
{
if (cancel)
{
watcher.cancel();
}
watcher.waitForFinished();
}
}
} // utils
} // gui

View File

@ -66,8 +66,7 @@ screenshot_manager_dialog::screenshot_manager_dialog(QWidget* parent) : QDialog(
}
}
m_icon_loader = new QFutureWatcher<thumbnail>(this);
connect(m_icon_loader, &QFutureWatcher<QIcon>::resultReadyAt, this, &screenshot_manager_dialog::update_icon);
connect(&m_icon_loader, &QFutureWatcher<QIcon>::resultReadyAt, this, &screenshot_manager_dialog::update_icon);
connect(m_grid, &QListWidget::itemDoubleClicked, this, &screenshot_manager_dialog::show_preview);
connect(m_grid->verticalScrollBar(), &QScrollBar::valueChanged, this, &screenshot_manager_dialog::update_icons);
@ -82,8 +81,7 @@ screenshot_manager_dialog::screenshot_manager_dialog(QWidget* parent) : QDialog(
screenshot_manager_dialog::~screenshot_manager_dialog()
{
m_icon_loader->cancel();
m_icon_loader->waitForFinished();
gui::utils::stop_future_watcher(m_icon_loader, true);
}
void screenshot_manager_dialog::show_preview(QListWidgetItem* item)
@ -101,7 +99,7 @@ void screenshot_manager_dialog::show_preview(QListWidgetItem* item)
void screenshot_manager_dialog::update_icon(int index) const
{
const thumbnail tn = m_icon_loader->resultAt(index);
const thumbnail tn = m_icon_loader.resultAt(index);
if (QListWidgetItem* item = m_grid->item(tn.index))
{
@ -144,11 +142,7 @@ void screenshot_manager_dialog::update_icons(int value)
}
}
if (m_icon_loader->isRunning())
{
m_icon_loader->cancel();
m_icon_loader->waitForFinished();
}
gui::utils::stop_future_watcher(m_icon_loader, true);
const std::function<thumbnail(thumbnail)> load = [icon_size = m_icon_size](thumbnail tn) -> thumbnail
{
@ -156,7 +150,7 @@ void screenshot_manager_dialog::update_icons(int value)
return tn;
};
m_icon_loader->setFuture(QtConcurrent::mapped(thumbnails_to_load, load));
m_icon_loader.setFuture(QtConcurrent::mapped(thumbnails_to_load, load));
}
void screenshot_manager_dialog::resizeEvent(QResizeEvent* event)

View File

@ -44,7 +44,7 @@ private:
QListWidget* m_grid = nullptr;
QFutureWatcher<thumbnail>* m_icon_loader;
QFutureWatcher<thumbnail> m_icon_loader;
QSize m_icon_size;
QIcon m_placeholder;

View File

@ -368,6 +368,8 @@ trophy_manager_dialog::trophy_manager_dialog(std::shared_ptr<gui_settings> gui_s
trophy_manager_dialog::~trophy_manager_dialog()
{
gui::utils::stop_future_watcher(m_game_repaint_watcher, true);
gui::utils::stop_future_watcher(m_trophy_repaint_watcher, true);
}
bool trophy_manager_dialog::LoadTrophyFolderToDB(const std::string& trop_name)
@ -559,11 +561,7 @@ void trophy_manager_dialog::ResizeGameIcons()
if (m_game_combo->count() <= 0)
return;
if (m_game_repaint_watcher.isRunning())
{
m_game_repaint_watcher.cancel();
m_game_repaint_watcher.waitForFinished();
}
gui::utils::stop_future_watcher(m_game_repaint_watcher, true);
QPixmap placeholder(m_game_icon_size);
placeholder.fill(Qt::transparent);
@ -594,11 +592,7 @@ void trophy_manager_dialog::ResizeTrophyIcons()
if (m_game_combo->count() <= 0)
return;
if (m_trophy_repaint_watcher.isRunning())
{
m_trophy_repaint_watcher.cancel();
m_trophy_repaint_watcher.waitForFinished();
}
gui::utils::stop_future_watcher(m_trophy_repaint_watcher, true);
const int db_pos = m_game_combo->currentData().toInt();
const qreal dpr = devicePixelRatioF();
@ -825,15 +819,8 @@ void trophy_manager_dialog::ShowGameTableContextMenu(const QPoint& pos)
void trophy_manager_dialog::StartTrophyLoadThreads()
{
if (m_game_repaint_watcher.isRunning())
{
m_game_repaint_watcher.waitForFinished();
}
if (m_trophy_repaint_watcher.isRunning())
{
m_trophy_repaint_watcher.waitForFinished();
}
gui::utils::stop_future_watcher(m_game_repaint_watcher, false);
gui::utils::stop_future_watcher(m_trophy_repaint_watcher, false);
m_trophies_db.clear();
@ -894,11 +881,7 @@ void trophy_manager_dialog::StartTrophyLoadThreads()
void trophy_manager_dialog::PopulateGameTable()
{
if (m_game_repaint_watcher.isRunning())
{
m_game_repaint_watcher.cancel();
m_game_repaint_watcher.waitForFinished();
}
gui::utils::stop_future_watcher(m_game_repaint_watcher, true);
m_game_table->setSortingEnabled(false); // Disable sorting before using setItem calls