Browse Source

Make window DPI aware, need to restart on application on the window you wish to run for it to work right now

master
Taylor Bockman 4 years ago
parent
commit
b6d6cecfb7
  1. BIN
      .DS_Store
  2. 11
      main_window.py
  3. 6
      voronoiview.py
  4. 12
      voronoiview.ui
  5. 5
      voronoiview/ui/mode_handlers.py
  6. 25
      voronoiview/ui/opengl_widget.py
  7. 5
      voronoiview_ui.py

BIN
.DS_Store vendored

Binary file not shown.

11
main_window.py

@ -38,13 +38,14 @@ class MainWindow(QMainWindow, Ui_MainWindow):
# Size of point for drawing # Size of point for drawing
self._point_size = 8 self._point_size = 8
# TODO: THESE ARE HARD CODED TO THE CURRENT QT WIDGET SIZES self._viewport_width = self.opengl_widget.width() * self.devicePixelRatio()
# FIX THIS PROPERLY WITH A RESIZE EVENT DETECT. self._viewport_height = self.opengl_widget.height() * self.devicePixelRatio()
print(self.height())
print(self.width())
# PointManager is a class that is filled with static methods # PointManager is a class that is filled with static methods
# designed for managing state. # designed for managing state.
self._viewport_width = 833
self._viewport_height = 656
PointManager.point_set = PointSet(self._point_size, PointManager.point_set = PointSet(self._point_size,
self._viewport_width, self._viewport_width,
self._viewport_height) self._viewport_height)

6
voronoiview.py

@ -1,12 +1,16 @@
import os
import sys import sys
from PyQt5.QtWidgets import QApplication from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import Qt
from main_window import MainWindow from main_window import MainWindow
def main(): def main():
os.environ["QT_AUTO_SCREEN_SCALE_FACTOR"] = "1"
app = QApplication(sys.argv) app = QApplication(sys.argv)
app.setAttribute(Qt.AA_EnableHighDpiScaling)
window = MainWindow() window = MainWindow()
window.show() window.show()
@ -15,4 +19,4 @@ def main():
if __name__ == '__main__': if __name__ == '__main__':
main() main()

12
voronoiview.ui

@ -36,15 +36,21 @@
<item> <item>
<widget class="QOpenGLWidget" name="opengl_widget"> <widget class="QOpenGLWidget" name="opengl_widget">
<property name="sizePolicy"> <property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Preferred"> <sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch> <horstretch>0</horstretch>
<verstretch>0</verstretch> <verstretch>0</verstretch>
</sizepolicy> </sizepolicy>
</property> </property>
<property name="minimumSize">
<size>
<width>833</width>
<height>656</height>
</size>
</property>
<property name="maximumSize"> <property name="maximumSize">
<size> <size>
<width>900</width> <width>833</width>
<height>16777215</height> <height>656</height>
</size> </size>
</property> </property>
</widget> </widget>

5
voronoiview/ui/mode_handlers.py

@ -106,7 +106,8 @@ def _handle_add_point(ctx, event):
try: try:
# No attribute at the moment, default point color is Color.GREY. # No attribute at the moment, default point color is Color.GREY.
PointManager.point_set.add_point(event.x(), event.y(), Color.GREY) PointManager.point_set.add_point(event.x() * ctx.devicePixelRatio(),
event.y() * ctx.devicePixelRatio(), Color.GREY)
except ExceededWindowBoundsError: except ExceededWindowBoundsError:
# The user tried to place a point whos edges would be # The user tried to place a point whos edges would be
# on the outside of the window. We will just ignore it. # on the outside of the window. We will just ignore it.
@ -359,7 +360,7 @@ def _handle_voronoi(ctx, _):
point_arr = [point.array for point in points] point_arr = [point.array for point in points]
PointManager.voronoi_results = Voronoi(point_arr) PointManager.voronoi_results = Voronoi(point_arr, qhull_options='Qbb Qz Qx Qi Qc QJ')
ctx.opengl_widget.update() ctx.opengl_widget.update()
ctx.voronoi_solved = True ctx.voronoi_solved = True

25
voronoiview/ui/opengl_widget.py

@ -13,22 +13,18 @@ It should be split up into a few more separate files eventually...
Probably even into it's own module folder. Probably even into it's own module folder.
""" """
import math
from typing import List
from OpenGL.GL import (glBegin, glClearColor, glColor3f, glColor4f, from OpenGL.GL import (glBegin, glClearColor, glColor3f, glColor4f,
glEnable, glEnd, GL_LINES, GL_LINE_LOOP, GL_LINE_SMOOTH, glEnable, glEnd, GL_LINES, GL_LINE_LOOP, GL_LINE_SMOOTH,
GL_POINTS, glPointSize, glVertex3f, GL_POINTS, glPointSize, glVertex3f,
glViewport) glViewport)
from voronoiview.colors import Color, COLOR_TO_RGBA from voronoiview.colors import (Color, COLOR_TO_RGBA)
from voronoiview.exceptions import (handle_exceptions,
InvalidStateError) from voronoiview.exceptions import InvalidStateError
from voronoiview.mode import Mode from voronoiview.mode import Mode
from voronoiview.point_manager import PointManager from voronoiview.point_manager import PointManager
# Constants set based on the size of the window. # Constants set based on the size of the window.
__BOTTOM_LEFT = (0, 0)
__WIDTH = None __WIDTH = None
__HEIGHT = None __HEIGHT = None
@ -147,9 +143,10 @@ def resize_gl(w, h):
""" """
global __WIDTH global __WIDTH
global __HEIGHT global __HEIGHT
global __current_context
__WIDTH = __current_context.opengl_widget.width() __WIDTH = __current_context.opengl_widget.width() * __current_context.devicePixelRatio()
__HEIGHT = __current_context.opengl_widget.height() __HEIGHT = __current_context.opengl_widget.height() * __current_context.devicePixelRatio()
def viewport_width(): def viewport_width():
@ -160,7 +157,6 @@ def viewport_height():
return __HEIGHT return __HEIGHT
@handle_exceptions
def paint_gl(): def paint_gl():
""" """
Stock PaintGL function from OpenGL that switches Stock PaintGL function from OpenGL that switches
@ -410,15 +406,16 @@ def draw_voronoi_diagram():
color = COLOR_TO_RGBA[Color.BLACK] color = COLOR_TO_RGBA[Color.BLACK]
for region_indices in results.regions: print(results.regions)
# The region index is out of bounds
if -1 in region_indices:
continue
for region_indices in results.regions:
glBegin(GL_LINE_LOOP) glBegin(GL_LINE_LOOP)
for idx in region_indices: for idx in region_indices:
vertex = vertices[idx] vertex = vertices[idx]
if -1 in region_indices:
continue
glColor3f(color[0], color[1], color[2]) glColor3f(color[0], color[1], color[2])
glVertex3f(__clamp_x(vertex[0]), glVertex3f(__clamp_x(vertex[0]),
__clamp_y(vertex[1]), __clamp_y(vertex[1]),

5
voronoiview_ui.py

@ -26,12 +26,13 @@ class Ui_MainWindow(object):
self.horizontalLayout = QtWidgets.QHBoxLayout(self.centralwidget) self.horizontalLayout = QtWidgets.QHBoxLayout(self.centralwidget)
self.horizontalLayout.setObjectName("horizontalLayout") self.horizontalLayout.setObjectName("horizontalLayout")
self.opengl_widget = QtWidgets.QOpenGLWidget(self.centralwidget) self.opengl_widget = QtWidgets.QOpenGLWidget(self.centralwidget)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Preferred) sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0) sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0) sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.opengl_widget.sizePolicy().hasHeightForWidth()) sizePolicy.setHeightForWidth(self.opengl_widget.sizePolicy().hasHeightForWidth())
self.opengl_widget.setSizePolicy(sizePolicy) self.opengl_widget.setSizePolicy(sizePolicy)
self.opengl_widget.setMaximumSize(QtCore.QSize(900, 16777215)) self.opengl_widget.setMinimumSize(QtCore.QSize(833, 656))
self.opengl_widget.setMaximumSize(QtCore.QSize(833, 656))
self.opengl_widget.setObjectName("opengl_widget") self.opengl_widget.setObjectName("opengl_widget")
self.horizontalLayout.addWidget(self.opengl_widget) self.horizontalLayout.addWidget(self.opengl_widget)
self.verticalLayout = QtWidgets.QVBoxLayout() self.verticalLayout = QtWidgets.QVBoxLayout()

Loading…
Cancel
Save