worldsim/src/simplecontrol.cpp

00001 /********************************************************************************
00002  *  WorldSim -- library for robot simulations                                   *
00003  *  Copyright (C) 2008-2011 Gianluca Massera <emmegian@yahoo.it>                *
00004  *                                                                              *
00005  *  This program is free software; you can redistribute it and/or modify        *
00006  *  it under the terms of the GNU General Public License as published by        *
00007  *  the Free Software Foundation; either version 2 of the License, or           *
00008  *  (at your option) any later version.                                         *
00009  *                                                                              *
00010  *  This program is distributed in the hope that it will be useful,             *
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of              *
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               *
00013  *  GNU General Public License for more details.                                *
00014  *                                                                              *
00015  *  You should have received a copy of the GNU General Public License           *
00016  *  along with this program; if not, write to the Free Software                 *
00017  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA  *
00018  ********************************************************************************/
00019 
00020 #include "simplecontrol.h"
00021 #include "world.h"
00022 #include <QLabel>
00023 #include <QBoxLayout>
00024 #include <QPushButton>
00025 #include "renderworld.h"
00026 
00027 namespace farsa {
00028 
00029 SimpleControl::SimpleControl( QWidget* centralUI, World* world, QWidget* parent )
00030     : QWidget( parent ) {
00031     cui = centralUI;
00032     cui->setParent( this );
00033     this->world = world;
00034 
00035     QBoxLayout* toplay = new QVBoxLayout( this );
00036     
00037     //--- if cui is a RenderWorld it also add infoObject label on top
00038     RenderWorld* rw = dynamic_cast<RenderWorld*>(cui);
00039     if ( rw ) {
00040         infoObject = new QLabel("Info about selected Object", this );
00041         infoObject->setFixedHeight( 50 );
00042         connect( rw, SIGNAL( drawFinished(bool) ),
00043                 this, SLOT( updateInfoObject() ), Qt::DirectConnection );
00044         toplay->addWidget( infoObject );
00045     }
00046     toplay->addWidget( cui );
00047 
00048     QBoxLayout* inlay = new QHBoxLayout(  );
00049     toplay->addLayout( inlay );
00050 
00051     QPushButton* bt = new QPushButton( "Play", this );
00052     connect( bt, SIGNAL( clicked() ),
00053              world, SLOT( play() ), Qt::DirectConnection );
00054     inlay->addWidget( bt );
00055 
00056     bt = new QPushButton( "Pause", this );
00057     connect( bt, SIGNAL( clicked() ),
00058              world, SLOT( pause() ), Qt::DirectConnection );
00059     inlay->addWidget( bt );
00060 
00061     bt = new QPushButton( "Stop", this );
00062     connect( bt, SIGNAL( clicked() ),
00063              world, SLOT( stop() ), Qt::DirectConnection );
00064     inlay->addWidget( bt );
00065 
00066     bt = new QPushButton( "Step", this );
00067     connect( bt, SIGNAL( clicked() ),
00068              world, SLOT( advance() ), Qt::DirectConnection );
00069     bt->setAutoRepeat( true );
00070     inlay->addWidget( bt );
00071 }
00072 
00073 void SimpleControl::onQuit() {
00074     delete world;
00075 }
00076 
00077 void SimpleControl::updateInfoObject() {
00078     RenderWorld* rw = (RenderWorld*)(cui);
00079     int i = rw->selectedName();
00080     if ( i != -1 ) {
00081         QString name = rw->graphics()[i]->object()->name();
00082         wVector pos = rw->graphics()[i]->object()->matrix().w_pos;
00083         infoObject->setText( QString("Name: %1 \t Pos: %2, %3, %4").arg(name).arg(pos[0]).arg(pos[1]).arg(pos[2]) );
00084     }
00085 }
00086 
00087 } // end namespace farsa