Skip to content

Commit

Permalink
Added an editor for dictionary properties.
Browse files Browse the repository at this point in the history
  • Loading branch information
Beliar83 committed Apr 23, 2015
1 parent f8c6510 commit dda1ad4
Show file tree
Hide file tree
Showing 4 changed files with 276 additions and 3 deletions.
210 changes: 210 additions & 0 deletions editor/dict_editor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
# -*- 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 dicts
.. module:: dict_editor
:synopsis: Editor for editing dicts
.. moduleauthor:: Karsten Bock <[email protected]>
"""
import PyCEGUI

from .dialog import Dialog


class DictEditor(Dialog):

"""Editor for editing dicts"""

def __init__(self, app, _dict=None):
Dialog.__init__(self, app)
self.dict = _dict or {}
self.edit_dict = None
self.key_input = None
self.value_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 Dict"))

dict_size = PyCEGUI.USize(PyCEGUI.UDim(1.0, 0), PyCEGUI.UDim(0.8, 0))
edit_dict = root.createChild("TaharezLook/MultiColumnList",
"EditDict")
edit_dict.setSize(dict_size)
edit_dict.subscribeEvent(PyCEGUI.MultiColumnList.EventSelectionChanged,
self.cb_edit_dict_changed)
edit_dict.resetList()
edit_dict.addColumn("Key", 0, PyCEGUI.UDim(0.4, 0))
edit_dict.addColumn("Value", 1, PyCEGUI.UDim(0.4, 0))
edit_dict.setSelectionMode(PyCEGUI.MultiColumnList.
SelectionMode.RowSingle)
self.items = []
self.values = {}
for key, value in self.dict.iteritems():
row = edit_dict.addRow()
item = PyCEGUI.ListboxTextItem(str(key))
item.setSelectionBrushImage("TaharezLook/"
"MultiListSelectionBrush")
edit_dict.setItem(item, 0, row)
self.items.append(item)
item = PyCEGUI.ListboxTextItem(str(value))
item.setSelectionBrushImage("TaharezLook/"
"MultiListSelectionBrush")
edit_dict.setItem(item, 1, row)
self.items.append(item)
self.values[key] = value
edit_dict.performChildWindowLayout()
self.edit_dict = edit_dict
input_layout = root.createChild("HorizontalLayoutContainer",
"input_layout")
key_layout = input_layout.createChild("HorizontalLayoutContainer",
"key_layout")
key_layout.setWidth(PyCEGUI.UDim(0.5, 0))
key_label = key_layout.createChild("TaharezLook/Label",
"key_label")
key_label.setProperty("HorzFormatting", "LeftAligned")
key_label.setText(_("Key"))
text_width = font.getTextExtent(key_label.getText())
key_label.setHeight(PyCEGUI.UDim(0.0, text_height))
key_label.setWidth(PyCEGUI.UDim(0.0, text_width))
key_input = key_layout.createChild("TaharezLook/Editbox",
"key_input")
key_input.setHeight(PyCEGUI.UDim(0.0, text_height))
key_input.setWidth(PyCEGUI.UDim(0.5, -text_width))
key_input.subscribeEvent(PyCEGUI.Editbox.EventTextChanged,
self.cb_text_changed)
self.key_input = key_input
value_layout = input_layout.createChild("HorizontalLayoutContainer",
"value_layout")
value_layout.setWidth(PyCEGUI.UDim(0.5, 0))
value_label = value_layout.createChild("TaharezLook/Label",
"value_label")
value_label.setProperty("HorzFormatting", "LeftAligned")
value_label.setText(_("Value"))
text_width = font.getTextExtent(value_label.getText())
value_label.setHeight(PyCEGUI.UDim(0.0, text_height))
value_label.setWidth(PyCEGUI.UDim(0.0, text_width))

value_input = value_layout.createChild("TaharezLook/Editbox",
"value_input")
value_input.setHeight(PyCEGUI.UDim(0.0, text_height))
value_input.setWidth(PyCEGUI.UDim(0.5, -text_width))
value_input.subscribeEvent(PyCEGUI.Editbox.EventTextChanged,
self.cb_text_changed)
self.value_input = value_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_dict_changed(self, args):
"""Called when something in the dict 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(self.key_input.getText()) > 0 and
len(self.value_input.getText()) > 0)

def cb_add_clicked(self, args):
"""Called when the add button was clicked
Args:
args: PyCEGUI.WindowEventArgs
"""
key = self.key_input.getText()
value = self.value_input.getText()
item = self.edit_dict.findColumnItemWithText(key, 0, None)
if item is not None:
row = self.edit_dict.getItemRowIndex(item)
pos = PyCEGUI.MCLGridRef(row, 1)
item = self.edit_dict.getItemAtGridReference(pos)
item.setText(value)
else:
row = self.edit_dict.addRow()
item = PyCEGUI.ListboxTextItem(str(key))
item.setSelectionBrushImage("TaharezLook/"
"MultiListSelectionBrush")
self.edit_dict.setItem(item, 0, row)
self.items.append(item)
item = PyCEGUI.ListboxTextItem(str(value))
item.setSelectionBrushImage("TaharezLook/"
"MultiListSelectionBrush")
self.edit_dict.setItem(item, 1, row)
self.items.append(item)
self.values[key] = value
self.edit_dict.performChildWindowLayout()
self.edit_dict.invalidate(True)

def cb_delete_clicked(self, args):
"""Called when the delete button was clicked
Args:
args: PyCEGUI.WindowEventArgs
"""
item = self.edit_dict.getFirstSelectedItem()
row = self.edit_dict.getItemRowIndex(item)
del self.values[item.getText()]
self.edit_dict.removeRow(row)

def get_values(self):
return self.values

def validate(self):
"""Check if the current state of the dialog fields is valid"""
return True
4 changes: 3 additions & 1 deletion editor/editor_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
from .basic_toolbar import BasicToolbar
from .property_editor import PropertyEditor
from .property import (ComboProperty, Point3DProperty, PointProperty,
TextProperty, ToggleProperty, ListProperty)
TextProperty, ToggleProperty, ListProperty,
DictProperty)


class EditorGui(object):
Expand Down Expand Up @@ -175,6 +176,7 @@ def __init__(self, app):
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_property_type(DictProperty)
self.property_editor.add_value_changed_callback(self.cb_value_changed)

cegui_system.getDefaultGUIContext().setRootWindow(
Expand Down
63 changes: 62 additions & 1 deletion editor/property.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@
"""

from abc import ABCMeta, abstractmethod
from .list_editor import ListEditor

import PyCEGUI

from .list_editor import ListEditor
from .dict_editor import DictEditor


class BaseProperty(object):

Expand Down Expand Up @@ -502,3 +504,62 @@ def cb_mouse_clicked(self, args):
values = dialog.get_values()
self.editor.send_value_changed(self.section, self.name,
values["items"])


class DictProperty(BaseProperty):

"""Class for a dict 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], dict)

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("(dict)")
property_edit.setTooltipText("(dict)")
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 = DictEditor(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)
2 changes: 1 addition & 1 deletion pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ variable-rgx=[a-z_][a-z0-9_]{2,50}$
inlinevar-rgx=[A-Za-z_][A-Za-z0-9_]*$

# Good variable names which should always be accepted, separated by a comma
good-names=i,j,k,ex,Run,_,TDS
good-names=x,i,j,k,ex,Run,_,TDS

# Bad variable names which should always be refused, separated by a comma
bad-names=foo,bar,baz,toto,tutu,tata
Expand Down

0 comments on commit dda1ad4

Please sign in to comment.