GripperWidget.cpp
Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
00004 ** All rights reserved.
00005 ** Contact: Nokia Corporation (qt-info@nokia.com)
00006 **
00007 ** This file is part of the examples of the Qt Toolkit.
00008 **
00009 ** $QT_BEGIN_LICENSE:BSD$
00010 ** You may use this file under the terms of the BSD license as follows:
00011 **
00012 ** "Redistribution and use in source and binary forms, with or without
00013 ** modification, are permitted provided that the following conditions are
00014 ** met:
00015 **   * Redistributions of source code must retain the above copyright
00016 **     notice, this list of conditions and the following disclaimer.
00017 **   * Redistributions in binary form must reproduce the above copyright
00018 **     notice, this list of conditions and the following disclaimer in
00019 **     the documentation and/or other materials provided with the
00020 **     distribution.
00021 **   * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
00022 **     the names of its contributors may be used to endorse or promote
00023 **     products derived from this software without specific prior written
00024 **     permission.
00025 **
00026 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00027 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00028 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00029 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00030 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00031 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00032 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00033 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00034 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00035 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00036 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
00037 ** $QT_END_LICENSE$
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 //set the state of the gripper, 1 = open, 2 = closed, 3 = moving
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      receive ROS state value*/
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  //draw the scene background
00137  {
00138      Q_UNUSED(rect);
00139 
00140      // Shadow
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      // Fill
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  //scale the view
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 


corobot_teleop
Author(s): Morgan Cormier/Gang Li/mcormier@coroware.com
autogenerated on Tue Jan 7 2014 11:39:41