arbeit
Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | File Members

CanvasItems.h

Go to the documentation of this file.
00001 //------------------------------------------------------------------------
00002 //
00003 //   Joe Kniss
00004 //     6-20-03
00005 //                   ________    ____   ___ 
00006 //                  |        \  /    | /  /
00007 //                  +---+     \/     |/  /
00008 //                  +--+|  |\    /|     < 
00009 //                  |  ||  | \  / |  |\  \ 
00010 //                  |      |  \/  |  | \  \ 
00011 //                   \_____|      |__|  \__\
00012 //                       Copyright  2003 
00013 //                      Joe Michael Kniss
00014 //                   <<< jmk@cs.utah.edu >>>
00015 //               "All Your Base are Belong to Us"
00016 //-------------------------------------------------------------------------
00017 
00018 
00019 /// CanvasItems.h
00020 ///  mostly copied from QT examples
00021 
00022 #ifndef __CANVAS_ITEMS_DOT_H
00023 #define __CANVAS_ITEMS_DOT_H
00024 
00025 #include <smartptr.h>
00026 #include <qcanvas.h>
00027 #include <vector>
00028 #include <list>
00029 #include <mathGutz.h>
00030 
00031 typedef std::vector<QCanvasItem*> CanvasItemPVec;
00032 
00033 namespace CID { //< canvas item defines
00034    enum PEN_BRUSH_COLOR_IDS {
00035       PRIMARY,
00036       SECONDARY,
00037       TERTIERY,
00038       PICKED,
00039       PRE_PICKED,
00040       PBC_LAST
00041    };
00042    
00043    const QColor PRIMARY_COLOR    = Qt::white;
00044    const QColor SECONDARY_COLOR  = Qt::lightGray;
00045    const QColor TERTIERY_COLOR   = Qt::gray;
00046    const QColor PICKED_COLOR     = Qt::red;
00047    const QColor PRE_PICKED_COLOR = Qt::darkRed;
00048 
00049    /// argh!! global arrays, but... you want
00050    /// to customize these and have them global too
00051    /// just too lazy to make a singleton here
00052    /// all of these must have the same number of
00053    ///  elements and at least PBC_LAST elements!!!
00054 
00055    extern int    NUM_PCB;  //< number of pen/brush/colors
00056    extern QColor COLORS[]; 
00057    extern QPen   PENS[];
00058    extern QBrush BRUSHES[];
00059 
00060 } /// end canvas item defines
00061 
00062 
00063 /// forward decl
00064 class CanvasItemBase;
00065 
00066 ///////////////////////////////////////////////////////////////////////////
00067 /// a composite item, for hierarchical grouping
00068 ///  these don't actually draw
00069 ///////////////////////////////////////////////////////////////////////////
00070 class CompositeItem : public gutz::Counted {
00071 public:
00072    CompositeItem() {}
00073    virtual ~CompositeItem() {}
00074 
00075    /// something changed...
00076    virtual void childUpdate(CanvasItemBase *child) = 0;
00077    /// delta change
00078    virtual void childMovedBy(CanvasItemBase *child, gutz::vec3d dv) = 0;
00079    /// absolute change
00080    virtual void childMoved(CanvasItemBase *child, gutz::vec3d last, gutz::vec3d point) = 0;
00081 
00082 protected:
00083 };
00084 
00085 typedef gutz::SmartPtr<CompositeItem> CompositeItemSP;
00086 typedef std::list<CompositeItemSP> CompositeItemSPList;
00087 
00088 class NodeItem;
00089 
00090 ///////////////////////////////////////////////////////////////////////////
00091 ///////////////////////////////////////////////////////////////////////////
00092 /// a interface for smart canvas items
00093 ///////////////////////////////////////////////////////////////////////////
00094 ///////////////////////////////////////////////////////////////////////////
00095 
00096 class CanvasItemIF : 
00097    public QCanvasPolygonalItem
00098 {
00099 public:
00100    virtual ~CanvasItemIF(){}
00101 
00102    /// You have to override these!
00103    /// polygonalItem interface stuff
00104    virtual QPointArray areaPoints() const = 0;
00105    /// polygonalItem interface stuff
00106    virtual void drawShape(QPainter &p)    = 0;
00107 
00108    /// is it selected? (currently being modified)
00109    /// bool isSelected() declared in QCanvasItem
00110    virtual void setSelected(bool yes);
00111    /// has it been selected and modifiable as a group?
00112    /// bool isActive() declared in QCanvasItem
00113    virtual void setActive(bool yes);
00114 
00115    /// do you want this object to update other objects
00116    /// that are linked to it?
00117    bool  getUpdateLinks() const   { return _updateLinks; }
00118    void  setUpdateLinks(bool yes) {_updateLinks = yes; }
00119 
00120 protected:
00121    CanvasItemIF(); //< not used
00122    CanvasItemIF(QCanvas *canvas, CompositeItem *comp)
00123       : QCanvasPolygonalItem(canvas), _comp(comp),
00124         _updateLinks(true)
00125    {}
00126 
00127    void updatePen();
00128 
00129    CompositeItem *_comp;
00130 
00131    bool _updateLinks;
00132 };
00133 
00134 ///////////////////////////////////////////////////////////////////////////
00135 ///////////////////////////////////////////////////////////////////////////
00136 /// a highlevel counted canvas item
00137 ///////////////////////////////////////////////////////////////////////////
00138 ///////////////////////////////////////////////////////////////////////////
00139 class CanvasItemBase : public CanvasItemIF, public gutz::Counted
00140 {
00141 public:
00142 
00143 protected:
00144    CanvasItemBase(); //<not used
00145    CanvasItemBase(QCanvas *canvas, CompositeItem *comp)
00146       : CanvasItemIF(canvas, comp)
00147    {}
00148 };
00149 
00150 typedef gutz::SmartPtr<CanvasItemBase> CanvasItemBaseSP;
00151 
00152 ///////////////////////////////////////////////////////////////////////////
00153 ///////////////////////////////////////////////////////////////////////////
00154 /// an edge (line) composed from 2 nodes (points)
00155 ///////////////////////////////////////////////////////////////////////////
00156 ///////////////////////////////////////////////////////////////////////////
00157 class EdgeItem: public CanvasItemBase
00158 {
00159 public:
00160    EdgeItem( NodeItem*, NodeItem*, QCanvas *canvas, CompositeItem *comp = 0 );
00161    virtual ~EdgeItem() {}
00162 
00163    /// set start [sx,sy] and end [ex, ey] points
00164    void setPoints(double sx, double sy, double ex, double ey);
00165    void setPoints(const gutz::vec3d start, const gutz::vec3d end);
00166 
00167    gutz::vec3d startPoint() const {return _start;}
00168    gutz::vec3d endPoint()   const {return _end;}
00169    /// doesn't update nodes
00170    virtual void setStartPoint(gutz::vec3d start);
00171    virtual void setEndPoint(gutz::vec3d end);
00172 
00173    /// does update nodes
00174    virtual void moveBy(double dx, double dy);
00175 
00176    /// polygonalItem interface stuff
00177    virtual QPointArray areaPoints() const;
00178 
00179 protected:
00180    /// polygonalItem interface stuff
00181    virtual void drawShape(QPainter &p);
00182 
00183    gutz::vec3d _start;
00184    gutz::vec3d _end;
00185    NodeItem *_startNode;
00186    NodeItem *_endNode;
00187 };
00188 
00189 typedef gutz::SmartPtr<EdgeItem> EdgeItemSP;
00190 typedef std::vector<EdgeItemSP> EdgeItemVec;
00191 
00192 ///////////////////////////////////////////////////////////////////////////
00193 ///////////////////////////////////////////////////////////////////////////
00194 /// a Node (point) also can keep track of edges it is connected to
00195 ///////////////////////////////////////////////////////////////////////////
00196 ///////////////////////////////////////////////////////////////////////////
00197 class NodeItem: public CanvasItemBase
00198 {
00199 public:
00200    NodeItem( QCanvas *canvas, double x = 0, double y = 0, CompositeItem *comp = 0 );
00201    virtual ~NodeItem() {}
00202 
00203    /// set the position of this object, also updates any 
00204    /// linked edges
00205    virtual void        setPoint(double x, double y, EdgeItem *mover=0);
00206    virtual void        setPoint(gutz::vec3d v, EdgeItem *mover=0);
00207    gutz::vec3d getPoint() const { return gutz::vec3d(x(),y(),z()); }
00208 
00209    /// change the diameter of the node 
00210    void setSize(int sz) {_sz = sz;}
00211 
00212    QPoint center() { return boundingRect().center(); }
00213 
00214    /// part of QCanvasItem spec, not for general use
00215    /// updates edge items
00216    virtual void moveBy(double dx, double dy);
00217 
00218    /// polygonalItem interface stuff
00219    virtual QPointArray areaPoints() const;
00220 
00221    /// these are for registering edge links, called by edge 
00222    /// automatically, not for general use
00223    void addInEdge( EdgeItem *edge ) { inList.append( edge ); }
00224    void addOutEdge( EdgeItem *edge ) { outList.append( edge ); }
00225 
00226 protected:
00227    /// polygonalItem interface stuff
00228    virtual void drawShape(QPainter &p);
00229 
00230    QPtrList<EdgeItem> inList;
00231    QPtrList<EdgeItem> outList;
00232    
00233    int _sz;
00234 
00235 };
00236 
00237 typedef gutz::SmartPtr<NodeItem> NodeItemSP;
00238 typedef std::vector<NodeItemSP>  NodeItemVec;
00239 
00240 ///////////////////////////////////////////////////////////////////////////
00241 ///////////////////////////////////////////////////////////////////////////
00242 /// A Polygon that you can edit
00243 ///////////////////////////////////////////////////////////////////////////
00244 ///////////////////////////////////////////////////////////////////////////
00245 class PolygonEdit: public CanvasItemIF, public CompositeItem
00246 {
00247 public:
00248    PolygonEdit( QCanvas *canvas, CompositeItem *comp = 0 );
00249    virtual ~PolygonEdit();
00250 
00251    virtual void setPoints(QPointArray p);
00252 
00253    QPointArray areaPoints() const;
00254 
00255    /// TODO: as far as I can tell child update is unused!!
00256    virtual void childUpdate(CanvasItemBase *child){};
00257    /// child Moveby (relative motion)
00258    virtual void childMovedBy(CanvasItemBase *child, gutz::vec3d dv){};
00259    /// child moved (absolute modtion)
00260    virtual void childMoved(CanvasItemBase *child, gutz::vec3d last, gutz::vec3d point){};
00261 
00262    virtual void moveBy(double dx, double dy) {}
00263    /// this (incrmentaly) translates each node by differential vec "dv"
00264    virtual void moveBy(gutz::vec3d dv);
00265 
00266 protected:
00267    virtual void drawShape(QPainter &p);
00268    void nukePoints();
00269    void nukeEdges();
00270 
00271    /// returns -1 if not an edge/node respectively
00272    int getEdgeIndex(CanvasItemBase *ptr);
00273    int getNodeIndex(CanvasItemBase *ptr);
00274 
00275    NodeItemVec _points;
00276    EdgeItemVec _edges;
00277 };
00278 
00279 #endif
00280 

Send questions, comments, and bug reports to:
jmk