-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.py
316 lines (275 loc) · 12.4 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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# imports
import json
import sys, os
from PyQt5 import uic, QtGui
from PyQt5.QtWidgets import *
from PyQt5.QtCore import Qt
import traceback
from highlighter import *
import webbrowser
import multiprocessing
from presence import set_discord_rpc_filename, update_presence
from QImageWidget import QImageWidget
import subprocess
# Give imports up
Ui_MainWindow, QtBaseClass = uic.loadUiType('EditorUI.ui')
def format(color, style=''):
"""Return a QTextCharFormat with the given attributes.
"""
_color = QtGui.QColor()
_color.setNamedColor(color)
_format = QtGui.QTextCharFormat()
_format.setForeground(_color)
if 'bold' in style:
_format.setFontWeight(QtGui.QFont.Bold)
if 'italic' in style:
_format.setFontItalic(True)
return _format
try:
STYLES = eval(open(open('themes/current_theme.txt', 'r').read(), 'r').read())
except:
STYLES = {
"lines": "#AAAAAA",
"background": "white",
"text": "black",
"keyword": format("blue"),
"keyword2": format("#fd7e00"),
"keyword3": format("grey", "italic"),
"operator": format("red"),
"brace": format("darkGray"),
"defclass": format("black", "bold"),
"string": format("magenta"),
"string2": format("darkMagenta"),
"comment": format("darkGreen", "italic"),
"self": format("black", "italic"),
"numbers": format("brown")
}
# Main window of the program
class RickWindow(QMainWindow):
docs_links = [
'https://github.com/Rick-Lang/rickroll-lang/blob/main/doc.md',
'https://github.com/Rick-Lang/rickroll-lang/blob/main/doc-Ch.md',
'https://github.com/Rick-Lang/rickroll-lang/blob/main/doc-RU.md'
]
rickroll_folder = open('rcllngdir', 'r').read()
def __init__(self):
super().__init__()
# Variables
self.scriptDir = os.path.dirname(os.path.realpath(__file__))
self.curFile = None
self.curFolder = None
self.curTheme = open('themes/current_theme.txt', 'r').read()
# Asking how does window feeling
uic.loadUi('ui/code.ui', self)
print('''------------------------------------------------------------------------------
------------------####################################------------------------
--------------##################################################--------------
--------------######################################################----------
------------##########################################################--------
----------############################################################--------
----------##################--------##################################--------
--------################----------##----##############--------##########------
--------##############------------####----########------------############----
------####------##################------########------######################--
----####--######------####----####################--##############----######--
--####--######--######----########################--######--------##########--
--####--####------##################################--######################--
--####--##----####----##############--################--##########--########--
--####--######--######------########--##----########----########----######----
----##########----####--####------##--##########----##########--------####----
------##########----------########--------################------------####----
--------########--####------######--######------------------####------####----
--------##########--##--##----------########--####--######--##--------##------
----------##########----########--------------------------------------##------
----------############--########--####--------------------------------##------
------------############----####--########--##----------------------####------
--------------############------##########--######--####--##--##----######----
----------------##############------######--######--##----##------########----
--------------------##################------------------------############----
----------------------####################################################----
----------------------------##############################################----
--------------------------------##########################################----
------------------------------------####################################------
----------------------------------------################################------
------------------------------------------------####################----------''')
# Tell the window how it should feel
self.setWindowTitle(f'Rickroll IDE')
set_discord_rpc_filename("Rickroll IDE")
self.setWindowIcon(QtGui.QIcon(self.scriptDir + os.path.sep + 'ui/src/icon.png'))
# Give some shit up
self.codeEdit.hide()
self.foldersList.hide()
# Oh yes, preparing code area
self.highlighter = RickHighlighter(STYLES, self.codeEdit.document())
self.codeEdit.setTabStopDistance(4)
self.codeEdit.setTabStopWidth(20)
self.logo = QImageWidget(self, 'ui/src/icon.png')
self.show()
# Connecting widgets to events
self.actionOpen.triggered.connect(self.openFile)
self.actionSave.triggered.connect(self.saveFile)
self.actionSave_as.triggered.connect(self.saveFileAs)
self.actionSave_as.triggered.connect(self.saveFileAs)
self.actionNew.triggered.connect(self.newFile)
self.actionThemes.triggered.connect(self.themes_options)
self.foldersList.itemDoubleClicked.connect(self.folderClicked)
self.update_theme(self.curTheme)
self.actionEnglish.triggered.connect(lambda: webbrowser.open(self.docs_links[0]))
self.actionChinese.triggered.connect(lambda: webbrowser.open(self.docs_links[1]))
self.actionRussian.triggered.connect(lambda: webbrowser.open(self.docs_links[2]))
self.actionRun_current_script.triggered.connect(self.runscript)
self.actionAbout_IDE.triggered.connect(lambda: webbrowser.open('https://www.youtube.com/watch?v=dQw4w9WgXcQ'))
self.actionSelInt.triggered.connect(self.askforfolder)
#Keybinds
self.actionSave.setShortcut(QtGui.QKeySequence("Ctrl+s"))
self.actionNew.setShortcut(QtGui.QKeySequence("Ctrl+n"))
self.actionOpen.setShortcut(QtGui.QKeySequence("Ctrl+o"))
if self.rickroll_folder == '':
self.askforfolder()
def runscript(self):
with open('script.bat', 'w') as f:
f.write(f"python -i {self.rickroll_folder} {self.curFile}")
subprocess.Popen(f"start script.bat", shell=True)
print(f"script {self.curFile} opened via {self.rickroll_folder}")
def askforfolder(self):
folder = QFileDialog.getOpenFileName(
self, 'Select interpreter (RickRoll.py)', os.getcwd(),
'Python script(*.py)')[0]
with open('rcllngdir', 'w') as f:
f.write(str(folder))
self.rickroll_folder = folder
def resizeEvent(self, event):
size = self.size()
codepos = self.codeEdit.geometry()
folderpos = self.foldersList.geometry()
#self.codeEdit.resize(self.size().width() - codepos.left() - 10, self.size().height() - codepos.top() - 30)
self.codeEdit.resize(self.size().width() - codepos.left() - 10, self.size().height() - codepos.top() - 30)
self.foldersList.resize(folderpos.width(), self.size().height() - folderpos.top() - 30)
if not self.codeEdit.isVisible():
self.logo.setScale((self.height() // 1.6, self.height() // 1.6), Qt.KeepAspectRatio)
self.logo.move((self.width() - self.logo.width()) // 2, (self.height() - self.logo.height()) // 2)
# Themes
def update_theme(self, folder):
try:
theme = eval(open(folder, 'r').read())
self.highlighter = RickHighlighter(theme, self.codeEdit.document())
self.setStyleSheet(f"background-color: {theme['background']};\ncolor: {theme['text']}")
with open('themes/current_theme.txt', 'w') as f:
f.write(folder)
except:
pass
# Set window title when it has a file
def setTitle(self):
self.setWindowTitle(f'{self.curFile.split("/")[-1]} - {self.curFolder}')
set_discord_rpc_filename(self.curFile.split("/")[-1])
self.folder()
#Options
def themes_options(self, checked):
self.themesw = ThemesWindow(self)
self.themesw.show()
# Folders
def folder(self):
foldsmb = '🗀'
filesmb = '🗊'
self.foldersList.clear()
self.foldersList.addItem('...')
for i in os.listdir(self.curFolder):
symb = ''
if os.path.isdir(self.curFolder + i):
symb = foldsmb
elif os.path.isfile(self.curFolder + i):
symb = filesmb
self.foldersList.addItem(symb + i)
def folderClicked(self, event):
if event.text() == '...':
self.curFolder = '/'.join(self.curFolder.split('/')[:-2]) + '/'
self.folder()
return
folder = self.curFolder + event.text()[1:]
if os.path.isfile(folder):
self.codeEdit.setPlainText(open(folder, 'r', encoding="utf-8").read())
self.curFile = folder
self.curFolder = '/'.join(self.curFile.split('/')[:-1]) + '/'
self.setTitle()
else:
self.curFolder = folder + '/'
self.folder()
# Files
def newFile(self):
self.codeEdit.setPlainText('')
self.codeEdit.show()
self.foldersList.show()
self.logo.hide()
self.setWindowTitle(f'Rickroll IDE (New unnamed file)')
set_discord_rpc_filename("New unnamed file")
def saveFile(self):
if not self.curFile:
self.saveFileAs()
else:
with open(self.curFile, 'w', encoding="utf-8") as file:
file.write(self.codeEdit.toPlainText())
def saveFileAs(self):
folder = QFileDialog.getSaveFileName(
self, 'Create file', os.getcwd(),
'Rickroll script (*.rickroll);;Another file type(*)')[0]
with open(folder, 'w', encoding="utf-8") as file:
file.write(self.codeEdit.toPlainText())
self.curFile = folder
self.curFolder = '/'.join(folder.split('/')[:-1]) + '/'
print(self.curFolder)
self.setTitle()
def openFile(self):
folder = QFileDialog.getOpenFileName(
self, 'Select file', os.getcwd(),
'Rickroll script (*.rickroll);;Another file type(*)')[0]
self.codeEdit.setPlainText(open(folder, 'r').read())
self.codeEdit.show()
self.foldersList.show()
self.logo.hide()
self.curFile = folder
self.curFolder = '/'.join(self.curFile.split('/')[:-1]) + '/'
self.setTitle()
class ThemesWindow(QWidget):
def __init__(self, parent):
self.main = parent
super().__init__()
uic.loadUi('ui/themes.ui', self)
self.setWindowTitle(f'Themes options')
set_discord_rpc_filename("Themes options")
try:
self.update_theme(self.main.curTheme)
except Exception:
pass
self.selectButton.clicked.connect(self.addTheme)
def addTheme(self):
folder = folder = QFileDialog.getOpenFileName(
self, 'Select file', os.getcwd() + '/themes',
'Json theme file (*.json);;Another file type(*)')[0]
self.main.curTheme = folder
self.main.update_theme(folder)
self.update_theme(folder)
self.close()
def update_theme(self, folder):
try:
theme = eval(open(folder, 'r').read())
self.highlighter = RickHighlighter(theme, self.codeEdit.document())
self.setStyleSheet(f"background-color: {theme['background']};\ncolor: {theme['text']}")
except:
pass
def log_uncaught_exceptions(ex_cls, e, tb): # Let errors cry
text = '{}: {}:\n'.format(ex_cls.__name__, e)
text += ''.join(traceback.format_tb(tb))
print(text)
#QMessageBox.critical(None, 'Error', text)
# sys.exit()
if __name__ == '__main__':
# Give presence up
multiprocessing.freeze_support()
p = multiprocessing.Process(target=update_presence)
#p.daemon = True
p.start()
never = QApplication(sys.argv)
sys.excepthook = log_uncaught_exceptions
gonna = RickWindow()
gonna.show()
sys.exit(never.exec_())