~thomas-deruyter-3/qreator/qreator

« back to all changes in this revision

Viewing changes to qreator/QreatorWindow.py

  • Committer: David Planella
  • Date: 2012-01-18 19:35:20 UTC
  • Revision ID: david.planella@ubuntu.com-20120118193520-ciu0erfgqbxlaaxl
Added a file chooser to save the resulting image. Started playing with fade ins.

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
### END LICENSE
5
5
 
6
6
import gettext
7
 
#from gettext import gettext as _
 
7
from gettext import gettext as _
8
8
gettext.textdomain('qreator')
9
9
 
10
 
from gi.repository import Gtk, Gdk # pylint: disable=E0611
 
10
import gi
 
11
gi.require_version('Gdk', '3.0')
 
12
gi.require_version('Gtk', '3.0')
 
13
from gi.repository import Gtk, Gdk, GLib # pylint: disable=E0611
11
14
import cairo
12
15
import logging
13
16
logger = logging.getLogger('qreator')
51
54
 
52
55
        self.ui.notebook1.set_show_tabs(False)
53
56
        self.ui.entry1.connect('changed', self.on_entry1_changed)
 
57
        
 
58
        ## alpha is starting transparency
 
59
        self.alpha = 0
 
60
        ## delta is amount to increase alpha
 
61
        self.delta = 0.01
54
62
 
55
63
    def on_toolbuttonHome_clicked(self, widget, data=None):
56
64
        self.ui.notebook1.set_current_page(PAGE_HOME)
84
92
        if not self.surface:
85
93
            return
86
94
 
87
 
        # We cannot write directly from the surface, as the
88
 
        # Surface.write_to_png() method writes the image in the original
89
 
        # size returned by qrencode (i.e. non-scaled), and the SurfacePattern
90
 
        # does not have any methods to write to disk.
91
 
        # So we read the contents from the Gtk.DrawingArea, put them into a
92
 
        # Gdk.Pixbuf and use its 'savev' method to write to disk.
93
 
 
94
 
        pixbuf = self.get_pixbuf_from_drawing_area()
95
 
 
96
 
        pixbuf.savev(r'/tmp/somefile.png', 'png', [], [])
 
95
        dialog = Gtk.FileChooserDialog(_("Please choose a file"), self,
 
96
            Gtk.FileChooserAction.SAVE,
 
97
            (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
 
98
             Gtk.STOCK_SAVE, Gtk.ResponseType.OK))
 
99
 
 
100
        filter_png = Gtk.FileFilter()
 
101
        filter_png.set_name(_("PNG images"))
 
102
        filter_png.add_mime_type("image/png")
 
103
        dialog.add_filter(filter_png)
 
104
 
 
105
        response = dialog.run()
 
106
 
 
107
        if response == Gtk.ResponseType.OK:
 
108
            # We cannot write directly from the surface, as the
 
109
            # Surface.write_to_png() method writes the image in the original
 
110
            # size returned by qrencode (i.e. non-scaled), and the
 
111
            # SurfacePattern does not have any methods to write to disk.
 
112
            # So we read the contents from the Gtk.DrawingArea, put them into
 
113
            # a Gdk.Pixbuf and use its 'savev' method to write to disk.
 
114
 
 
115
            pixbuf = self.get_pixbuf_from_drawing_area()
 
116
 
 
117
            pixbuf.savev(dialog.get_filename(), 'png', [], [])
 
118
 
 
119
        dialog.destroy()
97
120
 
98
121
    def on_toolbuttonCopy_clicked(self, widget, data=None):
99
122
        if not self.surface:
115
138
        return (drawing_area_width / 2 - image_width / 2,
116
139
                drawing_area_height / 2 - image_height / 2)
117
140
 
 
141
    def fadeImage(self):
 
142
        self.ui.drawingarea1.queue_draw()
 
143
 
118
144
    def on_drawingarea1_draw(self, widget, ctx, data=None):
119
145
        text = self.ui.entry1.get_text()
120
146
        if text == '':
121
147
            return
122
148
 
123
149
        version, size, im = qrencode.encode(text)
 
150
 
 
151
        # --------------- Cairo surface conversion ---------------------------
 
152
        # See http://cairographics.org/pythoncairopil/ and
 
153
        # http://mail.python.org/pipermail/image-sig/2005-October/003604.html
 
154
        # http://mail.gnome.org/archives/gtkmm-list/2007-May/msg00111.html
 
155
 
124
156
        im = im.convert('RGBA')
125
 
 
126
 
        # --------------- Cairo surface conversion ---------------------------
127
 
        # See http://cairographics.org/pythoncairopil/
128
 
        # and http://mail.python.org/pipermail/image-sig/2005-October/003604.html
129
 
 
130
157
        bytearr = array.array('B', im.tostring())
131
158
        height, width = im.size
132
159
 
145
172
        # Set resampling filter
146
173
        imgpat.set_filter(cairo.FILTER_NEAREST)
147
174
 
 
175
        # Center the image
148
176
        centered_width, centered_height = \
149
177
            self.get_centered_coordinates(widget, self.surface)
150
178
        ctx.translate(centered_width, centered_height)
151
179
        ctx.set_source(imgpat)
152
180
 
153
181
        # Render the image
154
 
        ctx.paint()
 
182
        #ctx.paint()
 
183
        ctx.paint_with_alpha(self.alpha)
 
184
        self.alpha += self.delta
 
185
 
 
186
        if self.alpha >= 1:
 
187
            self.alpha = 0
 
188
            return False
 
189
        else:
 
190
            GLib.timeout_add(50, self.fadeImage)