diff --git a/src/calibre/gui2/tweak_book/__init__.py b/src/calibre/gui2/tweak_book/__init__.py index dc2a7d2c81..886bfb6935 100644 --- a/src/calibre/gui2/tweak_book/__init__.py +++ b/src/calibre/gui2/tweak_book/__init__.py @@ -63,6 +63,7 @@ d['editor_format_toolbar'] = [('format-text-' + x) if x else x for x in ( 'justify-right', 'justify-fill')] d['spell_check_case_sensitive_search'] = False d['add_cover_preserve_aspect_ratio'] = False +d['templates'] = {} del d ucase_map = {l:string.ascii_uppercase[i] for i, l in enumerate(string.ascii_lowercase)} diff --git a/src/calibre/gui2/tweak_book/preferences.py b/src/calibre/gui2/tweak_book/preferences.py index 782b662061..f5c3f9fdfd 100644 --- a/src/calibre/gui2/tweak_book/preferences.py +++ b/src/calibre/gui2/tweak_book/preferences.py @@ -18,13 +18,14 @@ from PyQt4.Qt import ( QListWidgetItem, QIcon, QWidget, QSize, QFormLayout, Qt, QSpinBox, QCheckBox, pyqtSignal, QDoubleSpinBox, QComboBox, QLabel, QFont, QFontComboBox, QPushButton, QSizePolicy, QHBoxLayout, QGroupBox, - QToolButton, QVBoxLayout, QSpacerItem) + QToolButton, QVBoxLayout, QSpacerItem, QTimer) from calibre.gui2.keyboard import ShortcutConfig from calibre.gui2.tweak_book import tprefs, toolbar_actions, editor_toolbar_actions, actions from calibre.gui2.tweak_book.editor.themes import default_theme, all_theme_names, ThemeEditor from calibre.gui2.tweak_book.spell import ManageDictionaries from calibre.gui2.font_family_chooser import FontFamilyChooser +from calibre.gui2.tweak_book.widgets import Dialog class BasicSettings(QWidget): # {{{ @@ -200,6 +201,10 @@ class EditorSettings(BasicSettings): ' time you open a HTML/CSS/etc. file for editing.')) l.addRow(lw) + self.tb = b = QPushButton(_('Change &templates')) + l.addRow(_('Change the templates for creating new files:'), b) + b.clicked.connect(lambda : TemplatesDialog(self).exec_()) + lw = self('inline_spell_check') lw.setText(_('Show misspelled words underlined in the code view')) lw.setToolTip('
' + _(
@@ -513,6 +518,77 @@ class ToolbarSettings(QWidget):
# }}}
+class TemplatesDialog(Dialog): # {{{
+
+ def __init__(self, parent=None):
+ self.ignore_changes = False
+ Dialog.__init__(self, _('Customize templates'), 'customize-templates', parent=parent)
+
+ def setup_ui(self):
+ from calibre.gui2.tweak_book.templates import DEFAULT_TEMPLATES
+ from calibre.gui2.tweak_book.editor.text import TextEdit
+ self.l = l = QFormLayout(self)
+ self.setLayout(l)
+
+ self.syntaxes = s = QComboBox(self)
+ s.addItems(sorted(DEFAULT_TEMPLATES.iterkeys()))
+ s.setCurrentIndex(s.findText('html'))
+ l.addRow(_('Choose the &type of template to edit:'), s)
+ s.currentIndexChanged.connect(self.show_template)
+
+ self.helpl = la = QLabel(_(
+ 'The variables {0} and {1} will be replaced with the title and author of the book. {2}'
+ ' is where the cursor will be positioned.').format('{TITLE}', '{AUTHOR}', '%CURSOR%'))
+ la.setWordWrap(True)
+ l.addRow(la)
+
+ self.save_timer = t = QTimer(self)
+ t.setSingleShot(True), t.setInterval(100)
+ t.timeout.connect(self._save_syntax)
+
+ self.editor = e = TextEdit(self)
+ l.addRow(e)
+ e.textChanged.connect(self.save_syntax)
+
+ self.show_template()
+
+ self.bb.clear()
+ self.bb.addButton(self.bb.Close)
+ self.rd = b = self.bb.addButton(self.bb.RestoreDefaults)
+ b.clicked.connect(self.restore_defaults)
+ l.addRow(self.bb)
+
+ @property
+ def current_syntax(self):
+ return unicode(self.syntaxes.currentText())
+
+ def show_template(self):
+ from calibre.gui2.tweak_book.templates import raw_template_for
+ syntax = self.current_syntax
+ self.ignore_changes = True
+ try:
+ self.editor.load_text(raw_template_for(syntax), syntax=syntax)
+ finally:
+ self.ignore_changes = False
+
+ def save_syntax(self):
+ if self.ignore_changes:
+ return
+ self.save_timer.start()
+
+ def _save_syntax(self):
+ custom = tprefs['templates']
+ custom[self.current_syntax] = unicode(self.editor.toPlainText())
+ tprefs['templates'] = custom
+
+ def restore_defaults(self):
+ custom = tprefs['templates']
+ custom.pop(self.current_syntax, None)
+ tprefs['templates'] = custom
+ self.show_template()
+ self._save_syntax()
+# }}}
+
class Preferences(QDialog):
def __init__(self, gui, initial_panel=None):
diff --git a/src/calibre/gui2/tweak_book/templates.py b/src/calibre/gui2/tweak_book/templates.py
index 5024819bb5..69bd18fdb9 100644
--- a/src/calibre/gui2/tweak_book/templates.py
+++ b/src/calibre/gui2/tweak_book/templates.py
@@ -7,7 +7,7 @@ __license__ = 'GPL v3'
__copyright__ = '2013, Kovid Goyal