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 #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
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
00088 {
00089 angle_type = value;
00090 }
00091
00092
00093 void WristWidget::angleReceived(double value)
00094
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
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
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
00129 {
00130 Q_UNUSED(event);
00131
00132
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
00164 {
00165 Q_UNUSED(rect);
00166
00167
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
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