~roignac/software-center/bug-630731-free-software-view

« back to all changes in this revision

Viewing changes to softwarecenter/ui/gtk3/views/catview_gtk.py

  • Committer: Michael Vogt
  • Date: 2011-07-22 16:15:26 UTC
  • mfrom: (1917.4.43 lobby-aesthetics)
  • Revision ID: michael.vogt@ubuntu.com-20110722161526-fuwe66wp1x0qq5zr
mergedĀ lp:~mmcg069/software-center/the-aestheticsĀ 

Show diffs side-by-side

added added

removed removed

Lines of Context:
7
7
from gettext import gettext as _
8
8
 
9
9
from appview import AppViewFilter
10
 
from softwarecenter.ui.gtk3.widgets.containers import FlowableGrid
11
 
from softwarecenter.ui.gtk3.widgets.buttons import CategoryTile
 
10
from softwarecenter.enums import NonAppVisibility
 
11
from softwarecenter.ui.gtk3.models.appstore2 import AppEnquire, AppPropertiesHelper
 
12
from softwarecenter.ui.gtk3.widgets.containers import *
 
13
from softwarecenter.ui.gtk3.widgets.exhibits import ExhibitBanner
 
14
from softwarecenter.ui.gtk3.widgets.buttons import (LabelTile,
 
15
                                                    CategoryTile,
 
16
                                                    FeaturedTile)
 
17
from softwarecenter.ui.gtk3.em import StockEms
12
18
from softwarecenter.db.categories import (Category,
13
 
                                          CategoriesParser, 
 
19
                                          CategoriesParser,
 
20
                                          get_category_by_name,
14
21
                                          categories_sorted_by_name)
15
22
 
16
23
LOG_ALLOCATION = logging.getLogger("softwarecenter.ui.gtk.allocation")
17
24
LOG=logging.getLogger(__name__)
18
25
 
19
 
 
 
26
# temp: fake exhibits
 
27
from mock import Mock
 
28
exhibits_list = []
 
29
for (i, (title, url)) in enumerate([
 
30
        ("1 some title", "https://wiki.ubuntu.com/Brand?action=AttachFile&do=get&target=orangeubuntulogo.png"),
 
31
        ("2 another title", "https://wiki.ubuntu.com/Brand?action=AttachFile&do=get&target=blackeubuntulogo.png"),
 
32
        ("3 yet another title", "https://wiki.ubuntu.com/Brand?action=AttachFile&do=get&target=xubuntu.png"),
 
33
        ]):
 
34
     exhibit = Mock()
 
35
     exhibit.background_color = "#000000"
 
36
     exhibit.banner_url = url
 
37
     exhibit.date_created = "2011-07-20 08:49:15"
 
38
     exhibit.font_color = "#000000"
 
39
     exhibit.font_name = ""
 
40
     exhibit.font_size = 24
 
41
     exhibit.id = i
 
42
     exhibit.package_names = "apt,2vcard"
 
43
     exhibit.published = True
 
44
     exhibit.title_coords = [10, 10]
 
45
     exhibit.title_translated = title
 
46
     exhibits_list.append(exhibit)
 
47
 
 
48
 
 
49
_asset_cache = {}
20
50
class CategoriesViewGtk(Gtk.Viewport, CategoriesParser):
21
51
 
22
52
    __gsignals__ = {
40
70
                                   (),)
41
71
        }
42
72
 
 
73
    SPACING = PADDING = 3
 
74
 
 
75
    # art stuff
 
76
    STIPPLE = "softwarecenter/ui/gtk3/art/stipple.png"
43
77
 
44
78
    def __init__(self, 
45
79
                 datadir,
74
108
        # setup base widgets
75
109
        # we have our own viewport so we know when the viewport grows/shrinks
76
110
        # setup widgets
77
 
        self.vbox = Gtk.VBox()
78
 
        self.vbox.set_spacing(18)
79
 
        self.vbox.set_border_width(20)
 
111
 
 
112
        self.vbox = Gtk.VBox(spacing=3)
80
113
        self.add(self.vbox)
81
114
 
82
115
        # atk stuff
92
125
        # more stuff
93
126
        self._poster_sigs = []
94
127
        self._allocation = None
 
128
 
 
129
        assets = self._cache_art_assets()
 
130
        self.vbox.connect("draw", self.on_draw, assets)
 
131
        self._prev_alloc = None
 
132
        self.connect("size-allocate", self.on_size_allocate)
 
133
        return
 
134
 
 
135
    def on_size_allocate(self, widget, _):
 
136
        a = widget.get_allocation()
 
137
        prev = self._prev_alloc
 
138
        if prev is None or a.width != prev.width or a.height != prev.height:
 
139
            self._prev_alloc = a
 
140
            self.queue_draw()
 
141
        return
 
142
 
 
143
    def _cache_art_assets(self):
 
144
        global _asset_cache
 
145
        if _asset_cache: return _asset_cache
 
146
        assets = _asset_cache
 
147
        # cache the bg pattern
 
148
        surf = cairo.ImageSurface.create_from_png(self.STIPPLE)
 
149
        ptrn = cairo.SurfacePattern(surf)
 
150
        ptrn.set_extend(cairo.EXTEND_REPEAT)
 
151
        assets["stipple"] = ptrn
 
152
        return assets
 
153
 
 
154
    def on_app_clicked(self, btn, app):
 
155
        """emit the category-selected signal when a category was clicked"""
 
156
        def idle_emit():
 
157
            self.emit("application-selected", app)
 
158
            self.emit("application-activated", app)
 
159
            return False
 
160
 
 
161
        GObject.timeout_add(50, idle_emit)
 
162
        return
 
163
 
 
164
    def on_category_clicked(self, btn, cat):
 
165
        """emit the category-selected signal when a category was clicked"""
 
166
        def idle_emit():
 
167
            self.emit("category-selected", cat)
 
168
            return False
 
169
 
 
170
        GObject.timeout_add(50, idle_emit)
95
171
        return
96
172
 
97
173
    def build(self, desktopdir):
98
174
        pass
99
175
 
100
 
    #~ def _on_app_clicked(self, btn):
101
 
        #~ app = btn.app
102
 
        #~ appname = app[AppStore.COL_APP_NAME]
103
 
        #~ pkgname = app[AppStore.COL_PKGNAME]
104
 
        #~ rating = app[AppStore.COL_RATING]
105
 
        #~ self.emit("application-selected", Application(appname, pkgname, "", rating))
106
 
        #~ self.emit("application-activated", Application(appname, pkgname, "", rating))
107
 
        #~ return False
108
 
 
109
 
    def _on_category_clicked(self, cat_btn, cat):
110
 
        """emit the category-selected signal when a category was clicked"""
111
 
        LOG.debug("on_category_changed: %s" % cat.name)
112
 
        self.emit("category-selected", cat)
 
176
    def on_draw(self, widget, cr, assets):
 
177
        cr.set_source(assets["stipple"])
 
178
        cr.paint()
113
179
        return
114
180
 
115
181
    def set_section(self, section):
154
220
        # these methods add sections to the page
155
221
        # changing order of methods changes order that they appear in the page
156
222
        #~ self._append_recommendations()
 
223
        self._append_banner_ads()
 
224
 
 
225
        self.top_hbox = Gtk.HBox(spacing=self.SPACING)
 
226
        top_hbox_alignment = Gtk.Alignment()
 
227
        top_hbox_alignment.set_padding(0, 0, self.PADDING, self.PADDING)
 
228
        top_hbox_alignment.add(self.top_hbox)
 
229
        self.vbox.pack_start(top_hbox_alignment, False, False, 0)
 
230
 
157
231
        self._append_departments()
158
 
        #~ self._append_featured()
159
 
        #~ self._append_whatsnew()
 
232
 
 
233
        self.right_column = Gtk.Box.new(Gtk.Orientation.VERTICAL, self.SPACING)
 
234
        self.top_hbox.pack_start(self.right_column, True, True, 0)
 
235
 
 
236
        self._append_featured()
 
237
        self._append_recommendations()
 
238
 
 
239
        self._append_video_clips()
 
240
        self._append_top_of_the_pops()
 
241
        return
 
242
 
 
243
    def _append_top_of_the_pops(self):
 
244
        self.totp_hbox = Gtk.HBox(spacing=self.SPACING)
 
245
 
 
246
        alignment = Gtk.Alignment()
 
247
        alignment.set_padding(0, 0, self.PADDING, self.PADDING)
 
248
        alignment.add(self.totp_hbox)
 
249
 
 
250
        frame = FramedHeaderBox()
 
251
        frame.header_implements_more_button()
 
252
        frame.set_header_label(_("Most Popular"))
 
253
 
 
254
        label = Gtk.Label.new("Soda pop!!!")
 
255
        label.set_name("placeholder")
 
256
        label.set_size_request(-1, 200)
 
257
 
 
258
        frame.add(label)
 
259
        self.totp_hbox.add(frame)
 
260
 
 
261
        frame = FramedHeaderBox()
 
262
        frame.header_implements_more_button()
 
263
        frame.set_header_label(_("Top Rated"))
 
264
 
 
265
        label = Gtk.Label.new("Demos ftw(?)")
 
266
        label.set_name("placeholder")
 
267
        label.set_size_request(-1, 200)
 
268
 
 
269
        frame.add(label)
 
270
        self.totp_hbox.add(frame)
 
271
 
 
272
        self.vbox.pack_start(alignment, False, False, 0)
 
273
        return
 
274
 
 
275
    def _append_video_clips(self):
 
276
        frame = FramedHeaderBox()
 
277
        frame.set_header_expand(False)
 
278
        frame.set_header_position(HeaderPosition.LEFT)
 
279
        frame.set_header_label(_("Latest Demo Videos"))
 
280
 
 
281
        label = Gtk.Label.new("Videos go here")
 
282
        label.set_name("placeholder")
 
283
        label.set_size_request(-1, 200)
 
284
 
 
285
        frame.add(label)
 
286
 
 
287
        alignment = Gtk.Alignment()
 
288
        alignment.set_padding(0, 0, self.PADDING, self.PADDING)
 
289
        alignment.add(frame)
 
290
 
 
291
        self.vbox.pack_start(alignment, False, False, 0)
 
292
        return
 
293
 
 
294
    def _append_banner_ads(self):
 
295
        exhibit_banner = ExhibitBanner()
 
296
        #~ exhibit_banner.set_exhibits(exhibits_list)
 
297
        exhibit_banner.set_size_request(-1, 200)
 
298
        self.vbox.pack_start(exhibit_banner, False, False, 0)
160
299
        return
161
300
 
162
301
    def _append_departments(self):
163
 
#        # set the departments section to use the label markup we have just defined
164
 
        label = Gtk.Label()
165
 
        label.set_markup("<b><big>%s</big></b>" % self.header)
166
 
        label.set_alignment(0, 0.5)
167
 
        self.vbox.pack_start(label, False, False, 0)
168
 
 
169
 
        self.departments = FlowableGrid()
170
 
        self.departments.set_row_spacing(6)
171
 
        self.departments.set_column_spacing(6)
172
 
        self.vbox.pack_start(self.departments, True, True, 0)
 
302
        # set the departments section to use the label markup we have just defined
 
303
        cat_vbox = FramedBox(Gtk.Orientation.VERTICAL)
 
304
        self.top_hbox.pack_start(cat_vbox, False, False, 0)
173
305
 
174
306
        # sort Category.name's alphabetically
175
307
        sorted_cats = categories_sorted_by_name(self.categories)
176
 
        #layout = self.create_pango_layout('')
177
308
 
 
309
        mrkup = "<small>%s</small>"
178
310
        for cat in sorted_cats:
179
311
            if 'carousel-only' in cat.flags: continue
180
 
 
181
 
            tile = CategoryTile(cat.name, cat.iconname)
182
 
            tile.set_name("tile")
183
 
            tile.connect('clicked', self._on_category_clicked, cat)
184
 
            self.departments.add_child(tile)
 
312
            category_name = mrkup % GObject.markup_escape_text(cat.name)
 
313
            label = LabelTile(category_name, None)
 
314
            label.label.set_alignment(0.0, 0.5)
 
315
            label.label.set_use_markup(True)
 
316
            label.connect('clicked', self.on_category_clicked, cat)
 
317
            cat_vbox.pack_start(label, False, False, 0)
 
318
        return
 
319
 
 
320
    def _append_featured(self):
 
321
        #~ featured_cat = get_category_by_name(self.categories, 
 
322
                                            #~ u"What\u2019s New")  # unstranslated name
 
323
        featured_cat = get_category_by_name(self.categories, 
 
324
                                            u"Featured")  # unstranslated name
 
325
 
 
326
 
 
327
        enq = AppEnquire(self.cache, self.db)
 
328
        app_filter = AppViewFilter(self.db, self.cache)
 
329
        enq.set_query(featured_cat.query,
 
330
                      limit=24,
 
331
                      filter=app_filter,
 
332
                      nonapps_visible=NonAppVisibility.ALWAYS_VISIBLE,
 
333
                      nonblocking_load=False)
 
334
 
 
335
        self.featured = FlowableGrid()
 
336
        #~ self.featured.row_spacing = StockEms.SMALL
 
337
        frame = Frame()
 
338
        frame.set_corner_label(_("New"))
 
339
        frame.add(self.featured)
 
340
        self.right_column.pack_start(frame, True, True, 0)
 
341
 
 
342
        helper = AppPropertiesHelper(self.db, self.cache, self.icons)
 
343
        for doc in enq.get_documents()[15:21]:
 
344
            name = helper.get_appname(doc)
 
345
            icon_pb = helper.get_icon_at_size(doc, 48, 48)
 
346
            stats = helper.get_review_stats(doc)
 
347
            tile = FeaturedTile(name, icon_pb, stats)
 
348
            tile.connect('clicked', self.on_app_clicked,
 
349
                         helper.get_application(doc))
 
350
            self.featured.add_child(tile)
 
351
        return
 
352
 
 
353
    def _append_recommendations(self):
 
354
        featured_cat = get_category_by_name(self.categories, 
 
355
                                            u"Featured")  # unstranslated name
 
356
 
 
357
        enq = AppEnquire(self.cache, self.db)
 
358
        app_filter = AppViewFilter(self.db, self.cache)
 
359
        enq.set_query(featured_cat.query,
 
360
                      limit=6,
 
361
                      filter=app_filter,
 
362
                      nonapps_visible=NonAppVisibility.ALWAYS_VISIBLE,
 
363
                      nonblocking_load=False)
 
364
 
 
365
        self.featured = FlowableGrid()
 
366
        frame = FramedHeaderBox(Gtk.Orientation.VERTICAL)
 
367
        frame.add(self.featured)
 
368
        frame.set_header_label(_("Recommended For You"))
 
369
        frame.header_implements_more_button()
 
370
        self.right_column.pack_start(frame, True, True, 0)
 
371
 
 
372
        helper = AppPropertiesHelper(self.db, self.cache, self.icons)
 
373
        for doc in enq.get_documents():
 
374
            name = helper.get_appname(doc)
 
375
            icon_pb = helper.get_icon_at_size(doc, 48, 48)
 
376
            stats = helper.get_review_stats(doc)
 
377
            tile = FeaturedTile(name, icon_pb, stats)
 
378
            tile.connect('clicked', self.on_app_clicked,
 
379
                         helper.get_application(doc))
 
380
            self.featured.add_child(tile)
185
381
        return
186
382
 
187
383
    def build(self, desktopdir):
232
428
        if self.departments is None:
233
429
            self.subcat_label = Gtk.Label()
234
430
            self.subcat_label.set_alignment(0, 0.5)
235
 
            self.vbox.pack_start(self.subcat_label, False, False, 0)
236
431
 
237
 
            self.departments = FlowableGrid()
 
432
            self.departments = FlowableGrid(paint_grid_pattern=False)
238
433
            self.departments.set_row_spacing(6)
239
434
            self.departments.set_column_spacing(6)
240
435
 
 
436
            frame = FramedBox(spacing=StockEms.MEDIUM,
 
437
                              padding=StockEms.LARGE)
 
438
            # set x/y-alignment and x/y-expand
 
439
            frame.set(0.5, 0.0, 1.0, 1.0)
 
440
 
 
441
            frame.pack_start(self.subcat_label, False, False, 0)
 
442
            frame.pack_start(self.departments, True, True, 0)
 
443
 
241
444
            # append the departments section to the page
242
 
            self.vbox.pack_start(self.departments, True, True, 0)
 
445
            self.vbox.pack_start(frame, True, True, 0)
243
446
        else:
244
447
            self.departments.remove_all()
245
448
 
256
459
 
257
460
            if len(enquire.get_mset(0,1)):
258
461
                tile = CategoryTile(cat.name, cat.iconname)
259
 
                tile.set_name("tile")
260
 
                tile.connect('clicked', self._on_category_clicked, cat)
 
462
                tile.connect('clicked', self.on_category_clicked, cat)
261
463
                self.departments.add_child(tile)
262
464
 
263
465
        # partialy work around a (quite rare) corner case
274
476
        all_cat = Category("All", _("All"), "category-show-all", root_category.query)
275
477
        name = GObject.markup_escape_text('%s %s' % (_("All"), num_items))
276
478
        tile = CategoryTile(name, "category-show-all")
277
 
        tile.set_name("tile")
278
 
        tile.connect('clicked', self._on_category_clicked, all_cat)
 
479
        tile.connect('clicked', self.on_category_clicked, all_cat)
279
480
        self.departments.add_child(tile)
280
481
        return
281
482