E-book viewer: Allow quick saving and loading of viewer settings as 'themes'. Fixes #1024611 ([Enhancements] Ebook viewer : Display profiles)

This commit is contained in:
Kovid Goyal 2012-08-02 21:26:12 +05:30
parent d681396a15
commit 408e205ec7
2 changed files with 187 additions and 16 deletions

View File

@ -10,10 +10,11 @@ __docformat__ = 'restructuredtext en'
import zipfile
from functools import partial
from PyQt4.Qt import QFont, QVariant, QDialog, Qt, QColor, QColorDialog
from PyQt4.Qt import (QFont, QVariant, QDialog, Qt, QColor, QColorDialog,
QMenu, QInputDialog)
from calibre.constants import iswindows, isxp
from calibre.utils.config import Config, StringConfig
from calibre.utils.config import Config, StringConfig, JSONConfig
from calibre.gui2.shortcuts import ShortcutConfig
from calibre.gui2.viewer.config_ui import Ui_Dialog
from calibre.utils.localization import get_language
@ -112,8 +113,55 @@ class ConfigDialog(QDialog, Ui_Dialog):
self.hyphenate_default_lang.setVisible(False)
self.hyphenate_label.setVisible(False)
self.themes = JSONConfig('viewer_themes')
self.save_theme_button.clicked.connect(self.save_theme)
self.load_theme_button.m = m = QMenu()
self.load_theme_button.setMenu(m)
m.triggered.connect(self.load_theme)
self.delete_theme_button.m = m = QMenu()
self.delete_theme_button.setMenu(m)
m.triggered.connect(self.delete_theme)
opts = config().parse()
self.load_options(opts)
self.init_load_themes()
def save_theme(self):
themename, ok = QInputDialog.getText(self, _('Theme name'),
_('Choose a name for this theme'))
if not ok: return
themename = unicode(themename).strip()
if not themename: return
c = config('')
c.add_opt('theme_name_xxx', default=themename)
self.save_options(c)
self.themes['theme_'+themename] = c.src
self.init_load_themes()
self.theming_message.setText(_('Saved settings as the theme named: %s')%
themename)
def init_load_themes(self):
for x in ('load', 'delete'):
m = getattr(self, '%s_theme_button'%x).menu()
m.clear()
for x in self.themes.iterkeys():
title = x[len('theme_'):]
ac = m.addAction(title)
ac.theme_id = x
def load_theme(self, ac):
theme = ac.theme_id
raw = self.themes[theme]
self.load_options(config(raw).parse())
self.theming_message.setText(_('Loaded settings from the theme %s')%
theme[len('theme_'):])
def delete_theme(self, ac):
theme = ac.theme_id
del self.themes[theme]
self.init_load_themes()
self.theming_message.setText(_('Deleted the theme named: %s')%
theme[len('theme_'):])
def restore_defaults(self):
opts = config('').parse()
@ -193,11 +241,10 @@ class ConfigDialog(QDialog, Ui_Dialog):
' first complete that, by clicking outside the '
' shortcut editing box.'), show=True)
return
self.save_options()
self.save_options(config())
return QDialog.accept(self, *args)
def save_options(self):
c = config()
def save_options(self, c):
c.set('serif_family', unicode(self.serif_family.currentFont().family()))
c.set('sans_family', unicode(self.sans_family.currentFont().family()))
c.set('mono_family', unicode(self.mono_family.currentFont().family()))
@ -230,4 +277,3 @@ class ConfigDialog(QDialog, Ui_Dialog):
c.set(x+'_margin', int(getattr(self, 'opt_%s_margin'%x).value()))

View File

@ -18,6 +18,16 @@
<normaloff>:/images/config.png</normaloff>:/images/config.png</iconset>
</property>
<layout class="QGridLayout" name="gridLayout_4">
<item row="1" column="0">
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok|QDialogButtonBox::RestoreDefaults</set>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QTabWidget" name="tabs">
<property name="currentIndex">
@ -627,16 +637,131 @@ QToolBox::tab:hover {
</item>
</layout>
</widget>
</widget>
</item>
<item row="1" column="0">
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok|QDialogButtonBox::RestoreDefaults</set>
</property>
<widget class="QWidget" name="tab_4">
<attribute name="title">
<string>&amp;Theming</string>
</attribute>
<layout class="QFormLayout" name="formLayout_7">
<property name="fieldGrowthPolicy">
<enum>QFormLayout::ExpandingFieldsGrow</enum>
</property>
<item row="0" column="0" colspan="2">
<widget class="QLabel" name="label_23">
<property name="text">
<string>You can save and load the viewer settings as &lt;i&gt;themes&lt;/i&gt;</string>
</property>
</widget>
</item>
<item row="1" column="0">
<spacer name="verticalSpacer_2">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label_15">
<property name="text">
<string>Save current settings as a theme:</string>
</property>
<property name="buddy">
<cstring>save_theme_button</cstring>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QToolButton" name="save_theme_button">
<property name="text">
<string>&amp;Save</string>
</property>
<property name="icon">
<iconset resource="../../../../resources/images.qrc">
<normaloff>:/images/save.png</normaloff>:/images/save.png</iconset>
</property>
<property name="toolButtonStyle">
<enum>Qt::ToolButtonTextBesideIcon</enum>
</property>
</widget>
</item>
<item row="4" column="0">
<widget class="QLabel" name="label_20">
<property name="text">
<string>Load a previously saved theme:</string>
</property>
<property name="buddy">
<cstring>load_theme_button</cstring>
</property>
</widget>
</item>
<item row="4" column="1">
<widget class="QToolButton" name="load_theme_button">
<property name="text">
<string>&amp;Load</string>
</property>
<property name="icon">
<iconset resource="../../../../resources/images.qrc">
<normaloff>:/images/document_open.png</normaloff>:/images/document_open.png</iconset>
</property>
<property name="popupMode">
<enum>QToolButton::InstantPopup</enum>
</property>
<property name="toolButtonStyle">
<enum>Qt::ToolButtonTextBesideIcon</enum>
</property>
</widget>
</item>
<item row="5" column="0">
<widget class="QLabel" name="label_21">
<property name="text">
<string>Delete a saved theme:</string>
</property>
</widget>
</item>
<item row="5" column="1">
<widget class="QToolButton" name="delete_theme_button">
<property name="text">
<string>&amp;Delete</string>
</property>
<property name="icon">
<iconset resource="../../../../resources/images.qrc">
<normaloff>:/images/trash.png</normaloff>:/images/trash.png</iconset>
</property>
<property name="popupMode">
<enum>QToolButton::InstantPopup</enum>
</property>
<property name="toolButtonStyle">
<enum>Qt::ToolButtonTextBesideIcon</enum>
</property>
</widget>
</item>
<item row="6" column="1">
<spacer name="verticalSpacer_3">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item row="7" column="0" colspan="2">
<widget class="QLabel" name="theming_message">
<property name="text">
<string/>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
</item>
</layout>