Subversion Repositories basico

Rev

Rev 240 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
4 t00mlabs 1
#!/usr/bin/python
2
# -*- coding: utf-8 -*-
3
# File: uifuncs.py
4
# Author: Tomás Vírseda
5
# License: GPL v3
6
# Description: Generic UI functions service
7
 
8
 
9
from gi.repository import Gtk
10
from gi.repository import Gio
11
from gi.repository import Pango
12
from gi.repository.GdkPixbuf import Pixbuf
13
 
233 t00m 14
from basico.core.service import Service
4 t00mlabs 15
 
16
class UIFuncs(Service):
17
    def initialize(self):
223 t00m 18
        self.get_services()
19
 
20
 
21
    def get_services(self):
4 t00mlabs 22
        self.gui = self.app.get_service('GUI')
23
        self.cb = self.app.get_service('Callbacks')
24
        self.im = self.app.get_service('IM')
223 t00m 25
        self.db = self.app.get_service('DB')
4 t00mlabs 26
 
27
 
74 t00mlabs 28
    def get_gtk_version(self):
29
        return Gtk.get_major_version(), Gtk.get_minor_version(), Gtk.get_micro_version()
30
 
31
 
32
    def check_gtk_version(self):
33
        vmajor, vminor, vmicro =  self.get_gtk_version()
133 t00mlabs 34
        self.debug("GTK+ Version: %d.%d.%d" % (vmajor, vminor, vmicro))
74 t00mlabs 35
 
36
        if vmajor == 3 and vminor >= 18:
133 t00mlabs 37
            self.debug("GTK+ version supported")
74 t00mlabs 38
            return True
39
        else:
40
            self.log.error("Please, install a modern version of GTK+ (>= 3.18)")
41
            return False
42
 
43
 
4 t00mlabs 44
    def message_dialog(self, head, body):
45
        dialog = Gtk.MessageDialog(None, 0, Gtk.MessageType.INFO, Gtk.ButtonsType.OK, "%s" % head)
46
        dialog.format_secondary_text("%s" % body)
47
        dialog.run()
48
        dialog.destroy()
142 t00mlabs 49
 
50
 
51
    def statusbar_msg(self, message):
213 t00m 52
        statusbar = self.gui.get_widget('widget_statusbar')
142 t00mlabs 53
        context = statusbar.get_context_id("basico")
54
        statusbar.push(context, message)
184 t00m 55
 
56
 
57
    def create_menuview_button(self, view):
58
        hbox = Gtk.Box(spacing = 0, orientation="horizontal")
203 t00m 59
        icon = self.im.get_pixbuf_icon('basico-%s' % view, 24, 24)
184 t00m 60
        image = Gtk.Image()
61
        image.set_from_pixbuf(icon)
62
        label = Gtk.Label("View by %s" % view)
63
        hbox.pack_start(image, False, False, 3)
64
        hbox.pack_start(label, False, False, 3)
65
        button = Gtk.Button()
66
        button.add(hbox)
67
        button.set_relief(Gtk.ReliefStyle.NONE)
188 t00m 68
        button.connect('clicked', self.cb.gui_refresh_view, '%s' % view)
184 t00m 69
 
70
        return button
193 t00m 71
 
72
 
196 t00mlabs 73
    def create_button(self, icon_name=None, icon_width=32, icon_heigth=32, title=''):
193 t00m 74
        button = Gtk.Button()
75
        button.set_relief(Gtk.ReliefStyle.NONE)
76
        hbox = Gtk.HBox()
196 t00mlabs 77
        icon = self.im.get_new_image_icon(icon_name, icon_width, icon_heigth)
193 t00m 78
        label = Gtk.Label()
79
        label.set_markup(title)
80
        label.set_xalign(0.0)
81
        hbox.pack_start(icon, False, False, 6)
82
        hbox.pack_start(label, True, True, 0)
83
        button.add(hbox)
84
 
85
        return button
212 t00m 86
 
87
 
241 t00m 88
    def warning_message_default(self, question, explanation):
89
        window = self.gui.get_window()
90
        icon = self.im.get_new_image_icon('basico-priority', 96, 96)
91
        icon.show_all()
92
        dialog = Gtk.MessageDialog(window, 0, Gtk.MessageType.QUESTION,
93
            Gtk.ButtonsType.YES_NO, question)
94
        # ~ dialog.set_size_request(800, 600);
95
        dialog.set_image(icon)
96
        dialog.format_secondary_text(explanation)
97
 
98
        return dialog
99
 
100
 
212 t00m 101
    def warning_message(self, widget, question, explanation, bag):
102
        self.debug("DBAG: %s(%d)" % (bag, len(bag)))
213 t00m 103
        window = self.gui.get_widget('gtk_app_window_main')
212 t00m 104
        icon = self.im.get_new_image_icon('basico-delete', 96, 96)
105
        icon.show_all()
106
        dialog = Gtk.MessageDialog(window, 0, Gtk.MessageType.QUESTION,
107
            Gtk.ButtonsType.YES_NO, question)
223 t00m 108
        dialog.set_size_request(800, 600);
212 t00m 109
        content_area = dialog.get_content_area()
110
        vbox = Gtk.VBox()
111
        label = Gtk.Label()
112
        label.set_markup('<b>This is the list of SAP Notes to be deleted</b>')
113
        vbox.pack_start(label, False, False, 0)
114
        scr = Gtk.ScrolledWindow()
115
        scr.set_shadow_type(Gtk.ShadowType.NONE)
116
        scr.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC)
117
        label = Gtk.Label()
223 t00m 118
        label.set_property('ellipsize', Pango.EllipsizeMode.MIDDLE)
119
        label.set_property('selectable', True)
120
        label.grab_focus()
121
        msg = '\n'
212 t00m 122
        if len(bag) == 1:
123
            label.set_text(bag[0])
124
        else:
125
            for sid in bag:
223 t00m 126
                msg += '   %s - %s   \n' % (sid, self.db.get_title(sid))
127
            msg += '\n'
128
            label.set_markup(msg)
212 t00m 129
        scr.add(label)
130
        vbox.pack_start(scr, True, True, 0)
131
        content_area.pack_start(vbox, True, True, 0)
132
        content_area.show_all()
133
        dialog.set_image(icon)
134
        dialog.format_secondary_text(explanation)
135
        response = dialog.run()
136
        if response == Gtk.ResponseType.YES:
137
            answer = True
138
        elif response == Gtk.ResponseType.NO:
139
            answer = False
140
        dialog.destroy()
141
 
142
        return answer
221 t00m 143
 
144
 
223 t00m 145
    def set_widget_visibility(self, widget_name, visibility):
146
        widget = self.gui.get_widget(widget_name)
147
        if visibility:
148
            widget.set_no_show_all(False)
149
            widget.show_all()
150
        else:
151
            widget.set_no_show_all(True)
152
            widget.hide()
153
 
154
 
221 t00m 155
    def get_combobox_text(self, combobox, col):
156
        treeiter = combobox.get_active_iter()
157
        model = combobox.get_model()
158
        return model[treeiter][col]
159
 
223 t00m 160
 
221 t00m 161
    def set_combobox_active(self, combobox, value):
162
        model = combobox.get_model()
163
        for treeiter in model:
164
            self.debug (model[treeiter][0])
165
 
240 t00m 166
 
221 t00m 167
    def get_textview_text(self, textview):
168
        textbuffer = textview.get_buffer()
169
        istart, iend = textbuffer.get_bounds()
170
        return textbuffer.get_text(istart, iend, False)
171
 
240 t00m 172
 
221 t00m 173
    def set_textview_text(self, textview, text):
174
        textbuffer = textview.get_buffer()
175
        textbuffer.set_text(text)
241 t00m 176
 
177
    def dialog_info(self, title, message):
178
        window = self.gui.get_window()
179
        dialog = Gtk.MessageDialog(window, 0, Gtk.MessageType.INFO, Gtk.ButtonsType.OK, title)
180
        dialog.format_secondary_text(message)
181
        dialog.run()
182
        dialog.destroy()