Browse Source

Python forms designer: Control creation code now generated. Control variable names now prefixed with underscore. Control properties now set correctly on loading the form into the designer.

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/branches/3.0@3916 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
shortcuts
Matt Ward 16 years ago
parent
commit
5e60ee626e
  1. 51
      src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonControlFieldExpression.cs
  2. 20
      src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonForm.cs
  3. 22
      src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonFormWalker.cs
  4. 9
      src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/GenerateTextBoxFormTestFixture.cs
  5. 10
      src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/LoadFormTestFixtureBase.cs
  6. 2
      src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/LoadSimpleFormTestFixture.cs
  7. 36
      src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/LoadTextBoxTestFixture.cs

51
src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonControlFieldExpression.cs

@ -43,6 +43,13 @@ namespace ICSharpCode.PythonBinding @@ -43,6 +43,13 @@ namespace ICSharpCode.PythonBinding
get { return fullMemberName; }
}
/// <summary>
/// From a member expression of the form: self._textBox1.Name this property will return "textBox1".
/// </summary>
public string VariableName {
get { return GetVariableNameFromSelfReference(fullMemberName); }
}
/// <summary>
/// Creates a PythonControlField from a member expression:
///
@ -55,28 +62,7 @@ namespace ICSharpCode.PythonBinding @@ -55,28 +62,7 @@ namespace ICSharpCode.PythonBinding
string fullMemberName = PythonControlFieldExpression.GetMemberName(expression);
return new PythonControlFieldExpression(memberName, fullMemberName);
}
/// <summary>
/// Gets the variable name from an expression of the form:
///
/// self._textBox1.Name
///
/// Returns "textBox1"
/// </summary>
public static string GetVariableNameFromSelfReference(string name)
{
int startIndex = name.IndexOf('.');
if (startIndex > 0) {
name = name.Substring(startIndex + 1);
int endIndex = name.IndexOf('.');
if (endIndex > 0) {
return GetVariableName(name.Substring(0, endIndex));
}
return String.Empty;
}
return name;
}
/// <summary>
/// From a name such as "System.Windows.Forms.Cursors.AppStarting" this method returns:
/// "System.Windows.Forms.Cursors"
@ -139,6 +125,27 @@ namespace ICSharpCode.PythonBinding @@ -139,6 +125,27 @@ namespace ICSharpCode.PythonBinding
}
return typeName.ToString();
}
/// <summary>
/// Gets the variable name from an expression of the form:
///
/// self._textBox1.Name
///
/// Returns "textBox1"
/// </summary>
static string GetVariableNameFromSelfReference(string name)
{
int startIndex = name.IndexOf('.');
if (startIndex > 0) {
name = name.Substring(startIndex + 1);
int endIndex = name.IndexOf('.');
if (endIndex > 0) {
return GetVariableName(name.Substring(0, endIndex));
}
return String.Empty;
}
return name;
}
}
}

20
src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonForm.cs

@ -84,11 +84,12 @@ namespace ICSharpCode.PythonBinding @@ -84,11 +84,12 @@ namespace ICSharpCode.PythonBinding
void GenerateInitializeComponentMethodBodyInternal(Form form)
{
// Add method body.
foreach (Control control in form.Controls) {
AppendControlCreation(control);
}
AppendIndentedLine("self.SuspendLayout()");
AppendForm(form);
AppendForm(form);
AppendIndentedLine("self.ResumeLayout(False)");
AppendIndentedLine("self.PerformLayout()");
}
@ -98,12 +99,16 @@ namespace ICSharpCode.PythonBinding @@ -98,12 +99,16 @@ namespace ICSharpCode.PythonBinding
/// </summary>
void AppendForm(Form form)
{
// Add the controls on the form.
foreach (Control control in form.Controls) {
AppendControl(control);
}
// Add form.
AppendControl(form, false);
// Add controls to form.
foreach (Control control in form.Controls) {
AppendIndentedLine("self.Controls.Add(self." + control.Name + ")");
}
@ -150,7 +155,7 @@ namespace ICSharpCode.PythonBinding @@ -150,7 +155,7 @@ namespace ICSharpCode.PythonBinding
if (String.IsNullOrEmpty(propertyOwnerName)) {
return "self." + propertyName;
}
return "self." + propertyOwnerName + "." + propertyName;
return "self._" + propertyOwnerName + "." + propertyName;
}
/// <summary>
@ -193,5 +198,10 @@ namespace ICSharpCode.PythonBinding @@ -193,5 +198,10 @@ namespace ICSharpCode.PythonBinding
}
codeBuilder.Append(text);
}
void AppendControlCreation(Control control)
{
AppendIndentedLine("self._" + control.Name + " = " + control.GetType().FullName + "()");
}
}
}

22
src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonFormWalker.cs

@ -111,8 +111,8 @@ namespace ICSharpCode.PythonBinding @@ -111,8 +111,8 @@ namespace ICSharpCode.PythonBinding
Type type = componentCreator.GetType(name);
if (type != null) {
List<object> args = deserializer.GetArguments(node);
object instance = componentCreator.CreateInstance(type, args, fieldExpression.MemberName, false);
if (!SetPropertyValue(form, fieldExpression.MemberName, instance)) {
object instance = componentCreator.CreateInstance(type, args, null, false);
if (!SetPropertyValue(fieldExpression.MemberName, instance)) {
AddComponent(fieldExpression.MemberName, instance);
}
} else {
@ -184,12 +184,10 @@ namespace ICSharpCode.PythonBinding @@ -184,12 +184,10 @@ namespace ICSharpCode.PythonBinding
Control GetControl(string name)
{
object o = null;
if (createdObjects.TryGetValue(name, out o)) {
return o as Control;
}
return null;
createdObjects.TryGetValue(name, out o);
return o as Control;
}
/// <summary>
/// Adds a component to the list of created objects.
/// </summary>
@ -199,12 +197,14 @@ namespace ICSharpCode.PythonBinding @@ -199,12 +197,14 @@ namespace ICSharpCode.PythonBinding
componentCreator.Add(component as IComponent, variableName);
createdObjects.Add(variableName, component);
}
/// <summary>
/// Gets the current control being walked.
/// </summary>
Control GetCurrentControl()
{
string variableName = PythonControlFieldExpression.GetVariableNameFromSelfReference(fieldExpression.FullMemberName);
if (variableName.Length > 0) {
return GetControl(variableName);
if (fieldExpression.VariableName.Length > 0) {
return GetControl(fieldExpression.VariableName);
}
return form;
}

9
src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/GenerateTextBoxFormTestFixture.cs

@ -44,14 +44,15 @@ namespace PythonBinding.Tests.Designer @@ -44,14 +44,15 @@ namespace PythonBinding.Tests.Designer
public void GeneratedCode()
{
string expectedCode = "def InitializeComponent(self):\r\n" +
" self._textBox1 = System.Windows.Forms.TextBox()\r\n" +
" self.SuspendLayout()\r\n" +
" # \r\n" +
" # textBox1\r\n" +
" # \r\n" +
" self.textBox1.Location = System.Drawing.Point(10, 10)\r\n" +
" self.textBox1.Name = \"textBox1\"\r\n" +
" self.textBox1.Size = System.Drawing.Size(110, 20)\r\n" +
" self.textBox1.TabIndex = 1\r\n" +
" self._textBox1.Location = System.Drawing.Point(10, 10)\r\n" +
" self._textBox1.Name = \"textBox1\"\r\n" +
" self._textBox1.Size = System.Drawing.Size(110, 20)\r\n" +
" self._textBox1.TabIndex = 1\r\n" +
" # \r\n" +
" # MainForm\r\n" +
" # \r\n" +

10
src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/LoadFormTestFixtureBase.cs

@ -135,5 +135,15 @@ namespace PythonBinding.Tests.Designer @@ -135,5 +135,15 @@ namespace PythonBinding.Tests.Designer
}
return null;
}
protected CreatedInstance GetCreatedInstance(string name)
{
foreach (CreatedInstance instance in createdInstances) {
if (instance.Name == name) {
return instance;
}
}
return null;
}
}
}

2
src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/LoadSimpleFormTestFixture.cs

@ -100,7 +100,7 @@ namespace PythonBinding.Tests.Designer @@ -100,7 +100,7 @@ namespace PythonBinding.Tests.Designer
args.Add(width);
args.Add(height);
CreatedInstance expectedInstance = new CreatedInstance(typeof(Size), args, "ClientSize", false);
CreatedInstance expectedInstance = new CreatedInstance(typeof(Size), args, null, false);
Assert.AreEqual(expectedInstance, CreatedInstances[0]);
}
}

36
src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/LoadTextBoxTestFixture.cs

@ -31,9 +31,11 @@ namespace PythonBinding.Tests.Designer @@ -31,9 +31,11 @@ namespace PythonBinding.Tests.Designer
" # textBox1\r\n" +
" # \r\n" +
" self._textBox1.Name = \"textBoxName\"\r\n" +
" self._textBox1.Location = System.Drawing.Point(108, 120)\r\n" +
" # \r\n" +
" # form1\r\n" +
" # \r\n" +
" self.Location = System.Drawing.Point(10, 20)\r\n" +
" self.Name = \"form1\"\r\n" +
" self.Controls.Add(self._textBox1)\r\n" +
" self.ResumeLayout(False)\r\n";
@ -43,13 +45,6 @@ namespace PythonBinding.Tests.Designer @@ -43,13 +45,6 @@ namespace PythonBinding.Tests.Designer
public TextBox TextBox {
get { return Form.Controls[0] as TextBox; }
}
[Test]
public void TextBoxInstanceCreated()
{
CreatedInstance instance = new CreatedInstance(typeof(TextBox), new List<object>(), "_textBox1", false);
Assert.Contains(instance, CreatedInstances);
}
[Test]
public void AddedComponentsContainsTextBox()
@ -59,7 +54,14 @@ namespace PythonBinding.Tests.Designer @@ -59,7 +54,14 @@ namespace PythonBinding.Tests.Designer
AddedComponent component = new AddedComponent(instance.Object as IComponent, "textBox1");
Assert.Contains(component, AddedComponents);
}
[Test]
public void TextBoxInstanceCreated()
{
CreatedInstance instance = new CreatedInstance(typeof(TextBox), new List<object>(), null, false);
Assert.Contains(instance, CreatedInstances);
}
[Test]
public void TextBoxAddedToForm()
{
@ -78,5 +80,23 @@ namespace PythonBinding.Tests.Designer @@ -78,5 +80,23 @@ namespace PythonBinding.Tests.Designer
{
Assert.AreEqual("textBoxName", TextBox.Name);
}
[Test]
public void TextBoxLocation()
{
Assert.AreEqual(new Point(108, 120), TextBox.Location);
}
[Test]
public void CreatedInstancesDoesNotIncludeLocation()
{
Assert.IsNull(GetCreatedInstance("Location"));
}
[Test]
public void FormLocation()
{
Assert.AreEqual(new Point(10, 20), Form.Location);
}
}
}

Loading…
Cancel
Save