Skip to content
Snippets Groups Projects
Commit 2bca65f8 authored by Lucas Philippe's avatar Lucas Philippe
Browse files

Ré-échantillonage d'un tracé

parent 72f4071d
No related branches found
No related tags found
No related merge requests found
No preview for this file type
......@@ -14,8 +14,8 @@
{ "symbol": "i", "x" : 7, "y" : 1, "width": 1 },
{ "symbol": "o", "x" : 8, "y" : 1, "width": 1 },
{ "symbol": "p", "x" : 9, "y" : 1, "width": 1 },
{ "symbol": "q", "x" : 1, "y" : 2, "width": 1 },
{ "symbol": "s", "x" : 0, "y" : 2, "width": 1 },
{ "symbol": "q", "x" : 0, "y" : 2, "width": 1 },
{ "symbol": "s", "x" : 1, "y" : 2, "width": 1 },
{ "symbol": "d", "x" : 2, "y" : 2, "width": 1 },
{ "symbol": "f", "x" : 3, "y" : 2, "width": 1 },
{ "symbol": "g", "x" : 4, "y" : 2, "width": 1 },
......@@ -24,16 +24,15 @@
{ "symbol": "k", "x" : 7, "y" : 2, "width": 1 },
{ "symbol": "l", "x" : 8, "y" : 2, "width": 1 },
{ "symbol": "m", "x" : 9, "y" : 2, "width": 1 },
{ "symbol": "q", "x" : 1, "y" : 2, "width": 1 },
{ "symbol": "⇧", "x" : 0, "y" : 3, "width": 2 },
{ "symbol": "⇧", "x" : 0, "y" : 3, "width": 2 },
{ "symbol": "w", "x" : 2, "y" : 3, "width": 1 },
{ "symbol": "x", "x" : 3, "y" : 3, "width": 1 },
{ "symbol": "c", "x" : 4, "y" : 3, "width": 1 },
{ "symbol": "v", "x" : 5, "y" : 3, "width": 1 },
{ "symbol": "b", "x" : 6, "y" : 3, "width": 1 },
{ "symbol": "n", "x" : 7, "y" : 3, "width": 1 },
{ "symbol": "⇦", "x" : 8, "y" : 3, "width": 2 },
{ "symbol": "", "x" : 8, "y" : 3, "width": 2 },
{ "symbol": " ", "x" : 0, "y" : 4, "width": 8 },
{ "symbol": "↵", "x" : 8, "y" : 4, "width": 2 }
{ "symbol": "", "x" : 8, "y" : 4, "width": 2 }
]
}
\ No newline at end of file
import math
def interpolate(firstPoint, secondPoint, distance):
ab = math.sqrt((secondPoint[0] - firstPoint[0]) ** 2 + (secondPoint[1] - firstPoint[1]) ** 2)
x = firstPoint[0] + (secondPoint[0] - firstPoint[0]) * distance / ab
y = firstPoint[1] + (secondPoint[1] - firstPoint[1]) * distance / ab
return int(x), int(y)
def resample(listOfPoints, d):
if len(listOfPoints) == 0:
return []
newListOfPoints = [listOfPoints[0]]
idx = 1
currentDistance = 0
while idx < len(listOfPoints):
firstPoint = listOfPoints[idx - 1]
secondPoint = listOfPoints[idx]
currentDistance += math.sqrt((secondPoint[0] - firstPoint[0]) ** 2 + (secondPoint[1] - firstPoint[1]) ** 2)
if currentDistance >= d:
newPoint = interpolate(firstPoint, secondPoint, d - currentDistance)
newListOfPoints.append(newPoint)
currentDistance = 0
idx += 1
return newListOfPoints
import json
from PyQt5.QtCore import Qt, QSize, QPoint, pyqtSignal
from PyQt5.QtGui import QPainter, QColor
from PyQt5.QtGui import QPainter, QColor, QPen
from PyQt5.QtWidgets import QWidget
from documents.FileDefinition import LAYOUT_FILE
from src.model.Key import Key
from PySide6.QtCore import Signal
from src.module.Reconnaisseur import resample
DEFAULT_COLOR = QColor("white")
OVER_COLOR = QColor("gray")
PEN_COLOR = QColor(0, 0, 255, 128)
PEN_COLOR2 = QColor(255, 0, 0, 255)
PEN_WIDTH = 5
class KeyboardWidget(QWidget):
newletter = pyqtSignal(str)
def __init__(self):
......@@ -21,6 +28,8 @@ class KeyboardWidget(QWidget):
self.keyboardWidth = 0
self.keyboardHeight = 0
self.trace = []
self.resampledTrace = []
self.mousePos = QPoint(0,0)
self.pressedKey = None
......@@ -59,12 +68,30 @@ class KeyboardWidget(QWidget):
return key
def paintEvent(self, event):
self.sizeHint()
painter = QPainter(self)
painter.setRenderHint(QPainter.Antialiasing)
# Dessiner les touches du clavier
for key in self.keys:
if key.isOver(self.mousePos):
self.paintKey(key, OVER_COLOR)
self.paintKey(painter, key, OVER_COLOR)
else:
self.paintKey(key, DEFAULT_COLOR)
self.paintKey(painter, key, DEFAULT_COLOR)
# Dessiner le tracé courant
pen = QPen(PEN_COLOR, PEN_WIDTH, Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin)
pen2 = QPen(PEN_COLOR2, PEN_WIDTH, Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin)
# Dessiner la ligne entre les points
for i in range(len(self.trace) - 1):
painter.setPen(pen)
painter.drawLine(self.trace[i][0], self.trace[i][1], self.trace[i + 1][0], self.trace[i + 1][1])
# Dessiner les points du tracé resample
for i in range(len(self.resampledTrace) - 1):
painter.setPen(pen2)
painter.drawPoint(self.resampledTrace[i][0], self.resampledTrace[i][1])
def sizeHint(self):
return QSize(self.keyboardWidth, self.keyboardHeight)
......@@ -84,12 +111,29 @@ class KeyboardWidget(QWidget):
if key == self.pressedKey:
self.pressedKey = None
self.newletter.emit(key.symbol)
return
break
else:
return
break
self.clearTrace()
def paintKey(self, key, color):
painter = QPainter(self)
def paintKey(self, painter, key, color):
painter.setBrush(color)
painter.drawRect(key.rect)
painter.drawText(key.rect, Qt.AlignCenter, key.symbol)
def clearTrace(self):
self.trace = []
self.resampledTrace = []
self.update()
self.pressedKey = None
def mouseMoveEvent(self, event):
self.mousePos = event.pos()
if self.pressedKey is not None:
point = (self.mousePos.x(), self.mousePos.y())
self.trace.append(point)
self.resampledTrace = resample(self.trace, 50)
self.update()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment