Skip to content

Commit

Permalink
1.4.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
noembryo committed Jun 14, 2021
1 parent 18ee49f commit 9173325
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 41 deletions.
1 change: 1 addition & 0 deletions boot_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ def except_hook(class_type, value, trace_back):


BOOKS_VIEW, HIGHLIGHTS_VIEW = range(2) # app views
CHANGE_DB, NEW_DB, RELOAD_DB = range(3) # db change mode
TITLE, AUTHOR, TYPE, PERCENT, RATING, MODIFIED, PATH = range(7) # file_table columns
PAGE, HIGHLIGHT_TEXT, DATE, PAGE_ID, COMMENT = range(5) # high_list item data
(HIGHLIGHT_H, COMMENT_H,
Expand Down
10 changes: 6 additions & 4 deletions gui_toolbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# Form implementation generated from reading ui file 'D:\Apps\DEV\PROJECTS\KoHighlights\gui_toolbar.ui'
#
# Created: Mon Dec 21 18:01:29 2020
# Created: Mon Jun 14 19:48:17 2021
# by: pyside-uic 0.2.15 running on PySide 1.2.4
#
# WARNING! All changes made in this file will be lost!
Expand Down Expand Up @@ -198,7 +198,7 @@ def setupUi(self, ToolBar):
self.loaded_btn.setAutoRaise(True)
self.loaded_btn.setObjectName("loaded_btn")
self.verticalLayout.addWidget(self.loaded_btn)
self.db_btn = QtGui.QToolButton(self.mode_grp)
self.db_btn = XToolButton(self.mode_grp)
sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Preferred, QtGui.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
Expand Down Expand Up @@ -268,11 +268,13 @@ def retranslateUi(self, ToolBar):
self.loaded_btn.setToolTip(QtGui.QApplication.translate("ToolBar", "Show the loaded files", None, QtGui.QApplication.UnicodeUTF8))
self.loaded_btn.setStatusTip(QtGui.QApplication.translate("ToolBar", "Show the loaded files", None, QtGui.QApplication.UnicodeUTF8))
self.loaded_btn.setText(QtGui.QApplication.translate("ToolBar", "Loaded", None, QtGui.QApplication.UnicodeUTF8))
self.db_btn.setToolTip(QtGui.QApplication.translate("ToolBar", "Show the archived files", None, QtGui.QApplication.UnicodeUTF8))
self.db_btn.setStatusTip(QtGui.QApplication.translate("ToolBar", "Show the archived files", None, QtGui.QApplication.UnicodeUTF8))
self.db_btn.setToolTip(QtGui.QApplication.translate("ToolBar", "Show the archived files in the database\n"
"(Right click to change the database file)", None, QtGui.QApplication.UnicodeUTF8))
self.db_btn.setStatusTip(QtGui.QApplication.translate("ToolBar", "Show the archived files in the database (Right click to change the database file)", None, QtGui.QApplication.UnicodeUTF8))
self.db_btn.setText(QtGui.QApplication.translate("ToolBar", "Archived", None, QtGui.QApplication.UnicodeUTF8))
self.about_btn.setToolTip(QtGui.QApplication.translate("ToolBar", "Info about the KoHighlights", None, QtGui.QApplication.UnicodeUTF8))
self.about_btn.setStatusTip(QtGui.QApplication.translate("ToolBar", "Info about the KoHighlights", None, QtGui.QApplication.UnicodeUTF8))
self.about_btn.setText(QtGui.QApplication.translate("ToolBar", "About", None, QtGui.QApplication.UnicodeUTF8))

from secondary import XToolButton
import images_rc
1 change: 1 addition & 0 deletions images.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<file alias='file_edit.png'>stuff/file_edit.png</file>
<file alias='folder_reader.png'>stuff/folder_reader.png</file>
<file alias='sort.png'>stuff/sort.png</file>
<file alias='db_open.png'>stuff/db_open.png</file>
<file alias='db_add.png'>stuff/db_add.png</file>
<file alias='books.png'>stuff/books.png</file>
<file alias='view_books.png'>stuff/view_books.png</file>
Expand Down
8 changes: 4 additions & 4 deletions images_rc.py

Large diffs are not rendered by default.

90 changes: 68 additions & 22 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@


__author__ = "noEmbryo"
__version__ = "1.4.0.0"
__version__ = "1.4.2.0"


def _(text): # for future gettext support
Expand Down Expand Up @@ -157,6 +157,7 @@ def __init__(self, parent=None):
self.high_merge_warning = True
self.archive_warning = True
self.exit_msg = True
self.db_path = join(SETTINGS_DIR, "data.db")
self.date_vacuumed = datetime.now().strftime(DATE_FORMAT)
# ___ ___________________________________

Expand Down Expand Up @@ -214,6 +215,7 @@ def __init__(self, parent=None):
self.ico_label_green = QIcon(":/stuff/label_green.png")
self.ico_view_books = QIcon(":/stuff/view_books.png")
self.ico_db_add = QIcon(":/stuff/db_add.png")
self.ico_db_open = QIcon(":/stuff/db_open.png")
self.ico_app = QIcon(":/stuff/logo64.png")
self.ico_empty = QIcon(":/stuff/trans32.png")
self.ico_refresh = QIcon(":/stuff/refresh16.png")
Expand Down Expand Up @@ -257,8 +259,8 @@ def __init__(self, parent=None):
def on_load(self):
""" Things that must be done after the initialization
"""
self.init_db()
self.settings_load()
self.init_db()
if FIRST_RUN: # on first run
self.toolbar.loaded_btn.click()
self.splitter.setSizes((500, 250))
Expand Down Expand Up @@ -382,10 +384,9 @@ def bye_bye_stuff(self):
def init_db(self):
""" Initialize the database tables
"""
db_path = join(SETTINGS_DIR, "data.db")
# noinspection PyTypeChecker,PyCallByClass
self.db = QSqlDatabase.addDatabase("QSQLITE")
self.db.setDatabaseName(db_path)
self.db.setDatabaseName(self.db_path)
if not self.db.open():
print("Could not open database!")
return
Expand All @@ -395,7 +396,7 @@ def init_db(self):
# self.query.exec_("""PRAGMA user_version""") # 2do: enable if db changes
# while self.query.next():
# self.check_db_version(self.query.value(0)) # check the db version
self.set_db_version() if not isfile(db_path) else None
self.set_db_version() if not isfile(self.db_path) else None
self.create_books_table()

def check_db_version(self, version):
Expand All @@ -404,7 +405,7 @@ def check_db_version(self, version):
:type version: int
:param version: The db file version
"""
if version == DB_VERSION or not isfile(join(SETTINGS_DIR, "data.db")):
if version == DB_VERSION or not isfile(self.db_path):
return # the db is up to date or does not exists yet
self.update_db(version)

Expand All @@ -413,6 +414,48 @@ def set_db_version(self):
"""
self.query.exec_("""PRAGMA user_version = {}""".format(DB_VERSION))

def change_db(self, mode):
""" Changes the current db file
:type mode: int
:param mode: Change, create new or reload the current db
"""
if mode == NEW_DB:
# noinspection PyCallByClass
filename = QFileDialog.getSaveFileName(self, _("Type the name of the new db"),
self.db_path,
(_("database files (*.db)")))[0]
elif mode == CHANGE_DB:
# noinspection PyCallByClass
filename = QFileDialog.getOpenFileName(self, _("Select a database file"),
self.db_path,
(_("database files (*.db)")))[0]
elif mode == RELOAD_DB:
filename = self.db_path
else:
return
if filename:
# self.toolbar.loaded_btn.click()
if self.toolbar.db_btn.isChecked():
self.toolbar.update_loaded()
self.delete_data()
self.db_path = filename
self.db_mode = False
self.init_db()
self.read_books_from_db()
if self.toolbar.db_btn.isChecked():
# noinspection PyTypeChecker,PyCallByClass
QTimer.singleShot(0, self.toolbar.update_archived)

def delete_data(self):
""" Deletes the database data
"""
self.db.close() # close the db
self.db = None
self.query = None
# print(self.db.connectionNames())
# self.db.removeDatabase(self.db.connectionName())

def create_books_table(self):
""" Create the books table
"""
Expand Down Expand Up @@ -1565,7 +1608,8 @@ def sort_high4write(self, data):
:type data: tuple
param: data: The highlight's data
"""
return int(data[3][5:]) if self.high_by_page else data[0]
return int(data[3][5:]) if (self.high_by_page and
self.status.act_page.isChecked()) else data[0]

# ___ ___________________ MERGING - SYNCING STUFF _______________

Expand Down Expand Up @@ -2115,20 +2159,19 @@ def save_multi_files(self, dir_path, format_):
self.status.act_date.isChecked() else "")
line_break = (":" + os.linesep if self.status.act_page.isChecked() or
self.status.act_date.isChecked() else "")
html = format_ == MANY_HTML
encoding = "utf-8-sig" if format_ == MANY_CSV else "utf-8"
for idx in self.sel_indexes:
(authors, title, highlights,
title_counter) = self.get_item_data(idx, format_, title_counter)
if not highlights: # no highlights
if not highlights: # no highlights in book
continue
name = title
if authors:
name = "{} - {}".format(authors, title)
if format_ == MANY_TEXT:
ext = ".txt"
text = ""
elif html:
elif format_ == MANY_HTML:
ext = ".html"
text = HTML_HEAD + BOOK_BLOCK % {"title": title, "authors": authors}
elif format_ == MANY_CSV:
Expand All @@ -2140,22 +2183,23 @@ def save_multi_files(self, dir_path, format_):
with open(filename, "w+", encoding=encoding, newline="") as text_file:
for highlight in sorted(highlights, key=self.sort_high4write):
date_text, high_comment, high_text, page_text = highlight
if html:
text += (page_text + space + date_text + line_break +
high_text + high_comment)
text += 2 * os.linesep
elif format_ == MANY_TEXT:
if format_ == MANY_HTML:
text += HIGH_BLOCK % {"page": page_text, "date": date_text,
"highlight": high_text,
"comment": high_comment}
elif format_ == MANY_TEXT:
text += (
page_text + space + date_text + line_break +
high_text + high_comment)
text += 2 * os.linesep
elif format_ == MANY_CSV:
data = {"title": title, "authors": authors, "page": page_text,
"date": date_text, "text": high_text,
"comment": high_comment}
text += get_csv_row(data) + "\n"
else:
return
if html:
if format_ == MANY_HTML:
text += "\n</div>\n</body>\n</html>"

text_file.write(text)
Expand Down Expand Up @@ -2374,6 +2418,7 @@ def settings_load(self):
self.comment_width = app_config.get("comment_width", None)
self.last_dir = app_config.get("last_dir", os.getcwd())
self.current_view = app_config.get("current_view", BOOKS_VIEW)
self.db_path = app_config.get("db_path", join(SETTINGS_DIR, "data.db"))
self.db_mode = app_config.get("db_mode", False)
self.fold_btn.setChecked(app_config.get("show_info", True))
self.opened_times = app_config.get("opened_times", 0)
Expand Down Expand Up @@ -2411,7 +2456,7 @@ def settings_save(self):
"about_geometry": self.pickle(self.about.saveGeometry()),
"col_sort_asc": self.col_sort_asc, "col_sort": self.col_sort,
"col_sort_asc_h": self.col_sort_asc_h, "col_sort_h": self.col_sort_h,
"highlight_width": self.highlight_width,
"highlight_width": self.highlight_width, "db_path": self.db_path,
"comment_width": self.comment_width, "toolbar_size": self.toolbar_size,
"last_dir": self.last_dir, "alt_title_sort": self.alt_title_sort,
"archive_warning": self.archive_warning, "exit_msg": self.exit_msg,
Expand Down Expand Up @@ -2727,10 +2772,11 @@ def __init__(self, *args, **kwargs):
super(KOHighlights, self).__init__(*args, **kwargs)

# decode app's arguments
try:
sys.argv = [i.decode(sys.getfilesystemencoding()) for i in sys.argv]
except AttributeError: # i.decode does not exists in Python 3
pass
# try:
# sys.argv = [i.decode(sys.getfilesystemencoding()) for i in sys.argv]
# except AttributeError: # i.decode does not exists in Python 3
# pass
sys.argv = self.arguments()

self.parser = argparse.ArgumentParser(prog=APP_NAME,
description=_("{} v{} - A KOReader's "
Expand Down Expand Up @@ -3033,7 +3079,7 @@ def cli_sort(args, data):
:type data: tuple
param: data: The highlight's data
"""
return int(data[3][5:]) if args.sort_page else data[0]
return int(data[3][5:]) if (args.sort_page and not args.no_page) else data[0]

def cli_analyze_high(self, data, page, page_id, args):
""" Get the highlight's info (text, comment, date and page)
Expand Down
76 changes: 65 additions & 11 deletions secondary.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
from pprint import pprint

if QT4: # ___ ______________ DEPENDENCIES __________________________
from PySide.QtCore import Qt, Slot, QObject, Signal, QSize, QPoint
from PySide.QtGui import (QApplication, QMessageBox, QIcon, QFileDialog,
QTableWidgetItem, QDialog, QWidget, QMovie, QFont, QMenu,
QAction, QTableWidget, QCheckBox, QToolButton, QActionGroup)
from PySide.QtCore import Qt, Slot, QObject, Signal, QSize, QPoint, QEvent
from PySide.QtGui import (QApplication, QMessageBox, QIcon, QFileDialog, QTableWidgetItem,
QDialog, QWidget, QMovie, QFont, QMenu, QAction, QTableWidget,
QCheckBox, QToolButton, QActionGroup, QCursor)
else:
from PySide2.QtCore import QObject, Qt, Signal, QPoint, Slot, QSize
from PySide2.QtGui import QFont, QMovie, QIcon
Expand All @@ -22,20 +22,15 @@
import requests
from bs4 import BeautifulSoup

from gui_about import Ui_About # ___ ______ GUI STUFF ______________
from gui_auto_info import Ui_AutoInfo
from gui_toolbar import Ui_ToolBar
from gui_status import Ui_Status
from gui_edit import Ui_TextDialog


def _(text): # for future gettext support
return text


__all__ = ("XTableWidgetIntItem", "XTableWidgetPercentItem", "XTableWidgetTitleItem",
"DropTableWidget", "XMessageBox", "About", "AutoInfo", "ToolBar", "TextDialog",
"Status", "LogStream", "Scanner", "HighlightScanner", "ReLoader", "DBLoader")
"Status", "LogStream", "Scanner", "HighlightScanner", "ReLoader", "DBLoader",
"XToolButton")


# ___ _______________________ SUBCLASSING ___________________________
Expand Down Expand Up @@ -136,6 +131,30 @@ def exec_(self, *args, **kwargs):
self.check_box.isChecked())


class XToolButton(QToolButton):
right_clicked = Signal()

def __init__(self, parent=None):
super(XToolButton, self).__init__(parent)
self.installEventFilter(self)

# def mousePressEvent(self, QMouseEvent):
# if QMouseEvent.button() == Qt.RightButton:
# # do what you want here
# print("Right Button Clicked")
# QMouseEvent.accept()

def eventFilter(self, obj, event):
if obj.objectName() == "db_btn":
if event.type() == QEvent.ContextMenu:
self.right_clicked.emit()
return True
else:
return False
else:
# pass the event on to the parent class
return QToolButton.eventFilter(self, obj, event)

# ___ _______________________ WORKERS _______________________________


Expand Down Expand Up @@ -276,6 +295,12 @@ def get_book_highlights(self, data, path):

# ___ _______________________ GUI STUFF _____________________________

from gui_about import Ui_About
from gui_auto_info import Ui_AutoInfo
from gui_toolbar import Ui_ToolBar
from gui_status import Ui_Status
from gui_edit import Ui_TextDialog


class ToolBar(QWidget, Ui_ToolBar):

Expand Down Expand Up @@ -410,6 +435,35 @@ def on_clear_btn_clicked(self):
self.base.file_table.model().removeRows(0, self.base.file_table.rowCount())
self.activate_buttons()

@Slot()
def on_db_btn_right_clicked(self):
""" The context menu of the "Archived" button is pressed
"""
menu = self.create_db_menu()
# noinspection PyArgumentList
menu.exec_(QCursor.pos())

def create_db_menu(self):
""" Create the database menu
"""
menu = QMenu(self)

action = QAction(_("Create new database"), menu)
action.setIcon(self.base.ico_db_add)
action.triggered.connect(partial(self.base.change_db, NEW_DB))
menu.addAction(action)

action = QAction(_("Reload database"), menu)
action.setIcon(self.base.ico_refresh)
action.triggered.connect(partial(self.base.change_db, RELOAD_DB))
menu.addAction(action)

action = QAction(_("Change database"), menu)
action.setIcon(self.base.ico_db_open)
action.triggered.connect(partial(self.base.change_db, CHANGE_DB))
menu.addAction(action)
return menu

def change_view(self):
""" Changes what is shown in the app
"""
Expand Down

0 comments on commit 9173325

Please sign in to comment.