Background threads for tab and application closing

This commit is contained in:
BasioMeusPuga
2017-11-14 11:51:56 +05:30
parent 1a59653011
commit b08d15deec
2 changed files with 35 additions and 17 deletions

View File

@@ -63,7 +63,7 @@ import mainwindow
import database import database
import sorter import sorter
from widgets import LibraryToolBar, BookToolBar, Tab, LibraryDelegate from widgets import LibraryToolBar, BookToolBar, Tab, LibraryDelegate, BackGroundTabUpdate
from library import Library from library import Library
from settings import Settings from settings import Settings
@@ -125,6 +125,9 @@ class MainUI(QtWidgets.QMainWindow, mainwindow.Ui_MainWindow):
self.tab_switch() self.tab_switch()
self.tabWidget.currentChanged.connect(self.tab_switch) self.tabWidget.currentChanged.connect(self.tab_switch)
# Background Thread
self.thread = None
# For fullscreening purposes # For fullscreening purposes
self.current_contentView = None self.current_contentView = None
@@ -275,10 +278,12 @@ class MainUI(QtWidgets.QMainWindow, mainwindow.Ui_MainWindow):
current_author + ' - ' + current_title) current_author + ' - ' + current_title)
def tab_close(self, tab_index): def tab_close(self, tab_index):
self.database_update_position(tab_index) tab_metadata = self.tabWidget.widget(tab_index).metadata
temp_dir = self.tabWidget.widget(tab_index).metadata['temp_dir']
if temp_dir: self.thread = BackGroundTabUpdate(
shutil.rmtree(temp_dir) self.database_path, tab_metadata)
self.thread.start()
self.tabWidget.removeTab(tab_index) self.tabWidget.removeTab(tab_index)
def set_toc_position(self, event=None): 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.verticalScrollBar().setValue(0)
current_tab.contentView.setHtml(required_content) 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): def set_fullscreen(self):
current_tab = self.tabWidget.currentIndex() current_tab = self.tabWidget.currentIndex()
current_tab_widget = self.tabWidget.widget(current_tab) current_tab_widget = self.tabWidget.widget(current_tab)
@@ -458,14 +456,15 @@ class MainUI(QtWidgets.QMainWindow, mainwindow.Ui_MainWindow):
font, font_size, foreground, background)) font, font_size, foreground, background))
def closeEvent(self, event=None): def closeEvent(self, event=None):
# All tabs must be iterated upon here # All tabs must be iterated upon here
for i in range(1, self.tabWidget.count()): for i in range(1, self.tabWidget.count()):
self.database_update_position(i)
tab_metadata = self.tabWidget.widget(i).metadata tab_metadata = self.tabWidget.widget(i).metadata
if tab_metadata['temp_dir']: self.thread = BackGroundTabUpdate(
shutil.rmtree(tab_metadata['temp_dir']) self.database_path, tab_metadata)
self.thread.start()
self.thread.finished.connect(QtWidgets.qApp.exit)
self.temp_dir.remove()
Settings(self).save_settings() Settings(self).save_settings()
QtWidgets.qApp.exit() QtWidgets.qApp.exit()

View File

@@ -1,9 +1,11 @@
#!usr/bin/env python3 #!usr/bin/env python3
import os import os
import shutil
from PyQt5 import QtWidgets, QtGui, QtCore from PyQt5 import QtWidgets, QtGui, QtCore
import pie_chart import pie_chart
import database
class BookToolBar(QtWidgets.QToolBar): class BookToolBar(QtWidgets.QToolBar):
@@ -372,3 +374,20 @@ class MyAbsModel(QtGui.QStandardItemModel, QtCore.QAbstractItemModel):
def __init__(self, parent=None): def __init__(self, parent=None):
# We're using this to be able to access the match() method # We're using this to be able to access the match() method
super(MyAbsModel, self).__init__(parent) 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)