[r46]: / trunk / cforge / cforge / Package / CFORGE / Scripts / ui.py  Maximize  Restore  History

Download this file

232 lines (195 with data), 7.1 kB

  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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#!/usr/bin/ipy

import clr
clr.AddReference("System.Windows.Forms")
clr.AddReference("System.Drawing")

from System.Drawing import Point, Font, FontStyle, Color, Icon
from System.Windows.Forms import Application, Form, TextBox
from System.Windows.Forms import Label, ToolBar, ToolBarButton, OpenFileDialog, Button, ScrollBars
from System.Windows.Forms import DialogResult, ScrollBars, DockStyle
import sys
import os
import json
import ntpath

GlobalResult = False

class IFormSettings(Form):

	def __init__(self, Title, Config, Folder=False, Credentials=False, Info=False, InfoText="", Message=False):
		self.Config = Config
		
		y = 0
		self.TitleLable = Label()
		self.TitleLable.Text = Title
		self.TitleLable.Parent = self
		self.TitleLable.Height = 36
		self.TitleLable.Font = Font("Arial", 24,FontStyle.Bold)
		self.TitleLable.BackColor = Color.FromArgb(80,80,80)
		self.TitleLable.ForeColor = Color.FromArgb(255,255,255)
		self.TitleLable.Dock = DockStyle.Top
		y += 42
		
		if Folder:
			# Folder
			self.FolderLabel = Label()
			self.FolderLabel.Text = "Folder:"
			self.FolderLabel.Location = Point(0, y)
			self.FolderLabel.Width = 60
			self.FolderLabel.Parent = self
			
			self.FolderBox = TextBox()
			self.FolderBox.Text = Config["folder"]
			self.FolderBox.Enabled = True
			self.FolderBox.Location = Point(60, y)
			self.FolderBox.Parent = self
			self.FolderBox.Width = 300
			
			self.FolderButton = Button()
			self.FolderButton.Click += self.OnClickBrowse
			self.FolderButton.Text = "..."
			self.FolderButton.Parent = self
			self.FolderButton.Location = Point(364, y)
			y += 24
		
		if Credentials:
			# Credentials
			self.UserLabel = Label()
			self.UserLabel.Text = "Username:"
			self.UserLabel.Location = Point(0, y)
			self.UserLabel.Width = 60
			self.UserLabel.Parent = self

			self.UserBox = TextBox()
			self.UserBox.Text = Config["user"]
			self.UserBox.Enabled = True
			self.UserBox.Location = Point(60, y)
			self.UserBox.Parent = self
			self.UserBox.Width = 300
			y += 24

			self.PassLabel = Label()
			self.PassLabel.Text = "Password:"
			self.PassLabel.Location = Point(0, y)
			self.PassLabel.Width = 60
			self.PassLabel.Parent = self

			self.PassBox = TextBox()
			self.PassBox.Text = Config["pass"]
			self.PassBox.Enabled = True
			self.PassBox.PasswordChar = "*"
			self.PassBox.Location = Point(60, y)
			self.PassBox.Parent = self
			self.PassBox.Width = 300
			y += 24

		if Info:
			# Info Box
			self.InfoLabel = Label()
			self.InfoLabel.Text = "Info:"
			self.InfoLabel.Location = Point(0, y)
			self.InfoLabel.Width = 60
			self.InfoLabel.Parent = self
			y += 24
			
			self.InfoBox = TextBox()
			self.InfoBox.Text = InfoText
			self.InfoBox.Enabled = False
			self.InfoBox.Location = Point(0, y)
			self.InfoBox.Parent = self
			self.InfoBox.WordWrap = False
			self.InfoBox.Multiline = True
			self.InfoBox.ScrollBars = ScrollBars.Horizontal | ScrollBars.Vertical
			self.InfoBox.Width = 360
			self.InfoBox.Height = 260
			y += 280
		
		if Message:
			# Message
			self.MessageLabel = Label()
			self.MessageLabel.Text = "Message:"
			self.MessageLabel.Location = Point(0, y)
			self.MessageLabel.Width = 60
			self.MessageLabel.Parent = self
			y += 24
			
			self.MessageBox = TextBox()
			self.MessageBox.Text = "Enter Message..."
			if Config["msg"] != "":
				self.MessageBox.Text = Config["msg"]
			self.MessageBox.Enabled = True
			self.MessageBox.Location = Point(0, y)
			self.MessageBox.Parent = self
			self.MessageBox.WordWrap = False
			self.MessageBox.Multiline = True
			self.MessageBox.AcceptsReturn = True
			self.MessageBox.ScrollBars = ScrollBars.Horizontal | ScrollBars.Vertical
			self.MessageBox.Width = 360
			self.MessageBox.Height = 160
			y += 180
		
		# OK / Cancel Button
		self.AcceptButton = Button()
		self.AcceptButton.Click += self.OnClickOK
		self.AcceptButton.Text = "OK"
		self.AcceptButton.Parent = self
		self.AcceptButton.Location = Point(364, y)
		self.AcceptButton.Select()
		
		self.CancelButton = Button()
		self.CancelButton.Click += self.OnClickCancel
		self.CancelButton.Text = "Cancel"
		self.CancelButton.Parent = self
		self.CancelButton.Location = Point(0, y)
		
		y += 24
		
		# Self (Dialog)
		self.Width = 458
		self.Height = y + 50
		self.Text = "cforge - " + Title
		workingdir = os.path.dirname(sys.argv[0])
		self.Icon = Icon(os.path.join(workingdir, "icon.ico"))
		
		self.CenterToScreen()

	def OnClickBrowse(self, sender, event):
		dialog = OpenFileDialog()
		dialog.Filter = "all files (*.*) |*.*"
		dialog.ShowHelp = True
		dialog.ValidateNames = False;
		dialog.CheckFileExists = False;
		dialog.CheckPathExists = False;
		dialog.FileName = "Folder Selection"

		if dialog.ShowDialog(self) == DialogResult.OK:
			folder, file = ntpath.split(dialog.FileName)
			print (self.FolderBox)
			self.FolderBox.Text = folder

	def OnClickOK(self, sender, event):
		global GlobalResult
		# Return content of form in config structure
		if hasattr(self, 'FolderBox'):
			self.Config["folder"] = self.FolderBox.Text
		if hasattr(self, 'UserBox'):
			self.Config["user"] = self.UserBox.Text
		if hasattr(self, 'PassBox'):
			self.Config["pass"] = self.PassBox.Text
		if hasattr(self, 'MessageBox'):
			self.Config["msg"] = self.MessageBox.Text
		GlobalResult = True
		self.Dispose()

	def OnClickCancel(self, sender, event):
		global GlobalResult
		# Discard form data, don't return it
		GlobalResult = False
		self.Dispose()
		
	   
def GetSettings():            
	dirname = os.path.expanduser("~\\cforge")
	filename = dirname + "\\cache.json"
	config = { 'folder' : dirname, 'user' : os.environ["USERNAME"], 'pass' : '', 'msg' : ''}
	if not os.path.isfile(filename):
		if not os.path.isdir(dirname):
			os.mkdir(dirname)
		with open(filename, "w") as f:
			config = json.dump(config, f)
	with open(filename) as f:
		config = json.load(f)
		
	return config
	
def SaveSettings(config):
	dirname = os.path.expanduser("~\\cforge")
	filename = dirname + "\\cache.json"
	with open(filename, "w") as f:
		config = json.dump(config, f)
	
def Dialog(Title, Folder=False, Credentials=False, DefaultFolder="", Info=False, InfoText="", Message=False):            
	config = GetSettings()
		
	if DefaultFolder != "":
		folder, file = ntpath.split(config['folder'])
		config['folder'] = os.path.join(folder, DefaultFolder)

	Application.Run(IFormSettings(Title, Folder=Folder, Credentials=Credentials, Info=Info, InfoText=InfoText, Message=Message, Config=config))
	if GlobalResult:
		SaveSettings(config)
		return config
	else:
		return None
	
	

#Dialog("Checkout", Folder=True, Credentials=True, DefaultFolder="test,y,z")
#Application.Run(IFormSettings("Checkout", Folder=True, Credentials=True))
#Application.Run(IFormSettings("Checkout", Credentials=True))
#Application.Run(IFormSettings("Checkout", Folder=True))