-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.py
230 lines (184 loc) · 7.39 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
import os
import sys
from PySide6.QtCore import QModelIndex, Qt
from PySide6.QtGui import QIcon, QShortcut
from PySide6.QtWidgets import (
QApplication,
QHBoxLayout,
QMainWindow,
QVBoxLayout,
QWidget,
)
from scr import *
from scr.interface.basic import Splitter
from scr.project.pyproject import SetupPyProject
class MainWidget(QWidget):
def __init__(self) -> None:
super().__init__()
# self
self.setObjectName("main-widget")
# init layouts
self.mainLayout = QVBoxLayout()
self.workbenchLayout = QHBoxLayout()
# init
self.fileTree = FileTree()
self.tabEditor = TabEditor()
self.sideBar = SideBar()
self.statusBar = StatusBar()
self.settingActionMenu = SettingsActionMenu()
self.restarter = Restarter(self)
self.themeChanger = ThemeChangerWindow(self, self.restarter)
self.settingsMenu = SettingsMenu(self, self.restarter)
self.tabsSwitcher = TabsSwitcherWindow(self)
self.projectList = ProjectChangerWindow(self)
self.splitter = Splitter("horizontal")
self.init_ui()
self.setup_ui()
def init_ui(self) -> None:
# to splitter
self.splitter.addWidget(self.fileTree)
self.splitter.addWidget(self.tabEditor)
# layouts
self.workbenchLayout.addWidget(self.sideBar, stretch=1)
self.workbenchLayout.addWidget(self.splitter)
self.mainLayout.addLayout(self.workbenchLayout)
self.mainLayout.addWidget(self.statusBar)
def setup_ui(self) -> None:
self.tabEditor.add_tab(
Tab("Welcome!", WelcomeScreen(), icon=IconPaths.SystemIcons.WELCOME)
)
self.__changed_tab()
# connections
self.fileTree.clicked.connect(self.__click_file_tree)
self.sideBar.settings_opener_connect(self.settingActionMenu.show)
self.sideBar.file_tree_opener_connect(self.fileTree.show_hide_file_tree)
self.tabEditor.currentChanged.connect(self.__changed_tab)
self.fileTree.directory_changed_connect(self.__changed_tab)
self.settingActionMenu.connect_by_title("Themes...", self.__show_theme_changer)
self.settingActionMenu.connect_by_title(
"Open Settings...", self.settingsMenu.show
)
# shortcuts
QShortcut("Ctrl+W", self).activated.connect(self.projectList.show)
QShortcut("Ctrl+B", self).activated.connect(self.fileTree.show_hide_file_tree)
QShortcut("Ctrl+T", self).activated.connect(self.__show_theme_changer)
QShortcut("Ctrl+,", self).activated.connect(self.settingsMenu.show)
QShortcut("Ctrl+Tab", self).activated.connect(self.__open_tab_switcher)
QShortcut("Ctrl+O", self).activated.connect(
lambda: self.fileTree.open_directory(FileDialog.get_open_directory())
)
QShortcut("Ctrl+P", self).activated.connect(
lambda: self.__click_file_tree(
self.fileTree.open_file(FileDialog.get_open_file_name())
)
)
QShortcut("Ctrl+F5", self).activated.connect(
lambda: FileRunner.run_python_file(
self.tabEditor.get_current_path(), self.fileTree.get_current_directory()
)
)
# updaters
EditorFontManager.add_font_updater(self.tabEditor.update_all_tabs_font)
EditorSettingsUpdater.add_updater(self.tabEditor.update_all_tabs_settings)
WorkbenchFontManager.add_font_updater(self.fileTree.update_font)
WorkbenchFontManager.add_font_updater(self.statusBar.update_font)
WorkbenchFontManager.add_font_updater(self.tabEditor.update_font)
# set layout (draw)
self.setLayout(self.mainLayout)
def __click_file_tree(self, __index: QModelIndex) -> None:
path = self.fileTree.get_path_by_index(__index)
if os.path.isfile(path):
self.__open_file_for_edit(path, self.fileTree.get_file_icon(__index))
def __open_file_for_edit(self, __path: str, __icon) -> None:
if FileChecker.is_python_file(__path):
self.tabEditor.add_tab(
Tab(
os.path.basename(__path),
PythonCodeEditorArea(__path),
__icon,
__path,
)
)
elif FileChecker.is_style_file(__path):
self.tabEditor.add_tab(
Tab(
os.path.basename(__path),
StyleCodeEditorArea(__path),
__icon,
__path,
)
)
elif FileChecker.is_json_file(__path):
self.tabEditor.add_tab(
Tab(
os.path.basename(__path), JsonCodeEditorArea(__path), __icon, __path
)
)
elif FileChecker.is_picture_file(__path):
self.tabEditor.add_tab(
Tab(os.path.basename(__path), ImageViewer(__path), __icon, __path)
)
elif FileChecker.is_html_file(__path):
self.tabEditor.add_tab(
Tab(
os.path.basename(__path), HtmlCodeEditorArea(__path), __icon, __path
)
)
elif FileChecker.is_readable(__path):
try:
self.tabEditor.add_tab(
Tab(
os.path.basename(__path), TextEditorArea(__path), __icon, __path
)
)
except UnicodeDecodeError:
pass
self.tabEditor.setCurrentWidget(self.tabEditor.find_by_path(__path))
def __open_tab_switcher(self):
self.tabsSwitcher.show_window(
self.tabEditor.get_tabs(),
self.tabEditor.currentIndex(),
lambda index: self.tabEditor.setCurrentIndex(index),
)
def __show_theme_changer(self):
themes = [
FileLoader.load_json(f"scr/data/themes/{i}")["name"]
for i in os.listdir("scr/data/themes")
]
self.themeChanger.themeChanger.set_items(*themes)
self.themeChanger.show()
def __changed_tab(self) -> None:
tab = self.tabEditor.get_current_tab()
self.statusBar.update_status_bar(tab)
self.statusBar.change_file_status(tab)
if tab.is_readable(): # if this file is text
self.statusBar.set_current_position(*tab.widget.get_position())
tab.widget.cursorPositionChanged.connect(
lambda: self.statusBar.set_current_position(*tab.widget.get_position())
)
class Window(QMainWindow):
def __init__(self):
super().__init__()
# self.resize(*WINDOW_SIZE)
self.setWindowTitle("PyPad")
self.setWindowIcon(QIcon(IconPaths.SystemIcons.ICON))
self.setStyleSheet(
FileLoader.load_style("scr/style/main.css")
+ FileLoader.load_style("scr/subwidgets/styles/action_menu.css")
)
self.setObjectName("window")
self.mainWidget = MainWidget()
self.setCentralWidget(self.mainWidget)
def __toggle_full_screen(self):
if self.isFullScreen():
self.showMaximized()
else:
self.showFullScreen()
def keyPressEvent(self, event):
if event.key() == Qt.Key.Key_F11:
self.__toggle_full_screen()
if __name__ == "__main__":
app = QApplication(sys.argv)
window = Window()
window.showMaximized()
app.exec()