Browse Source

Python form designer now supports loading/generating code for a form's AcceptButton and CancelButton properties.

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/branches/3.0@3945 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
shortcuts
Matt Ward 17 years ago
parent
commit
2c62e65fb9
  1. 7
      src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonForm.cs
  2. 25
      src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonFormWalker.cs
  3. 82
      src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/GenerateAcceptButtonFormTestFixture.cs
  4. 64
      src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/LoadAcceptButtonFormTestFixture.cs
  5. 2
      src/AddIns/BackendBindings/Python/PythonBinding/Test/PythonBinding.Tests.csproj

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

@ -195,7 +195,12 @@ namespace ICSharpCode.PythonBinding @@ -195,7 +195,12 @@ namespace ICSharpCode.PythonBinding
}
string propertyName = GetPropertyName(propertyOwnerName, propertyDescriptor.Name);
AppendIndentedLine(propertyName + " = " + PythonPropertyValueAssignment.ToString(propertyValue));
Control control = propertyValue as Control;
if (control != null) {
AppendIndentedLine(propertyName + " = self._" + control.Name);
} else {
AppendIndentedLine(propertyName + " = " + PythonPropertyValueAssignment.ToString(propertyValue));
}
}
static string GetPropertyName(string propertyOwnerName, string propertyName)

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

@ -81,10 +81,7 @@ namespace ICSharpCode.PythonBinding @@ -81,10 +81,7 @@ namespace ICSharpCode.PythonBinding
MemberExpression rhsMemberExpression = node.Right as MemberExpression;
if (rhsMemberExpression != null) {
object propertyValue = deserializer.Deserialize(rhsMemberExpression);
if (propertyValue == null) {
propertyValue = PythonControlFieldExpression.GetMemberName(rhsMemberExpression);
}
object propertyValue = GetPropertyValueFromAssignmentRhs(rhsMemberExpression);
SetPropertyValue(fieldExpression.MemberName, propertyValue);
} else {
walkingAssignment = true;
@ -213,7 +210,7 @@ namespace ICSharpCode.PythonBinding @@ -213,7 +210,7 @@ namespace ICSharpCode.PythonBinding
/// </summary>
static object ConvertPropertyValue(PropertyDescriptor propertyDescriptor, object propertyValue)
{
if (propertyDescriptor.PropertyType != propertyValue.GetType()) {
if (!propertyDescriptor.PropertyType.IsAssignableFrom(propertyValue.GetType())) {
return propertyDescriptor.Converter.ConvertFrom(propertyValue);
}
return propertyValue;
@ -260,5 +257,23 @@ namespace ICSharpCode.PythonBinding @@ -260,5 +257,23 @@ namespace ICSharpCode.PythonBinding
}
parentControl.Controls.Add(GetControl(childControlName));
}
/// <summary>
/// Gets the property value from the member expression. The member expression is taken from the
/// right hand side of an assignment.
/// </summary>
object GetPropertyValueFromAssignmentRhs(MemberExpression memberExpression)
{
object propertyValue = deserializer.Deserialize(memberExpression);
if (propertyValue == null) {
PythonControlFieldExpression field = PythonControlFieldExpression.Create(memberExpression);
if (field.MemberName.Length > 0) {
propertyValue = GetControl(PythonControlFieldExpression.GetVariableName(field.MemberName));
} else {
propertyValue = field.FullMemberName;
}
}
return propertyValue;
}
}
}

82
src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/GenerateAcceptButtonFormTestFixture.cs

@ -0,0 +1,82 @@ @@ -0,0 +1,82 @@
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <owner name="Matthew Ward" email="mrward@users.sourceforge.net"/>
// <version>$Revision$</version>
// </file>
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.ComponentModel.Design.Serialization;
using System.Drawing;
using System.Windows.Forms;
using ICSharpCode.PythonBinding;
using NUnit.Framework;
using PythonBinding.Tests.Utils;
namespace PythonBinding.Tests.Designer
{
[TestFixture]
public class GenerateAcceptButtonFormTestFixture
{
string generatedPythonCode;
[TestFixtureSetUp]
public void SetUpFixture()
{
using (DesignSurface designSurface = new DesignSurface(typeof(Form))) {
IDesignerHost host = (IDesignerHost)designSurface.GetService(typeof(IDesignerHost));
IEventBindingService eventBindingService = new MockEventBindingService(host);
Form form = (Form)host.RootComponent;
form.ClientSize = new Size(200, 300);
Button button = new Button();
button.Location = new Point(0, 0);
button.Size = new Size(10, 10);
button.Name = "button1";
button.Text = "button1";
button.UseCompatibleTextRendering = false;
form.Controls.Add(button);
PropertyDescriptorCollection descriptors = TypeDescriptor.GetProperties(form);
PropertyDescriptor acceptButtonPropertyDescriptor = descriptors.Find("AcceptButton", false);
acceptButtonPropertyDescriptor.SetValue(form, button);
PropertyDescriptor namePropertyDescriptor = descriptors.Find("Name", false);
namePropertyDescriptor.SetValue(form, "MainForm");
PythonForm pythonForm = new PythonForm(" ");
generatedPythonCode = pythonForm.GenerateInitializeComponentMethod(form);
}
}
[Test]
public void GeneratedCode()
{
string expectedCode = "def InitializeComponent(self):\r\n" +
" self._button1 = System.Windows.Forms.Button()\r\n" +
" self.SuspendLayout()\r\n" +
" # \r\n" +
" # button1\r\n" +
" # \r\n" +
" self._button1.Location = System.Drawing.Point(0, 0)\r\n" +
" self._button1.Name = \"button1\"\r\n" +
" self._button1.Size = System.Drawing.Size(10, 10)\r\n" +
" self._button1.TabIndex = 0\r\n" +
" self._button1.Text = \"button1\"\r\n" +
" # \r\n" +
" # MainForm\r\n" +
" # \r\n" +
" self.AcceptButton = self._button1\r\n" +
" self.ClientSize = System.Drawing.Size(200, 300)\r\n" +
" self.Name = \"MainForm\"\r\n" +
" self.Controls.Add(self._button1)\r\n" +
" self.ResumeLayout(False)\r\n" +
" self.PerformLayout()\r\n";
Assert.AreEqual(expectedCode, generatedPythonCode);
}
}
}

64
src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/LoadAcceptButtonFormTestFixture.cs

@ -0,0 +1,64 @@ @@ -0,0 +1,64 @@
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <owner name="Matthew Ward" email="mrward@users.sourceforge.net"/>
// <version>$Revision$</version>
// </file>
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using ICSharpCode.PythonBinding;
using NUnit.Framework;
using PythonBinding.Tests.Utils;
namespace PythonBinding.Tests.Designer
{
[TestFixture]
public class LoadAcceptButtonFormTestFixture : LoadFormTestFixtureBase
{
public override string PythonCode {
get {
return "class TestForm(System.Windows.Forms.Form):\r\n" +
" def InitializeComponent(self):\r\n" +
" self._button1 = System.Windows.Forms.Button()\r\n" +
" self.SuspendLayout()\r\n" +
" # \r\n" +
" # button1\r\n" +
" # \r\n" +
" self._button1.Location = System.Drawing.Point(0, 0)\r\n" +
" self._button1.Name = \"button1\"\r\n" +
" self._button1.Size = System.Drawing.Size(10, 10)\r\n" +
" self._button1.TabIndex = 0\r\n" +
" self._button1.Text = \"button1\"\r\n" +
" # \r\n" +
" # MainForm\r\n" +
" # \r\n" +
" self.AcceptButton = self._button1\r\n" +
" self.ClientSize = System.Drawing.Size(200, 300)\r\n" +
" self.Name = \"MainForm\"\r\n" +
" self.Controls.Add(self._button1)\r\n" +
" self.ResumeLayout(False)\r\n";
}
}
[Test]
public void FormHasAcceptButton()
{
Assert.IsNotNull(Form.AcceptButton);
}
[Test]
public void AcceptButtonPropertyDescriptorObjectMatchesButton()
{
Button button = Form.Controls[0] as Button;
PropertyDescriptor p = TypeDescriptor.GetProperties(Form).Find("AcceptButton", true);
Assert.AreEqual(button, p.GetValue(Form));
}
}
}

2
src/AddIns/BackendBindings/Python/PythonBinding/Test/PythonBinding.Tests.csproj

@ -150,6 +150,7 @@ @@ -150,6 +150,7 @@
<Compile Include="Designer\DeserializeColorFromArgbTestFixture.cs" />
<Compile Include="Designer\EnabledSetUsingPropertyDescriptorTestFixture.cs" />
<Compile Include="Designer\FindInitializeComponentMethodTestFixture.cs" />
<Compile Include="Designer\GenerateAcceptButtonFormTestFixture.cs" />
<Compile Include="Designer\GenerateAccessibleRoleFormTestFixture.cs" />
<Compile Include="Designer\GenerateAutoScaleModeFormTestFixture.cs" />
<Compile Include="Designer\GenerateAutoScrollFormTestFixture.cs" />
@ -171,6 +172,7 @@ @@ -171,6 +172,7 @@
<Compile Include="Designer\InsertNewEventHandlerTestFixture.cs" />
<Compile Include="Designer\InsertSecondEventHandlerTestFixture.cs" />
<Compile Include="Designer\IsFullyQualifiedBaseClassFormDesignableTestFixture.cs" />
<Compile Include="Designer\LoadAcceptButtonFormTestFixture.cs" />
<Compile Include="Designer\LoadAccessibleRoleTestFixture.cs" />
<Compile Include="Designer\LoadAnchorStylesFormTestFixture.cs" />
<Compile Include="Designer\LoadColorFromArgbTestFixture.cs" />

Loading…
Cancel
Save