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

Download this file

185 lines (152 with data), 5.5 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
#!/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
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):
		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

		# 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
		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' : '' }
	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=""):            
	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, 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))