2008-09-21 04:22:56 +02:00
|
|
|
"""
|
2017-07-19 09:31:09 +02:00
|
|
|
Alerts appear in an activity below the toolbox and above the canvas.
|
2008-09-21 04:22:56 +02:00
|
|
|
|
2017-07-19 09:31:09 +02:00
|
|
|
:class:`Alert` and the derived :class:`TimeoutAlert`,
|
|
|
|
:class:`ConfirmationAlert`, :class:`ErrorAlert`, and
|
|
|
|
:class:`NotifyAlert`, each have a title, a message and optional
|
|
|
|
buttons.
|
|
|
|
|
|
|
|
:class:`Alert` will emit a `response` signal when a button is
|
|
|
|
clicked.
|
|
|
|
|
|
|
|
The :class:`TimeoutAlert` and :class:`NotifyAlert` display a countdown
|
|
|
|
and will emit a `response` signal when a timeout occurs.
|
2008-09-21 04:22:56 +02:00
|
|
|
|
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()
|
2017-07-19 09:31:09 +02:00
|
|
|
|
|
|
|
# Set 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')
|
2017-07-19 09:31:09 +02:00
|
|
|
|
|
|
|
# Add the alert to the activity
|
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
|
|
|
|
2018-11-19 04:09:18 +01:00
|
|
|
if not hasattr(GObject.ParamFlags, 'READWRITE'):
|
|
|
|
GObject.ParamFlags.READWRITE = GObject.ParamFlags.WRITABLE | \
|
|
|
|
GObject.ParamFlags.READABLE
|
|
|
|
|
|
|
|
|
2011-11-15 19:29:07 +01:00
|
|
|
class Alert(Gtk.EventBox):
|
2008-09-21 04:22:56 +02:00
|
|
|
"""
|
2017-07-19 09:31:09 +02:00
|
|
|
Alerts are inside the activity window instead of being a
|
|
|
|
separate popup window. They do not hide the canvas.
|
|
|
|
|
|
|
|
Use :func:`~sugar3.graphics.window.Window.add_alert` and
|
|
|
|
:func:`~sugar3.graphics.window.Window.remove_alert` to add and
|
|
|
|
remove an alert. These methods are inherited by an
|
|
|
|
:class:`~sugar3.activity.activity.Activity` via superclass
|
|
|
|
:class:`~sugar3.graphics.window.Window`.
|
2007-10-11 20:04:04 +02:00
|
|
|
|
2017-07-19 09:31:09 +02:00
|
|
|
The alert is placed between the canvas and the toolbox, or above
|
|
|
|
the canvas 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__ = {
|
2018-07-24 21:00:25 +02:00
|
|
|
'title': (str, None, None, None, GObject.ParamFlags.READWRITE),
|
|
|
|
'msg': (str, None, None, None, GObject.ParamFlags.READWRITE),
|
|
|
|
'icon': (object, None, None, GObject.ParamFlags.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)
|
2016-02-27 01:36:26 +01:00
|
|
|
self._title_label.set_ellipsize(style.ELLIPSIZE_MODE_DEFAULT)
|
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)
|
2016-02-27 01:36:26 +01:00
|
|
|
self._msg_label.set_ellipsize(style.ELLIPSIZE_MODE_DEFAULT)
|
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)
|
2017-06-01 04:50:29 +02:00
|
|
|
self._hbox.pack_end(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-10-14 05:38:03 +02:00
|
|
|
Get alert property, GObject internal method.
|
2015-08-02 00:47:22 +02:00
|
|
|
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
|
|
|
|
|
2017-06-01 04:50:29 +02:00
|
|
|
def add_entry(self):
|
|
|
|
"""
|
2017-07-19 09:31:09 +02:00
|
|
|
Create an entry and add it to the alert.
|
|
|
|
|
|
|
|
The entry is placed after the title and before the buttons.
|
|
|
|
|
|
|
|
Caller is responsible for capturing the entry text in the
|
|
|
|
`response` signal handler or a :class:`Gtk.Entry` signal
|
|
|
|
handler.
|
2017-06-01 04:50:29 +02:00
|
|
|
|
|
|
|
Returns:
|
2017-07-19 09:31:09 +02:00
|
|
|
:class:`Gtk.Entry`: the entry added to the alert
|
2017-06-01 04:50:29 +02:00
|
|
|
"""
|
|
|
|
entry = Gtk.Entry()
|
|
|
|
self._hbox.pack_start(entry, True, True, 0)
|
|
|
|
entry.show()
|
|
|
|
|
|
|
|
self._hbox.set_child_packing(self._buttons_box, False, False, 0,
|
|
|
|
Gtk.PackType.END)
|
|
|
|
|
|
|
|
return entry
|
|
|
|
|
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
|
|
|
"""
|
2017-07-19 09:31:09 +02:00
|
|
|
Create a button and add it to the alert.
|
|
|
|
|
|
|
|
The button is added to the end of the alert.
|
|
|
|
|
|
|
|
When the button is clicked, the `response` signal will be
|
|
|
|
emitted, along with a response identifier.
|
2008-09-16 16:10:36 +02:00
|
|
|
|
2015-08-02 00:47:22 +02:00
|
|
|
Args:
|
2017-07-19 09:31:09 +02:00
|
|
|
response_id (int): the response identifier, a
|
|
|
|
:class:`Gtk.ResponseType` constant or any positive
|
|
|
|
integer,
|
|
|
|
label (str): a label for the button
|
|
|
|
icon (:class:`~sugar3.graphics.icon.Icon` or \
|
|
|
|
:class:`Gtk.Image`, optional):
|
|
|
|
an icon for the button
|
|
|
|
position (int, optional): the position of the button in
|
|
|
|
the box of buttons,
|
2008-09-16 16:10:36 +02:00
|
|
|
|
2015-08-02 00:47:22 +02:00
|
|
|
Returns:
|
2017-07-19 09:31:09 +02:00
|
|
|
:class:`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
|
|
|
"""
|
2017-07-19 09:31:09 +02:00
|
|
|
Remove a button from the alert.
|
|
|
|
|
|
|
|
The button is selected for removal using the response
|
|
|
|
identifier that was passed to :func:`add_button`.
|
2008-09-16 16:10:36 +02:00
|
|
|
|
2015-08-02 00:47:22 +02:00
|
|
|
Args:
|
2017-07-19 09:31:09 +02:00
|
|
|
response_id (int): the response identifier
|
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
|
2017-08-01 01:39:02 +02:00
|
|
|
a timeout has occurred, the id identifies the button
|
2007-10-11 20:04:04 +02:00
|
|
|
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)
|
2015-12-31 12:53:47 +01:00
|
|
|
if hasattr(Alert, 'set_css_name'):
|
|
|
|
Alert.set_css_name('alert')
|
2007-10-11 20:04:04 +02:00
|
|
|
|
|
|
|
|
|
|
|
class ConfirmationAlert(Alert):
|
2008-09-21 04:22:56 +02:00
|
|
|
"""
|
2017-07-19 09:31:09 +02:00
|
|
|
An alert with two buttons; Ok and Cancel.
|
2008-09-21 04:22:56 +02:00
|
|
|
|
2017-07-19 09:31:09 +02:00
|
|
|
When a button is clicked, the :class:`ConfirmationAlert` will emit
|
|
|
|
a `response` signal with a response identifier. For the Ok
|
|
|
|
button, the response identifier will be
|
|
|
|
:class:`Gtk.ResponseType.OK`. For the Cancel button,
|
2015-08-02 00:47:22 +02:00
|
|
|
:class:`Gtk.ResponseType.CANCEL`.
|
2008-09-21 04:22:56 +02:00
|
|
|
|
2015-08-02 00:47:22 +02:00
|
|
|
Args:
|
2017-07-19 09:31:09 +02:00
|
|
|
**kwargs: parameters 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
|
|
|
|
|
2017-07-19 09:31:09 +02:00
|
|
|
# Create a Confirmation alert and 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)
|
|
|
|
|
2017-07-19 09:31:09 +02:00
|
|
|
# Called when an alert object sends a response signal.
|
2008-09-21 04:22:56 +02:00
|
|
|
def _alert_response_cb(self, alert, response_id):
|
2017-07-19 09:31:09 +02:00
|
|
|
# Remove the alert
|
2008-09-21 04:22:56 +02:00
|
|
|
self.remove_alert(alert)
|
|
|
|
|
2017-07-19 09:31:09 +02:00
|
|
|
# Check the response identifier.
|
2011-11-15 19:29:07 +01:00
|
|
|
if response_id is Gtk.ResponseType.OK:
|
2017-07-19 09:31:09 +02:00
|
|
|
print 'Ok Button was clicked.'
|
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):
|
|
|
|
"""
|
2017-07-19 09:31:09 +02:00
|
|
|
An alert with one button; Ok.
|
2010-06-14 04:21:23 +02:00
|
|
|
|
2017-07-19 09:31:09 +02:00
|
|
|
When the button is clicked, the :class:`ErrorAlert` will
|
|
|
|
emit a `response` signal with a response identifier
|
2015-08-02 00:47:22 +02:00
|
|
|
:class:`Gtk.ResponseType.OK`.
|
2010-06-14 04:21:23 +02:00
|
|
|
|
2015-08-02 00:47:22 +02:00
|
|
|
Args:
|
2017-07-19 09:31:09 +02:00
|
|
|
**kwargs: parameters 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
|
|
|
|
|
2017-07-19 09:31:09 +02:00
|
|
|
# Create a Error alert and add it to the UI.
|
2010-06-14 04:21:23 +02:00
|
|
|
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):
|
2017-07-19 09:31:09 +02:00
|
|
|
# Remove the alert
|
2010-06-14 04:21:23 +02:00
|
|
|
self.remove_alert(alert)
|
|
|
|
|
2017-07-19 09:31:09 +02:00
|
|
|
# Check the response identifier.
|
2011-11-15 19:29:07 +01:00
|
|
|
if response_id is Gtk.ResponseType.OK:
|
2017-07-19 09:31:09 +02:00
|
|
|
print 'Ok Button was clicked.'
|
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)))
|
2015-12-31 12:53:47 +01:00
|
|
|
if hasattr(_TimeoutIcon, 'set_css_name'):
|
|
|
|
_TimeoutIcon.set_css_name('timeouticon')
|
2007-10-15 19:03:48 +02:00
|
|
|
|
|
|
|
|
2015-10-16 05:06:51 +02:00
|
|
|
class _TimeoutAlert(Alert):
|
|
|
|
def __init__(self, timeout=5, label=_('Ok'), **kwargs):
|
|
|
|
Alert.__init__(self, **kwargs)
|
|
|
|
|
|
|
|
self._timeout = timeout
|
|
|
|
|
|
|
|
self._timeout_text = _TimeoutIcon()
|
|
|
|
self._timeout_text.set_text(self._timeout)
|
|
|
|
self.add_button(Gtk.ResponseType.OK, label, self._timeout_text)
|
|
|
|
self._timeout_text.show()
|
|
|
|
|
|
|
|
self._timeout_sid = GLib.timeout_add(1000, self.__timeout_cb)
|
|
|
|
|
|
|
|
def __timeout_cb(self):
|
|
|
|
self._timeout -= 1
|
|
|
|
self._timeout_text.set_text(self._timeout)
|
|
|
|
if self._timeout == 0:
|
2018-02-07 17:11:41 +01:00
|
|
|
Alert._response(self, -1)
|
2015-10-16 05:06:51 +02:00
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
def _response(self, *args):
|
|
|
|
GLib.source_remove(self._timeout_sid)
|
|
|
|
Alert._response(self, *args)
|
|
|
|
|
|
|
|
|
|
|
|
class TimeoutAlert(_TimeoutAlert):
|
2008-09-21 16:01:48 +02:00
|
|
|
"""
|
2017-07-19 09:31:09 +02:00
|
|
|
A timed alert with two buttons; Continue and Cancel. The Continue
|
|
|
|
button contains a countdown of seconds remaining.
|
|
|
|
|
|
|
|
When a button is clicked, the :class:`TimeoutAlert` will emit
|
|
|
|
a `response` signal with a response identifier. For the Continue
|
|
|
|
button, the response identifier will be
|
|
|
|
:class:`Gtk.ResponseType.OK`. For the Cancel button,
|
|
|
|
:class:`Gtk.ResponseType.CANCEL`.
|
|
|
|
|
|
|
|
If the countdown reaches zero before a button is clicked, the
|
|
|
|
:class:`TimeoutAlert` will emit a `response` signal with a
|
|
|
|
response identifier of -1.
|
2008-09-21 16:01:48 +02:00
|
|
|
|
2015-08-02 00:47:22 +02:00
|
|
|
Args:
|
2017-07-19 09:31:09 +02:00
|
|
|
timeout (int, optional): time in seconds, default 5
|
|
|
|
**kwargs: parameters 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
|
|
|
|
2017-07-19 09:31:09 +02:00
|
|
|
# Create a Timeout alert and add it to the UI
|
2008-09-21 16:01:48 +02:00
|
|
|
def _alert_timeout(self):
|
2017-07-19 09:31:09 +02:00
|
|
|
alert = TimeoutAlert(timeout=10)
|
2015-08-02 00:47:22 +02:00
|
|
|
alert.props.title = _('Title of Alert Goes Here')
|
2017-07-19 09:31:09 +02:00
|
|
|
alert.props.msg = _('Text message of 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):
|
2017-07-19 09:31:09 +02:00
|
|
|
# Remove the alert
|
2008-09-21 16:01:48 +02:00
|
|
|
self.remove_alert(alert)
|
|
|
|
|
2017-07-19 09:31:09 +02:00
|
|
|
# Check the response identifier.
|
2011-11-15 19:29:07 +01:00
|
|
|
if response_id is Gtk.ResponseType.OK:
|
2017-07-19 09:31:09 +02:00
|
|
|
print 'Continue Button was clicked.'
|
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:
|
2015-10-14 05:38:03 +02:00
|
|
|
print 'Timeout occurred'
|
2007-10-15 19:03:48 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, timeout=5, **kwargs):
|
2015-10-16 05:06:51 +02:00
|
|
|
_TimeoutAlert.__init__(self, timeout, _('Continue'), **kwargs)
|
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
|
|
|
|
2007-10-25 14:04:04 +02:00
|
|
|
|
2015-10-16 05:06:51 +02:00
|
|
|
class NotifyAlert(_TimeoutAlert):
|
2008-09-21 16:01:48 +02:00
|
|
|
"""
|
2017-07-19 09:31:09 +02:00
|
|
|
A timed alert with one button; Ok. The button contains a
|
|
|
|
countdown of seconds remaining.
|
|
|
|
|
|
|
|
When the button is clicked, the :class:`NotifyAlert` will
|
|
|
|
emit a `response` signal with a response identifier
|
|
|
|
:class:`Gtk.ResponseType.OK`.
|
|
|
|
|
|
|
|
If the countdown reaches zero before the button is clicked, the
|
|
|
|
:class:`NotifyAlert` will emit a `response` signal with a
|
|
|
|
response identifier of -1.
|
2008-09-21 16:01:48 +02:00
|
|
|
|
2015-08-02 00:47:22 +02:00
|
|
|
Args:
|
2017-07-19 09:31:09 +02:00
|
|
|
timeout (int, optional): time in seconds, default 5
|
|
|
|
**kwargs: parameters 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
|
|
|
|
|
2017-07-19 09:31:09 +02:00
|
|
|
# create a Notify alert 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')
|
2017-07-19 09:31:09 +02:00
|
|
|
alert.props.msg = _('Text message of alert goes here')
|
2008-09-21 16:01:48 +02:00
|
|
|
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):
|
2017-07-19 09:31:09 +02:00
|
|
|
# Remove the alert
|
2015-08-02 00:47:22 +02:00
|
|
|
self.remove_alert(alert)
|
|
|
|
|
2017-07-19 09:31:09 +02:00
|
|
|
# Check the response identifier.
|
|
|
|
if response_id is Gtk.ResponseType.OK:
|
|
|
|
print 'Ok Button was clicked.'
|
|
|
|
elif response_id == -1:
|
|
|
|
print 'Timeout occurred'
|
2008-09-21 16:01:48 +02:00
|
|
|
"""
|
2007-10-25 14:04:04 +02:00
|
|
|
|
|
|
|
def __init__(self, timeout=5, **kwargs):
|
2015-10-16 05:06:51 +02:00
|
|
|
_TimeoutAlert.__init__(self, timeout, _('Ok'), **kwargs)
|