~dpm/qreator/snap

« back to all changes in this revision

Viewing changes to qreator/QreatorWindow.py

  • Committer: David Planella
  • Date: 2012-05-29 21:08:53 UTC
  • Revision ID: david.planella@ubuntu.com-20120529210853-oe5ogs9gf19ut77i
Added icons for the 'clear' action to all text entries, used symbolic icons in the QR page, delayed loading Software Centre apps until the apps tab is loaded for the first time

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
import cairo
18
18
import math
19
 
from gi.repository import Gtk, Gdk, GdkPixbuf  # pylint: disable=E0611
 
19
from gi.repository import Gtk, Gdk, GdkPixbuf, GLib  # pylint: disable=E0611
20
20
from gi.repository import GtkChamplain, Clutter, Champlain
21
21
from gi.repository import NetworkManager, NMClient
22
22
import logging
75
75
        # Initialize placeholder text (we need to do that because due to
76
76
        # a Glade bug they are otherwise not marked as translatable)
77
77
        self.ui.entryURL.set_placeholder_text(_('[URL]'))
 
78
        # TRANSLATORS: 'expand' refers to expanding the drop-down menu
 
79
        # that shows autodetected wireless network identifiers
78
80
        self.ui.entrySSID.set_placeholder_text(
79
81
            _('[Network identifier - expand for autodetection]'))
80
82
        self.ui.entryPassword.set_placeholder_text(_('[Network password]'))
114
116
        self.usc_apps_complete.set_hexpand(True)
115
117
        self.usc_apps_complete.connect("changed",
116
118
                                    self.on_usc_apps_complete_changed, None)
 
119
        self.usc_apps_complete.connect("icon-press",
 
120
                                    self.on_usc_apps_complete_icon_pressed)
 
121
        self.usc_apps_complete.set_icon_from_icon_name(
 
122
            Gtk.EntryIconPosition.PRIMARY, 'edit-find-symbolic')
 
123
        self.usc_apps_complete.set_icon_from_stock(
 
124
            Gtk.EntryIconPosition.SECONDARY, None)
117
125
 
118
126
        #self.get_softwarecenterapps()
119
127
        #self.on_get_softwarecenterapps()
120
 
        deferred = defer.defer(self.get_softwarecenterapps)
121
 
        deferred.add_callback(self.on_get_softwarecenterapps)
 
128
        #deferred = defer.defer(self.get_softwarecenterapps)
 
129
        #deferred.add_callback(self.on_get_softwarecenterapps)
122
130
 
123
131
        self.ui.qr_app.attach(self.usc_apps_complete, 1, 0, 1, 1)
 
132
        self.softwarecenter_apps_init = False
124
133
 
125
134
        # Load the background texture
126
135
        self.texture = cairo.ImageSurface.create_from_png(
166
175
        # Add an initial text, so that there is an initial QR code
167
176
        self.qr_code_placeholder = 'http://launchpad.net/qreator'
168
177
 
 
178
    def on_usc_apps_complete_icon_pressed(self, widget, icon, mouse_button):
 
179
        self._clear_text_entry(widget, icon)
 
180
 
 
181
    def _clear_text_entry(self, widget, icon):
 
182
        if icon == Gtk.EntryIconPosition.SECONDARY:
 
183
            widget.set_text("")
 
184
            self._check_style(widget)
 
185
 
 
186
        elif icon == Gtk.EntryIconPosition.PRIMARY:
 
187
            widget.select_region(0, -1)
 
188
            widget.grab_focus()
 
189
 
 
190
    def _check_style(self, widget):
 
191
        """
 
192
        Show the clear icon whenever the field is not empty
 
193
        """
 
194
        if widget.get_text() != "":
 
195
            widget.set_icon_from_stock(Gtk.EntryIconPosition.SECONDARY,
 
196
                Gtk.STOCK_CLEAR)
 
197
            # reverse the icon if we are in an rtl environment
 
198
            if widget.get_direction() == Gtk.TextDirection.RTL:
 
199
                pb = widget.get_icon_pixbuf(
 
200
                    Gtk.EntryIconPosition.SECONDARY).flip(True)
 
201
                widget.set_icon_from_pixbuf(Gtk.EntryIconPosition.SECONDARY,
 
202
                    pb)
 
203
        else:
 
204
            widget.set_icon_from_stock(Gtk.EntryIconPosition.SECONDARY, None)
 
205
 
 
206
    def init_softwarecenter_apps(self):
 
207
        deferred = defer.defer(self.get_softwarecenterapps)
 
208
        deferred.add_callback(self.on_get_softwarecenterapps)
 
209
        self.softwarecenter_apps_init = True
 
210
 
169
211
    def fill_qr_types_store(self):
170
212
        self.qr_types_store.clear()
171
213
 
242
284
        # We're now loading the UI for the Software Center app QR type
243
285
        self._hide_children(self.ui.qr_input)
244
286
        self.ui.qr_app.show_all()
 
287
        if not self.softwarecenter_apps_init:
 
288
            GLib.idle_add(self.init_softwarecenter_apps)
245
289
 
246
290
    def on_qr_text_clicked(self):
247
291
        # Initialize multi-line text entry
352
396
        self.ui.qr_drawingarea.queue_draw()
353
397
 
354
398
    def on_usc_apps_complete_changed(self, widget, data=None):
 
399
        self._check_style(widget)
355
400
        USC_APPS_BASE_URL = 'https://apps.ubuntu.com/cat/applications/'
356
401
        self.update_qr_code(USC_APPS_BASE_URL + widget.get_text())
357
402
 
 
403
    def on_entryURL_icon_press(self, widget, icon, mouse_button):
 
404
        self._clear_text_entry(widget, icon)
 
405
 
358
406
    def on_entryURL_changed(self, widget, data=None):
 
407
        self._check_style(widget)
359
408
        self.update_url_qr_code(www=widget.get_text())
360
409
 
361
410
    def on_comboboxtextProtocol_changed(self, widget, data=None):
390
439
    def on_comboboxtextSecurity_changed(self, widget, data=None):
391
440
        self.update_wifi_qr_code(security=widget.get_active_text())
392
441
 
393
 
    def on_entrySSID_changed(self, widget, data=None):
394
 
        self.update_wifi_qr_code(ssid=widget.get_text())
 
442
    def on_entrySSID_icon_press(self, widget, icon, mouse_button):
 
443
        self._clear_text_entry(widget, icon)
395
444
 
396
445
    def on_comboboxtextSSID_changed(self, widget, data=None):
 
446
        self._check_style(self.ui.entrySSID)
397
447
        self.update_wifi_qr_code(ssid=widget.get_active_text())
398
448
 
 
449
    def on_entryPassword_icon_press(self, widget, icon, mouse_button):
 
450
        self._clear_text_entry(widget, icon)
 
451
 
399
452
    def on_entryPassword_changed(self, widget, data=None):
 
453
        self._check_style(widget)
400
454
        self.update_wifi_qr_code(password=widget.get_text())
401
455
 
402
456
    def on_checkbutton1_clicked(self, widget):
417
471
 
418
472
    def on_qr_drawingarea_draw(self, widget, ctx, data=None):
419
473
        text = self.qr_code_placeholder
420
 
        if text == '':
421
 
            return
422
474
 
423
475
        ## Fill the background
424
476
 
457
509
        ctx.set_source(pattern)
458
510
        ctx.paint()
459
511
 
460
 
        ## Create the QR code
461
 
        qr_code = QRCode()
462
 
        self.surface = qr_code.encode(text, QRCodeOutput.CAIRO_SURFACE)
463
 
 
464
 
        # Center the image in the drawing area
465
 
        centered_width, centered_height = \
466
 
            self.get_centered_coordinates(widget, self.surface)
467
 
        ctx.translate(centered_width, centered_height)
468
 
 
469
 
        # Set the surface as the context's source
470
 
        ctx.set_source_surface(self.surface)
471
 
 
472
 
        # Render the image
473
 
        ctx.paint()
 
512
        if not text == '':
 
513
            ## Create the QR code
 
514
            qr_code = QRCode()
 
515
            self.surface = qr_code.encode(text, QRCodeOutput.CAIRO_SURFACE)
 
516
 
 
517
            # Center the image in the drawing area
 
518
            centered_width, centered_height = \
 
519
                self.get_centered_coordinates(widget, self.surface)
 
520
            ctx.translate(centered_width, centered_height)
 
521
 
 
522
            # Set the surface as the context's source
 
523
            ctx.set_source_surface(self.surface)
 
524
 
 
525
            # Render the image
 
526
            ctx.paint()
474
527
 
475
528
    def get_softwarecenterapps(self):
476
529
        print 'Task STARTED!'
559
612
        if apps:
560
613
            self.add(apps)
561
614
        else:
 
615
            # TRANSLATORS: this message appears for a few seconds on the
 
616
            # Software Center apps text entry until the app names to
 
617
            # suggest for autocompletion have been read from the Software
 
618
            # Center database
562
619
            self.set_placeholder_text(
563
620
                _('[Wait a few seconds to enable autocompletion...]'))
564
621
 
569
626
                _('[Type the name of an app]'))
570
627
 
571
628
    def _remember(self, app):
572
 
        '''add a value to the list of strings to suggest'''
 
629
        '''Add a value to the list of strings to suggest'''
573
630
        try:
574
631
            icon = self.icons.load_icon(
575
632
                "{0}".format(app.icon),