Skip to content

Commit

Permalink
Added a ListProperty and a ListEditor for editing list properties.
Browse files Browse the repository at this point in the history
  • Loading branch information
Beliar83 committed Apr 17, 2015
1 parent 812312f commit f8c6510
Show file tree
Hide file tree
Showing 4 changed files with 218 additions and 11 deletions.
13 changes: 7 additions & 6 deletions editor/editor_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.

#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

Expand Down Expand Up @@ -41,7 +41,7 @@
from .basic_toolbar import BasicToolbar
from .property_editor import PropertyEditor
from .property import (ComboProperty, Point3DProperty, PointProperty,
TextProperty, ToggleProperty)
TextProperty, ToggleProperty, ListProperty)


class EditorGui(object):
Expand Down Expand Up @@ -167,13 +167,14 @@ def __init__(self, app):
)
property_editor_size = PyCEGUI.USize(PyCEGUI.UDim(1.0, 0),
PyCEGUI.UDim(0.780, 0))
self.property_editor = PropertyEditor(right_area_container)
self.property_editor = PropertyEditor(right_area_container, self.app)
self.property_editor.set_size(property_editor_size)
self.property_editor.add_property_type(TextProperty)
self.property_editor.add_property_type(PointProperty)
self.property_editor.add_property_type(Point3DProperty)
self.property_editor.add_property_type(ComboProperty)
self.property_editor.add_property_type(ToggleProperty)
self.property_editor.add_property_type(ListProperty)
self.property_editor.add_value_changed_callback(self.cb_value_changed)

cegui_system.getDefaultGUIContext().setRootWindow(
Expand Down Expand Up @@ -410,10 +411,10 @@ def update_property_editor(self):
comp_name, field,
[pos])
else:
str_val = yaml.dump(value).split('\n')[0]
# str_val = yaml.dump(value).split('\n')[0]
property_editor.add_property(
comp_name, field,
[str_val])
[value])
else:
property_editor.add_property(
"Instance", "Identifier",
Expand Down
149 changes: 149 additions & 0 deletions editor/list_editor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
# -*- coding: utf-8 -*-
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

"""Contains an editor for editing lists
.. module:: list_editor
:synopsis: Editor for editing lists
.. moduleauthor:: Karsten Bock <[email protected]>
"""
from copy import copy

import PyCEGUI

from .dialog import Dialog


class ListEditor(Dialog):

"""Editor for editing lists"""

def __init__(self, app, _list=None):
Dialog.__init__(self, app)
self.list = _list or []
self.edit_list = None
self.text_input = None
self.add_button = None
self.delete_button = None
self.items = []
self.values = []

def setup_dialog(self, root):
"""Sets up the dialog windows
Args:
root: The root window to which the windows should be added
"""
font = root.getFont()
text_height = font.getFontHeight() + 2
self.window.setArea(PyCEGUI.UDim(0, 3), PyCEGUI.UDim(0, 4),
PyCEGUI.UDim(0.4, 3), PyCEGUI.UDim(0.5, 4))
self.window.setMinSize(PyCEGUI.USize(PyCEGUI.UDim(0.4, 3),
PyCEGUI.UDim(0.5, 4)))
self.window.setText(_("Edit List"))

list_size = PyCEGUI.USize(PyCEGUI.UDim(1.0, 0), PyCEGUI.UDim(0.8, 0))
edit_list = root.createChild("TaharezLook/ItemListbox",
"EditList")
edit_list.setSize(list_size)
edit_list.subscribeEvent(PyCEGUI.ItemListbox.EventSelectionChanged,
self.cb_edit_list_changed)
edit_list.resetList()
self.items = []
self.values = []
for value in self.list:
item = edit_list.createChild("TaharezLook/ListboxItem")
item.setText(value)
self.items.append(item)
self.values.append(value)
edit_list.performChildWindowLayout()
self.edit_list = edit_list
text_input = root.createChild("TaharezLook/Editbox", "text_input")
text_input.setHeight(PyCEGUI.UDim(0.0, text_height))
text_input.setWidth(PyCEGUI.UDim(1.0, 0))
text_input.subscribeEvent(PyCEGUI.Editbox.EventTextChanged,
self.cb_text_changed)
self.text_input = text_input

buttons_layout = root.createChild("HorizontalLayoutContainer",
"buttons_layout")
buttons_layout.setHorizontalAlignment(PyCEGUI.HA_CENTRE)
add_button = buttons_layout.createChild("TaharezLook/Button",
"add_button")
add_button.setText("Add")
add_button.setHeight(PyCEGUI.UDim(0.0, text_height))
add_button.setEnabled(False)
add_button.subscribeEvent(PyCEGUI.ButtonBase.EventMouseClick,
self.cb_add_clicked)
self.add_button = add_button
delete_button = buttons_layout.createChild("TaharezLook/Button",
"delete_button")
delete_button.setText("Delete")
delete_button.setHeight(PyCEGUI.UDim(0.0, text_height))
delete_button.setEnabled(False)
delete_button.subscribeEvent(PyCEGUI.ButtonBase.EventMouseClick,
self.cb_delete_clicked)
self.delete_button = delete_button

def cb_edit_list_changed(self, args):
"""Called when something in the list was changed
Args:
args: PyCEGUI.WindowEventArgs
"""
self.delete_button.setEnabled(args.window.getSelectedCount() > 0)

def cb_text_changed(self, args):
"""Called when the editbox was changed
Args:
args: PyCEGUI.WindowEventArgs
"""
self.add_button.setEnabled(len(args.window.getText()) > 0)

def cb_add_clicked(self, args):
"""Called when the add button was clicked
Args:
args: PyCEGUI.WindowEventArgs
"""
value = self.text_input.getText()
item = self.edit_list.createChild("TaharezLook/ListboxItem")
item.setText(value)
self.edit_list.performChildWindowLayout()
self.items.append(item)
self.values.append(value)

def cb_delete_clicked(self, args):
"""Called when the delete button was clicked
Args:
args: PyCEGUI.WindowEventArgs
"""
item = self.edit_list.getFirstSelectedItem()
self.values.remove(item.getText())
self.edit_list.removeItem(item)

def get_values(self):
return {"items": copy(self.values)}

def validate(self):
"""Check if the current state of the dialog fields is valid"""
return True
64 changes: 60 additions & 4 deletions editor/property.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"""

from abc import ABCMeta, abstractmethod
from .list_editor import ListEditor

import PyCEGUI

Expand Down Expand Up @@ -436,13 +437,68 @@ def cb_value_changed(self, args):
Args:
section: The section the edit box belongs to
property: The property the editbox belongs to
args: PyCEGUI event args
"""
window = args.window
new_value = unicode(window.getText())
window.setTooltipText(new_value)
self.editor.send_value_changed(self.section, self.name, new_value)


class ListProperty(BaseProperty):

"""Class for a list property"""

@classmethod
def check_type(cls, value_data):
"""Checks if the value_data is of the type this class is for
Args:
value_data: The value_data to check
Returns:
True if the property can handle the type, False if not
"""
if len(value_data) != 1:
return False
return isinstance(value_data[0], list)

def setup_widget(self, root, y_pos):
"""Sets up the widget for this property
Args:
root: The root widget to which to add the widget to
y_pos: The vertical position of the widget
"""
base_text, container = self._create_base_widget(root, y_pos)
property_edit = container.createChild(
"TaharezLook/Editbox", "%s_edit" % (base_text))
property_edit.setWidth(PyCEGUI.UDim(0.49, 0))
property_edit.setHeight(self.editor.WIDGET_HEIGHT)
property_edit.setText("(list)")
property_edit.setTooltipText("(list)")
property_edit.setReadOnly(True)

property_edit.subscribeEvent(
PyCEGUI.Editbox.EventMouseClick,
self.cb_mouse_clicked)

def cb_mouse_clicked(self, args):
"""Called when the text value of a widget was changed
Args:
args: PyCEGUI event args
"""
dialog = ListEditor(self.editor.app, self.value_data[0])
dialog.show_modal(self.editor.app.editor_gui.editor_window,
self.editor.app.engine.pump)
if not dialog.return_value:
return
values = dialog.get_values()
self.editor.send_value_changed(self.section, self.name,
values["items"])
3 changes: 2 additions & 1 deletion editor/property_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ class PropertyEditor(object):
WIDGET_HEIGHT = PyCEGUI.UDim(0.05, 0)
WIDGET_MARGIN = PyCEGUI.UDim(0.01, 0)

def __init__(self, root):
def __init__(self, root, app):
self.app = app
size = PyCEGUI.USize(PyCEGUI.UDim(1.0, 0),
PyCEGUI.UDim(1.0, 0))
self.properties_box = root.createChild(
Expand Down

0 comments on commit f8c6510

Please sign in to comment.