kindle manager
This commit is contained in:
248
kmanapp.py
248
kmanapp.py
@@ -1,19 +1,20 @@
|
||||
|
||||
import sys
|
||||
from time import sleep
|
||||
from threading import Thread
|
||||
import _thread
|
||||
import threading
|
||||
|
||||
from PySide2.QtWidgets import QApplication
|
||||
from PySide2.QtWidgets import QMainWindow
|
||||
from PySide2.QtWidgets import QLabel
|
||||
from PySide2.QtWidgets import QMessageBox
|
||||
from PySide2.QtWidgets import QFileDialog
|
||||
|
||||
from PySide2.QtCore import (QCoreApplication, QDate, QDateTime, QMetaObject,
|
||||
QObject, QPoint, QRect, QSize, QTime, QUrl, Qt)
|
||||
QAbstractTableModel, QObject, QPoint, QRect, QSize, QTime,
|
||||
QUrl, Qt, QThread, Signal, QTimer)
|
||||
from PySide2.QtGui import (QBrush, QColor, QConicalGradient, QCursor, QFont,
|
||||
QFontDatabase, QIcon, QKeySequence, QLinearGradient, QPalette, QPainter,
|
||||
QPixmap, QRadialGradient, QStandardItem, QStandardItemModel)
|
||||
from PySide2.QtWidgets import *
|
||||
#from PySide2.QtWidgets import *
|
||||
|
||||
from mainwindow import Ui_MainWindow
|
||||
from kman import *
|
||||
@@ -21,38 +22,34 @@ from kman import *
|
||||
# import binary resource file(kmanapp_rc.py)
|
||||
import kmanapp_rc
|
||||
|
||||
ONLY_TEST = 0
|
||||
ONLY_TEST = 1
|
||||
|
||||
class kmanWindow(QMainWindow):
|
||||
"""
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(kmanWindow, self).__init__(*args, **kwargs)
|
||||
"""
|
||||
flag = True
|
||||
def __init__(self, parent=None):
|
||||
super(kmanWindow, self).__init__(parent)
|
||||
|
||||
self.stat_str = 'status information'
|
||||
self.search_str = ''
|
||||
self.local_fn = CLIPPATH
|
||||
|
||||
# create ui and initial it
|
||||
ui = Ui_MainWindow()
|
||||
ui.setupUi(self)
|
||||
self.ui = ui
|
||||
|
||||
self.km = kMan()
|
||||
self.books = self.km.import_clips('local')
|
||||
self.books = self.km.import_clips()
|
||||
|
||||
# loop check kindle is connected or not
|
||||
# to be implement
|
||||
"""
|
||||
try:
|
||||
#_thread.start_new_thread(self.check_kindle_status)
|
||||
t1 = threading.Thread(target=check_kindle_status)
|
||||
t1.start()
|
||||
except:
|
||||
print ("Error: can not start thread")
|
||||
"""
|
||||
self.add_ui_component()
|
||||
|
||||
self.show_status_info()
|
||||
self.fill_treeview()
|
||||
|
||||
# timer to check status of kindle
|
||||
self.timer = QTimer(self)
|
||||
self.timer.timeout.connect(self.show_status_info)
|
||||
self.timer.start(1000)
|
||||
|
||||
# connect action/toolbutton to slot functions
|
||||
ui.actionimportkindle.triggered.connect(lambda: self.import_kindle(self.books))
|
||||
@@ -62,18 +59,34 @@ class kmanWindow(QMainWindow):
|
||||
ui.actionstatistic.triggered.connect(lambda: self.statistic())
|
||||
ui.actionhomepage.triggered.connect(lambda: self.homepage())
|
||||
ui.actionabout.triggered.connect(lambda: self.about())
|
||||
ui.actionflush.triggered.connect(lambda: self.flush())
|
||||
ui.actionflush.triggered.connect(lambda: self.refresh())
|
||||
|
||||
ui.searchComboBox.currentIndexChanged.connect(self.search_scope_change)
|
||||
ui.searchToolButton.clicked.connect(self.search_button_clicked)
|
||||
ui.treeView.clicked.connect(self.clicked_items)
|
||||
|
||||
self.add_ui_component()
|
||||
#add_ui_component() ###! can not found this function
|
||||
ui.treeView.clicked.connect(self.tree_item_clicked)
|
||||
ui.tableView.clicked.connect(self.table_item_clicked)
|
||||
|
||||
def add_ui_component(self):
|
||||
self.ui.searchComboBox.addItems(['ALL','bookname','content','author'])
|
||||
|
||||
# status bar
|
||||
self.ui.status_label = QLabel()
|
||||
self.ui.pe = QPalette()
|
||||
|
||||
# table
|
||||
#self.ui.tablemodel=QStandardItemModel(100,5)
|
||||
self.ui.tablemodel= nTableModel(100,5)
|
||||
self.ui.tablemodel.setHorizontalHeaderLabels(
|
||||
[u'Content',u'Type',u'Bookname',u'Author',u'Position',u'Date'])
|
||||
self.ui.tablemodel.update(self.km.filter_clips(self.books))
|
||||
self.ui.tableView.verticalHeader().hide()
|
||||
for row in range(5):
|
||||
for column in range(5):
|
||||
i=QStandardItem("row %s,column %s"%(row,column))
|
||||
self.ui.tablemodel.setItem(row,column,i)
|
||||
self.ui.tableView.setModel(self.ui.tablemodel)
|
||||
|
||||
"""
|
||||
if not ONLY_TEST: # XXXXXXXXXXXXX
|
||||
model = QStandardItemModel()
|
||||
rootItem = model.invisibleRootItem()
|
||||
@@ -113,11 +126,25 @@ class kmanWindow(QMainWindow):
|
||||
icon.addFile(u":/icons/book_open.png", QSize(), QIcon.Normal, QIcon.Off)
|
||||
item.setIcon(icon)
|
||||
parentItem.appendRow(item)
|
||||
self.ui.treeView.setModel(model)
|
||||
"""
|
||||
|
||||
self.ui.treeView.setModel(model)
|
||||
def tree_item_clicked(self, modelidx):
|
||||
model_index_list = self.ui.treeView.selectedIndexes() # QModelIndexList
|
||||
model_index = model_index_list[0] # QModelIndex
|
||||
itemmodel = model_index.model() #QAbstractItemModel/QStandardItemModel
|
||||
item = itemmodel.itemFromIndex(modelidx) #QStandardItem
|
||||
|
||||
def clicked_items(self):
|
||||
print( 'call clicked_items()' )
|
||||
print(item.text())
|
||||
|
||||
# filter clips
|
||||
|
||||
#item = self.ui.treeView.selectedIndexes()[0]
|
||||
#print(item.model().itemFromIndex(modelidx).text())
|
||||
|
||||
def table_item_clicked(self, item):
|
||||
content = item.data()
|
||||
#print('call table_item_clicked() {}'.format(content))
|
||||
|
||||
def search_button_clicked(self):
|
||||
print( 'call search_button_clicked()' )
|
||||
@@ -129,10 +156,11 @@ class kmanWindow(QMainWindow):
|
||||
#print(search_clip(self.books,s,'ALL',p[t]))
|
||||
print('call search_scope_change()')
|
||||
|
||||
## XXXX
|
||||
def check_kindle_status(self):
|
||||
while True:
|
||||
while self.flag:
|
||||
self.show_status_info()
|
||||
sleep(1)
|
||||
sleep(2)
|
||||
|
||||
def show_status_info(self):
|
||||
""" show status information on statusbar
|
||||
@@ -143,33 +171,92 @@ class kmanWindow(QMainWindow):
|
||||
"""
|
||||
status = self.km.status_info()
|
||||
self.ui.statusbar.showMessage(status[0],0)
|
||||
clabel = QLabel(status[1])
|
||||
self.ui.status_label.setText(status[1])
|
||||
if not self.km.status:
|
||||
pe = QPalette()
|
||||
pe.setColor(QPalette.WindowText,Qt.red)
|
||||
#clabel.setAutoFillBackground(True)
|
||||
clabel.setPalette(pe)
|
||||
self.ui.statusbar.addPermanentWidget(clabel, stretch=0)
|
||||
self.ui.pe.setColor(QPalette.WindowText,Qt.red)
|
||||
#self.ui.status_label.setAutoFillBackground(True)
|
||||
self.ui.status_label.setPalette(pe)
|
||||
self.ui.statusbar.addPermanentWidget(self.ui.status_label, stretch=0)
|
||||
|
||||
# define slot functions
|
||||
def import_kindle(self,bks):
|
||||
fp = self.km.get_kindle_path()
|
||||
if not fp:
|
||||
self.messagebox(ico=2, info='\n\n kindle is not connected')
|
||||
return 0
|
||||
|
||||
self.books = self.km.import_clips(self.km.get_kindle_path())
|
||||
|
||||
status = self.km.status_info()
|
||||
self.show_status_info()
|
||||
|
||||
print(bks)
|
||||
|
||||
pass
|
||||
|
||||
def import_local(self):
|
||||
fn, ft = QFileDialog.getOpenFileName(self,
|
||||
"choose file to import",
|
||||
'./', # 起始路径
|
||||
"All Files (*);;Text Files (*.txt)") # 设置文件扩展名过滤,用双分号间隔
|
||||
'./', # start path
|
||||
"All Files (*);;Text Files (*.txt)") # filter file type
|
||||
self.fn = fn
|
||||
|
||||
self.books = self.km.import_clips(fn)
|
||||
|
||||
self.show_status_info()
|
||||
|
||||
#print('filename ', fn, 'filetype ', ft)
|
||||
if fn == "": return False
|
||||
|
||||
def fill_treeview(self):
|
||||
self.ui.model = QStandardItemModel()
|
||||
rootItem = self.ui.model.invisibleRootItem()
|
||||
item = QStandardItem('All Notes')
|
||||
icon = QIcon()
|
||||
icon.addFile(u":/icons/emblem_library.png", QSize(), QIcon.Normal, QIcon.Off)
|
||||
item.setIcon(icon)
|
||||
item.setAccessibleDescription('top')
|
||||
rootItem.appendRow(item)
|
||||
parent_item = item
|
||||
|
||||
# add bookname tree
|
||||
[totalnum, booknum] = self.km.get_bookname_num(self.books)
|
||||
bookname_item = QStandardItem('Bookname ({})'.format(totalnum))
|
||||
icon = QIcon()
|
||||
icon.addFile(u":/icons/book_open.png", QSize(), QIcon.Normal, QIcon.Off)
|
||||
bookname_item.setIcon(icon)
|
||||
bookname_item.setAccessibleDescription('root')
|
||||
parent_item.appendRow(bookname_item)
|
||||
|
||||
if totalnum > 0:
|
||||
for k, v in booknum.items():
|
||||
item = QStandardItem('{} ({})'.format(k, v))
|
||||
icon = QIcon()
|
||||
icon.addFile(u":/icons/book_open_bookmark.png", QSize(), QIcon.Normal, QIcon.Off)
|
||||
item.setIcon(icon)
|
||||
item.setAccessibleDescription('leaf')
|
||||
bookname_item.appendRow(item)
|
||||
|
||||
# add author tree
|
||||
[totalnum, authnum] = self.km.get_author_num(self.books)
|
||||
author_item = QStandardItem('Author ({})'.format(totalnum))
|
||||
icon = QIcon()
|
||||
icon.addFile(u":/icons/person.png", QSize(), QIcon.Normal, QIcon.Off)
|
||||
author_item.setIcon(icon)
|
||||
author_item.setAccessibleDescription('root')
|
||||
parent_item.appendRow(author_item)
|
||||
parent_item = author_item
|
||||
|
||||
if totalnum > 0:
|
||||
for k, v in authnum.items():
|
||||
item = QStandardItem('{} ({})'.format(k, v))
|
||||
icon = QIcon()
|
||||
icon.addFile(u":/icons/user.png", QSize(), QIcon.Normal, QIcon.Off)
|
||||
item.setIcon(icon)
|
||||
item.setAccessibleDescription('leaf')
|
||||
author_item.appendRow(item)
|
||||
|
||||
self.ui.treeView.setModel(self.ui.model)
|
||||
self.ui.treeView.expandAll()
|
||||
|
||||
def config(self):
|
||||
print("call slot config()")
|
||||
pass
|
||||
@@ -187,7 +274,7 @@ class kmanWindow(QMainWindow):
|
||||
pass
|
||||
|
||||
def about(self):
|
||||
self.messagebox('\n'+ \
|
||||
self.messagebox(ico=1, info='\n'+ \
|
||||
' kindle management tool \n\n' + \
|
||||
' v1.0.4\n\n' + \
|
||||
' Author: chengan\n\n' + \
|
||||
@@ -196,28 +283,87 @@ class kmanWindow(QMainWindow):
|
||||
print("call slot about()")
|
||||
pass
|
||||
|
||||
def flush(self):
|
||||
print("call slot flush()")
|
||||
def refresh(self):
|
||||
print("call slot refresh()")
|
||||
pass
|
||||
|
||||
# unify messagebox
|
||||
def messagebox(self, showinfo):
|
||||
def messagebox(self, ico=1, info=''):
|
||||
""" unify messagebox
|
||||
Args: ico - QMessageBox.NoIcon 0
|
||||
QMessageBox.Information 1
|
||||
QMessageBox.Warning 2
|
||||
QMessageBox.Critical 3
|
||||
QMessageBox.Question 4
|
||||
"""
|
||||
icons = {0:QMessageBox.NoIcon, \
|
||||
1:QMessageBox.Information, \
|
||||
2:QMessageBox.Warning, \
|
||||
3:QMessageBox.Critical, \
|
||||
4:QMessageBox.Question }
|
||||
msgBox = QMessageBox()
|
||||
msgBox.setText(showinfo)
|
||||
msgBox.setText(info)
|
||||
msgBox.setInformativeText("")
|
||||
msgBox.setIcon(QMessageBox.Information)
|
||||
msgBox.setIcon(icons[ico])
|
||||
msgBox.setStandardButtons(QMessageBox.Cancel | QMessageBox.Ok)
|
||||
msgBox.setBaseSize(QSize(600, 300))
|
||||
r = msgBox.exec()
|
||||
|
||||
## XXXX
|
||||
def closeEvent(self, e):
|
||||
# stop check thread
|
||||
self.flag = False
|
||||
|
||||
## thanks 勤奋的小青蛙 ^_^ - http://www.jyguagua.com/?p=2615
|
||||
#class nTableModel(QAbstractTableModel):
|
||||
class nTableModel(QStandardItemModel):
|
||||
def __init__(self, parent=None, seclist=None, *args):
|
||||
super(nTableModel, self).__init__()
|
||||
self.seclist = None
|
||||
|
||||
def update(self, seclist):
|
||||
self.seclist = seclist
|
||||
|
||||
def rowCount(self, parent):
|
||||
if not self.seclist: return 0
|
||||
return len(self.seclist)
|
||||
|
||||
def columnCount(self, parent):
|
||||
if not self.seclist: return 0
|
||||
return len(self.seclist[0])
|
||||
|
||||
def data(self, index, role):
|
||||
if not index.isValid():
|
||||
return None
|
||||
elif role != Qt.DisplayRole:
|
||||
return None
|
||||
return self.seclist[index.row()][index.column()]
|
||||
|
||||
def sort(self, col, order):
|
||||
"""sort table by given column number col"""
|
||||
self.emit(SIGNAL("layoutAboutToBeChanged()"))
|
||||
|
||||
self.seclist = sorted(self.seclist, key=operator.itemgetter(col))
|
||||
if order == Qt.DescendingOrder:
|
||||
self.seclist.reverse()
|
||||
|
||||
self.emit(SIGNAL("layoutChanged()"))
|
||||
|
||||
if __name__ == "__main__":
|
||||
import sys
|
||||
from PySide2.QtWidgets import QApplication, QLabel
|
||||
|
||||
app = QApplication(sys.argv)
|
||||
kmw = kmanWindow()
|
||||
kmw.resize(900, 600)
|
||||
kmw.show()
|
||||
|
||||
# loop check kindle is connected or not
|
||||
# BUG to be implement XXXX
|
||||
"""
|
||||
try:
|
||||
t = threading.Thread(target=kmw.check_kindle_status)
|
||||
t.start()
|
||||
except:
|
||||
print ("Error: can not start thread")
|
||||
"""
|
||||
|
||||
app.exec_()
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user