~thomas-deruyter-3/qreator/qreator

« back to all changes in this revision

Viewing changes to qreator/QreatorWindow.py

  • Committer: David Planella
  • Date: 2012-05-17 08:12:32 UTC
  • Revision ID: david.planella@ubuntu.com-20120517081232-uq92gvxfji2v68gi
First working version ready for release

Show diffs side-by-side

added added

removed removed

Lines of Context:
7
7
from gettext import gettext as _
8
8
gettext.textdomain('qreator')
9
9
 
10
 
from gi.repository import Gtk, Gdk # pylint: disable=E0611
 
10
from gi.repository import Gtk, Gdk  # pylint: disable=E0611
11
11
from gi.repository import GtkChamplain, Clutter, Champlain
12
12
import logging
13
13
logger = logging.getLogger('qreator')
14
14
 
15
15
from qreator_lib import Window
16
 
from qreator.AboutQreatorDialog import AboutQreatorDialog
17
 
from qreator.PreferencesQreatorDialog import PreferencesQreatorDialog
18
16
 
19
17
import Geoclue
20
18
from QRCode import QRCode as QRCode
21
19
from QRCode import QRCodeOutput as QRCodeOutput
22
20
 
23
 
COL_TEXT = 0
 
21
COL_DESC = 0
24
22
COL_PIXBUF = 1
25
23
COL_ID = 2
 
24
COL_CALLBACK = 3
26
25
 
27
 
PAGE_HOME = 0
28
 
PAGE_HISTORY = 1
29
 
PAGE_SETTINGS = 2
30
 
PAGE_ABOUT = 3
31
 
PAGE_URL = 4
32
 
PAGE_GEO = 5
 
26
PAGE_NEW = 0
 
27
#PAGE_HISTORY = 1
 
28
#PAGE_SETTINGS = 2
 
29
PAGE_ABOUT = 1
 
30
PAGE_QR = 2
33
31
 
34
32
POS_PROVIDER = 'Ubuntu GeoIP'
35
33
 
36
34
 
37
 
class BarcodeSize(object):
38
 
    SMALL = 120
39
 
    MEDIUM = 230
40
 
    LARGE = 350
41
 
 
42
 
 
43
35
# See qreator_lib.Window.py for more details about how this class works
44
36
class QreatorWindow(Window):
45
37
    __gtype_name__ = "QreatorWindow"
46
38
 
47
 
    def finish_initializing(self, builder): # pylint: disable=E1002
 
39
    def finish_initializing(self, builder):  # pylint: disable=E1002
48
40
        """Set up the main window"""
49
41
        super(QreatorWindow, self).finish_initializing(builder)
50
42
 
51
 
        self.AboutDialog = AboutQreatorDialog
52
 
        self.PreferencesDialog = PreferencesQreatorDialog
53
 
 
54
43
        # Code for other initialization actions should be added here.
55
44
 
56
45
        # Initialize the clipboard
64
53
        self.surface = None
65
54
 
66
55
        # Initialize the icon view widget
67
 
        self.ui.iconview1.set_text_column(COL_TEXT)
 
56
        self.ui.iconview1.set_text_column(COL_DESC)
68
57
        self.ui.iconview1.set_pixbuf_column(COL_PIXBUF)
69
58
 
70
59
        # Hide the notebook tabs
71
60
        self.ui.notebook1.set_show_tabs(False)
72
61
 
73
 
        self.qr_code_placeholder = 'aa'
 
62
        # Initialize combo boxes
 
63
        self.ui.comboboxtextSecurity.set_active(0)
 
64
        self.ui.comboboxtextProtocol.set_active(0)
 
65
 
 
66
        self.qr_code_placeholder = 'qreator'
 
67
 
 
68
    def on_entryText_changed(self, widget, data=None):
 
69
        self.update_qr_code(widget.get_text(widget.get_start_iter(),
 
70
                                            widget.get_end_iter(),
 
71
                                            False))
74
72
 
75
73
    def on_map_widget_button_press_event(self, actor, event, view):
76
74
        x, y = event.get_coords()
77
75
        lat, lon = view.x_to_longitude(x), view.y_to_latitude(y)
78
 
        print "Mouse click at: %f %f" % (lon, lat)
 
76
 
 
77
        self.ui.lat_entry.set_text(str(lat))
 
78
        self.ui.lon_entry.set_text(str(lon))
 
79
 
 
80
        self.update_qr_code('geo:{0},{1}'.format(str(lon), str(lat)))
 
81
 
79
82
        return True
80
83
 
81
 
    def on_toolbuttonHome_clicked(self, widget, data=None):
 
84
    def on_toolbuttonNew_clicked(self, widget, data=None):
82
85
        '''Shows the home page'''
83
 
        self.ui.notebook1.set_current_page(PAGE_HOME)
 
86
        self.ui.notebook1.set_current_page(PAGE_NEW)
84
87
 
85
88
    def on_toolbuttonHistory_clicked(self, widget, data=None):
86
89
        '''Shows the history page'''
87
 
        self.ui.notebook1.set_current_page(PAGE_HISTORY)
 
90
        pass  # self.ui.notebook1.set_current_page(PAGE_HISTORY)
88
91
 
89
92
    def on_toolbuttonSettings_clicked(self, widget, data=None):
90
93
        '''Shows the settings page'''
91
 
        self.ui.notebook1.set_current_page(PAGE_SETTINGS)
 
94
        pass  # self.ui.notebook1.set_current_page(PAGE_SETTINGS)
92
95
 
93
96
    def on_toolbuttonAbout_clicked(self, widget, data=None):
94
97
        '''Shows the about page'''
95
98
        self.ui.notebook1.set_current_page(PAGE_ABOUT)
96
99
 
 
100
    def _hide_children(self, widget):
 
101
        for child in widget.get_children():
 
102
            child.hide()
 
103
 
 
104
    def on_qr_url_clicked(self):
 
105
        self._hide_children(self.ui.qr_input)
 
106
        self.ui.qr_url.show_all()
 
107
 
 
108
    def on_qr_wifi_clicked(self):
 
109
        self._hide_children(self.ui.qr_input)
 
110
        self.ui.qr_wifi.show_all()
 
111
 
 
112
    def on_qr_text_clicked(self):
 
113
        # Initialize multi-line text entry
 
114
        textbuffer = self.ui.entryText.get_buffer()
 
115
        textbuffer.connect("changed", self.on_entryText_changed, None)
 
116
 
 
117
        self._hide_children(self.ui.qr_input)
 
118
        self.ui.qr_text.show_all()
 
119
 
 
120
    def on_qr_location_clicked(self):
 
121
        # We're now loading the UI for the location QR type
 
122
        self._hide_children(self.ui.qr_input)
 
123
        self.ui.qr_location.show_all()
 
124
 
 
125
        map_widget = GtkChamplain.Embed()
 
126
        map_widget.set_hexpand(True)
 
127
        map_widget.set_vexpand(True)
 
128
 
 
129
        map_grid = self.ui.qr_location
 
130
 
 
131
        map_grid.set_size_request(-1, 250)
 
132
        map_grid.attach(map_widget, 0, 0, 1, 1)
 
133
 
 
134
        self.map_view = map_widget.get_view()
 
135
        self.map_view.set_reactive(True)
 
136
        map_widget.connect('button-release-event',
 
137
                           self.on_map_widget_button_press_event,
 
138
                           self.map_view)
 
139
 
 
140
        # Get the current location, center the map on it, and initialize
 
141
        # other map features
 
142
        latitude, longitude = get_current_location()
 
143
        self.map_view.center_on(latitude, longitude)
 
144
        if latitude == 0 and longitude == 0:
 
145
            # In case something went wrong in getting the current location
 
146
            self.map_view.set_zoom_level(1)
 
147
        else:
 
148
            self.map_view.set_zoom_level(15)
 
149
        self.map_view.set_kinetic_mode(True)
 
150
 
 
151
        scale = Champlain.Scale()
 
152
        scale.connect_view(self.map_view)
 
153
        self.map_view.bin_layout_add(scale, Clutter.BinAlignment.START,
 
154
                                     Clutter.BinAlignment.END)
 
155
 
 
156
        self.ui.qr_location.show_all()
 
157
 
97
158
    def on_iconview1_item_activated(self, widget, item):
98
159
        '''Loads the UI for the appropriate QR type'''
99
160
 
100
161
        model = widget.get_model()
101
 
        qr_type = model[item][COL_ID]
102
 
 
103
 
        # We're loading each UI type inside this grid
104
 
        grid = self.ui.grid1
105
 
 
106
 
        # FIXME: the way of choosing the page is a bit of a hack for now
107
 
        if qr_type + PAGE_URL == PAGE_URL:
108
 
            # We're now loading the UI for the URL QR type
109
 
            entry1 = Gtk.Entry()
110
 
            entry1.connect('changed', self.on_entry1_changed)
111
 
            entry1.set_hexpand(True)
112
 
 
113
 
            # Remove the current widgets from the top half of the UI,
114
 
            # replace them with the current QR type's UI
115
 
            children = grid.get_children()
116
 
            grid.remove(children[0])
117
 
            grid.attach(entry1, 0, 0, 1, 1)
118
 
 
119
 
        elif qr_type + PAGE_URL == PAGE_GEO:
120
 
            # We're now loading the UI for the map QR type
121
 
            map_widget = GtkChamplain.Embed()
122
 
            map_widget.set_hexpand(True)
123
 
            map_widget.set_vexpand(True)
124
 
 
125
 
            map_grid = Gtk.Grid()
126
 
            map_grid.attach(map_widget, 0, 0, 2, 1)
127
 
 
128
 
            lat_entry = Gtk.Entry()
129
 
            lat_entry.set_hexpand(True)
130
 
            lat_label = Gtk.Label(_('Latitude:'))
131
 
            lat_label.set_alignment(xalign=0, yalign=0.5)
132
 
            lon_entry = Gtk.Entry()
133
 
            lon_label = Gtk.Label(_('Longitude:'))
134
 
            lon_label.set_alignment(xalign=0, yalign=0.5)
135
 
 
136
 
            map_grid.attach_next_to(lat_label, map_widget,
137
 
                                    Gtk.PositionType.BOTTOM, 1, 1)
138
 
            map_grid.attach_next_to(lon_label, lat_label,
139
 
                                    Gtk.PositionType.BOTTOM, 1, 1)
140
 
 
141
 
            map_grid.attach_next_to(lat_entry, lat_label,
142
 
                                    Gtk.PositionType.RIGHT, 1, 1)
143
 
            map_grid.attach_next_to(lon_entry, lon_label,
144
 
                                    Gtk.PositionType.RIGHT, 1, 1)
145
 
 
146
 
            # Remove the current widgets from the top half of the UI,
147
 
            # replace them with the current QR type's UI
148
 
            children = grid.get_children()
149
 
            grid.remove(children[0])
150
 
            self.ui.grid1.attach(map_grid, 0, 0, 1, 1)
151
 
 
152
 
            self.map_view = map_widget.get_view()
153
 
            self.map_view.set_reactive(True)
154
 
            map_widget.connect('button-release-event',
155
 
                               self.on_map_widget_button_press_event,
156
 
                               self.map_view)
157
 
 
158
 
            # Get the current location, center the map on it, and initialize
159
 
            # other map features
160
 
            latitude, longitude = get_current_location()
161
 
            self.map_view.center_on(latitude, longitude)
162
 
            self.map_view.set_zoom_level(15)
163
 
            self.map_view.set_kinetic_mode(True)
164
 
 
165
 
            scale = Champlain.Scale()
166
 
            scale.connect_view(self.map_view)
167
 
            self.map_view.bin_layout_add(scale, Clutter.BinAlignment.START,
168
 
                                         Clutter.BinAlignment.END)
169
 
 
170
 
        grid.show_all()
171
 
        self.ui.notebook1.set_current_page(PAGE_URL)
 
162
        qr_callback = getattr(QreatorWindow, model[item][COL_CALLBACK])
 
163
 
 
164
        qr_callback(self)
 
165
 
 
166
        self.ui.notebook1.set_current_page(PAGE_QR)
172
167
 
173
168
    def get_pixbuf_from_drawing_area(self):
174
 
        window = self.ui.drawingarea1.get_window()
 
169
        window = self.ui.qr_drawingarea.get_window()
175
170
 
176
 
        src_x, src_y = self.get_centered_coordinates(self.ui.drawingarea1,
 
171
        src_x, src_y = self.get_centered_coordinates(self.ui.qr_drawingarea,
177
172
                                                     self.surface)
178
173
        image_height = self.surface.get_height()
179
174
        image_width = self.surface.get_width()
218
213
        pixbuf = self.get_pixbuf_from_drawing_area()
219
214
        self.clipboard.set_image(pixbuf)
220
215
 
221
 
    def on_entry1_changed(self, widget, data=None):
222
 
        self.qr_code_placeholder = widget.get_text()
223
 
        self.ui.drawingarea1.queue_draw()
 
216
    def update_qr_code(self, text):
 
217
        self.qr_code_placeholder = text
 
218
        self.ui.qr_drawingarea.queue_draw()
 
219
 
 
220
    def on_entryURL_changed(self, widget, data=None):
 
221
        self.update_url_qr_code(www=widget.get_text())
 
222
 
 
223
    def on_comboboxtextProtocol_changed(self, widget, data=None):
 
224
        self.update_url_qr_code(protocol=widget.get_active_text())
 
225
 
 
226
    def update_url_qr_code(self, protocol=None, www=None):
 
227
        if not protocol:
 
228
            protocol = self.ui.comboboxtextProtocol.get_active_text()
 
229
        if not www:
 
230
            www = self.ui.entryURL.get_text()
 
231
 
 
232
        if not protocol or www == '':
 
233
            return
 
234
 
 
235
        self.update_qr_code(protocol + www)
 
236
 
 
237
    def update_wifi_qr_code(self, security=None, ssid=None, password=None):
 
238
        if not security:
 
239
            security = self.ui.comboboxtextSecurity.get_active_text()
 
240
        if not ssid:
 
241
            ssid = self.ui.entrySSID.get_text()
 
242
        if not password:
 
243
            password = self.ui.entryPassword.get_text()
 
244
 
 
245
        if not security or ssid == '' or password == '':
 
246
            return
 
247
 
 
248
        wifi_qr_code = 'WIFI:T:{0};S:{1};P:{2};;'.format(security, ssid,
 
249
                                                         password)
 
250
        self.update_qr_code(wifi_qr_code)
 
251
 
 
252
    def on_comboboxtextSecurity_changed(self, widget, data=None):
 
253
        self.update_wifi_qr_code(security=widget.get_active_text())
 
254
 
 
255
    def on_entrySSID_changed(self, widget, data=None):
 
256
        self.update_wifi_qr_code(ssid=widget.get_text())
 
257
 
 
258
    def on_entryPassword_changed(self, widget, data=None):
 
259
        self.update_wifi_qr_code(password=widget.get_text())
 
260
 
 
261
    def on_checkbutton1_clicked(self, widget):
 
262
        if widget.get_active():
 
263
            self.ui.entryPassword.set_visibility(True)
 
264
        else:
 
265
            self.ui.entryPassword.set_visibility(False)
224
266
 
225
267
    def get_centered_coordinates(self, drawing_area, surface):
226
268
 
232
274
        return (drawing_area_width / 2 - image_width / 2,
233
275
                drawing_area_height / 2 - image_height / 2)
234
276
 
235
 
    def on_drawingarea1_draw(self, widget, ctx, data=None):
236
 
        #text = self.ui.entry1.get_text()
 
277
    def on_qr_drawingarea_draw(self, widget, ctx, data=None):
237
278
        text = self.qr_code_placeholder
238
279
        if text == '':
239
280
            return