Subversion Repositories basico

Rev

Rev 223 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

#!/usr/bin/python
# -*- coding: utf-8 -*-
# File: uifuncs.py
# Author: Tomás Vírseda
# License: GPL v3
# Description: Generic UI functions service


from gi.repository import Gtk
from gi.repository import Gio
from gi.repository import Pango
from gi.repository.GdkPixbuf import Pixbuf

from basico.core.service import Service

class UIFuncs(Service):
    def initialize(self):
        self.get_services()


    def get_services(self):
        self.gui = self.app.get_service('GUI')
        self.cb = self.app.get_service('Callbacks')
        self.im = self.app.get_service('IM')
        self.db = self.app.get_service('DB')


    def get_gtk_version(self):
        return Gtk.get_major_version(), Gtk.get_minor_version(), Gtk.get_micro_version()


    def check_gtk_version(self):
        vmajor, vminor, vmicro =  self.get_gtk_version()
        self.debug("GTK+ Version: %d.%d.%d" % (vmajor, vminor, vmicro))

        if vmajor == 3 and vminor >= 18:
            self.debug("GTK+ version supported")
            return True
        else:
            self.log.error("Please, install a modern version of GTK+ (>= 3.18)")
            return False


    def message_dialog(self, head, body):
        dialog = Gtk.MessageDialog(None, 0, Gtk.MessageType.INFO, Gtk.ButtonsType.OK, "%s" % head)
        dialog.format_secondary_text("%s" % body)
        dialog.run()
        dialog.destroy()


    def statusbar_msg(self, message):
        statusbar = self.gui.get_widget('widget_statusbar')
        context = statusbar.get_context_id("basico")
        statusbar.push(context, message)


    def create_menuview_button(self, view):
        hbox = Gtk.Box(spacing = 0, orientation="horizontal")
        icon = self.im.get_pixbuf_icon('basico-%s' % view, 24, 24)
        image = Gtk.Image()
        image.set_from_pixbuf(icon)
        label = Gtk.Label("View by %s" % view)
        hbox.pack_start(image, False, False, 3)
        hbox.pack_start(label, False, False, 3)
        button = Gtk.Button()
        button.add(hbox)
        button.set_relief(Gtk.ReliefStyle.NONE)
        button.connect('clicked', self.cb.gui_refresh_view, '%s' % view)

        return button


    def create_button(self, icon_name=None, icon_width=32, icon_heigth=32, title=''):
        button = Gtk.Button()
        button.set_relief(Gtk.ReliefStyle.NONE)
        hbox = Gtk.HBox()
        icon = self.im.get_new_image_icon(icon_name, icon_width, icon_heigth)
        label = Gtk.Label()
        label.set_markup(title)
        label.set_xalign(0.0)
        hbox.pack_start(icon, False, False, 6)
        hbox.pack_start(label, True, True, 0)
        button.add(hbox)

        return button


    def warning_message(self, widget, question, explanation, bag):
        self.debug("DBAG: %s(%d)" % (bag, len(bag)))
        window = self.gui.get_widget('gtk_app_window_main')
        icon = self.im.get_new_image_icon('basico-delete', 96, 96)
        icon.show_all()
        dialog = Gtk.MessageDialog(window, 0, Gtk.MessageType.QUESTION,
            Gtk.ButtonsType.YES_NO, question)
        dialog.set_size_request(800, 600);
        content_area = dialog.get_content_area()
        vbox = Gtk.VBox()
        label = Gtk.Label()
        label.set_markup('<b>This is the list of SAP Notes to be deleted</b>')
        vbox.pack_start(label, False, False, 0)
        scr = Gtk.ScrolledWindow()
        scr.set_shadow_type(Gtk.ShadowType.NONE)
        scr.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC)
        label = Gtk.Label()
        label.set_property('ellipsize', Pango.EllipsizeMode.MIDDLE)
        label.set_property('selectable', True)
        label.grab_focus()
        msg = '\n'
        if len(bag) == 1:
            label.set_text(bag[0])
        else:
            for sid in bag:
                msg += '   %s - %s   \n' % (sid, self.db.get_title(sid))
            msg += '\n'
            label.set_markup(msg)
        scr.add(label)
        vbox.pack_start(scr, True, True, 0)
        content_area.pack_start(vbox, True, True, 0)
        content_area.show_all()
        dialog.set_image(icon)
        dialog.format_secondary_text(explanation)
        response = dialog.run()
        if response == Gtk.ResponseType.YES:
            answer = True
        elif response == Gtk.ResponseType.NO:
            answer = False
        dialog.destroy()

        return answer


    def set_widget_visibility(self, widget_name, visibility):
        widget = self.gui.get_widget(widget_name)
        if visibility:
            widget.set_no_show_all(False)
            widget.show_all()
        else:
            widget.set_no_show_all(True)
            widget.hide()


    def get_combobox_text(self, combobox, col):
        treeiter = combobox.get_active_iter()
        model = combobox.get_model()
        return model[treeiter][col]


    def set_combobox_active(self, combobox, value):
        model = combobox.get_model()
        for treeiter in model:
            self.debug (model[treeiter][0])

    def get_textview_text(self, textview):
        textbuffer = textview.get_buffer()
        istart, iend = textbuffer.get_bounds()
        return textbuffer.get_text(istart, iend, False)

    def set_textview_text(self, textview, text):
        textbuffer = textview.get_buffer()
        textbuffer.set_text(text)