From 7c55692c9913bf8593d37533443b9d21cd402311 Mon Sep 17 00:00:00 2001 From: DYefremov Date: Sat, 4 May 2019 11:21:20 +0300 Subject: [PATCH] small decoupling of dialogs --- app/ui/dialogs.glade | 111 +++++++------------------------------------ app/ui/dialogs.py | 106 +++++++++++++++++++++++------------------ 2 files changed, 75 insertions(+), 142 deletions(-) diff --git a/app/ui/dialogs.glade b/app/ui/dialogs.glade index efb243ec..aa063d46 100644 --- a/app/ui/dialogs.glade +++ b/app/ui/dialogs.glade @@ -111,28 +111,24 @@ Author: Dmitriy Yefremov + 320 False - - False - True - True - gtk-edit + dialog - + - 320 False vertical 2 - + False end - + gtk-undo True True @@ -146,7 +142,7 @@ Author: Dmitriy Yefremov - + gtk-ok True True @@ -167,29 +163,17 @@ Author: Dmitriy Yefremov - + True - False + True 5 5 5 - vertical - 5 - - - True - True - gtk-edit - False - False - False - - - False - True - 1 - - + 5 + gtk-edit + False + False + False False @@ -200,71 +184,8 @@ Author: Dmitriy Yefremov - button3 - button4 - - - - False - - True - True - document-open - dialog - select-folder - True - - - - - - False - vertical - 2 - - - False - end - - - gtk-undo - True - True - True - True - - - True - True - 0 - - - - - gtk-ok - True - True - True - True - - - True - True - 1 - - - - - False - False - 0 - - - - - - button2 - button1 + input_dialog_cancel_button + input_dialog_ok_button @@ -316,7 +237,7 @@ Author: Dmitriy Yefremov - + 118 False vertical diff --git a/app/ui/dialogs.py b/app/ui/dialogs.py index 6ace5ec1..151ab88b 100644 --- a/app/ui/dialogs.py +++ b/app/ui/dialogs.py @@ -1,10 +1,13 @@ """ Common module for showing dialogs """ import locale +import os from enum import Enum from app.commons import run_idle from .uicommons import Gtk, UI_RESOURCES_PATH, TEXT_DOMAIN +_IS_GNOME_SESSION = bool(os.environ.get("GNOME_DESKTOP_SESSION_ID")) + class Action(Enum): EDIT = 0 @@ -45,56 +48,19 @@ def show_dialog(dialog_type: DialogType, transient, text=None, options=None, act """ Shows dialogs by name """ if dialog_type is DialogType.INFO: return get_info_dialog(transient, text) - - builder, dialog = get_dialog_from_xml(dialog_type, transient) - - if dialog_type is DialogType.CHOOSER and options: - if action_type is not None: - dialog.set_action(action_type) - if file_filter is not None: - dialog.add_filter(file_filter) - - path = options.get("data_dir_path") - dialog.set_current_folder(path) - + elif dialog_type is DialogType.CHOOSER and options: + return get_file_chooser_dialog(transient, text, options, action_type, file_filter) + elif dialog_type is DialogType.INPUT: + return get_input_dialog(transient, text) + else: + builder, dialog = get_dialog_from_xml(dialog_type, transient) + if text: + dialog.set_markup(get_message(text)) response = dialog.run() - if response == -12: # -12 for fix assertion 'gtk_widget_get_can_default (widget)' failed - if dialog.get_filename(): - path = dialog.get_filename() - if action_type is not Gtk.FileChooserAction.OPEN: - path = path + "/" - - response = path dialog.destroy() return response - if dialog_type is DialogType.INPUT: - entry = builder.get_object("input_entry") - entry.set_text(text if text else "") - response = dialog.run() - txt = entry.get_text() - dialog.destroy() - - return txt if response == Gtk.ResponseType.OK else Gtk.ResponseType.CANCEL - - if text: - dialog.set_markup(get_message(text)) - response = dialog.run() - dialog.destroy() - - return response - - -def get_dialog_from_xml(dialog_type, transient): - builder = Gtk.Builder() - builder.set_translation_domain(TEXT_DOMAIN) - builder.add_objects_from_file(UI_RESOURCES_PATH + "dialogs.glade", (dialog_type.value,)) - dialog = builder.get_object(dialog_type.value) - dialog.set_transient_for(transient) - - return builder, dialog - def get_chooser_dialog(transient, options, pattern, name): file_filter = Gtk.FileFilter() @@ -108,13 +74,59 @@ def get_chooser_dialog(transient, options, pattern, name): file_filter=file_filter) +def get_file_chooser_dialog(transient, text, options, action_type, file_filter): + 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), + use_header_bar=_IS_GNOME_SESSION) + if file_filter is not None: + dialog.add_filter(file_filter) + + path = options.get("data_dir_path") + 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 + "/" + response = path + dialog.destroy() + + return response + + def get_info_dialog(transient, text): - dialog = Gtk.MessageDialog(parent=transient, type=Gtk.MessageType.INFO, buttons=Gtk.ButtonsType.OK) - dialog.set_markup(get_message(text)) + dialog = Gtk.MessageDialog(text=get_message(text), + parent=transient, + type=Gtk.MessageType.INFO, + buttons=Gtk.ButtonsType.OK, + use_header_bar=_IS_GNOME_SESSION) dialog.run() dialog.destroy() +def get_input_dialog(transient, text): + builder, dialog = get_dialog_from_xml(DialogType.INPUT, transient) + entry = builder.get_object("input_entry") + entry.set_text(text if text else "") + response = dialog.run() + txt = entry.get_text() + dialog.destroy() + + return txt if response == Gtk.ResponseType.OK else Gtk.ResponseType.CANCEL + + +def get_dialog_from_xml(dialog_type, transient): + builder = Gtk.Builder() + builder.set_translation_domain(TEXT_DOMAIN) + builder.add_objects_from_file(UI_RESOURCES_PATH + "dialogs.glade", (dialog_type.value,)) + dialog = builder.get_object(dialog_type.value) + dialog.set_transient_for(transient) + + return builder, dialog + + def get_message(message): """ returns translated message """ return locale.dgettext(TEXT_DOMAIN, message)