File indexing completed on 2024-05-19 13:32:55

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 QCANVAS_H
0011 #define QCANVAS_H
0012 
0013 #include <map>
0014 
0015 #include "ktlqt3support/ktlq3scrollview.h"
0016 #include <QPixmap>
0017 // #include "q3ptrlist.h"
0018 #include <QBrush>
0019 #include <QList>
0020 #include <QPen>
0021 // #include "q3pointarray.h" // 2018.08.14
0022 
0023 #include "canvasitemlist.h"
0024 
0025 class KtlQCanvasView;
0026 class KtlQCanvasChunk;
0027 
0028 class KtlQCanvas : public QObject
0029 {
0030     Q_OBJECT
0031 public:
0032     KtlQCanvas(QObject *parent = nullptr);
0033     KtlQCanvas(const int w, const int h);
0034     KtlQCanvas(QPixmap p, int h, int v, int tilewidth, int tileheight);
0035 
0036     ~KtlQCanvas() override;
0037 
0038     virtual void setTiles(QPixmap tiles, int h, int v, int tilewidth, int tileheight);
0039     virtual void setBackgroundPixmap(const QPixmap &p);
0040     QPixmap backgroundPixmap() const;
0041 
0042     virtual void setBackgroundColor(const QColor &c);
0043     QColor backgroundColor() const;
0044 
0045     virtual void setTile(int x, int y, int tilenum);
0046     int tile(int x, int y) const
0047     {
0048         return grid[x + y * htiles];
0049     }
0050 
0051     int tilesHorizontally() const
0052     {
0053         return htiles;
0054     }
0055     int tilesVertically() const
0056     {
0057         return vtiles;
0058     }
0059 
0060     int tileWidth() const
0061     {
0062         return tilew;
0063     }
0064     int tileHeight() const
0065     {
0066         return tileh;
0067     }
0068 
0069     virtual void resize(const QRect &newSize);
0070     int width() const
0071     {
0072         return size().width();
0073     }
0074     int height() const
0075     {
0076         return size().height();
0077     }
0078     QSize size() const
0079     {
0080         return m_size.size();
0081     }
0082     QRect rect() const
0083     {
0084         return m_size;
0085     }
0086     bool onCanvas(const int x, const int y) const
0087     {
0088         return onCanvas(QPoint(x, y));
0089     }
0090     bool onCanvas(const QPoint &p) const
0091     {
0092         return m_size.contains(p);
0093     }
0094     bool validChunk(const int x, const int y) const
0095     {
0096         return validChunk(QPoint(x, y));
0097     }
0098     bool validChunk(const QPoint &p) const
0099     {
0100         return m_chunkSize.contains(p);
0101     }
0102 
0103     int chunkSize() const
0104     {
0105         return chunksize;
0106     }
0107     virtual void retune(int chunksize, int maxclusters = 100);
0108     virtual void setChangedChunk(int i, int j);
0109     virtual void setChangedChunkContaining(int x, int y);
0110     virtual void setAllChanged();
0111     virtual void setChanged(const QRect &area);
0112     virtual void setUnchanged(const QRect &area);
0113 
0114     // These call setChangedChunk.
0115     void addItemToChunk(KtlQCanvasItem *, int i, int j);
0116     void removeItemFromChunk(KtlQCanvasItem *, int i, int j);
0117     void addItemToChunkContaining(KtlQCanvasItem *, int x, int y);
0118     void removeItemFromChunkContaining(KtlQCanvasItem *, int x, int y);
0119 
0120     KtlQCanvasItemList allItems();
0121     KtlQCanvasItemList collisions(const QPoint &) /* const */;
0122     KtlQCanvasItemList collisions(const QRect &) /* const */;
0123     KtlQCanvasItemList collisions(const QPolygon &pa, const KtlQCanvasItem *item, bool exact) const;
0124 
0125     void drawArea(const QRect &, QPainter *p);
0126 
0127     // These are for KtlQCanvasView to call
0128     virtual void addView(KtlQCanvasView *);
0129     virtual void removeView(KtlQCanvasView *);
0130     void drawCanvasArea(const QRect &, QPainter *p, bool double_buffer);
0131     void drawViewArea(KtlQCanvasView *view, QPainter *p, const QRect &r, bool dbuf);
0132 
0133     // These are for KtlQCanvasItem to call
0134     virtual void addItem(KtlQCanvasItem *);
0135     virtual void removeItem(const KtlQCanvasItem *);
0136 
0137     virtual void setUpdatePeriod(int ms);
0138     int toChunkScaling(int x) const;
0139 
0140 signals:
0141     void resized();
0142 
0143 public slots:
0144     virtual void advance();
0145     virtual void update();
0146 
0147 protected:
0148     virtual void drawBackground(QPainter &, const QRect &area);
0149     virtual void drawForeground(QPainter &, const QRect &area);
0150 
0151 private:
0152     void init(int w, int h, int chunksze = 16, int maxclust = 100);
0153     void init(const QRect &r, int chunksze = 16, int maxclust = 100);
0154     void initChunkSize(const QRect &s);
0155 
0156     KtlQCanvasChunk &chunk(int i, int j) const;
0157     KtlQCanvasChunk &chunkContaining(int x, int y) const;
0158 
0159     QRect changeBounds(const QRect &inarea);
0160     void drawChanges(const QRect &inarea);
0161     void drawChangedItems(QPainter &painter);
0162     void setNeedRedraw(const KtlQCanvasItemList *list);
0163 
0164     QPixmap offscr;
0165     int chunksize;
0166     int maxclusters;
0167     QRect m_size;
0168     QRect m_chunkSize;
0169     KtlQCanvasChunk *chunks;
0170 
0171     SortedCanvasItems m_canvasItems;
0172     QList<KtlQCanvasView *> m_viewList;
0173 
0174     void initTiles(QPixmap p, int h, int v, int tilewidth, int tileheight);
0175     ushort *grid;
0176     ushort htiles;
0177     ushort vtiles;
0178     ushort tilew;
0179     ushort tileh;
0180     bool oneone;
0181     QPixmap pm;
0182     QTimer *update_timer;
0183     QColor bgcolor;
0184     bool debug_redraw_areas;
0185 
0186     friend void qt_unview(KtlQCanvas *c);
0187 
0188     KtlQCanvas(const KtlQCanvas &);
0189     KtlQCanvas &operator=(const KtlQCanvas &);
0190 };
0191 
0192 class KtlQCanvasViewData;
0193 
0194 class KtlQCanvasView : public KtlQ3ScrollView
0195 {
0196     Q_OBJECT
0197 public:
0198     KtlQCanvasView(QWidget *parent = nullptr, Qt::WindowFlags f = {}); // 2018.08.15 - unused?
0199     KtlQCanvasView(KtlQCanvas *viewing, QWidget *parent = nullptr, Qt::WindowFlags f = {});
0200     ~KtlQCanvasView() override;
0201 
0202     KtlQCanvas *canvas() const
0203     {
0204         return viewing;
0205     }
0206     void setCanvas(KtlQCanvas *v);
0207 
0208     const QTransform &worldMatrix() const;
0209     const QTransform &inverseWorldMatrix() const;
0210     bool setWorldTransform(const QTransform &);
0211 
0212 protected:
0213     /** overrides KtlQ3ScrollView::drawContents() */ // override paintEvent?
0214     void drawContents(QPainter *, int cx, int cy, int cw, int ch) override;
0215     QSize sizeHint() const override;
0216 
0217 private:
0218     void drawContents(QPainter *) override;
0219     KtlQCanvas *viewing;
0220     KtlQCanvasViewData *d;
0221     friend void qt_unview(KtlQCanvas *c);
0222     KtlQCanvasView(const KtlQCanvasView &);
0223     KtlQCanvasView &operator=(const KtlQCanvasView &);
0224 
0225 private slots:
0226     void cMoving(int, int);
0227     void updateContentsSize();
0228 };
0229 
0230 #endif // QCANVAS_H