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

Commit 811124b

Browse files
committed
Added a button for showing the distances between different points
The button shows up inside 'Extra Info' dropdown
1 parent 3ebaf5d commit 811124b

File tree

3 files changed

+90
-42
lines changed

3 files changed

+90
-42
lines changed

modules/gui/__init__.py

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import datetime
77
import math
88

9-
109
shapes = {
1110
3: 'Triangle',
1211
4: 'Quadrilateral',
@@ -17,6 +16,8 @@
1716
9: 'Enneagon',
1817
10: 'Decagon'
1918
}
19+
20+
2021
class Graph(QtWidgets.QWidget):
2122
"""Class which defines the window of the application"""
2223

@@ -47,10 +48,10 @@ def __init__(self, ctx, surface, line_color):
4748

4849
# 'Draw' Button to draw the line using user-given coordinates
4950
drawButton = QtWidgets.QPushButton(self.tr("Draw"),
50-
clicked=self.draw)
51+
clicked=self.draw)
5152
# 'Reset' Button to clear everything in the input boxes
5253
clearButton = QtWidgets.QPushButton(self.tr("Reset"),
53-
clicked=self.clear)
54+
clicked=self.clear)
5455
# Save Button for saving the plotted graph with transparent background
5556
save = QtWidgets.QPushButton(self.tr("Save this Graph"),
5657
clicked=self.save)
@@ -73,22 +74,26 @@ def __init__(self, ctx, surface, line_color):
7374
self.line_color = line_color
7475

7576
def makeInputLayout(self):
77+
text = 'Extra Info \N{Black Down-Pointing Triangle}'
7678
self.inputLayout = QtWidgets.QFormLayout()
7779
self.coordLayout = QtWidgets.QFormLayout()
7880
self.scrollArea = QtWidgets.QScrollArea()
7981
self.widget = QtWidgets.QWidget()
8082
self.str = QtWidgets.QLabel('Geometric Figure:')
8183
self.shapeName = QtWidgets.QLabel('Line')
82-
self.extraInfo = QtWidgets.QPushButton(self.tr('Extra Info \N{Black Down-Pointing Triangle}'), clicked=self.info)
83-
self.extraInfo.setToolTip('Show extra information for the line or shape')
84+
self.extraInfo = QtWidgets.QPushButton(text, clicked=self.info)
85+
self.extraInfo.setToolTip(('Show extra information '
86+
'for the line or shape'))
8487
self.shapeInfo = ExtraInfo()
8588
self.widget.setLayout(self.coordLayout)
8689
self.inputLayout.addRow(self.str, self.shapeName)
8790
self.inputLayout.addRow(self.extraInfo, QtWidgets.QWidget())
8891
self.inputLayout.addRow(self.shapeInfo)
8992
self.scrollArea.setWidget(self.widget)
90-
self.scrollArea.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAsNeeded)
91-
self.scrollArea.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
93+
self.scrollArea.setVerticalScrollBarPolicy(
94+
QtCore.Qt.ScrollBarAsNeeded)
95+
self.scrollArea.setHorizontalScrollBarPolicy(
96+
QtCore.Qt.ScrollBarAlwaysOff)
9297
self.scrollArea.setWidgetResizable(True)
9398
self.inputLayout.addRow(self.scrollArea)
9499

@@ -162,15 +167,21 @@ def paintEvent(self, e):
162167
p.setPen(pen)
163168
try:
164169
for i in range(0, len(self.points)):
165-
p1 = geometry.Point(int(self.points[i][0].text()), int(self.points[i][1].text()))
166-
p2 = geometry.Point(int(self.points[i+1][0].text()), int(self.points[i+1][1].text()))
170+
p1 = geometry.Point(int(self.points[i][0].text()),
171+
int(self.points[i][1].text()))
172+
p2 = geometry.Point(int(self.points[i + 1][0].text()),
173+
int(self.points[i + 1][1].text()))
167174
p.drawLine(p1.x(), p1.y(), p2.x(), p2.y())
168175
except IndexError:
169-
p1 = geometry.Point(int(self.points[-1][0].text()), int(self.points[-1][1].text()))
170-
p2 = geometry.Point(int(self.points[0][0].text()), int(self.points[0][1].text()))
176+
p1 = geometry.Point(int(self.points[-1][0].text()),
177+
int(self.points[-1][1].text()))
178+
p2 = geometry.Point(int(self.points[0][0].text()),
179+
int(self.points[0][1].text()))
171180
p.drawLine(p1.x(), p1.y(), p2.x(), p2.y())
172181
p.end()
173-
self.shapeInfo.updateInfo(self.points, self.shapeName)
182+
self.shapeInfo.updateInfo(self.points,
183+
self.shapeName,
184+
self.pointNum)
174185
self.image.setPixmap(QtGui.QPixmap().fromImage(graph1))
175186
self.graph1 = graph1
176187

@@ -188,24 +199,27 @@ def addPoint(self):
188199
self.points.append([])
189200
index = len(self.points) - 1
190201
self.points[index].extend([x1, y1])
191-
self.input = PointInput('Point'+str(self.pointNum), self.points[index][0], self.points[index][1])
202+
self.input = PointInput('Point' + str(self.pointNum),
203+
self.points[index][0],
204+
self.points[index][1])
192205
self.coordLayout.insertRow(self.coordLayout.rowCount(), self.input)
193206
self.shapeInfo.str1.setText('Length of the sides:')
194207
self.shapeInfo.distance.setStyleSheet('background:solid #F2F3f4')
195208
self.pointNum += 1
196-
self.shapeName.setText(shapes.get(index + 1, 'Undefined shape with {} number of sides'.format(index + 1)))
209+
self.shapeName.setText(shapes.get(index + 1,
210+
('Undefined shape with {} number of '
211+
'sides').format(index + 1)))
197212

198213
def info(self):
199-
if not self.pressedOnce:
200-
self.extraInfo.setText(self.tr('Extra Info \N{Black Up-Pointing Triangle}'))
214+
if not self.pressedOnce:
215+
self.extraInfo.setText('Extra Info \N{Black Up-Pointing Triangle}')
201216
self.shapeInfo.show()
202-
self.extraInfo.setStyleSheet('border: transparent')
203217
self.pressedOnce = True
204218
else:
205219
self.shapeInfo.hide()
206220
self.pressedOnce = False
207-
self.extraInfo.setStyleSheet('background:solid #F2F3f4')
208-
self.extraInfo.setText(self.tr('Extra Info \N{Black Down-Pointing Triangle}'))
221+
self.extraInfo.setText(('Extra Info'
222+
'\N{Black Down-Pointing Triangle}'))
209223

210224
def dist(self):
211225
x0 = int(self.points[0][0].text())
@@ -240,8 +254,8 @@ def shapeType(self):
240254
for i in range(0, len(self.points)):
241255
x0 = int(self.points[i][0].text())
242256
y0 = int(self.points[i][1].text())
243-
x3 = int(self.points[i+1][0].text())
244-
y3 = int(self.points[i+1][1].text())
257+
x3 = int(self.points[i + 1][0].text())
258+
y3 = int(self.points[i + 1][1].text())
245259
x1 = x0 if x0 >= x3 else x3
246260
y1 = y0 if y0 >= y3 else y3
247261
x2 = x0 if x0 < x3 else x3
@@ -260,3 +274,6 @@ def shapeType(self):
260274
distance = math.sqrt(((x1 - x2) ** 2) + ((y1 - y2) ** 2))
261275
dist.append(distance)
262276
return 'Regular' if len(set(dist)) == 1 else 'Irregular'
277+
278+
def closeEvent(self, e):
279+
self.shapeInfo.sidesLength.close()

modules/gui/info.py

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
from PySide2 import QtWidgets, QtGui
22
from PySide2.QtCore import Qt
33
import math
4+
import time
45

56

67
class ExtraInfo(QtWidgets.QWidget):
78
def __init__(self):
89
super().__init__()
910

10-
self.sidesLength = QtWidgets.QWidget()
11+
self.updateNeeded = True
1112
layout = QtWidgets.QFormLayout()
1213
self.str1 = QtWidgets.QLabel('Length of the line:')
13-
self.distance = QtWidgets.QPushButton('N/A', clicked=)
14+
self.distance = QtWidgets.QPushButton('N/A', clicked=self.sidesDialog)
1415
self.totalSumAngle = QtWidgets.QLabel('N/A')
1516
self.sides = QtWidgets.QLabel('N/A')
1617
self.shapeType = QtWidgets.QLabel('N/A')
@@ -19,7 +20,8 @@ def __init__(self):
1920
layout.addRow(self.str1, self.distance)
2021
layout.addRow('Sum of all the angles:', self.totalSumAngle)
2122
layout.addRow('Number of sides:', self.sides)
22-
layout.addRow('Type of the shape\n(Regular or Irregular):', self.shapeType)
23+
layout.addRow('Type of the shape\n(Regular or Irregular):',
24+
self.shapeType)
2325

2426
self.setLayout(layout)
2527
self.hide()
@@ -36,7 +38,7 @@ def dist(self, points, shapeName):
3638
if shapeName.text() == 'Line':
3739
dist = math.sqrt(((x1 - x2) ** 2) + ((y1 - y2) ** 2))
3840
else:
39-
dist = 'N/A'
41+
dist = 'Open'
4042
return dist if isinstance(dist, str) else f'{dist:.2f}'
4143

4244
def sumAngle(self, sides, shapeName):
@@ -59,8 +61,8 @@ def shapeTypeName(self, points):
5961
for i in range(0, len(points)):
6062
x0 = int(points[i][0].text())
6163
y0 = int(points[i][1].text())
62-
x3 = int(points[i+1][0].text())
63-
y3 = int(points[i+1][1].text())
64+
x3 = int(points[i + 1][0].text())
65+
y3 = int(points[i + 1][1].text())
6466
x1 = x0 if x0 >= x3 else x3
6567
y1 = y0 if y0 >= y3 else y3
6668
x2 = x0 if x0 < x3 else x3
@@ -81,26 +83,51 @@ def shapeTypeName(self, points):
8183
return 'Regular' if len(set(dist)) == 1 else 'Irregular'
8284

8385
def updateInfo(self, points, shapeName, pointNum):
86+
self.pointNum = pointNum
87+
self.points = points
8488
self.distance.setText(self.dist(points, shapeName))
8589
noOfSides = self.shapeSides(shapeName, points)
8690
self.totalSumAngle.setText(self.sumAngle(noOfSides, shapeName))
8791
self.sides.setText(str(noOfSides))
8892
self.shapeType.setText(self.shapeTypeName(points))
8993

90-
def sidesDialog(self, pointNum):
91-
self.sidesLayout = QtWidgets.QFormLayout()
92-
x = 10
93-
for i in range(0, pointNum):
94-
if not (i+1) > pointNum:
95-
x0 = int(points[i][0].text())
96-
y0 = int(points[i][1].text())
97-
x3 = int(points[i-1][0].text())
98-
y3 = int(points[i-1][1].text())
94+
def sidesDialog(self):
95+
if True:
96+
self.sidesLength = QtWidgets.QWidget()
97+
self.sidesLayout = QtWidgets.QFormLayout()
98+
self.sidesLayout.addRow(QtWidgets.QLabel(('Lengths of'
99+
'the lines :-')))
100+
try:
101+
for i in range(0, len(self.points)):
102+
x0 = int(self.points[i][0].text())
103+
y0 = int(self.points[i][1].text())
104+
x3 = int(self.points[i + 1][0].text())
105+
y3 = int(self.points[i + 1][1].text())
106+
x1 = x0 if x0 >= x3 else x3
107+
y1 = y0 if y0 >= y3 else y3
108+
x2 = x0 if x0 < x3 else x3
109+
y2 = y0 if y0 < y3 else y3
110+
dist = math.sqrt(((x1 - x2) ** 2) + ((y1 - y2) ** 2))
111+
if i + 2 > len(self.points):
112+
raise Exception
113+
else:
114+
self.sidesLayout.addRow(f'P{i+1} to P{i+2} = ',
115+
QtWidgets.QLabel(str(dist)))
116+
except (IndexError, Exception):
117+
x0 = int(self.points[-1][0].text())
118+
y0 = int(self.points[-1][1].text())
119+
x3 = int(self.points[0][0].text())
120+
y3 = int(self.points[0][1].text())
99121
x1 = x0 if x0 >= x3 else x3
100122
y1 = y0 if y0 >= y3 else y3
101123
x2 = x0 if x0 < x3 else x3
102124
y2 = y0 if y0 < y3 else y3
103-
if shapeName.text() == 'Line':
104-
dist = math.sqrt(((x1 - x2) ** 2) + ((y1 - y2) ** 2))
105-
self.sidesLayout.addRow('Lengths of the lines :-')
106-
self.sidesLayout.addRow(f'P{i} to P{i+1}')
125+
dist = math.sqrt(((x1 - x2) ** 2) + ((y1 - y2) ** 2))
126+
self.sidesLayout.addRow(f'P{len(self.points)} to P1 = ',
127+
QtWidgets.QLabel(str(dist)))
128+
self.sidesLength.setLayout(self.sidesLayout)
129+
if self.sidesLength.isHidden():
130+
self.sidesLength.show()
131+
del self.sidesLayout
132+
else:
133+
pass

modules/gui/input.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,22 @@ def __init__(self):
1313
onlyInt = QtGui.QIntValidator(-300, 300)
1414
self.setValidator(onlyInt)
1515
self.setText("0")
16-
self.setSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Minimum)
16+
self.setSizePolicy(QtWidgets.QSizePolicy.Minimum,
17+
QtWidgets.QSizePolicy.Minimum)
1718

1819
def setzero(self):
1920
if self.text() == '':
2021
self.setText('0')
2122

23+
2224
class ShapeList(QtWidgets.QComboBox):
2325

2426
def __init__(self):
2527
super().__init__()
2628
self.addItems(['Line', 'Triangle', 'Quadrilateral', 'Polygon'])
27-
self.setSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Minimum)
29+
self.setSizePolicy(QtWidgets.QSizePolicy.Minimum,
30+
QtWidgets.QSizePolicy.Minimum)
31+
2832

2933
class PointInput(QtWidgets.QWidget):
3034
def __init__(self, str, x, y):

0 commit comments

Comments
 (0)