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

Commit

Permalink
Made the 'Extra Info' button usable and working
Browse files Browse the repository at this point in the history
  • Loading branch information
sumagnadas committed May 2, 2020
1 parent e772c40 commit 3ebaf5d
Show file tree
Hide file tree
Showing 2 changed files with 155 additions and 12 deletions.
72 changes: 65 additions & 7 deletions modules/gui/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
from PySide2 import QtWidgets, QtGui
from PySide2.QtCore import Qt
from PySide2 import QtWidgets, QtGui, QtCore
from .input import NumInput, ShapeList, PointInput
from .info import ExtraInfo
import os.path as pt
from modules import geometry
import datetime
from more_itertools import pairwise, take
import math


shapes = {
Expand Down Expand Up @@ -57,7 +56,7 @@ def __init__(self, ctx, surface, line_color):
clicked=self.save)

# Widget containing the 'Draw', 'Save this Graph' and 'Reset' button
self.buttonBox = QtWidgets.QDialogButtonBox(Qt.Vertical)
self.buttonBox = QtWidgets.QDialogButtonBox(QtCore.Qt.Vertical)
self.buttonBox.addButton(drawButton,
QtWidgets.QDialogButtonBox.ActionRole)
self.buttonBox.addButton(clearButton,
Expand Down Expand Up @@ -88,8 +87,8 @@ def makeInputLayout(self):
self.inputLayout.addRow(self.extraInfo, QtWidgets.QWidget())
self.inputLayout.addRow(self.shapeInfo)
self.scrollArea.setWidget(self.widget)
self.scrollArea.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded)
self.scrollArea.setHorizontalScrollBarPolicy(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 @@ -157,7 +156,7 @@ def paintEvent(self, e):
if self.draw_call:
graph1.load("resources/graph.png")
self.draw_call = False
pen = QtGui.QPen(Qt.black, 5, Qt.SolidLine)
pen = QtGui.QPen(QtCore.Qt.black, 5, QtCore.Qt.SolidLine)
p = QtGui.QPainter()
p.begin(graph1)
p.setPen(pen)
Expand All @@ -171,6 +170,7 @@ def paintEvent(self, e):
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.image.setPixmap(QtGui.QPixmap().fromImage(graph1))
self.graph1 = graph1

Expand All @@ -190,15 +190,73 @@ def addPoint(self):
self.points[index].extend([x1, y1])
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)))

def info(self):
if not self.pressedOnce:
self.extraInfo.setText(self.tr('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}'))

def dist(self):
x0 = int(self.points[0][0].text())
y0 = int(self.points[0][1].text())
x3 = int(self.points[1][0].text())
y3 = int(self.points[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
if self.shapeName.text() == 'Line':
dist = math.sqrt(((x1 - x2) ** 2) + ((y1 - y2) ** 2))
else:
dist = 'N/A'
return dist if isinstance(dist, str) else f'{dist:.2f}'

def sumAngle(self):
sides = len(self.points)
sumOfAngles = (sides - 2) * 180
return str(sumOfAngles)

def sides(self):
if self.shapeName.text() == 'Line':
sides = str(1)
else:
sides = str(len(self.points))
return sides

def shapeType(self):
dist = []
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
distance = math.sqrt(((x1 - x2) ** 2) + ((y1 - y2) ** 2))
dist.append(distance)
except IndexError:
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
distance = math.sqrt(((x1 - x2) ** 2) + ((y1 - y2) ** 2))
dist.append(distance)
return 'Regular' if len(set(dist)) == 1 else 'Irregular'
95 changes: 90 additions & 5 deletions modules/gui/info.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,106 @@
from PySide2 import QtWidgets, QtGui
from PySide2.QtCore import Qt
import math


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

self.sidesLength = QtWidgets.QWidget()
layout = QtWidgets.QFormLayout()
self.distance = QtWidgets.QLabel()
self.totalSumAngle = QtWidgets.QLabel()
self.sides = QtWidgets.QLabel()
self.shapeType = QtWidgets.QLabel()
self.str1 = QtWidgets.QLabel('Length of the line:')
self.distance = QtWidgets.QPushButton('N/A', clicked=)
self.totalSumAngle = QtWidgets.QLabel('N/A')
self.sides = QtWidgets.QLabel('N/A')
self.shapeType = QtWidgets.QLabel('N/A')

layout.addRow('Length of the line:', self.distance)
self.distance.setStyleSheet('border: transparent')
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)

self.setLayout(layout)
self.hide()

def dist(self, points, shapeName):
x0 = int(points[0][0].text())
y0 = int(points[0][1].text())
x3 = int(points[1][0].text())
y3 = int(points[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
if shapeName.text() == 'Line':
dist = math.sqrt(((x1 - x2) ** 2) + ((y1 - y2) ** 2))
else:
dist = 'N/A'
return dist if isinstance(dist, str) else f'{dist:.2f}'

def sumAngle(self, sides, shapeName):
if shapeName.text() == 'Line':
sumOfAngles = 0
else:
sumOfAngles = (sides - 2) * 180
return str(sumOfAngles)

def shapeSides(self, shapeName, points):
if shapeName.text() == 'Line':
sides = 1
else:
sides = len(points)
return sides

def shapeTypeName(self, points):
dist = []
try:
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())
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
distance = math.sqrt(((x1 - x2) ** 2) + ((y1 - y2) ** 2))
dist.append(distance)
except IndexError:
x0 = int(points[-1][0].text())
y0 = int(points[-1][1].text())
x3 = int(points[0][0].text())
y3 = int(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
distance = math.sqrt(((x1 - x2) ** 2) + ((y1 - y2) ** 2))
dist.append(distance)
return 'Regular' if len(set(dist)) == 1 else 'Irregular'

def updateInfo(self, points, shapeName, pointNum):
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())
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}')

0 comments on commit 3ebaf5d

Please sign in to comment.