void PlayArea::repaintAll() { QGraphicsView* canvas = scene->views().at(0); canvas->resetTransform(); scaleX = canvas->width() / 1280.f; scaleY = canvas->height() / 720.f; qreal cscale = std::min(scaleX, scaleY); canvas->scale(cscale, cscale); qreal offsetX = (1280.f - 1280.f * cscale) / 2; qreal offsetY = (720.f - 720.f * cscale) / 2; if (offsetX > offsetY) { offsetY = 0; } else { offsetX = 0; } qDebug() << "scale" << cscale << "offsets" << offsetX << offsetY; field->setX(offsetX); field->setY(offsetY); Stack* zone; QString pstack, ostack; for (int i = 0; i < 12; i++) { pstack = PlayArea::stackNames[i]; ostack = PlayArea::stackNames[11 - i]; zone = getPlayerStack(pstack); repaint(zone); zone = getOpponentStack(ostack); repaint(zone); } }
QRectF ArrangementGraphicsItemBase::getViewportRect( ) const { QRectF clipRect; if ( this->scene == NULL || this->scene->views( ).size( ) == 0 ) { return clipRect; } QGraphicsView* view = this->scene->views( ).first( ); QPointF p1 = view->mapToScene( 0, 0 ); QPointF p2 = view->mapToScene( view->width( ), view->height( ) ); clipRect = QRectF( p1, p2 ); return clipRect; }
/*Tutorial: QGraphicsScene: world, player (Inivisble ~concept) QGraphicsItem(QGraphicsRectitem): goes into scene QGraphicsView: */ int main(int argc, char *argv[]) { QApplication a(argc, argv); //Create a scene QGraphicsScene *scene = new QGraphicsScene(); //Create an item to put into the scene MyRect *rect = new MyRect(); rect->setRect(0,0,100,100); //(x,y,width,height) //Add item to the Scene scene->addItem(rect); //Make Rect focusable rect->setFlag(QGraphicsItem::ItemIsFocusable); //Only one focused item at a time //Allowing rect to be focusable rect->setFocus(); //Setting rect item to focus //Add a view QGraphicsView *view = new QGraphicsView(scene); //Initializing in constructor //view->setScene(scene); alternative view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); view->show(); view->setFixedSize(800,600); scene->setSceneRect(0,0,800,600); rect->setPos(view->width()/2,view->height() - rect->rect().height()); //Spawn Enemies QTimer *timer = new QTimer(); QObject::connect(timer,SIGNAL(timeout()),rect,SLOT(spawn())); timer->start(2000); return a.exec(); }
QRectF GraphicsViewSegmentInputBase::viewportRect( ) const { QRectF res; if ( this->scene == NULL ) { return res; } QList< QGraphicsView* > views = this->scene->views( ); if ( views.size( ) == 0 ) { return res; } // assumes the first view is the right one QGraphicsView* viewport = views.first( ); QPointF p1 = viewport->mapToScene( 0, 0 ); QPointF p2 = viewport->mapToScene( viewport->width( ), viewport->height( ) ); res = QRectF( p1, p2 ); return res; }
int main(int argc, char *argv[]) { QApplication a(argc, argv); // Create a scene QGraphicsScene * mygamescene = new QGraphicsScene(); MyRect * player = new MyRect(); player->setRect(0,0,50,50); mygamescene->addItem(player); // Make rect respond to events player->setFlag(QGraphicsItem::ItemIsFocusable); player->setFocus(); QGraphicsView * myview = new QGraphicsView(mygamescene); myview->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); myview->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); myview->show(); myview->setFixedSize(800,600); mygamescene->setSceneRect(0,0,800,600); player->setPos((myview->width()-player->rect().width())/2,myview->height()-player->rect().height()); return a.exec(); }
int main(int argc, char *argv[]) { QApplication a(argc, argv); // create scene QGraphicsScene *scene = new QGraphicsScene(); // create Item MyRec * player = new MyRec(); //add Item scene->addItem(player); //make focusable player->setFlag(QGraphicsItem::ItemIsFocusable); player->setFocus(); //spawn enemy QTimer *timer = new QTimer; QObject::connect(timer,SIGNAL(timeout()),player,SLOT(spawn())); timer->start(2000); // vitualize QGraphicsView *view = new QGraphicsView(scene); view->setSceneRect(0,0,800,600); view->setFixedSize(800,600); //set initial position player player->setPos(view->width()/2,view->height()-player->rect().height()); view->show(); // view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); // view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); return a.exec(); }
void TimeScene::createBackground() { QBrush brush(QColor("white")); QPen pen(QColor("white")); // Background color QList<QGraphicsView*> views = this->views(); int maxWidth = 0; int maxHeight = 0; for (QList<QGraphicsView*>::iterator iter = views.begin(); iter != views.end(); iter++) { QGraphicsView* view = *iter; int viewHeight = view->height(); int viewWidth = view->width(); if (viewHeight > maxHeight) { maxHeight = viewHeight; } if (viewWidth > maxWidth) { maxWidth = viewWidth; } } if (_viewSizeHeight > maxHeight) { maxHeight = _viewSizeHeight; } if (_viewSizeWidth > maxWidth) { maxWidth = _viewSizeWidth; } _viewSizeHeight = maxHeight; _viewSizeWidth = maxWidth; this->addRect(0, 0, maxWidth, maxHeight, pen, brush); // int columnSize = geometry().width() / NU_COLS; int textSize = 30; int margin = 15; int cols = _totalDays; int columnSize = textSize + margin; if ((cols * columnSize) < maxWidth) { cols = (maxWidth / columnSize) + 10; _totalDays = cols; } DateTime startDate = this->_startDate; DateTime today; today.setHour(0); today.setMin(0); today.setSecs(0); for (int x = 0; x < (cols + 1); x++) { QColor barcolor; if ((x % 2) > 0) { barcolor = QColor("white"); } else { barcolor = QColor(240, 240, 240); } QBrush brushBar(barcolor); QPen penBar(barcolor); int left = (x*columnSize) + 0; int top = 0; int heigth = maxHeight; addRect(left, top, columnSize, heigth, penBar, brushBar); QPen pen(QColor(200, 200, 200)); pen.setStyle(Qt::DashLine); addLine(x*columnSize, 0 , x*columnSize, heigth, pen); if (startDate == today) { barcolor = QColor(230, 230, 250); addRect(left, top, columnSize, heigth, penBar, QBrush(barcolor)); } startDate = startDate.addDays(1); } }
void Guideline::rangeOfAttachedObjects(double& min, double& max) const { min = DBL_MAX; max = -DBL_MAX; for (RelsList::const_iterator curr = relationships.begin(); curr != relationships.end(); ++curr) { QRectF itemRect; if ((*curr)->shape) { itemRect = (*curr)->shape->boundingRect(); itemRect.moveCenter((*curr)->shape->pos()); } else if ((*curr)->distro) { itemRect = (*curr)->distro->boundingRect(); itemRect.moveCenter((*curr)->distro->pos()); } else if ((*curr)->separation) { itemRect = (*curr)->separation->boundingRect(); itemRect.moveCenter((*curr)->separation->pos()); } if (itemRect.isValid()) { if (get_dir() == GUIDE_TYPE_HORI) { min = qMin(min, itemRect.left()); max = qMax(max, itemRect.right()); } else { min = qMin(min, itemRect.top()); max = qMax(max, itemRect.bottom()); } } } // Cope with the case where there are no attached objects. if ((min == DBL_MAX) && (max = -DBL_MAX) && canvas()) { QList<QGraphicsView *> views = canvas()->views(); QRectF sceneBounds; // Determine bounds by taking the union of all view bounds. for (int v = 0; v < views.size(); ++v) { QGraphicsView *view = views.at(v); sceneBounds = sceneBounds.united(QRectF(view->mapToScene(0,0), view->mapToScene(view->width(), view->height()))); } if (get_dir() == GUIDE_TYPE_HORI) { min = sceneBounds.left(); max = sceneBounds.right(); } else { min = sceneBounds.top(); max = sceneBounds.bottom(); } } }