Skip to content
This repository has been archived by the owner on Jul 3, 2023. It is now read-only.

Commit

Permalink
Merge pull request fract4d#23 from cjmayo/messagedialog
Browse files Browse the repository at this point in the history
Alert Message Dialog
  • Loading branch information
edyoung committed Feb 13, 2018
2 parents 339a7f4 + 8542dbf commit a12bf54
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 133 deletions.
96 changes: 23 additions & 73 deletions fract4dgui/hig.py
Original file line number Diff line number Diff line change
@@ -1,86 +1,35 @@
# utilities to comply with Gnome Human Interface Guidelines.
# these are defined at http://developer.gnome.org/projects/gup/hig/2.0/

from gi.repository import Gtk, GObject, GLib
import xml.sax.saxutils
from gi.repository import Gtk, GLib

class Alert(Gtk.MessageDialog):
def __init__(self, **kwds):

image = kwds.get("image")
primary_text = kwds.get("primary")
secondary_text= kwds.get("secondary","")
buttontype = kwds.get("buttontype",Gtk.ButtonsType.NONE)
buttons = kwds.get("buttons",())
secondary_text = kwds.get("secondary", "")
buttontype = kwds.get("buttontype", Gtk.ButtonsType.NONE)
buttons = kwds.get("buttons", ())
parent = kwds.get("parent")
flags = kwds.get("flags",0)
title = kwds.get("title","")

self.ignore_info = kwds.get("ignore")

if not isinstance(image,Gtk.Image):
image = Gtk.Image.new_from_icon_name(image, Gtk.IconSize.DIALOG)


Gtk.MessageDialog.__init__(
self,
transient_for=parent,
modal=True,
destroy_with_parent=True,
text=title,
title=title,
text=primary_text,
buttons=buttontype)

self.set_resizable(False)
self.set_border_width(6)

self.add_buttons(*buttons)

#self.set_has_separator(False)

self.vbox.set_spacing(12)

upper_hbox = Gtk.HBox()
upper_hbox.set_spacing(12)
upper_hbox.set_border_width(6)

image.icon_size = Gtk.IconSize.DIALOG

upper_hbox.pack_start(image, True, True, 0)

if secondary_text and len(secondary_text) > 0:
secondary_text = "\n\n" + secondary_text
secondary_text = xml.sax.saxutils.escape(secondary_text)
else:
secondary_text = ""
label_text = '<span weight="bold" size="larger">%s</span>%s' % \
(primary_text, secondary_text)

label = Gtk.Label(label=label_text)
label.set_use_markup(True)
label.set_line_wrap(True)
self.set_image(image)
self.format_secondary_text(secondary_text)

upper_hbox.pack_start(label, True, True, 0)

self.vbox.pack_start(upper_hbox, True, True, 0)

if self.ignore_info:
self.dont_show_again = Gtk.CheckButton(label=_("Don't show this message again"))
self.vbox.pack_end(self.dont_show_again, True, True, 0)
self.dont_show_again.set_active(self.ignore_info.is_ignore_suggested())

def on_response(self,*args):
if self.ignore_info and self.dont_show_again.get_active():
self.ignore_info.ignore()
return False

self.connect("response",on_response)

self.show_all()

def run(self):
if self.ignore_info and self.ignore_info.is_ignored():
return self.ignore_info.response
return Gtk.Dialog.run(self)

class InformationAlert(Alert):
def __init__(self,**kwds):
Expand All @@ -90,32 +39,32 @@ def __init__(self,**kwds):

class ErrorAlert(Alert):
FIX = 1

def __init__(self, **kwds):

fix_button = kwds.get("fix_button")

# if optional fix button supplied, add to list of buttons
buttons = list(kwds.get("buttons",()))
if fix_button:
buttons = [ fix_button, ErrorAlert.FIX]
buttons = [fix_button, ErrorAlert.FIX]
buttons += [Gtk.STOCK_OK, Gtk.ResponseType.ACCEPT]

kwds["buttons"] = tuple(buttons)
kwds.setdefault("image", Gtk.STOCK_DIALOG_ERROR)
kwds.setdefault("image", Gtk.STOCK_DIALOG_ERROR)
Alert.__init__(self, **kwds)
self.set_default_response(Gtk.ResponseType.ACCEPT)

class ConfirmationAlert(Alert):
ALTERNATE=1
def __init__(self, **kwds):
ALTERNATE = 1

def __init__(self, **kwds):
proceed_button = kwds.get("proceed_button", Gtk.STOCK_OK)
alternate_button = kwds.get("alternate_button")
cancel_button = kwds.get("cancel_button", Gtk.STOCK_CANCEL)
# if optional fix button supplied, add to list of buttons
buttons = list(kwds.get("buttons", ()))
if alternate_button:
buttons = [ alternate_button, ConfirmationAlert.ALTERNATE]
buttons = [alternate_button, ConfirmationAlert.ALTERNATE]

buttons += [cancel_button, Gtk.ResponseType.CANCEL,
proceed_button, Gtk.ResponseType.ACCEPT]
Expand All @@ -139,14 +88,15 @@ def _periodText(seconds):

class SaveConfirmationAlert(ConfirmationAlert):
NOSAVE = ConfirmationAlert.ALTERNATE

def __init__(self, **kwds):
document_name = kwds["document_name"]
time_period = kwds.get("period",-1)
if time_period==-1:
if time_period == -1:
text = "If you don't save, changes will be discarded."
else:
text = ("If you don't save, changes from the past %s " + \
"will be discarded.") % _periodText(time_period)
text = ("If you don't save, changes from the past %s "
"will be discarded.") % _periodText(time_period)

kwds.setdefault("primary",_('Save changes to document "%s" before closing?') % document_name)
kwds.setdefault("secondary", text)
Expand All @@ -165,15 +115,15 @@ class MessagePopper:
def __init__(self):
pass

def show_error(self,msg, extra_message=""):
def show_error(self, msg, extra_message=""):
d = ErrorAlert(
parent=self,
primary=msg,
secondary=extra_message)

return self.do(d)

def show_info(self,msg,extra_message = ""):
def show_info(self, msg, extra_message=""):
d = InformationAlert(
parent=self,
primary=msg,
Expand All @@ -186,9 +136,9 @@ def ask_question(self, msg, secondary):
parent=self,
primary=msg,
secondary=secondary,
image = Gtk.STOCK_DIALOG_QUESTION,
proceed_button = Gtk.STOCK_YES,
cancel_button = Gtk.STOCK_NO)
image=Gtk.STOCK_DIALOG_QUESTION,
proceed_button=Gtk.STOCK_YES,
cancel_button=Gtk.STOCK_NO)

return self.do(d)

Expand Down
25 changes: 0 additions & 25 deletions fract4dgui/ignore_info.py

This file was deleted.

2 changes: 1 addition & 1 deletion fract4dgui/main_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@


from . import gtkfractal, model, preferences, autozoom, settings, toolbar
from . import undo, browser, fourway, angle, utils, hig, ignore_info, painter
from . import undo, browser, fourway, angle, utils, hig, painter
from . import icons, renderqueue, director

re_ends_with_num = re.compile(r'\d+\Z')
Expand Down
38 changes: 4 additions & 34 deletions fract4dgui/test_hig.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
#!/usr/bin/env python3

import unittest
import copy
import math
import gettext
import os

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, GObject, GLib
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, GLib

import hig

Expand All @@ -17,20 +15,6 @@

toplevel = Gtk.Window()

class MockIgnoreInfo:
def __init__(self,ignored, suggest_ignore):
self.ignored = ignored
self.suggest_ignore = suggest_ignore

def is_ignored(self):
return self.ignored

def ignore(self):
self.ignored = True

def is_ignore_suggested(self):
return self.suggest_ignore

class MockDialog(Gtk.MessageDialog,hig.MessagePopper):
def __init__(self):
Gtk.MessageDialog.__init__(
Expand Down Expand Up @@ -116,7 +100,7 @@ def dismiss():
# increase timeout to see what dialogs look like
GLib.timeout_add(10,dismiss)

r = d.run()
d.run()
d.destroy()

def testPeriodText(self):
Expand All @@ -139,21 +123,6 @@ def testSaveConfirm(self):

self.runAndDismiss(d)

def testIgnore(self):
ii = MockIgnoreInfo(False,True)

d = hig.Alert(
parent=toplevel,
image=Gtk.STOCK_DIALOG_INFO,
primary="Foo",
secondary="Bar",
buttons=(Gtk.STOCK_OK, Gtk.ResponseType.ACCEPT),
ignore=ii)

self.assertEqual(ii.is_ignored(), False)
self.runAndDismiss(d)
self.assertEqual(ii.is_ignored(), True)

def testMessagePopper(self):
dd = MockDialog()
self.assertEqual(0, hig.timeout)
Expand All @@ -166,5 +135,6 @@ def testMessagePopper(self):
def suite():
return unittest.makeSuite(Test,'test')


if __name__ == '__main__':
unittest.main(defaultTest='suite')

0 comments on commit a12bf54

Please sign in to comment.