~thomas-deruyter-3/qreator/qreator

« back to all changes in this revision

Viewing changes to qreator/QreatorWindow.py

  • Committer: Stefan Schwarzburg
  • Date: 2012-11-22 08:43:25 UTC
  • mfrom: (89.12.25 qreator)
  • mto: This revision was merged to the branch mainline in revision 101.
  • Revision ID: stefan.schwarzburg@googlemail.com-20121122084325-xubvujmrf0tz7d6r
merged with latest trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
2
2
### BEGIN LICENSE
3
3
# Copyright (C) 2012 David Planella <david.planella@ubuntu.com>
4
 
# This program is free software: you can redistribute it and/or modify it
5
 
# under the terms of the GNU General Public License version 3, as published
 
4
# This program is free software: you can redistribute it and/or modify it 
 
5
# under the terms of the GNU General Public License version 3, as published 
6
6
# by the Free Software Foundation.
7
 
#
8
 
# This program is distributed in the hope that it will be useful, but
9
 
# WITHOUT ANY WARRANTY; without even the implied warranties of
10
 
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 
7
 
8
# This program is distributed in the hope that it will be useful, but 
 
9
# WITHOUT ANY WARRANTY; without even the implied warranties of 
 
10
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR 
11
11
# PURPOSE.  See the GNU General Public License for more details.
12
 
#
13
 
# You should have received a copy of the GNU General Public License along
 
12
 
13
# You should have received a copy of the GNU General Public License along 
14
14
# with this program.  If not, see <http://www.gnu.org/licenses/>.
15
15
### END LICENSE
16
16
 
79
79
        # after having clicked once on an iconview icon
80
80
        self.iconview_activation_delay = 200
81
81
 
 
82
        # Initialize color editing
 
83
        self.current_color_bg = Gdk.Color(65535, 65535, 65535)  # White
 
84
        self.previous_color_bg = Gdk.Color(65535, 65535, 65535)
 
85
        self.current_color_fg = Gdk.Color(0, 0, 0)  # Black
 
86
        self.previous_color_fg = Gdk.Color(0, 0, 0)
 
87
 
82
88
        # Add an initial text, so that there is an initial QR code
83
89
        self.qr_code_placeholder = 'http://launchpad.net/qreator'
84
90
 
294
300
        pixbuf = self.get_pixbuf_from_drawing_area()
295
301
        self.clipboard.set_image(pixbuf)
296
302
 
 
303
    def on_toolbuttonEdit_clicked(self, widget, data=None):
 
304
        if widget.get_active():
 
305
            self.ui.toolbar2.show()
 
306
        else:
 
307
            self.ui.toolbar2.hide()
 
308
 
 
309
##########################################
 
310
 
 
311
    def set_selector_colors(self):
 
312
        '''Preloads the color selector with the current and previous
 
313
        QR code colors'''
 
314
        colorsel = self.ui.color_selection_dialog.get_color_selection()
 
315
        if self.ui.radiobuttonfg.get_active():
 
316
            colorsel.set_current_color(self.current_color_fg)
 
317
            colorsel.set_previous_color(self.previous_color_fg)
 
318
        elif self.ui.radiobuttonbg.get_active():
 
319
            colorsel.set_current_color(self.current_color_bg)
 
320
            colorsel.set_previous_color(self.previous_color_bg)
 
321
 
 
322
    def on_toolbuttonColorSelector_clicked(self, widget, data=None):
 
323
        '''Loads the color selector dialog and saves the chosen
 
324
        current and previous QR code colors'''
 
325
        colorsel = self.ui.color_selection_dialog.get_color_selection()
 
326
        
 
327
        self.set_selector_colors()
 
328
 
 
329
        colordiag = self.ui.color_selection_dialog
 
330
        response = colordiag.run()
 
331
        
 
332
        # It seems that when invoked from Glade, the color selector dialog
 
333
        # returns an integer instead of Gtk.ResponseType.OK, and we need to
 
334
        # set this integer explicitly in the .ui file. The 'OK' button
 
335
        # returns '1'.
 
336
        if response == 1:
 
337
            if self.ui.radiobuttonfg.get_active():
 
338
                self.current_color_fg = colorsel.get_current_color()
 
339
                self.previous_color_fg = colorsel.get_previous_color()
 
340
            elif self.ui.radiobuttonbg.get_active():
 
341
                self.current_color_bg = colorsel.get_current_color()
 
342
                self.previous_color_bg = colorsel.get_previous_color()                
 
343
 
 
344
        self.update_qr_code(self.qr_code_placeholder)
 
345
 
 
346
        self.ui.color_selection_dialog.hide()
 
347
 
 
348
    def on_radiobuttonfg_toggled(self, widget, data=None):
 
349
        '''Loads the foreground color in the selector when the foreground
 
350
        radio button is toggled'''
 
351
        self.set_selector_colors()
 
352
 
 
353
    def on_radiobuttonbg_toggled(self, widget, data=None):
 
354
        '''Loads the foreground color in the selector when the background
 
355
        radio button is toggled'''
 
356
        self.set_selector_colors()
 
357
 
 
358
    def on_toolbuttonColorSwap_clicked(self, widget, data=None):
 
359
        '''Swaps the QR code's foreground and background colors'''
 
360
 
 
361
        self.previous_color_bg = self.current_color_bg
 
362
        self.previous_color_fg = self.current_color_fg
 
363
        
 
364
        self.current_color_bg = self.current_color_fg
 
365
        self.current_color_fg = self.previous_color_bg
 
366
 
 
367
        self.update_qr_code(self.qr_code_placeholder)
 
368
 
 
369
    def on_toolbuttonColorReset_clicked(self, widget, data=None):
 
370
        '''Reset QR code to defaults. At this point only the colors 
 
371
        can be edited, so effectively it resets the foreground and
 
372
        background colors to black and white, respectively'''
 
373
        self.current_color_bg = Gdk.Color(65535, 65535, 65535)  # White
 
374
        self.previous_color_bg = Gdk.Color(65535, 65535, 65535)
 
375
        self.current_color_fg = Gdk.Color(0, 0, 0)  # Black
 
376
        self.previous_color_fg = Gdk.Color(0, 0, 0)
 
377
 
 
378
        self.update_qr_code(self.qr_code_placeholder)
 
379
 
297
380
##########################################
298
381
 
299
382
    def update_qr_code(self, text):
353
436
        if not text == '':
354
437
            ## Create the QR code
355
438
            qr_code = QRCode()  # output_size = int(min(width, height) * 0.9))
356
 
            self.surface = qr_code.encode(text, QRCodeOutput.CAIRO_SURFACE)
 
439
 
 
440
            # Note that we need to convert the 16-bit RGB values from
 
441
            # Gdk.Color to the 8-bit RGB values that PIL expects
 
442
            self.surface = qr_code.encode(text,
 
443
                                current_color_fg=(
 
444
                                    self.current_color_fg.red / 256,
 
445
                                    self.current_color_fg.green / 256,
 
446
                                    self.current_color_fg.blue / 256),
 
447
                                previous_color_fg=(
 
448
                                    self.previous_color_fg.red / 256,
 
449
                                    self.previous_color_fg.green / 256,
 
450
                                    self.previous_color_fg.blue / 256),
 
451
                                current_color_bg=(
 
452
                                    self.current_color_bg.red / 256,
 
453
                                    self.current_color_bg.green / 256,
 
454
                                    self.current_color_bg.blue / 256),
 
455
                                previous_color_bg=(
 
456
                                    self.previous_color_bg.red / 256,
 
457
                                    self.previous_color_bg.green / 256,
 
458
                                    self.previous_color_bg.blue / 256),
 
459
                                output_type=QRCodeOutput.CAIRO_SURFACE)
357
460
 
358
461
            # Center the image in the drawing area
359
462
            centered_width, centered_height = \