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

CanvasItems.cpp

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.cpp
00020 
00021 #include "CanvasItems.h"
00022 #include <qpainter.h>
00023 #include <iostream>
00024 
00025 using namespace std;
00026 using namespace gutz;
00027 
00028 namespace CID {
00029    int     NUM_COLORS = PBC_LAST;
00030    QColor COLORS[] = {
00031       PRIMARY_COLOR,
00032       SECONDARY_COLOR,
00033       TERTIERY_COLOR,
00034       PICKED_COLOR,
00035       PRE_PICKED_COLOR
00036    };
00037    QPen   PENS[] = {
00038       QPen(COLORS[0]),
00039       QPen(COLORS[1]),
00040       QPen(COLORS[2]),
00041       QPen(COLORS[3]),
00042       QPen(COLORS[4])
00043    };
00044    QBrush BRUSHES[] = {
00045       QBrush(COLORS[0]),
00046       QBrush(COLORS[1]),
00047       QBrush(COLORS[2]),
00048       QBrush(COLORS[3]),
00049       QBrush(COLORS[4])
00050    };
00051 }// end namespace CID
00052 
00053 ///////////////////////////////////////////////////////////////////////////
00054 ///////////////////////////////////////////////////////////////////////////
00055 /// CanvasItem interface
00056 ///////////////////////////////////////////////////////////////////////////
00057 ///////////////////////////////////////////////////////////////////////////
00058 
00059 ///////////////////////////////////////////////////////////////////////////
00060 /// setSelected
00061 void CanvasItemIF::setSelected(bool yes)
00062 {
00063    QCanvasItem::setSelected(yes);
00064 }
00065 
00066 ///////////////////////////////////////////////////////////////////////////
00067 /// setActive
00068 void CanvasItemIF::setActive(bool yes)
00069 {
00070    QCanvasItem::setActive(yes);
00071 }
00072 
00073 ///////////////////////////////////////////////////////////////////////////
00074 ///////////////////////////////////////////////////////////////////////////
00075 /// EdgeItem
00076 ///////////////////////////////////////////////////////////////////////////
00077 ///////////////////////////////////////////////////////////////////////////
00078 
00079 ///////////////////////////////////////////////////////////////////////////
00080 /// Construct
00081 EdgeItem::EdgeItem( NodeItem *from, NodeItem *to, QCanvas *canvas, CompositeItem *comp )
00082 : CanvasItemBase( canvas, comp ), _startNode(from), _endNode(to)
00083 {
00084    setPen( CID::PENS[0] );
00085    setBrush( CID::BRUSHES[0] );
00086    from->addOutEdge( this );
00087    to->addInEdge( this );
00088    if(from && to)
00089       setPoints( int(from->x()), int(from->y()), int(to->x()), int(to->y()) );
00090    setZ( 127 );
00091 }
00092 
00093 ///////////////////////////////////////////////////////////////////////////
00094 /// set Points
00095 void EdgeItem::setPoints(double sx, double sy, double ex, double ey)
00096 {
00097    CanvasItemIF::invalidate();
00098    {
00099       _start.x = sx;
00100       _start.y = sy;
00101       _end.x   = ex;
00102       _end.y   = ey;
00103    }
00104    update();
00105 }
00106 
00107 void EdgeItem::setPoints(const gutz::vec3d start, const gutz::vec3d end)
00108 {
00109    invalidate();
00110    {
00111       _start = start;
00112       _end = end;
00113    }
00114    update();
00115 }
00116 
00117 
00118 ///////////////////////////////////////////////////////////////////////////
00119 /// area Points
00120 QPointArray EdgeItem::areaPoints() const
00121 {
00122    QPointArray p(4);
00123    /// objects point
00124    int xi = int(x());
00125    int yi = int(y());
00126    /// line end points
00127    int x1 = int(_start.x);
00128    int x2 = int(_end.x);
00129    int y1 = int(_start.y);
00130    int y2 = int(_end.y);
00131    /// pen width
00132    int pw = pen().width();
00133    int dx = QABS(x1-x2);
00134    int dy = QABS(y1-y2);
00135    pw = pw*4/3+2; // approx pw*sqrt(2)
00136    int px = x1<x2 ? -pw : pw ;
00137    int py = y1<y2 ? -pw : pw ;
00138    if ( dx && dy && (dx > dy ? (dx*2/dy <= 2) : (dy*2/dx <= 2)) ) {
00139       // steep
00140       if ( px == py ) {
00141          p[0] = QPoint(x1+xi   ,y1+yi+py);
00142          p[1] = QPoint(x2+xi-px,y2+yi   );
00143          p[2] = QPoint(x2+xi   ,y2+yi-py);
00144          p[3] = QPoint(x1+xi+px,y1+yi   );
00145       } else {
00146          p[0] = QPoint(x1+xi+px,y1+yi   );
00147          p[1] = QPoint(x2+xi   ,y2+yi-py);
00148          p[2] = QPoint(x2+xi-px,y2+yi   );
00149          p[3] = QPoint(x1+xi   ,y1+yi+py);
00150       }
00151    } else if ( dx > dy ) {
00152       // horizontal
00153       p[0] = QPoint(x1+xi+px,y1+yi+py);
00154       p[1] = QPoint(x2+xi-px,y2+yi+py);
00155       p[2] = QPoint(x2+xi-px,y2+yi-py);
00156       p[3] = QPoint(x1+xi+px,y1+yi-py);
00157    } else {
00158       // vertical
00159       p[0] = QPoint(x1+xi+px,y1+yi+py);
00160       p[1] = QPoint(x2+xi+px,y2+yi-py);
00161       p[2] = QPoint(x2+xi-px,y2+yi-py);
00162       p[3] = QPoint(x1+xi-px,y1+yi+py);
00163    }
00164 
00165    return p;
00166 }
00167 
00168 ///////////////////////////////////////////////////////////////////////////
00169 /// draw Shape
00170 void EdgeItem::drawShape(QPainter &p)
00171 {
00172     p.drawLine((int)(x()+_start.x), (int)(y()+_start.y), 
00173                (int)(x()+_end.x), (int)(y()+_end.y));
00174 }
00175 
00176 void EdgeItem::setStartPoint(gutz::vec3d start)
00177 {
00178    setPoints(start, _end);
00179 }
00180 
00181 void EdgeItem::setEndPoint(gutz::vec3d end)
00182 {
00183    setPoints(_start, end);
00184 }
00185 
00186 ///////////////////////////////////////////////////////////////////////////
00187 /// Move by
00188 void EdgeItem::moveBy(double dx, double dy)
00189 {
00190    vec3d last = startPoint();
00191       
00192    invalidate();
00193    {
00194       setStartPoint(startPoint() + vec3d(dx,dy,0.0));
00195       setEndPoint(  endPoint() + vec3d(dx,dy,0.0));
00196 
00197       if(_updateLinks)
00198       {
00199          if(_startNode)
00200             _startNode->setPoint(startPoint(),this);
00201          if(_endNode)
00202             _endNode->setPoint(endPoint(),this);
00203       }
00204 
00205       if(_comp)
00206       {
00207          _comp->childMoved(this,last,startPoint());
00208       }
00209    }
00210    update();
00211 }
00212 
00213 
00214 ///////////////////////////////////////////////////////////////////////////
00215 ///////////////////////////////////////////////////////////////////////////
00216 /// NodeItem
00217 ///////////////////////////////////////////////////////////////////////////
00218 ///////////////////////////////////////////////////////////////////////////
00219 
00220 ///////////////////////////////////////////////////////////////////////////
00221 /// Construct
00222 NodeItem::NodeItem( QCanvas *canvas, double x, double y, CompositeItem *comp )
00223 : CanvasItemBase( canvas, comp ), _sz(6)
00224 {
00225    setPen( CID::PENS[0] );
00226    setBrush( CID::BRUSHES[0] );
00227    setZ( 128 );
00228    move(x,y);
00229 }
00230 
00231 ///////////////////////////////////////////////////////////////////////////
00232 /// set point
00233 void NodeItem::setPoint(double x, double y, EdgeItem *mover)
00234 {
00235    if((x == this->x()) && (y == this->y())) return;
00236 
00237    setPoint(vec3d(x,y,z()),mover);
00238 }
00239 
00240 void NodeItem::setPoint(gutz::vec3d v, EdgeItem *mover)
00241 {
00242    if((v.x == this->x()) && (v.y == this->y()) && (v.z == this->z())) return;
00243    
00244    invalidate();
00245    {
00246       vec3d last(getPoint());
00247       ///QCanvasItem::move(v.x,v.y);
00248       setX(v.x); setY(v.y); setZ(v.z);
00249 
00250       if(_updateLinks)
00251       {
00252          QPtrListIterator<EdgeItem> it1( inList );
00253          EdgeItem *edge;
00254          while (( edge = it1.current() )) 
00255          {
00256             ++it1;
00257             if(edge != mover)
00258             {
00259                edge->setEndPoint( v );
00260             }
00261          }
00262          QPtrListIterator<EdgeItem> it2( outList );
00263          while (( edge = it2.current() )) 
00264          {
00265             ++it2;
00266             if(edge != mover)
00267             {
00268                edge->setStartPoint( v );
00269             }
00270          }
00271       }
00272       //if(_comp)
00273       //{
00274       //   _comp->childMoved(this, last, getPoint());
00275       //}
00276    }
00277    update();
00278 }
00279 
00280 
00281 ///////////////////////////////////////////////////////////////////////////
00282 /// Move by
00283 void NodeItem::moveBy(double dx, double dy)
00284 {
00285    vec3d last = getPoint();
00286 
00287    invalidate();
00288    {
00289       QCanvasItem::moveBy(dx,dy);
00290 
00291       if(_updateLinks)
00292       {
00293          QPtrListIterator<EdgeItem> it1( inList );
00294          EdgeItem *edge;
00295          while (( edge = it1.current() )) 
00296          {
00297             ++it1;
00298             edge->setEndPoint( getPoint() );
00299          }
00300          QPtrListIterator<EdgeItem> it2( outList );
00301          while (( edge = it2.current() )) 
00302          {
00303             ++it2;
00304             edge->setStartPoint( getPoint() );
00305          }
00306       }
00307       if(_comp)
00308          _comp->childMoved(this,last,getPoint());
00309    }
00310    update();
00311 }
00312 
00313 ///////////////////////////////////////////////////////////////////////////
00314 /// areaPoints
00315 QPointArray NodeItem::areaPoints() const
00316 {
00317    int pw = pen().width();
00318    int sz = _sz/2 + pen().width()/2 + 1;
00319    QPointArray b(4);
00320    b[0] = QPoint(int(x() - sz), int(y() - sz));
00321    b[1] = QPoint(int(x() + sz), int(y() - sz));
00322    b[2] = QPoint(int(x() + sz), int(y() + sz));
00323    b[3] = QPoint(int(x() - sz), int(y() + sz));
00324 
00325    return b;
00326 }
00327 
00328 ///////////////////////////////////////////////////////////////////////////
00329 /// drawShape
00330 void NodeItem::drawShape(QPainter &p)
00331 {
00332         p.drawEllipse(int(x()-_sz/2.0+0.5), int(y()-_sz/2.0+0.5), _sz, _sz);
00333 }
00334 
00335 ///////////////////////////////////////////////////////////////////////////
00336 ///////////////////////////////////////////////////////////////////////////
00337 /// PolygonEdit
00338 ///////////////////////////////////////////////////////////////////////////
00339 ///////////////////////////////////////////////////////////////////////////
00340 
00341 ///////////////////////////////////////////////////////////////////////////
00342 /// Construct
00343 PolygonEdit::PolygonEdit( QCanvas *canvas, CompositeItem *comp )
00344 : CanvasItemIF(canvas, comp)
00345 {
00346 
00347 }
00348 
00349 ///////////////////////////////////////////////////////////////////////////
00350 /// Destruct
00351 PolygonEdit::~PolygonEdit()
00352 {
00353    nukePoints();
00354    nukeEdges();
00355    hide();
00356 }
00357 
00358 ///////////////////////////////////////////////////////////////////////////
00359 /// Area points
00360 QPointArray PolygonEdit::areaPoints() const
00361 {
00362    QPointArray pa(_points.size());
00363    for(int i=0; i<int(_points.size()); ++i)
00364    {
00365       pa[i] = QPoint(_points[i]->x(), _points[i]->y());
00366       cerr << "point - area - " 
00367          << _points[i]->x() << " " << _points[i]->y() << endl;
00368    }
00369    return pa;
00370 }
00371 
00372 ///////////////////////////////////////////////////////////////////////////
00373 /// Set Points
00374 void PolygonEdit::setPoints(QPointArray p)
00375 {
00376    nukePoints();
00377    nukeEdges();
00378 
00379    /// create nodes
00380    for(int i=0; i<int(p.size()); ++i)
00381    {
00382       _points.push_back(NodeItemSP(new NodeItem(canvas(), p.point(i).x(), p.point(i).y(), this)));
00383       _points[i]->show();
00384    }
00385    /// create points
00386    for(int i=0; i<int(_points.size()); ++i)
00387    {
00388       _edges.push_back(EdgeItemSP(new EdgeItem(_points[i], _points[(i+1)%_points.size()], canvas(), this)));
00389       _edges[i]->show();
00390    }
00391 }
00392 
00393 ///////////////////////////////////////////////////////////////////////////
00394 /// Nuke Points
00395 void PolygonEdit::nukePoints()
00396 {
00397    for(int i=0; i< int(_points.size()); ++i)
00398    {
00399       _points[i] = 0;
00400    }
00401    _points = NodeItemVec();
00402 }
00403 
00404 ///////////////////////////////////////////////////////////////////////////
00405 /// Nuke Edges
00406 void PolygonEdit::nukeEdges()
00407 {
00408    for(int i=0; i< int(_edges.size()); ++i)
00409    {
00410       _edges[i] = 0;
00411    }
00412    _edges = EdgeItemVec(); 
00413 }
00414 
00415 ///////////////////////////////////////////////////////////////////////////
00416 /// Draw Shape
00417 void PolygonEdit::drawShape(QPainter &p)
00418 {
00419    
00420 }
00421 
00422 ///////////////////////////////////////////////////////////////////////////
00423 /// Edge index
00424 int PolygonEdit::getEdgeIndex(CanvasItemBase *ptr)
00425 {
00426    for(int i=0; i<int(_edges.size()); ++i)
00427    {
00428       if(_edges[i] == ptr) return i;
00429    }
00430    return -1;
00431 }
00432 
00433 ///////////////////////////////////////////////////////////////////////////
00434 /// node index
00435 int PolygonEdit::getNodeIndex(CanvasItemBase *ptr)
00436 {
00437    for(int i=0; i<int(_points.size()); ++i)
00438    {
00439       if(_points[i] == ptr) return i;
00440    }
00441    return -1;
00442 }
00443 
00444 ///////////////////////////////////////////////////////////////////////////
00445 /// move by
00446 void PolygonEdit::moveBy(gutz::vec3d dv)
00447 {
00448    for(int i=0; i<int(_points.size()); ++i)
00449    {
00450       _points[i]->setPoint(_points[i]->getPoint() + dv);
00451    }
00452 }

Send questions, comments, and bug reports to:
jmk