This repository has been archived on 2022-12-22. You can view files and clone it, but cannot push or open issues/pull-requests.
Concorde-IDE/gui_handler.py

101 lines
4.0 KiB
Python
Raw Normal View History

2022-03-27 21:00:44 +02:00
from PySide6 import QtWidgets, QtCore, QtGui
import util
app = QtWidgets.QApplication([])
2022-03-26 09:43:00 +01:00
#TODO: Implement separate editor, terminal and main_window classes using Window as a parent
2022-03-19 14:05:10 +01:00
class Window(QtWidgets.QMainWindow):
def __init__(self, size=(640, 480), title="Concorde", on_resize=lambda event: None):
2022-03-19 14:05:10 +01:00
super().__init__()
self.setWindowTitle(title)
self.resize(size[0], size[1])
self.__on_resize = on_resize
2022-03-19 05:45:51 +01:00
def __del__(self):
#TODO: whatever needs to be done here
2022-03-19 05:45:51 +01:00
pass
2022-03-19 05:45:51 +01:00
def set_title(self, title):
2022-03-19 14:05:10 +01:00
self.setWindowTitle(title)
def get_size(self):
#TODO: implement
util.warn("Not implemented!")
2022-03-19 05:45:51 +01:00
return None
def set_size(self, size_x, size_y):
2022-03-19 14:05:10 +01:00
self.resize(size_x, size_y)
def on_resize(self, function):
self.__on_resize = function
def update_menus(self, menu_dict, menu=None):
# if not a sub menu
if menu == None:
menu = self.menuBar()
menu.clear()
2022-03-19 14:05:10 +01:00
#Looping through entire menu_dict
for entry in menu_dict:
# inactive or separator
if menu_dict[entry] == None:
# determine if entry is a separator or an inactive menu item
if type(entry) == int:
menu.addSeparator()
else:
menu_item = menu.addAction(entry)
menu_item.setEnabled(False)
# sub menus
if type(menu_dict[entry]) == dict:
submenu = menu.addMenu(entry)
# recurse because sub menus may have sub menus
self.update_menus(menu_dict[entry], menu=submenu)
# ordinary menu entries
if callable(menu_dict[entry]):
menu_item = menu.addAction(entry)
menu_item.triggered.connect(menu_dict[entry])
# Toolkit specific! Do not use outside gui_handler.
def resizeEvent(self, event):
self.__on_resize(event)
2022-03-26 09:43:00 +01:00
class Editor(Window):
def __init__(self, size=(640, 480)):
super().__init__(size, "Editor")
2022-03-27 21:00:44 +02:00
#TODO: Figure out a way to do the fucking line numbers
2022-03-27 12:27:58 +02:00
#Text Editor
2022-03-28 22:43:30 +02:00
self.text_edit = QtWidgets.QPlainTextEdit()
2022-03-27 12:27:58 +02:00
self.text_edit.setFrameStyle(QtWidgets.QFrame.NoFrame)
2022-03-28 23:04:29 +02:00
self.text_edit.setWordWrapMode(QtGui.QTextOption.NoWrap)
2022-04-16 16:17:08 +02:00
self.tabs = QtWidgets.QTabWidget()
self.tabs.addTab(self.text_edit, "untitled")
self.setCentralWidget(self.tabs)
class Message(QtWidgets.QMessageBox):
def __init__(self, title, text):
super().__init__()
self.setWindowTitle(title)
self.setText(text)
self.setStandardButtons(QtWidgets.QMessageBox.Ok)
self.exec()
#TODO: This needs to run in a thread but Qt really doesn't want it to. There are two ways around this:
# - create the QtWidgets.QApplication inside a thread and run all QT stuff inside that thread
# - make a generic wrapper for window mainloop that will always run in the main thread while the actual main control flow of the program gets moved to another thread
# There are some issues with these workarounds though; mainly that QT isn't thread safe.
# I really want to keep QT running in its own thread because I want to retain the ability to arbitrarily spawn and manipulate windows while other windows are running.
# Another issue that is probably easily worked around / fixed is that app.exec() will return once all running windows are closed.
2022-03-19 12:17:12 +01:00
# Idea for a workaround for both:
# Maybe Qt has scheduled events in which case a scheduled polling event could run a function inside the Qt thread that fetches commands and executes them.
# This could work by passing (lambda) functions through a Communication object.
2022-03-21 05:39:54 +01:00
#UPDATE: Tried implementing both approaches, neither worked. :(
def fixme_window_mainloop_workaround_to_just_get_a_window_started_really_should_not_be_implemented_this_way_for_reasons_stated_in_the_comment_above_the_definition_of_this_function():
app.exec()