Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gui #109

Merged
merged 5 commits into from
Feb 23, 2024
Merged

Gui #109

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 26 additions & 3 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# from Controller import Controller
# from umleditor.mvc_controller.controller import Controller
from umleditor.mvc_controller import Controller
from umleditor.mvc_view.gui_view.view_GUI import ViewGUI
from umleditor.mvc_controller.controller_GUI import ControllerGUI
import sys
from PyQt6 import QtWidgets

def debug_main():
app = Controller()
Expand All @@ -20,8 +22,29 @@ def main():
# Never expect errors to be caught here
print('Oh no! Unexpected Error!')

def mainGUI():
try:
# Create QApplication for running the program
app = QtWidgets.QApplication(sys.argv)
# Create an instance of our view and pass to controller
mainWindow = ViewGUI()
controller = ControllerGUI(mainWindow)
# Set window to visible and start the application
mainWindow.show()
app.exec()
except KeyboardInterrupt:
# This handles ctrl+C
pass
except EOFError:
# This handles ctrl+D
pass
except Exception as e:
# Never expect errors to be caught here
print('Oh no! Unexpected Error!')

if __name__ == '__main__':
if not __debug__:
debug_main()
else:
main()
main()
#mainGUI()
33 changes: 33 additions & 0 deletions src/umleditor/mvc_controller/controller_GUI.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from umleditor.mvc_view.gui_view.view_GUI import ViewGUI
from umleditor.mvc_model.diagram import Diagram
from PyQt6 import QtWidgets
from PyQt6.QtWidgets import QInputDialog, QLineEdit
from PyQt6.QtCore import QDir


class ControllerGUI:

def __init__(self, window: ViewGUI) -> None:
self._window = window
self._diagram = Diagram()
# Used for detecting when tasks need run
self._window.get_signal().connect(self.run)

def run(self, task: str):
if "class -a" in task:
self.run_add_class(task)
# Parse / Run Command
# If success, return true
# Else, create error messagebox and return false

def run_add_class(self, task):
#TODO Parse
success = False
warning = "Invalid class name!"
print(task)
if success:
self._window.close_class_dialog()
self._window.add_class_card()
else:
self._window.invalid_input_message(warning)

1 change: 0 additions & 1 deletion src/umleditor/mvc_view/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
from .cli_lexer import lex_input as lex
3 changes: 3 additions & 0 deletions src/umleditor/mvc_view/gui_view/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from .view_GUI import ViewGUI
from .class_card import ClassCard
from .class_input_dialog import ClassInputDialog
36 changes: 36 additions & 0 deletions src/umleditor/mvc_view/gui_view/class_card.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from PyQt6.QtWidgets import QWidget, QVBoxLayout, QListWidget, QMenu, QLineEdit
from PyQt6.QtGui import QAction
from PyQt6.QtCore import Qt

class ClassCard(QWidget):
def __init__(self):
super().__init__()
self.initUI()

def initUI(self):
self.layout = QVBoxLayout()

# Create list widget
self.list_widget = QListWidget()
self.list_widget.setContextMenuPolicy(Qt.ContextMenuPolicy.CustomContextMenu)
self.list_widget.customContextMenuRequested.connect(self.showContextMenu)
self.layout.addWidget(self.list_widget)

self.setLayout(self.layout)

# Add some initial items to the list
for i in range(5):
self.list_widget.addItem(f"Item {i}")

def showContextMenu(self, position):
item = self.list_widget.itemAt(position)
if item is not None:
menu = QMenu()
edit_action = QAction("Edit", self)
edit_action.triggered.connect(lambda: self.editItem(item))
menu.addAction(edit_action)
menu.exec(self.list_widget.mapToGlobal(position))

def editItem(self, item):
index = self.list_widget.row(item)
print(item.text())
24 changes: 24 additions & 0 deletions src/umleditor/mvc_view/gui_view/class_input_dialog.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from PyQt6.QtWidgets import QPushButton, QVBoxLayout, QLineEdit, QDialog, QDialogButtonBox, QLabel
'''
Custom Dialog Box w/ custom accept signal allowing us to
check for valid class input
'''
class ClassInputDialog(QDialog):
def __init__(self, parent=None):
super().__init__(parent)
self.setWindowTitle("Add Class")

self.input_text = QLineEdit()
self.ok_button = QPushButton("OK")
self.cancel_button = QPushButton("Cancel")

button_box = QDialogButtonBox()
button_box.addButton(self.ok_button, QDialogButtonBox.ButtonRole.AcceptRole)
button_box.addButton(self.cancel_button, QDialogButtonBox.ButtonRole.RejectRole)

layout = QVBoxLayout()
layout.addWidget(self.input_text)
layout.addWidget(button_box)
self.setLayout(layout)

self.cancel_button.clicked.connect(self.reject)
91 changes: 91 additions & 0 deletions src/umleditor/mvc_view/gui_view/uml.ui
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QWidget" name="gridLayoutWidget">
<property name="geometry">
<rect>
<x>-1</x>
<y>-1</y>
<width>801</width>
<height>571</height>
</rect>
</property>
<layout class="QGridLayout" name="gridLayout"/>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>22</height>
</rect>
</property>
<widget class="QMenu" name="menuFile">
<property name="title">
<string>File</string>
</property>
<addaction name="actionSave"/>
<addaction name="actionLoad"/>
</widget>
<widget class="QMenu" name="menuEdit">
<property name="title">
<string>Edit</string>
</property>
<addaction name="actionAdd_Class"/>
</widget>
<widget class="QMenu" name="menuView">
<property name="title">
<string>View</string>
</property>
<addaction name="actionList"/>
<addaction name="actionHelp"/>
</widget>
<addaction name="menuFile"/>
<addaction name="menuEdit"/>
<addaction name="menuView"/>
</widget>
<widget class="QStatusBar" name="statusbar"/>
<action name="actionSave">
<property name="text">
<string>Save</string>
</property>
</action>
<action name="actionLoad">
<property name="text">
<string>Load</string>
</property>
</action>
<action name="actionAdd_Class">
<property name="text">
<string>Add Class</string>
</property>
</action>
<action name="actionList">
<property name="text">
<string>List</string>
</property>
</action>
<action name="actionHelp">
<property name="text">
<string>Help</string>
</property>
</action>
</widget>
<resources/>
<connections/>
</ui>
45 changes: 45 additions & 0 deletions src/umleditor/mvc_view/gui_view/view_GUI.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import sys, os
from PyQt6 import QtCore, QtGui, QtWidgets
from PyQt6 import uic
from PyQt6.QtWidgets import QMessageBox, QWidget, QVBoxLayout
from PyQt6.QtCore import pyqtSignal
from umleditor.mvc_view.gui_view.class_input_dialog import ClassInputDialog
from umleditor.mvc_view.gui_view.class_card import ClassCard

class ViewGUI(QtWidgets.QMainWindow):
# Signal triggered for task processing
_process_task_signal = pyqtSignal(str)

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
print(os.path.dirname(__file__))
self._ui = uic.loadUi(os.path.join(os.path.dirname(__file__),"uml.ui"), self)
self.connect_menu()

def get_signal(self):
return self._process_task_signal

def connect_menu(self):
self._ui.actionAdd_Class.triggered.connect(self.add_class_click)

def invalid_input_message(self, warning: str):
QMessageBox.critical(self, "Error", warning)

############# Add Class Methods ############
def add_class_click(self):
self._dialog = ClassInputDialog()
self._dialog.ok_button.clicked.connect(self.confirm_class_clicked)
self._dialog.exec()

def confirm_class_clicked(self):
task = 'class -a ' + self._dialog.input_text.text()
# Emit signal to controller to handle task
self._process_task_signal.emit(task)

def close_class_dialog(self):
self._dialog.reject()

def add_class_card(self):
self._class_card = ClassCard()
# Add the custom widget to the central widget of the main window
self._ui.gridLayout.addWidget(self._class_card, 0, 0)