Skip to content
This repository was archived by the owner on Oct 12, 2020. It is now read-only.

Commit b34e5d9

Browse files
committed
Some major changes -
1) Fixed some problems with the point system 2) Added a dialog for choosing where to save the graph
1 parent 811124b commit b34e5d9

File tree

3 files changed

+38
-16
lines changed

3 files changed

+38
-16
lines changed

modules/geometry.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ class Point(QPoint):
1717

1818
def __init__(self, x=0, y=0):
1919
x *= 14.85
20-
x += 300
20+
x += 299
2121
y *= 14.85
22-
y = -y + 300
22+
y = -y + 299
2323
super().__init__(x, y)
2424

2525

modules/gui/__init__.py

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
from PySide2 import QtWidgets, QtGui, QtCore
2+
from PySide2.QtWidgets import QFileDialog, QDialog
23
from .input import NumInput, ShapeList, PointInput
34
from .info import ExtraInfo
4-
import os.path as pt
5+
from os.path import abspath
56
from modules import geometry
67
import datetime
78
import math
@@ -24,6 +25,7 @@ class Graph(QtWidgets.QWidget):
2425
def __init__(self, ctx, surface, line_color):
2526
super().__init__()
2627

28+
self.errorSaving = QtWidgets.QDialog()
2729
self.fileDir = ''
2830
self.save_call = False
2931
self.draw_call = False
@@ -91,15 +93,28 @@ def makeInputLayout(self):
9193
self.inputLayout.addRow(self.shapeInfo)
9294
self.scrollArea.setWidget(self.widget)
9395
self.scrollArea.setVerticalScrollBarPolicy(
94-
QtCore.Qt.ScrollBarAsNeeded)
96+
QtCore.Qt.ScrollBarAsNeeded)
9597
self.scrollArea.setHorizontalScrollBarPolicy(
96-
QtCore.Qt.ScrollBarAlwaysOff)
98+
QtCore.Qt.ScrollBarAlwaysOff)
9799
self.scrollArea.setWidgetResizable(True)
98100
self.inputLayout.addRow(self.scrollArea)
99101

102+
def makeErrorDialog(self):
103+
imageObj = QtGui.QImage('resources/error_symbol.ico')
104+
caption = self.tr('Graph has to be drawn before saving')
105+
image = QtWidgets.QLabel()
106+
image.setPixmap(QtGui.QPixmap().fromImage(imageObj))
107+
label = QtWidgets.QLabel(caption)
108+
errorLayout = QtWidgets.QBoxLayout(QtWidgets.QBoxLayout.LeftToRight)
109+
errorLayout.addWidget(image)
110+
errorLayout.addWidget(label)
111+
self.errorSaving.setLayout(errorLayout)
112+
self.errorSaving.setModal(True)
113+
100114
def makeWindowLayout(self, buttonBox):
101115
'''Make the layout of the window'''
102116

117+
self.makeErrorDialog()
103118
self.pointinput = QtWidgets.QFormLayout()
104119
input1 = PointInput('Point1', self.points[0][0], self.points[0][1])
105120
input2 = PointInput("Point2", self.points[1][0], self.points[1][1])
@@ -119,7 +134,7 @@ def makeWindowLayout(self, buttonBox):
119134
def setImage(self, filename):
120135
'''Set the image for the graph'''
121136

122-
self.graph_image = QtGui.QImage(pt.abspath(filename))
137+
self.graph_image = QtGui.QImage(abspath(filename))
123138
self.image.setPixmap(QtGui.QPixmap().fromImage(self.graph_image))
124139

125140
def draw(self):
@@ -145,8 +160,19 @@ def clear(self):
145160
self.image.setPixmap("resources/graph.png")
146161

147162
def save(self):
148-
self.save_call = True
149-
self.image.update()
163+
if hasattr(self, 'graph1'):
164+
caption = self.tr('Choose a filename for saving the graph')
165+
currentDT = datetime.datetime.now()
166+
currentTime = currentDT.strftime("%H:%M:%S")
167+
defaultFilename = self.fileDir + "graph-" + currentTime + ".png"
168+
filename = QFileDialog.getSaveFileName(self,
169+
caption,
170+
abspath('./untitled.png'),
171+
self.tr('Images (*.png)'))
172+
name = filename[0] if len(filename[0]) > 1 else defaultFilename
173+
self.graph1.save(name, "PNG")
174+
else:
175+
self.errorSaving.show()
150176

151177
def check(self):
152178
for i in self.points:
@@ -185,13 +211,6 @@ def paintEvent(self, e):
185211
self.image.setPixmap(QtGui.QPixmap().fromImage(graph1))
186212
self.graph1 = graph1
187213

188-
elif self.save_call:
189-
self.save_call = False
190-
currentDT = datetime.datetime.now()
191-
currentTime = currentDT.strftime("%H:%M:%S")
192-
filename = self.fileDir + "graph-" + currentTime + ".png"
193-
self.graph1.save(filename, "PNG")
194-
195214
def addPoint(self):
196215
x1 = NumInput()
197216
y1 = NumInput()
@@ -276,4 +295,7 @@ def shapeType(self):
276295
return 'Regular' if len(set(dist)) == 1 else 'Irregular'
277296

278297
def closeEvent(self, e):
279-
self.shapeInfo.sidesLength.close()
298+
if hasattr(self.shapeInfo, 'sidesLength'):
299+
self.shapeInfo.sidesLength.close()
300+
else:
301+
e.accept()

resources/error_symbol.ico

16.6 KB
Binary file not shown.

0 commit comments

Comments
 (0)