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


corobot_teleop
Author(s): Morgan Cormier/Gang Li/mcormier@coroware.com
autogenerated on Tue Jan 7 2014 11:39:41