File indexing completed on 2022-09-27 12:31:08

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