Merge from main branch

This commit is contained in:
Tom Scholl 2011-06-02 23:00:19 +00:00
commit fd0318621f
24 changed files with 543 additions and 995 deletions

View File

@ -3,71 +3,39 @@ __copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>'
''' '''
Profile to download CNN Profile to download CNN
''' '''
import re
from calibre.web.feeds.news import BasicNewsRecipe from calibre.web.feeds.news import BasicNewsRecipe
from calibre.ebooks.BeautifulSoup import BeautifulSoup
class CNN(BasicNewsRecipe): class CNN(BasicNewsRecipe):
title = 'CNN' title = 'CNN'
description = 'Global news' description = 'Global news'
timefmt = ' [%d %b %Y]' timefmt = ' [%d %b %Y]'
__author__ = 'Krittika Goyal and Sujata Raman' __author__ = 'Kovid Goyal'
language = 'en' language = 'en'
no_stylesheets = True no_stylesheets = True
use_embedded_content = False use_embedded_content = False
oldest_article = 15 oldest_article = 15
recursions = 1 #recursions = 1
match_regexps = [r'http://sportsillustrated.cnn.com/.*/[1-9].html'] #match_regexps = [r'http://sportsillustrated.cnn.com/.*/[1-9].html']
max_articles_per_feed = 25 max_articles_per_feed = 25
extra_css = ''' preprocess_regexps = [
.cnn_strycntntlft{font-family :Arial,Helvetica,sans-serif;} (re.compile(r'<!--\[if.*if\]-->', re.DOTALL), lambda m: ''),
h2{font-family :Arial,Helvetica,sans-serif; font-size:x-small} (re.compile(r'<script.*?</script>', re.DOTALL), lambda m: ''),
.cnnTxtCmpnt{font-family :Arial,Helvetica,sans-serif; font-size:x-small} (re.compile(r'<style.*?</style>', re.DOTALL), lambda m: ''),
.cnnTMcontent{font-family :Arial,Helvetica,sans-serif; font-size:x-small;color:#575757}
.storytext{font-family :Arial,Helvetica,sans-serif; font-size:small}
.storybyline{font-family :Arial,Helvetica,sans-serif; font-size:x-small; color:#575757}
.credit{font-family :Arial,Helvetica,sans-serif; font-size:xx-small; color:#575757}
.storyBrandingBanner{font-family :Arial,Helvetica,sans-serif; font-size:x-small; color:#575757}
.storytimestamp{font-family :Arial,Helvetica,sans-serif; font-size:x-small; color:#575757}
.timestamp{font-family :Arial,Helvetica,sans-serif; font-size:x-small; color:#575757}
.cnn_strytmstmp{font-family :Arial,Helvetica,sans-serif; font-size:x-small; color:#666666;}
.cnn_stryimg640caption{font-family :Arial,Helvetica,sans-serif; font-size:x-small; color:#666666;}
.cnn_strylccimg300cntr{font-family :Arial,Helvetica,sans-serif; font-size:x-small; color:#666666;}
.cnn_stryichgfcpt{font-family :Arial,Helvetica,sans-serif; font-size:x-small; color:#666666;}
.cnnByline{font-family :Arial,Helvetica,sans-serif; font-size:x-small; color:#666666;}
.cnn_bulletbin cnnStryHghLght{ font-size:xx-small;}
.subhead p{font-family :Arial,Helvetica,sans-serif; font-size:x-small;}
.cnnStoryContent{font-family :Arial,Helvetica,sans-serif; font-size:x-small}
.cnnContentContainer{font-family :Arial,Helvetica,sans-serif; font-size:x-small}
.col1{font-family :Arial,Helvetica,sans-serif; font-size:x-small; color:#666666;}
.col3{color:#333333; font-family :Arial,Helvetica,sans-serif; font-size:x-small;font-weight:bold;}
.cnnInlineT1Caption{font-family :Arial,Helvetica,sans-serif; font-size:x-small;font-weight:bold;}
.cnnInlineT1Credit{font-family :Arial,Helvetica,sans-serif; font-size:x-small;color:#333333;}
.col10{color:#5A637E;}
.cnnInlineRailBulletList{color:black;}
.cnnLine0{font-family :Arial,Helvetica,sans-serif; color:#666666;font-weight:bold;}
.cnnTimeStamp{font-family :Arial,Helvetica,sans-serif; font-size:x-small;color:#333333;}
.galleryhedDek{font-family :Arial,Helvetica,sans-serif; font-size:x-small;color:#575757;}
.galleryWidgetHeader{font-family :Arial,Helvetica,sans-serif; font-size:x-small;color:#004276;}
.article-content{font-family :Arial,Helvetica,sans-serif; font-size:x-small}
.cnnRecapStory{font-family :Arial,Helvetica,sans-serif; font-size:x-small}
h1{font-family :Arial,Helvetica,sans-serif; font-size:x-large}
.captionname{font-family :Arial,Helvetica,sans-serif; font-size:x-small;color:#575757;}
inStoryIE{{font-family :Arial,Helvetica,sans-serif; font-size:x-small;}
'''
#remove_tags_before = dict(name='h1', attrs={'class':'heading'})
#remove_tags_after = dict(name='td', attrs={'class':'newptool1'})
remove_tags = [
dict(name='iframe'),
dict(name='div', attrs={'class':['cnnEndOfStory', 'cnnShareThisItem', 'cnn_strylctcntr cnn_strylctcqrelt', 'cnnShareBoxContent', 'cnn_strybtmcntnt', 'cnn_strycntntrgt']}),
dict(name='div', attrs={'id':['IEContainer', 'clickIncludeBox']}),
#dict(name='ul', attrs={'class':'article-tools'}),
#dict(name='ul', attrs={'class':'articleTools'}),
] ]
keep_only_tags = [dict(id='cnnContentContainer')]
remove_tags = [
{'class':['cnn_strybtntools', 'cnn_strylftcntnt',
'cnn_strybtntools', 'cnn_strybtntoolsbttm', 'cnn_strybtmcntnt',
'cnn_strycntntrgt']},
]
feeds = [ feeds = [
('Top News', 'http://rss.cnn.com/rss/cnn_topstories.rss'), ('Top News', 'http://rss.cnn.com/rss/cnn_topstories.rss'),
('World', 'http://rss.cnn.com/rss/cnn_world.rss'), ('World', 'http://rss.cnn.com/rss/cnn_world.rss'),
@ -84,15 +52,8 @@ class CNN(BasicNewsRecipe):
('Offbeat', 'http://rss.cnn.com/rss/cnn_offbeat.rss'), ('Offbeat', 'http://rss.cnn.com/rss/cnn_offbeat.rss'),
('Most Popular', 'http://rss.cnn.com/rss/cnn_mostpopular.rss') ('Most Popular', 'http://rss.cnn.com/rss/cnn_mostpopular.rss')
] ]
def preprocess_html(self, soup):
story = soup.find(name='div', attrs={'class':'cnnBody_Left'}) def get_article_url(self, article):
if story is None: ans = BasicNewsRecipe.get_article_url(self, article)
story = soup.find(name='div', attrs={'id':'cnnContentContainer'}) return ans.partition('?')[0]
soup = BeautifulSoup('<html><head><title>t</title></head><body></body></html>')
body = soup.find(name='body')
body.insert(0, story)
else:
soup = BeautifulSoup('<html><head><title>t</title></head><body></body></html>')
body = soup.find(name='body')
body.insert(0, story)
return soup

View File

@ -1,52 +0,0 @@
#!/usr/bin/env python
__license__ = 'GPL v3'
__author__ = 'Mori'
__version__ = 'v. 0.1'
'''
www.runa.pl/blog
'''
from calibre.web.feeds.news import BasicNewsRecipe
import re
class FantazmatyRecipe(BasicNewsRecipe):
__author__ = 'Mori'
language = 'pl'
title = u'Fantazmaty'
publisher = u'Agencja Wydawnicza Runa'
description = u'Blog Agencji Wydawniczej Runa'
no_stylesheets = True
remove_javascript = True
encoding = 'utf-8'
oldest_article = 100
max_articles_per_feed = 100
extra_css = '''
img{float: left; padding-right: 10px; padding-bottom: 5px;}
'''
feeds = [
(u'Fantazmaty', u'http://www.runa.pl/blog/rss.xml')
]
remove_tags = [
dict(name = 'div', attrs = {'class' : 'path'}),
dict(name = 'div', attrs = {'class' : 'drdot'}),
dict(name = 'div', attrs = {'class' : 'picture'})
]
remove_tags_after = [
dict(name = 'div', attrs = {'class' : 'content'})
]
preprocess_regexps = [
(re.compile(i[0], re.IGNORECASE | re.DOTALL), i[1]) for i in
[
(r'<body>.*?<div id="primary"', lambda match: '<body><div id="primary"'),
(r'<!--.*?-->', lambda match: '')
]
]

View File

@ -52,6 +52,7 @@ class ANDROID(USBMS):
0x04e8 : { 0x681d : [0x0222, 0x0223, 0x0224, 0x0400], 0x04e8 : { 0x681d : [0x0222, 0x0223, 0x0224, 0x0400],
0x681c : [0x0222, 0x0224, 0x0400], 0x681c : [0x0222, 0x0224, 0x0400],
0x6640 : [0x0100], 0x6640 : [0x0100],
0x685e : [0x0400],
0x6877 : [0x0400], 0x6877 : [0x0400],
}, },
@ -113,7 +114,8 @@ class ANDROID(USBMS):
'MB525'] 'MB525']
WINDOWS_CARD_A_MEM = ['ANDROID_PHONE', 'GT-I9000_CARD', 'SGH-I897', WINDOWS_CARD_A_MEM = ['ANDROID_PHONE', 'GT-I9000_CARD', 'SGH-I897',
'FILE-STOR_GADGET', 'SGH-T959', 'SAMSUNG_ANDROID', 'GT-P1000_CARD', 'FILE-STOR_GADGET', 'SGH-T959', 'SAMSUNG_ANDROID', 'GT-P1000_CARD',
'A70S', 'A101IT', '7', 'INCREDIBLE', 'A7EB', 'SGH-T849_CARD'] 'A70S', 'A101IT', '7', 'INCREDIBLE', 'A7EB', 'SGH-T849_CARD',
'__UMS_COMPOSITE']
OSX_MAIN_MEM = 'Android Device Main Memory' OSX_MAIN_MEM = 'Android Device Main Memory'

View File

@ -2743,7 +2743,6 @@ class ITUNES(DriverBase):
# Update metadata from plugboard # Update metadata from plugboard
# If self.plugboard is None (no transforms), original metadata is returned intact # If self.plugboard is None (no transforms), original metadata is returned intact
metadata_x = self._xform_metadata_via_plugboard(metadata, this_book.format) metadata_x = self._xform_metadata_via_plugboard(metadata, this_book.format)
self.log("metadata.title_sort: %s metadata_x.title_sort: %s" % (metadata.title_sort, metadata_x.title_sort))
if isosx: if isosx:
if lb_added: if lb_added:
lb_added.name.set(metadata_x.title) lb_added.name.set(metadata_x.title)
@ -3024,6 +3023,8 @@ class ITUNES_ASYNC(ITUNES):
pythoncom.CoInitialize() pythoncom.CoInitialize()
self._launch_iTunes() self._launch_iTunes()
except: except:
import traceback
traceback.print_exc()
raise UserFeedback('unable to launch iTunes', details=None, level=UserFeedback.WARN) raise UserFeedback('unable to launch iTunes', details=None, level=UserFeedback.WARN)
finally: finally:
pythoncom.CoUninitialize() pythoncom.CoUninitialize()

View File

@ -35,8 +35,8 @@ class EB600(USBMS):
PRODUCT_ID = [0x1688] PRODUCT_ID = [0x1688]
BCD = [0x110] BCD = [0x110]
VENDOR_NAME = 'NETRONIX' VENDOR_NAME = ['NETRONIX', 'WOLDER']
WINDOWS_MAIN_MEM = 'EBOOK' WINDOWS_MAIN_MEM = ['EBOOK', 'MIBUK_GAMMA_6.2']
WINDOWS_CARD_A_MEM = 'EBOOK' WINDOWS_CARD_A_MEM = 'EBOOK'
OSX_MAIN_MEM = 'EB600 Internal Storage Media' OSX_MAIN_MEM = 'EB600 Internal Storage Media'

View File

@ -115,5 +115,6 @@ class NOOK_TSR(NOOK):
BCD = [0x216] BCD = [0x216]
EBOOK_DIR_MAIN = EBOOK_DIR_CARD_A = 'My Files/Books' EBOOK_DIR_MAIN = EBOOK_DIR_CARD_A = 'My Files/Books'
WINDOWS_MAIN_MEM = WINDOWS_CARD_A_MEM = 'EBOOK_DISK'

View File

@ -54,7 +54,10 @@ class SafeFormat(TemplateFormatter):
key = orig_key key = orig_key
else: else:
raise ValueError(_('Value: unknown field ') + orig_key) raise ValueError(_('Value: unknown field ') + orig_key)
try:
b = self.book.get_user_metadata(key, False) b = self.book.get_user_metadata(key, False)
except:
b = None
if b and b['datatype'] == 'int' and self.book.get(key, 0) == 0: if b and b['datatype'] == 'int' and self.book.get(key, 0) == 0:
v = '' v = ''
elif b and b['datatype'] == 'float' and self.book.get(key, 0.0) == 0.0: elif b and b['datatype'] == 'float' and self.book.get(key, 0.0) == 0.0:

View File

@ -18,7 +18,7 @@ from calibre.ebooks.chardet import xml_to_unicode
from calibre.utils.zipfile import safe_replace from calibre.utils.zipfile import safe_replace
from calibre.utils.config import DynamicConfig from calibre.utils.config import DynamicConfig
from calibre.utils.logging import Log from calibre.utils.logging import Log
from calibre import guess_type, prints from calibre import guess_type, prints, prepare_string_for_xml
from calibre.ebooks.oeb.transforms.cover import CoverManager from calibre.ebooks.oeb.transforms.cover import CoverManager
TITLEPAGE = CoverManager.SVG_TEMPLATE.decode('utf-8').replace(\ TITLEPAGE = CoverManager.SVG_TEMPLATE.decode('utf-8').replace(\
@ -229,8 +229,8 @@ class EbookIterator(object):
cover = self.opf.cover cover = self.opf.cover
if self.ebook_ext in ('lit', 'mobi', 'prc', 'opf', 'fb2') and cover: if self.ebook_ext in ('lit', 'mobi', 'prc', 'opf', 'fb2') and cover:
cfile = os.path.join(self.base, 'calibre_iterator_cover.html') cfile = os.path.join(self.base, 'calibre_iterator_cover.html')
chtml = (TITLEPAGE%os.path.relpath(cover, self.base).replace(os.sep, rcpath = os.path.relpath(cover, self.base).replace(os.sep, '/')
'/')).encode('utf-8') chtml = (TITLEPAGE%prepare_string_for_xml(rcpath, True)).encode('utf-8')
open(cfile, 'wb').write(chtml) open(cfile, 'wb').write(chtml)
self.spine[0:0] = [SpineItem(cfile, self.spine[0:0] = [SpineItem(cfile,
mime_type='application/xhtml+xml')] mime_type='application/xhtml+xml')]

View File

@ -171,10 +171,9 @@ class ConvertAction(InterfaceAction):
raise Exception(_('Empty output file, ' raise Exception(_('Empty output file, '
'probably the conversion process crashed')) 'probably the conversion process crashed'))
data = open(temp_files[-1].name, 'rb') with open(temp_files[-1].name, 'rb') as data:
self.gui.library_view.model().db.add_format(book_id, \ self.gui.library_view.model().db.add_format(book_id, \
fmt, data, index_is_id=True) fmt, data, index_is_id=True)
data.close()
self.gui.status_bar.show_message(job.description + \ self.gui.status_bar.show_message(job.description + \
(' completed'), 2000) (' completed'), 2000)
finally: finally:

View File

@ -5,13 +5,15 @@ __license__ = 'GPL v3'
import json import json
from PyQt4.Qt import (Qt, QDialog, QDialogButtonBox, QSyntaxHighlighter, from PyQt4.Qt import (Qt, QDialog, QDialogButtonBox, QSyntaxHighlighter, QFont,
QRegExp, QApplication, QRegExp, QApplication, QTextCharFormat, QColor, QCursor)
QTextCharFormat, QFont, QColor, QCursor)
from calibre.gui2 import error_dialog
from calibre.gui2.dialogs.template_dialog_ui import Ui_TemplateDialog from calibre.gui2.dialogs.template_dialog_ui import Ui_TemplateDialog
from calibre.utils.formatter_functions import formatter_functions from calibre.utils.formatter_functions import formatter_functions
from calibre.ebooks.metadata.book.base import composite_formatter from calibre.ebooks.metadata.book.base import composite_formatter, Metadata
from calibre.library.coloring import (displayable_columns)
class ParenPosition: class ParenPosition:
@ -195,12 +197,24 @@ class TemplateHighlighter(QSyntaxHighlighter):
class TemplateDialog(QDialog, Ui_TemplateDialog): class TemplateDialog(QDialog, Ui_TemplateDialog):
def __init__(self, parent, text, mi): def __init__(self, parent, text, mi=None, fm=None, color_field=None):
QDialog.__init__(self, parent) QDialog.__init__(self, parent)
Ui_TemplateDialog.__init__(self) Ui_TemplateDialog.__init__(self)
self.setupUi(self) self.setupUi(self)
self.coloring = color_field is not None
if self.coloring:
cols = sorted([k for k in displayable_columns(fm)])
self.colored_field.addItems(cols)
self.colored_field.setCurrentIndex(self.colored_field.findText(color_field))
else:
self.colored_field.setVisible(False)
self.colored_field_label.setVisible(False)
if mi:
self.mi = mi self.mi = mi
else:
self.mi = Metadata(None, None)
# Remove help icon on title bar # Remove help icon on title bar
icon = self.windowIcon() icon = self.windowIcon()
@ -238,24 +252,26 @@ class TemplateDialog(QDialog, Ui_TemplateDialog):
self.function.setCurrentIndex(0) self.function.setCurrentIndex(0)
self.function.currentIndexChanged[str].connect(self.function_changed) self.function.currentIndexChanged[str].connect(self.function_changed)
self.textbox_changed() self.textbox_changed()
self.rule = (None, '')
def textbox_changed(self): def textbox_changed(self):
cur_text = unicode(self.textbox.toPlainText()) cur_text = unicode(self.textbox.toPlainText())
if self.last_text != cur_text: if self.last_text != cur_text:
self.last_text = cur_text self.last_text = cur_text
self.highlighter.regenerate_paren_positions() self.highlighter.regenerate_paren_positions()
self.text_cursor_changed()
self.template_value.setText( self.template_value.setText(
composite_formatter.safe_format(cur_text, self.mi, composite_formatter.safe_format(cur_text, self.mi,
_('EXCEPTION: '), self.mi)) _('EXCEPTION: '), self.mi))
def text_cursor_changed(self): def text_cursor_changed(self):
cursor = self.textbox.textCursor() cursor = self.textbox.textCursor()
block_number = cursor.blockNumber()
pos_in_block = cursor.positionInBlock()
position = cursor.position() position = cursor.position()
t = unicode(self.textbox.toPlainText()) t = unicode(self.textbox.toPlainText())
if position < len(t): if position > 0 and position <= len(t):
self.highlighter.check_cursor_pos(t[position], block_number, block_number = cursor.blockNumber()
pos_in_block = cursor.positionInBlock() - 1
self.highlighter.check_cursor_pos(t[position-1], block_number,
pos_in_block) pos_in_block)
def function_changed(self, toWhat): def function_changed(self, toWhat):
@ -270,3 +286,17 @@ class TemplateDialog(QDialog, Ui_TemplateDialog):
else: else:
self.source_code.setPlainText(self.funcs[name].program_text) self.source_code.setPlainText(self.funcs[name].program_text)
def accept(self):
txt = unicode(self.textbox.toPlainText()).rstrip()
if self.coloring:
if self.colored_field.currentIndex() == -1:
error_dialog(self, _('No column chosen'),
_('You must specify a column to be colored'), show=True)
return
if not txt:
error_dialog(self, _('No template provided'),
_('The template box cannot be empty'), show=True)
return
self.rule = (unicode(self.colored_field.currentText()), txt)
QDialog.accept(self)

View File

@ -20,12 +20,30 @@
<string>Edit Comments</string> <string>Edit Comments</string>
</property> </property>
<layout class="QVBoxLayout" name="verticalLayout"> <layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QHBoxLayout">
<item>
<widget class="QLabel" name="colored_field_label">
<property name="text">
<string>Set the color of the column:</string>
</property>
<property name="buddy">
<cstring>colored_field</cstring>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="colored_field">
</widget>
</item>
</layout>
</item>
<item> <item>
<widget class="QPlainTextEdit" name="textbox"/> <widget class="QPlainTextEdit" name="textbox"/>
</item> </item>
<item> <item>
<layout class="QGridLayout" name="gridLayout"> <layout class="QGridLayout" name="gridLayout">
<item row="0" column="0"> <item row="5" column="0">
<widget class="QLabel"> <widget class="QLabel">
<property name="text"> <property name="text">
<string>Template value:</string> <string>Template value:</string>
@ -38,14 +56,14 @@
</property> </property>
</widget> </widget>
</item> </item>
<item row="0" column="1"> <item row="5" column="1">
<widget class="QLineEdit" name="template_value"> <widget class="QLineEdit" name="template_value">
<property name="readOnly"> <property name="readOnly">
<bool>true</bool> <bool>true</bool>
</property> </property>
</widget> </widget>
</item> </item>
<item row="1" column="1"> <item row="6" column="1">
<widget class="QDialogButtonBox" name="buttonBox"> <widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation"> <property name="orientation">
<enum>Qt::Horizontal</enum> <enum>Qt::Horizontal</enum>
@ -55,7 +73,7 @@
</property> </property>
</widget> </widget>
</item> </item>
<item row="2" column="0"> <item row="6" column="0">
<widget class="QLabel" name="label"> <widget class="QLabel" name="label">
<property name="text"> <property name="text">
<string>Function &amp;name:</string> <string>Function &amp;name:</string>
@ -65,10 +83,10 @@
</property> </property>
</widget> </widget>
</item> </item>
<item row="2" column="1"> <item row="7" column="1">
<widget class="QComboBox" name="function"/> <widget class="QComboBox" name="function"/>
</item> </item>
<item row="3" column="0"> <item row="8" column="0">
<widget class="QLabel" name="label_2"> <widget class="QLabel" name="label_2">
<property name="text"> <property name="text">
<string>&amp;Documentation:</string> <string>&amp;Documentation:</string>
@ -81,7 +99,7 @@
</property> </property>
</widget> </widget>
</item> </item>
<item row="4" column="0"> <item row="9" column="0">
<widget class="QLabel" name="label_3"> <widget class="QLabel" name="label_3">
<property name="text"> <property name="text">
<string>Python &amp;code:</string> <string>Python &amp;code:</string>
@ -94,7 +112,7 @@
</property> </property>
</widget> </widget>
</item> </item>
<item row="3" column="1"> <item row="8" column="1">
<widget class="QPlainTextEdit" name="documentation"> <widget class="QPlainTextEdit" name="documentation">
<property name="maximumSize"> <property name="maximumSize">
<size> <size>
@ -104,7 +122,7 @@
</property> </property>
</widget> </widget>
</item> </item>
<item row="4" column="1"> <item row="9" column="1">
<widget class="QPlainTextEdit" name="source_code"/> <widget class="QPlainTextEdit" name="source_code"/>
</item> </item>
</layout> </layout>

View File

@ -5,17 +5,10 @@ __license__ = 'GPL v3'
__copyright__ = '2010, Kovid Goyal <kovid@kovidgoyal.net>' __copyright__ = '2010, Kovid Goyal <kovid@kovidgoyal.net>'
__docformat__ = 'restructuredtext en' __docformat__ = 'restructuredtext en'
from functools import partial
from collections import defaultdict
from PyQt4.Qt import (Qt, QLineEdit, QDialog, QGridLayout, QLabel, QCheckBox, from PyQt4.Qt import QLineEdit
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.dialogs.template_dialog import TemplateDialog
from calibre.gui2.complete import MultiCompleteLineEdit
from calibre.gui2 import error_dialog
from calibre.utils.icu import sort_key
class TemplateLineEditor(QLineEdit): class TemplateLineEditor(QLineEdit):
@ -25,16 +18,11 @@ class TemplateLineEditor(QLineEdit):
def __init__(self, parent): def __init__(self, parent):
QLineEdit.__init__(self, parent) QLineEdit.__init__(self, parent)
self.tags = None
self.mi = None self.mi = None
self.txt = None
def set_mi(self, mi): def set_mi(self, mi):
self.mi = mi self.mi = mi
def set_db(self, db):
self.db = db
def contextMenuEvent(self, event): def contextMenuEvent(self, event):
menu = self.createStandardContextMenu() menu = self.createStandardContextMenu()
menu.addSeparator() menu.addSeparator()
@ -46,494 +34,10 @@ class TemplateLineEditor(QLineEdit):
menu.exec_(event.globalPos()) menu.exec_(event.globalPos())
def clear_field(self): def clear_field(self):
self.txt = None
self.setText('') self.setText('')
self.setReadOnly(False)
self.setStyleSheet('TemplateLineEditor { color: black }')
def open_editor(self): def open_editor(self):
if self.txt: t = TemplateDialog(self, self.text(), mi=self.mi)
t = TemplateDialog(self, self.txt, self.mi)
else:
t = TemplateDialog(self, self.text(), self.mi)
t.setWindowTitle(_('Edit template')) t.setWindowTitle(_('Edit template'))
if t.exec_(): if t.exec_():
self.txt = None self.setText(t.rule[1])
self.setText(t.textbox.toPlainText())
def enable_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 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.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):
text_template = (" strcmp(field('{f}'), '{v}', '{ltv}', '{eqv}', '{gtv}')", True)
text_empty_template = (" test(field('{f}'), '{fv}', '{tv}')", False)
text_re_template = (" contains(field('{f}'), '{v}', '{tv}', '{fv}')", False)
templates = {
'text.mult' : (" str_in_list(field('{f}'), '{mult}', '{v}', '{tv}', '{fv}')", False),
'text.mult.re' : (" in_list(field('{f}'), '{mult}', '^{v}$', '{tv}', '{fv}')", False),
'text.mult.empty' : (" test(field('{f}'), '{fv}', '{tv}')", False),
'text' : text_template,
'text.re' : text_re_template,
'text.empty' : text_empty_template,
'rating' : (" cmp(raw_field('{f}'), '{v}', '{ltv}', '{eqv}', '{gtv}')", True),
'rating.empty' : text_empty_template,
'int' : (" cmp(raw_field('{f}'), '{v}', '{ltv}', '{eqv}', '{gtv}')", True),
'int.empty' : text_empty_template,
'float' : (" cmp(raw_field('{f}'), '{v}', '{ltv}', '{eqv}', '{gtv}')", True),
'float.empty' : text_empty_template,
'bool' : (" strcmp(field('{f}'), '{v}', '{ltv}', '{eqv}', '{gtv}')", True),
'bool.empty' : text_empty_template,
'datetime' : (" strcmp(format_date(raw_field('{f}'), 'yyyyMMdd'), format_date('{v}', 'yyyyMMdd'), '{ltv}', '{eqv}', '{gtv}')", True),
'datetime.empty' : text_empty_template,
'series' : text_template,
'series.re' : text_re_template,
'series.empty' : text_empty_template,
'composite' : text_template,
'composite.re' : text_re_template,
'composite.empty' : text_empty_template,
'enumeration' : text_template,
'enumeration.re' : text_re_template,
'enumeration.empty' : text_empty_template,
'comments' : text_template,
'comments.re' : text_re_template,
'comments.empty' : text_empty_template,
}
relationals = ('=', '!=', '<', '>', '<=', '>=')
relational_truth_vals = {
'=': ('', '1', ''),
'!=': ('1', '', '1'),
'<': ('1', '', ''),
'>': ('', '', '1'),
'<=': ('1', '1', ''),
'>=': ('', '1', '1'),
}
@staticmethod
def uses_this_wizard(txt):
if not txt or txt.startswith('program:\n#tag wizard'):
return True
return False
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 k.endswith('_index') or (
m['kind'] == 'field' and m['name'] and
k not in ('ondevice', 'path', 'size', 'sort')):
self.columns.append(k)
self.completion_values[k]['dt'] = m['datatype']
if m['is_custom']:
if m['datatype'] in ('int', 'float'):
self.completion_values[k]['v'] = []
elif m['datatype'] == 'bool':
self.completion_values[k]['v'] = [_('Yes'), _('No')]
else:
self.completion_values[k]['v'] = db.all_custom(m['label'])
elif k == 'tags':
self.completion_values[k]['v'] = db.all_tags()
elif k == 'formats':
self.completion_values[k]['v'] = db.all_formats()
else:
if k in ('publisher'):
ck = k + 's'
else:
ck = k
f = getattr(db, 'all_' + ck, None)
if 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()]
else:
self.completion_values[k]['v'] = []
if k in self.completion_values:
if k == 'authors':
mult = '&'
else:
mult = ',' if m['is_multiple'] == '|' else m['is_multiple']
self.completion_values[k]['m'] = mult
self.columns.sort(key=sort_key)
self.columns.insert(0, '')
l = QGridLayout()
self.setLayout(l)
l.setColumnStretch(2, 10)
l.setColumnMinimumWidth(5, 300)
h = QLabel(_('And'))
h.setToolTip('<p>' +
_('Set this box to indicate that the two conditions must both '
'be true to use the color. For example, you '
'can check if two tags are present, if the book has a tag '
'and a #read custom column is checked, or if a book has '
'some tag and has a particular format.'))
l.addWidget(h, 0, 0, 1, 1)
h = QLabel(_('Column'))
h.setAlignment(Qt.AlignCenter)
l.addWidget(h, 0, 1, 1, 1)
h = QLabel(_('is'))
h.setAlignment(Qt.AlignCenter)
l.addWidget(h, 0, 2, 1, 1)
h = QLabel(_('op'))
h.setToolTip('<p>' +
_('Use this box to tell what comparison operation to use. Some '
'comparisons cannot be used with certain options. For example, '
'if regular expressions are used, only equals and not equals '
'are valid.') + '</p>')
h.setAlignment(Qt.AlignCenter)
l.addWidget(h, 0, 3, 1, 1)
c = QLabel(_('empty'))
c.setToolTip('<p>' +
_('Check this box to check if the column is empty') + '</p>')
l.addWidget(c, 0, 4, 1, 1)
h = QLabel(_('Values'))
h.setAlignment(Qt.AlignCenter)
h.setToolTip('<p>' +
_('You can enter more than one value per box, separated by commas. '
'The comparison ignores letter case. Special note: authors are '
'separated by ampersands (&).<br>'
'A value can be a regular expression. Check the box to turn '
'them on. When using regular expressions, note that the wizard '
'puts anchors (^ and $) around the expression, so you '
'must ensure your expression matches from the beginning '
'to the end of the column/value you are checking.<br>'
'Regular expression examples:') + '<ul>' +
_('<li><code><b>.*</b></code> matches anything in the column.</li>'
'<li><code><b>A.*</b></code> matches anything beginning with A</li>'
'<li><code><b>.*mystery.*</b></code> matches anything containing '
'the word "mystery"</li>') + '</ul></p>')
l.addWidget(h , 0, 5, 1, 1)
c = QLabel(_('is RE'))
c.setToolTip('<p>' +
_('Check this box if the values box contains regular expressions') + '</p>')
l.addWidget(c, 0, 6, 1, 1)
c = QLabel(_('color'))
c.setAlignment(Qt.AlignCenter)
c.setToolTip('<p>' +
_('Use this color if the column matches the tests.') + '</p>')
l.addWidget(c, 0, 7, 1, 1)
self.andboxes = []
self.opboxes = []
self.tagboxes = []
self.colorboxes = []
self.reboxes = []
self.colboxes = []
self.emptyboxes = []
self.colors = [unicode(s) for s in list(QColor.colorNames())]
self.colors.insert(0, '')
def create_widget(klass, box, layout, row, col, items,
align=Qt.AlignCenter, rowspan=False):
w = klass(self)
if box is not None:
box.append(w)
if rowspan:
layout.addWidget(w, row, col, 2, 1, alignment=Qt.Alignment(align))
else:
layout.addWidget(w, row, col, 1, 1, alignment=Qt.Alignment(align))
if items:
w.addItems(items)
return w
maxlines = 10
for i in range(1, maxlines+1):
w = create_widget(QCheckBox, self.andboxes, l, i, 0, None, rowspan=True)
w.stateChanged.connect(partial(self.and_box_changed, line=i-1))
if i == maxlines:
# last box is invisible
w.setVisible(False)
w = create_widget(QComboBox, self.colboxes, l, i, 1, self.columns)
w.currentIndexChanged[str].connect(partial(self.column_changed, line=i-1))
w = QLabel(self)
w.setText(_('is'))
l.addWidget(w, i, 2, 1, 1)
w = create_widget(QComboBox, self.opboxes, l, i, 3, None)
w.setMaximumWidth(40)
w = create_widget(QCheckBox, self.emptyboxes, l, i, 4, None)
w.stateChanged.connect(partial(self.empty_box_changed, line=i-1))
create_widget(MultiCompleteLineEdit, self.tagboxes, l, i, 5, None, align=0)
w = create_widget(QCheckBox, self.reboxes, l, i, 6, None)
w.stateChanged.connect(partial(self.re_box_changed, line=i-1))
create_widget(QComboBox, self.colorboxes, l, i, 7, self.colors)
w = create_widget(QLabel, None, l, maxlines+1, 5, None)
w.setText(_('If none of the tests match, set the color to'))
self.elsebox = create_widget(QComboBox, None, l, maxlines+1, 7, self.colors)
self.elsebox.setToolTip('<p>' +
_('If this box contains a color, it will be used if none '
'of the above rules match.') + '</p>')
if txt:
lines = txt.split('\n')[3:]
i = 0
for line in lines:
if line.startswith('#'):
vals = line[1:].split(':|:')
if len(vals) == 1 and line.startswith('#else:'):
try:
self.elsebox.setCurrentIndex(self.elsebox.findText(line[6:]))
except:
pass
continue
if len(vals) == 2:
t, c = vals
f = 'tags'
a = re = e = 0
op = '='
else:
t,c,f,re,a,op,e = vals
try:
self.colboxes[i].setCurrentIndex(self.colboxes[i].findText(f))
self.colorboxes[i].setCurrentIndex(
self.colorboxes[i].findText(c))
self.tagboxes[i].setText(t)
self.reboxes[i].setChecked(re == '2')
self.emptyboxes[i].setChecked(e == '2')
self.andboxes[i].setChecked(a == '2')
self.opboxes[i].setCurrentIndex(self.opboxes[i].findText(op))
i += 1
except:
import traceback
traceback.print_exc()
pass
w = QLabel(_('Preview'))
l.addWidget(w, 99, 1, 1, 1)
w = self.test_box = QLineEdit(self)
w.setReadOnly(True)
l.addWidget(w, 99, 2, 1, 5)
w = QPushButton(_('Test'))
w.setToolTip('<p>' +
_('Press this button to see what color this template will '
'produce for the book that was selected when you '
'entered the preferences dialog.'))
l.addWidget(w, 99, 7, 1, 1)
w.clicked.connect(self.preview)
bb = QDialogButtonBox(QDialogButtonBox.Ok|QDialogButtonBox.Cancel, parent=self)
l.addWidget(bb, 100, 5, 1, 3)
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 generate_program(self):
res = ("program:\n#tag wizard -- do not directly edit\n"
" first_non_empty(\n")
lines = []
was_and = had_line = False
line = 0
for tb, cb, fb, reb, ab, ob, eb in zip(
self.tagboxes, self.colorboxes, self.colboxes,
self.reboxes, self.andboxes, self.opboxes, self.emptyboxes):
f = unicode(fb.currentText())
if not f:
continue
m = self.completion_values[f]['m']
dt = self.completion_values[f]['dt']
c = unicode(cb.currentText()).strip()
re = reb.checkState()
a = ab.checkState()
op = unicode(ob.currentText())
e = eb.checkState()
line += 1
if m:
tags = [t.strip() for t in unicode(tb.text()).split(m) if t.strip()]
if re == 2:
tags = '$|^'.join(tags)
else:
tags = m.join(tags)
if m == '&':
tags = tags.replace(',', '|')
else:
tags = unicode(tb.text()).strip()
if (tags or f) and not ((tags or e) and f and (a == 2 or c)):
error_dialog(self, _('Invalid line'),
_('Line number {0} is not valid').format(line),
show=True, show_copy_button=False)
return False
if not was_and:
if had_line:
lines[-1] += ','
had_line = True
lines.append(" test(and(")
else:
lines[-1] += ','
key = dt + ('.mult' if m else '') + ('.empty' if e else '') + ('.re' if re else '')
tval = '1' if op == '=' else ''
fval = '' if op == '=' else '1'
template, is_relational = self.templates[key]
if is_relational:
ltv, eqv, gtv = self.relational_truth_vals[op]
else:
ltv, eqv, gtv = (None, None, None)
lines.append(template.format(v=tags, f=f, tv=tval, fv=fval, mult=m,
ltv=ltv, eqv=eqv, gtv=gtv))
if a == 2:
was_and = True
else:
was_and = False
lines.append(" ), '{0}', '')".format(c))
res += '\n'.join(lines)
else_txt = unicode(self.elsebox.currentText())
if else_txt:
res += ",\n '" + else_txt + "'"
res += ')\n'
self.template = res
res = ''
for tb, cb, fb, reb, ab, ob, eb in zip(
self.tagboxes, self.colorboxes, self.colboxes,
self.reboxes, self.andboxes, self.opboxes, self.emptyboxes):
t = unicode(tb.text()).strip()
if t.endswith(','):
t = t[:-1]
c = unicode(cb.currentText()).strip()
f = unicode(fb.currentText())
re = unicode(reb.checkState())
a = unicode(ab.checkState())
op = unicode(ob.currentText())
e = unicode(eb.checkState())
if f and (t or e) and (a == '2' or c):
res += '#' + t + ':|:' + c + ':|:' + f + ':|:' + re + ':|:' + \
a + ':|:' + op + ':|:' + e + '\n'
res += '#else:' + else_txt + '\n'
self.template += res
return True
def column_changed(self, s, line=None):
k = unicode(s)
valbox = self.tagboxes[line]
if k in self.completion_values:
valbox.update_items_cache(self.completion_values[k]['v'])
if self.completion_values[k]['m']:
valbox.set_separator(', ')
else:
valbox.set_separator(None)
dt = self.completion_values[k]['dt']
if dt in ('int', 'float', 'rating', 'bool'):
self.reboxes[line].setChecked(0)
self.reboxes[line].setEnabled(False)
else:
self.reboxes[line].setEnabled(True)
self.fill_in_opbox(line)
else:
valbox.update_items_cache([])
valbox.set_separator(None)
def fill_in_opbox(self, line):
opbox = self.opboxes[line]
opbox.clear()
k = unicode(self.colboxes[line].currentText())
if not k:
return
if k in self.completion_values:
rebox = self.reboxes[line]
ebox = self.emptyboxes[line]
idx = opbox.currentIndex()
if self.completion_values[k]['m'] or \
rebox.checkState() == 2 or ebox.checkState() == 2:
opbox.addItems(self.relationals[0:2])
idx = idx if idx < 2 else 0
else:
opbox.addItems(self.relationals)
opbox.setCurrentIndex(max(idx, 0))
def re_box_changed(self, state, line=None):
self.fill_in_opbox(line)
def empty_box_changed(self, state, line=None):
if state == 2:
self.tagboxes[line].setText('')
self.tagboxes[line].setEnabled(False)
self.reboxes[line].setChecked(0)
self.reboxes[line].setEnabled(False)
else:
self.reboxes[line].setEnabled(True)
self.tagboxes[line].setEnabled(True)
self.fill_in_opbox(line)
def and_box_changed(self, state, line=None):
if state == 2:
self.colorboxes[line].setCurrentIndex(0)
self.colorboxes[line].setEnabled(False)
else:
self.colorboxes[line].setEnabled(True)
def accepted(self):
if self.generate_program():
self.accept()
else:
self.template = ''

View File

@ -426,7 +426,7 @@ class CcTemplateDelegate(QStyledItemDelegate): # {{{
editor.textbox.setTabStopWidth(20) editor.textbox.setTabStopWidth(20)
d = editor.exec_() d = editor.exec_()
if d: if d:
m.setData(index, QVariant(editor.textbox.toPlainText()), Qt.EditRole) m.setData(index, QVariant(editor.rule[1]), Qt.EditRole)
return None return None
def setModelData(self, editor, model, index): def setModelData(self, editor, model, index):

View File

@ -99,8 +99,7 @@ class BooksModel(QAbstractTableModel): # {{{
self.ids_to_highlight_set = set() self.ids_to_highlight_set = set()
self.current_highlighted_idx = None self.current_highlighted_idx = None
self.highlight_only = False self.highlight_only = False
self.column_color_list = [] self.colors = frozenset([unicode(c) for c in QColor.colorNames()])
self.colors = [unicode(c) for c in QColor.colorNames()]
self.read_config() self.read_config()
def change_alignment(self, colname, alignment): def change_alignment(self, colname, alignment):
@ -156,7 +155,6 @@ class BooksModel(QAbstractTableModel): # {{{
self.headers[col] = self.custom_columns[col]['name'] self.headers[col] = self.custom_columns[col]['name']
self.build_data_convertors() self.build_data_convertors()
self.set_color_templates(reset=False)
self.reset() self.reset()
self.database_changed.emit(db) self.database_changed.emit(db)
self.stop_metadata_backup() self.stop_metadata_backup()
@ -545,16 +543,6 @@ class BooksModel(QAbstractTableModel): # {{{
img = self.default_image img = self.default_image
return img return img
def set_color_templates(self, reset=True):
self.column_color_list = []
for i in range(1,self.db.column_color_count+1):
name = self.db.prefs.get('column_color_name_'+str(i))
if name:
self.column_color_list.append((name,
self.db.prefs.get('column_color_template_'+str(i))))
if reset:
self.reset()
def build_data_convertors(self): def build_data_convertors(self):
def authors(r, idx=-1): def authors(r, idx=-1):
au = self.db.data[r][idx] au = self.db.data[r][idx]
@ -726,14 +714,16 @@ class BooksModel(QAbstractTableModel): # {{{
return QVariant(QColor('lightgreen')) return QVariant(QColor('lightgreen'))
elif role == Qt.ForegroundRole: elif role == Qt.ForegroundRole:
key = self.column_map[col] key = self.column_map[col]
for k,fmt in self.column_color_list: mi = None
for k, fmt in self.db.prefs['column_color_rules']:
if k != key: if k != key:
continue continue
id_ = self.id(index) id_ = self.id(index)
if id_ in self.color_cache: if id_ in self.color_cache:
if key in self.color_cache[id_]: if key in self.color_cache[id_]:
return self.color_cache[id_][key] return self.color_cache[id_][key]
mi = self.db.get_metadata(self.id(index), index_is_id=True) if mi is None:
mi = self.db.get_metadata(id_, index_is_id=True)
try: try:
color = composite_formatter.safe_format(fmt, mi, '', mi) color = composite_formatter.safe_format(fmt, mi, '', mi)
if color in self.colors: if color in self.colors:
@ -743,7 +733,7 @@ class BooksModel(QAbstractTableModel): # {{{
self.color_cache[id_][key] = color self.color_cache[id_][key] = color
return color return color
except: except:
return NONE continue
if self.is_custom_column(key) and \ if self.is_custom_column(key) and \
self.custom_columns[key]['datatype'] == 'enumeration': self.custom_columns[key]['datatype'] == 'enumeration':
cc = self.custom_columns[self.column_map[col]]['display'] cc = self.custom_columns[self.column_map[col]]['display']

View File

@ -35,8 +35,9 @@ from calibre import force_unicode
class RichTextDelegate(QStyledItemDelegate): # {{{ class RichTextDelegate(QStyledItemDelegate): # {{{
def __init__(self, parent=None): def __init__(self, parent=None, max_width=160):
QStyledItemDelegate.__init__(self, parent) QStyledItemDelegate.__init__(self, parent)
self.max_width = max_width
def to_doc(self, index): def to_doc(self, index):
doc = QTextDocument() doc = QTextDocument()
@ -46,8 +47,8 @@ class RichTextDelegate(QStyledItemDelegate): # {{{
def sizeHint(self, option, index): def sizeHint(self, option, index):
doc = self.to_doc(index) doc = self.to_doc(index)
ans = doc.size().toSize() ans = doc.size().toSize()
if ans.width() > 150: if ans.width() > self.max_width - 10:
ans.setWidth(160) ans.setWidth(self.max_width)
ans.setHeight(ans.height()+10) ans.setHeight(ans.height()+10)
return ans return ans

View File

@ -7,14 +7,17 @@ __license__ = 'GPL v3'
__copyright__ = '2011, Kovid Goyal <kovid@kovidgoyal.net>' __copyright__ = '2011, Kovid Goyal <kovid@kovidgoyal.net>'
__docformat__ = 'restructuredtext en' __docformat__ = 'restructuredtext en'
from PyQt4.Qt import (QWidget, QDialog, QLabel, QGridLayout, QComboBox, from PyQt4.Qt import (QWidget, QDialog, QLabel, QGridLayout, QComboBox, QSize,
QLineEdit, QIntValidator, QDoubleValidator, QFrame, QColor, Qt, QIcon, QLineEdit, QIntValidator, QDoubleValidator, QFrame, QColor, Qt, QIcon,
QScrollArea, QPushButton, QVBoxLayout, QDialogButtonBox) QScrollArea, QPushButton, QVBoxLayout, QDialogButtonBox, QToolButton,
QListView, QAbstractListModel, pyqtSignal, QSizePolicy, QSpacerItem)
from calibre.utils.icu import sort_key from calibre.utils.icu import sort_key
from calibre.gui2 import error_dialog from calibre.gui2 import error_dialog
from calibre.gui2.dialogs.template_dialog import TemplateDialog
from calibre.gui2.metadata.single_download import RichTextDelegate
from calibre.library.coloring import (Rule, conditionable_columns, from calibre.library.coloring import (Rule, conditionable_columns,
displayable_columns) displayable_columns, rule_from_template)
class ConditionEditor(QWidget): # {{{ class ConditionEditor(QWidget): # {{{
@ -28,6 +31,14 @@ class ConditionEditor(QWidget): # {{{
(_('is false'), 'is false'), (_('is false'), 'is false'),
(_('is undefined'), 'is undefined') (_('is undefined'), 'is undefined')
), ),
'ondevice' : (
(_('is true'), 'is set',),
(_('is false'), 'is not set'),
),
'identifiers' : (
(_('has id'), 'has id'),
(_('does not have id'), 'does not have id'),
),
'int' : ( 'int' : (
(_('is equal to'), 'eq'), (_('is equal to'), 'eq'),
(_('is less than'), 'lt'), (_('is less than'), 'lt'),
@ -69,7 +80,7 @@ class ConditionEditor(QWidget): # {{{
self.action_box = QComboBox(self) self.action_box = QComboBox(self)
l.addWidget(self.action_box, 0, 3) l.addWidget(self.action_box, 0, 3)
self.l3 = l3 = QLabel(_(' the value ')) self.l3 = l3 = QLabel(_(' value '))
l.addWidget(l3, 0, 4) l.addWidget(l3, 0, 4)
self.value_box = QLineEdit(self) self.value_box = QLineEdit(self)
@ -78,7 +89,7 @@ class ConditionEditor(QWidget): # {{{
self.column_box.addItem('', '') self.column_box.addItem('', '')
for key in sorted( for key in sorted(
conditionable_columns(fm), conditionable_columns(fm),
key=lambda x:sort_key(fm[x]['name'])): key=sort_key):
self.column_box.addItem(key, key) self.column_box.addItem(key, key)
self.column_box.setCurrentIndex(0) self.column_box.setCurrentIndex(0)
@ -151,6 +162,11 @@ class ConditionEditor(QWidget): # {{{
dt = m['datatype'] dt = m['datatype']
if dt in self.action_map: if dt in self.action_map:
actions = self.action_map[dt] actions = self.action_map[dt]
else:
if col == 'ondevice':
k = 'ondevice'
elif col == 'identifiers':
k = 'identifiers'
else: else:
k = 'multiple' if m['is_multiple'] else 'single' k = 'multiple' if m['is_multiple'] else 'single'
actions = self.action_map[k] actions = self.action_map[k]
@ -173,7 +189,10 @@ class ConditionEditor(QWidget): # {{{
if not col or not action: if not col or not action:
return return
tt = '' tt = ''
if dt in ('int', 'float', 'rating'): if col == 'identifiers':
tt = _('Enter either an identifier type or an '
'identifier type and value of the form identifier:value')
elif dt in ('int', 'float', 'rating'):
tt = _('Enter a number') tt = _('Enter a number')
v = QIntValidator if dt == 'int' else QDoubleValidator v = QIntValidator if dt == 'int' else QDoubleValidator
self.value_box.setValidator(v(self.value_box)) self.value_box.setValidator(v(self.value_box))
@ -181,9 +200,12 @@ class ConditionEditor(QWidget): # {{{
self.value_box.setInputMask('9999-99-99') self.value_box.setInputMask('9999-99-99')
tt = _('Enter a date in the format YYYY-MM-DD') tt = _('Enter a date in the format YYYY-MM-DD')
else: else:
tt = _('Enter a string') tt = _('Enter a string.')
if 'pattern' in action: if 'pattern' in action:
tt = _('Enter a regular expression') tt = _('Enter a regular expression')
elif m.get('is_multiple', False):
tt += '\n' + _('You can match multiple values by separating'
' them with %s')%m['is_multiple']
self.value_box.setToolTip(tt) self.value_box.setToolTip(tt)
if action in ('is set', 'is not set', 'is true', 'is false', if action in ('is set', 'is not set', 'is true', 'is false',
'is undefined'): 'is undefined'):
@ -204,11 +226,11 @@ class RuleEditor(QDialog): # {{{
self.l1 = l1 = QLabel(_('Create a coloring rule by' self.l1 = l1 = QLabel(_('Create a coloring rule by'
' filling in the boxes below')) ' filling in the boxes below'))
l.addWidget(l1, 0, 0, 1, 4) l.addWidget(l1, 0, 0, 1, 5)
self.f1 = QFrame(self) self.f1 = QFrame(self)
self.f1.setFrameShape(QFrame.HLine) self.f1.setFrameShape(QFrame.HLine)
l.addWidget(self.f1, 1, 0, 1, 4) l.addWidget(self.f1, 1, 0, 1, 5)
self.l2 = l2 = QLabel(_('Set the color of the column:')) self.l2 = l2 = QLabel(_('Set the color of the column:'))
l.addWidget(l2, 2, 0) l.addWidget(l2, 2, 0)
@ -217,37 +239,36 @@ class RuleEditor(QDialog): # {{{
l.addWidget(self.column_box, 2, 1) l.addWidget(self.column_box, 2, 1)
self.l3 = l3 = QLabel(_('to')) self.l3 = l3 = QLabel(_('to'))
l3.setAlignment(Qt.AlignHCenter)
l.addWidget(l3, 2, 2) l.addWidget(l3, 2, 2)
self.color_box = QComboBox(self) self.color_box = QComboBox(self)
l.addWidget(self.color_box, 2, 3) l.addWidget(self.color_box, 2, 3)
l.addItem(QSpacerItem(10, 10, QSizePolicy.Expanding), 2, 4)
self.l4 = l4 = QLabel( self.l4 = l4 = QLabel(
_('Only if the following conditions are all satisfied:')) _('Only if the following conditions are all satisfied:'))
l4.setAlignment(Qt.AlignHCenter) l.addWidget(l4, 3, 0, 1, 5)
l.addWidget(l4, 3, 0, 1, 4)
self.scroll_area = sa = QScrollArea(self) self.scroll_area = sa = QScrollArea(self)
sa.setMinimumHeight(300) sa.setMinimumHeight(300)
sa.setMinimumWidth(950) sa.setMinimumWidth(950)
sa.setWidgetResizable(True) sa.setWidgetResizable(True)
l.addWidget(sa, 4, 0, 1, 4) l.addWidget(sa, 4, 0, 1, 5)
self.add_button = b = QPushButton(QIcon(I('plus.png')), self.add_button = b = QPushButton(QIcon(I('plus.png')),
_('Add another condition')) _('Add another condition'))
l.addWidget(b, 5, 0, 1, 4) l.addWidget(b, 5, 0, 1, 5)
b.clicked.connect(self.add_blank_condition) b.clicked.connect(self.add_blank_condition)
self.l5 = l5 = QLabel(_('You can disable a condition by' self.l5 = l5 = QLabel(_('You can disable a condition by'
' blanking all of its boxes')) ' blanking all of its boxes'))
l.addWidget(l5, 6, 0, 1, 4) l.addWidget(l5, 6, 0, 1, 5)
self.bb = bb = QDialogButtonBox( self.bb = bb = QDialogButtonBox(
QDialogButtonBox.Ok|QDialogButtonBox.Cancel) QDialogButtonBox.Ok|QDialogButtonBox.Cancel)
bb.accepted.connect(self.accept) bb.accepted.connect(self.accept)
bb.rejected.connect(self.reject) bb.rejected.connect(self.reject)
l.addWidget(bb, 7, 0, 1, 4) l.addWidget(bb, 7, 0, 1, 5)
self.conditions_widget = QWidget(self) self.conditions_widget = QWidget(self)
sa.setWidget(self.conditions_widget) sa.setWidget(self.conditions_widget)
@ -261,7 +282,7 @@ class RuleEditor(QDialog): # {{{
for key in sorted( for key in sorted(
displayable_columns(fm), displayable_columns(fm),
key=lambda x:sort_key(fm[x]['name'])): key=sort_key):
name = fm[key]['name'] name = fm[key]['name']
if name: if name:
self.column_box.addItem(key, key) self.column_box.addItem(key, key)
@ -277,6 +298,27 @@ class RuleEditor(QDialog): # {{{
self.conditions.append(c) self.conditions.append(c)
self.conditions_widget.layout().addWidget(c) self.conditions_widget.layout().addWidget(c)
def apply_rule(self, col, rule):
for i in range(self.column_box.count()):
c = unicode(self.column_box.itemData(i).toString())
if col == c:
self.column_box.setCurrentIndex(i)
break
if rule.color:
idx = self.color_box.findText(rule.color)
if idx >= 0:
self.color_box.setCurrentIndex(idx)
for c in rule.conditions:
ce = ConditionEditor(self.fm, parent=self.conditions_widget)
self.conditions.append(ce)
self.conditions_widget.layout().addWidget(ce)
try:
ce.condition = c
except:
import traceback
traceback.print_exc()
def accept(self): def accept(self):
if self.validate(): if self.validate():
QDialog.accept(self) QDialog.accept(self)
@ -316,11 +358,232 @@ class RuleEditor(QDialog): # {{{
return col, r return col, r
# }}} # }}}
class EditRules(QWidget): class RulesModel(QAbstractListModel): # {{{
def __init__(self, db, parent=None): def __init__(self, prefs, fm, parent=None):
QAbstractListModel.__init__(self, parent)
self.fm = fm
rules = list(prefs['column_color_rules'])
self.rules = []
for col, template in rules:
try:
rule = rule_from_template(self.fm, template)
except:
rule = template
self.rules.append((col, rule))
def rowCount(self, *args):
return len(self.rules)
def data(self, index, role):
row = index.row()
try:
col, rule = self.rules[row]
except:
return None
if role == Qt.DisplayRole:
return self.rule_to_html(col, rule)
if role == Qt.UserRole:
return (col, rule)
def add_rule(self, col, rule):
self.rules.append((col, rule))
self.reset()
return self.index(len(self.rules)-1)
def replace_rule(self, index, col, r):
self.rules[index.row()] = (col, r)
self.dataChanged.emit(index, index)
def remove_rule(self, index):
self.rules.remove(self.rules[index.row()])
self.reset()
def commit(self, prefs):
rules = []
for col, r in self.rules:
if isinstance(r, Rule):
r = r.template
if r is not None:
rules.append((col, r))
prefs['column_color_rules'] = rules
def move(self, idx, delta):
row = idx.row() + delta
if row >= 0 and row < len(self.rules):
t = self.rules[row]
self.rules[row] = self.rules[row-delta]
self.rules[row-delta] = t
self.dataChanged.emit(idx, idx)
idx = self.index(row)
self.dataChanged.emit(idx, idx)
return idx
def clear(self):
self.rules = []
self.reset()
def rule_to_html(self, col, rule):
if not isinstance(rule, Rule):
return _('''
<p>Advanced Rule for column <b>%s</b>:
<pre>%s</pre>
''')%(col, rule)
conditions = [self.condition_to_html(c) for c in rule.conditions]
return _('''\
<p>Set the color of <b>%s</b> to <b>%s</b> if the following
conditions are met:</p>
<ul>%s</ul>
''') % (col, rule.color, ''.join(conditions))
def condition_to_html(self, condition):
return (
_('<li>If the <b>%s</b> column <b>%s</b> value: <b>%s</b>') %
tuple(condition))
# }}}
class EditRules(QWidget): # {{{
changed = pyqtSignal()
def __init__(self, parent=None):
QWidget.__init__(self, parent) QWidget.__init__(self, parent)
self.db = db
self.l = l = QGridLayout(self)
self.setLayout(l)
self.l1 = l1 = QLabel(_(
'You can control the color of columns in the'
' book list by creating "rules" that tell calibre'
' what color to use. Click the Add Rule button below'
' to get started. You can change an existing rule by double'
' clicking it.'))
l1.setWordWrap(True)
l.addWidget(l1, 0, 0, 1, 2)
self.add_button = QPushButton(QIcon(I('plus.png')), _('Add Rule'),
self)
self.remove_button = QPushButton(QIcon(I('minus.png')),
_('Remove Rule'), self)
self.add_button.clicked.connect(self.add_rule)
self.remove_button.clicked.connect(self.remove_rule)
l.addWidget(self.add_button, 1, 0)
l.addWidget(self.remove_button, 1, 1)
self.g = g = QGridLayout()
self.rules_view = QListView(self)
self.rules_view.doubleClicked.connect(self.edit_rule)
self.rules_view.setSelectionMode(self.rules_view.SingleSelection)
self.rules_view.setAlternatingRowColors(True)
self.rtfd = RichTextDelegate(parent=self.rules_view, max_width=400)
self.rules_view.setItemDelegate(self.rtfd)
g.addWidget(self.rules_view, 0, 0, 2, 1)
self.up_button = b = QToolButton(self)
b.setIcon(QIcon(I('arrow-up.png')))
b.setToolTip(_('Move the selected rule up'))
b.clicked.connect(self.move_up)
g.addWidget(b, 0, 1, 1, 1, Qt.AlignTop)
self.down_button = b = QToolButton(self)
b.setIcon(QIcon(I('arrow-down.png')))
b.setToolTip(_('Move the selected rule down'))
b.clicked.connect(self.move_down)
g.addWidget(b, 1, 1, 1, 1, Qt.AlignBottom)
l.addLayout(g, 2, 0, 1, 2)
l.setRowStretch(2, 10)
self.add_advanced_button = b = QPushButton(QIcon(I('plus.png')),
_('Add Advanced Rule'), self)
b.clicked.connect(self.add_advanced)
l.addWidget(b, 3, 0, 1, 2)
def initialize(self, fm, prefs, mi):
self.model = RulesModel(prefs, fm)
self.rules_view.setModel(self.model)
self.fm = fm
self.mi = mi
def _add_rule(self, dlg):
if dlg.exec_() == dlg.Accepted:
col, r = dlg.rule
if r and col:
idx = self.model.add_rule(col, r)
self.rules_view.scrollTo(idx)
self.changed.emit()
def add_rule(self):
d = RuleEditor(self.model.fm)
d.add_blank_condition()
self._add_rule(d)
def add_advanced(self):
td = TemplateDialog(self, '', mi=self.mi, fm=self.fm, color_field='')
self._add_rule(td)
def edit_rule(self, index):
try:
col, rule = self.model.data(index, Qt.UserRole)
except:
return
if isinstance(rule, Rule):
d = RuleEditor(self.model.fm)
d.apply_rule(col, rule)
else:
d = TemplateDialog(self, rule, mi=self.mi, fm=self.fm, color_field=col)
if d.exec_() == d.Accepted:
col, r = d.rule
if r is not None and col:
self.model.replace_rule(index, col, r)
self.rules_view.scrollTo(index)
self.changed.emit()
def get_selected_row(self, txt):
sm = self.rules_view.selectionModel()
rows = list(sm.selectedRows())
if not rows:
error_dialog(self, _('No rule selected'),
_('No rule selected for %s.')%txt, show=True)
return None
return rows[0]
def remove_rule(self):
row = self.get_selected_row(_('removal'))
if row is not None:
self.model.remove_rule(row)
self.changed.emit()
def move_up(self):
idx = self.rules_view.currentIndex()
if idx.isValid():
idx = self.model.move(idx, -1)
if idx is not None:
sm = self.rules_view.selectionModel()
sm.select(idx, sm.ClearAndSelect)
self.rules_view.setCurrentIndex(idx)
self.changed.emit()
def move_down(self):
idx = self.rules_view.currentIndex()
if idx.isValid():
idx = self.model.move(idx, 1)
if idx is not None:
sm = self.rules_view.selectionModel()
sm.select(idx, sm.ClearAndSelect)
self.rules_view.setCurrentIndex(idx)
self.changed.emit()
def clear(self):
self.model.clear()
self.changed.emit()
def commit(self, prefs):
self.model.commit(prefs)
# }}}
if __name__ == '__main__': if __name__ == '__main__':
from PyQt4.Qt import QApplication from PyQt4.Qt import QApplication
@ -328,7 +591,10 @@ if __name__ == '__main__':
from calibre.library import db from calibre.library import db
d = RuleEditor(db().field_metadata) db = db()
if False:
d = RuleEditor(db.field_metadata)
d.add_blank_condition() d.add_blank_condition()
d.exec_() d.exec_()
@ -337,4 +603,12 @@ if __name__ == '__main__':
print ('Column to be colored:', col) print ('Column to be colored:', col)
print ('Template:') print ('Template:')
print (r.template) print (r.template)
else:
d = EditRules()
d.resize(QSize(800, 600))
d.initialize(db.field_metadata, db.prefs, None)
d.show()
app.exec_()
d.commit(db.prefs)

View File

@ -5,21 +5,19 @@ __license__ = 'GPL v3'
__copyright__ = '2010, Kovid Goyal <kovid@kovidgoyal.net>' __copyright__ = '2010, Kovid Goyal <kovid@kovidgoyal.net>'
__docformat__ = 'restructuredtext en' __docformat__ = 'restructuredtext en'
from functools import partial
from PyQt4.Qt import (QApplication, QFont, QFontInfo, QFontDialog, from PyQt4.Qt import (QApplication, QFont, QFontInfo, QFontDialog,
QAbstractListModel, Qt, QColor, QIcon, QToolButton, QComboBox) QAbstractListModel, Qt, QIcon)
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
from calibre.gui2 import config, gprefs, qt_app from calibre.gui2 import config, gprefs, qt_app
from calibre.gui2.dialogs.template_line_editor import TemplateLineEditor
from calibre.utils.localization import (available_translations, 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 import NONE
from calibre.gui2.book_details import get_field_list from calibre.gui2.book_details import get_field_list
from calibre.gui2.preferences.coloring import EditRules
class DisplayedFields(QAbstractListModel): # {{{ class DisplayedFields(QAbstractListModel): # {{{
@ -162,117 +160,11 @@ class ConfigWidget(ConfigWidgetBase, Ui_Form):
self.df_up_button.clicked.connect(self.move_df_up) self.df_up_button.clicked.connect(self.move_df_up)
self.df_down_button.clicked.connect(self.move_df_down) self.df_down_button.clicked.connect(self.move_df_down)
self.color_help_text.setText('<p>' + self.edit_rules = EditRules(self.tabWidget)
_('Here you can specify coloring rules for columns shown in the ' self.edit_rules.changed.connect(self.changed_signal)
'library view. Choose the column you wish to color, then ' self.tabWidget.addTab(self.edit_rules,
'supply a template that specifies the color to use based on ' QIcon(I('format-fill-color.png')), _('Column coloring'))
'the values in the column. There is a ' self.tabWidget.setCurrentIndex(0)
'<a href="http://manual.calibre-ebook.com/template_lang.html">'
'tutorial</a> on using templates.') +
'</p><p>' +
_('If you want to color a field based on contents of columns, '
'then click the button next to an empty line to open the wizard. '
'It will build a template for you. You can later edit that '
'template with the same wizard. This is by far the easiest '
'way to specify a template.') +
'</p><p>' +
_('If you manually construct a template, then the template must '
'evaluate to a valid color name shown in the color names box.'
'You can use any legal template expression. '
'For example, you can set the title to always display in '
'green using the template "green" (without the quotes). '
'To show the title in the color named in the custom column '
'#column, use "{#column}". To show the title in blue if the '
'custom column #column contains the value "foo", in red if the '
'column contains the value "bar", otherwise in black, use '
'<pre>{#column:switch(foo,blue,bar,red,black)}</pre>'
'To show the title in blue if the book has the exact tag '
'"Science Fiction", red if the book has the exact tag '
'"Mystery", or black if the book has neither tag, use'
"<pre>program: \n"
" t = field('tags'); \n"
" first_non_empty(\n"
" in_list(t, ',', '^Science Fiction$', 'blue', ''), \n"
" in_list(t, ',', '^Mystery$', 'red', 'black'))</pre>"
'To show the title in green if it has one format, blue if it '
'two formats, and red if more, use'
"<pre>program:cmp(count(field('formats'),','), 2, 'green', 'blue', 'red')</pre>") +
'</p><p>' +
_('You can access a multi-line template editor from the '
'context menu (right-click).') + '</p><p>' +
_('<b>Note:</b> if you want to color a "custom column with a fixed set '
'of values", it is often easier to specify the '
'colors in the column definition dialog. There you can '
'provide a color for each value without using a template.')+ '</p>')
self.color_help_scrollArea.setVisible(False)
self.color_help_button.clicked.connect(self.change_help_text)
self.colors_scrollArea.setVisible(False)
self.colors_label.setVisible(False)
self.colors_button.clicked.connect(self.change_colors_text)
choices = db.field_metadata.displayable_field_keys()
choices.sort(key=sort_key)
choices.insert(0, '')
self.column_color_count = db.column_color_count+1
mi=None
try:
idx = gui.library_view.currentIndex().row()
mi = db.get_metadata(idx, index_is_id=False)
except:
pass
l = self.column_color_layout
for i in range(1, self.column_color_count):
ccn = QComboBox(parent=self)
setattr(self, 'opt_column_color_name_'+str(i), ccn)
l.addWidget(ccn, i, 0, 1, 1)
wtb = QToolButton(parent=self)
setattr(self, 'opt_column_color_wizard_'+str(i), wtb)
wtb.setIcon(QIcon(I('wizard.png')))
l.addWidget(wtb, i, 1, 1, 1)
ttb = QToolButton(parent=self)
setattr(self, 'opt_column_color_tpledit_'+str(i), ttb)
ttb.setIcon(QIcon(I('edit_input.png')))
l.addWidget(ttb, i, 2, 1, 1)
tpl = TemplateLineEditor(parent=self)
setattr(self, 'opt_column_color_template_'+str(i), tpl)
tpl.textChanged.connect(partial(self.tpl_edit_text_changed, ctrl=i))
tpl.set_db(db)
tpl.set_mi(mi)
l.addWidget(tpl, i, 3, 1, 1)
wtb.clicked.connect(tpl.tag_wizard)
ttb.clicked.connect(tpl.open_editor)
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)
wtb.setEnabled(tpl.enable_wizard_button(txt))
ttb.setEnabled(not tpl.enable_wizard_button(txt) or not txt)
all_colors = [unicode(s) for s in list(QColor.colorNames())]
self.colors_box.setText(', '.join(all_colors))
def change_help_text(self):
self.color_help_scrollArea.setVisible(not self.color_help_scrollArea.isVisible())
def change_colors_text(self):
self.colors_scrollArea.setVisible(not self.colors_scrollArea.isVisible())
self.colors_label.setVisible(not self.colors_label.isVisible())
def tpl_edit_text_changed(self, ign, ctrl=None):
tpl = getattr(self, 'opt_column_color_template_'+str(ctrl))
txt = unicode(tpl.text())
wtb = getattr(self, 'opt_column_color_wizard_'+str(ctrl))
ttb = getattr(self, 'opt_column_color_tpledit_'+str(ctrl))
wtb.setEnabled(tpl.enable_wizard_button(txt))
ttb.setEnabled(not tpl.enable_wizard_button(txt) or not txt)
tpl.setFocus()
def initialize(self): def initialize(self):
ConfigWidgetBase.initialize(self) ConfigWidgetBase.initialize(self)
@ -283,6 +175,13 @@ class ConfigWidget(ConfigWidgetBase, Ui_Form):
self.current_font = self.initial_font = font self.current_font = self.initial_font = font
self.update_font_display() self.update_font_display()
self.display_model.initialize() self.display_model.initialize()
db = self.gui.current_db
try:
idx = self.gui.library_view.currentIndex().row()
mi = db.get_metadata(idx, index_is_id=False)
except:
mi=None
self.edit_rules.initialize(db.field_metadata, db.prefs, mi)
def restore_defaults(self): def restore_defaults(self):
ConfigWidgetBase.restore_defaults(self) ConfigWidgetBase.restore_defaults(self)
@ -292,6 +191,7 @@ class ConfigWidget(ConfigWidgetBase, Ui_Form):
self.changed_signal.emit() self.changed_signal.emit()
self.update_font_display() self.update_font_display()
self.display_model.restore_defaults() self.display_model.restore_defaults()
self.edit_rules.clear()
self.changed_signal.emit() self.changed_signal.emit()
def build_font_obj(self): def build_font_obj(self):
@ -341,12 +241,6 @@ class ConfigWidget(ConfigWidgetBase, Ui_Form):
self.changed_signal.emit() self.changed_signal.emit()
def commit(self, *args): def commit(self, *args):
for i in range(1, self.column_color_count):
col = getattr(self, 'opt_column_color_name_'+str(i))
tpl = getattr(self, 'opt_column_color_template_'+str(i))
if not col.currentIndex() or not unicode(tpl.text()).strip():
col.setCurrentIndex(0)
tpl.setText('')
rr = ConfigWidgetBase.commit(self, *args) rr = ConfigWidgetBase.commit(self, *args)
if self.current_font != self.initial_font: if self.current_font != self.initial_font:
gprefs['font'] = (self.current_font[:4] if self.current_font else gprefs['font'] = (self.current_font[:4] if self.current_font else
@ -356,10 +250,11 @@ class ConfigWidget(ConfigWidgetBase, Ui_Form):
QApplication.setFont(self.font_display.font()) QApplication.setFont(self.font_display.font())
rr = True rr = True
self.display_model.commit() self.display_model.commit()
self.edit_rules.commit(self.gui.current_db.prefs)
return rr return rr
def refresh_gui(self, gui): def refresh_gui(self, gui):
gui.library_view.model().set_color_templates() gui.library_view.model().reset()
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() gui.library_view.refresh_book_details()

View File

@ -6,7 +6,7 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>717</width> <width>820</width>
<height>519</height> <height>519</height>
</rect> </rect>
</property> </property>
@ -407,161 +407,6 @@ then the tags will be displayed each on their own line.</string>
</item> </item>
</layout> </layout>
</widget> </widget>
<widget class="QWidget" name="tab_5">
<attribute name="icon">
<iconset resource="../../../../resources/images.qrc">
<normaloff>:/images/format-fill-color.png</normaloff>:/images/format-fill-color.png</iconset>
</attribute>
<attribute name="title">
<string>Column Coloring</string>
</attribute>
<layout class="QGridLayout" name="column_color_layout">
<item row="0" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>Column to color</string>
</property>
</widget>
</item>
<item row="0" column="3">
<layout class="QHBoxLayout">
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>Color selection template</string>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>10</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>The template wizard is easiest to use</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="color_help_button">
<property name="text">
<string>Show/hide help text</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="colors_button">
<property name="text">
<string>Show/hide colors</string>
</property>
</widget>
</item>
</layout>
</item>
<item row="20" column="0">
<widget class="QLabel" name="colors_label">
<property name="text">
<string>Color names</string>
</property>
</widget>
</item>
<item row="21" column="0" colspan="8">
<widget class="QScrollArea" name="colors_scrollArea">
<property name="maximumSize">
<size>
<width>16777215</width>
<height>300</height>
</size>
</property>
<property name="widgetResizable">
<bool>true</bool>
</property>
<widget class="QWidget" name="scrollAreaWidgetContents_2">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>687</width>
<height>61</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QLabel" name="colors_box">
<property name="wordWrap">
<bool>true</bool>
</property>
<property name="alignment">
<set>Qt::AlignLeft|Qt::AlignTop</set>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
</item>
<item row="30" column="0" colspan="8">
<widget class="QScrollArea" name="color_help_scrollArea">
<property name="minimumSize">
<size>
<width>0</width>
<height>200</height>
</size>
</property>
<property name="widgetResizable">
<bool>true</bool>
</property>
<property name="alignment">
<set>Qt::AlignLeft|Qt::AlignTop</set>
</property>
<widget class="QWidget" name="scrollAreaWidgetContents">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>687</width>
<height>194</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="color_help_text">
<property name="wordWrap">
<bool>true</bool>
</property>
<property name="openExternalLinks">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
</item>
<item row="40" column="0">
<spacer>
<property name="sizePolicy">
<sizepolicy vsizetype="Expanding" hsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>10</verstretch>
</sizepolicy>
</property>
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>0</width>
<height>0</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
</widget> </widget>
</item> </item>
</layout> </layout>
@ -572,11 +417,6 @@ then the tags will be displayed each on their own line.</string>
<extends>QLineEdit</extends> <extends>QLineEdit</extends>
<header>calibre/gui2/complete.h</header> <header>calibre/gui2/complete.h</header>
</customwidget> </customwidget>
<customwidget>
<class>TemplateLineEditor</class>
<extends>QLineEdit</extends>
<header>calibre/gui2/dialogs/template_line_editor.h</header>
</customwidget>
</customwidgets> </customwidgets>
<resources> <resources>
<include location="../../../../resources/images.qrc"/> <include location="../../../../resources/images.qrc"/>

View File

@ -283,7 +283,9 @@ class ConfigWidget(ConfigWidgetBase, Ui_Form):
self.fields_model.dataChanged.connect(self.changed_signal) self.fields_model.dataChanged.connect(self.changed_signal)
self.select_all_button.clicked.connect(self.fields_model.select_all) self.select_all_button.clicked.connect(self.fields_model.select_all)
self.select_all_button.clicked.connect(self.changed_signal)
self.clear_all_button.clicked.connect(self.fields_model.clear_all) self.clear_all_button.clicked.connect(self.fields_model.clear_all)
self.clear_all_button.clicked.connect(self.changed_signal)
def configure_plugin(self): def configure_plugin(self):
for index in self.sources_view.selectionModel().selectedRows(): for index in self.sources_view.selectionModel().selectedRows():

View File

@ -61,7 +61,7 @@ class Rule(object): # {{{
{sig} {sig}
test(and( test(and(
{conditions} {conditions}
), {color}, ''); ), '{color}', '');
''').format(sig=self.signature, conditions=conditions, ''').format(sig=self.signature, conditions=conditions,
color=self.color) color=self.color)
@ -70,6 +70,12 @@ class Rule(object): # {{{
m = self.fm[col] m = self.fm[col]
dt = m['datatype'] dt = m['datatype']
if col == 'ondevice':
return self.ondevice_condition(col, action, val)
if col == 'identifiers':
return self.identifiers_condition(col, action, val)
if dt == 'bool': if dt == 'bool':
return self.bool_condition(col, action, val) return self.bool_condition(col, action, val)
@ -85,6 +91,17 @@ class Rule(object): # {{{
return self.multiple_condition(col, action, val, ism) return self.multiple_condition(col, action, val, ism)
return self.text_condition(col, action, val) return self.text_condition(col, action, val)
def identifiers_condition(self, col, action, val):
if action == 'has id':
return "identifier_in_list(field('identifiers'), '%s', '1', '')"%val
return "identifier_in_list(field('identifiers'), '%s', '', '1')"%val
def ondevice_condition(self, col, action, val):
if action == 'is set':
return "test(ondevice(), '1', '')"
if action == 'is not set':
return "test(ondevice(), '', '1')"
def bool_condition(self, col, action, val): def bool_condition(self, col, action, val):
test = {'is true': 'True', test = {'is true': 'True',
'is false': 'False', 'is false': 'False',
@ -98,7 +115,7 @@ class Rule(object): # {{{
'gt': ('', '', '1') 'gt': ('', '', '1')
}[action] }[action]
lt, eq, gt = '', '1', '' lt, eq, gt = '', '1', ''
return "cmp(field('%s'), %s, '%s', '%s', '%s')" % (col, val, lt, eq, gt) return "cmp(raw_field('%s'), %s, '%s', '%s', '%s')" % (col, val, lt, eq, gt)
def date_condition(self, col, action, val): def date_condition(self, col, action, val):
lt, eq, gt = { lt, eq, gt = {
@ -167,12 +184,26 @@ def conditionable_columns(fm):
dt = m['datatype'] dt = m['datatype']
if m.get('name', False) and dt in ('bool', 'int', 'float', 'rating', 'series', if m.get('name', False) and dt in ('bool', 'int', 'float', 'rating', 'series',
'comments', 'text', 'enumeration', 'datetime'): 'comments', 'text', 'enumeration', 'datetime'):
if key == 'sort':
yield 'title_sort'
else:
yield key yield key
def displayable_columns(fm): def displayable_columns(fm):
for key in fm.displayable_field_keys(): for key in fm.displayable_field_keys():
if key not in ('sort', 'author_sort', 'comments', 'formats', if key not in ('sort', 'author_sort', 'comments', 'formats',
'identifiers', 'path'): 'identifiers', 'path'):
yield key yield key
def migrate_old_rule(fm, template):
if template.startswith('program:\n#tag wizard'):
rules = []
for line in template.splitlines():
if line.startswith('#') and ':|:' in line:
value, color = line[1:].split(':|:')
r = Rule(fm, color=color)
r.add_condition('tags', 'has', value)
rules.append(r.template)
return rules
return [template]

View File

@ -7,7 +7,7 @@ __docformat__ = 'restructuredtext en'
The database used to store ebook metadata The database used to store ebook metadata
''' '''
import os, sys, shutil, cStringIO, glob, time, functools, traceback, re, \ import os, sys, shutil, cStringIO, glob, time, functools, traceback, re, \
json, uuid json, uuid, tempfile
import threading, random import threading, random
from itertools import repeat from itertools import repeat
from math import ceil from math import ceil
@ -211,10 +211,7 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns):
defs = self.prefs.defaults defs = self.prefs.defaults
defs['gui_restriction'] = defs['cs_restriction'] = '' defs['gui_restriction'] = defs['cs_restriction'] = ''
defs['categories_using_hierarchy'] = [] defs['categories_using_hierarchy'] = []
self.column_color_count = 5 defs['column_color_rules'] = []
for i in range(1,self.column_color_count+1):
defs['column_color_name_'+str(i)] = ''
defs['column_color_template_'+str(i)] = ''
# Migrate the bool tristate tweak # Migrate the bool tristate tweak
defs['bools_are_tristate'] = \ defs['bools_are_tristate'] = \
@ -222,6 +219,24 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns):
if self.prefs.get('bools_are_tristate') is None: if self.prefs.get('bools_are_tristate') is None:
self.prefs.set('bools_are_tristate', defs['bools_are_tristate']) self.prefs.set('bools_are_tristate', defs['bools_are_tristate'])
# Migrate column coloring rules
if self.prefs.get('column_color_name_1', None) is not None:
from calibre.library.coloring import migrate_old_rule
old_rules = []
for i in range(1, 6):
col = self.prefs.get('column_color_name_'+str(i), None)
templ = self.prefs.get('column_color_template_'+str(i), None)
if col and templ:
try:
del self.prefs['column_color_name_'+str(i)]
rules = migrate_old_rule(self.field_metadata, templ)
for templ in rules:
old_rules.append((col, templ))
except:
pass
if old_rules:
self.prefs['column_color_rules'] += old_rules
# Migrate saved search and user categories to db preference scheme # Migrate saved search and user categories to db preference scheme
def migrate_preference(key, default): def migrate_preference(key, default):
oldval = prefs[key] oldval = prefs[key]
@ -576,10 +591,12 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns):
f.write(cdata) f.write(cdata)
for format in formats: for format in formats:
# Get data as string (can't use file as source and target files may be the same) # Get data as string (can't use file as source and target files may be the same)
f = self.format(id, format, index_is_id=True, as_file=False) f = self.format(id, format, index_is_id=True, as_file=True)
if not f: if f is None:
continue continue
stream = cStringIO.StringIO(f) with tempfile.SpooledTemporaryFile(max_size=100*(1024**2)) as stream:
shutil.copyfileobj(f, stream)
stream.seek(0)
self.add_format(id, format, stream, index_is_id=True, self.add_format(id, format, stream, index_is_id=True,
path=tpath, notify=False) path=tpath, notify=False)
self.conn.execute('UPDATE books SET path=? WHERE id=?', (path, id)) self.conn.execute('UPDATE books SET path=? WHERE id=?', (path, id))
@ -1556,13 +1573,13 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns):
if ids is not None: if ids is not None:
count = self.conn.get('''SELECT COUNT(id) count = self.conn.get('''SELECT COUNT(id)
FROM data FROM data
WHERE format="%s" AND WHERE format=? AND
books_list_filter(book)'''%fmt, books_list_filter(book)''', (fmt,),
all=False) all=False)
else: else:
count = self.conn.get('''SELECT COUNT(id) count = self.conn.get('''SELECT COUNT(id)
FROM data FROM data
WHERE format="%s"'''%fmt, WHERE format=?''', (fmt,),
all=False) all=False)
if count > 0: if count > 0:
categories['formats'].append(Tag(fmt, count=count, icon=icon, categories['formats'].append(Tag(fmt, count=count, icon=icon,
@ -1584,13 +1601,13 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns):
if ids is not None: if ids is not None:
count = self.conn.get('''SELECT COUNT(book) count = self.conn.get('''SELECT COUNT(book)
FROM identifiers FROM identifiers
WHERE type="%s" AND WHERE type=? AND
books_list_filter(book)'''%ident, books_list_filter(book)''', (ident,),
all=False) all=False)
else: else:
count = self.conn.get('''SELECT COUNT(id) count = self.conn.get('''SELECT COUNT(id)
FROM identifiers FROM identifiers
WHERE type="%s"'''%ident, WHERE type=?''', (ident,),
all=False) all=False)
if count > 0: if count > 0:
categories['identifiers'].append(Tag(ident, count=count, icon=icon, categories['identifiers'].append(Tag(ident, count=count, icon=icon,

View File

@ -374,6 +374,8 @@ class FieldMetadata(dict):
self.get = self._tb_cats.get self.get = self._tb_cats.get
def __getitem__(self, key): def __getitem__(self, key):
if key == 'title_sort':
return self._tb_cats['sort']
return self._tb_cats[key] return self._tb_cats[key]
def __setitem__(self, key, val): def __setitem__(self, key, val):
@ -390,6 +392,8 @@ class FieldMetadata(dict):
return self.has_key(key) return self.has_key(key)
def has_key(self, key): def has_key(self, key):
if key == 'title_sort':
return True
return key in self._tb_cats return key in self._tb_cats
def keys(self): def keys(self):

View File

@ -11,6 +11,7 @@ You can "install" calibre onto a USB stick that you can take with you and use on
* Run a Mobile Calibre installation with both the Calibre binaries and your ebook library resident on a USB disk or other portable media. In particular it is not necessary to have Calibre installed on the Windows PC that is to run Calibre. This batch file also does not care what drive letter is assigned when you plug in the USB device. It also will not affect any settings on the host machine being a completely self-contained Calibre installation. * Run a Mobile Calibre installation with both the Calibre binaries and your ebook library resident on a USB disk or other portable media. In particular it is not necessary to have Calibre installed on the Windows PC that is to run Calibre. This batch file also does not care what drive letter is assigned when you plug in the USB device. It also will not affect any settings on the host machine being a completely self-contained Calibre installation.
* Run a networked Calibre installation optimised for performance when the ebook files are located on a networked share. * Run a networked Calibre installation optimised for performance when the ebook files are located on a networked share.
If you find setting up the bat file too challenging, there is a third party portable calibre build available at `portableapps.com http://portableapps.com`_.
This calibre-portable.bat file is intended for use on Windows based systems, but the principles are easily adapted for use on Linux or OS X based systems. Note that calibre requires the Microsoft Visual C++ 2008 runtimes to run. Most windows computers have them installed already, but it may be a good idea to have the installer for installing them on your USB stick. The installer is available from `Microsoft <http://www.microsoft.com/downloads/details.aspx?FamilyID=9b2da534-3e03-4391-8a4d-074b9f2bc1bf&displaylang=en>`_. This calibre-portable.bat file is intended for use on Windows based systems, but the principles are easily adapted for use on Linux or OS X based systems. Note that calibre requires the Microsoft Visual C++ 2008 runtimes to run. Most windows computers have them installed already, but it may be a good idea to have the installer for installing them on your USB stick. The installer is available from `Microsoft <http://www.microsoft.com/downloads/details.aspx?FamilyID=9b2da534-3e03-4391-8a4d-074b9f2bc1bf&displaylang=en>`_.

View File

@ -361,8 +361,7 @@ class BuiltinInList(BuiltinFormatterFunction):
class BuiltinStrInList(BuiltinFormatterFunction): class BuiltinStrInList(BuiltinFormatterFunction):
name = 'str_in_list' name = 'str_in_list'
arg_count = 5 arg_count = 5
category = 'List Lookup' category = 'List lookup'
category = 'Iterating over values'
__doc__ = doc = _('str_in_list(val, separator, string, found_val, not_found_val) -- ' __doc__ = doc = _('str_in_list(val, separator, string, found_val, not_found_val) -- '
'treat val as a list of items separated by separator, ' 'treat val as a list of items separated by separator, '
'comparing the string against each value in the list. If the ' 'comparing the string against each value in the list. If the '
@ -380,6 +379,32 @@ class BuiltinStrInList(BuiltinFormatterFunction):
return fv return fv
return nfv return nfv
class BuiltinIdentifierInList(BuiltinFormatterFunction):
name = 'identifier_in_list'
arg_count = 4
category = 'List lookup'
__doc__ = doc = _('identifier_in_list(val, id, found_val, not_found_val) -- '
'treat val as a list of identifiers separated by commas, '
'comparing the string against each value in the list. An identifier '
'has the format "identifier:value". The id parameter should be '
'either "id" or "id:regexp". The first case matches if there is any '
'identifier with that id. The second case matches if the regexp '
'matches the identifier\'s value. If there is a match, '
'return found_val, otherwise return not_found_val.')
def evaluate(self, formatter, kwargs, mi, locals, val, ident, fv, nfv):
l = [v.strip() for v in val.split(',') if v.strip()]
(id, _, regexp) = ident.partition(':')
if not id:
return nfv
id += ':'
if l:
for v in l:
if v.startswith(id):
if not regexp or re.search(regexp, v[len(id):], flags=re.I):
return fv
return nfv
class BuiltinRe(BuiltinFormatterFunction): class BuiltinRe(BuiltinFormatterFunction):
name = 're' name = 're'
arg_count = 3 arg_count = 3
@ -748,6 +773,7 @@ builtin_eval = BuiltinEval()
builtin_first_non_empty = BuiltinFirstNonEmpty() builtin_first_non_empty = BuiltinFirstNonEmpty()
builtin_field = BuiltinField() builtin_field = BuiltinField()
builtin_format_date = BuiltinFormatDate() builtin_format_date = BuiltinFormatDate()
builtin_identifier_in_list = BuiltinIdentifierInList()
builtin_ifempty = BuiltinIfempty() builtin_ifempty = BuiltinIfempty()
builtin_in_list = BuiltinInList() builtin_in_list = BuiltinInList()
builtin_list_item = BuiltinListitem() builtin_list_item = BuiltinListitem()