2017-11-09 19:01:09 +03:00
|
|
|
""" Common module for showing dialogs """
|
2018-03-06 11:34:06 +03:00
|
|
|
import locale
|
2017-12-09 16:25:54 +03:00
|
|
|
from enum import Enum
|
2019-05-09 14:48:29 +03:00
|
|
|
from functools import lru_cache
|
2017-12-09 16:25:54 +03:00
|
|
|
|
2018-02-18 17:14:02 +03:00
|
|
|
from app.commons import run_idle
|
2019-05-09 14:48:29 +03:00
|
|
|
from .uicommons import Gtk, UI_RESOURCES_PATH, TEXT_DOMAIN, IS_GNOME_SESSION
|
2019-05-08 23:05:32 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class Dialog(Enum):
|
2019-05-08 23:35:42 +03:00
|
|
|
MESSAGE = """
|
|
|
|
|
<?xml version="1.0" encoding="UTF-8"?>
|
|
|
|
|
<interface>
|
2019-05-09 11:11:54 +03:00
|
|
|
<requires lib="gtk+" version="3.16"/>
|
2019-05-08 23:35:42 +03:00
|
|
|
<object class="GtkMessageDialog" id="message_dialog">
|
|
|
|
|
<property name="use-header-bar">{use_header}</property>
|
|
|
|
|
<property name="can_focus">False</property>
|
|
|
|
|
<property name="modal">True</property>
|
|
|
|
|
<property name="default_width">320</property>
|
|
|
|
|
<property name="destroy_with_parent">True</property>
|
|
|
|
|
<property name="type_hint">dialog</property>
|
2019-05-09 11:11:54 +03:00
|
|
|
<property name="skip_taskbar_hint">True</property>
|
|
|
|
|
<property name="skip_pager_hint">True</property>
|
|
|
|
|
<property name="gravity">center</property>
|
|
|
|
|
<property name="message_type">{message_type}</property>
|
|
|
|
|
<property name="buttons">{buttons_type}</property>
|
2019-05-08 23:35:42 +03:00
|
|
|
</object>
|
|
|
|
|
</interface>
|
|
|
|
|
"""
|
2019-05-04 11:21:20 +03:00
|
|
|
|
2017-11-09 19:01:09 +03:00
|
|
|
|
2018-03-11 21:52:10 +03:00
|
|
|
class Action(Enum):
|
|
|
|
|
EDIT = 0
|
|
|
|
|
ADD = 1
|
|
|
|
|
|
|
|
|
|
|
2017-12-09 16:25:54 +03:00
|
|
|
class DialogType(Enum):
|
2019-05-09 11:11:54 +03:00
|
|
|
INPUT = "input"
|
|
|
|
|
CHOOSER = "chooser"
|
|
|
|
|
ERROR = "error"
|
|
|
|
|
QUESTION = "question"
|
|
|
|
|
INFO = "info"
|
2019-05-09 12:53:11 +03:00
|
|
|
ABOUT = "about"
|
|
|
|
|
WAIT = "wait"
|
2019-05-09 11:11:54 +03:00
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
|
return self.value
|
2018-02-18 17:14:02 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class WaitDialog:
|
2018-04-09 21:28:19 +03:00
|
|
|
def __init__(self, transient, text=None):
|
2018-02-18 17:14:02 +03:00
|
|
|
builder, dialog = get_dialog_from_xml(DialogType.WAIT, transient)
|
|
|
|
|
self._dialog = dialog
|
|
|
|
|
self._dialog.set_transient_for(transient)
|
2018-04-09 21:28:19 +03:00
|
|
|
if text is not None:
|
|
|
|
|
builder.get_object("wait_dialog_label").set_text(text)
|
2018-02-18 17:14:02 +03:00
|
|
|
|
|
|
|
|
def show(self):
|
|
|
|
|
self._dialog.show()
|
|
|
|
|
|
|
|
|
|
@run_idle
|
|
|
|
|
def hide(self):
|
|
|
|
|
self._dialog.hide()
|
2017-12-09 16:25:54 +03:00
|
|
|
|
2018-04-09 21:28:19 +03:00
|
|
|
@run_idle
|
|
|
|
|
def destroy(self):
|
|
|
|
|
self._dialog.destroy()
|
|
|
|
|
|
2017-12-09 16:25:54 +03:00
|
|
|
|
2019-12-13 13:31:07 +03:00
|
|
|
def show_dialog(dialog_type: DialogType, transient, text=None, settings=None, action_type=None, file_filter=None):
|
2017-11-09 19:01:09 +03:00
|
|
|
""" Shows dialogs by name """
|
2019-05-09 11:11:54 +03:00
|
|
|
if dialog_type in (DialogType.INFO, DialogType.ERROR):
|
|
|
|
|
return get_message_dialog(transient, dialog_type, Gtk.ButtonsType.OK, text)
|
2019-12-13 13:31:07 +03:00
|
|
|
elif dialog_type is DialogType.CHOOSER and settings:
|
|
|
|
|
return get_file_chooser_dialog(transient, text, settings, action_type, file_filter)
|
2019-05-04 11:21:20 +03:00
|
|
|
elif dialog_type is DialogType.INPUT:
|
|
|
|
|
return get_input_dialog(transient, text)
|
2019-05-08 23:35:42 +03:00
|
|
|
elif dialog_type is DialogType.QUESTION:
|
2019-06-15 21:43:24 +03:00
|
|
|
return get_message_dialog(transient, DialogType.QUESTION, Gtk.ButtonsType.OK_CANCEL, text or "Are you sure?")
|
2019-05-09 12:53:11 +03:00
|
|
|
elif dialog_type is DialogType.ABOUT:
|
|
|
|
|
return get_about_dialog(transient)
|
2017-11-09 19:01:09 +03:00
|
|
|
|
2017-12-30 21:51:57 +03:00
|
|
|
|
2019-12-13 13:31:07 +03:00
|
|
|
def get_chooser_dialog(transient, settings, pattern, name):
|
2019-05-04 11:21:20 +03:00
|
|
|
file_filter = Gtk.FileFilter()
|
|
|
|
|
file_filter.add_pattern(pattern)
|
|
|
|
|
file_filter.set_name(name)
|
|
|
|
|
|
|
|
|
|
return show_dialog(dialog_type=DialogType.CHOOSER,
|
|
|
|
|
transient=transient,
|
2019-12-13 13:31:07 +03:00
|
|
|
settings=settings,
|
2019-05-04 11:21:20 +03:00
|
|
|
action_type=Gtk.FileChooserAction.OPEN,
|
|
|
|
|
file_filter=file_filter)
|
2017-12-09 16:25:54 +03:00
|
|
|
|
|
|
|
|
|
2019-12-13 13:31:07 +03:00
|
|
|
def get_file_chooser_dialog(transient, text, settings, action_type, file_filter):
|
2019-05-04 11:21:20 +03:00
|
|
|
dialog = Gtk.FileChooserDialog(get_message(text) if text else "", transient,
|
|
|
|
|
action_type if action_type is not None else Gtk.FileChooserAction.SELECT_FOLDER,
|
|
|
|
|
(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL, Gtk.STOCK_OPEN, Gtk.ResponseType.OK),
|
2019-05-09 14:48:29 +03:00
|
|
|
use_header_bar=IS_GNOME_SESSION)
|
2019-05-04 11:21:20 +03:00
|
|
|
if file_filter is not None:
|
|
|
|
|
dialog.add_filter(file_filter)
|
|
|
|
|
|
2019-12-13 13:31:07 +03:00
|
|
|
path = settings.data_dir_path
|
2019-05-04 11:21:20 +03:00
|
|
|
dialog.set_current_folder(path)
|
|
|
|
|
response = dialog.run()
|
|
|
|
|
if response == Gtk.ResponseType.OK:
|
|
|
|
|
if dialog.get_filename():
|
|
|
|
|
path = dialog.get_filename()
|
|
|
|
|
if action_type is not Gtk.FileChooserAction.OPEN:
|
|
|
|
|
path = path + "/"
|
2017-12-09 16:25:54 +03:00
|
|
|
response = path
|
2019-05-04 11:21:20 +03:00
|
|
|
dialog.destroy()
|
2017-12-09 16:25:54 +03:00
|
|
|
|
2019-05-04 11:21:20 +03:00
|
|
|
return response
|
2017-11-09 19:01:09 +03:00
|
|
|
|
2017-12-09 16:25:54 +03:00
|
|
|
|
2019-05-04 11:21:20 +03:00
|
|
|
def get_input_dialog(transient, text):
|
2019-05-11 00:09:20 +03:00
|
|
|
builder, dialog = get_dialog_from_xml(DialogType.INPUT, transient, use_header=IS_GNOME_SESSION)
|
2019-05-04 11:21:20 +03:00
|
|
|
entry = builder.get_object("input_entry")
|
|
|
|
|
entry.set_text(text if text else "")
|
2017-11-09 19:01:09 +03:00
|
|
|
response = dialog.run()
|
2019-05-04 11:21:20 +03:00
|
|
|
txt = entry.get_text()
|
2018-03-13 10:42:56 +03:00
|
|
|
dialog.destroy()
|
2017-11-09 19:01:09 +03:00
|
|
|
|
2019-05-04 11:21:20 +03:00
|
|
|
return txt if response == Gtk.ResponseType.OK else Gtk.ResponseType.CANCEL
|
2017-11-09 19:01:09 +03:00
|
|
|
|
|
|
|
|
|
2019-05-09 11:11:54 +03:00
|
|
|
def get_message_dialog(transient, message_type, buttons_type, text):
|
2019-05-08 23:35:42 +03:00
|
|
|
builder = Gtk.Builder()
|
2019-05-09 00:01:49 +03:00
|
|
|
builder.set_translation_domain(TEXT_DOMAIN)
|
2019-05-11 00:09:20 +03:00
|
|
|
dialog_str = Dialog.MESSAGE.value.format(use_header=0, message_type=message_type, buttons_type=int(buttons_type))
|
|
|
|
|
builder.add_from_string(dialog_str)
|
2019-05-08 23:35:42 +03:00
|
|
|
dialog = builder.get_object("message_dialog")
|
|
|
|
|
dialog.set_transient_for(transient)
|
2019-05-11 00:09:20 +03:00
|
|
|
dialog.set_markup(get_message(text))
|
2019-05-08 23:35:42 +03:00
|
|
|
response = dialog.run()
|
|
|
|
|
dialog.destroy()
|
2019-05-09 12:53:11 +03:00
|
|
|
|
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_about_dialog(transient):
|
|
|
|
|
builder, dialog = get_dialog_from_xml(DialogType.ABOUT, transient)
|
|
|
|
|
dialog.set_transient_for(transient)
|
|
|
|
|
response = dialog.run()
|
|
|
|
|
dialog.destroy()
|
|
|
|
|
|
2019-05-08 23:35:42 +03:00
|
|
|
return response
|
|
|
|
|
|
|
|
|
|
|
2019-05-11 00:09:20 +03:00
|
|
|
def get_dialog_from_xml(dialog_type, transient, use_header=0, title=""):
|
2019-05-09 12:53:11 +03:00
|
|
|
dialog_name = dialog_type.value + "_dialog"
|
2018-02-18 17:14:02 +03:00
|
|
|
builder = Gtk.Builder()
|
2018-03-02 17:06:53 +03:00
|
|
|
builder.set_translation_domain(TEXT_DOMAIN)
|
2019-05-11 00:09:20 +03:00
|
|
|
dialog_str = get_dialogs_string(UI_RESOURCES_PATH + "dialogs.glade").format(use_header=use_header, title=title)
|
|
|
|
|
builder.add_objects_from_string(dialog_str, (dialog_name,))
|
2019-05-09 12:53:11 +03:00
|
|
|
dialog = builder.get_object(dialog_name)
|
2018-02-18 17:14:02 +03:00
|
|
|
dialog.set_transient_for(transient)
|
2018-03-13 10:42:56 +03:00
|
|
|
|
2018-02-18 17:14:02 +03:00
|
|
|
return builder, dialog
|
|
|
|
|
|
|
|
|
|
|
2018-03-06 11:34:06 +03:00
|
|
|
def get_message(message):
|
|
|
|
|
""" returns translated message """
|
|
|
|
|
return locale.dgettext(TEXT_DOMAIN, message)
|
|
|
|
|
|
|
|
|
|
|
2019-05-09 14:48:29 +03:00
|
|
|
@lru_cache(maxsize=5)
|
|
|
|
|
def get_dialogs_string(path):
|
|
|
|
|
with open(path, "r") as f:
|
|
|
|
|
return "".join(f)
|
|
|
|
|
|
|
|
|
|
|
2017-11-09 19:01:09 +03:00
|
|
|
if __name__ == "__main__":
|
|
|
|
|
pass
|