gui_canvas.cpp

gehe zur Dokumentation dieser Datei
00001 #include "gui_canvas.h"
00002 
00006 void MyCanvasView::initCanvas() {
00007    this->setCanvas (canvas);
00008 
00009    // set covered distance to 0
00010    this->distance = 0;
00011 
00012    // draw Bottom first
00013    this->paintBottom();
00014 
00015    // load Pictures
00016    this->frame_pix = new QCanvasPixmapArray (FRAMEPIX, 0);
00017    if(!this->frame_pix->isValid()){
00018       cout << "Could not load pix: " << FRAMEPIX << endl;
00019    }
00020    this->base_pix = new QCanvasPixmapArray (BASEPIX, 0);
00021    if(!this->base_pix->isValid()){
00022       cout << "Could not load pix: " << BASEPIX << endl;
00023    }
00024    this->holder_pix = new QCanvasPixmapArray (HOLDERPIX, 0);
00025    if(!this->holder_pix->isValid()){
00026       cout << "Could not load pix: " << HOLDERPIX << endl;
00027    }
00028 
00029    // create CanvasSprite
00030    this->frame = new QCanvasSprite (this->frame_pix, canvas);
00031    this->base = new QCanvasSprite (this->base_pix, canvas);
00032    this->holder = new QCanvasSprite (this->holder_pix, canvas);
00033 
00034    // create Text-LAbel
00035    this->distanceText = new QCanvasText ("distance travelled: " + QString::number(this->distance), canvas);
00036    this->distanceText->move(10, bottom_start_y + 10);
00037    this->posText = new QCanvasText ("robot state: {x,y} = {2,2}", canvas);
00038    this->posText->move(10, bottom_start_y + 30);
00039 
00040 
00041    // at least set to position 0,0
00042    this->setDimension (5, 5, 3, 3);
00043    this->moveRobot(3, 3);
00044 
00045    // add remaining elements
00046    this->holder->show();
00047    this->distanceText->show();
00048    this->posText->show();
00049    this->base->show();
00050    this->frame->show();
00051    
00052    
00053    // draw Canvas
00054    this->canvas->update();
00055 
00056 }
00057 
00063 void MyCanvasView::moveRobot(int _row, int _col) {
00064    
00065    QPoint rpos, spos, hpos;
00066    float pixX, pixY;
00067 
00068    _row--;
00069    _col--;
00070 
00071    // Calculate pixel for one Step on the x and y axis
00072    pixX = stand_inner_width / (float)iCols;
00073    pixY = base_inner_height / (float)iRows;
00074    
00075    /**********************************************
00076    // set frame and base position:
00077    // =====================
00078    //   - if _row < 1/2*iRows, 
00079         //     frame move up 
00080         //   - if _row >= 1/2*iRows, then
00081    //     base move up
00082    **********************************************/
00083    // frame X-Position
00084    rpos.setX((int)(_col * pixX));
00085    // base X-Position
00086    spos.setX((int)(stand_beam_width + rpos.x() + (pixX * (float)(iCols - _col))));
00087    
00088    // frame up and base on the bottom
00089    if (_row <= (0.5 * (float)(iRows))) {
00090 
00091       // base Y-Position
00092       spos.setY((int)(bottom_start_y - base_total_height));
00093 
00094       // frame Y-Position     
00095       rpos.setY((int)(spos.y() + (float)(_row * pixY)));
00096    
00097    // base up and frame on the bottom
00098    } else {
00099 
00100       // frame Y-Position
00101       rpos.setY((int)(bottom_start_y - stand_total_height));
00102       
00103       // base Y-Position
00104       spos.setY((int)(rpos.y() - (float)(_row * pixY)) +1); 
00105 
00106       // the base can get a new x-position if its up. Therefore the textlabel and the distance must be updated
00107       if (_col != this->oldPos.x()) {
00108          distance += this->oldPos.x() - _col;
00109          this->distanceText->setText("distance travelled: " + QString::number(this->distance));
00110       }
00111    }
00112 
00113    // Position of the red holder
00114    hpos.setX((int)(  spos.x() + (base_total_width/2) - (holder_total_width/2) )); 
00115    hpos.setY((int)(  rpos.y() - (holder_total_height/2) + (stand_beam_height/2) )); 
00116 
00117    // calculate new stand-Position
00118    this->frame->move(rpos.x(), rpos.y());
00119    this->base->move(spos.x(), spos.y());
00120    this->holder->move(hpos.x(), hpos.y());
00121    // update Position-Label 
00122    this->posText->setText("robot state: {x,y} = {" +
00123             QString::number(_col+1) + "," + QString::number(_row+1) + "}");
00124 
00125    this->canvas->update();
00126 
00127    // save position
00128    this->oldPos.setX(_col);
00129    this->oldPos.setY(_row);
00130 }
00131 
00132 
00136 void MyCanvasView::paintBottom() {
00137 
00138    this->bottom = new QCanvasRectangle (0, bottom_start_y, canvas_width, canvas_height, this->canvas);
00139    this->bottom->setBrush( QColor(200,200,200) );
00140    this->bottom->show();
00141 }
00142 
00143 
00152 void MyCanvasView::setDimension(int _rows, int _cols, int _initRow, int _initCol) {
00153 
00154    _rows--;
00155    _cols--;
00156    _initRow--;
00157    _initCol--;
00158 
00159    this->iRows = _rows;
00160    this->iCols = _cols;
00161 
00162    // Weg mit 0 initialisieren
00163    this->distance = 0;
00164    this->distanceText->setText("distance travelled: " + QString::number(this->distance));
00165 
00166    // Initialposition einnehmen
00167    this->moveRobot(_initRow, _initCol);
00168 
00169    // Diese initial-Position als aktuelle Position speichern
00170    this->oldPos.setX(_initCol);
00171    this->oldPos.setY(_initRow);
00172 }
00173 

Erzeugt am Mon Nov 24 15:30:59 2008 für Walking Robot Simulation GUI - API Documentation von  doxygen 1.5.5