00001 #include "gui_canvas.h"
00002
00006 void MyCanvasView::initCanvas() {
00007 this->setCanvas (canvas);
00008
00009
00010 this->distance = 0;
00011
00012
00013 this->paintBottom();
00014
00015
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
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
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
00042 this->setDimension (5, 5, 3, 3);
00043 this->moveRobot(3, 3);
00044
00045
00046 this->holder->show();
00047 this->distanceText->show();
00048 this->posText->show();
00049 this->base->show();
00050 this->frame->show();
00051
00052
00053
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
00072 pixX = stand_inner_width / (float)iCols;
00073 pixY = base_inner_height / (float)iRows;
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084 rpos.setX((int)(_col * pixX));
00085
00086 spos.setX((int)(stand_beam_width + rpos.x() + (pixX * (float)(iCols - _col))));
00087
00088
00089 if (_row <= (0.5 * (float)(iRows))) {
00090
00091
00092 spos.setY((int)(bottom_start_y - base_total_height));
00093
00094
00095 rpos.setY((int)(spos.y() + (float)(_row * pixY)));
00096
00097
00098 } else {
00099
00100
00101 rpos.setY((int)(bottom_start_y - stand_total_height));
00102
00103
00104 spos.setY((int)(rpos.y() - (float)(_row * pixY)) +1);
00105
00106
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
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
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
00122 this->posText->setText("robot state: {x,y} = {" +
00123 QString::number(_col+1) + "," + QString::number(_row+1) + "}");
00124
00125 this->canvas->update();
00126
00127
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
00163 this->distance = 0;
00164 this->distanceText->setText("distance travelled: " + QString::number(this->distance));
00165
00166
00167 this->moveRobot(_initRow, _initCol);
00168
00169
00170 this->oldPos.setX(_initCol);
00171 this->oldPos.setY(_initRow);
00172 }
00173