Add a preview field to the wizard.

Vary the button according to whether the wizard created the template
Don't show the template if wizard created
This commit is contained in:
Charles Haley 2011-05-29 21:21:02 +01:00
parent 19261f15ee
commit cc288ff4ca
2 changed files with 81 additions and 16 deletions

View File

@ -8,9 +8,10 @@ __docformat__ = 'restructuredtext en'
from functools import partial
from collections import defaultdict
from PyQt4.Qt import (QLineEdit, QDialog, QGridLayout, QLabel, QCheckBox,
QDialogButtonBox, QColor, QComboBox, QIcon)
from PyQt4.Qt import (QLineEdit, QDialog, QGridLayout, QLabel, QCheckBox, QIcon,
QDialogButtonBox, QColor, QComboBox, QPushButton)
from calibre.ebooks.metadata.book.base import composite_formatter
from calibre.gui2.dialogs.template_dialog import TemplateDialog
from calibre.gui2.complete import MultiCompleteLineEdit
from calibre.gui2 import error_dialog
@ -26,6 +27,7 @@ class TemplateLineEditor(QLineEdit):
QLineEdit.__init__(self, parent)
self.tags = None
self.mi = None
self.txt = None
def set_mi(self, mi):
self.mi = mi
@ -42,46 +44,82 @@ class TemplateLineEditor(QLineEdit):
menu.exec_(event.globalPos())
def open_editor(self):
t = TemplateDialog(self, self.text(), self.mi)
if self.txt:
t = TemplateDialog(self, self.txt, self.mi)
else:
t = TemplateDialog(self, self.text(), self.mi)
t.setWindowTitle(_('Edit template'))
if t.exec_():
self.setText(t.textbox.toPlainText())
self.txt = None
def show_wizard_button(self, txt):
if not txt or txt.startswith('program:\n#tag wizard'):
return True
return False
def setText(self, txt):
txt = unicode(txt)
if txt and txt.startswith('program:\n#tag wizard'):
self.txt = txt
self.setReadOnly(True)
QLineEdit.setText(self, '')
QLineEdit.setText(self, _('Template generated by the wizard'))
self.setStyleSheet('TemplateLineEditor { color: gray }')
else:
QLineEdit.setText(self, txt)
def tag_wizard(self):
txt = unicode(self.text())
if txt and not txt.startswith('program:\n#tag wizard'):
if txt and not self.txt:
error_dialog(self, _('Invalid text'),
_('The text in the box was not generated by this wizard'),
show=True, show_copy_button=False)
return
d = TagWizard(self, self.db, unicode(self.text()))
d = TagWizard(self, self.db, unicode(self.txt), self.mi)
if d.exec_():
self.setText(d.template)
def text(self):
if self.txt:
return self.txt
return QLineEdit.text(self)
class TagWizard(QDialog):
def __init__(self, parent, db, txt):
def __init__(self, parent, db, txt, mi):
QDialog.__init__(self, parent)
self.setWindowTitle(_('Coloring Wizard'))
self.setWindowIcon(QIcon(I('wizard.png')))
self.mi = mi
self.columns = []
self.completion_values = defaultdict(dict)
for k in db.all_field_keys():
m = db.metadata_for_field(k)
if m['datatype'] in ('text', 'enumeration', 'series'):
if m['datatype'] in ('text', 'enumeration', 'series') and \
m['is_category'] and k not in ('identifiers'):
self.columns.append(k)
if m['is_custom']:
# self.completion_values[k] = {}
self.completion_values[k]['v'] = db.all_custom(m['label'])
elif k == 'tags':
# self.completion_values[k] = {}
self.completion_values[k]['v'] = db.all_tags()
elif k == 'formats':
self.completion_values[k]['v'] = db.all_formats()
else:
f = getattr(db, 'all' + k, None)
if k in ('publisher'):
ck = k + 's'
else:
ck = k
f = getattr(db, 'all_' + ck, None)
if f:
self.completion_values[k] = {}
self.completion_values[k]['v'] = [v[1] for v in f()]
if k == 'authors':
self.completion_values[k]['v'] = [v[1].\
replace('|', ',') for v in f()]
else:
self.completion_values[k]['v'] = [v[1] for v in f()]
if k in self.completion_values:
self.completion_values[k]['m'] = m['is_multiple']
@ -188,12 +226,28 @@ class TagWizard(QDialog):
pass
i += 1
w = QLabel(_('Preview'))
l.addWidget(w, 99, 0, 1, 1)
w = self.test_box = QLineEdit(self)
w.setReadOnly(True)
l.addWidget(w, 99, 1, 1, 1)
w = QPushButton(_('Test'))
l.addWidget(w, 99, 3, 1, 1)
w.clicked.connect(self.preview)
bb = QDialogButtonBox(QDialogButtonBox.Ok|QDialogButtonBox.Cancel, parent=self)
l.addWidget(bb, 100, 3, 1, 2)
bb.accepted.connect(self.accepted)
bb.rejected.connect(self.reject)
self.template = ''
def preview(self):
if not self.generate_program():
return
t = composite_formatter.safe_format(self.template, self.mi,
_('EXCEPTION'), self.mi)
self.test_box.setText(t)
def column_changed(self, s, valbox=None):
k = unicode(s)
if k in self.completion_values:
@ -206,7 +260,7 @@ class TagWizard(QDialog):
valbox.update_items_cache([])
valbox.set_separator(None)
def accepted(self):
def generate_program(self):
res = ("program:\n#tag wizard -- do not directly edit\n"
" first_non_empty(\n")
lines = []
@ -270,4 +324,10 @@ class TagWizard(QDialog):
if f and t and c:
res += '#' + t + ':|:' + c + ':|:' + f +':|:' + nfc + ':|:' + re + '\n'
self.template += res
self.accept()
return True
def accepted(self):
if self.generate_program():
self.accept()
else:
self.template = ''

View File

@ -6,7 +6,7 @@ __copyright__ = '2010, Kovid Goyal <kovid@kovidgoyal.net>'
__docformat__ = 'restructuredtext en'
from PyQt4.Qt import (QApplication, QFont, QFontInfo, QFontDialog,
QAbstractListModel, Qt, QColor)
QAbstractListModel, Qt, QColor, QIcon)
from calibre.gui2.preferences import ConfigWidgetBase, test_widget, CommaSeparatedList
from calibre.gui2.preferences.look_feel_ui import Ui_Form
@ -215,11 +215,16 @@ class ConfigWidget(ConfigWidgetBase, Ui_Form):
for i in range(1, self.column_color_count):
r('column_color_name_'+str(i), db.prefs, choices=choices)
r('column_color_template_'+str(i), db.prefs)
txt = db.prefs.get('column_color_template_'+str(i), None)
tpl = getattr(self, 'opt_column_color_template_'+str(i))
tpl.set_db(db)
tpl.set_mi(mi)
toolbutton = getattr(self, 'opt_column_color_wizard_'+str(i))
toolbutton.clicked.connect(tpl.tag_wizard)
if tpl.show_wizard_button(txt):
toolbutton.clicked.connect(tpl.tag_wizard)
else:
toolbutton.clicked.connect(tpl.open_editor)
toolbutton.setIcon(QIcon(I('edit_input.png')))
all_colors = [unicode(s) for s in list(QColor.colorNames())]
self.colors_box.setText(', '.join(all_colors))