From 408e205ec7505452e99ed97b175132b62f79de8c Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 2 Aug 2012 21:26:12 +0530 Subject: [PATCH] E-book viewer: Allow quick saving and loading of viewer settings as 'themes'. Fixes #1024611 ([Enhancements] Ebook viewer : Display profiles) --- src/calibre/gui2/viewer/config.py | 58 ++++++++++-- src/calibre/gui2/viewer/config.ui | 145 +++++++++++++++++++++++++++--- 2 files changed, 187 insertions(+), 16 deletions(-) diff --git a/src/calibre/gui2/viewer/config.py b/src/calibre/gui2/viewer/config.py index a5b13637cd..8631d9e6a4 100644 --- a/src/calibre/gui2/viewer/config.py +++ b/src/calibre/gui2/viewer/config.py @@ -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())) - diff --git a/src/calibre/gui2/viewer/config.ui b/src/calibre/gui2/viewer/config.ui index 1583fdca37..a024a980d5 100644 --- a/src/calibre/gui2/viewer/config.ui +++ b/src/calibre/gui2/viewer/config.ui @@ -18,6 +18,16 @@ :/images/config.png:/images/config.png + + + + Qt::Horizontal + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok|QDialogButtonBox::RestoreDefaults + + + @@ -627,16 +637,131 @@ QToolBox::tab:hover { - - - - - - Qt::Horizontal - - - QDialogButtonBox::Cancel|QDialogButtonBox::Ok|QDialogButtonBox::RestoreDefaults - + + + &Theming + + + + QFormLayout::ExpandingFieldsGrow + + + + + You can save and load the viewer settings as <i>themes</i> + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + Save current settings as a theme: + + + save_theme_button + + + + + + + &Save + + + + :/images/save.png:/images/save.png + + + Qt::ToolButtonTextBesideIcon + + + + + + + Load a previously saved theme: + + + load_theme_button + + + + + + + &Load + + + + :/images/document_open.png:/images/document_open.png + + + QToolButton::InstantPopup + + + Qt::ToolButtonTextBesideIcon + + + + + + + Delete a saved theme: + + + + + + + &Delete + + + + :/images/trash.png:/images/trash.png + + + QToolButton::InstantPopup + + + Qt::ToolButtonTextBesideIcon + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + + + + + +