Quantcast
Channel: MobileRead Forums
Viewing all articles
Browse latest Browse all 46939

Rules, templates, and functions for column coloring and composed icons

$
0
0
I want to visually format my library (the column grid) as follows:

1) Color all columns red if the book contains no ebook formats (although it might have other formats, such as LNK or MP3).

2) Fill a column with "composed icons" (a series of icons) to represent different statuses of the book:
a) if I own a physical copy of the book (#own:"=p")
b) if the book is part of a series
c) if the book has a LNK format (a folder shortcut)

There seem to be many different ways to accomplish the above. Following are some of the things I've tried. Some work, and I'm requesting help to fix the things that don't.

Ideally I would like to use the most efficient method possible, to minimize any impact on calibre's performance. So, to the best of my knowedge, the methods I describe below are arranged in order of increasing efficiency (but please correct me if I'm wrong :wink: )

METHOD 1 - Basic rules - THIS WORKS

1) Color all columns red if the book contains no ebook formats:

Preferences > Look and Feel > Column coloring:

Set the color of <All Columns> to <red>
if the <Formats> column <does not have> values <EPUB, MOBI, PDF>

(Note: Since a book might have other formats such as LNK or MP3, I can't just use the rule: if the <Formats> column <is not set>.)

2) Fill a column with "composed icons":

Preferences > Look and Feel > Column icons:

Set the <composed icons w/no text> of <Icons> to <book.png>
if the <Own> column <has> value <p>

Set the <composed icons w/no text> of <Icons> to <series.png>
if the <Series> column <is set>

Set the <composed icons w/no text> of <Icons> to <folder.png>
if the <Formats> column <has> value <LNK>


METHOD 2 - Multiple advanced rules for column icons - THIS WORKS

Preferences > Look and Feel > Column icons:

Advanced Rule: Set <composed icons w/no text> for column <Icons>:
program:
contains(field('#own'), 'p', 'book.png', '')

Advanced Rule: Set <composed icons w/no text> for column <Icons>:
program:
test(field('series'), 'series.png', '')

Advanced Rule: Set <composed icons w/no text> for column <Icons>:
program:
contains(approximate_formats(), 'LNK', 'folder.png', '')


METHOD 3 - Single advanced rule for column icons - PARTIALLY WORKS

Advanced Rule: Set <composed icons w/no text> for column <Icons>:
Code:

program:
        list_union(
                '',
                strcat(
                        contains(field('#own'), 'p', 'book.png,', ''),
                        test(field('series'), 'series.png', ''),
                        contains(approximate_formats(), 'LNK', 'folder.png,', ''),
                ), ',')

This rule sort of works: an icon is displayed if only one of the tests is true, but if two or more tests are true, no icons are displayed.

Note: I got the idea for the above rule from chaley's posts here and here.


METHOD 4 - User-defined template functions

As chaley has suggested (e.g. here), a custom template function would probably be the most efficient method, but I haven't been able to get very far.

For example:

1) Color all columns red if the book contains no ebook formats:

Preferences > Look and Feel > Template Functions:

name: has_ebook
arg count: 1
doc: has_ebook(val) -- evaluate whether 'val' (passed as 'approximate_formats()') has any of the given ebook formats, and return "true" or "false"
program code:
Code:

def evaluate(self, formatter, kwargs, mi, locals, val):
        contains(val, 'EPUB|MOBI|PDF', 'true', 'false')

Preferences > Add your own columns > Add custom column:

Column type: Column built from other columns
Template (press F2 in grid to edit):

Code:

program:
        f = approximate_formats();
        has_ebook(f)

Result:
EXCEPTION: global name 'contains' is not defined


If you've made it this far, thanks for reading! It's been an interesting exploration, and I'd be very grateful for any feedback. :thanks:

calibre 2.63 [64bit] on Windows 10 Pro

Viewing all articles
Browse latest Browse all 46939

Trending Articles