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

Commit

Permalink
Added a button for showing the distances between different points
Browse files Browse the repository at this point in the history
The button shows up inside 'Extra Info' dropdown
  • Loading branch information
sumagnadas committed May 2, 2020
1 parent 3ebaf5d commit 811124b
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 42 deletions.
59 changes: 38 additions & 21 deletions modules/gui/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import datetime
import math


shapes = {
3: 'Triangle',
4: 'Quadrilateral',
Expand All @@ -17,6 +16,8 @@
9: 'Enneagon',
10: 'Decagon'
}


class Graph(QtWidgets.QWidget):
"""Class which defines the window of the application"""

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

# 'Draw' Button to draw the line using user-given coordinates
drawButton = QtWidgets.QPushButton(self.tr("Draw"),
clicked=self.draw)
clicked=self.draw)
# 'Reset' Button to clear everything in the input boxes
clearButton = QtWidgets.QPushButton(self.tr("Reset"),
clicked=self.clear)
clicked=self.clear)
# Save Button for saving the plotted graph with transparent background
save = QtWidgets.QPushButton(self.tr("Save this Graph"),
clicked=self.save)
Expand All @@ -73,22 +74,26 @@ def __init__(self, ctx, surface, line_color):
self.line_color = line_color

def makeInputLayout(self):
text = 'Extra Info \N{Black Down-Pointing Triangle}'
self.inputLayout = QtWidgets.QFormLayout()
self.coordLayout = QtWidgets.QFormLayout()
self.scrollArea = QtWidgets.QScrollArea()
self.widget = QtWidgets.QWidget()
self.str = QtWidgets.QLabel('Geometric Figure:')
self.shapeName = QtWidgets.QLabel('Line')
self.extraInfo = QtWidgets.QPushButton(self.tr('Extra Info \N{Black Down-Pointing Triangle}'), clicked=self.info)
self.extraInfo.setToolTip('Show extra information for the line or shape')
self.extraInfo = QtWidgets.QPushButton(text, clicked=self.info)
self.extraInfo.setToolTip(('Show extra information '
'for the line or shape'))
self.shapeInfo = ExtraInfo()
self.widget.setLayout(self.coordLayout)
self.inputLayout.addRow(self.str, self.shapeName)
self.inputLayout.addRow(self.extraInfo, QtWidgets.QWidget())
self.inputLayout.addRow(self.shapeInfo)
self.scrollArea.setWidget(self.widget)
self.scrollArea.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAsNeeded)
self.scrollArea.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.scrollArea.setVerticalScrollBarPolicy(
QtCore.Qt.ScrollBarAsNeeded)
self.scrollArea.setHorizontalScrollBarPolicy(
QtCore.Qt.ScrollBarAlwaysOff)
self.scrollArea.setWidgetResizable(True)
self.inputLayout.addRow(self.scrollArea)

Expand Down Expand Up @@ -162,15 +167,21 @@ def paintEvent(self, e):
p.setPen(pen)
try:
for i in range(0, len(self.points)):
p1 = geometry.Point(int(self.points[i][0].text()), int(self.points[i][1].text()))
p2 = geometry.Point(int(self.points[i+1][0].text()), int(self.points[i+1][1].text()))
p1 = geometry.Point(int(self.points[i][0].text()),
int(self.points[i][1].text()))
p2 = geometry.Point(int(self.points[i + 1][0].text()),
int(self.points[i + 1][1].text()))
p.drawLine(p1.x(), p1.y(), p2.x(), p2.y())
except IndexError:
p1 = geometry.Point(int(self.points[-1][0].text()), int(self.points[-1][1].text()))
p2 = geometry.Point(int(self.points[0][0].text()), int(self.points[0][1].text()))
p1 = geometry.Point(int(self.points[-1][0].text()),
int(self.points[-1][1].text()))
p2 = geometry.Point(int(self.points[0][0].text()),
int(self.points[0][1].text()))
p.drawLine(p1.x(), p1.y(), p2.x(), p2.y())
p.end()
self.shapeInfo.updateInfo(self.points, self.shapeName)
self.shapeInfo.updateInfo(self.points,
self.shapeName,
self.pointNum)
self.image.setPixmap(QtGui.QPixmap().fromImage(graph1))
self.graph1 = graph1

Expand All @@ -188,24 +199,27 @@ def addPoint(self):
self.points.append([])
index = len(self.points) - 1
self.points[index].extend([x1, y1])
self.input = PointInput('Point'+str(self.pointNum), self.points[index][0], self.points[index][1])
self.input = PointInput('Point' + str(self.pointNum),
self.points[index][0],
self.points[index][1])
self.coordLayout.insertRow(self.coordLayout.rowCount(), self.input)
self.shapeInfo.str1.setText('Length of the sides:')
self.shapeInfo.distance.setStyleSheet('background:solid #F2F3f4')
self.pointNum += 1
self.shapeName.setText(shapes.get(index + 1, 'Undefined shape with {} number of sides'.format(index + 1)))
self.shapeName.setText(shapes.get(index + 1,
('Undefined shape with {} number of '
'sides').format(index + 1)))

def info(self):
if not self.pressedOnce:
self.extraInfo.setText(self.tr('Extra Info \N{Black Up-Pointing Triangle}'))
if not self.pressedOnce:
self.extraInfo.setText('Extra Info \N{Black Up-Pointing Triangle}')
self.shapeInfo.show()
self.extraInfo.setStyleSheet('border: transparent')
self.pressedOnce = True
else:
self.shapeInfo.hide()
self.pressedOnce = False
self.extraInfo.setStyleSheet('background:solid #F2F3f4')
self.extraInfo.setText(self.tr('Extra Info \N{Black Down-Pointing Triangle}'))
self.extraInfo.setText(('Extra Info'
'\N{Black Down-Pointing Triangle}'))

def dist(self):
x0 = int(self.points[0][0].text())
Expand Down Expand Up @@ -240,8 +254,8 @@ def shapeType(self):
for i in range(0, len(self.points)):
x0 = int(self.points[i][0].text())
y0 = int(self.points[i][1].text())
x3 = int(self.points[i+1][0].text())
y3 = int(self.points[i+1][1].text())
x3 = int(self.points[i + 1][0].text())
y3 = int(self.points[i + 1][1].text())
x1 = x0 if x0 >= x3 else x3
y1 = y0 if y0 >= y3 else y3
x2 = x0 if x0 < x3 else x3
Expand All @@ -260,3 +274,6 @@ def shapeType(self):
distance = math.sqrt(((x1 - x2) ** 2) + ((y1 - y2) ** 2))
dist.append(distance)
return 'Regular' if len(set(dist)) == 1 else 'Irregular'

def closeEvent(self, e):
self.shapeInfo.sidesLength.close()
65 changes: 46 additions & 19 deletions modules/gui/info.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
from PySide2 import QtWidgets, QtGui
from PySide2.QtCore import Qt
import math
import time


class ExtraInfo(QtWidgets.QWidget):
def __init__(self):
super().__init__()

self.sidesLength = QtWidgets.QWidget()
self.updateNeeded = True
layout = QtWidgets.QFormLayout()
self.str1 = QtWidgets.QLabel('Length of the line:')
self.distance = QtWidgets.QPushButton('N/A', clicked=)
self.distance = QtWidgets.QPushButton('N/A', clicked=self.sidesDialog)
self.totalSumAngle = QtWidgets.QLabel('N/A')
self.sides = QtWidgets.QLabel('N/A')
self.shapeType = QtWidgets.QLabel('N/A')
Expand All @@ -19,7 +20,8 @@ def __init__(self):
layout.addRow(self.str1, self.distance)
layout.addRow('Sum of all the angles:', self.totalSumAngle)
layout.addRow('Number of sides:', self.sides)
layout.addRow('Type of the shape\n(Regular or Irregular):', self.shapeType)
layout.addRow('Type of the shape\n(Regular or Irregular):',
self.shapeType)

self.setLayout(layout)
self.hide()
Expand All @@ -36,7 +38,7 @@ def dist(self, points, shapeName):
if shapeName.text() == 'Line':
dist = math.sqrt(((x1 - x2) ** 2) + ((y1 - y2) ** 2))
else:
dist = 'N/A'
dist = 'Open'
return dist if isinstance(dist, str) else f'{dist:.2f}'

def sumAngle(self, sides, shapeName):
Expand All @@ -59,8 +61,8 @@ def shapeTypeName(self, points):
for i in range(0, len(points)):
x0 = int(points[i][0].text())
y0 = int(points[i][1].text())
x3 = int(points[i+1][0].text())
y3 = int(points[i+1][1].text())
x3 = int(points[i + 1][0].text())
y3 = int(points[i + 1][1].text())
x1 = x0 if x0 >= x3 else x3
y1 = y0 if y0 >= y3 else y3
x2 = x0 if x0 < x3 else x3
Expand All @@ -81,26 +83,51 @@ def shapeTypeName(self, points):
return 'Regular' if len(set(dist)) == 1 else 'Irregular'

def updateInfo(self, points, shapeName, pointNum):
self.pointNum = pointNum
self.points = points
self.distance.setText(self.dist(points, shapeName))
noOfSides = self.shapeSides(shapeName, points)
self.totalSumAngle.setText(self.sumAngle(noOfSides, shapeName))
self.sides.setText(str(noOfSides))
self.shapeType.setText(self.shapeTypeName(points))

def sidesDialog(self, pointNum):
self.sidesLayout = QtWidgets.QFormLayout()
x = 10
for i in range(0, pointNum):
if not (i+1) > pointNum:
x0 = int(points[i][0].text())
y0 = int(points[i][1].text())
x3 = int(points[i-1][0].text())
y3 = int(points[i-1][1].text())
def sidesDialog(self):
if True:
self.sidesLength = QtWidgets.QWidget()
self.sidesLayout = QtWidgets.QFormLayout()
self.sidesLayout.addRow(QtWidgets.QLabel(('Lengths of'
'the lines :-')))
try:
for i in range(0, len(self.points)):
x0 = int(self.points[i][0].text())
y0 = int(self.points[i][1].text())
x3 = int(self.points[i + 1][0].text())
y3 = int(self.points[i + 1][1].text())
x1 = x0 if x0 >= x3 else x3
y1 = y0 if y0 >= y3 else y3
x2 = x0 if x0 < x3 else x3
y2 = y0 if y0 < y3 else y3
dist = math.sqrt(((x1 - x2) ** 2) + ((y1 - y2) ** 2))
if i + 2 > len(self.points):
raise Exception
else:
self.sidesLayout.addRow(f'P{i+1} to P{i+2} = ',
QtWidgets.QLabel(str(dist)))
except (IndexError, Exception):
x0 = int(self.points[-1][0].text())
y0 = int(self.points[-1][1].text())
x3 = int(self.points[0][0].text())
y3 = int(self.points[0][1].text())
x1 = x0 if x0 >= x3 else x3
y1 = y0 if y0 >= y3 else y3
x2 = x0 if x0 < x3 else x3
y2 = y0 if y0 < y3 else y3
if shapeName.text() == 'Line':
dist = math.sqrt(((x1 - x2) ** 2) + ((y1 - y2) ** 2))
self.sidesLayout.addRow('Lengths of the lines :-')
self.sidesLayout.addRow(f'P{i} to P{i+1}')
dist = math.sqrt(((x1 - x2) ** 2) + ((y1 - y2) ** 2))
self.sidesLayout.addRow(f'P{len(self.points)} to P1 = ',
QtWidgets.QLabel(str(dist)))
self.sidesLength.setLayout(self.sidesLayout)
if self.sidesLength.isHidden():
self.sidesLength.show()
del self.sidesLayout
else:
pass
8 changes: 6 additions & 2 deletions modules/gui/input.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,22 @@ def __init__(self):
onlyInt = QtGui.QIntValidator(-300, 300)
self.setValidator(onlyInt)
self.setText("0")
self.setSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Minimum)
self.setSizePolicy(QtWidgets.QSizePolicy.Minimum,
QtWidgets.QSizePolicy.Minimum)

def setzero(self):
if self.text() == '':
self.setText('0')


class ShapeList(QtWidgets.QComboBox):

def __init__(self):
super().__init__()
self.addItems(['Line', 'Triangle', 'Quadrilateral', 'Polygon'])
self.setSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Minimum)
self.setSizePolicy(QtWidgets.QSizePolicy.Minimum,
QtWidgets.QSizePolicy.Minimum)


class PointInput(QtWidgets.QWidget):
def __init__(self, str, x, y):
Expand Down

0 comments on commit 811124b

Please sign in to comment.