From 52d9878580424177b3cae3ac73a931be4d960c19 Mon Sep 17 00:00:00 2001 From: Taylor Bockman Date: Wed, 7 Aug 2019 15:20:44 -0700 Subject: [PATCH] Add some stubbing for points --- clusterview/exceptions.py | 47 +++++++++++++++++++++++++++++++++++++++++++++++ clusterview/mode.py | 35 +++++++++++++++++++++++++++++++++++ main_window.py | 35 ++++++++++++++++++++--------------- requirements.txt | 8 ++++++++ 4 files changed, 110 insertions(+), 15 deletions(-) create mode 100644 clusterview/exceptions.py create mode 100644 clusterview/mode.py diff --git a/clusterview/exceptions.py b/clusterview/exceptions.py new file mode 100644 index 0000000..149a370 --- /dev/null +++ b/clusterview/exceptions.py @@ -0,0 +1,47 @@ +from PyQt5.QtWidgets import QErrorMessage + +from clusterview.mode import Mode + + +def handle_exceptions(func): + """ + A decorator designed to make exceptions thrown + from a function easier to handle. + + The result will be that all exceptions coming from + the decorated function will be caught and displayed + as a error message box. + + Usage: + + @handle_exceptions + def my_qt_func(): + raises SomeException + """ + def wrapped(self, *args, **kwargs): + try: + return func(*args, **kwargs) + except Exception as e: + error_dialog = QErrorMessage() + error_dialog.showMessage(str(e)) + error_dialog.exec_() + + return wrapped + + +class InvalidMode(Exception): + """ + An exception to specify an invalid mode has been provided. + """ + + _exc = None + + def __init__(self, mode): + """ + Initializes the InvalidMode exception with a + mode. + """ + super().__init__("You must select a mode before continuing.") + if type(mode) != Mode: + raise ValueError("Mode argument to InvalidMode must be of "+ + " type mode") diff --git a/clusterview/mode.py b/clusterview/mode.py new file mode 100644 index 0000000..a7180e5 --- /dev/null +++ b/clusterview/mode.py @@ -0,0 +1,35 @@ +from enum import Enum + +class Mode(Enum): + """ + Class to make it easier to figure out what mode + we are operating in when the OpenGL window is + clicked. + """ + OFF = 0 + ADD = 1 + EDIT = 2 + DELETE = 3 + +def __handle_add_point(ctx, event): + """ + TODO: These functions should all take a context to + the openGL widget that will perform the + function. + """ + print("GOT POINT: ({}, {})".format(event.x(), event.y())) + +def __handle_edit_point(ctx, event): + print("GOT POINT: ({}, {})".format(event.x(), event.y())) + +def __handle_delete_point(ctx, event): + print("GOT POINT: ({}, {})".format(event.x(), event.y())) + + +# Simple dispatch to make life easy for the window. +MODE_MAP = { + Mode.OFF: lambda: None, + Mode.ADD: __handle_add_point, + Mode.EDIT: __handle_edit_point, + Mode.DELETE: __handle_delete_point +} diff --git a/main_window.py b/main_window.py index a9e5c71..3a7871b 100644 --- a/main_window.py +++ b/main_window.py @@ -4,18 +4,8 @@ from enum import Enum from PyQt5 import QtWidgets, uic from clusterview_ui import Ui_MainWindow - -class Mode(Enum): - """ - Class to make it easier to figure out what mode - we are operating in when the OpenGL window is - clicked - """ - OFF = 0 - ADD = 1 - EDIT = 2 - DELETE = 3 - +from clusterview.exceptions import handle_exceptions, InvalidMode +from clusterview.mode import Mode, MODE_MAP class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow): """ @@ -38,22 +28,27 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow): self.action_delete_points.triggered.connect(self.__delete_points) self.action_solve.triggered.connect(self.__solve_launcher) + # Override handler for mouse press so we can draw points based on + # the OpenGL coordinate system inside of the OpenGL Widget. + self.opengl_widget.mousePressEvent = self.__ogl_click_dispatcher + # TODO: These handlers should probably be broken out into classes # Such as "GraphicsDrawer", etc that implements these functions # in isolation so this file remains relatively clean aside from # the UI hooks to the functions above. def __add_points(self): self.__mode = Mode.ADD - print("ADDING POINT MODE ENABLED!") + # Should change the on-hover mouse to something else. def __edit_points(self): self.__mode = Mode.EDIT - print("EDITING POINT MODE ENABLED!") + # Should change the on-hover mouse to something else. def __delete_points(self): self.__mode = Mode.DELETE - print("DELETE POINT MODE ENABLED!") + # Should change the on-hover mouse to something else. + @handle_exceptions def __solve_launcher(self): """ Launched the solve menu. This function will call into a subclass @@ -63,3 +58,13 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow): solve window. """ print("LAUNCHING SOLVE DIALOG...") + + + def __ogl_click_dispatcher(self, event): + """ + Mode dispatcher for click actions on the OpenGL widget. + """ + if self.__mode == Mode.OFF: + raise InvalidMode(Mode.OFF) + + MODE_MAP[self.__mode](self.opengl_widget, event) diff --git a/requirements.txt b/requirements.txt index f45d0e7..d3239ce 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,14 +1,22 @@ backcall==0.1.0 +Cython==0.29.13 decorator==4.4.0 +entrypoints==0.3 +flake8==3.7.8 ipython==7.7.0 ipython-genutils==0.2.0 jedi==0.14.1 +mccabe==0.6.1 parso==0.5.1 pexpect==4.7.0 pickleshare==0.7.5 prompt-toolkit==2.0.9 ptyprocess==0.6.0 +pycodestyle==2.5.0 +pyflakes==2.1.1 Pygments==2.4.2 +PyOpenGL==3.1.0 +PyOpenGL-accelerate==3.1.3b1 PyQt5==5.13.0 PyQt5-sip==4.19.18 six==1.12.0