Skip to content

Commit

Permalink
Convert "monitors" options to strings
Browse files Browse the repository at this point in the history
  • Loading branch information
kalgasnik committed Mar 13, 2015
1 parent b04cd76 commit 30d133d
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 104 deletions.
2 changes: 1 addition & 1 deletion data/MultiheadSetupDialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@
<property name="show_border">False</property>
<property name="scrollable">True</property>
<property name="group_name">monitors</property>
<signal name="switch-page" handler="on_monitors_notebook_switch_page" swapped="no"/>
<signal name="switch-page" handler="on_switch_page" swapped="no"/>
<child>
<object class="GtkBox" id="empty_page">
<property name="visible">True</property>
Expand Down
8 changes: 5 additions & 3 deletions lightdm_gtk_greeter_settings/MonitorsGroup.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@

from gi.repository import Gtk

from lightdm_gtk_greeter_settings.helpers import WidgetsWrapper
from lightdm_gtk_greeter_settings.helpers import (
bool2string,
WidgetsWrapper)
from lightdm_gtk_greeter_settings.MultiheadSetupDialog import MultiheadSetupDialog
from lightdm_gtk_greeter_settings.OptionEntry import BaseEntry
from lightdm_gtk_greeter_settings.OptionGroup import BaseGroup
Expand Down Expand Up @@ -48,8 +50,8 @@ def read(self, config):
name = name[len(self.GROUP_PREFIX):].strip()
entry = MonitorEntry(self._widgets)
entry['background'] = section.get('background', None)
entry['user-background'] = section.getboolean('user-background', None)
entry['laptop'] = section.getboolean('laptop', None)
entry['user-background'] = bool2string(section.getboolean('user-background', None), 1)
entry['laptop'] = bool2string(section.getboolean('laptop', None), True)
self._entries[name] = entry
self.entry_added.emit(entry, name)

Expand Down
197 changes: 99 additions & 98 deletions lightdm_gtk_greeter_settings/MultiheadSetupDialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
C_ = lambda t: lightdm_gtk_greeter_settings.helpers.C_('option|multihead', t)


class PageOptions:
class MonitorConfig:
name = None
background = None
background_disabled = None
Expand Down Expand Up @@ -78,11 +78,14 @@ class Widgets(WidgetsEnum):
def init_window(self):
self._widgets = self.Widgets(builder=self.builder)

# To set size of widgets.notebook to size of widgets.editor
self.realize()

self._widgets.notebook.remove_page(self._widgets.notebook.page_num(self._widgets.editor))

self._current_page = None
self._defaults = {}
self._options = {}
self._configs = {}

self._option_name = OptionEntry.StringEntry(WidgetsWrapper(self.builder, 'name'))
self._option_bg = OptionEntry.BackgroundEntry(WidgetsWrapper(self.builder, 'background'))
Expand All @@ -109,42 +112,42 @@ def init_window(self):
item.connect('activate', self.on_add_button_clicked, name)

def set_model(self, values):
self._widgets.notebook.handler_block_by_func(self.on_monitors_notebook_switch_page)
self._widgets.notebook.handler_block_by_func(self.on_switch_page)
for page in self._widgets.notebook.get_children():
self._remove_monitor(page, update=False)
self._widgets.notebook.handler_unblock_by_func(self.on_monitors_notebook_switch_page)
self._remove_page(page, update=False)
self._widgets.notebook.handler_unblock_by_func(self.on_switch_page)

self._options.clear()
self._configs.clear()
for name, entry in values.items():
options = PageOptions()
options.name = name
options.background = entry['background']
options.user_background = entry['user-background']
options.laptop = entry['laptop']
config = MonitorConfig()
config.name = name
config.background = entry['background']
config.user_background = entry['user-background']
config.laptop = entry['laptop']

options.background_disabled = self._get_first_not_none(
options.background, self._defaults.get('background'), '')
options.user_background_disabled = self._get_first_not_none(
options.user_background, self._defaults.get('user-background'), 'true')
options.laptop_disabled = self._get_first_not_none(
options.laptop, self._defaults.get('laptop'), 'false')
config.background_disabled = self._get_first_not_none(
config.background, self._defaults.get('background'), '')
config.user_background_disabled = self._get_first_not_none(
config.user_background, self._defaults.get('user-background'), 'true')
config.laptop_disabled = self._get_first_not_none(
config.laptop, self._defaults.get('laptop'), 'false')

self._add_monitor(options)
self._add_page(config)

self._widgets.empty.props.visible = not self._options
self._widgets.empty.props.visible = not self._configs

self._update_monitors_list()

def get_model(self):
sections = []
for page in self._widgets.notebook.get_children():
options = self._options.get(page)
if not options or not options.name:
config = self._configs.get(page)
if not config or not config.name:
continue
sections.append((options.name,
{'background': options.background,
'user-background': options.user_background,
'laptop': options.laptop}))
sections.append((config.name,
{'background': config.background,
'user-background': config.user_background,
'laptop': config.laptop}))
return sections

def set_defaults(self, values):
Expand All @@ -153,109 +156,109 @@ def set_defaults(self, values):
def _get_first_not_none(self, *values, fallback=None):
return next((v for v in values if v is not None), fallback)

def _add_monitor(self, options):
def _add_page(self, config):
holder = Gtk.Box()
holder.show()

options.label = Gtk.Label(options.name)
options.error_image = Gtk.Image.new_from_icon_name('dialog-warning', Gtk.IconSize.INVALID)
options.error_image.set_pixel_size(16)
options.error_image.set_no_show_all(True)
config.label = Gtk.Label(config.name)
config.error_image = Gtk.Image.new_from_icon_name('dialog-warning', Gtk.IconSize.INVALID)
config.error_image.set_pixel_size(16)
config.error_image.set_no_show_all(True)

close_button = Gtk.Button()
close_button.set_focus_on_click(False)
close_button.set_relief(Gtk.ReliefStyle.NONE)
close_button.connect('clicked', lambda w, p: self._remove_monitor(p), holder)
close_button.connect('clicked', lambda w, p: self._remove_page(p), holder)
close_image = Gtk.Image.new_from_icon_name('stock_close', Gtk.IconSize.INVALID)
close_image.set_pixel_size(16)
close_button.add(close_image)

label_box = Gtk.Box(Gtk.Orientation.HORIZONTAL)
label_box.pack_start(options.error_image, False, False, 1)
label_box.pack_start(options.label, False, False, 3)
label_box.pack_start(config.error_image, False, False, 1)
label_box.pack_start(config.label, False, False, 3)
label_box.pack_start(close_button, False, False, 0)

label_eventbox = Gtk.EventBox()
label_eventbox.add(label_box)
label_eventbox.show_all()

self._options[holder] = options
self._configs[holder] = config

if self._widgets.empty.get_parent():
self._widgets.empty.hide()

current_idx = self._widgets.notebook.get_current_page()
current_idx = self._widgets.notebook.insert_page(holder, label_eventbox, current_idx + 1)

# self._widgets.notebook.set_tab_reorderable(holder, True)

return current_idx

def _remove_monitor(self, page, update=True):
if page not in self._options:
def _remove_page(self, page, update=True):
if not self._configs.pop(page, None):
return

self._options.pop(page)

if page == self._widgets.editor.props.parent:
page.remove(self._widgets.editor)

if update:
self._update_monitors_list()
if not self._options:
if not self._configs:
self._widgets.empty.show()

self._widgets.notebook.remove_page(self._widgets.notebook.page_num(page))

def _apply_monitor_options(self, options):
for entry, value, fallback in \
((self._option_bg, options.background, options.background_disabled),
(self._option_user_bg, options.user_background, options.user_background_disabled),
(self._option_laptop, options.laptop, options.laptop_disabled)):
entry.handler_block_by_func(self._on_option_changed)
entry.value = fallback if value is None else value
entry.enabled = value is not None
entry.handler_unblock_by_func(self._on_option_changed)
def _update_monitors_list(self):
configs = set(config.name for config in self._configs.values())
used_count = 0
self._widgets.name_combo.get_model().clear()
for name, item in self._available_monitors:
used = name in configs
if used:
used_count += 1
item.props.visible = not used
if not used:
self._widgets.name_combo.append_text(name)

self._option_name.value = options.name or ''
self._on_option_changed()
self._on_name_changed()
show_button = used_count < len(self._available_monitors)
self._widgets.name_combo.props.button_sensitivity = (Gtk.SensitivityType.ON if show_button
else Gtk.SensitivityType.OFF)
self._widgets.editor_add_menu_button.props.visible = show_button
self._widgets.empty_add_menu_button.props.visible = show_button

def _on_option_changed(self, entry=None):
options = self._options.get(self._current_page)
options.background_disabled = self._option_bg.value
options.background = options.background_disabled if self._option_bg.enabled else None
options.user_background_disabled = self._option_user_bg.value
options.user_background = (options.user_background_disabled
if self._option_user_bg.enabled else None)
options.laptop_disabled = self._option_laptop.value
options.laptop = options.laptop_disabled if self._option_laptop.enabled else None

if not options.background or Gdk.RGBA().parse(options.background):
config = self._configs.get(self._current_page)
config.background_disabled = self._option_bg.value
config.background = config.background_disabled if self._option_bg.enabled else None
config.user_background_disabled = self._option_user_bg.value
config.user_background = (config.user_background_disabled
if self._option_user_bg.enabled else None)
config.laptop_disabled = self._option_laptop.value
config.laptop = config.laptop_disabled if self._option_laptop.enabled else None

if not config.background or Gdk.RGBA().parse(config.background):
self._option_bg.error = None
else:
self._option_bg.error = check_path_accessibility(options.background)
self._option_bg.error = check_path_accessibility(config.background)

def _on_name_changed(self, entry=None):
options = self._options[self._current_page]
options.name = self._option_name.value.strip()
config = self._configs[self._current_page]
config.name = self._option_name.value.strip()
self._update_monitors_list()

markup = None
error = None

if not options.name:
if not config.name:
markup = C_('<i>No name</i>')
error = C_('The name can\'t be empty. Configuration will not be saved.')
elif any(options.name == o.name and p != self._current_page
for p, o in self._options.items()):
elif any(config.name == o.name and p != self._current_page
for p, o in self._configs.items()):
error = (C_('"{name}" is already defined. Only last configuration will be saved.')
.format(name=options.name))
.format(name=config.name))

if markup:
options.label.set_markup(markup)
config.label.set_markup(markup)
else:
options.label.set_label(options.name)
config.label.set_label(config.name)

self._option_name.error = error

Expand All @@ -264,20 +267,23 @@ def _focus_name_entry(self):
self._widgets.name.set_position(0)

def on_add_button_clicked(self, widget, name=""):
options = PageOptions()
options.name = name
config = MonitorConfig()
config.name = name

options.background_disabled = self._get_first_not_none(
config.background_disabled = self._get_first_not_none(
self._defaults.get('background'), '')
options.user_background_disabled = self._get_first_not_none(
config.user_background_disabled = self._get_first_not_none(
self._defaults.get('user-background'), 'true')
options.laptop = self._get_first_not_none(self._defaults.get('laptop'), 'false')
config.laptop_disabled = self._get_first_not_none(self._defaults.get('laptop'), 'false')

self._widgets.notebook.set_current_page(self._add_monitor(options))
self._widgets.notebook.set_current_page(self._add_page(config))
if name:
self._update_monitors_list()

def on_monitors_notebook_switch_page(self, notebook, page, page_idx):
def aaa(self, *args):
print(self._widgets.notebook.get_current_page(), args)

def on_switch_page(self, notebook, page, page_idx):
if page == self._widgets.empty:
self._current_page = None
buttons = self._widgets.editor_add_menu_button, self._widgets.empty_add_menu_button
Expand All @@ -289,27 +295,22 @@ def on_monitors_notebook_switch_page(self, notebook, page, page_idx):
if old_parent:
old_parent.remove(self._widgets.editor)
page.add(self._widgets.editor)
self._apply_monitor_options(self._options[page])

config = self._configs[page]
for entry, value, fallback in \
((self._option_bg, config.background, config.background_disabled),
(self._option_user_bg, config.user_background, config.user_background_disabled),
(self._option_laptop, config.laptop, config.laptop_disabled)):
entry.handler_block_by_func(self._on_option_changed)
entry.value = fallback if value is None else value
entry.enabled = value is not None
entry.handler_unblock_by_func(self._on_option_changed)

self._option_name.value = config.name or ''
self._on_option_changed()
self._on_name_changed()

GLib.idle_add(self._focus_name_entry)

buttons[0].props.popup = None
buttons[1].props.popup = self._widgets.available_menu

def _update_monitors_list(self):
used_monitors = set(options.name for options in self._options.values())
used_count = 0
self._widgets.name_combo.get_model().clear()
for name, item in self._available_monitors:
used = name in used_monitors
if used:
used_count += 1
item.props.visible = not used
if not used:
self._widgets.name_combo.append_text(name)

show_button = used_count < len(self._available_monitors)
self._widgets.name_combo.props.button_sensitivity = (Gtk.SensitivityType.ON if show_button
else Gtk.SensitivityType.OFF)
self._widgets.editor_add_menu_button.props.visible = show_button
self._widgets.empty_add_menu_button.props.visible = show_button
4 changes: 2 additions & 2 deletions lightdm_gtk_greeter_settings/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ def get_greeter_version():
return get_greeter_version._version


def bool2string(value):
return 'true' if value else 'false'
def bool2string(value, skip_none=False):
return 'true' if value else 'false' if not skip_none or value is not None else None


def string2bool(value, fallback=False):
Expand Down

0 comments on commit 30d133d

Please sign in to comment.