00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 
00032 
00033 
00034 
00035 
00036 
00037 
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 
00096 {
00097     angle_type = value;
00098 }
00099 
00100 
00101 void WristWidget::angleReceived(double value)
00102 
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 
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 
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      
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      
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  
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