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 "GripperWidget.h"
00031
00032 #include <QtGui>
00033
00039 const int GRIPPER_X = 48;
00040 const int GRIPPER_Y = 48;
00041 const int GRIPPER_CENTER_X = GRIPPER_X / 2;
00042 const int GRIPPER_CENTER_Y = GRIPPER_Y / 2;
00043 const int GRIPPER_BOTTOM_LINE = GRIPPER_Y * 8 / 10;
00044 const int GRIPPER_RIGHT_LINE = GRIPPER_X * 8 / 10;
00045 const int GRIPPER_LEFT_LINE = GRIPPER_X - GRIPPER_RIGHT_LINE;
00046 const int GRIPPER_LINE_WIDTH = 3;
00047 const int GRIPPER_LINE_TOP = GRIPPER_Y / 6;
00048 const int GRIPPER_LINE_BOTTOM = GRIPPER_Y * 5 / 6;
00049 const int GRIPPER_CIRCLE_RADIUS = GRIPPER_X / 3;
00050
00051
00052 GripperWidget::GripperWidget(QWidget *parent)
00053 : QGraphicsView(parent), timerId(0)
00054 {
00055
00056 QGraphicsScene *scene = new QGraphicsScene(this);
00057 scene->setItemIndexMethod(QGraphicsScene::NoIndex);
00058 scene->setSceneRect(0, 0, 50, 50);
00059
00060
00061 setScene(scene);
00062 setCacheMode(CacheBackground);
00063 setViewportUpdateMode(BoundingRectViewportUpdate);
00064 setRenderHint(QPainter::Antialiasing);
00065 setTransformationAnchor(AnchorUnderMouse);
00066 scale(qreal(1), qreal(1));
00067 setMinimumSize(50, 50);
00068 setWindowTitle(tr("Elastic Nodes"));
00069
00070 percent = 1.0;
00071
00072
00073 int x = GRIPPER_CENTER_X - (GRIPPER_CENTER_X - GRIPPER_LEFT_LINE) * percent;
00074 QGraphicsLineItem *line = new QGraphicsLineItem(
00075 x, GRIPPER_LINE_TOP, x, GRIPPER_LINE_BOTTOM);
00076 x = GRIPPER_CENTER_X + (GRIPPER_RIGHT_LINE - GRIPPER_CENTER_X) * percent;
00077 QGraphicsLineItem *line2 = new QGraphicsLineItem(
00078 x, GRIPPER_LINE_TOP, x, GRIPPER_LINE_BOTTOM);
00079
00080 line->setPen(QPen(Qt::red,GRIPPER_LINE_WIDTH));
00081 line2->setPen(QPen(Qt::red,GRIPPER_LINE_WIDTH));
00082
00083 scene->addItem(line);
00084 scene->addItem(line2);
00085
00086 timerId = startTimer(1000 / 25);
00087
00088
00089 }
00090
00091 void GripperWidget::setState(int value)
00092
00093 {
00094 if(value ==1)
00095 percent = 1.0;
00096 else if(value ==2)
00097 percent = 0.0;
00098 else if(value==3)
00099 percent = 0.5;
00100
00101
00102
00103 }
00104
00105
00106 void GripperWidget::timerEvent(QTimerEvent *event)
00107 {
00108 Q_UNUSED(event);
00109
00110
00111 QList<QGraphicsLineItem *> lines;
00112 foreach (QGraphicsItem *item, scene()->items()) {
00113 if (QGraphicsLineItem *l = qgraphicsitem_cast<QGraphicsLineItem *>(item))
00114 lines << l;
00115 }
00116
00117
00118
00119 int x = GRIPPER_CENTER_X - (GRIPPER_CENTER_X - GRIPPER_LEFT_LINE) * percent;
00120
00121 lines.at(0)->setLine(x, GRIPPER_LINE_TOP, x, GRIPPER_LINE_BOTTOM);
00122
00123 x = GRIPPER_CENTER_X + (GRIPPER_RIGHT_LINE - GRIPPER_CENTER_X) * percent;
00124
00125 lines.at(1)->setLine(x, GRIPPER_LINE_TOP, x, GRIPPER_LINE_BOTTOM);
00126 }
00127
00128
00129 void GripperWidget::drawBackground(QPainter *painter, const QRectF &rect)
00130
00131 {
00132 Q_UNUSED(rect);
00133
00134
00135 QRectF sceneRect = this->sceneRect();
00136 QRectF rightShadow(sceneRect.right(), sceneRect.top() + 5, 5, sceneRect.height());
00137 QRectF bottomShadow(sceneRect.left() + 5, sceneRect.bottom(), sceneRect.width(), 5);
00138 if (rightShadow.intersects(rect) || rightShadow.contains(rect))
00139 painter->fillRect(rightShadow, Qt::darkGray);
00140 if (bottomShadow.intersects(rect) || bottomShadow.contains(rect))
00141 painter->fillRect(bottomShadow, Qt::darkGray);
00142
00143
00144
00145 QLinearGradient gradient(sceneRect.topLeft(), sceneRect.bottomRight());
00146 gradient.setColorAt(0, Qt::white);
00147 gradient.setColorAt(1, Qt::lightGray);
00148 painter->fillRect(rect.intersect(sceneRect), gradient);
00149 painter->setBrush(Qt::NoBrush);
00150 painter->drawRect(sceneRect);
00151
00152
00153 painter->setPen(Qt::blue);
00154 painter->drawLine(0, GRIPPER_BOTTOM_LINE, GRIPPER_X, GRIPPER_BOTTOM_LINE);
00155 painter->drawLine(GRIPPER_LEFT_LINE, 0, GRIPPER_LEFT_LINE, GRIPPER_Y);
00156 painter->drawLine(GRIPPER_RIGHT_LINE, 0, GRIPPER_RIGHT_LINE, GRIPPER_Y);
00157
00158 painter->setPen(QPen(Qt::green,2));
00159 painter->drawArc(GRIPPER_CENTER_X - GRIPPER_CIRCLE_RADIUS,
00160 GRIPPER_CENTER_Y - GRIPPER_CIRCLE_RADIUS,
00161 GRIPPER_CIRCLE_RADIUS * 2,GRIPPER_CIRCLE_RADIUS * 2,
00162 0, 360 * 64);
00163 }
00164
00165
00166