Fixed delegate display error, tab retention

This commit is contained in:
BasioMeusPuga
2017-11-15 11:57:33 +05:30
parent 13170fc14e
commit a17a150a45
5 changed files with 74 additions and 29 deletions

View File

@@ -72,6 +72,14 @@ class MainUI(QtWidgets.QMainWindow, mainwindow.Ui_MainWindow):
super(MainUI, self).__init__() super(MainUI, self).__init__()
self.setupUi(self) self.setupUi(self)
# Empty variables that will be infested soon
self.last_open_books = None
self.last_open_tab = None
self.last_open_path = None
self.thread = None # Background Thread
self.viewModel = None
self.current_contentView = None # For fullscreening purposes
# Initialize application # Initialize application
Settings(self).read_settings() # This should populate all variables that need Settings(self).read_settings() # This should populate all variables that need
# to be remembered across sessions # to be remembered across sessions
@@ -79,16 +87,12 @@ class MainUI(QtWidgets.QMainWindow, mainwindow.Ui_MainWindow):
# Create the database in case it doesn't exist # Create the database in case it doesn't exist
database.DatabaseInit(self.database_path) database.DatabaseInit(self.database_path)
# Background Thread
self.thread = None
# Create and right align the statusbar label widget # Create and right align the statusbar label widget
self.statusMessage = QtWidgets.QLabel() self.statusMessage = QtWidgets.QLabel()
self.statusMessage.setObjectName('statusMessage') self.statusMessage.setObjectName('statusMessage')
self.statusBar.addPermanentWidget(self.statusMessage) self.statusBar.addPermanentWidget(self.statusMessage)
# Init the QListView # Init the QListView
self.viewModel = None
self.lib_ref = Library(self) self.lib_ref = Library(self)
# Application wide temporary directory # Application wide temporary directory
@@ -127,9 +131,6 @@ 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)
# For fullscreening purposes
self.current_contentView = None
# Tab closing # Tab closing
self.tabWidget.setTabsClosable(True) self.tabWidget.setTabsClosable(True)
# TODO # TODO
@@ -151,6 +152,9 @@ class MainUI(QtWidgets.QMainWindow, mainwindow.Ui_MainWindow):
self.exit_all.setContext(QtCore.Qt.ApplicationShortcut) self.exit_all.setContext(QtCore.Qt.ApplicationShortcut)
self.exit_all.activated.connect(self.closeEvent) self.exit_all.activated.connect(self.closeEvent)
# Open last... open books.
self.open_files(self.last_open_books)
self.last_open_books = None
def resizeEvent(self, event=None): def resizeEvent(self, event=None):
if event: if event:
@@ -316,11 +320,6 @@ class MainUI(QtWidgets.QMainWindow, mainwindow.Ui_MainWindow):
def list_doubleclick(self, myindex): def list_doubleclick(self, myindex):
index = self.listView.model().index(myindex.row(), 0) index = self.listView.model().index(myindex.row(), 0)
file_exists = self.listView.model().data(index, QtCore.Qt.UserRole + 5)
if not file_exists:
return
metadata = self.listView.model().data(index, QtCore.Qt.UserRole + 3) metadata = self.listView.model().data(index, QtCore.Qt.UserRole + 3)
# Shift focus to the tab that has the book open (if there is one) # Shift focus to the tab that has the book open (if there is one)
@@ -331,12 +330,30 @@ class MainUI(QtWidgets.QMainWindow, mainwindow.Ui_MainWindow):
return return
path = metadata['path'] path = metadata['path']
contents = sorter.BookSorter( self.open_files([path])
[path], 'reading', self.database_path, self.temp_dir.path()).initiate_threads()
tab_ref = Tab(contents, self.tabWidget) def open_files(self, file_paths):
self.tabWidget.setCurrentWidget(tab_ref) # file_paths is expected to be a list
self.format_contentView() # This allows for threading file opening
# Which should speed up multiple file opening
# especially @ application start
if not file_paths:
return
contents = sorter.BookSorter(
file_paths, 'reading', self.database_path, self.temp_dir.path()).initiate_threads()
found_a_focusable_tab = False
for i in contents:
file_data = contents[i]
Tab(file_data, self.tabWidget)
if file_data['path'] == self.last_open_tab:
found_a_focusable_tab = True
self.tabWidget.setCurrentIndex(self.tabWidget.count() - 1)
if not found_a_focusable_tab:
self.tabWidget.setCurrentIndex(0)
def get_color(self): def get_color(self):
signal_sender = self.sender().objectName() signal_sender = self.sender().objectName()
@@ -445,13 +462,16 @@ class MainUI(QtWidgets.QMainWindow, mainwindow.Ui_MainWindow):
self.temp_dir.remove() self.temp_dir.remove()
self.hide() self.hide()
self.last_open_books = []
if self.tabWidget.count() > 1: if self.tabWidget.count() > 1:
all_metadata = []
all_metadata = []
for i in range(1, self.tabWidget.count()): for i in range(1, self.tabWidget.count()):
tab_metadata = self.tabWidget.widget(i).metadata tab_metadata = self.tabWidget.widget(i).metadata
self.last_open_books.append(tab_metadata['path'])
all_metadata.append(tab_metadata) all_metadata.append(tab_metadata)
Settings(self).save_settings()
self.thread = BackGroundTabUpdate(self.database_path, all_metadata) self.thread = BackGroundTabUpdate(self.database_path, all_metadata)
self.thread.finished.connect(QtWidgets.qApp.exit) self.thread.finished.connect(QtWidgets.qApp.exit)
self.thread.start() self.thread.start()

View File

@@ -97,6 +97,8 @@
</property> </property>
</widget> </widget>
</widget> </widget>
<resources/> <resources>
<include location="resources.qrc"/>
</resources>
<connections/> <connections/>
</ui> </ui>

View File

@@ -58,6 +58,11 @@ class Settings:
'currentProfileIndex', 0)) 'currentProfileIndex', 0))
self.settings.endGroup() self.settings.endGroup()
self.settings.beginGroup('lastOpen')
self.parent_window.last_open_books = self.settings.value('lastOpenFiles', [])
self.parent_window.last_open_tab = self.settings.value('lastOpenTab', 'library')
self.settings.endGroup()
def save_settings(self): def save_settings(self):
self.settings.beginGroup('mainWindow') self.settings.beginGroup('mainWindow')
self.settings.setValue('windowSize', self.parent_window.size()) self.settings.setValue('windowSize', self.parent_window.size())
@@ -81,3 +86,17 @@ class Settings:
current_profile3]) current_profile3])
self.settings.setValue('currentProfileIndex', current_profile_index) self.settings.setValue('currentProfileIndex', current_profile_index)
self.settings.endGroup() self.settings.endGroup()
# TODO
# Last open order
# Last open highlighted
current_tab_index = self.parent_window.tabWidget.currentIndex()
if current_tab_index == 0:
last_open_tab = 'library'
else:
last_open_tab = self.parent_window.tabWidget.widget(current_tab_index).metadata['path']
self.settings.beginGroup('lastOpen')
self.settings.setValue('lastOpenFiles', self.parent_window.last_open_books)
self.settings.setValue('lastOpenTab', last_open_tab)
self.settings.endGroup()

View File

@@ -32,7 +32,7 @@ class BookSorter:
# This includes getting file info for the database # This includes getting file info for the database
# Parsing for the reader proper # Parsing for the reader proper
# Caching upon closing # Caching upon closing
self.file_list = file_list self.file_list = [i for i in file_list if os.path.exists(i)]
self.statistics = [0, (len(file_list))] self.statistics = [0, (len(file_list))]
self.all_books = {} self.all_books = {}
self.hashes = [] self.hashes = []
@@ -145,7 +145,7 @@ class BookSorter:
content['Invalid'] = 'Possible Parse Error' content['Invalid'] = 'Possible Parse Error'
position = self.database_position(file_md5) position = self.database_position(file_md5)
self.all_books = { self.all_books[file_md5] = {
'title': title, 'title': title,
'author': author, 'author': author,
'year': year, 'year': year,

View File

@@ -412,7 +412,6 @@ class LibraryDelegate(QtWidgets.QStyledItemDelegate):
self.temp_dir = temp_dir self.temp_dir = temp_dir
def paint(self, painter, option, index): def paint(self, painter, option, index):
QtWidgets.QStyledItemDelegate.paint(self, painter, option, index)
# This is a hint for the future # This is a hint for the future
# Color icon slightly red # Color icon slightly red
# if option.state & QtWidgets.QStyle.State_Selected: # if option.state & QtWidgets.QStyle.State_Selected:
@@ -426,15 +425,23 @@ class LibraryDelegate(QtWidgets.QStyledItemDelegate):
# TODO # TODO
# Calculate progress on the basis of lines # Calculate progress on the basis of lines
if not file_exists:
read_icon = QtGui.QIcon.fromTheme('vcs-conflicting').pixmap(36)
painter.setOpacity(.7)
QtWidgets.QStyledItemDelegate.paint(self, painter, option, index)
painter.setOpacity(1)
x_draw = option.rect.bottomRight().x() - 30
y_draw = option.rect.bottomRight().y() - 35
painter.drawPixmap(x_draw, y_draw, read_icon)
return
QtWidgets.QStyledItemDelegate.paint(self, painter, option, index)
if position: if position:
current_chapter = position['current_chapter'] current_chapter = position['current_chapter']
total_chapters = position['total_chapters'] total_chapters = position['total_chapters']
progress_percent = int(current_chapter * 100 / total_chapters) progress_percent = int(current_chapter * 100 / total_chapters)
if not file_exists: if current_chapter == total_chapters:
read_icon = QtGui.QIcon.fromTheme('vcs-conflicting').pixmap(36)
QtWidgets.QStyledItemDelegate.paint(self, painter, option, index)
elif current_chapter == total_chapters:
QtWidgets.QStyledItemDelegate.paint(self, painter, option, index) QtWidgets.QStyledItemDelegate.paint(self, painter, option, index)
read_icon = QtGui.QIcon.fromTheme('vcs-normal').pixmap(36) read_icon = QtGui.QIcon.fromTheme('vcs-normal').pixmap(36)
elif current_chapter == 1: elif current_chapter == 1:
@@ -450,9 +457,6 @@ class LibraryDelegate(QtWidgets.QStyledItemDelegate):
if current_chapter != 1: if current_chapter != 1:
painter.drawPixmap(x_draw, y_draw, read_icon) painter.drawPixmap(x_draw, y_draw, read_icon)
else:
QtWidgets.QStyledItemDelegate.paint(self, painter, option, index)
class MyAbsModel(QtGui.QStandardItemModel, QtCore.QAbstractItemModel): class MyAbsModel(QtGui.QStandardItemModel, QtCore.QAbstractItemModel):
def __init__(self, parent=None): def __init__(self, parent=None):