kindle manager

This commit is contained in:
gavin
2020-06-28 17:50:31 +08:00
parent 3117b4f55a
commit 674daeb5e9
8 changed files with 120 additions and 150 deletions

View File

@@ -19,7 +19,7 @@ import threading
# os.environ['PATH'] = sys._MEIPASS + ";" + os.environ['PATH']
from PySide2.QtWidgets import (QMainWindow, QApplication, QMessageBox,
QFileDialog, QLabel, QAbstractItemView, QHeaderView)
QFileDialog, QLabel, QAbstractItemView, QHeaderView, QMenu)
from PySide2.QtCore import (QAbstractTableModel, Signal, QSize, QTimer, Qt)
from PySide2.QtGui import (QPalette, QStandardItemModel, QStandardItem, QIcon)
@@ -75,6 +75,8 @@ class kmanWindow(QMainWindow):
def __init__(self, parent=None):
super(kmanWindow, self).__init__(parent)
self.copyinfo = ''
# create ui and initial it
ui = Ui_MainWindow()
ui.setupUi(self)
@@ -92,6 +94,7 @@ class kmanWindow(QMainWindow):
### substitute pandas table with mTable
self.mt = mTable()
self.books_info = defaultdict(dict)
# initial check order:
# 1. backup file bk.data ->
# 2. kindle(My Clippings.txt) ->
@@ -118,7 +121,6 @@ class kmanWindow(QMainWindow):
# if the information exist in backup file, initial with this file,
# and grap new book's information from douban
# else grap all book information from douban
self.books_info = defaultdict(dict)
try:
if os.path.exists(BACKUPINFOFN):
self.books_info = self.km.json2dict(BACKUPINFOFN)
@@ -158,7 +160,8 @@ class kmanWindow(QMainWindow):
#ui.searchLineEdit.returnPressed.connect(self.search_return_press())
ui.searchComboBox.currentIndexChanged.connect(self.search_scope_change)
ui.searchToolButton.clicked.connect(self.search_button_clicked)
ui.treeView.clicked.connect(self.tree_item_clicked)
#ui.treeView.clicked.connect(self.tree_item_clicked)
ui.treeView.selectionModel().selectionChanged.connect(self.tree_item_selectionchanged)
ui.tableView.clicked.connect(self.table_item_clicked)
ui.tableView.horizontalHeader().setStretchLastSection(True)
#ui.tableView.horizontalHeader().setSectionResizeMode(QHeaderView.ResizeToContents)
@@ -174,7 +177,10 @@ class kmanWindow(QMainWindow):
ui.tableView.verticalHeader().hide()
ui.tableView.setModel(self.ui.tablemodel)
ui.tableView.setSortingEnabled(True)
ui.tableView.selectionModel().selectionChanged.connect(self.table_item_selectionchanged)
#ui.tableView.contextMenuEvent().selectionChanged.connect(self.table_item_selectionchanged)
# XXXX
self.ui.textEdit.installEventFilter(self)
# XXXX
@@ -194,21 +200,6 @@ class kmanWindow(QMainWindow):
else:
super(kmanWindow, self).eventFilter(source, event)
"""
app = QApplication(sys.argv)
editor = MyWidget()
cursor = editor.textCursor()
fmt = cursor.charFormat()
fmt.setForeground(QColor('blue'))
address = 'http://example.com'
fmt.setAnchor(True)
fmt.setAnchorHref(address)
fmt.setToolTip(address)
cursor.insertText("Hello world again", fmt)
editor.show()
app.exec_()
"""
def check_increase_books(self, bks, bksinfo):
new_list = list(bks.keys()) # kindle's books with note
new_list = [re.split(r'[\(\-\:_\s]',nn.strip())[0] for nn in new_list]
@@ -256,6 +247,7 @@ class kmanWindow(QMainWindow):
self.ui.tableView.verticalHeader().hide()
self.ui.tableView.setModel(self.ui.tablemodel)
#self.ui.tablemodel.tabledata_update.connect(self.tabledata_update_slot)
self.ui.tableView.selectionModel().selectionChanged.connect(self.table_item_selectionchanged)
# refresh textedit content
if comp in [1,2]:
@@ -265,6 +257,11 @@ class kmanWindow(QMainWindow):
note=stype, content=scontent, position=sposition)
print(re.split(r'[\(\-\:_\s]',sbookname.strip())[0])
self.ui.textEdit.setHtml(tt)
self.copyinfo = \
""" {content} \n\n--《{bookname}{author} ({time}) 【{note} P{position}""". \
format( content=scontent, bookname=sbookname, author=sauthor, time=stime,
note=stype, position=sposition )
elif comp in [3,4]:
self.render_textedit_words(self.words_data)
elif comp == 5:
@@ -282,6 +279,7 @@ class kmanWindow(QMainWindow):
word = index.sibling(index.row(), 0).data()
txt = ""
self.copyinfo = ''
for row in lookups:
if words[row[1]]['word'] == word:
[susage, stimestamp, sbookname, sauthor, scategory, sposition] = \
@@ -293,6 +291,12 @@ class kmanWindow(QMainWindow):
author=sauthor,category=scategory,
timestamp=stimestamp,position=sposition)
self.copyinfo += \
"""{usage} \n\n-- {bookname} {author} {category} {timestamp} {position} \n """ \
.format( usage=susage,bookname=sbookname, \
author=sauthor,category=scategory, \
timestamp=stimestamp,position=sposition)
self.ui.textEdit.setHtml(txt)
'''
@@ -321,10 +325,24 @@ class kmanWindow(QMainWindow):
self.ui.textEdit.setOpenExternalLinks(True)
self.ui.textEdit.setHtml(infos_temp.format(link=vv['link'],
bookname=vv['bookname'],
author=vv['author'], ratenum=vv['ratenum'],
score=vv['score'], publisher=vv['publisher'],
publishing=vv['publishing'],description=vv['description'],
author=vv['author'],
ratenum=vv['ratenum'],
score=vv['score'],
publisher=vv['publisher'],
publishing=vv['publishing'],
description=vv['description'],
img=vv['img'].split('/')[-1]))
self.copyinfo = """ 1 | :green_book: | [{bookname}]({link}) | """ \
"""{author}/{score}/{publisher}/{publishing} {description}""" \
.format( bookname=vv['bookname'], \
link=vv['link'],
author=vv['author'],
score=vv['score'],
publisher=vv['publisher'],
publishing=vv['publishing'],
description=vv['description'],)
else: pass
@@ -345,7 +363,15 @@ class kmanWindow(QMainWindow):
def tabledata_update_slot(self, s):
print('call tabledata_update_slot() {}'.format(s))
def tree_item_selectionchanged(self, selected, deselected):
# selected, deselected are QItemSelection objects
model_index_list = selected.indexes() # QModelIndexList
if not model_index_list: return 0
model_index = model_index_list[0] # QModelIndex
self.tree_item_clicked(model_index)
def tree_item_clicked(self, modelidx):
# modelidx is a QModelIndex object
model_index_list = self.ui.treeView.selectedIndexes() # QModelIndexList
model_index = model_index_list[0] # QModelIndex
itemmodel = model_index.model() #QAbstractItemModel/QStandardItemModel
@@ -360,6 +386,7 @@ class kmanWindow(QMainWindow):
comp = 0
if self.tree_selected.split('_')[0] == 'info':
self.ui.tableView.setVisible(False)
self.ui.tableView.clearSelection()
self.render_textedit_infos(self.books_info, item.text())
comp = 5
else:
@@ -398,6 +425,13 @@ class kmanWindow(QMainWindow):
self.refresh_ui_component(comp)
def table_item_selectionchanged(self, selected, deselected):
# selected, deselected are QItemSelection objects
model_index_list = selected.indexes() # QModelIndexList
if not model_index_list: return 0
model_index = model_index_list[0] # QModelIndex
self.table_item_clicked(model_index)
def table_item_clicked(self, index):
"""
print('tableView.currentIndex().row() {} tableView.currentIndex().column() {}'
@@ -419,7 +453,11 @@ class kmanWindow(QMainWindow):
self.ui.textEdit.setHtml(notes_temp.format(
bookname=sbookname, author=sauthor, time=stime,
note=stype, content=scontent, position=sposition))
elif ss=='note':
self.copyinfo = \
""" {content} \n\n-- 《{bookname}{author} ({time}) 【{note} P{position}""". \
format( content=scontent, bookname=sbookname, author=sauthor, time=stime,
note=stype, position=sposition )
elif ss=='word':
self.render_textedit_words(self.words_data)
def search_button_clicked(self):
@@ -443,6 +481,39 @@ class kmanWindow(QMainWindow):
print('call keyPressEvent() {} '.format(event.key()))
self.search_button_clicked()
def contextMenuEvent(self, event):
# do not work!?
#if(self.ui.tableView.geometry().contains(event.pos())):
# print('contextMenuEvent pos {}'.format(event.globalPos()))
tree_index_list = self.ui.treeView.selectedIndexes() # QModelIndexList
table_index_list = self.ui.tableView.selectedIndexes()
# tableview item selected
if table_index_list:
model_index = table_index_list[0] # QModelIndex
print('contextMenuEvent pos {}'.format(event.globalPos()))
menu = QMenu()
copyAction = menu.addAction("Copy")
action = menu.exec_(self.mapToGlobal(event.pos()))
if action ==copyAction:
self.copyAction(model_index.row(), model_index.column())
elif tree_index_list and self.tree_selected.split('_')[0]=='info':
model_index = tree_index_list[0] # QModelIndex
print('contextMenuEvent pos {}'.format(event.globalPos()))
menu = QMenu()
copyAction = menu.addAction("Copy")
action = menu.exec_(self.mapToGlobal(event.pos()))
if action ==copyAction:
self.copyAction(model_index.row(), model_index.column())
def copyAction(self, row, column):
import clipboard
clipboard.copy(self.copyinfo)
print('copyAction row {} column {}'.format(row, column))
def search_return_press(self):
self.search_button_clicked()