From b08d15deec1052e2251cc78780959fa345d3ca8e Mon Sep 17 00:00:00 2001 From: BasioMeusPuga Date: Tue, 14 Nov 2017 11:51:56 +0530 Subject: [PATCH] Background threads for tab and application closing --- __main__.py | 33 ++++++++++++++++----------------- widgets.py | 19 +++++++++++++++++++ 2 files changed, 35 insertions(+), 17 deletions(-) diff --git a/__main__.py b/__main__.py index d113973..fda65f3 100755 --- a/__main__.py +++ b/__main__.py @@ -63,7 +63,7 @@ import mainwindow import database import sorter -from widgets import LibraryToolBar, BookToolBar, Tab, LibraryDelegate +from widgets import LibraryToolBar, BookToolBar, Tab, LibraryDelegate, BackGroundTabUpdate from library import Library from settings import Settings @@ -125,6 +125,9 @@ class MainUI(QtWidgets.QMainWindow, mainwindow.Ui_MainWindow): self.tab_switch() self.tabWidget.currentChanged.connect(self.tab_switch) + # Background Thread + self.thread = None + # For fullscreening purposes self.current_contentView = None @@ -244,7 +247,7 @@ class MainUI(QtWidgets.QMainWindow, mainwindow.Ui_MainWindow): self.bookToolBar.hide() self.libraryToolBar.show() - + if self.lib_ref.proxy_model: # Making the proxy model available doesn't affect # memory utilization at all. Bleh. @@ -275,10 +278,12 @@ class MainUI(QtWidgets.QMainWindow, mainwindow.Ui_MainWindow): current_author + ' - ' + current_title) def tab_close(self, tab_index): - self.database_update_position(tab_index) - temp_dir = self.tabWidget.widget(tab_index).metadata['temp_dir'] - if temp_dir: - shutil.rmtree(temp_dir) + tab_metadata = self.tabWidget.widget(tab_index).metadata + + self.thread = BackGroundTabUpdate( + self.database_path, tab_metadata) + self.thread.start() + self.tabWidget.removeTab(tab_index) def set_toc_position(self, event=None): @@ -314,13 +319,6 @@ class MainUI(QtWidgets.QMainWindow, mainwindow.Ui_MainWindow): current_tab.contentView.verticalScrollBar().setValue(0) current_tab.contentView.setHtml(required_content) - def database_update_position(self, tab_index): - tab_metadata = self.tabWidget.widget(tab_index).metadata - file_hash = tab_metadata['hash'] - position = tab_metadata['position'] - database.DatabaseFunctions( - self.database_path).modify_position(file_hash, position) - def set_fullscreen(self): current_tab = self.tabWidget.currentIndex() current_tab_widget = self.tabWidget.widget(current_tab) @@ -458,14 +456,15 @@ class MainUI(QtWidgets.QMainWindow, mainwindow.Ui_MainWindow): font, font_size, foreground, background)) def closeEvent(self, event=None): + # All tabs must be iterated upon here for i in range(1, self.tabWidget.count()): - self.database_update_position(i) tab_metadata = self.tabWidget.widget(i).metadata - if tab_metadata['temp_dir']: - shutil.rmtree(tab_metadata['temp_dir']) + self.thread = BackGroundTabUpdate( + self.database_path, tab_metadata) + self.thread.start() + self.thread.finished.connect(QtWidgets.qApp.exit) - self.temp_dir.remove() Settings(self).save_settings() QtWidgets.qApp.exit() diff --git a/widgets.py b/widgets.py index 2634bec..a6a0d2a 100644 --- a/widgets.py +++ b/widgets.py @@ -1,9 +1,11 @@ #!usr/bin/env python3 import os +import shutil from PyQt5 import QtWidgets, QtGui, QtCore import pie_chart +import database class BookToolBar(QtWidgets.QToolBar): @@ -372,3 +374,20 @@ class MyAbsModel(QtGui.QStandardItemModel, QtCore.QAbstractItemModel): def __init__(self, parent=None): # We're using this to be able to access the match() method super(MyAbsModel, self).__init__(parent) + + +class BackGroundTabUpdate(QtCore.QThread): + def __init__(self, database_path, tab_metadata, parent=None): + super(BackGroundTabUpdate, self).__init__(parent) + self.database_path = database_path + self.tab_metadata = tab_metadata + + def run(self): + file_hash = self.tab_metadata['hash'] + position = self.tab_metadata['position'] + database.DatabaseFunctions( + self.database_path).modify_position(file_hash, position) + + temp_dir = self.tab_metadata['temp_dir'] + if temp_dir: + shutil.rmtree(temp_dir) \ No newline at end of file