Implement preferences for Book Details

This commit is contained in:
Kovid Goyal 2011-04-24 20:28:59 -06:00
parent 1acc3716b6
commit f924c45de5
4 changed files with 129 additions and 15 deletions

View File

@ -67,8 +67,9 @@ def render_html(mi, css, vertical, widget, all_fields=False): # {{{
% (table, right_pane)) % (table, right_pane))
return ans return ans
def get_field_list(fm): def get_field_list(fm, use_defaults=False):
fieldlist = list(gprefs['book_display_fields']) src = gprefs.defaults if use_defaults else gprefs
fieldlist = list(src['book_display_fields'])
names = frozenset([x[0] for x in fieldlist]) names = frozenset([x[0] for x in fieldlist])
for field in fm.displayable_field_keys(): for field in fm.displayable_field_keys():
if field not in names: if field not in names:

View File

@ -650,6 +650,11 @@ class BooksView(QTableView): # {{{
def column_map(self): def column_map(self):
return self._model.column_map return self._model.column_map
def refresh_book_details(self):
idx = self.currentIndex()
if idx.isValid():
self._model.current_changed(idx, idx)
def scrollContentsBy(self, dx, dy): def scrollContentsBy(self, dx, dy):
# Needed as Qt bug causes headerview to not always update when scrolling # Needed as Qt bug causes headerview to not always update when scrolling
QTableView.scrollContentsBy(self, dx, dy) QTableView.scrollContentsBy(self, dx, dy)

View File

@ -6,7 +6,7 @@ __copyright__ = '2010, Kovid Goyal <kovid@kovidgoyal.net>'
__docformat__ = 'restructuredtext en' __docformat__ = 'restructuredtext en'
from PyQt4.Qt import (QApplication, QFont, QFontInfo, QFontDialog, from PyQt4.Qt import (QApplication, QFont, QFontInfo, QFontDialog,
QAbstractListModel) QAbstractListModel, Qt)
from calibre.gui2.preferences import ConfigWidgetBase, test_widget, CommaSeparatedList from calibre.gui2.preferences import ConfigWidgetBase, test_widget, CommaSeparatedList
from calibre.gui2.preferences.look_feel_ui import Ui_Form from calibre.gui2.preferences.look_feel_ui import Ui_Form
@ -15,12 +15,82 @@ from calibre.utils.localization import (available_translations,
get_language, get_lang) get_language, get_lang)
from calibre.utils.config import prefs from calibre.utils.config import prefs
from calibre.utils.icu import sort_key from calibre.utils.icu import sort_key
from calibre.gui2 import NONE
from calibre.gui2.book_details import get_field_list
class DisplayedFields(QAbstractListModel): class DisplayedFields(QAbstractListModel): # {{{
def __init__(self, parent=None): def __init__(self, db, parent=None):
QAbstractListModel.__init__(self, parent) QAbstractListModel.__init__(self, parent)
self.fields = []
self.db = db
self.changed = False
def initialize(self, use_defaults=False):
self.fields = [[x[0], x[1]] for x in
get_field_list(self.db.field_metadata,
use_defaults=use_defaults)]
self.reset()
self.changed = True
def rowCount(self, *args):
return len(self.fields)
def data(self, index, role):
try:
field, visible = self.fields[index.row()]
except:
return NONE
if role == Qt.DisplayRole:
name = field
try:
name = self.db.field_metadata[field]['name']
except:
pass
if not name:
name = field
return name
if role == Qt.CheckStateRole:
return Qt.Checked if visible else Qt.Unchecked
return NONE
def flags(self, index):
ans = QAbstractListModel.flags(self, index)
return ans | Qt.ItemIsUserCheckable
def setData(self, index, val, role):
ret = False
if role == Qt.CheckStateRole:
val, ok = val.toInt()
if ok:
self.fields[index.row()][1] = bool(val)
self.changed = True
ret = True
self.dataChanged.emit(index, index)
return ret
def restore_defaults(self):
self.initialize(use_defaults=True)
def commit(self):
if self.changed:
gprefs['book_display_fields'] = self.fields
def move(self, idx, delta):
row = idx.row() + delta
if row >= 0 and row < len(self.fields):
t = self.fields[row]
self.fields[row] = self.fields[row-delta]
self.fields[row-delta] = t
self.dataChanged.emit(idx, idx)
idx = self.index(row)
self.dataChanged.emit(idx, idx)
self.changed = True
return idx
# }}}
class ConfigWidget(ConfigWidgetBase, Ui_Form): class ConfigWidget(ConfigWidgetBase, Ui_Form):
def genesis(self, gui): def genesis(self, gui):
@ -82,11 +152,18 @@ class ConfigWidget(ConfigWidgetBase, Ui_Form):
self.current_font = self.initial_font = None self.current_font = self.initial_font = None
self.change_font_button.clicked.connect(self.change_font) self.change_font_button.clicked.connect(self.change_font)
self.display_model = DisplayedFields(self.gui.current_db,
self.field_display_order)
self.display_model.dataChanged.connect(self.changed_signal)
self.field_display_order.setModel(self.display_model)
self.df_up_button.clicked.connect(self.move_df_up)
self.df_down_button.clicked.connect(self.move_df_down)
def initialize(self): def initialize(self):
ConfigWidgetBase.initialize(self) ConfigWidgetBase.initialize(self)
self.current_font = self.initial_font = gprefs['font'] self.current_font = self.initial_font = gprefs['font']
self.update_font_display() self.update_font_display()
self.display_model.initialize()
def restore_defaults(self): def restore_defaults(self):
ConfigWidgetBase.restore_defaults(self) ConfigWidgetBase.restore_defaults(self)
@ -95,6 +172,8 @@ class ConfigWidget(ConfigWidgetBase, Ui_Form):
if ofont is not None: if ofont is not None:
self.changed_signal.emit() self.changed_signal.emit()
self.update_font_display() self.update_font_display()
self.display_model.restore_defaults()
self.changed_signal.emit()
def build_font_obj(self): def build_font_obj(self):
font_info = self.current_font font_info = self.current_font
@ -113,6 +192,24 @@ class ConfigWidget(ConfigWidgetBase, Ui_Form):
self.font_display.setText(name + self.font_display.setText(name +
' [%dpt]'%fi.pointSize()) ' [%dpt]'%fi.pointSize())
def move_df_up(self):
idx = self.field_display_order.currentIndex()
if idx.isValid():
idx = self.display_model.move(idx, -1)
if idx is not None:
sm = self.field_display_order.selectionModel()
sm.select(idx, sm.ClearAndSelect)
self.field_display_order.setCurrentIndex(idx)
def move_df_down(self):
idx = self.field_display_order.currentIndex()
if idx.isValid():
idx = self.display_model.move(idx, 1)
if idx is not None:
sm = self.field_display_order.selectionModel()
sm.select(idx, sm.ClearAndSelect)
self.field_display_order.setCurrentIndex(idx)
def change_font(self, *args): def change_font(self, *args):
fd = QFontDialog(self.build_font_obj(), self) fd = QFontDialog(self.build_font_obj(), self)
if fd.exec_() == fd.Accepted: if fd.exec_() == fd.Accepted:
@ -129,12 +226,13 @@ class ConfigWidget(ConfigWidgetBase, Ui_Form):
gprefs['font'] = self.current_font gprefs['font'] = self.current_font
QApplication.setFont(self.font_display.font()) QApplication.setFont(self.font_display.font())
rr = True rr = True
self.display_model.commit()
return rr return rr
def refresh_gui(self, gui): def refresh_gui(self, gui):
self.update_font_display() self.update_font_display()
gui.tags_view.reread_collapse_parameters() gui.tags_view.reread_collapse_parameters()
gui.library_view.refresh_book_details()
if __name__ == '__main__': if __name__ == '__main__':
app = QApplication([]) app = QApplication([])

View File

@ -21,8 +21,8 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>682</width> <width>699</width>
<height>254</height> <height>306</height>
</rect> </rect>
</property> </property>
<attribute name="label"> <attribute name="label">
@ -189,8 +189,8 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>699</width> <width>649</width>
<height>151</height> <height>96</height>
</rect> </rect>
</property> </property>
<attribute name="label"> <attribute name="label">
@ -308,8 +308,8 @@ then the tags will be displayed each on their own line.</string>
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>699</width> <width>429</width>
<height>306</height> <height>63</height>
</rect> </rect>
</property> </property>
<attribute name="label"> <attribute name="label">
@ -374,7 +374,7 @@ then the tags will be displayed each on their own line.</string>
</property> </property>
</widget> </widget>
</item> </item>
<item row="0" column="0"> <item row="0" column="0" rowspan="2">
<widget class="QGroupBox" name="groupBox"> <widget class="QGroupBox" name="groupBox">
<property name="title"> <property name="title">
<string>Select displayed metadata</string> <string>Select displayed metadata</string>
@ -388,7 +388,7 @@ then the tags will be displayed each on their own line.</string>
</widget> </widget>
</item> </item>
<item row="0" column="1"> <item row="0" column="1">
<widget class="QToolButton" name="toolButton"> <widget class="QToolButton" name="df_up_button">
<property name="toolTip"> <property name="toolTip">
<string>Move up</string> <string>Move up</string>
</property> </property>
@ -399,7 +399,7 @@ then the tags will be displayed each on their own line.</string>
</widget> </widget>
</item> </item>
<item row="2" column="1"> <item row="2" column="1">
<widget class="QToolButton" name="toolButton_2"> <widget class="QToolButton" name="df_down_button">
<property name="toolTip"> <property name="toolTip">
<string>Move down</string> <string>Move down</string>
</property> </property>
@ -425,6 +425,16 @@ then the tags will be displayed each on their own line.</string>
</layout> </layout>
</widget> </widget>
</item> </item>
<item row="1" column="1">
<widget class="QLabel" name="label_3">
<property name="text">
<string>Note that comments will always be displayed at the end, regardless of the position you assign here.</string>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
</widget>
</item>
</layout> </layout>
</widget> </widget>
</widget> </widget>