forked from mcneel/rhino-developer-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SampleEtoListBoxDialog.py
131 lines (114 loc) · 4.62 KB
/
SampleEtoListBoxDialog.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
################################################################################
# SampleEtoListBoxDialog.py
# Copyright (c) 2017 Robert McNeel & Associates.
# See License.md in the root of this repository for details.
################################################################################
# Imports
import System
from System.Collections.ObjectModel import ObservableCollection
import Rhino
import Rhino.UI
import Eto.Drawing as drawing
import Eto.Forms as forms
################################################################################
# Fruit class
################################################################################
class Fruit(object):
# Initializer
def __init__(self, name, color):
self.m_name = name
self.m_color = color
@property
def Name(self):
return self.m_name
@property
def Color(self):
return self.m_color
################################################################################
# SampleEtoListBoxDialog class
################################################################################
class SampleEtoListBoxDialog(forms.Dialog[bool]):
# Initializer
def __init__(self, collection):
self.m_collection = collection
# Initialize dialog box properties
self.Title = 'Sample Eto ListBox'
self.Padding = drawing.Padding(5)
self.Resizable = True
# Create a label control
label = forms.Label()
label.Text = 'Select a fruit:'
# Create dynamic layout control
layout = forms.DynamicLayout()
layout.Padding = drawing.Padding(5)
layout.Spacing = drawing.Size(5, 5)
# Add controls to layout
layout.AddRow(label)
layout.AddRow(self.CreateListBox())
layout.AddRow(None) # spacer
layout.AddRow(self.CreateButtons())
# Set the dialog content
self.Content = layout
# Delegate function to retrieve the name of a Fruit object
def FruitDelegate(self, fruit):
return fruit.Name
# Creates a ListBox control
def CreateListBox(self):
# Create labels
listbox = forms.ListBox()
listbox.Size = drawing.Size(100, 150)
listbox.ItemTextBinding = forms.Binding.Delegate[Fruit, System.String](self.FruitDelegate)
listbox.DataStore = self.m_collection
listbox.SelectedIndex = 0
self.m_listbox = listbox
return self.m_listbox
# OK button click handler
def OnOkButtonClick(self, sender, e):
self.m_selected_index = self.m_listbox.SelectedIndex
self.Close(True)
# Cancel button click handler
def OnCancelButtonClick(self, sender, e):
self.Close(False)
# Create button controls
def CreateButtons(self):
# Create the default button
self.DefaultButton = forms.Button(Text = 'OK')
self.DefaultButton.Click += self.OnOkButtonClick
# Create the abort button
self.AbortButton = forms.Button(Text = 'Cancel')
self.AbortButton.Click += self.OnCancelButtonClick
# Create button layout
button_layout = forms.DynamicLayout()
button_layout.Spacing = drawing.Size(5, 5)
button_layout.AddRow(None, self.DefaultButton, self.AbortButton, None)
return button_layout
# Returns the selected index
@property
def SelectedIndex(self):
return self.m_selected_index
################################################################################
# TestSampleEtoListBoxDialog function
################################################################################
def TestSampleEtoListBoxDialog():
# Create and initialize collection
collection = ObservableCollection[Fruit]()
collection.Add(Fruit('Apple', 'Red'))
collection.Add(Fruit('Banana', 'Yellow'))
collection.Add(Fruit('Grape', 'Purple'))
collection.Add(Fruit('Orange', 'Orange'))
collection.Add(Fruit('Strawberry', 'Red'))
# Create and display dialog
dlg = SampleEtoListBoxDialog(collection)
rc = dlg.ShowModal(Rhino.UI.RhinoEtoApp.MainWindow)
if (rc):
# Print results
fruit = collection[dlg.SelectedIndex]
print fruit.Name
print fruit.Color
################################################################################
# Check to see if this file is being executed as the 'main' python
# script instead of being used as a module by some other python script
# This allows us to use the module which ever way we want.
################################################################################
if __name__ == '__main__':
TestSampleEtoListBoxDialog()