~dpm/qreator/snap

« back to all changes in this revision

Viewing changes to qreator/QreatorWindow.py

  • Committer: Stefan Schwarzburg
  • Date: 2012-11-23 14:30:18 UTC
  • mfrom: (89.10.32 qreator_printing)
  • Revision ID: stefan.schwarzburg@googlemail.com-20121123143018-j9n5vshtuh3mdpjz
merged basic printing support

Show diffs side-by-side

added added

removed removed

Lines of Context:
43
43
 
44
44
 
45
45
# See qreator_lib.Window.py for more details about how this class works
46
 
class QreatorWindow(Window):
 
46
class QreatorWindow(Window):   # pylint: disable=W0232
47
47
    __gtype_name__ = "QreatorWindow"
48
48
 
49
49
    def finish_initializing(self, builder):  # pylint: disable=E1002
53
53
        # Code for other initialization actions should be added here.
54
54
 
55
55
        # Initialize the clipboard
 
56
        # pylint: disable=W0201
56
57
        self.clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD)
57
58
 
58
59
        # Initialize the style for the main toolbar
68
69
        # Initialize about dialog
69
70
        self.init_about_dialog()
70
71
 
 
72
        # Create printing dialog
 
73
        self.reset_print_dialog()
 
74
 
71
75
        # Initialize the QR types icon view
72
76
        self.init_qr_types()
73
77
 
74
78
        # Load the background texture in the QR code page
75
79
        self.texture = cairo.ImageSurface.create_from_png(
76
 
                                                get_media_file("pattern.png"))
 
80
            get_media_file("pattern.png"))
77
81
 
78
82
        # Time to wait in milliseconds before switching to the next view
79
83
        # after having clicked once on an iconview icon
91
95
        # Prepare the qrcode for drag n drop
92
96
        self.tempfilepath = None
93
97
 
 
98
        # pylint: disable=W0613
94
99
        def on_drag_data_get(widget, drag_context, selection_data,
95
100
                             info, time):
96
101
            if self.tempfilepath is None:
 
102
                # pylint: disable=W0612
97
103
                handle, self.tempfilepath = tempfile.mkstemp(
98
104
                    suffix=".png", prefix="qreator_")
99
105
                pixbuf = self.get_pixbuf_from_drawing_area()
100
106
                pixbuf.savev(self.tempfilepath, 'png', [], [])
101
 
            selection_data.set_uris(["file://{}".format(self.tempfilepath),])
 
107
            selection_data.set_uris(["file://{}".format(self.tempfilepath), ])
102
108
            return True
103
109
 
104
110
        def remove_tempfile(path):
125
131
        # By importing the different qrcode dataformats in the __init__ file,
126
132
        # they have automatically registered themselves in the base class.
127
133
        self.qr_types = [d(self.update_qr_code) for d in QRCodeType.dataformats]
128
 
 
 
134
        # pylint: disable=W0201
129
135
        self.qr_types_store = Gtk.ListStore(str, GdkPixbuf.Pixbuf, int)
130
136
        # ^ desc, icon, id ^
131
137
        self.qr_types_store.set_sort_column_id(COL_DESC,
137
143
        self.curr_width = 0
138
144
 
139
145
        for qr_type in self.qr_types:
140
 
            qr_type.widget.grid.hide() # keep the window from growing with each qrtype
 
146
            # keep the window from growing with each qrtype
 
147
            qr_type.widget.grid.hide()
141
148
            self.ui.qr_input_box.add(qr_type.widget.grid)
142
149
            qr_type.widget.on_prepared()
143
150
 
161
168
        # If the size has changed...
162
169
        if(new_width != self.curr_width or new_height != self.curr_height):
163
170
            # Remember new size
164
 
            self.curr_width = new_width
165
 
            self.curr_height = new_height
 
171
            self.curr_width = new_width  # pylint: disable=W0201
 
172
            self.curr_height = new_height  # pylint: disable=W0201
166
173
            # and refill iconviews with icons to adjust columns number
167
174
            self.fill_qr_types_store()
168
175
 
191
198
        # Workaround for bug #1031657, remove David from translation credits
192
199
        # unless the locale is Catalan
193
200
        import locale
 
201
        # pylint: disable=W0612
194
202
        default_locale, encoding = locale.getdefaultlocale()
195
203
        # Gtk.AboutDialog does not recognize https and would not render the
196
204
        # markup otherwise
197
 
        credits = _("translator-credits").replace('https', 'http')
 
205
        translator_credits = _("translator-credits").replace('https', 'http')
198
206
        if default_locale.split('_')[0] != 'ca':
199
 
            credits = credits.replace('David Planella' + ' ' +
200
 
                'http://launchpad.net/~dpm', '').strip()
201
 
        about.set_translator_credits(credits)
 
207
            translator_credits = translator_credits.replace(
 
208
                'David Planella' + ' ' + 'http://launchpad.net/~dpm',
 
209
                '').strip()
 
210
        about.set_translator_credits(translator_credits)
202
211
 
203
212
        box = self.ui.about_box
204
213
        about.vbox.reparent(box)
209
218
                button.destroy()
210
219
 
211
220
##########################################
212
 
 
 
221
    # pylint: disable=W0613
213
222
    def on_toolbuttonNew_clicked(self, widget, data=None):
214
223
        '''Shows the home page'''
215
224
        self.ui.qr_types_view.unselect_all()
219
228
        '''Shows the history page'''
220
229
        pass  # self.ui.notebook1.set_current_page(PAGE_HISTORY)
221
230
 
 
231
    # pylint: disable=W0613
222
232
    def on_toolbuttonAbout_clicked(self, widget, data=None):
223
233
        '''Shows the about page'''
224
234
        self.ui.notebook1.set_current_page(PAGE_ABOUT)
263
273
                                          image_width, image_height)
264
274
 
265
275
##########################################
 
276
    def reset_print_dialog(self):
 
277
        # pylint: disable=W0201
 
278
        self.print_dialog = Gtk.PrintOperation()
 
279
        self.print_dialog.set_n_pages(1)
 
280
        # pylint: disable=W0201
 
281
        self.print_handler = self.builder.get_object(
 
282
            "toolbuttonPrint").connect(
 
283
            "clicked", self.on_toolbuttonPrint_clicked)
 
284
 
 
285
    # pylint: disable=W0613
 
286
    def on_toolbuttonPrint_clicked(self, widget, data=None):
 
287
        self.builder.get_object("toolbuttonPrint").disconnect(
 
288
            self.print_handler)
 
289
        self.print_dialog.connect(
 
290
            "draw_page", self.draw_page)
 
291
        result = self.print_dialog.run(
 
292
            Gtk.PrintOperationAction.PRINT_DIALOG, None)
 
293
        self.reset_print_dialog()
 
294
 
 
295
    # pylint: disable=W0613
 
296
    def draw_page(self, operation=None, context=None, page_nr=None):
 
297
        ctx = context.get_cairo_context()
 
298
        ctx.set_source_surface(self.surface)
 
299
        # Render the image
 
300
        ctx.paint()
 
301
        return
266
302
 
267
303
    def on_toolbuttonSave_clicked(self, widget, data=None):
268
304
        if not self.surface:
357
393
        self.set_selector_colors()
358
394
 
359
395
    def on_toolbuttonColorSwap_clicked(self, widget, data=None):
360
 
        '''Swaps the QR code's foreground and background colors'''
 
396
        """Swaps the QR code's foreground and background colors"""
361
397
 
362
398
        self.previous_color_bg = self.current_color_bg
363
399
        self.previous_color_fg = self.current_color_fg
371
407
        '''Reset QR code to defaults. At this point only the colors
372
408
        can be edited, so effectively it resets the foreground and
373
409
        background colors to black and white, respectively'''
 
410
        # pylint: disable=W0201
374
411
        self.current_color_bg = Gdk.Color(65535, 65535, 65535)  # White
375
412
        self.previous_color_bg = Gdk.Color(65535, 65535, 65535)
376
413
        self.current_color_fg = Gdk.Color(0, 0, 0)  # Black
381
418
##########################################
382
419
 
383
420
    def update_qr_code(self, text):
 
421
        # pylint: disable=W0201
384
422
        self.qr_code_placeholder = text
385
423
        self.ui.qr_drawingarea.queue_draw()
386
424
 
440
478
 
441
479
            # Note that we need to convert the 16-bit RGB values from
442
480
            # Gdk.Color to the 8-bit RGB values that PIL expects
 
481
            # pylint: disable=W0201
443
482
            self.surface = qr_code.encode(text,
444
483
                                current_color_fg=(
445
484
                                    self.current_color_fg.red / 256,