GripperWidget.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2009, CoroWare
00003  * All rights reserved.
00004  * 
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  * 
00008  *     * Redistributions of source code must retain the above copyright
00009  *       notice, this list of conditions and the following disclaimer.
00010  *     * Redistributions in binary form must reproduce the above copyright
00011  *       notice, this list of conditions and the following disclaimer in the
00012  *       documentation and/or other materials provided with the distribution.
00013  *     * Neither the name of the Willow Garage, Stanford U. nor the names of its
00014  *       contributors may be used to endorse or promote products derived from
00015  *       this software without specific prior written permission.
00016  * 
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00021  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00022  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00023  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00024  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00025  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00026  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00027  * POSSIBILITY OF SUCH DAMAGE.
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 //set the state of the gripper, 1 = open, 2 = closed, 3 = moving
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      receive ROS state value*/
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  //draw the scene background
00131  {
00132      Q_UNUSED(rect);
00133 
00134      // Shadow
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      // Fill
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 


corobot_teleop
Author(s):
autogenerated on Wed Aug 26 2015 11:09:59