Moved EditFormDialog to ui.common

This commit is contained in:
Sumner Evans
2019-07-20 09:46:56 -06:00
parent ba6620495b
commit 54134ad144
5 changed files with 110 additions and 100 deletions

View File

@@ -0,0 +1 @@
from .edit_form_dialog import EditFormDialog

View File

@@ -0,0 +1,94 @@
from typing import List, Tuple
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
class EditFormDialog(Gtk.Dialog):
entity_name: str
initial_size: Tuple[int, int]
text_fields: List[Tuple[str, str, bool]] = []
boolean_fields: List[Tuple[str, str]] = []
extra_buttons: List[Gtk.Button] = []
def get_object_name(self, obj):
"""
Gets the friendly object name. Can be overridden.
"""
return obj.name if obj else ''
def get_default_object(self):
return None
def __init__(self, parent, existing_object=None):
editing = existing_object is not None
Gtk.Dialog.__init__(
self,
f'Edit {self.get_object_name(existing_object)}'
if editing else f'Create New {self.entity_name}',
parent,
0,
(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
Gtk.STOCK_EDIT if editing else Gtk.STOCK_ADD,
Gtk.ResponseType.OK),
)
if not existing_object:
existing_object = self.get_default_object()
self.set_default_size(*self.initial_size)
# Store a map of field label to GTK component.
self.data = {}
content_area = self.get_content_area()
content_grid = Gtk.Grid(
column_spacing=10,
row_spacing=5,
margin_left=10,
margin_right=10,
)
# Add the text entries to the content area.
i = 0
for label, value_field_name, is_password in self.text_fields:
entry_label = Gtk.Label(label=label + ':')
entry_label.set_halign(Gtk.Align.START)
content_grid.attach(entry_label, 0, i, 1, 1)
entry = Gtk.Entry(
text=getattr(existing_object, value_field_name, ''),
hexpand=True,
)
if is_password:
entry.set_visibility(False)
content_grid.attach(entry, 1, i, 1, 1)
self.data[label] = entry
i += 1
# Add the boolean entries to the content area.
for label, value_field_name in self.boolean_fields:
entry_label = Gtk.Label(label=label + ':')
entry_label.set_halign(Gtk.Align.START)
content_grid.attach(entry_label, 0, i, 1, 1)
# Put the checkbox in the right box. Note we have to pad here
# since the checkboxes are smaller than the text fields.
checkbox = Gtk.CheckButton(
active=getattr(existing_object, value_field_name, False))
self.data[label] = checkbox
content_grid.attach(checkbox, 1, i, 1, 1)
i += 1
content_area.pack_start(content_grid, True, True, 10)
# Create a box for buttons.
if len(self.extra_buttons) > 0:
button_box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL)
for button in self.extra_buttons:
button_box.pack_start(button, False, True, 5)
content_area.pack_start(button_box, True, True, 10)
self.show_all()

View File

@@ -5,10 +5,10 @@ from gi.repository import Gtk, GObject
from libremsonic.server import Server
from libremsonic.config import ServerConfiguration
from libremsonic.ui import util
from libremsonic.ui.common import EditFormDialog
class EditServerDialog(util.EditFormDialog):
class EditServerDialog(EditFormDialog):
entity_name: str = 'Server'
initial_size = (450, 250)
text_fields = [
@@ -97,13 +97,15 @@ class ConfigureServersDialog(Gtk.Dialog):
# Add all of the buttons to the button box.
self.buttons = [
# TODO get good icons for these
(Gtk.Button('Edit...'), lambda e: self.on_edit_clicked(e, False),
'start', True),
(Gtk.Button('Add...'), lambda e: self.on_edit_clicked(e, True),
'start', False),
(Gtk.Button('Remove'), self.on_remove_clicked, 'start', True),
(Gtk.Button('Close'), lambda _: self.close(), 'end', False),
(Gtk.Button('Connect'), self.on_connect_clicked, 'end', True),
(Gtk.Button(label='Edit...'),
lambda e: self.on_edit_clicked(e, False), 'start', True),
(Gtk.Button(label='Add...'),
lambda e: self.on_edit_clicked(e, True), 'start', False),
(Gtk.Button(label='Remove'), self.on_remove_clicked, 'start',
True),
(Gtk.Button(label='Close'), lambda _: self.close(), 'end', False),
(Gtk.Button(label='Connect'), self.on_connect_clicked, 'end',
True),
]
for button_cfg in self.buttons:
btn, action, pack_end, requires_selection = button_cfg
@@ -133,7 +135,7 @@ class ConfigureServersDialog(Gtk.Dialog):
# Add all of the rows for each of the servers.
for config in self.server_configs:
row = Gtk.ListBoxRow()
server_name_label = Gtk.Label(config.name)
server_name_label = Gtk.Label(label=config.name)
server_name_label.set_halign(Gtk.Align.START)
row.add(server_name_label)
self.server_list.add(row)

View File

@@ -13,9 +13,10 @@ from libremsonic.server.api_objects import Child, PlaylistWithSongs
from libremsonic.state_manager import ApplicationState
from libremsonic.cache_manager import CacheManager, SongCacheStatus
from libremsonic.ui import util
from libremsonic.ui.common import EditFormDialog
class EditPlaylistDialog(util.EditFormDialog):
class EditPlaylistDialog(EditFormDialog):
__gsignals__ = {
'delete-playlist': (GObject.SignalFlags.RUN_FIRST, GObject.TYPE_NONE,
()),

View File

@@ -1,5 +1,5 @@
import functools
from typing import Callable, List, Tuple, Any, Optional
from typing import Callable, List, Tuple, Any
from concurrent.futures import Future
@@ -174,93 +174,6 @@ def show_song_popover(
popover.show_all()
class EditFormDialog(Gtk.Dialog):
entity_name: str
initial_size: Tuple[int, int]
text_fields: List[Tuple[str, str, bool]] = []
boolean_fields: List[Tuple[str, str]] = []
extra_buttons: List[Gtk.Button] = []
def get_object_name(self, obj):
"""
Gets the friendly object name. Can be overridden.
"""
return obj.name if obj else ''
def get_default_object(self):
return None
def __init__(self, parent, existing_object=None):
editing = existing_object is not None
Gtk.Dialog.__init__(
self,
f'Edit {self.get_object_name(existing_object)}'
if editing else f'Create New {self.entity_name}',
parent,
0,
(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
Gtk.STOCK_EDIT if editing else Gtk.STOCK_ADD,
Gtk.ResponseType.OK),
)
if not existing_object:
existing_object = self.get_default_object()
self.set_default_size(*self.initial_size)
if len(self.text_fields) + len(self.boolean_fields) > 0:
# Create two columns for the labels and corresponding entry fields.
content_area = self.get_content_area()
flowbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL)
label_box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
entry_box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
flowbox.pack_start(label_box, False, True, 10)
flowbox.pack_start(entry_box, True, True, 10)
# Store a map of field label to GTK component.
self.data = {}
# Create all of the text entry fields.
for label, value_field_name, is_password in self.text_fields:
# Put the label in the left box.
entry_label = Gtk.Label(label=label + ':')
entry_label.set_halign(Gtk.Align.START)
label_box.pack_start(entry_label, True, True, 0)
# Put the text entry in the right box.
entry = Gtk.Entry(
text=getattr(existing_object, value_field_name, ''))
if is_password:
entry.set_visibility(False)
entry_box.pack_start(entry, True, True, 0)
self.data[label] = entry
# Create all of the check box fields.
for label, value_field_name in self.boolean_fields:
# Put the label in the left box.
entry_label = Gtk.Label(label=label + ':')
entry_label.set_halign(Gtk.Align.START)
label_box.pack_start(entry_label, True, True, 0)
# Put the checkbox in the right box. Note we have to pad here
# since the checkboxes are smaller than the text fields.
checkbox = Gtk.CheckButton(
active=getattr(existing_object, value_field_name, False))
entry_box.pack_start(checkbox, True, True, 5)
self.data[label] = checkbox
content_area.pack_start(flowbox, True, True, 10)
# Create a box for buttons.
if len(self.extra_buttons) > 0:
button_box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL)
for button in self.extra_buttons:
button_box.pack_start(button, False, True, 5)
content_area.pack_start(button_box, True, True, 10)
self.show_all()
def async_callback(future_fn, before_download=None, on_failure=None):
"""
Defines the ``async_callback`` decorator.
@@ -273,7 +186,6 @@ def async_callback(future_fn, before_download=None, on_failure=None):
:param future_fn: a function which generates a
``concurrent.futures.Future``.
"""
def decorator(callback_fn):
@functools.wraps(callback_fn)
def wrapper(self, *args, **kwargs):