/
main.py
271 lines (249 loc) · 10.3 KB
/
main.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
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
from PyQt4.QtGui import (QApplication,QPixmap,QSplashScreen,QMessageBox,
QIcon,QAction,QCheckBox,QFileDialog)
from PyQt4.QtCore import SIGNAL,Qt,QStringList,QString
import icons
from window import Window
from Widget import Editor,PyInterp,Adb,Parser,Command
from globals import (ospathsep,ospathjoin,ospathbasename,workDir,
OS_NAME,config,workSpace,
iconSize,iconDir,ospathexists,os_icon,app,Pix)
class MainWindow(Window):
def __init__(self, parent = None):
Window.__init__(self,parent)
#Important must be empty this is a reference
self.files = []
self.projects = None
self.recent = None
self.dirty = None
self.isFull = False
self.adb = Adb(self)
self.parser = Parser(self)
self.command = Command(self)
self.init()
def init(self):
self.initConfig()
self.initToolBar()
self.initProjects()
self.connect(self, SIGNAL('triggered()'), self.closeEvent)
self.connect(self.tabWidget,SIGNAL("dropped"), self.createTabs)
#self.initInterpreter()
def initConfig(self):
self.projects = config.projects()
self.recent = config.recent()
self.dirty = []
if(config.files() != None):
for i in config.files():
self.createTab(i)
self.tabWidget.tabCloseRequested.connect(self.closeTab)
self.treeWidget.itemDoubleClicked.connect(self.ss)
def initProjects(self):
if self.projects != None:
if len(self.projects) != 0:
for pro in self.projects:
self.createProject(pro)
#Important all projects must go through this
def createProject(self,startDir):
if(ospathexists(startDir)):
self.treeWidget.addProject(startDir)
else:
config.removeProject(startDir)
QMessageBox.about(self,"Can't Open Project","Project Does Not Exist %s"%startDir)
def ss(self,item):
if(item.isFile()):
if(item.isDoc()):
self.createTab(item.getPath())
elif(item.isPic()):
self.createTab(item.getPath())
def initInterpreter(self):
self.ipy = PyInterp(self)
self.ipy.initInterpreter(locals())
self.tabWidget_3.addTab(self.ipy, "Python")
def createTab(self,nfile):
if(nfile != None):
if self.files != None:
if len(self.files) != 0:
for i in self.files:
if(i == nfile):
#print "File Already Open\n"+nfile
self.tabWidget.setCurrentIndex(self.files.index(nfile))
return
if type(nfile) == str:
if(ospathexists(nfile)):
text = ""
try:
infile = open(nfile, 'r')
text = infile.read()
infile.close()
config.addFile(nfile)
self.dirty.append(False)
self.files.append(nfile)
#print len(self.files)
except:
config.removeFile(nfile)
QMessageBox.about(self,"Can't Open","File Does Not Exist or Locked\n"+nfile)
tab = Editor(self,text,self.syntax(nfile),self.colorStyle)
self.tabWidget.addTab(tab,ospathbasename(nfile))
tab.textChanged.connect(lambda:self.setDirty(nfile))
if(self.files != None):
if(len(self.files)) != 0:
#This line sets the opened file to display first Important not checked
self.tabWidget.setCurrentIndex(len(self.files)-1)
else:
#dont know must check this the last file is not removed executes only
#twice when it has to remove 3 files
#check sel.files
#print len(config.files())
config.removeFile(nfile)
QMessageBox.about(self,"Can't Open","File Does Not Exist or Locked\n"+nfile)
def createTabs(self,link):
for i in link:
self.createTab(i)
def closeTab(self,index):
'''Boolean result invocation method.'''
done = True
if self.dirty[index]:
reply = QMessageBox.question(self,
"Sabel IDE - Unsaved Changes",
"Save unsaved changes?",
QMessageBox.Yes|QMessageBox.No|QMessageBox.Cancel)
if reply == QMessageBox.Cancel:
done = False
elif reply == QMessageBox.Yes:
done = self.fileSave(index)
elif reply == QMessageBox.No:
self.clearDirty(index)
done = True
if(done):
#print index
config.removeFile(self.files[index])
self.files.remove(self.files[index])
self.tabWidget.removeTab(index)
#return True
def setDirty(self,file):
'''On change of text in textEdit window, set the flag
"dirty" to True'''
index = self.files.index(file)
if self.dirty[index]:
return True
self.dirty[index] = True
flbase = ospathbasename(self.files[index])
self.tabWidget.setTabText(index,"*"+flbase)
def clearDirty(self,index):
'''Clear the dirty.'''
self.dirty[index] = False
flbase = ospathbasename(self.files[index])
self.tabWidget.setTabText(index,flbase)
def newProject(self):
fname = str(QFileDialog.getExistingDirectory(self,"Open Project Folder"))
if not (fname == ""):
fname = fname+"/"
#print fname
if self.projects != None:
for nfile in self.projects:
if(nfile != fname):
self.createProject(fname)
config.addProject(fname)
return
else:
QMessageBox.about(self, "Already Open","Project Already Open\n"+fname)
return
else:
self.treeWidget.addProject(fname)
config.addProject(fname)
return
def fileOpen(self):
fname = str(QFileDialog.getOpenFileName(self,
"Open File", '.', "Files (*.*)"))
if not (fname == ""):
if self.files != None:
if len(self.files) != 0:
for file in self.files:
if(file != fname):
self.createTab(fname)
return
else:
QMessageBox.about(self, "Already Open","File Already Open")
return
else:
self.createTab(fname)
else:
print "not"
#this is when the files list is empty and None type
if(self.files == None):
self.files = []
self.createTab(fname)
else:
return
def fileSave(self):
if(self.files != None):
if len(self.files) != 0:
index = self.tabWidget.currentIndex()
if not self.dirty[index]:
return
fname = self.files[index]
fl = open(fname, 'w')
tempText = self.tabWidget.widget(index).text()
if tempText:
fl.write(tempText)
fl.close()
self.clearDirty(index)
self.parser.run(self.files[index])
else:
QMessageBox.about(self, "Can't Save","Failed to save ...")
self.statusBar().showMessage('Failed to save ...', 5000)
def fileSaveAll(self):
def fileSaveIndex(index):
if not self.dirty[index]:
return
fname = self.files[index]
fl = open(fname, 'w')
tempText = self.tabWidget.widget(index).text()
if tempText:
fl.write(tempText)
fl.close()
self.clearDirty(index)
else:
QMessageBox.about(self, "Can't Save","Failed to save ...")
self.statusBar().showMessage('Failed to save ...', 5000)
if(self.files != None):
if len(self.files) != 0:
for file in self.files:
fileSaveIndex(self.files.index(file))
def closeEvent(self, event):
#check this ine adb.exe process is always on
self.adb.close()
self.parser.close()
self.command.close()
notSaved = False
for files in self.dirty:
if files == True:
notSaved = True
if notSaved:
reply = QMessageBox.question(self,
"Simple Editor - Unsaved Changes",
"Save unsaved changes?",
QMessageBox.Yes|QMessageBox.No|QMessageBox.Cancel)
if reply == QMessageBox.Cancel:
pass
elif reply == QMessageBox.Yes:
self.fileSaveAll()
def syntax(self,nfile):
lang = 0
if nfile.endswith(".py"):
lang = 0
elif (nfile.endswith(".cpp") or nfile.endswith(".h") or nfile.endswith(".c")):
lang = 1
elif nfile.endswith(".nut"):
lang = 2
return lang
def options(self):
pass
splash_pix = Pix.logosabel
splash = QSplashScreen(splash_pix, Qt.WindowStaysOnTopHint)
splash.setMask(splash_pix.mask())
splash.show()
app.processEvents()
frame = MainWindow()
frame.showMaximized()
splash.finish(frame)
app.exec_()