Public Types | Signals | Public Member Functions | Protected Member Functions
DrawView Class Reference

Brief description of class still missing. More...

#include <DrawView.h>

List of all members.

Public Types

enum  SnapOption { NoSnap = 0, SnapX = 1, SnapY = 2 }
enum  SnapType { SnapNodes, SnapGrid, SnapContours }

Signals

void mouseMoved (const QPointF &p)
void mouseReleased (const QPointF &p)

Public Member Functions

 DrawView (DrawScene *scene)
DrawScenescene () const
void setMousePoint (const QPointF &p)
void setSnapOption (SnapOptions ms)
void setSnapType (SnapType ms)
SnapOptions snapOption () const
SnapType snapType () const
 ~DrawView ()

Protected Member Functions

virtual void drawBackground (QPainter *painter, const QRectF &rect)
virtual void drawForeground (QPainter *painter, const QRectF &rect)
virtual void mouseMoveEvent (QMouseEvent *e)
virtual void mouseReleaseEvent (QMouseEvent *e)
virtual void wheelEvent (QWheelEvent *e)

Detailed Description

Brief description of class still missing.

Full description of class still missing


Member Enumeration Documentation

Enumerator:
NoSnap 
SnapX 
SnapY 
{NoSnap=0, SnapX=1, SnapY=2};
Enumerator:
SnapNodes 
SnapGrid 
SnapContours 

Constructor & Destructor Documentation

Description of constructor still missing

References NoSnap, SnapNodes, and TRACE.

    : QGraphicsView(scene)
{
  TRACE;
  _snapType=SnapNodes;
  _snapOption=NoSnap;
  setDragMode(QGraphicsView::NoDrag);
  setMouseTracking(true);
}

Description of destructor still missing

References TRACE.

{
  TRACE;
}

Member Function Documentation

void DrawView::drawBackground ( QPainter *  painter,
const QRectF &  rect 
) [protected, virtual]
{
  painter->save();
  QRectF r=mapToScene(viewport()->rect()).boundingRect();
  qreal xMajor, xMinor, yMajor, yMinor;
  autoTicks(r.width(), xMajor, xMinor);
  autoTicks(r.height(), yMajor, yMinor);
  qreal val, start;
  // Minor grid lines
  painter->setPen(QPen(QColor(240,240,240)));
  start=ceil(rect.left()/xMinor)*xMinor;
  for(val=start; val<=rect.right(); val+=xMinor) {
    painter->drawLine(QLineF( val, r.top(), val, r.bottom()) );
  }
  start=ceil(rect.top()/yMinor)*yMinor;
  for(val=start; val<=rect.bottom(); val+=yMinor) {
    painter->drawLine(QLineF( r.left(), val, r.right(), val) );
  }
  // Major grid lines
  painter->setPen(QPen(QColor(200,200,200)));
  start=ceil(rect.left()/xMajor)*xMajor;
  for(val=start; val<=rect.right(); val+=xMajor) {
    painter->drawLine(QLineF( val, r.top(), val, r.bottom()) );
  }
  start=ceil(rect.top()/yMajor)*yMajor;
  for(val=start; val<=rect.bottom(); val+=yMajor) {
    painter->drawLine(QLineF( r.left(), val, r.right(), val) );
  }
  painter->restore();
}
void DrawView::drawForeground ( QPainter *  painter,
const QRectF &  rect 
) [protected, virtual]

References DrawScene::currentPoint(), scene(), and w.

{
  painter->save();
  painter->setPen(QPen(QColor(220,0,0)));
  // Draw current point
  QPointF currentPointF=scene()->currentPoint();
  QPoint currentPoint=mapFromScene(currentPointF);
  QRect currentRect(currentPoint.x()-5, currentPoint.y()-5, 10, 10);
  QRectF currentRectF=mapToScene(currentRect).boundingRect();
  if(currentRectF.intersects(rect) ) {
    QPointF c=currentRectF.center();
    qreal dx=0.5 * currentRectF.width();
    qreal dy=0.5 * currentRectF.height();
    painter->drawLine(QLineF( c.x()-dx, c.y(), c.x()+dx, c.y()) );
    painter->drawLine(QLineF( c.x(), c.y()-dy, c.x(), c.y()+dy) );
    painter->drawEllipse(currentRectF);
  }
  // Draw mouse point
  int w=viewport()->width();
  int h=viewport()->height();
  QPointF topLeftF=mapToScene(QPoint(0,0));
  QPointF bottomRightF=mapToScene(QPoint(w,h));
  painter->drawLine(QLineF( _mousePoint.x(), topLeftF.y(), _mousePoint.x(), bottomRightF.y()) );
  painter->drawLine(QLineF( topLeftF.x(), _mousePoint.y(), bottomRightF.x(), _mousePoint.y()) );
  painter->restore();

  QGraphicsView::drawForeground(painter,rect);
}
void DrawView::mouseMoved ( const QPointF &  p) [signal]

Referenced by mouseMoveEvent().

void DrawView::mouseMoveEvent ( QMouseEvent *  e) [protected, virtual]

References mouseMoved(), scene(), and setMousePoint().

{
  setMousePoint(mapToScene(e->pos()) );
  emit mouseMoved(_mousePoint);
  // Nothing better now, need a cache for items (like in GraphContent), see QGraphicsView::CacheMode
  scene()->update();
  //update();
  QGraphicsView::mouseMoveEvent(e);
}
void DrawView::mouseReleased ( const QPointF &  p) [signal]

Referenced by mouseReleaseEvent().

void DrawView::mouseReleaseEvent ( QMouseEvent *  e) [protected, virtual]

References mouseReleased(), scene(), and setMousePoint().

{
  setMousePoint(mapToScene( e->pos()) );
  emit mouseReleased(_mousePoint);
  QGraphicsView::mouseReleaseEvent(e);
  scene()->update();
}
void DrawView::setMousePoint ( const QPointF &  p)

References QGpCoreTools::Rect::add(), DrawScene::nodes(), NoSnap, scene(), SnapContours, SnapGrid, SnapNodes, SnapX, and SnapY.

Referenced by mouseMoveEvent(), and mouseReleaseEvent().

{
  if(_snapOption==NoSnap) {
    _mousePoint=p;
    return;
  }
  switch(_snapType) {
  case SnapNodes: {
      QRectF r=mapToScene(viewport()->rect()).boundingRect();
      if(!r.contains(_mousePoint)) {
        Rect rExt(r.left(), r.top(), r.right(), r.bottom());
        rExt.add(_mousePoint.x(), _mousePoint.y());
        r=QRectF(rExt.x1(), rExt.y1(), rExt.width(), rExt.height());
      }
      QList<QPointF> nodes=scene()->nodes(r);
      nodes.append(scene()->currentPoint());
      qreal d, dmin;
      // Find closest node along X axis
      if(_snapOption & SnapX) {
        dmin=1e99;
        for(QList<QPointF>::iterator it=nodes.begin();it!=nodes.end();it++) {
          d=fabs(it->x()-p.x());
          if(d<dmin) {
            _mousePoint.setX(it->x());
            dmin=d;
          }
        }
      } else {
        _mousePoint.setX(p.x());
      }
      // Find closest node along Y axis
      if(_snapOption & SnapY) {
        dmin=1e99;
        for(QList<QPointF>::iterator it=nodes.begin();it!=nodes.end();it++) {
          d=fabs(it->y()-p.y());
          if(d<dmin) {
            _mousePoint.setY(it->y());
            dmin=d;
          }
        }
      } else {
        _mousePoint.setY(p.y());
      }
    }
    break;
  case SnapContours:
  case SnapGrid:
    // TODO
    _mousePoint=p;
    break;
  }
}
void DrawView::setSnapOption ( SnapOptions  ms) [inline]

Referenced by MapView::setSnapOption().

{_snapOption=ms;}
void DrawView::setSnapType ( SnapType  ms) [inline]

Referenced by MapView::setSnapType().

{_snapType=ms;}
SnapOptions DrawView::snapOption ( ) const [inline]

Referenced by MapView::snapOption().

{return _snapOption;}
SnapType DrawView::snapType ( ) const [inline]

Referenced by MapView::snapType().

{return _snapType;}
void DrawView::wheelEvent ( QWheelEvent *  e) [protected, virtual]

References contains(), and TRACE.

{
  TRACE;
  if(e->modifiers() & Qt::ControlModifier) {
    int d=e->delta()/120;
    QPointF mousePosReal=mapToScene(e->pos());
    if(d>0) {
      scale(1/1.5, 1/1.5);
    } else {
      scale(1.5, 1.5);
    }
    centerOn(mousePosReal);
    QPoint mp=mapFromScene(mousePosReal);
    if(rect().contains(mp)) {
      QCursor::setPos(mapToGlobal( mp) );
    }
    e->accept();
  } else {
    QGraphicsView::wheelEvent(e);
  }
}

The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines