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