Menu

[r239]: / drplugins / myProject.py  Maximize  Restore  History

Download this file

185 lines (164 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
# Programmer: Antonio Barbosa
# E-mail: ab@jn.pt
# Note: Initial Release 22 Fev 2006
#
#Plugin
#myProject
import wx
import drScrolledMessageDialog
from drSourceBrowser import drSourceBrowserPanel
import drPrefsFile
import os.path
def OnHelp(DrFrame):
HelpString = """myProject
History:
0.0.1 22 Fev 2006
0.0.2 10 Fev 2007
0.0.3 03 Apr 2007 #Force extension to .prj
0.0.4 05 Apr 2007 #history with last 10 projects
"""
drScrolledMessageDialog.ShowMessage(DrFrame, HelpString, "Help")
class cfg(object): #Globals
recentprojects=[]
actualproject=""
myprefsfile = ""
projectwildcard = "DrPython Project File (*.prj)|*.prj|All Files (*)|*"
def OnAbout(DrFrame):
Version = "0.0.4"
NameAndVersion = "Simple Project: " + Version + "\n"
AboutString = NameAndVersion + "By Antonio Barbosa"
drScrolledMessageDialog.ShowMessage(DrFrame, AboutString, "About")
def OnPreferences(DrFrame):
DrFrame.ShowMessage("Nothing to configure ... ;)")
def Plugin(DrFrame):
def myProject (event):
ID_SAVE = DrFrame.GetNewId() //wx.NewId()
ID_LOAD = DrFrame.GetNewId() //wx.NewId()
ID_UPDATE = DrFrame.GetNewId() //wx.NewId()
menu = wx.Menu()
menu.Append(ID_LOAD, "Load Project")
DrFrame.Bind(wx.EVT_MENU, OnLoadProject, id=ID_LOAD)
menu.Append(ID_SAVE, "Save Project")
DrFrame.Bind(wx.EVT_MENU, OnSaveProject, id=ID_SAVE)
if len(cfg.actualproject):
menu.AppendSeparator()
menu.Append(ID_UPDATE, "Update %s" %cfg.actualproject.split('/')[-1].split('.')[0])
DrFrame.Bind(wx.EVT_MENU, OnUpdateProject, id=ID_UPDATE)
if len(cfg.recentprojects):
menu.AppendSeparator()
id=0
for s in cfg.recentprojects:
menu.Append(id, "Load %s" %s.split('/')[-1].split('.')[0])
DrFrame.Bind(wx.EVT_MENU, OnRecentProject, id=id)
id+=1
(x,y) = DrFrame.ScreenToClient(wx.GetMousePosition())
DrFrame.PopupMenu(menu, (x,y+40))
menu.Destroy()
def OnUpdateProject(event):
auxSaveProject(cfg.actualproject)
def OnRecentProject(event):
id=event.GetId()
OnLoadProject(None,cfg.recentprojects[id])
def OnLoadProject(event,projectfilename=""):
try:
if projectfilename=="":
dlg = wx.FileDialog(DrFrame, "Load Project", "", "", cfg.projectwildcard, wx.OPEN|wx.HIDE_READONLY)
if DrFrame.ddirectory:
try:
d = DrFrame.ddirectory
dlg.SetDirectory(d)
except:
DrFrame.ShowMessage(("Error Setting Default Directory To: " + d), "DrPython Error")
if dlg.ShowModal() == wx.ID_OK:
projectfilename = dlg.GetPath().replace("\\", '/')
dlg.Destroy()
if os.path.exists(projectfilename)==False:
return
DrFrame.OnCloseAllDocuments(None)
f = file(projectfilename, 'r')
text = f.read()
f.close()
filenames = drPrefsFile.ExtractPreferenceFromText(text, "Files").rstrip().split('\n')
#check for existing files
if filenames and filenames[0] != '':
fnames = []
for i in range (len(filenames)):
if os.path.exists(filenames[i]):
fnames.append (filenames[i])
else:
DrFrame.ShowMessage("File does not exist: " + filenames[i], "Projects")
filenames = fnames
#Load the Files
l = len(filenames)
if l > 0 and len(filenames[0]) > 0:
old = DrFrame.txtDocument.filename
x = 1
if l > 1:
filename = filenames[0].replace("\\", "/")
DrFrame.OpenFile(filename, len(old) > 0)
while x < l:
filename = filenames[x].replace("\\", "/")
DrFrame.OpenFile(filename, True)
x = x + 1
if l <= 1:
filename = filenames[0].replace("\\", "/")
if old or DrFrame.txtDocument.GetModify():
DrFrame.OpenFile(filename, True)
else:
DrFrame.OpenFile(filename, False)
else:
DrFrame.ShowMessage("No File in Project: " + projectfilename, "Projects")
DrFrame.documentnotebook.SetSelection (0)
DrFrame.documentnotebook.SetTab()
DrFrame.txtDocument.SetFocus()
DrFrame.SetStatusText(projectfilename, 2)
# Save Prefs
f = open(cfg.myprefsfile, 'w')
for s in cfg.recentprojects:
f.write(s + '\n')
f.close()
cfg.actualproject=projectfilename
except :
DrFrame.ShowMessage("Error Reading Project" , "Read Project Error")
def OnSaveProject(event):
dlg = wx.FileDialog(DrFrame, "Save Project As", "", "", cfg.projectwildcard, wx.SAVE|wx.OVERWRITE_PROMPT)
if dlg.ShowModal() == wx.ID_OK:
projectfilename = dlg.GetPath().replace("\\", "/")
if projectfilename.endswith('.prj')==False: #AB: bug of drFileDialog
projectfilename=projectfilename.split('.')[0]+'.prj'
auxSaveProject(projectfilename)
dlg.Destroy()
def auxSaveProject(projectfilename):
f = file(projectfilename, 'w')
f.write("<Files>")
x = 0
for document in DrFrame.txtDocumentArray:
if document.filename:
f.write(document.filename + '\n')
f.write("</Files>\n")
f.close()
DrFrame.SetStatusText(projectfilename, 2)
if cfg.recentprojects.__contains__(projectfilename):
i=cfg.recentprojects.index(projectfilename)
cfg.recentprojects.pop(i)
cfg.recentprojects.insert(0,projectfilename)
if len(cfg.recentprojects)>10:
cfg.recentprojects.pop(-1)
# Save Prefs
f = open(cfg.myprefsfile, 'w')
for s in cfg.recentprojects:
f.write(str(s) + '\n')
f.close()
#------------------------------------------------------------------------------------
cfg.myprefsfile = DrFrame.pluginsdatdirectory + "/myproject.log"
if os.path.exists(cfg.myprefsfile):
f = open(cfg.myprefsfile, 'r')
for s in f:
cfg.recentprojects.append(s.strip())
f.close()
DrFrame.LoadPluginShortcuts('myProject')
ID_MYPROJECT = DrFrame.GetNewId()
DrFrame.programmenu.AppendSeparator()
DrFrame.programmenu.Append(ID_MYPROJECT, "MyProject", " MyProject")
DrFrame.Bind(wx.EVT_MENU, myProject, id=ID_MYPROJECT)
DrFrame.AddPluginFunction("myProject", "myProject", myProject)