Browse Source

Smooth out movement, add window boundaries to moving and addition, etc

tb-init-ui-render
Taylor Bockman 5 years ago
parent
commit
ea88f79c70
  1. 16
      clusterview/mode_handlers.py
  2. 3
      clusterview/opengl_widget.py
  3. 58
      clusterview/points.py
  4. 3
      main_window.py

16
clusterview/mode_handlers.py

@ -2,6 +2,7 @@ from enum import Enum
from PyQt5.QtCore import QEvent, Qt
from .exceptions import ExceededWindowBoundsError
from .mode import Mode
from .opengl_widget import (get_bb_bottom_right, get_bb_top_left,
set_current_points, set_drawing_event,
@ -73,11 +74,22 @@ def __handle_add_point(ctx, event):
if (event.button() == Qt.LeftButton and
event.type() == QEvent.MouseButtonPress):
# At this point we can be sure resize_gl has been called
# at least once, so set the viewport properties of the
# point set so it knows the canvas bounds.
__point_set.viewport_width = viewport_width()
__point_set.viewport_height = viewport_height()
# Clear any existing selections
__point_set.clear_selection()
# No attribute at the moment.
__point_set.add_point(event.x(), event.y())
try:
# No attribute at the moment.
__point_set.add_point(event.x(), event.y())
except ExceededWindowBoundsError:
# The user tried to place a point whos edges would be
# on the outside of the window. We will just ignore it.
return
__refresh_point_list(ctx)

3
clusterview/opengl_widget.py

@ -168,7 +168,6 @@ def reset_move_bbs():
__move_bb_top_left = None
__move_bb_bottom_right = None
def initialize_gl():
"""
Initializes the OpenGL context on the Window.
@ -177,7 +176,6 @@ def initialize_gl():
# Set white background
glClearColor(255, 255, 255, 0)
def resize_gl(w, h):
"""
OpenGL resize handler used to get the current viewport size.
@ -187,6 +185,7 @@ def resize_gl(w, h):
"""
global __WIDTH
global __HEIGHT
global __current_points
__WIDTH = __current_context.width()
__HEIGHT = __current_context.height()

58
clusterview/points.py

@ -25,12 +25,16 @@ class Point:
self.__point_size = point_size
self.__x = x
self.__y = y
self.__selected = False
self.__viewport_width = viewport_width
self.__viewport_height = viewport_height
half_point = floor(point_size / 2.0)
self.__check_window_bounds(x, y)
self.__selected = False
self.__top_left_corner = (self.__x - half_point,
self.__y + half_point)
@ -52,29 +56,40 @@ class Point:
def selected(self):
return self.__selected
def move(self, dx, dy):
def __check_window_bounds(self, x, y):
"""
Adds the deltas dx and dy to the point.
Simple window bound check that raises an exception when
the point (x, y) exceeds the known viewport bounds.
@param dx The delta in the x direction.
@param dy The delta in the y direction.
@param x The x-coordinate under test.
@param y The y-coordinate under test.
@raises ExceededWindowBoundsError If the viewport bounds are exceeded.
"""
half_point = floor(self.point_size / 2.0)
# Screen size in pixels is always positive
# We need to include the half point here because
# the (x, y) for a point is the center of the square and we
# do not want the EDGES to exceed the viewport bounds.
if (self.__x + dx > self.__viewport_width - half_point or
self.__y + dy > self.__viewport_height - half_point or
self.__x + dx < half_point or self.__y + dy < half_point):
if (x > self.__viewport_width - half_point or
y > self.__viewport_height - half_point or
x < half_point or
y < half_point):
raise ExceededWindowBoundsError
def move(self, dx, dy):
"""
Adds the deltas dx and dy to the point.
@param dx The delta in the x direction.
@param dy The delta in the y direction.
"""
self.__check_window_bounds(self.__x + dx, self.__y + dy)
self.__x += dx
self.__y += dy
def __eq__(self, other):
"""
@ -86,7 +101,6 @@ class Point:
self.__y == other.y and
self.__point_size == other.point_size)
def __hash__(self):
"""
Overridden hashing function so it can be used as a dictionary key
@ -94,7 +108,6 @@ class Point:
"""
return hash((self.__x, self.__y, self.__point_size))
def __repr__(self):
return "POINT<X :{} Y: {} SIZE: {}>".format(self.__x,
self.__y,
@ -182,6 +195,22 @@ class PointSet:
def point_size(self):
return self.__point_size
@property
def viewport_height(self):
return self.__viewport_height
@property
def viewport_width(self):
return self.__viewport_width
@viewport_height.setter
def viewport_height(self, height):
self.__viewport_height = height
@viewport_width.setter
def viewport_width(self, width):
self.__viewport_width = width
def clear_selection(self):
"""
Handy helper function to clear all selected points.
@ -197,6 +226,9 @@ class PointSet:
@param x The x-coordinate.
@param y The y-coordinate.
@param attr An optional attribute.
@raises ExceededWindowBoundsError If the point could not be constructed
because it would be outside the window
bounds.
"""
if attrs != [] and not all(isinstance(x, Attribute) for x in attrs):

3
main_window.py

@ -105,7 +105,8 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
self.__mode = Mode.MOVE
set_drawing_mode(self.__mode)
self.opengl_widget.setCursor(QCursor(Qt.CursorShape.SizeAllCursor))
self.status_bar.showMessage("MOVE MODE")
self.status_bar.showMessage("MOVE MODE - PRESS ESC OR SWITCH MODES TO "+
"CANCEL SELECTION")
clear_selection()
self.opengl_widget.update()

Loading…
Cancel
Save