Library filtering menu. Also applied to table model.

This commit is contained in:
BasioMeusPuga
2018-01-21 23:06:43 +05:30
parent 7c6993a170
commit 5ed5ce0504
5 changed files with 49 additions and 13 deletions

2
TODO
View File

@@ -26,6 +26,7 @@ TODO
Information dialog widget
Context menu: Cache, Read, Edit database, delete, Mark read/unread
Set focus to newly added file
Allow editing of database data through the UI
Reading:
✓ Drop down for TOC
✓ Override the keypress event of the textedit
@@ -44,6 +45,7 @@ TODO
Record progress
Pagination
Set context menu for definitions and the like
Line spacing
Filetypes:
✓ cbz, cbr support
✓ Keep font settings enabled but only for background color

View File

@@ -49,7 +49,7 @@ class MainUI(QtWidgets.QMainWindow, mainwindow.Ui_MainWindow):
self.current_profile_index = None
self.comic_profile = {}
self.database_path = None
self.library_filter_menu = None
self.active_library_filters = []
# Initialize toolbars
self.libraryToolBar = LibraryToolBar(self)
@@ -185,6 +185,8 @@ class MainUI(QtWidgets.QMainWindow, mainwindow.Ui_MainWindow):
self.lib_ref.create_table_model()
self.lib_ref.create_proxymodel()
self.lib_ref.generate_library_tags()
self.set_library_filter()
self.start_culling_timer()
# ListView
self.listView.setGridSize(QtCore.QSize(175, 240))
@@ -327,6 +329,7 @@ class MainUI(QtWidgets.QMainWindow, mainwindow.Ui_MainWindow):
# Remember file addition modality
# If a file is added from here, it should not be removed
# from the libary in case of a database refresh
# Individually added files are not subject to library filtering
opened_files = QtWidgets.QFileDialog.getOpenFileNames(
self, 'Open file', self.settings['last_open_path'],
@@ -827,8 +830,27 @@ class MainUI(QtWidgets.QMainWindow, mainwindow.Ui_MainWindow):
self.libraryToolBar.libraryFilterButton.setMenu(self.library_filter_menu)
def set_library_filter(self, event=None):
print(event)
print(self.sender().text())
self.active_library_filters = []
something_was_unchecked = False
if self.sender(): # Program startup sends a None here
if self.sender().text() == 'All':
for i in self.library_filter_menu.actions():
i.setChecked(self.sender().isChecked())
for i in self.library_filter_menu.actions()[:-2]:
if i.isChecked():
self.active_library_filters.append(i.text())
else:
something_was_unchecked = True
if something_was_unchecked:
self.library_filter_menu.actions()[-1].setChecked(False)
else:
self.library_filter_menu.actions()[-1].setChecked(True)
self.lib_ref.update_proxymodel()
self.lib_ref.update_table_proxy_model()
def toggle_toolbars(self):
self.settings['show_toolbars'] = not self.settings['show_toolbars']

View File

@@ -35,9 +35,6 @@ class DatabaseInit:
def create_database(self):
# TODO
# Add separate columns for:
# directory tags
# bookmarks
# date added
# addition mode
self.database.execute(
"CREATE TABLE books \

View File

@@ -26,7 +26,7 @@ import pathlib
from PyQt5 import QtGui, QtCore
import database
from models import MostExcellentTableModel, TableProxyModel
from models import MostExcellentTableModel, TableProxyModel, ItemProxyModel
class Library:
@@ -131,6 +131,9 @@ class Library:
item.setToolTip(tooltip_string)
# The following order is needed to keep sorting working
# search_workaround_base is present in 2 places so that the UserRole + 10
# value can be used to create a new UserRole + 4 value whenever there's
# a change in the directory tags
item.setData(title, QtCore.Qt.UserRole)
item.setData(author, QtCore.Qt.UserRole + 1)
item.setData(year, QtCore.Qt.UserRole + 2)
@@ -169,7 +172,9 @@ class Library:
def update_table_proxy_model(self):
self.table_proxy_model.invalidateFilter()
self.table_proxy_model.setFilterParams(
self.parent.libraryToolBar.searchBar.text(), [0, 1, 4])
self.parent.libraryToolBar.searchBar.text(),
[0, 1, 4],
self.parent.active_library_filters)
# This isn't needed, but it forces a model update every time the
# text in the line edit changes. So I guess it is needed.
self.table_proxy_model.setFilterFixedString(
@@ -177,6 +182,7 @@ class Library:
def create_proxymodel(self):
self.proxy_model = QtCore.QSortFilterProxyModel()
# self.proxy_model = ItemProxyModel()
self.proxy_model.setSourceModel(self.view_model)
self.proxy_model.setSortCaseSensitivity(False)
s = QtCore.QSize(160, 250) # Set icon sizing here

View File

@@ -21,14 +21,13 @@ import pathlib
from PyQt5 import QtCore, QtWidgets
from resources import pie_chart
class ItemProxyModel(QtCore.QSortFilterProxyModel):
# TODO
# Implement filterAcceptsRow
def __init__(self, parent=None):
super(ItemProxyModel, self).__init__(parent)
def filterAcceptsRow(self, row, parent):
return True
class MostExcellentTableModel(QtCore.QAbstractTableModel):
# Sorting is taken care of by the QSortFilterProxy model
@@ -131,10 +130,12 @@ class TableProxyModel(QtCore.QSortFilterProxyModel):
super(TableProxyModel, self).__init__(parent)
self.filter_string = None
self.filter_columns = None
self.active_library_filters = None
def setFilterParams(self, filter_text, filter_columns):
def setFilterParams(self, filter_text, filter_columns, active_library_filters):
self.filter_string = filter_text.lower()
self.filter_columns = filter_columns
self.active_library_filters = active_library_filters
def filterAcceptsRow(self, row_num, parent):
if self.filter_string is None or self.filter_columns is None:
@@ -152,6 +153,14 @@ class TableProxyModel(QtCore.QSortFilterProxyModel):
except IndexError: # Columns 7 and 8 are added after creation of the model
pass
# Filter out all books not in the active library filters
if self.active_library_filters:
current_library_name = valid_data[-2]
if current_library_name not in self.active_library_filters:
return False
else:
return False
for i in valid_data:
if i:
if self.filter_string in i: