mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Viewer: Refactor the bookmarks manager to make it a little prettier
This commit is contained in:
parent
9db39821b4
commit
be4dbafc9d
@ -5,8 +5,8 @@ __copyright__ = '2009, John Schember <john@nachtimwald.com>'
|
||||
|
||||
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_()
|
||||
|
||||
|
||||
|
@ -1,7 +1,8 @@
|
||||
<ui version="4.0" >
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<ui version="4.0">
|
||||
<class>BookmarkManager</class>
|
||||
<widget class="QDialog" name="BookmarkManager" >
|
||||
<property name="geometry" >
|
||||
<widget class="QDialog" name="BookmarkManager">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
@ -9,80 +10,93 @@
|
||||
<height>363</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="windowTitle" >
|
||||
<property name="windowTitle">
|
||||
<string>Bookmark Manager</string>
|
||||
</property>
|
||||
<layout class="QGridLayout" name="gridLayout" >
|
||||
<item row="0" column="0" >
|
||||
<widget class="QGroupBox" name="groupBox" >
|
||||
<property name="title" >
|
||||
<string>Actions</string>
|
||||
</property>
|
||||
<layout class="QVBoxLayout" name="verticalLayout" >
|
||||
<item>
|
||||
<widget class="QPushButton" name="button_edit" >
|
||||
<property name="text" >
|
||||
<string>Edit</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QPushButton" name="button_delete" >
|
||||
<property name="text" >
|
||||
<string>Delete</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QPushButton" name="button_revert" >
|
||||
<property name="text" >
|
||||
<string>Reset</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QPushButton" name="button_export" >
|
||||
<property name="text" >
|
||||
<string>Export</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QPushButton" name="button_import" >
|
||||
<property name="text" >
|
||||
<string>Import</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="0" column="1" >
|
||||
<widget class="QTableView" name="bookmarks_table" >
|
||||
<property name="showDropIndicator" stdset="0" >
|
||||
<bool>false</bool>
|
||||
</property>
|
||||
<property name="alternatingRowColors" >
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
<property name="selectionMode" >
|
||||
<enum>QAbstractItemView::SingleSelection</enum>
|
||||
</property>
|
||||
<property name="sortingEnabled" >
|
||||
<bool>false</bool>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="1" column="0" colspan="2" >
|
||||
<widget class="QDialogButtonBox" name="buttonBox" >
|
||||
<property name="standardButtons" >
|
||||
<layout class="QGridLayout" name="gridLayout">
|
||||
<item row="1" column="0" colspan="3">
|
||||
<widget class="QDialogButtonBox" name="buttonBox">
|
||||
<property name="standardButtons">
|
||||
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="0" column="0">
|
||||
<widget class="QGroupBox" name="groupBox">
|
||||
<property name="title">
|
||||
<string>Actions</string>
|
||||
</property>
|
||||
<layout class="QVBoxLayout" name="verticalLayout">
|
||||
<item>
|
||||
<widget class="QPushButton" name="button_edit">
|
||||
<property name="text">
|
||||
<string>Edit</string>
|
||||
</property>
|
||||
<property name="icon">
|
||||
<iconset resource="../../../../resources/images.qrc">
|
||||
<normaloff>:/images/edit_input.png</normaloff>:/images/edit_input.png</iconset>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QPushButton" name="button_delete">
|
||||
<property name="text">
|
||||
<string>Delete</string>
|
||||
</property>
|
||||
<property name="icon">
|
||||
<iconset resource="../../../../resources/images.qrc">
|
||||
<normaloff>:/images/trash.png</normaloff>:/images/trash.png</iconset>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QPushButton" name="button_revert">
|
||||
<property name="text">
|
||||
<string>Reset</string>
|
||||
</property>
|
||||
<property name="icon">
|
||||
<iconset resource="../../../../resources/images.qrc">
|
||||
<normaloff>:/images/edit-undo.png</normaloff>:/images/edit-undo.png</iconset>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QPushButton" name="button_export">
|
||||
<property name="text">
|
||||
<string>Export</string>
|
||||
</property>
|
||||
<property name="icon">
|
||||
<iconset resource="../../../../resources/images.qrc">
|
||||
<normaloff>:/images/back.png</normaloff>:/images/back.png</iconset>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QPushButton" name="button_import">
|
||||
<property name="text">
|
||||
<string>Import</string>
|
||||
</property>
|
||||
<property name="icon">
|
||||
<iconset resource="../../../../resources/images.qrc">
|
||||
<normaloff>:/images/forward.png</normaloff>:/images/forward.png</iconset>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<resources/>
|
||||
</item>
|
||||
<item row="0" column="1">
|
||||
<widget class="QListView" name="bookmarks_list">
|
||||
<property name="alternatingRowColors">
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<resources>
|
||||
<include location="../../../../resources/images.qrc"/>
|
||||
</resources>
|
||||
<connections>
|
||||
<connection>
|
||||
<sender>buttonBox</sender>
|
||||
@ -90,11 +104,11 @@
|
||||
<receiver>BookmarkManager</receiver>
|
||||
<slot>accept()</slot>
|
||||
<hints>
|
||||
<hint type="sourcelabel" >
|
||||
<hint type="sourcelabel">
|
||||
<x>225</x>
|
||||
<y>337</y>
|
||||
</hint>
|
||||
<hint type="destinationlabel" >
|
||||
<hint type="destinationlabel">
|
||||
<x>225</x>
|
||||
<y>181</y>
|
||||
</hint>
|
||||
@ -106,11 +120,11 @@
|
||||
<receiver>BookmarkManager</receiver>
|
||||
<slot>reject()</slot>
|
||||
<hints>
|
||||
<hint type="sourcelabel" >
|
||||
<hint type="sourcelabel">
|
||||
<x>225</x>
|
||||
<y>337</y>
|
||||
</hint>
|
||||
<hint type="destinationlabel" >
|
||||
<hint type="destinationlabel">
|
||||
<x>225</x>
|
||||
<y>181</y>
|
||||
</hint>
|
||||
|
Loading…
x
Reference in New Issue
Block a user