2008-09-21 04:22:56 +02:00
|
|
|
"""
|
|
|
|
Alerts appear at the top of the body of your activity.
|
|
|
|
|
|
|
|
At a high level, Alert and its different variations (TimeoutAlert,
|
|
|
|
ConfirmationAlert, etc.) have a title, an alert message and then several
|
|
|
|
buttons that the user can click. The Alert class will pass "response" events
|
|
|
|
to your activity when any of these buttons are clicked, along with a
|
|
|
|
response_id to help you identify what button was clicked.
|
|
|
|
|
2015-08-02 00:47:22 +02:00
|
|
|
Example:
|
|
|
|
Create a simple alert message.
|
2008-09-21 04:22:56 +02:00
|
|
|
|
2015-08-02 00:47:22 +02:00
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
from sugar3.graphics.alert import Alert
|
2008-09-21 04:22:56 +02:00
|
|
|
|
2015-08-02 00:47:22 +02:00
|
|
|
# Create a new simple alert
|
2008-09-21 04:22:56 +02:00
|
|
|
alert = Alert()
|
|
|
|
# Populate the title and text body of the alert.
|
2015-08-02 00:47:22 +02:00
|
|
|
alert.props.title = _('Title of Alert Goes Here')
|
2008-09-21 04:22:56 +02:00
|
|
|
alert.props.msg = _('Text message of alert goes here')
|
2011-10-29 10:44:18 +02:00
|
|
|
# Call the add_alert() method (inherited via the sugar3.graphics.Window
|
2008-09-22 10:55:05 +02:00
|
|
|
# superclass of Activity) to add this alert to the activity window.
|
2008-09-21 04:22:56 +02:00
|
|
|
self.add_alert(alert)
|
|
|
|
alert.show()
|
|
|
|
|
2008-10-28 14:19:01 +01:00
|
|
|
STABLE.
|
2008-09-21 04:22:56 +02:00
|
|
|
"""
|
2007-10-15 19:03:48 +02:00
|
|
|
# Copyright (C) 2007, One Laptop Per Child
|
2010-06-14 04:21:23 +02:00
|
|
|
# Copyright (C) 2010, Anish Mangal <anishmangal2002@gmail.com>
|
2007-10-15 19:03:48 +02:00
|
|
|
#
|
|
|
|
# This library is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU Lesser General Public
|
|
|
|
# License as published by the Free Software Foundation; either
|
|
|
|
# version 2 of the License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This library is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
# Lesser General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Lesser General Public
|
|
|
|
# License along with this library; if not, write to the
|
|
|
|
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
# Boston, MA 02111-1307, USA.
|
|
|
|
|
2008-02-09 12:27:22 +01:00
|
|
|
import gettext
|
2007-10-11 20:04:04 +02:00
|
|
|
|
2011-11-15 19:29:07 +01:00
|
|
|
from gi.repository import Gtk
|
|
|
|
from gi.repository import GObject
|
2011-11-04 09:09:10 +01:00
|
|
|
from gi.repository import GLib
|
2007-10-15 19:03:48 +02:00
|
|
|
import math
|
2009-08-25 19:55:48 +02:00
|
|
|
|
2011-10-29 10:44:18 +02:00
|
|
|
from sugar3.graphics import style
|
|
|
|
from sugar3.graphics.icon import Icon
|
2007-10-11 20:04:04 +02:00
|
|
|
|
2013-09-11 15:59:34 +02:00
|
|
|
|
2013-09-11 16:02:47 +02:00
|
|
|
_ = lambda msg: gettext.dgettext('sugar-toolkit-gtk3', msg)
|
2007-10-11 20:04:04 +02:00
|
|
|
|
2009-08-25 21:12:40 +02:00
|
|
|
|
2011-11-15 19:29:07 +01:00
|
|
|
class Alert(Gtk.EventBox):
|
2008-09-21 04:22:56 +02:00
|
|
|
"""
|
|
|
|
UI interface for Alerts
|
2007-10-11 20:04:04 +02:00
|
|
|
|
|
|
|
Alerts are used inside the activity window instead of being a
|
|
|
|
separate popup window. They do not hide canvas content. You can
|
2015-08-02 00:47:22 +02:00
|
|
|
use `add_alert()` and `remove_alert()` inside your activity
|
2007-10-25 14:04:04 +02:00
|
|
|
to add and remove the alert. The position of the alert is below the
|
|
|
|
toolbox or top in fullscreen mode.
|
2007-10-11 20:04:04 +02:00
|
|
|
|
2015-08-02 00:47:22 +02:00
|
|
|
Args:
|
|
|
|
title (str): the title of the alert
|
|
|
|
message (str): the message of the alert
|
|
|
|
icon (str): the icon that appears at the far left
|
2007-10-11 20:04:04 +02:00
|
|
|
"""
|
|
|
|
__gtype_name__ = 'SugarAlert'
|
|
|
|
|
|
|
|
__gsignals__ = {
|
2011-11-15 19:29:07 +01:00
|
|
|
'response': (GObject.SignalFlags.RUN_FIRST, None, ([object])),
|
2009-08-25 21:12:40 +02:00
|
|
|
}
|
2007-10-11 20:04:04 +02:00
|
|
|
|
|
|
|
__gproperties__ = {
|
2011-11-15 19:29:07 +01:00
|
|
|
'title': (str, None, None, None, GObject.PARAM_READWRITE),
|
|
|
|
'msg': (str, None, None, None, GObject.PARAM_READWRITE),
|
|
|
|
'icon': (object, None, None, GObject.PARAM_WRITABLE),
|
2009-08-25 21:12:40 +02:00
|
|
|
}
|
2007-10-11 20:04:04 +02:00
|
|
|
|
|
|
|
def __init__(self, **kwargs):
|
|
|
|
self._title = None
|
|
|
|
self._msg = None
|
|
|
|
self._icon = None
|
|
|
|
self._buttons = {}
|
2008-02-17 13:40:14 +01:00
|
|
|
|
2011-11-15 19:29:07 +01:00
|
|
|
self._hbox = Gtk.HBox()
|
2008-02-17 13:40:14 +01:00
|
|
|
self._hbox.set_border_width(style.DEFAULT_SPACING)
|
|
|
|
self._hbox.set_spacing(style.DEFAULT_SPACING)
|
|
|
|
|
2011-11-15 19:29:07 +01:00
|
|
|
self._msg_box = Gtk.VBox()
|
|
|
|
self._title_label = Gtk.Label()
|
2007-10-11 20:04:04 +02:00
|
|
|
self._title_label.set_alignment(0, 0.5)
|
2011-10-30 08:01:32 +01:00
|
|
|
self._msg_box.pack_start(self._title_label, False, False, 0)
|
2007-10-11 20:04:04 +02:00
|
|
|
|
2011-11-15 19:29:07 +01:00
|
|
|
self._msg_label = Gtk.Label()
|
2007-10-11 20:04:04 +02:00
|
|
|
self._msg_label.set_alignment(0, 0.5)
|
2011-10-30 08:01:32 +01:00
|
|
|
self._msg_box.pack_start(self._msg_label, False, False, 0)
|
|
|
|
self._hbox.pack_start(self._msg_box, False, False, 0)
|
2009-08-25 19:55:48 +02:00
|
|
|
|
2011-11-15 19:29:07 +01:00
|
|
|
self._buttons_box = Gtk.HButtonBox()
|
|
|
|
self._buttons_box.set_layout(Gtk.ButtonBoxStyle.END)
|
2007-10-15 19:03:48 +02:00
|
|
|
self._buttons_box.set_spacing(style.DEFAULT_SPACING)
|
2011-11-15 19:29:07 +01:00
|
|
|
self._hbox.pack_start(self._buttons_box, True, True, 0)
|
2009-08-25 19:55:48 +02:00
|
|
|
|
2011-11-15 19:29:07 +01:00
|
|
|
GObject.GObject.__init__(self, **kwargs)
|
2007-10-11 20:04:04 +02:00
|
|
|
|
2009-08-25 19:55:48 +02:00
|
|
|
self.set_visible_window(True)
|
|
|
|
self.add(self._hbox)
|
2008-02-17 13:40:14 +01:00
|
|
|
self._title_label.show()
|
|
|
|
self._msg_label.show()
|
|
|
|
self._buttons_box.show()
|
2007-10-11 20:04:04 +02:00
|
|
|
self._msg_box.show()
|
|
|
|
self._hbox.show()
|
|
|
|
self.show()
|
2009-08-25 19:55:48 +02:00
|
|
|
|
|
|
|
def do_set_property(self, pspec, value):
|
2008-09-16 16:10:36 +02:00
|
|
|
"""
|
2015-08-02 00:47:22 +02:00
|
|
|
Set alert property, GObject internal method.
|
|
|
|
Use the `alert.props` object, eg::
|
2008-09-16 16:10:36 +02:00
|
|
|
|
2015-08-02 00:47:22 +02:00
|
|
|
alert.props.title = 'Are you happy?'
|
2008-09-16 16:10:36 +02:00
|
|
|
"""
|
2007-10-11 20:04:04 +02:00
|
|
|
if pspec.name == 'title':
|
|
|
|
if self._title != value:
|
|
|
|
self._title = value
|
2010-10-15 21:14:59 +02:00
|
|
|
self._title_label.set_markup('<b>' + self._title + '</b>')
|
2007-10-11 20:04:04 +02:00
|
|
|
elif pspec.name == 'msg':
|
|
|
|
if self._msg != value:
|
|
|
|
self._msg = value
|
|
|
|
self._msg_label.set_markup(self._msg)
|
2008-08-14 21:31:42 +02:00
|
|
|
self._msg_label.set_line_wrap(True)
|
2007-10-11 20:04:04 +02:00
|
|
|
elif pspec.name == 'icon':
|
|
|
|
if self._icon != value:
|
|
|
|
self._icon = value
|
2011-10-30 08:01:32 +01:00
|
|
|
self._hbox.pack_start(self._icon, False, False, 0)
|
2007-10-11 20:04:04 +02:00
|
|
|
self._hbox.reorder_child(self._icon, 0)
|
|
|
|
|
|
|
|
def do_get_property(self, pspec):
|
2008-09-16 16:10:36 +02:00
|
|
|
"""
|
2015-08-02 00:47:22 +02:00
|
|
|
Set alert property, GObject internal method.
|
|
|
|
Use the `alert.props` object, eg::
|
2008-09-16 16:10:36 +02:00
|
|
|
|
2015-08-02 00:47:22 +02:00
|
|
|
title = alert.props.title
|
2008-09-16 16:10:36 +02:00
|
|
|
"""
|
2007-10-11 20:04:04 +02:00
|
|
|
if pspec.name == 'title':
|
|
|
|
return self._title
|
|
|
|
elif pspec.name == 'msg':
|
|
|
|
return self._msg
|
|
|
|
|
2007-10-15 19:03:48 +02:00
|
|
|
def add_button(self, response_id, label, icon=None, position=-1):
|
2008-09-16 16:10:36 +02:00
|
|
|
"""
|
|
|
|
Add a button to the alert
|
|
|
|
|
2015-08-02 00:47:22 +02:00
|
|
|
Args:
|
|
|
|
response_id (int): will be emitted with the response signal a
|
|
|
|
response ID should one of the pre-defined GTK Response Type
|
|
|
|
Constants or a positive number
|
|
|
|
label (str): that will occure right to the buttom
|
|
|
|
icon (:class:`sugar3.graphics.icon.Icon` or :class:`Gtk.Image`, optional):
|
|
|
|
icon for the button, placed before the text
|
|
|
|
postion (int, optional): the position of the button in the box
|
2008-09-16 16:10:36 +02:00
|
|
|
|
2015-08-02 00:47:22 +02:00
|
|
|
Returns:
|
|
|
|
Gtk.Button: the button added to the alert
|
2008-09-16 16:10:36 +02:00
|
|
|
|
2007-10-11 20:04:04 +02:00
|
|
|
"""
|
2011-11-15 19:29:07 +01:00
|
|
|
button = Gtk.Button()
|
2007-10-11 20:04:04 +02:00
|
|
|
self._buttons[response_id] = button
|
2007-10-15 19:03:48 +02:00
|
|
|
if icon is not None:
|
|
|
|
button.set_image(icon)
|
2007-10-11 20:04:04 +02:00
|
|
|
button.set_label(label)
|
2011-11-15 19:29:07 +01:00
|
|
|
self._buttons_box.pack_start(button, True, True, 0)
|
2007-10-11 20:04:04 +02:00
|
|
|
button.show()
|
|
|
|
button.connect('clicked', self.__button_clicked_cb, response_id)
|
|
|
|
if position != -1:
|
|
|
|
self._buttons_box.reorder_child(button, position)
|
|
|
|
return button
|
|
|
|
|
|
|
|
def remove_button(self, response_id):
|
2008-09-16 16:10:36 +02:00
|
|
|
"""
|
|
|
|
Remove a button from the alert by the given response id
|
|
|
|
|
2015-08-02 00:47:22 +02:00
|
|
|
Args:
|
|
|
|
response_id (int): the same response id passed to add_button
|
2008-09-16 16:10:36 +02:00
|
|
|
|
2015-08-02 00:47:22 +02:00
|
|
|
Returns:
|
|
|
|
None
|
2008-09-16 16:10:36 +02:00
|
|
|
|
|
|
|
"""
|
2008-02-17 13:40:14 +01:00
|
|
|
self._buttons_box.remove(self._buttons[response_id])
|
2007-10-11 20:04:04 +02:00
|
|
|
|
2008-02-17 13:40:14 +01:00
|
|
|
def _response(self, response_id):
|
2015-08-02 00:47:22 +02:00
|
|
|
"""
|
|
|
|
Emitting response when we have a result
|
2007-10-11 20:04:04 +02:00
|
|
|
|
|
|
|
A result can be that a user has clicked a button or
|
|
|
|
a timeout has occured, the id identifies the button
|
|
|
|
that has been clicked and -1 for a timeout
|
|
|
|
"""
|
2008-02-17 13:40:14 +01:00
|
|
|
self.emit('response', response_id)
|
2007-10-11 20:04:04 +02:00
|
|
|
|
|
|
|
def __button_clicked_cb(self, button, response_id):
|
|
|
|
self._response(response_id)
|
|
|
|
|
|
|
|
|
|
|
|
class ConfirmationAlert(Alert):
|
2008-09-21 04:22:56 +02:00
|
|
|
"""
|
2015-08-02 00:47:22 +02:00
|
|
|
This is a ready-made two button (Cancel, Ok) alert.
|
2008-09-21 04:22:56 +02:00
|
|
|
|
|
|
|
A confirmation alert is a nice shortcut from a standard Alert because it
|
|
|
|
comes with 'OK' and 'Cancel' buttons already built-in. When clicked, the
|
2015-08-02 00:47:22 +02:00
|
|
|
'OK' button will emit a response with a response_id of
|
|
|
|
:class:`Gtk.ResponseType.OK`, while the 'Cancel' button will emit
|
|
|
|
:class:`Gtk.ResponseType.CANCEL`.
|
2008-09-21 04:22:56 +02:00
|
|
|
|
2015-08-02 00:47:22 +02:00
|
|
|
Args:
|
|
|
|
**kwargs: options for :class:`sugar3.graphics.alert.Alert`
|
2008-09-21 04:22:56 +02:00
|
|
|
|
|
|
|
.. code-block:: python
|
2015-08-02 00:47:22 +02:00
|
|
|
|
|
|
|
from sugar3.graphics.alert import ConfirmationAlert
|
|
|
|
|
|
|
|
# Create a Confirmation alert (with ok and cancel buttons standard)
|
|
|
|
# then add it to the UI.
|
2008-09-21 04:22:56 +02:00
|
|
|
def _alert_confirmation(self):
|
|
|
|
alert = ConfirmationAlert()
|
|
|
|
alert.props.title=_('Title of Alert Goes Here')
|
|
|
|
alert.props.msg = _('Text message of alert goes here')
|
|
|
|
alert.connect('response', self._alert_response_cb)
|
|
|
|
self.add_alert(alert)
|
|
|
|
|
2015-08-02 00:47:22 +02:00
|
|
|
# Called when an alert object throws a response event.
|
2008-09-21 04:22:56 +02:00
|
|
|
def _alert_response_cb(self, alert, response_id):
|
2015-08-02 00:47:22 +02:00
|
|
|
# Remove the alert from the screen, since either a response button
|
|
|
|
# was clicked or there was a timeout
|
2008-09-21 04:22:56 +02:00
|
|
|
self.remove_alert(alert)
|
|
|
|
|
2015-08-02 00:47:22 +02:00
|
|
|
# Do any work that is specific to the type of button clicked.
|
2011-11-15 19:29:07 +01:00
|
|
|
if response_id is Gtk.ResponseType.OK:
|
2008-09-21 04:22:56 +02:00
|
|
|
print 'Ok Button was clicked. Do any work upon ok here ...'
|
2011-11-15 19:29:07 +01:00
|
|
|
elif response_id is Gtk.ResponseType.CANCEL:
|
2008-09-21 04:22:56 +02:00
|
|
|
print 'Cancel Button was clicked.'
|
|
|
|
"""
|
2007-10-11 20:04:04 +02:00
|
|
|
|
|
|
|
def __init__(self, **kwargs):
|
|
|
|
Alert.__init__(self, **kwargs)
|
|
|
|
|
|
|
|
icon = Icon(icon_name='dialog-cancel')
|
2011-11-15 19:29:07 +01:00
|
|
|
self.add_button(Gtk.ResponseType.CANCEL, _('Cancel'), icon)
|
2007-10-11 20:04:04 +02:00
|
|
|
icon.show()
|
|
|
|
|
|
|
|
icon = Icon(icon_name='dialog-ok')
|
2011-11-15 19:29:07 +01:00
|
|
|
self.add_button(Gtk.ResponseType.OK, _('Ok'), icon)
|
2007-10-11 20:04:04 +02:00
|
|
|
icon.show()
|
|
|
|
|
2010-10-15 19:53:25 +02:00
|
|
|
|
2010-06-14 04:21:23 +02:00
|
|
|
class ErrorAlert(Alert):
|
|
|
|
"""
|
|
|
|
This is a ready-made one button (Ok) alert.
|
|
|
|
|
|
|
|
An error alert is a nice shortcut from a standard Alert because it
|
|
|
|
comes with the 'OK' button already built-in. When clicked, the
|
2015-08-02 00:47:22 +02:00
|
|
|
'OK' button will emit a response with a response_id of
|
|
|
|
:class:`Gtk.ResponseType.OK`.
|
2010-06-14 04:21:23 +02:00
|
|
|
|
2015-08-02 00:47:22 +02:00
|
|
|
Args:
|
|
|
|
**kwargs: options for :class:`sugar3.graphics.alert.Alert`
|
2010-06-14 04:21:23 +02:00
|
|
|
|
|
|
|
.. code-block:: python
|
2015-08-02 00:47:22 +02:00
|
|
|
|
|
|
|
from sugar3.graphics.alert import ErrorAlert
|
|
|
|
|
|
|
|
# Create a Error alert (with ok button standard)
|
2010-06-14 04:21:23 +02:00
|
|
|
# and add it to the UI.
|
|
|
|
def _alert_error(self):
|
|
|
|
alert = ErrorAlert()
|
|
|
|
alert.props.title=_('Title of Alert Goes Here')
|
|
|
|
alert.props.msg = _('Text message of alert goes here')
|
|
|
|
alert.connect('response', self._alert_response_cb)
|
|
|
|
self.add_alert(alert)
|
|
|
|
|
2015-08-02 00:47:22 +02:00
|
|
|
# called when an alert object throws a response event.
|
2010-06-14 04:21:23 +02:00
|
|
|
def _alert_response_cb(self, alert, response_id):
|
2015-08-02 00:47:22 +02:00
|
|
|
# Remove the alert from the screen, since either a response button
|
|
|
|
# was clicked or there was a timeout
|
2010-06-14 04:21:23 +02:00
|
|
|
self.remove_alert(alert)
|
|
|
|
|
2015-08-02 00:47:22 +02:00
|
|
|
# Do any work that is specific to the response_id.
|
2011-11-15 19:29:07 +01:00
|
|
|
if response_id is Gtk.ResponseType.OK:
|
2015-08-02 00:47:22 +02:00
|
|
|
print 'Ok Button was clicked. Do any work upon ok here'
|
2010-06-14 04:21:23 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, **kwargs):
|
|
|
|
Alert.__init__(self, **kwargs)
|
|
|
|
|
|
|
|
icon = Icon(icon_name='dialog-ok')
|
2011-11-15 19:29:07 +01:00
|
|
|
self.add_button(Gtk.ResponseType.OK, _('Ok'), icon)
|
2010-06-14 04:21:23 +02:00
|
|
|
icon.show()
|
2007-10-11 20:04:04 +02:00
|
|
|
|
2010-10-15 19:53:25 +02:00
|
|
|
|
2011-11-15 19:29:07 +01:00
|
|
|
class _TimeoutIcon(Gtk.Alignment):
|
2011-08-11 17:57:40 +02:00
|
|
|
__gtype_name__ = 'SugarTimeoutIcon'
|
|
|
|
|
|
|
|
def __init__(self):
|
2011-10-30 14:54:53 +01:00
|
|
|
Gtk.Alignment.__init__(self, xalign=0, yalign=0, xscale=1, yscale=1)
|
2011-08-11 17:57:40 +02:00
|
|
|
self.set_app_paintable(True)
|
2011-11-15 19:29:07 +01:00
|
|
|
self._text = Gtk.Label()
|
2011-08-11 17:57:40 +02:00
|
|
|
self._text.set_alignment(0.5, 0.5)
|
|
|
|
self.add(self._text)
|
|
|
|
self._text.show()
|
2011-10-30 23:06:13 +01:00
|
|
|
self.connect('draw', self.__draw_cb)
|
2011-08-11 17:57:40 +02:00
|
|
|
|
2011-10-30 23:06:13 +01:00
|
|
|
def __draw_cb(self, widget, context):
|
2011-08-11 17:57:40 +02:00
|
|
|
self._draw(context)
|
|
|
|
return False
|
|
|
|
|
2011-12-15 02:23:18 +01:00
|
|
|
def do_get_preferred_width(self):
|
|
|
|
width = Gtk.icon_size_lookup(Gtk.IconSize.BUTTON)[1]
|
|
|
|
return width, width
|
|
|
|
|
|
|
|
def do_get_preferred_height(self):
|
|
|
|
height = Gtk.icon_size_lookup(Gtk.IconSize.BUTTON)[2]
|
|
|
|
return height, height
|
2011-08-11 17:57:40 +02:00
|
|
|
|
|
|
|
def _draw(self, context):
|
2011-10-30 23:06:13 +01:00
|
|
|
w = self.get_allocated_width()
|
|
|
|
h = self.get_allocated_height()
|
|
|
|
x = w * 0.5
|
|
|
|
y = h * 0.5
|
|
|
|
radius = w / 2
|
2011-08-11 17:57:40 +02:00
|
|
|
context.arc(x, y, radius, 0, 2 * math.pi)
|
2012-10-01 17:29:29 +02:00
|
|
|
widget_style = self.get_style_context()
|
2012-11-20 19:46:33 +01:00
|
|
|
color = widget_style.get_background_color(self.get_state_flags())
|
2011-10-30 23:06:13 +01:00
|
|
|
context.set_source_rgb(color.red, color.green, color.blue)
|
2011-08-11 17:57:40 +02:00
|
|
|
context.fill_preserve()
|
|
|
|
|
|
|
|
def set_text(self, text):
|
2011-11-04 09:09:10 +01:00
|
|
|
self._text.set_markup('<b>%s</b>' % GLib.markup_escape_text(str(text)))
|
2007-10-15 19:03:48 +02:00
|
|
|
|
|
|
|
|
|
|
|
class TimeoutAlert(Alert):
|
2008-09-21 16:01:48 +02:00
|
|
|
"""
|
2015-08-02 00:47:22 +02:00
|
|
|
This is a ready-made two button (Cancel, Continue) alert. The continue
|
|
|
|
button contains a visual countdown indicating the time remaining to the
|
|
|
|
user. If the user does not select a button before the timeout, the
|
|
|
|
response callback is called and the alert is usually removed.
|
2008-09-21 16:01:48 +02:00
|
|
|
|
2015-08-02 00:47:22 +02:00
|
|
|
Args:
|
|
|
|
timeout (int, optional): the length in seconds for the timeout to
|
|
|
|
last, defaults to 5 seconds
|
|
|
|
**kwargs: options for :class:`sugar3.graphics.alert.Alert`
|
2008-09-21 16:01:48 +02:00
|
|
|
|
2015-08-02 00:47:22 +02:00
|
|
|
.. code-block:: python
|
2008-09-21 16:01:48 +02:00
|
|
|
|
2015-08-02 00:47:22 +02:00
|
|
|
from sugar3.graphics.alert import TimeoutAlert
|
2008-09-21 16:01:48 +02:00
|
|
|
|
2015-08-02 00:47:22 +02:00
|
|
|
# Create a Timeout alert (with ok and cancel buttons standard) then
|
|
|
|
# add it to the UI
|
2008-09-21 16:01:48 +02:00
|
|
|
def _alert_timeout(self):
|
2015-08-02 00:47:22 +02:00
|
|
|
# Notice that for a TimeoutAlert, you pass the number of seconds
|
|
|
|
# in which to timeout. By default, this is 5.
|
2008-09-21 16:01:48 +02:00
|
|
|
alert = TimeoutAlert(10)
|
2015-08-02 00:47:22 +02:00
|
|
|
alert.props.title = _('Title of Alert Goes Here')
|
2008-09-21 16:01:48 +02:00
|
|
|
alert.props.msg = _('Text message of timeout alert goes here')
|
2015-08-02 00:47:22 +02:00
|
|
|
alert.connect('response', self.__alert_response_cb)
|
2008-09-21 16:01:48 +02:00
|
|
|
self.add_alert(alert)
|
|
|
|
|
2015-08-02 00:47:22 +02:00
|
|
|
# Called when an alert object throws a response event.
|
|
|
|
def __alert_response_cb(self, alert, response_id):
|
|
|
|
# Remove the alert from the screen, since either a response button
|
|
|
|
# was clicked or there was a timeout
|
2008-09-21 16:01:48 +02:00
|
|
|
self.remove_alert(alert)
|
|
|
|
|
2015-08-02 00:47:22 +02:00
|
|
|
# Do any work that is specific to the type of button clicked.
|
2011-11-15 19:29:07 +01:00
|
|
|
if response_id is Gtk.ResponseType.OK:
|
2008-09-21 16:01:48 +02:00
|
|
|
print 'Ok Button was clicked. Do any work upon ok here ...'
|
2011-11-15 19:29:07 +01:00
|
|
|
elif response_id is Gtk.ResponseType.CANCEL:
|
2008-09-21 16:01:48 +02:00
|
|
|
print 'Cancel Button was clicked.'
|
|
|
|
elif response_id == -1:
|
|
|
|
print 'Timout occurred'
|
2007-10-15 19:03:48 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, timeout=5, **kwargs):
|
|
|
|
Alert.__init__(self, **kwargs)
|
|
|
|
|
|
|
|
self._timeout = timeout
|
2009-08-25 19:55:48 +02:00
|
|
|
|
2007-10-15 19:03:48 +02:00
|
|
|
icon = Icon(icon_name='dialog-cancel')
|
2011-11-15 19:29:07 +01:00
|
|
|
self.add_button(Gtk.ResponseType.CANCEL, _('Cancel'), icon)
|
2007-10-15 19:03:48 +02:00
|
|
|
icon.show()
|
2009-08-25 19:55:48 +02:00
|
|
|
|
2011-08-11 17:57:40 +02:00
|
|
|
self._timeout_text = _TimeoutIcon()
|
|
|
|
self._timeout_text.set_text(self._timeout)
|
2011-11-15 19:29:07 +01:00
|
|
|
self.add_button(Gtk.ResponseType.OK, _('Continue'), self._timeout_text)
|
2011-08-11 17:57:40 +02:00
|
|
|
self._timeout_text.show()
|
2007-10-15 19:03:48 +02:00
|
|
|
|
2015-10-15 02:17:41 +02:00
|
|
|
self._timeout_sid = GLib.timeout_add(1000, self.__timeout)
|
2009-08-25 19:55:48 +02:00
|
|
|
|
2007-10-15 19:03:48 +02:00
|
|
|
def __timeout(self):
|
|
|
|
self._timeout -= 1
|
2011-08-11 17:57:40 +02:00
|
|
|
self._timeout_text.set_text(self._timeout)
|
2007-10-15 19:03:48 +02:00
|
|
|
if self._timeout == 0:
|
2015-10-15 02:17:41 +02:00
|
|
|
Alert._response(self, Gtk.ResponseType.OK)
|
2007-10-15 19:03:48 +02:00
|
|
|
return False
|
|
|
|
return True
|
2007-10-25 14:04:04 +02:00
|
|
|
|
2015-10-15 02:17:41 +02:00
|
|
|
def _response(self, *args):
|
|
|
|
GLib.source_remove(self._timeout_sid)
|
|
|
|
Alert._response(self, *args)
|
|
|
|
|
2007-10-25 14:04:04 +02:00
|
|
|
|
|
|
|
class NotifyAlert(Alert):
|
2008-09-21 16:01:48 +02:00
|
|
|
"""
|
2015-08-02 00:47:22 +02:00
|
|
|
Timeout alert with only an "OK" button. This should be used just for
|
|
|
|
notifications and not for user interaction. The alert will timeout after
|
|
|
|
a given length, simmilar to a :class:`sugar3.graphics.alert.TimeoutAlert`.
|
2008-09-21 16:01:48 +02:00
|
|
|
|
2015-08-02 00:47:22 +02:00
|
|
|
Args:
|
|
|
|
timeout (int, optional): the length in seconds for the timeout to
|
|
|
|
last, defaults to 5 seconds
|
|
|
|
**kwargs: options for :class:`sugar3.graphics.alert.Alert`
|
2008-09-21 16:01:48 +02:00
|
|
|
|
|
|
|
.. code-block:: python
|
2015-08-02 00:47:22 +02:00
|
|
|
|
|
|
|
from sugar3.graphics.alert import NotifyAlert
|
|
|
|
|
|
|
|
# create a Notify alert (with only an 'OK' button) then show it
|
2008-09-21 16:01:48 +02:00
|
|
|
def _alert_notify(self):
|
2015-08-02 00:47:22 +02:00
|
|
|
alert = NotifyAlert()
|
|
|
|
alert.props.title = _('Title of Alert Goes Here')
|
2008-09-21 16:01:48 +02:00
|
|
|
alert.props.msg = _('Text message of notify alert goes here')
|
|
|
|
alert.connect('response', self._alert_response_cb)
|
|
|
|
self.add_alert(alert)
|
|
|
|
|
2015-08-02 00:47:22 +02:00
|
|
|
def __alert_response_cb(self, alert, response_id):
|
|
|
|
# Hide the alert from the user
|
|
|
|
self.remove_alert(alert)
|
|
|
|
|
|
|
|
assert response_id == Gtk.ResponseType.OK
|
2008-09-21 16:01:48 +02:00
|
|
|
"""
|
2007-10-25 14:04:04 +02:00
|
|
|
|
|
|
|
def __init__(self, timeout=5, **kwargs):
|
|
|
|
Alert.__init__(self, **kwargs)
|
|
|
|
|
|
|
|
self._timeout = timeout
|
|
|
|
|
2011-08-11 17:57:40 +02:00
|
|
|
self._timeout_text = _TimeoutIcon()
|
|
|
|
self._timeout_text.set_text(self._timeout)
|
2011-11-15 19:29:07 +01:00
|
|
|
self.add_button(Gtk.ResponseType.OK, _('Ok'), self._timeout_text)
|
2011-08-11 17:57:40 +02:00
|
|
|
self._timeout_text.show()
|
2007-10-25 14:04:04 +02:00
|
|
|
|
2013-05-06 18:18:10 +02:00
|
|
|
GLib.timeout_add(1000, self.__timeout)
|
2007-10-25 14:04:04 +02:00
|
|
|
|
|
|
|
def __timeout(self):
|
|
|
|
self._timeout -= 1
|
2011-08-11 17:57:40 +02:00
|
|
|
self._timeout_text.set_text(self._timeout)
|
2007-10-25 14:04:04 +02:00
|
|
|
if self._timeout == 0:
|
2011-11-15 19:29:07 +01:00
|
|
|
self._response(Gtk.ResponseType.OK)
|
2007-10-25 14:04:04 +02:00
|
|
|
return False
|
|
|
|
return True
|