Buckets:
| import json | |
| from PyQt6.QtWidgets import QWidget, QVBoxLayout, QHBoxLayout, QPushButton, QColorDialog, QSpinBox, QLabel | |
| from PyQt6.QtCore import Qt, QPoint, pyqtSignal | |
| from PyQt6.QtGui import QPainter, QPen, QColor | |
| class DrawingCanvas(QWidget): | |
| changed = pyqtSignal() | |
| def __init__(self): | |
| super().__init__() | |
| self.setMinimumHeight(400) | |
| self.setAttribute(Qt.WidgetAttribute.WA_StaticContents) | |
| self.paths = [] # Liste de dictionnaires: {"points": [], "color": str, "width": int} | |
| self.current_path = None | |
| self.pen_color = QColor("#000000") | |
| self.pen_width = 3 | |
| self.setStyleSheet("background-color: white; border: 1px solid #3c3c3c;") | |
| def mousePressEvent(self, event): | |
| if event.button() == Qt.MouseButton.LeftButton: | |
| self.current_path = { | |
| "points": [(event.position().x(), event.position().y())], | |
| "color": self.pen_color.name(), | |
| "width": self.pen_width | |
| } | |
| self.paths.append(self.current_path) | |
| self.update() | |
| def mouseMoveEvent(self, event): | |
| if (event.buttons() & Qt.MouseButton.LeftButton) and self.current_path: | |
| self.current_path["points"].append((event.position().x(), event.position().y())) | |
| self.update() | |
| def mouseReleaseEvent(self, event): | |
| if event.button() == Qt.MouseButton.LeftButton: | |
| self.current_path = None | |
| self.changed.emit() | |
| def paintEvent(self, event): | |
| painter = QPainter(self) | |
| painter.setRenderHint(QPainter.RenderHint.Antialiasing) | |
| for path in self.paths: | |
| pen = QPen(QColor(path["color"]), path["width"], Qt.PenStyle.SolidLine, Qt.PenCapStyle.RoundCap, Qt.PenJoinStyle.RoundJoin) | |
| painter.setPen(pen) | |
| points = path["points"] | |
| if len(points) > 1: | |
| for i in range(len(points) - 1): | |
| p1 = QPoint(int(points[i][0]), int(points[i][1])) | |
| p2 = QPoint(int(points[i+1][0]), int(points[i+1][1])) | |
| painter.drawLine(p1, p2) | |
| def clear(self): | |
| self.paths = [] | |
| self.update() | |
| self.changed.emit() | |
| def get_data(self): | |
| return json.dumps(self.paths) | |
| def set_data(self, json_data): | |
| try: | |
| self.paths = json.loads(json_data) if json_data and json_data != 'null' else [] | |
| self.update() | |
| except Exception as e: | |
| print(f"Error loading canvas data: {e}") | |
| self.paths = [] | |
| self.update() | |
Xet Storage Details
- Size:
- 2.63 kB
- Xet hash:
- d1cd46b9f83d074b8572c9c5c7362a94727bc31ab9655d819265991f3b277e12
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.