Finished implementing add/delete functions for library. Fixed a bug in the cover display code

This commit is contained in:
Kovid Goyal 2006-12-04 06:59:33 +00:00
parent ddc2e5bbdd
commit 844f4e1e08
2 changed files with 41 additions and 54 deletions

View File

@ -56,6 +56,7 @@ class LibraryDatabase(object):
id = self.con.execute("select max(id) from books_meta").next()[0] id = self.con.execute("select max(id) from books_meta").next()[0]
self.con.execute("insert into books_data values (?,?,?)", (id, ext, sqlite.Binary(file))) self.con.execute("insert into books_data values (?,?,?)", (id, ext, sqlite.Binary(file)))
self.con.commit() self.con.commit()
return id
def get_row_by_id(self, id, columns): def get_row_by_id(self, id, columns):
""" @param columns: list of column names """ """ @param columns: list of column names """

View File

@ -220,7 +220,8 @@ class LibraryBooksModel(QAbstractTableModel):
self.image_file.write(cover) self.image_file.write(cover)
self.image_file.flush() self.image_file.flush()
pix.loadFromData(cover, "", Qt.AutoColor) pix.loadFromData(cover, "", Qt.AutoColor)
cover = pix.scaledToHeight(COVER_HEIGHT, Qt.SmoothTransformation) if not pix.isNull(): cover = pix.scaledToHeight(COVER_HEIGHT, Qt.SmoothTransformation)
else: self.image_file, cover = None, DEFAULT_BOOK_COVER
return row["title"], row["authors"], human_readable(int(row["size"])), exts, cover return row["title"], row["authors"], human_readable(int(row["size"])), exts, cover
def id_from_index(self, index): return self._data[index.row()]["id"] def id_from_index(self, index): return self._data[index.row()]["id"]
@ -296,18 +297,24 @@ class LibraryBooksModel(QAbstractTableModel):
def delete(self, indices): def delete(self, indices):
if len(indices): self.emit(SIGNAL("layoutAboutToBeChanged()")) if len(indices): self.emit(SIGNAL("layoutAboutToBeChanged()"))
for index in indices: items = [ self._data[index.row()] for index in indices ]
id = self.id_from_index(index) for item in items:
id = item["id"]
try:
self._data.remove(item)
except ValueError: continue
self.db.delete_by_id(id) self.db.delete_by_id(id)
row = index.row() for x in self._orig_data:
self._data[row:row+1] = [] if x["id"] == id: self._orig_data.remove(x)
for i in range(len(self._orig_data)):
if self._orig_data[i]["id"] == id:
self._orig_data[i:i+1] = []
i -=1
self.emit(SIGNAL("layoutChanged()")) self.emit(SIGNAL("layoutChanged()"))
self.emit(SIGNAL("deleted()"))
self.db.commit() self.db.commit()
def add_book(self, path):
""" Must call search and sort after this """
id = self.db.add_book(path)
self._orig_data.append(self.db.get_row_by_id(id, self.FIELDS))
class DeviceBooksModel(QAbstractTableModel): class DeviceBooksModel(QAbstractTableModel):
def __init__(self, parent): def __init__(self, parent):
QAbstractTableModel.__init__(self, parent) QAbstractTableModel.__init__(self, parent)
@ -404,8 +411,8 @@ class DeviceBooksModel(QAbstractTableModel):
self.emit(SIGNAL("layoutChanged()")) self.emit(SIGNAL("layoutChanged()"))
self.emit(SIGNAL("deleted()")) self.emit(SIGNAL("deleted()"))
def path(self, index): return self._data[index.row()]["path"] def path(self, index): return self._data[index.row()].path
def title(self, index): return self._data[index.row()]["title"] def title(self, index): return self._data[index.row()].title
@ -440,11 +447,6 @@ class MainWindow(QObject, Ui_MainWindow):
self.device_view.setModel(self.card_model) self.device_view.setModel(self.card_model)
QObject.connect(self.device_view.selectionModel(), SIGNAL("currentChanged(QModelIndex, QModelIndex)"), self.show_book) QObject.connect(self.device_view.selectionModel(), SIGNAL("currentChanged(QModelIndex, QModelIndex)"), self.show_book)
self.show_device(True) self.show_device(True)
def set_device_data(self, data):
model.set_data(data)
self.device_view.resizeColumnsToContents()
def model_modified(self): def model_modified(self):
if self.library_view.isVisible(): view = self.library_view if self.library_view.isVisible(): view = self.library_view
@ -479,13 +481,18 @@ class MainWindow(QObject, Ui_MainWindow):
def delete(self, action): def delete(self, action):
if self.device_view.isVisible(): count = str(len(self.current_view.selectionModel().selectedRows()))
ret = QMessageBox.question(self.window, self.trUtf8("SONY Reader - confirm"), self.trUtf8("Are you sure you want to <b>permanently delete</b> these ") +count+self.trUtf8(" items?"), QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)
if ret != QMessageBox.Yes: return
self.window.setCursor(Qt.WaitCursor)
if self.library_view.isVisible():
self.library_model.delete(self.library_view.selectionModel().selectedRows())
else:
rows = self.device_view.selectionModel().selectedRows() rows = self.device_view.selectionModel().selectedRows()
items = [ row.model().title(row) + ": " + row.model().path(row)[row.model().path(row).rfind("/")+1:] for row in rows ] items = [ row.model().title(row) + ": " + row.model().path(row)[row.model().path(row).rfind("/")+1:] for row in rows ]
ret = QMessageBox.question(self.window, self.trUtf8("SONY Reader - confirm"), self.trUtf8("Are you sure you want to delete these items from the device?\n\n") + "\n".join(items),
QMessageBox.YesToAll | QMessageBox.No, QMessageBox.YesToAll)
if ret == QMessageBox.YesToAll: if ret == QMessageBox.YesToAll:
self.window.setCursor(Qt.WaitCursor)
paths, mc, cc = [], False, False paths, mc, cc = [], False, False
for book in rows: for book in rows:
path = book.model().path(book) path = book.model().path(book)
@ -516,10 +523,10 @@ class MainWindow(QObject, Ui_MainWindow):
if cc: if cc:
self.dev.del_file(self.card+self.dev.CACHE_XML) self.dev.del_file(self.card+self.dev.CACHE_XML)
self.dev.put_file(self.cache_xml, self.card+self.dev.CACHE_XML) self.dev.put_file(self.cache_xml, self.card+self.dev.CACHE_XML)
self.window.setCursor(Qt.ArrowCursor)
else:
self.library_model.delete(self.library_view.selectionModel().selectedRows()) self.window.setCursor(Qt.ArrowCursor)
def read_settings(self): def read_settings(self):
settings = QSettings() settings = QSettings()
settings.beginGroup("MainWindow") settings.beginGroup("MainWindow")
@ -547,20 +554,13 @@ class MainWindow(QObject, Ui_MainWindow):
files = str(files.join("|||")).split("|||") files = str(files.join("|||")).split("|||")
for file in files: for file in files:
file = os.path.abspath(file) file = os.path.abspath(file)
title, author, cover, publisher = None, None, None, None self.library_view.model().add_book(file)
if ext == "lrf": self.search.clear()
try: hv = self.library_view.horizontalHeader()
lrf = LRFMetaFile(open(file, "r+b")) col = hv.sortIndicatorSection()
title = lrf.title order = hv.sortIndicatorOrder()
author = lrf.author self.library_view.model().sort(col, order)
publisher = lrf.publisher
cover = lrf.thumbnail
if "unknown" in author.lower(): author = None
except IOError, e:
self.show_error(e, "Unable to access <b>"+file+"</b>")
return
except LRFException: pass
self.library_model.add(file, title, author, publisher, cover)
def edit(self, action): def edit(self, action):
if self.library_view.isVisible(): if self.library_view.isVisible():
@ -694,11 +694,6 @@ class MainWindow(QObject, Ui_MainWindow):
self.book_info.hide() self.book_info.hide()
self.device_view.hide() self.device_view.hide()
self.library_view.show() self.library_view.show()
def timeout_error(self):
""" @todo: display error dialog """
pass
def progress(self, val): def progress(self, val):
if val < 0: if val < 0:
@ -723,8 +718,8 @@ class MainWindow(QObject, Ui_MainWindow):
return return
except ProtocolError, e: except ProtocolError, e:
traceback.print_exc(e) traceback.print_exc(e)
print >> sys.stderr, "Unable to connect device. Please try unplugiing and reconnecting it" print >> sys.stderr, "Unable to connect to device. Please try unplugging and reconnecting it"
qFatal("Unable to connect device. Please try unplugiing and reconnecting it") qFatal("Unable to connect to device. Please try unplugging and reconnecting it")
sc = space[1][1] if space[1][1] else space[2][1] sc = space[1][1] if space[1][1] else space[2][1]
self.df.setText("SONY Reader: " + human_readable(space[0][1]) + "<br><br>Storage card: " + human_readable(sc)) self.df.setText("SONY Reader: " + human_readable(space[0][1]) + "<br><br>Storage card: " + human_readable(sc))
@ -755,17 +750,8 @@ def main():
global DEFAULT_BOOK_COVER global DEFAULT_BOOK_COVER
DEFAULT_BOOK_COVER = QPixmap(":/default_cover") DEFAULT_BOOK_COVER = QPixmap(":/default_cover")
window = QMainWindow() window = QMainWindow()
def handle_exceptions(t, val, tb):
sys.__excepthook__(t, val, tb)
try:
qCritical("There was an unexpected error: \n"+"\n".join(traceback.format_exception(t, val, tb)))
except: pass
sys.excepthook = handle_exceptions
QCoreApplication.setOrganizationName("KovidsBrain") QCoreApplication.setOrganizationName("KovidsBrain")
QCoreApplication.setApplicationName("SONY Reader") QCoreApplication.setApplicationName("SONY Reader")
handler = QErrorMessage.qtHandler()
handler.resize(600, 400)
handler.setModal(True)
gui = MainWindow(window, options.log_packets) gui = MainWindow(window, options.log_packets)
ret = app.exec_() ret = app.exec_()
return ret return ret