WristWidget.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 #include "WristWidget.h"
00042 #include <math.h>
00043 #include <QtGui>
00044 
00045 
00046 
00047 WristWidget::WristWidget(QWidget *parent)
00048     : QGraphicsView(parent), timerId(0)
00049 {
00050 
00051     QGraphicsScene *scene = new QGraphicsScene(this);
00052     scene->setItemIndexMethod(QGraphicsScene::NoIndex);
00053     scene->setSceneRect(0, 0, 50, 50);
00054 
00055 
00056     setScene(scene);
00057     setCacheMode(CacheBackground);
00058     setViewportUpdateMode(BoundingRectViewportUpdate);
00059     setRenderHint(QPainter::Antialiasing);
00060     setTransformationAnchor(AnchorUnderMouse);
00061     scale(qreal(1), qreal(1));
00062     setMinimumSize(50, 50);
00063     setWindowTitle(tr("Elastic Nodes"));
00064 
00065 
00066     wristAngle = 0;
00067 
00068 
00069     int x = WRIST_WIDTH / 2 * cos(wristAngle);
00070     int y = WRIST_WIDTH / 2 * sin(wristAngle);
00071     Line *line = new Line(
00072                     WRIST_CENTER_X - x, WRIST_CENTER_Y + y,
00073                     WRIST_CENTER_X + x, WRIST_CENTER_Y - y);
00074 
00075     line->setTransformOriginPoint(WRIST_CENTER_X,WRIST_CENTER_Y);
00076     x = WRIST_HEIGHT * cos(wristAngle + M_PI / 2);
00077     y = WRIST_HEIGHT * sin(wristAngle + M_PI / 2);
00078     QGraphicsLineItem *line2 = new QGraphicsLineItem(
00079                     WRIST_CENTER_X, WRIST_CENTER_Y,
00080                     WRIST_CENTER_X + x, WRIST_CENTER_Y - y);
00081 
00082     line->setPen(QPen(Qt::red,WRIST_LINE_WIDTH));
00083     line2->setPen(QPen(Qt::red,WRIST_LINE_WIDTH));
00084 
00085     line->setFlag(line->ItemIsMovable);
00086     line->setTransformOriginPoint(WRIST_CENTER_X, WRIST_CENTER_Y);
00087     scene->addItem(line);
00088     scene->addItem(line2);
00089 
00090     timerId = startTimer(1000 / 25);
00091 
00092  }
00093 
00094 void WristWidget::degree(bool value)
00095 //if value is true, emit the signal angle in degree,else in radian
00096 {
00097     angle_type = value;
00098 }
00099 
00100 
00101 void WristWidget::angleReceived(double value)
00102 //set a new value for the wrist angle
00103 {
00104     QList<Line  *> lines;
00105     foreach (QGraphicsItem  *item, scene()->items()) {
00106         if (Line  *l = qgraphicsitem_cast<Line  *>(item))
00107             lines << l;
00108     }
00109   lines.at(0)->setRotation(-value);
00110 
00111 }
00112 
00113 void WristWidget::turnClockwise()
00114 //turn the wrist clockwise
00115 {
00116     QList<Line  *> lines;
00117     foreach (QGraphicsItem  *item, scene()->items()) {
00118         if (Line  *l = qgraphicsitem_cast<Line  *>(item))
00119             lines << l;
00120     }
00121   lines.at(0)->setRotation(lines.at(0)->rotation-5);
00122 }
00123 
00124 void WristWidget::turnCounterClockwise()
00125 //turn the wrist counter clockwise
00126 {
00127     QList<Line  *> lines;
00128     foreach (QGraphicsItem  *item, scene()->items()) {
00129         if (Line  *l = qgraphicsitem_cast<Line  *>(item))
00130             lines << l;
00131     }
00132   lines.at(0)->setRotation((lines.at(0)->rotation)+5);
00133 }
00134 
00135 void WristWidget::timerEvent(QTimerEvent *event)
00136  {
00137      Q_UNUSED(event);
00138 
00139      QList<Line  *> lines;
00140      foreach (QGraphicsItem  *item, scene()->items()) {
00141          if (Line  *l = qgraphicsitem_cast<Line  *>(item))
00142              lines << l;
00143      }
00144      if(wristAngle != -lines.at(0)->rotation){
00145             wristAngle = -lines.at(0)->rotation;
00146 
00147         if(angle_type)
00148             emit angle(wristAngle);
00149         else
00150             emit angle(wristAngle/180*M_PI);
00151 
00152         emit angle_rad(wristAngle/180*M_PI);
00153 
00154         QList<QGraphicsLineItem  *> lines2;
00155             foreach (QGraphicsItem  *item, scene()->items()) {
00156                 if (QGraphicsLineItem  *l = qgraphicsitem_cast<QGraphicsLineItem  *>(item))
00157                     lines2 << l;
00158             }
00159 
00160           int x = WRIST_HEIGHT * cos(wristAngle*M_PI/180 + M_PI / 2);
00161           int y = WRIST_HEIGHT * sin(wristAngle*M_PI/180 + M_PI / 2);
00162 
00163           lines2.at(1)->setLine(WRIST_CENTER_X, WRIST_CENTER_Y,WRIST_CENTER_X + x, WRIST_CENTER_Y - y);
00164       }
00165  }
00166 
00167 
00168  void WristWidget::drawBackground(QPainter *painter, const QRectF &rect)
00169  {
00170      Q_UNUSED(rect);
00171 
00172      // Shadow
00173      QRectF sceneRect = this->sceneRect();
00174      QRectF rightShadow(sceneRect.right(), sceneRect.top() + 5, 5, sceneRect.height());
00175      QRectF bottomShadow(sceneRect.left() + 5, sceneRect.bottom(), sceneRect.width(), 5);
00176      if (rightShadow.intersects(rect) || rightShadow.contains(rect))
00177          painter->fillRect(rightShadow, Qt::darkGray);
00178      if (bottomShadow.intersects(rect) || bottomShadow.contains(rect))
00179          painter->fillRect(bottomShadow, Qt::darkGray);
00180 
00181 
00182      // Fill
00183      QLinearGradient gradient(sceneRect.topLeft(), sceneRect.bottomRight());
00184      gradient.setColorAt(0, Qt::white);
00185      gradient.setColorAt(1, Qt::lightGray);
00186      painter->fillRect(rect.intersect(sceneRect), gradient);
00187      painter->setBrush(Qt::NoBrush);
00188      painter->drawRect(sceneRect);
00189 
00190 
00191      painter->setPen(Qt::gray);
00192      painter->drawLine(0, WRIST_CENTER_Y,WRIST_X, WRIST_CENTER_Y);
00193      painter->drawLine( WRIST_CENTER_X, WRIST_CENTER_Y, WRIST_CENTER_X, 0);
00194 
00195      painter->setPen((Qt::blue));
00196      painter->drawArc(WRIST_CENTER_X - WRIST_CIRCLE_RADIUS, WRIST_CENTER_Y - WRIST_CIRCLE_RADIUS,
00197                                      WRIST_CIRCLE_RADIUS * 2, WRIST_CIRCLE_RADIUS * 2,
00198                                      0, 360 * 64);
00199 
00200  }
00201 
00202  void WristWidget::scaleView(qreal scaleFactor)
00203  //scale the view
00204  {
00205      qreal factor = transform().scale(scaleFactor, scaleFactor).mapRect(QRectF(0, 0, 1, 1)).width();
00206      if (factor < 0.07 || factor > 100)
00207          return;
00208 
00209      scale(scaleFactor, scaleFactor);
00210  }
00211 
00212 


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