From 62c44730d875cc5e73544b722993994676e730fc Mon Sep 17 00:00:00 2001 From: BasioMeusPuga Date: Thu, 24 May 2018 15:14:56 -0400 Subject: [PATCH] Start search functionality Multiple fixes --- TODO | 1 + lector/__main__.py | 62 ++++++++++++++++++++++++++++++++++++---------- lector/widgets.py | 2 +- setup.py | 2 +- 4 files changed, 52 insertions(+), 15 deletions(-) diff --git a/TODO b/TODO index 7994369..b0c2546 100644 --- a/TODO +++ b/TODO @@ -83,6 +83,7 @@ TODO Deselecting all directories in the settings dialog also filters out manually added books Clean up 'switch' page layout Colors aren't loaded properly for annotation previews + Cover page shouldn't be scolled midway Secondary: Graphical themes diff --git a/lector/__main__.py b/lector/__main__.py index 3f3d4d8..bca7421 100755 --- a/lector/__main__.py +++ b/lector/__main__.py @@ -182,6 +182,7 @@ class MainUI(QtWidgets.QMainWindow, mainwindow.Ui_MainWindow): self.bookToolBar.profileBox.currentIndexChanged.connect( self.profile_functions.format_contentView) self.bookToolBar.profileBox.setCurrentIndex(self.current_profile_index) + self.bookToolBar.searchBar.textChanged.connect(self.search_book) self.bookToolBar.fontBox.currentFontChanged.connect(self.modify_font) self.bookToolBar.fontSizeBox.currentIndexChanged.connect(self.modify_font) @@ -211,6 +212,9 @@ class MainUI(QtWidgets.QMainWindow, mainwindow.Ui_MainWindow): self.bookToolBar.tocBox.currentIndexChanged.connect(self.set_toc_position) self.addToolBar(self.bookToolBar) + # Get the stylesheet of the default QLineEdit + self.lineEditStyleSheet = self.bookToolBar.searchBar.styleSheet() + # Make the correct toolbar visible self.current_tab = self.tabWidget.currentIndex() self.tab_switch() @@ -330,6 +334,11 @@ class MainUI(QtWidgets.QMainWindow, mainwindow.Ui_MainWindow): def process_post_hoc_files(self, file_list, open_files_after_processing): # Takes care of both dragged and dropped files # As well as files sent as command line arguments + + file_list = [i for i in file_list if os.path.exists(i)] + if not file_list: + return + books = sorter.BookSorter( file_list, ('addition', 'manual'), @@ -338,7 +347,7 @@ class MainUI(QtWidgets.QMainWindow, mainwindow.Ui_MainWindow): self.temp_dir.path()) parsed_books = books.initiate_threads() - if not parsed_books: + if not parsed_books and not open_files_after_processing: return database.DatabaseFunctions(self.database_path).add_to_database(parsed_books) @@ -618,6 +627,10 @@ class MainUI(QtWidgets.QMainWindow, mainwindow.Ui_MainWindow): self.statusMessage.setText( str(self.lib_ref.itemProxyModel.rowCount()) + self._translate('Main_UI', ' Books')) + + if self.libraryToolBar.searchBar.text() != '': + self.statusBar.setVisible(True) + else: if self.settings['show_bars']: @@ -631,8 +644,6 @@ class MainUI(QtWidgets.QMainWindow, mainwindow.Ui_MainWindow): if self.bookToolBar.fontButton.isChecked(): self.bookToolBar.customize_view_on() - current_title = current_metadata['title'] - current_author = current_metadata['author'] current_position = current_metadata['position'] current_toc = [i[0] for i in current_metadata['content']] @@ -648,8 +659,7 @@ class MainUI(QtWidgets.QMainWindow, mainwindow.Ui_MainWindow): self.profile_functions.format_contentView() - self.statusMessage.setText( - current_author + ' - ' + current_title) + self.statusBar.setVisible(False) def tab_close(self, tab_index=None): if not tab_index: @@ -748,6 +758,30 @@ class MainUI(QtWidgets.QMainWindow, mainwindow.Ui_MainWindow): #____________________________________________ + def search_book(self, search_text): + current_tab = self.tabWidget.currentIndex() + if not (current_tab != 0 and not self.tabWidget.widget( + current_tab).are_we_doing_images_only): + return + + contentView = self.tabWidget.widget(current_tab).contentView + + text_cursor = contentView.textCursor() + something_found = True + if search_text: + text_cursor.setPosition(0, QtGui.QTextCursor.MoveAnchor) + contentView.setTextCursor(text_cursor) + contentView.verticalScrollBar().setValue(contentView.verticalScrollBar().maximum()) + something_found = contentView.find(search_text) + else: + text_cursor.clearSelection() + contentView.setTextCursor(text_cursor) + + if not something_found: + self.bookToolBar.searchBar.setStyleSheet("QLineEdit {color: red;}") + else: + self.bookToolBar.searchBar.setStyleSheet(self.lineEditStyleSheet) + def generate_library_context_menu(self, position): index = self.sender().indexAt(position) if not index.isValid(): @@ -791,8 +825,6 @@ class MainUI(QtWidgets.QMainWindow, mainwindow.Ui_MainWindow): if action == editAction: edit_book = selected_indexes[0] - metadata = self.lib_ref.libraryModel.data( - edit_book, QtCore.Qt.UserRole + 3) is_cover_loaded = self.lib_ref.libraryModel.data( edit_book, QtCore.Qt.UserRole + 8) @@ -805,15 +837,19 @@ class MainUI(QtWidgets.QMainWindow, mainwindow.Ui_MainWindow): self.database_path).fetch_covers_only([book_hash])[0][1] self.cover_functions.cover_loader(book_item, book_cover) - cover = self.lib_ref.libraryModel.item(edit_book.row()).icon() - title = metadata['title'] - author = metadata['author'] - year = str(metadata['year']) - tags = metadata['tags'] + cover = self.lib_ref.libraryModel.item( + edit_book.row()).icon() + title = self.lib_ref.libraryModel.data( + edit_book, QtCore.Qt.UserRole) + author = self.lib_ref.libraryModel.data( + edit_book, QtCore.Qt.UserRole + 1) + year = str(self.lib_ref.libraryModel.data( + edit_book, QtCore.Qt.UserRole + 2)) # Text cannot be int + tags = self.lib_ref.libraryModel.data( + edit_book, QtCore.Qt.UserRole + 4) self.metadataDialog.load_book( cover, title, author, year, tags, edit_book) - self.metadataDialog.show() if action == deleteAction: diff --git a/lector/widgets.py b/lector/widgets.py index 51cd6eb..7c038e8 100644 --- a/lector/widgets.py +++ b/lector/widgets.py @@ -228,7 +228,7 @@ class Tab(QtWidgets.QWidget): try: required_position = self.metadata['position']['cursor_position'] except KeyError: - print(f'Database: Cursor position error. Recommend retry.') + print('Database: Cursor position error. Recommend retry.') return if cursor_position: diff --git a/setup.py b/setup.py index ea7826b..5aedc16 100644 --- a/setup.py +++ b/setup.py @@ -6,7 +6,7 @@ HERE = path.abspath(path.dirname(__file__)) MAJOR_VERSION = '0' MINOR_VERSION = '4' -MICRO_VERSION = '0' +MICRO_VERSION = '1' VERSION = "{}.{}.{}".format(MAJOR_VERSION, MINOR_VERSION, MICRO_VERSION) # Get the long description from the README file