joint.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2009, CoroWare
00003  * All rights reserved.
00004  * 
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  * 
00008  *     * Redistributions of source code must retain the above copyright
00009  *       notice, this list of conditions and the following disclaimer.
00010  *     * Redistributions in binary form must reproduce the above copyright
00011  *       notice, this list of conditions and the following disclaimer in the
00012  *       documentation and/or other materials provided with the distribution.
00013  *     * Neither the name of the Willow Garage, Stanford U. nor the names of its
00014  *       contributors may be used to endorse or promote products derived from
00015  *       this software without specific prior written permission.
00016  * 
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00021  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00022  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00023  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00024  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00025  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00026  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00027  * POSSIBILITY OF SUCH DAMAGE.
00028  */
00029 
00030 //this class is a little circle made to represent the shoulder, elbow or gripper of an arm
00031 //a flag(ItemIsMovable) can be set to make the joint move.
00032 
00033 
00034  #include <QGraphicsScene>
00035  #include <QGraphicsSceneMouseEvent>
00036  #include <QPainter>
00037  #include <QStyleOption>
00038 
00039  #include "joint.h"
00040  #include "ArmWidget.h"
00041 #include "ArmRotationWidget.h"
00042 
00043  joint::joint(ArmWidget *graphWidget)
00044      : graph(graphWidget)
00045  {
00046      graph_rotation = NULL;
00047      setFlag(ItemSendsGeometryChanges);
00048      setCacheMode(DeviceCoordinateCache);
00049      setZValue(-1);
00050 
00051  }
00052 
00053 joint::joint(ArmRotationWidget *graphWidget)
00054      : graph_rotation(graphWidget)
00055  {
00056      graph = NULL;
00057      setFlag(ItemSendsGeometryChanges);
00058      setCacheMode(DeviceCoordinateCache);
00059      setZValue(-1);
00060 
00061  }
00062 
00063 
00064  bool joint::advance()
00065  //update the position of the joint
00066  {
00067      if (newPos == pos())
00068          return false;
00069 
00070      setPos(newPos);
00071      return true;
00072  }
00073 
00074  QRectF joint::boundingRect() const
00075  {
00076      qreal adjust = 2;
00077      return QRectF(-10 - adjust, -10 - adjust,
00078                    23 + adjust, 23 + adjust);
00079  }
00080 
00081  QPainterPath joint::shape() const
00082  {
00083      QPainterPath path;
00084      path.addEllipse(-10, -10, 20, 20);
00085      return path;
00086  }
00087 
00088  void joint::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *)
00089  {
00090      painter->setPen(Qt::NoPen);
00091      painter->setBrush(Qt::darkGray);
00092      painter->drawEllipse(-7, -7, 10, 10);
00093 
00094 
00095 
00096      QRadialGradient gradient(-3, -3, 10);
00097      if (option->state & QStyle::State_Sunken) {
00098          gradient.setCenter(3, 3);
00099          gradient.setFocalPoint(3, 3);
00100          gradient.setColorAt(1, QColor(Qt::yellow).light(120));
00101          gradient.setColorAt(0, QColor(Qt::darkYellow).light(120));
00102      } else {
00103          gradient.setColorAt(0, Qt::yellow);
00104          gradient.setColorAt(1, Qt::darkYellow);
00105      }
00106      painter->setBrush(gradient);
00107      painter->setPen(QPen(Qt::black, 0));
00108      painter->drawEllipse(-10, -10, 10, 10);
00109  }
00110 
00111  QVariant joint::itemChange(GraphicsItemChange change, const QVariant &value)
00112 // Request update of the parent graph
00113  {
00114      switch (change) {
00115          if(graph != NULL)
00116                 graph->itemMoved();
00117           if(graph_rotation != NULL)
00118                 graph_rotation->itemMoved();
00119          break;
00120      default:
00121          break;
00122      };
00123 
00124      return QGraphicsItem::itemChange(change, value);
00125  }
00126 
00127  void joint::mousePressEvent(QGraphicsSceneMouseEvent *event)
00128  {
00129      update();
00130      QGraphicsItem::mousePressEvent(event);
00131  }
00132 
00133  void joint::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
00134  {
00135      update();
00136      QGraphicsItem::mouseReleaseEvent(event);
00137  }


corobot_teleop
Author(s):
autogenerated on Sun Oct 5 2014 23:18:16