~mjz/awn-extras/to-do-two

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#-*- coding: utf-8 -*-
#
# Copyright (c) 2010 Sharkbaitbobby <sharkbaitbobby+awn@gmail.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
#
#To-Do applet - icon file

import cairo
from math import pi

import backends

#Setup the colors - lists going from darkest to lightest
#These are the Tango Desktop Project Color Palatte
#(The last set of numbers is the color of the number, added by yours truly)
colors = {}
colors['butter'] = [[252,233,79],[237,212,0],[196,160,0],[64,64,64]]
colors['chameleon'] = [[138,226,52],[115,210,22],[78,154,6],[255,255,255]]
colors['orange'] = [[252,175,62],[245,121,0],[206,92,0],[255,255,255]]
colors['skyblue'] = [[114,159,207],[52,101,164],[32,74,135],[255,255,255]]
colors['plum'] = [[173,127,168],[117,80,123],[92,53,102],[255,255,255]]
colors['chocolate'] = [[233,185,110],[193,125,17],[143,89,2],[255,255,255]]
colors['scarletred'] = [[239,41,41],[204,0,0],[164,0,0],[255,255,255]]
colors['aluminium1'] = [[238,238,236],[211,215,207],[186,189,182],[64,64,64]]
colors['aluminium2'] = [[136,138,133],[85,87,83],[46,52,54],[255,255,255]]

def icon(size, _list, colors):
    surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, size, size)
    cr = cairo.Context(surface)

    #Clear the surface
    cr.set_operator(cairo.OPERATOR_CLEAR)
    cr.paint()
    cr.set_operator(cairo.OPERATOR_OVER)

    #Get the needed data based on the icon type

    #progress: border shows progress, number shows percent completed
    if len(_list) == 0:
        progress = 100
        num_unfinished = 0

    else:
        progress = 0
        num_unfinished = 0
        num_items = 0

        for child in _list:
            if isinstance(child, backends.Group):
                for item in child:
                    if int(item['progress']) != 100:
                        num_unfinished += 1

                    num_items += 1
                    progress += int(item['progress'])

            else:
                if int(child['progress']) != 100:
                    num_unfinished += 1

                num_items += 1
                progress += int(child['progress'])

        if num_unfinished == 0:
            progress = 100

        else:
            progress = progress / num_items

    #Outer circle
    cr.set_source_rgba(*colors[0].get_cairo_color())
    cr.arc(size / 2.0, size / 2.0, size / 2.0 - 1, 0, 2 * pi)
    cr.stroke()
    cr.close_path()

    cr.set_line_width(2)

    #Inner border
    if _list['icon_mode'] in ['1', '2']:
        #(Probably) medium background
        cr.set_source_rgba(*colors[2].get_cairo_color())
        cr.arc(size / 2.0, size / 2.0, size / 2.0 - 2, 0, 2 * pi)
        cr.stroke()
        cr.close_path()
        
        #(Probably) lightest foreground to indicate progress
        cr.set_source_rgba(*colors[1].get_cairo_color())

        if progress == 100:
            cr.arc_negative(size / 2.0, size / 2.0, size / 2.0 - 2,
                3 * pi / 2, 3 * pi / 2.0 + pi * 2 * progress / 100.0)
        else:
            cr.arc_negative(size / 2.0, size / 2.0, size / 2.0 - 2,
                3 * pi / 2, 3 * pi / 2.0 + pi * 2 * progress / 100.0 + 0.001)
        cr.stroke()
        cr.close_path()

    #Draw the middle circle normally
    else:
        cr.set_source_rgba(*colors[2].get_cairo_color())
        cr.arc(size / 2.0, size / 2.0, size / 2.0 - 2, 0, 2 * pi)
        cr.stroke()
        cr.close_path()

    #Draw the inside circle and fill it
    cr.set_source_rgba(*colors[1].get_cairo_color())
    cr.arc(size / 2.0, size / 2.0, size / 2.0 - 2.5, 0, 2 * pi)
    cr.clip()
    cr.paint()

    #Draw the number
    #Set up the font of the number
    cr.select_font_face("monospace", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_BOLD)
    cr.set_source_rgba(*colors[3].get_cairo_color())
    #Actually draw the number
    #determine the size and position of the number based on its digits
    #Note that the number of "digits" includes any % sign

    if _list['icon_mode'] == '1':
        text = str(progress) + '%'

    else:
        text = str(num_unfinished)

    #1-digit number
    if len(text) == 1:
        cr.move_to(*scale(size, 13, 36))
        cr.set_font_size(*scale(size, 38))
        cr.show_text(text)

    #2-digit number
    elif len(text) == 2:
        cr.move_to(*scale(size, 8, 31))
        cr.set_font_size(*scale(size, 26))
        cr.show_text(text)

    #3+-digit number
    elif len(text) == 3:
        cr.move_to(*scale(size, 7, 28))
        cr.set_font_size(*scale(size, 19))
        cr.show_text(text)

    #100% or the user is crazy (>= 1000 items)
    else:
        cr.move_to(*scale(size, 6, 27))
        cr.set_font_size(*scale(size, 15))
        cr.show_text(text)

    #Finish the drawing
    cr.close_path()
    cr.set_line_width(1)
    cr.stroke()

    del cr
    return surface

def scale(size, *args):
    li = []
    for arg in args:
        li.append(arg * size / 48.0)

    return li

def button_icon(item, colors):
    size = 16
    surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, size, size)
    cr = cairo.Context(surface)

    #Clear the surface
    cr.set_operator(cairo.OPERATOR_CLEAR)
    cr.paint()
    cr.set_operator(cairo.OPERATOR_OVER)

    progress = float(item['progress']) / 100

    cr.set_line_width(1)

    #Outer circle
    cr.set_source_rgba(*colors[0].get_cairo_color()[:3] + (1.0, ))
    cr.arc(size / 2.0, size / 2.0, size / 2.0 - 1, 0, 2 * pi)
    cr.stroke()
    cr.close_path()

    #Inner border
    #(Probably) medium background
    cr.set_source_rgba(*colors[2].get_cairo_color()[:3] + (1.0, ))
    cr.arc(size / 2.0, size / 2.0, size / 2.0 - 2, 3 * pi / 2, 3 * pi / 2.0 + pi * 2 * progress)
    cr.line_to(size / 2.0, size / 2.0)
    cr.clip()

    cr.close_path()
    cr.fill()
    cr.paint()

    #Finish the drawing
    cr.close_path()
    cr.stroke()

    del cr
    return surface