diff --git a/.DS_Store b/.DS_Store index 3f5991bb2ac324dd8e23b185d0fbc96299e0db44..5435a7ff2b2edef631fc07ce7147d9366b17f6e0 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/TP4/documents/layout.json b/TP4/documents/layout.json index 46a7693b44809694e8dc06a291e753ba120ce3b8..cb1cff23e352c48081cab036c160e8ab7da34836 100644 --- a/TP4/documents/layout.json +++ b/TP4/documents/layout.json @@ -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 diff --git a/TP4/src/module/Reconnaisseur.py b/TP4/src/module/Reconnaisseur.py new file mode 100644 index 0000000000000000000000000000000000000000..8a1f0b81887d48cbea55b23d3a2de956233e71fe --- /dev/null +++ b/TP4/src/module/Reconnaisseur.py @@ -0,0 +1,32 @@ +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 + + diff --git a/TP4/src/module/__init__.py b/TP4/src/module/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/TP4/src/widgets/KeyboardWidget.py b/TP4/src/widgets/KeyboardWidget.py index c89b24b23fa8c13f4b6bd48b984445830a197d69..c4ba45e6557a17286a076cf5b421398017ec125a 100644 --- a/TP4/src/widgets/KeyboardWidget.py +++ b/TP4/src/widgets/KeyboardWidget.py @@ -1,15 +1,22 @@ 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() + +