File indexing completed on 2024-05-19 09:39:34

0001 /***************************************************************************
0002  *   Copyright (C) 1999-2005 Trolltech AS                                  *
0003  *   Copyright (C) 2006 David Saxton <david@bluehaze.org>                  *
0004  *                                                                         *
0005  *   This file may be distributed and/or modified under the terms of the   *
0006  *   GNU General Public License version 2 as published by the Free         *
0007  *   Software Foundation                                                   *
0008  ***************************************************************************/
0009 
0010 #ifndef KTL_CANVASITEMS_H_
0011 #define KTL_CANVASITEMS_H_
0012 
0013 #include "canvasitemlist.h"
0014 
0015 #include <QBrush>
0016 #include <QObject>
0017 #include <QPen>
0018 #include <QPolygon>
0019 #include <QRect>
0020 
0021 class QPainter;
0022 
0023 class KtlQCanvasPolygonalItem;
0024 class KtlQCanvasRectangle;
0025 class KtlQCanvasPolygon;
0026 class KtlQCanvasEllipse;
0027 class KtlQCanvasLine;
0028 class KtlQCanvasChunk;
0029 class KtlQCanvas;
0030 class KtlQCanvasItem;
0031 class KtlQCanvasView;
0032 
0033 class KtlQCanvasItemExtra;
0034 
0035 class KtlQCanvasItem : public QObject
0036 {
0037     Q_OBJECT
0038 public:
0039     KtlQCanvasItem(KtlQCanvas *canvas);
0040     ~KtlQCanvasItem() override;
0041 
0042     double x() const
0043     {
0044         return myx;
0045     }
0046     double y() const
0047     {
0048         return myy;
0049     }
0050     double z() const
0051     {
0052         return myz;
0053     } // (depth)
0054 
0055     virtual void moveBy(double const dx, double const dy);
0056     void move(double const x, double const y);
0057     void setX(double a)
0058     {
0059         move(a, y());
0060     }
0061     void setY(double a)
0062     {
0063         move(x(), a);
0064     }
0065     void setZ(double a);
0066 
0067     virtual bool collidesWith(const KtlQCanvasItem *) const = 0;
0068 
0069     KtlQCanvasItemList collisions(const bool exact /* NO DEFAULT */) const;
0070 
0071     virtual void setCanvas(KtlQCanvas *);
0072 
0073     virtual void draw(QPainter &) = 0;
0074 
0075     void show();
0076     void hide();
0077 
0078     virtual void setVisible(bool yes);
0079     bool isVisible() const
0080     {
0081         return vis;
0082     }
0083     virtual void setSelected(const bool yes);
0084     bool isSelected() const
0085     {
0086         return sel;
0087     }
0088 
0089     virtual QRect boundingRect() const = 0;
0090 
0091     KtlQCanvas *canvas() const
0092     {
0093         return cnv;
0094     }
0095 
0096     virtual bool collidesWith(const KtlQCanvasPolygonalItem *, const KtlQCanvasRectangle *, const KtlQCanvasEllipse *) const = 0;
0097 
0098     bool needRedraw() const
0099     {
0100         return m_bNeedRedraw;
0101     }
0102     void setNeedRedraw(const bool needRedraw)
0103     {
0104         m_bNeedRedraw = needRedraw;
0105     }
0106 
0107 protected:
0108     void update()
0109     {
0110         changeChunks();
0111     }
0112 
0113     virtual QPolygon chunks() const;
0114     virtual void addToChunks();
0115     virtual void removeFromChunks();
0116     virtual void changeChunks();
0117 
0118     bool val;
0119     double myx, myy, myz;
0120 
0121 private:
0122     KtlQCanvas *cnv;
0123     static KtlQCanvas *current_canvas;
0124     KtlQCanvasItemExtra *ext;
0125     KtlQCanvasItemExtra &extra();
0126     bool m_bNeedRedraw;
0127     bool vis;
0128     bool sel;
0129 };
0130 
0131 class KtlQPolygonalProcessor;
0132 
0133 class KtlQCanvasPolygonalItem : public KtlQCanvasItem
0134 {
0135 public:
0136     KtlQCanvasPolygonalItem(KtlQCanvas *canvas);
0137     ~KtlQCanvasPolygonalItem() override;
0138 
0139     bool collidesWith(const KtlQCanvasItem *) const override;
0140 
0141     virtual void setPen(const QPen &p);
0142     virtual void setBrush(const QBrush &b);
0143 
0144     QPen pen() const
0145     {
0146         return pn;
0147     }
0148     QBrush brush() const
0149     {
0150         return br;
0151     }
0152 
0153     virtual QPolygon areaPoints() const = 0;
0154     QRect boundingRect() const override;
0155 
0156 protected:
0157     void draw(QPainter &) override;
0158     virtual void drawShape(QPainter &) = 0;
0159 
0160     bool winding() const;
0161     void setWinding(bool);
0162 
0163     void invalidate();
0164     bool isValid() const
0165     {
0166         return val;
0167     }
0168 
0169 private:
0170     void scanPolygon(const QPolygon &pa, int winding, KtlQPolygonalProcessor &process) const;
0171 
0172     QPolygon chunks() const override;
0173 
0174     bool collidesWith(const KtlQCanvasPolygonalItem *, const KtlQCanvasRectangle *, const KtlQCanvasEllipse *) const override;
0175 
0176     QBrush br;
0177     QPen pn;
0178     bool wind;
0179 };
0180 
0181 class KtlQCanvasRectangle : public KtlQCanvasPolygonalItem
0182 {
0183 public:
0184     KtlQCanvasRectangle(KtlQCanvas *canvas);
0185     KtlQCanvasRectangle(const QRect &, KtlQCanvas *canvas);
0186     KtlQCanvasRectangle(int x, int y, int width, int height, KtlQCanvas *canvas);
0187 
0188     ~KtlQCanvasRectangle() override;
0189 
0190     int width() const;
0191     int height() const;
0192     void setSize(const int w, const int h);
0193     QSize size() const
0194     {
0195         return QSize(w, h);
0196     }
0197     QPolygon areaPoints() const override;
0198     QRect rect() const
0199     {
0200         return QRect(int(x()), int(y()), w, h);
0201     }
0202 
0203     bool collidesWith(const KtlQCanvasItem *) const override;
0204 
0205 protected:
0206     void drawShape(QPainter &) override;
0207     QPolygon chunks() const override;
0208 
0209 private:
0210     bool collidesWith(const KtlQCanvasPolygonalItem *, const KtlQCanvasRectangle *, const KtlQCanvasEllipse *) const override;
0211 
0212     int w, h;
0213 };
0214 
0215 class KtlQCanvasPolygon : public KtlQCanvasPolygonalItem
0216 {
0217 public:
0218     KtlQCanvasPolygon(KtlQCanvas *canvas);
0219     ~KtlQCanvasPolygon() override;
0220     void setPoints(QPolygon);
0221     QPolygon points() const;
0222     void moveBy(double dx, double dy) override;
0223 
0224     QPolygon areaPoints() const override;
0225 
0226 protected:
0227     void drawShape(QPainter &) override;
0228     // TODO FIXME guarts are added for debugging memory corruption (poly takes non-pointer values)
0229     int guardBef[10];
0230     QPolygon *poly;
0231     int guardAft[10];
0232 };
0233 
0234 class KtlQCanvasLine : public KtlQCanvasPolygonalItem
0235 {
0236 public:
0237     KtlQCanvasLine(KtlQCanvas *canvas);
0238     ~KtlQCanvasLine() override;
0239     void setPoints(int x1, int y1, int x2, int y2);
0240 
0241     QPoint startPoint() const
0242     {
0243         return QPoint(x1, y1);
0244     }
0245     QPoint endPoint() const
0246     {
0247         return QPoint(x2, y2);
0248     }
0249 
0250     void setPen(const QPen &p) override;
0251     void moveBy(double dx, double dy) override;
0252 
0253 protected:
0254     void drawShape(QPainter &) override;
0255     QPolygon areaPoints() const override;
0256 
0257 private:
0258     int x1, y1, x2, y2;
0259 };
0260 
0261 class KtlQCanvasEllipse : public KtlQCanvasPolygonalItem
0262 {
0263 public:
0264     KtlQCanvasEllipse(KtlQCanvas *canvas);
0265     KtlQCanvasEllipse(int width, int height, KtlQCanvas *canvas);
0266     KtlQCanvasEllipse(int width, int height, int startangle, int angle, KtlQCanvas *canvas);
0267 
0268     ~KtlQCanvasEllipse() override;
0269 
0270     int width() const;
0271     int height() const;
0272     void setSize(int w, int h);
0273     void setAngles(int start, int length);
0274     int angleStart() const
0275     {
0276         return a1;
0277     }
0278     int angleLength() const
0279     {
0280         return a2;
0281     }
0282     QPolygon areaPoints() const override;
0283 
0284     bool collidesWith(const KtlQCanvasItem *) const override;
0285 
0286 protected:
0287     void drawShape(QPainter &) override;
0288 
0289 private:
0290     bool collidesWith(const KtlQCanvasPolygonalItem *, const KtlQCanvasRectangle *, const KtlQCanvasEllipse *) const override;
0291     int w, h;
0292     int a1, a2;
0293 };
0294 
0295 #endif // KTL_CANVASITEMS_H_