small decoupling of dialogs

This commit is contained in:
DYefremov
2019-05-04 11:21:20 +03:00
parent 3aa29a788d
commit 7c55692c99
2 changed files with 75 additions and 142 deletions

View File

@@ -111,28 +111,24 @@ Author: Dmitriy Yefremov
</child>
</object>
<object class="GtkDialog" id="input_dialog">
<property name="width_request">320</property>
<property name="can_focus">False</property>
<property name="title"> </property>
<property name="resizable">False</property>
<property name="modal">True</property>
<property name="destroy_with_parent">True</property>
<property name="icon_name">gtk-edit</property>
<property name="title" translatable="yes"> </property>
<property name="type_hint">dialog</property>
<child type="titlebar">
<child>
<placeholder/>
</child>
<child internal-child="vbox">
<object class="GtkBox" id="input_dialog_vbox">
<property name="width_request">320</property>
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<property name="spacing">2</property>
<child internal-child="action_area">
<object class="GtkButtonBox" id="dialog-action_area2">
<object class="GtkButtonBox">
<property name="can_focus">False</property>
<property name="layout_style">end</property>
<child>
<object class="GtkButton" id="button3">
<object class="GtkButton" id="input_dialog_cancel_button">
<property name="label">gtk-undo</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
@@ -146,7 +142,7 @@ Author: Dmitriy Yefremov
</packing>
</child>
<child>
<object class="GtkButton" id="button4">
<object class="GtkButton" id="input_dialog_ok_button">
<property name="label">gtk-ok</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
@@ -167,29 +163,17 @@ Author: Dmitriy Yefremov
</packing>
</child>
<child>
<object class="GtkBox" id="input_dialog_box">
<object class="GtkEntry" id="input_entry">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="can_focus">True</property>
<property name="margin_left">5</property>
<property name="margin_right">5</property>
<property name="margin_top">5</property>
<property name="orientation">vertical</property>
<property name="spacing">5</property>
<child>
<object class="GtkEntry" id="input_entry">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="primary_icon_stock">gtk-edit</property>
<property name="primary_icon_activatable">False</property>
<property name="secondary_icon_activatable">False</property>
<property name="secondary_icon_sensitive">False</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
<property name="margin_bottom">5</property>
<property name="primary_icon_stock">gtk-edit</property>
<property name="primary_icon_activatable">False</property>
<property name="secondary_icon_activatable">False</property>
<property name="secondary_icon_sensitive">False</property>
</object>
<packing>
<property name="expand">False</property>
@@ -200,71 +184,8 @@ Author: Dmitriy Yefremov
</object>
</child>
<action-widgets>
<action-widget response="-6">button3</action-widget>
<action-widget response="-5">button4</action-widget>
</action-widgets>
</object>
<object class="GtkFileChooserDialog" id="path_chooser_dialog">
<property name="can_focus">False</property>
<property name="title" translatable="yes"> </property>
<property name="modal">True</property>
<property name="destroy_with_parent">True</property>
<property name="icon_name">document-open</property>
<property name="type_hint">dialog</property>
<property name="action">select-folder</property>
<property name="do_overwrite_confirmation">True</property>
<child>
<placeholder/>
</child>
<child internal-child="vbox">
<object class="GtkBox" id="filechooser_dialog_vbox">
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<property name="spacing">2</property>
<child internal-child="action_area">
<object class="GtkButtonBox" id="filechooser_dialog_action_area">
<property name="can_focus">False</property>
<property name="layout_style">end</property>
<child>
<object class="GtkButton" id="button2">
<property name="label">gtk-undo</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="use_stock">True</property>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkButton" id="button1">
<property name="label">gtk-ok</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="use_stock">True</property>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="position">0</property>
</packing>
</child>
</object>
</child>
<action-widgets>
<action-widget response="-6">button2</action-widget>
<action-widget response="-12">button1</action-widget>
<action-widget response="-6">input_dialog_cancel_button</action-widget>
<action-widget response="-5">input_dialog_ok_button</action-widget>
</action-widgets>
</object>
<object class="GtkMessageDialog" id="question_dialog">
@@ -316,7 +237,7 @@ Author: Dmitriy Yefremov
<placeholder/>
</child>
<child internal-child="vbox">
<object class="GtkBox" id="dialog-vbox4">
<object class="GtkBox" id="wait_dialog_vbox">
<property name="width_request">118</property>
<property name="can_focus">False</property>
<property name="orientation">vertical</property>

View File

@@ -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)