Go to the documentation of this file.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 "ArmRotationWidget.h"
00031
00032 #include <math.h>
00033 #include <QtGui>
00034 #include "corobot.h"
00035
00036
00037
00038 const int ARM_X = 120;
00039 const int ARM_Y = 120;
00040 const int ARM_CENTER_X = ARM_X / 2;
00041 const int ARM_BODY_TOP = ARM_X * 7 / 10;
00042 const int ARM_SHOULDER_X = ARM_CENTER_X - ARM_X / 7;
00043 const int SIZE_ARM = 25;
00044 const int LENGTH_ROBOT = 55;
00045 const int WIDTH_ROBOT = 40;
00046 ArmRotationWidget::ArmRotationWidget(QWidget *parent)
00047 : QGraphicsView(parent), timerId(0)
00048 {
00049
00050 QGraphicsScene *scene = new QGraphicsScene(this);
00051 scene->setItemIndexMethod(QGraphicsScene::NoIndex);
00052 scene->setSceneRect(0, 00, 210, 110);
00053
00054 setScene(scene);
00055 setCacheMode(CacheBackground);
00056 setViewportUpdateMode(BoundingRectViewportUpdate);
00057 setRenderHint(QPainter::Antialiasing);
00058 setTransformationAnchor(AnchorUnderMouse);
00059 scale(qreal(1), qreal(1));
00060 setMinimumSize(210, 110);
00061 setWindowTitle(tr("Elastic Nodes"));
00062
00063 joint *joint1 = new joint(this);
00064 joint *joint2 = new joint(this);
00065
00066 scene->addItem(joint1);
00067 scene->addItem(joint2);
00068
00069 joint2->setFlag(joint2->ItemIsMovable);
00070
00071
00072 joint1->setPos(this->sceneRect().right()/2 + 5, this->sceneRect().bottom()-10 - LENGTH_ROBOT * 4/5 +5);
00073 joint2->setPos(this->sceneRect().right()/2 + 5,this->sceneRect().bottom()-10 - LENGTH_ROBOT * 4/5 +5 - SIZE_ARM);
00074
00075 end_effector = QPointF(0,0);
00076
00077 QGraphicsLineItem *line = new QGraphicsLineItem(joint1->pos().x()-5,joint1->pos().y()-5,joint2->pos().x()-5,joint2->pos().y()-5);
00078 scene->addItem(line);
00079
00080 }
00081
00082
00083 void ArmRotationWidget::itemMoved()
00084
00085 {
00086 if (!timerId)
00087 timerId = startTimer(1000 / 25);
00088 }
00089
00090
00091
00092 void ArmRotationWidget::moveArmLeft()
00093
00094 {
00095 this->scene()->items().at(2)->setPos(end_effector + QPointF(-5,0));
00096 }
00097
00098 void ArmRotationWidget::moveArmRight()
00099
00100 {
00101 this->scene()->items().at(2)->setPos(end_effector + QPointF(5,0));
00102 }
00103
00104
00105 void ArmRotationWidget::timerEvent(QTimerEvent *event)
00106
00107
00108 {
00109 Q_UNUSED(event);
00110 double angle;
00111
00112
00113 QList<joint *> joints;
00114 foreach (QGraphicsItem *item, scene()->items()) {
00115 if (joint *j = qgraphicsitem_cast<joint *>(item))
00116 joints << j;
00117 }
00118
00119
00120 if(joints.at(1)->x()!=end_effector.x() || joints.at(1)->y()!=end_effector.y()){
00121 double t1,t2;
00122
00123 double x = joints.at(0)->x() - joints.at(1)->x();
00124 double y = joints.at(0)->y() - joints.at(1)->y();
00125
00126
00127 if(y > 0)
00128 {
00129 angle = atan(x/y) + M_PI/2;
00130 joints.at(1)->setPos(this->sceneRect().right()/2 + 5 + cos(angle)*SIZE_ARM, this->sceneRect().bottom()-10 - LENGTH_ROBOT * 4/5 +5 - sin(angle)*SIZE_ARM);
00131 emit armAngle_rad(angle);
00132 }
00133 else
00134 joints.at(1)->setPos(end_effector);
00135
00136
00137 QList<QGraphicsLineItem *> lines;
00138 foreach (QGraphicsItem *item, scene()->items()) {
00139 if (QGraphicsLineItem *l = qgraphicsitem_cast<QGraphicsLineItem *>(item))
00140 lines << l;
00141 }
00142
00143 lines.at(0)->setLine(joints.at(0)->pos().x()-5,joints.at(0)->pos().y()-5,joints.at(1)->pos().x()-5,joints.at(1)->pos().y()-5);
00144
00145 end_effector = joints.at(1)->pos();
00146 }
00147
00148
00149 }
00150
00151
00152 void ArmRotationWidget::drawBackground(QPainter *painter, const QRectF &rect)
00153
00154 {
00155 Q_UNUSED(rect);
00156
00157
00158 QRectF sceneRect = this->sceneRect();
00159 QRectF rightShadow(sceneRect.right(), sceneRect.top() + 5, 5, sceneRect.height());
00160 QRectF bottomShadow(sceneRect.left() + 5, sceneRect.bottom(), sceneRect.width(), 5);
00161 if (rightShadow.intersects(rect) || rightShadow.contains(rect))
00162 painter->fillRect(rightShadow, Qt::darkGray);
00163 if (bottomShadow.intersects(rect) || bottomShadow.contains(rect))
00164 painter->fillRect(bottomShadow, Qt::darkGray);
00165
00166
00167
00168 QLinearGradient gradient(sceneRect.topLeft(), sceneRect.bottomRight());
00169 gradient.setColorAt(0, Qt::white);
00170 gradient.setColorAt(1, Qt::lightGray);
00171 painter->fillRect(rect.intersect(sceneRect), gradient);
00172 painter->setBrush(Qt::NoBrush);
00173 painter->drawRect(sceneRect);
00174
00175
00176 QRectF robot((sceneRect.right() - sceneRect.left())/2 - WIDTH_ROBOT/2, sceneRect.bottom()-LENGTH_ROBOT-10, WIDTH_ROBOT, LENGTH_ROBOT);
00177 painter->fillRect(robot, Qt::gray);
00178 painter->setBrush(Qt::NoBrush);
00179 painter->drawRect(robot);
00180
00181
00182
00183 QRectF textRect(sceneRect.left() + 4, sceneRect.top() + 4,
00184 sceneRect.width() - 4, sceneRect.height() - 4);
00185
00186 QFont font = painter->font();
00187 font.setBold(true);
00188 font.setPointSize(14);
00189 painter->setFont(font);
00190 painter->setPen(Qt::lightGray);
00191 painter->setPen(Qt::black);
00192 }
00193