diff --git a/src/calibre/gui2/viewer/bookmarkmanager.py b/src/calibre/gui2/viewer/bookmarkmanager.py index c3686bd81e..073087f1e3 100644 --- a/src/calibre/gui2/viewer/bookmarkmanager.py +++ b/src/calibre/gui2/viewer/bookmarkmanager.py @@ -5,8 +5,8 @@ __copyright__ = '2009, John Schember ' import cPickle, os -from PyQt4.Qt import Qt, QDialog, QAbstractTableModel, QVariant, SIGNAL, \ - QModelIndex, QInputDialog, QLineEdit, QFileDialog +from PyQt4.Qt import (Qt, QDialog, QAbstractListModel, QVariant, + QModelIndex, QInputDialog, QLineEdit, QFileDialog, QItemSelectionModel) from calibre.gui2.viewer.bookmarkmanager_ui import Ui_BookmarkManager from calibre.gui2 import NONE @@ -20,28 +20,32 @@ class BookmarkManager(QDialog, Ui_BookmarkManager): self.bookmarks = bookmarks[:] self.set_bookmarks() - self.connect(self.button_revert, SIGNAL('clicked()'), self.set_bookmarks) - self.connect(self.button_delete, SIGNAL('clicked()'), self.delete_bookmark) - self.connect(self.button_edit, SIGNAL('clicked()'), self.edit_bookmark) - self.connect(self.button_export, SIGNAL('clicked()'), self.export_bookmarks) - self.connect(self.button_import, SIGNAL('clicked()'), self.import_bookmarks) + self.button_revert.clicked.connect(lambda :self.set_bookmarks()) + self.button_delete.clicked.connect(self.delete_bookmark) + self.button_edit.clicked.connect(self.edit_bookmark) + self.button_export.clicked.connect(self.export_bookmarks) + self.button_import.clicked.connect(self.import_bookmarks) + self.bookmarks_list.setStyleSheet('QListView::item { padding: 0.5ex }') + self.resize(600, 500) def set_bookmarks(self, bookmarks=None): - if bookmarks == None: + if bookmarks is None: bookmarks = self.bookmarks[:] - self._model = BookmarkTableModel(self, bookmarks) - self.bookmarks_table.setModel(self._model) - self.bookmarks_table.resizeColumnsToContents() + self._model = BookmarkListModel(self, bookmarks) + self.bookmarks_list.setModel(self._model) + if self._model.rowCount(QModelIndex()) > 0: + self.bookmarks_list.selectionModel().select(self._model.index(0), QItemSelectionModel.SelectCurrent) def delete_bookmark(self): - indexes = self.bookmarks_table.selectionModel().selectedIndexes() - if indexes != []: + indexes = list(self.bookmarks_list.selectionModel().selectedIndexes()) + if indexes: self._model.remove_row(indexes[0].row()) def edit_bookmark(self): - indexes = self.bookmarks_table.selectionModel().selectedIndexes() - if indexes != []: - title, ok = QInputDialog.getText(self, _('Edit bookmark'), _('New title for bookmark:'), QLineEdit.Normal, self._model.data(indexes[0], Qt.DisplayRole).toString()) + indexes = list(self.bookmarks_list.selectionModel().selectedIndexes()) + if indexes: + title, ok = QInputDialog.getText(self, _('Edit bookmark'), _( + 'New title for bookmark:'), QLineEdit.Normal, self._model.data(indexes[0], Qt.DisplayRole).toString()) title = QVariant(unicode(title).strip()) if ok and title: self._model.setData(indexes[0], title, Qt.EditRole) @@ -68,7 +72,7 @@ class BookmarkManager(QDialog, Ui_BookmarkManager): with open(filename, 'r') as fileobj: imported = cPickle.load(fileobj) - if imported != None: + if imported is not None: bad = False try: for bm in imported: @@ -86,11 +90,10 @@ class BookmarkManager(QDialog, Ui_BookmarkManager): self.set_bookmarks(bookmarks) -class BookmarkTableModel(QAbstractTableModel): - headers = [_("Name")] +class BookmarkListModel(QAbstractListModel): def __init__(self, parent, bookmarks): - QAbstractTableModel.__init__(self, parent) + QAbstractListModel.__init__(self, parent) self.bookmarks = bookmarks[:] @@ -99,11 +102,6 @@ class BookmarkTableModel(QAbstractTableModel): return 0 return len(self.bookmarks) - def columnCount(self, parent): - if parent and parent.isValid(): - return 0 - return len(self.headers) - def data(self, index, role): if role in (Qt.DisplayRole, Qt.EditRole): ans = self.bookmarks[index.row()]['title'] @@ -114,12 +112,12 @@ class BookmarkTableModel(QAbstractTableModel): if role == Qt.EditRole: bm = self.bookmarks[index.row()] bm['title'] = unicode(value.toString()).strip() - self.emit(SIGNAL("dataChanged(QModelIndex, QModelIndex)"), index, index) + self.dataChanged.emit(index, index) return True return False def flags(self, index): - flags = QAbstractTableModel.flags(self, index) + flags = QAbstractListModel.flags(self, index) flags |= Qt.ItemIsEditable return flags @@ -136,3 +134,10 @@ class BookmarkTableModel(QAbstractTableModel): del self.bookmarks[row] self.endRemoveRows() +if __name__ == '__main__': + from PyQt4.Qt import QApplication + app = QApplication([]) + d = BookmarkManager(None, [{'title':'Bookmark #%d' % i} for i in range(1, 50)]) + d.exec_() + + diff --git a/src/calibre/gui2/viewer/bookmarkmanager.ui b/src/calibre/gui2/viewer/bookmarkmanager.ui index 110b5db841..44c746038c 100644 --- a/src/calibre/gui2/viewer/bookmarkmanager.ui +++ b/src/calibre/gui2/viewer/bookmarkmanager.ui @@ -1,7 +1,8 @@ - + + BookmarkManager - - + + 0 0 @@ -9,80 +10,93 @@ 363 - + Bookmark Manager - - - - + + + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok + + + + + + Actions - + - - + + Edit + + + :/images/edit_input.png:/images/edit_input.png + - - + + Delete + + + :/images/trash.png:/images/trash.png + - - + + Reset - - - - - - Export + + + :/images/edit-undo.png:/images/edit-undo.png - - + + + Export + + + + :/images/back.png:/images/back.png + + + + + + Import + + + :/images/forward.png:/images/forward.png + - - - - false - - + + + true - - QAbstractItemView::SingleSelection - - - false - - - - - - - QDialogButtonBox::Cancel|QDialogButtonBox::Ok - - + + + buttonBox @@ -90,11 +104,11 @@ BookmarkManager accept() - + 225 337 - + 225 181 @@ -106,11 +120,11 @@ BookmarkManager reject() - + 225 337 - + 225 181