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


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