ThymioActionButtons.cpp
Go to the documentation of this file.
00001 #include <QPainter>
00002 #include <QGraphicsSceneMouseEvent>
00003 
00004 #include "ThymioButtons.h"
00005 
00006 
00007 namespace Aseba
00008 {
00009         // Move Action
00010         ThymioMoveAction::ThymioMoveAction( QGraphicsItem *parent ) :
00011                 ThymioButton(false, 0.2, false, false, parent)
00012         {
00013                 setData(0, "action");
00014                 setData(1, "move");
00015 
00016                 QTransform transMatrix(2.0,0.0,0.0,0.0,2.3,0.0,0.0,0.0,1.0);
00017                                 
00018                 for(int i=0; i<2; i++)
00019                 {
00020                         QSlider *s = new QSlider(Qt::Vertical);
00021                         s->setRange(-500,500);
00022                         s->setStyleSheet("QSlider::groove:vertical { width: 14px; border: 2px solid #000000; "
00023                                                           "background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #00FF00, stop:0.25 #FFFF00, stop:0.5 #FF0000, stop:0.75 #FFFF00, stop:1 #00FF00 ); }"
00024                                                       "QSlider::handle:vertical { "
00025                                                       "background: #FFFFFF; "
00026                                                       "border: 2px solid #000000; height: 10px; width: 20px; margin: 0px 0; }");
00027                         s->setSliderPosition(0);
00028 
00029                         QGraphicsProxyWidget *w = new QGraphicsProxyWidget(this);
00030                         w->setWidget(s);
00031                         w->setPos(10+i*200, 15);
00032                         w->setTransform(transMatrix);
00033                         
00034                         sliders.push_back(s);
00035                         widgets.push_back(w);
00036                         
00037                         connect(s, SIGNAL(valueChanged(int)), this, SLOT(valueChangeDetected()));
00038                         connect(s, SIGNAL(valueChanged(int)), this, SLOT(updateIRButton()));
00039                 }               
00040 
00041                 timer = new QTimeLine(2000);
00042                 timer->setFrameRange(0, 100);
00043                 timer->setCurveShape(QTimeLine::LinearCurve);
00044                 animation = new QGraphicsItemAnimation(this);
00045                 animation->setItem(thymioBody);
00046                 animation->setTimeLine(timer);                          
00047                 thymioBody->setTransformOriginPoint(0,-14);
00048         }
00049 
00050         ThymioMoveAction::~ThymioMoveAction()
00051         {
00052                 delete(timer);
00053         }
00054 
00055         void ThymioMoveAction::paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget)
00056         {
00057                 Q_UNUSED(option);
00058                 Q_UNUSED(widget);
00059         
00060                 painter->setPen(QColor(147, 134, 115));
00061                 painter->setBrush(buttonColor); // filling
00062                 painter->drawRoundedRect(0, 0, 256, 256, 5, 5);                 
00063         
00064                 qreal pt[2];
00065                 for(int i=0; i<2; i++) 
00066                         pt[i] = (sliders[i]->value())*0.06; // [-30,30]
00067                 
00068                 timer->stop();
00069                 timer->setLoopCount(1);
00070 
00071                 double angle=0;
00072                 double center = -23.5*(pt[1]+pt[0])/(pt[1] == pt[0] ? 0.03 : (pt[1]-pt[0]));            
00073                 
00074                 for (int i = 0; i < 200; ++i)
00075                 {
00076                         qreal step = i/200.0;
00077                         
00078                         angle = (pt[0]-pt[1]-0.04)*3*step;
00079                         animation->setPosAt(step, QPointF(center*(1-cos(-angle*3.14/180))+128,center*sin(-angle*3.14/180)+128));        
00080                         animation->setRotationAt(step, angle);
00081                 }
00082                 timer->start();
00083         }
00084 
00085         QPixmap ThymioMoveAction::image(bool on)
00086         {
00087                 QPixmap pixmap(256, 256);
00088                 pixmap.fill(buttonColor);
00089                 QPainter painter(&pixmap);
00090                 painter.setRenderHint(QPainter::Antialiasing);
00091 
00092                 painter.translate(QPointF(128,128));
00093                 painter.scale(0.4,0.4);
00094                 painter.rotate(thymioBody->rotation());
00095                 thymioBody->paint(&painter, 0, 0);
00096                 painter.resetTransform();
00097 
00098                 for(int i=0; i<getNumButtons(); i++)
00099                 {
00100                         painter.translate(widgets[i]->pos());
00101                         painter.scale(2.0, 2.3);
00102                         sliders[i]->render(&painter);
00103                         painter.resetTransform();
00104                 }
00105 
00106                 return pixmap;
00107         }
00108 
00109         void ThymioMoveAction::valueChangeDetected()
00110         {
00111                 update();
00112         }
00113 
00114         void ThymioMoveAction::setClicked(int i, int status) 
00115         { 
00116                 if(i<getNumButtons()) 
00117                         sliders[i]->setSliderPosition(status);          
00118         }
00119 
00120         int ThymioMoveAction::isClicked(int i) 
00121         { 
00122                 if(i<getNumButtons()) 
00123                         return sliders[i]->value();             
00124                 return -1;
00125         }
00126                                 
00127         // Color Action
00128         ThymioColorAction::ThymioColorAction( QGraphicsItem *parent ) :
00129                 ThymioButton(false, 1.0, true, false, parent)
00130         {
00131                 setData(0, "action");
00132                 setData(1, "color");
00133 
00134                 QTransform transMatrix(1.0,0.0,0.0,0.0,2.0,0.0,0.0,0.0,1.0);
00135                 QString sliderColor("FF0000");
00136                 
00137                 for(int i=0; i<3; i++)
00138                 {
00139                         if( i == 1 ) sliderColor="00FF00";
00140                         else if( i == 2 ) sliderColor="0000FF";
00141 
00142                         QSlider *s = new QSlider(Qt::Horizontal);
00143                         s->setRange(0,32);
00144                         s->setStyleSheet(QString("QSlider::groove:horizontal { height: 14px; border: 2px solid #000000; "
00145                                                           "background: qlineargradient(x1:0, y1:0, x2:1, y2:0, stop:0 #000000, stop:1 #%0); }"
00146                                                       "QSlider::handle:horizontal { "
00147                                                       "background: #FFFFFF; "
00148                                                       "border: 5px solid #000000; width: 18px; margin: -2px 0; }").arg(sliderColor));
00149                         s->setSliderPosition(0);
00150 
00151                         QGraphicsProxyWidget *w = new QGraphicsProxyWidget(this);
00152                         w->setWidget(s);
00153                         w->setPos(27, 70+i*60);
00154                         w->setTransform(transMatrix);
00155                         
00156                         
00157                         sliders.push_back(s);
00158                         widgets.push_back(w);
00159                         
00160                         connect(s, SIGNAL(valueChanged(int)), this, SLOT(valueChangeDetected()));
00161                         connect(s, SIGNAL(valueChanged(int)), this, SLOT(updateIRButton()));
00162                 }
00163 
00164         }
00165 
00166         void ThymioColorAction::paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget)
00167         {
00168                 Q_UNUSED(option);
00169                 Q_UNUSED(widget);
00170                 
00171                 painter->setPen(QColor(147, 134, 115));
00172                 painter->setBrush(buttonColor); // filling
00173                 painter->drawRoundedRect(0, 0, 256, 256, 5, 5);
00174 
00175                 thymioBody->bodyColor = QColor(sliders[0]->value()*5.46875+80, 
00176                                                                            sliders[1]->value()*5.46875+80, 
00177                                                                            sliders[2]->value()*5.46875+80);
00178         }
00179 
00180         QPixmap ThymioColorAction::image(bool on)
00181         {
00182                 QPixmap pixmap(256, 256);
00183                 pixmap.fill(buttonColor);
00184                 QPainter painter(&pixmap);
00185                 painter.setRenderHint(QPainter::Antialiasing);
00186 
00187                 painter.translate(thymioBody->pos());
00188                 painter.scale(thymioBody->scale(),thymioBody->scale());
00189                 painter.rotate(thymioBody->rotation());
00190                 thymioBody->paint(&painter, 0, 0);
00191                 painter.resetTransform();
00192 
00193                 for(int i=0; i<getNumButtons(); i++)
00194                 {
00195                         painter.translate(widgets[i]->pos());
00196                         painter.scale(1.0, 2.0);
00197                         sliders[i]->render(&painter);
00198                         painter.resetTransform();
00199                 }
00200         
00201                 return pixmap;
00202         }
00203 
00204         void ThymioColorAction::valueChangeDetected()
00205         {
00206                 update();
00207         }
00208 
00209         void ThymioColorAction::setClicked(int i, int status) 
00210         { 
00211                 if(i<getNumButtons()) 
00212                         sliders[i]->setSliderPosition(status);          
00213         }
00214 
00215         int ThymioColorAction::isClicked(int i) 
00216         { 
00217                 if(i<getNumButtons()) 
00218                         return sliders[i]->value();             
00219                 return -1;
00220         }
00221         
00222         // Circle Action
00223         ThymioCircleAction::ThymioCircleAction(QGraphicsItem *parent) : 
00224                 ThymioButton(false, 1.0, true, false, parent)
00225         {
00226                 setData(0, "action");           
00227                 setData(1, "circle");
00228                 
00229                 for(uint i=0; i<8; i++)
00230                 {
00231                         ThymioClickableButton *button = new ThymioClickableButton(QRectF(-25,-15,50,30), THYMIO_RECTANGULAR_BUTTON, 3, this);
00232 
00233                         qreal offset = (qreal)i;                        
00234                         button->setRotation(45*offset);
00235                         button->setPos(128 - 90*qCos(0.785398163*offset+1.57079633), 
00236                                                    128 - 90*qSin(0.785398163*offset+1.57079633));
00237 
00238                         button->setBeginButtonColor(QColor(255, 172, 155));
00239                         button->setButtonColor(QColor(255, 90, 54));
00240 
00241                         thymioButtons.push_back(button);
00242                         connect(button, SIGNAL(stateChanged()), this, SLOT(updateIRButton()));
00243                 }
00244         }
00245                 
00246         // Sound Action
00247         void ThymioSoundAction::Speaker::paint (QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget) 
00248         {               
00249                 Q_UNUSED(option);
00250                 Q_UNUSED(widget);
00251                 
00252                 painter->setPen(QPen(Qt::black, 3.0, Qt::SolidLine, Qt::RoundCap));
00253                 painter->setBrush(Qt::black);
00254                 painter->drawRect(25, 108, 30, 50);
00255 
00256                 QPointF points[4];
00257                 points[0] = QPointF(52, 158);
00258                 points[1] = QPointF(52, 108);
00259                 points[2] = QPointF(84, 78);
00260                 points[3] = QPointF(84, 188);
00261                 painter->drawPolygon(points, 4);
00262                 painter->drawChord(64, 78, 40, 110, -1440, 2880);
00263                 
00264                 painter->drawArc(64, 68, 60, 130, -1440, 2880);
00265                 painter->drawArc(64, 53, 80, 160, -1440, 2880);
00266         }       
00267         
00268         ThymioSoundAction::ThymioSoundAction(QGraphicsItem *parent) :
00269                 ThymioButton(false, 1.0, true, false, parent)
00270         {
00271                 setData(0, "action");           
00272                 setData(1, "sound");
00273 
00274                 for(int i=0; i<3; i++) 
00275                 {               
00276                         ThymioFaceButton *button = new ThymioFaceButton(QRectF(-30,-30,60,60), (ThymioSmileType)i, this);
00277 
00278                         button->setPos(195,70+70*(qreal)i);
00279                         button->setButtonColor(i == 0 ? Qt::green : (i == 1 ? Qt::yellow : Qt::red) );
00280                         button->setData(0, i);  
00281                         button->setToggleState(false);                                                                          
00282                         
00283                         thymioButtons.push_back(button);
00284                         connect(button, SIGNAL(stateChanged()), this, SLOT(updateIRButton()));
00285                 }
00286                 thymioButtons.at(0)->setClicked(1);
00287                 
00288                 for(int i=0; i<3; i++)
00289                 {
00290                         for(int j=0; j<3; j++) 
00291                         {
00292                                 if( i!=j )
00293                                         thymioButtons.at(i)->addSibling(thymioButtons.at(j));
00294                         }
00295                 }
00296                 
00297                 speaker = new Speaker(this);
00298         }
00299 
00300         QPixmap ThymioSoundAction::image(bool on)
00301         {
00302                 QPixmap pixmap = ThymioButton::image(on);
00303                 QPainter painter(&pixmap);
00304 
00305                 painter.translate(speaker->pos());
00306                 speaker->paint(&painter, 0, 0);
00307                 painter.resetTransform();
00308                 
00309                 return pixmap;
00310         }       
00311         
00312         // Memory Action
00313         ThymioMemoryAction::ThymioMemoryAction(QGraphicsItem *parent) : 
00314                 ThymioButton(false, 1.0, true, false, parent)
00315         {
00316                 setData(0, "action");           
00317                 setData(1, "memory");
00318                 
00319                 for(uint i=0; i<4; i++)
00320                 {
00321                         //ThymioClickableButton *button = new ThymioClickableButton(QRectF(-15,-30,30,60), THYMIO_CIRCULAR_BUTTON, 2, this);//THYMIO_RECTANGULAR_BUTTON , 2, this);
00322                         //button->setPos(128 + (2-i)*(i%2)*60, 138 + (i-1)*((i+1)%2)*60); 
00323                         //button->setRotation(90*(i+1));
00324                         ThymioClickableButton *button = new ThymioClickableButton(QRectF(-20,-20,40,40), THYMIO_CIRCULAR_BUTTON, 2, this);
00325 
00326                         button->setPos(128, i*60 + 40);
00327                         button->setButtonColor(QColor(255,200,0));
00328 
00329                         thymioButtons.push_back(button);
00330                         connect(button, SIGNAL(stateChanged()), this, SLOT(updateIRButton()));
00331                 }
00332         }       
00333 
00334 };
00335 


aseba
Author(s): Stéphane Magnenat
autogenerated on Thu Jan 2 2014 11:17:17