-
Benoit Favre authoredBenoit Favre authored
confirm.py 1.89 KiB
from gi.repository import GObject, Gtk
class ConfirmationBox(Gtk.HBox):
def __init__(self):
super(ConfirmationBox, self).__init__()
self.label = Gtk.Label()
self.label.get_style_context().add_class('confirm')
self.yes_button = Gtk.Button("YES")
self.no_button = Gtk.Button("NO")
self.pack_start(self.label, True, True, 5)
self.pack_start(self.yes_button, False, False, 5)
self.pack_start(self.no_button, False, False, 5)
self.yes_button.connect('clicked', self.click_yes)
self.no_button.connect('clicked', self.click_no)
self.counter = 0
self.yes_callback = None
self.no_callback = None
self.timer = None
def confirm(self, message, time, yes_callback=None, no_callback=None):
self.yes_callback = yes_callback
self.no_callback = no_callback
self.label.set_text(message)
self.counter = int(time)
self.yes_button.get_child().set_text("YES (%d)" % self.counter)
self.timer = GObject.timeout_add_seconds(1, self.countdown)
self.show()
def click_yes(self, button = None):
self.cancel_timer()
self.hide()
self.counter = 0
if self.yes_callback:
self.yes_callback()
self.yes_callback = None
def click_no(self, button = None):
self.cancel_timer()
self.hide()
self.counter = 0
if self.no_callback:
self.no_callback()
self.no_callback = None
def countdown(self):
if self.counter > 1:
self.counter -= 1
self.yes_button.get_child().set_text("YES (%d)" % self.counter)
self.timer = GObject.timeout_add_seconds(1, self.countdown)
else:
self.click_yes()
def cancel_timer(self):
if self.timer != None:
GObject.source_remove(self.timer)
self.timer = None