1
1
from PySide2 import QtWidgets , QtGui , QtCore
2
+ from PySide2 .QtWidgets import QFileDialog , QDialog
2
3
from .input import NumInput , ShapeList , PointInput
3
4
from .info import ExtraInfo
4
- import os .path as pt
5
+ from os .path import abspath
5
6
from modules import geometry
6
7
import datetime
7
8
import math
@@ -24,6 +25,7 @@ class Graph(QtWidgets.QWidget):
24
25
def __init__ (self , ctx , surface , line_color ):
25
26
super ().__init__ ()
26
27
28
+ self .errorSaving = QtWidgets .QDialog ()
27
29
self .fileDir = ''
28
30
self .save_call = False
29
31
self .draw_call = False
@@ -91,15 +93,28 @@ def makeInputLayout(self):
91
93
self .inputLayout .addRow (self .shapeInfo )
92
94
self .scrollArea .setWidget (self .widget )
93
95
self .scrollArea .setVerticalScrollBarPolicy (
94
- QtCore .Qt .ScrollBarAsNeeded )
96
+ QtCore .Qt .ScrollBarAsNeeded )
95
97
self .scrollArea .setHorizontalScrollBarPolicy (
96
- QtCore .Qt .ScrollBarAlwaysOff )
98
+ QtCore .Qt .ScrollBarAlwaysOff )
97
99
self .scrollArea .setWidgetResizable (True )
98
100
self .inputLayout .addRow (self .scrollArea )
99
101
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
+
100
114
def makeWindowLayout (self , buttonBox ):
101
115
'''Make the layout of the window'''
102
116
117
+ self .makeErrorDialog ()
103
118
self .pointinput = QtWidgets .QFormLayout ()
104
119
input1 = PointInput ('Point1' , self .points [0 ][0 ], self .points [0 ][1 ])
105
120
input2 = PointInput ("Point2" , self .points [1 ][0 ], self .points [1 ][1 ])
@@ -119,7 +134,7 @@ def makeWindowLayout(self, buttonBox):
119
134
def setImage (self , filename ):
120
135
'''Set the image for the graph'''
121
136
122
- self .graph_image = QtGui .QImage (pt . abspath (filename ))
137
+ self .graph_image = QtGui .QImage (abspath (filename ))
123
138
self .image .setPixmap (QtGui .QPixmap ().fromImage (self .graph_image ))
124
139
125
140
def draw (self ):
@@ -145,8 +160,19 @@ def clear(self):
145
160
self .image .setPixmap ("resources/graph.png" )
146
161
147
162
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 ()
150
176
151
177
def check (self ):
152
178
for i in self .points :
@@ -185,13 +211,6 @@ def paintEvent(self, e):
185
211
self .image .setPixmap (QtGui .QPixmap ().fromImage (graph1 ))
186
212
self .graph1 = graph1
187
213
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
-
195
214
def addPoint (self ):
196
215
x1 = NumInput ()
197
216
y1 = NumInput ()
@@ -276,4 +295,7 @@ def shapeType(self):
276
295
return 'Regular' if len (set (dist )) == 1 else 'Irregular'
277
296
278
297
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 ()
0 commit comments