File indexing completed on 2023-10-03 03:08:11
0001 /* 0002 SPDX-FileCopyrightText: 2003-2006 Cies Breijs <cies AT kde DOT nl> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #ifndef _CANVAS_H_ 0008 #define _CANVAS_H_ 0009 0010 #include <QGraphicsView> 0011 0012 #include "sprite.h" 0013 0014 0015 class Canvas : public QGraphicsView 0016 { 0017 Q_OBJECT 0018 0019 public: 0020 explicit Canvas(QWidget *parent = nullptr); 0021 ~Canvas() override; 0022 0023 double turtleAngle() { return turtle->angle(); } 0024 QImage getPicture(); 0025 void saveAsSvg(const QString&, const QString&); 0026 // void scene() { return _scene; } 0027 0028 public Q_SLOTS: 0029 void slotClear(); 0030 void slotGo(double x, double y) { turtle->setPos(x, y); }; 0031 void slotGoX(double x) { turtle->setPos(x, turtle->pos().y()); } 0032 void slotGoY(double y) { turtle->setPos(turtle->pos().x(), y); } 0033 void slotForward(double x); 0034 void slotBackward(double x); 0035 void slotDirection(double deg) { turtle->setAngle(deg); } 0036 void slotTurnLeft(double deg) { turtle->setAngle(turtle->angle() - deg); } 0037 void slotTurnRight(double deg) { turtle->setAngle(turtle->angle() + deg); } 0038 void slotCenter(); 0039 void slotPenWidth(double width); 0040 void slotPenUp() { pen->setStyle(Qt::NoPen); } 0041 void slotPenDown() { pen->setStyle(Qt::SolidLine); } 0042 void slotPenColor(double r, double g, double b); 0043 void slotCanvasColor(double r, double g, double b); 0044 void slotCanvasSize(double r, double g); 0045 void slotSpriteShow() { turtle->show(); } 0046 void slotSpriteHide() { turtle->hide(); } 0047 void slotPrint(const QString& text); 0048 void slotFontType(const QString& family, const QString& extra); 0049 void slotFontSize(double px) { textFont->setPixelSize((int)px); } 0050 void slotReset(); 0051 void getDirection(double& value); 0052 void getX(double& value); 0053 void getY(double& value); 0054 0055 protected: 0056 void resizeEvent(QResizeEvent* event) override; 0057 0058 private: 0059 void initValues(); 0060 QColor rgbDoublesToColor(double r, double g, double b); 0061 void drawLine(double x1, double y1, double x2, double y2); 0062 void wheelEvent(QWheelEvent *event) override; 0063 void scaleView(double scaleFactor); 0064 0065 QGraphicsScene *_scene; 0066 QPen *pen; 0067 Sprite *turtle; 0068 QList<QGraphicsLineItem*> lines; 0069 QGraphicsLineItem *line; 0070 QGraphicsRectItem *canvasFrame; 0071 bool penWidthIsZero; 0072 QFont *textFont; 0073 QColor textColor; 0074 }; 0075 0076 0077 #endif // _CANVAS_H_