ThymioButtons.h
Go to the documentation of this file.
00001 #ifndef THYMIO_BUTTONS_H
00002 #define THYMIO_BUTTONS_H
00003 
00004 #include <QGraphicsItem>
00005 #include <QGraphicsSvgItem>
00006 #include <QGraphicsLayoutItem>
00007 #include <QtCore/qmath.h>
00008 #include <QMimeData>
00009 #include <QPushButton>
00010 #include <QResizeEvent>
00011 #include <QDebug>
00012 #include <QSlider>
00013 #include <QGraphicsProxyWidget>
00014 #include <QGraphicsItemAnimation>
00015 #include <QTimeLine>
00016 
00017 #include "ThymioIntermediateRepresentation.h"
00018 
00019 namespace Aseba
00020 {
00023         enum ThymioButtonType 
00024         {
00025                 THYMIO_CIRCULAR_BUTTON = 0,
00026                 THYMIO_TRIANGULAR_BUTTON,
00027                 THYMIO_RECTANGULAR_BUTTON
00028         };
00029 
00030         enum ThymioSmileType 
00031         {
00032                 THYMIO_SMILE_BUTTON = 0,
00033                 THYMIO_NEUTRAL_BUTTON,
00034                 THYMIO_FROWN_BUTTON
00035         };
00036 
00037         class ThymioClickableButton : public QGraphicsObject
00038         {       
00039                 Q_OBJECT
00040                 
00041         public:
00042                 ThymioClickableButton ( QRectF rect, ThymioButtonType type, int nstates = 2, QGraphicsItem *parent=0 );
00043                 void paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget = 0);
00044 
00045                 int isClicked() { return buttonClicked; }
00046                 void setClicked(int clicked) { buttonClicked = clicked; }
00047                 void setToggleState(bool state) { toggleState = state; }
00048                 void setNumStates(int num) { numStates = num > 2 ? num : 2; }
00049                 int getNumStates() const { return numStates; }
00050 
00051                 QRectF boundingRect() const { return boundingRectangle; }
00052                 void setButtonColor(QColor color) { buttonColor = color; }
00053                 void setBeginButtonColor(QColor color) { buttonBeginColor = color; }
00054 
00055                 void addSibling(ThymioClickableButton *s) { siblings.push_back(s); }
00056         
00057         signals:
00058                 void stateChanged();
00059         
00060         protected:
00061                 ThymioButtonType buttonType;
00062                 int buttonClicked;
00063                 bool toggleState;
00064                 int numStates;
00065                 
00066                 QRectF boundingRectangle;               
00067                 QColor buttonColor;
00068                 QColor buttonBeginColor;
00069 
00070                 QList<ThymioClickableButton*> siblings;
00071 
00072                 virtual void mousePressEvent ( QGraphicsSceneMouseEvent * event );
00073         };
00074 
00075         class ThymioFaceButton : public ThymioClickableButton
00076         {
00077         public:
00078                 ThymioFaceButton ( QRectF rect, ThymioSmileType type, QGraphicsItem *parent=0 );
00079                 void paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget = 0);
00080         private:
00081                 QRectF leftEye;
00082                 QRectF rightEye;
00083                 QRectF mouth;
00084                 qreal arcStart;
00085                 qreal arcEnd;
00086         };
00087 
00088         // Button
00089         class ThymioButton : public QGraphicsSvgItem
00090         {
00091                 Q_OBJECT
00092                 
00093         public:
00094                 class ThymioBody : public QGraphicsItem
00095                 {
00096                 public:
00097                         ThymioBody(QGraphicsItem *parent=0) : QGraphicsItem(parent), bodyColor(Qt::white), up(true) { }
00098                         virtual void paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget = 0);
00099                         QRectF boundingRect() const { return QRectF(0, 0, 256, 256); }
00100                         void setUp(bool u) { up = u; }
00101                         
00102                         QColor bodyColor;
00103                 private:
00104                         bool up;
00105                 };
00106                 
00107                 ThymioButton(bool eventButton = true, qreal scale=1.0, bool up=true, bool advanced=false, QGraphicsItem *parent=0);
00108                 ~ThymioButton();
00109 
00110                 virtual void paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget = 0);
00111                 QRectF boundingRect() const { return QRectF(0, 0, 256+trans, 256); }
00112 
00113                 void setButtonColor(QColor color) { buttonColor = color; update(); }
00114                 QColor getButtonColor() const { return buttonColor; }
00115                 virtual void setClicked(int i, int status);
00116                 virtual int isClicked(int i) { if( i<thymioButtons.size() ) return thymioButtons.at(i)->isClicked(); return -1; }
00117 
00118                 void setParentID(int id) { parentID = id; }
00119                 int getParentID() const { return parentID; }
00120                 QString getType() const { return data(0).toString(); }
00121                 QString getName() const { return data(1).toString(); }
00122                 virtual int getNumButtons() { return thymioButtons.size(); }
00123                 void setScaleFactor(qreal factor);
00124                 void setAdvanced(bool advanced);
00125                 int getState() const;
00126                 void setState(int val);
00127                 
00128                 virtual QPixmap image(bool on=true);
00129         
00130                 virtual bool isValid(); // returns true if the current button state is valid, i.e., at least one button is on
00131         
00132                 ThymioIRButton *getIRButton();
00133 
00134         signals:
00135                 void stateChanged();
00136 
00137         private slots:
00138                 void updateIRButton();
00139                 
00140         protected:
00141                 QList<ThymioClickableButton*> thymioButtons;
00142                 QList<ThymioClickableButton*> stateButtons;
00143                 ThymioBody *thymioBody;
00144                 ThymioIRButton *buttonIR;
00145                 QColor buttonColor;
00146                 int parentID;
00147                 qreal scaleFactor;
00148                 qreal trans;
00149 
00150                 QPointF dragStartPosition;
00151 
00152                 virtual void mousePressEvent ( QGraphicsSceneMouseEvent * event );
00153                 virtual void mouseReleaseEvent( QGraphicsSceneMouseEvent *event );
00154                 virtual void mouseMoveEvent( QGraphicsSceneMouseEvent *event );
00155         };
00156         
00157         class ThymioButtonSet : public QGraphicsObject
00158         {
00159                 Q_OBJECT
00160                 
00161         public:
00162                 class ThymioRemoveButton : public QGraphicsItem
00163                 {
00164                 public:
00165                         ThymioRemoveButton(QGraphicsItem *parent=0);
00166                         virtual void paint (QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget = 0);                 
00167                         QRectF boundingRect() const { return QRectF(-64, -64, 128, 128); }
00168                 };
00169 
00170                 class ThymioAddButton : public QGraphicsItem
00171                 {
00172                 public:
00173                         ThymioAddButton(QGraphicsItem *parent=0);
00174                         virtual void paint (QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget = 0);                 
00175                         QRectF boundingRect() const { return QRectF(-64, -64, 128, 128); }
00176                 };
00177                         
00178                 ThymioButtonSet(int row, bool advanced, QGraphicsItem *parent=0);
00179                 
00180                 virtual void paint (QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget = 0);         
00181                 QRectF boundingRect() const { return advancedMode? QRectF(0, 0, 1064, 400) : QRectF(0, 0, 1000, 400); }
00182 
00183                 void addEventButton(ThymioButton *event);
00184                 void addActionButton(ThymioButton *action);
00185                 void setRow(int row);
00186                 int getRow() { return data(1).toInt(); }
00187                 ThymioButton *getEventButton() { return eventButton; }
00188                 ThymioButton *getActionButton() { return actionButton; }
00189                 
00190                 bool eventExists() { return eventButton == 0 ? false : true; }
00191                 bool actionExists() { return actionButton == 0 ? false : true; }
00192                 void setColorScheme(QColor eventColor, QColor actionColor);
00193                 
00194                 virtual QPixmap image();
00195                 void setScale(qreal factor);
00196                 void setAdvanced(bool advanced);
00197                 
00198                 ThymioIRButtonSet *getIRButtonSet() { return &buttonSetIR; }
00199 
00200                 void setErrorStatus(bool flag) { errorFlag = flag; }
00201                 
00202         signals:
00203                 void buttonUpdated();
00204         
00205         public slots:
00206                 void stateChanged();
00207         
00208         private:
00209                 ThymioButton *eventButton;
00210                 ThymioButton *actionButton;
00211                 ThymioRemoveButton *deleteButton;
00212                 ThymioAddButton *addButton;
00213                 ThymioIRButtonSet buttonSetIR;
00214                 
00215                 QColor eventButtonColor;
00216                 QColor actionButtonColor;
00217                 
00218                 bool highlightEventButton;
00219                 bool highlightActionButton;
00220                 bool errorFlag;
00221                 bool advancedMode;
00222                 qreal trans;
00223                 qreal xpos;
00224                 
00225                 virtual void mousePressEvent ( QGraphicsSceneMouseEvent *event );
00226                 virtual void mouseMoveEvent( QGraphicsSceneMouseEvent *event );
00227                 virtual void mouseReleaseEvent( QGraphicsSceneMouseEvent *event );
00228 
00229                 virtual void dragEnterEvent(QGraphicsSceneDragDropEvent *event);
00230                 virtual void dragMoveEvent(QGraphicsSceneDragDropEvent *event);
00231                 virtual void dragLeaveEvent(QGraphicsSceneDragDropEvent *event);
00232                 virtual void dropEvent(QGraphicsSceneDragDropEvent *event);
00233         };
00234         
00235         class ThymioPushButton : public QPushButton
00236         {
00237                 Q_OBJECT
00238                                 
00239         public:
00240                 ThymioPushButton(QString name, QSvgRenderer *renderer=0, QWidget *parent=0);
00241                 ~ThymioPushButton();
00242                         
00243                 void changeButtonColor(QColor color);
00244                 
00245         protected:
00246                 virtual void mouseMoveEvent( QMouseEvent *event );
00247                 virtual void dragEnterEvent( QDragEnterEvent *event );
00248                 virtual void dropEvent( QDropEvent *event );
00249 
00250         private:
00251                 ThymioButton *thymioButton;
00252                 int prevSpan;
00253         };      
00254         
00255         // Buttons Event
00256         class ThymioButtonsEvent : public ThymioButton
00257         {
00258         public:
00259                 ThymioButtonsEvent(QGraphicsItem *parent=0, bool advanced=false);
00260         };
00261         
00262         // Proximity Event
00263         class ThymioProxEvent : public ThymioButton
00264         {
00265         public:
00266                 ThymioProxEvent(QGraphicsItem *parent=0, bool advanced=false);
00267         };
00268 
00269         // Proximity Ground Event
00270         class ThymioProxGroundEvent : public ThymioButton
00271         {
00272         public:
00273                 ThymioProxGroundEvent(QGraphicsItem *parent=0, bool advanced=false);
00274         };
00275 
00276         // Tap Event
00277         class ThymioTapEvent : public ThymioButton
00278         {
00279         public:
00280                 ThymioTapEvent(QGraphicsItem *parent=0, bool advanced=false);
00281         };
00282 
00283         // Clap Event
00284         class ThymioClapEvent : public ThymioButton
00285         {
00286         public:
00287                 ThymioClapEvent(QGraphicsItem *parent=0, bool advanced=false);
00288         };
00289         
00290         // Move Action
00291         class ThymioMoveAction : public ThymioButton
00292         {
00293                 Q_OBJECT
00294                                                 
00295         public:
00296                 ThymioMoveAction(QGraphicsItem *parent=0);
00297                 virtual ~ThymioMoveAction();
00298                 virtual void paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget = 0);
00299                 virtual QPixmap image(bool on=true);
00300                 virtual void setClicked(int i, int status);
00301                 virtual int isClicked(int i);
00302                 virtual int getNumButtons() { return 2; }
00303 
00304         private slots:
00305                 void valueChangeDetected();
00306                                 
00307         private:
00308                 QList<QGraphicsProxyWidget *> widgets;
00309                 QList<QSlider *> sliders;
00310                 QGraphicsItemAnimation *animation;
00311                 QTimeLine *timer;
00312         };
00313         
00314         // Color Action
00315         class ThymioColorAction : public ThymioButton
00316         {
00317                 Q_OBJECT
00318                 
00319         public:
00320                 ThymioColorAction(QGraphicsItem *parent=0);
00321                 virtual void paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget = 0);
00322                 virtual QPixmap image(bool on=true);
00323                 virtual void setClicked(int i, int status);
00324                 virtual int isClicked(int i);
00325                 virtual int getNumButtons() { return 3; }
00326                         
00327         private slots:
00328                 void valueChangeDetected();
00329         
00330         private:
00331                 QList<QGraphicsProxyWidget *> widgets;
00332                 QList<QSlider *> sliders;
00333         };
00334         
00335         // Circle Action
00336         class ThymioCircleAction : public ThymioButton
00337         {
00338         public:
00339                 ThymioCircleAction(QGraphicsItem *parent=0);
00340         };
00341 
00342         // Sound Action
00343         class ThymioSoundAction : public ThymioButton
00344         {
00345         public:
00346                 class Speaker : public QGraphicsItem
00347                 {
00348                 public:
00349                         Speaker(QGraphicsItem *parent=0) : QGraphicsItem(parent) {}
00350                         virtual void paint (QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget = 0);
00351                         QRectF boundingRect() const { return QRectF(0, 0, 256, 256); }
00352                 };
00353         
00354                 ThymioSoundAction(QGraphicsItem *parent=0);
00355                 virtual QPixmap image(bool on=true);
00356                 
00357         protected:
00358                 Speaker *speaker;
00359         };
00360 
00361         // Memory Action
00362         class ThymioMemoryAction : public ThymioButton
00363         {               
00364         public:
00365                 ThymioMemoryAction(QGraphicsItem *parent=0);
00366         };
00367         
00369 }; // Aseba
00370 
00371 #endif


aseba
Author(s): Stéphane Magnenat
autogenerated on Sun Oct 5 2014 23:46:39