void FenPrincipale::LoadImageW() { delete ImageWidget; ImageWidget = new QWidget; dockImage->setWidget(ImageWidget); QString fileName = QFileDialog::getOpenFileName(this, tr("Load Image"),QString(), "Images (*.png *.gif *.jpeg *.jpg)"); if (fileName.isEmpty()) return; QFile file(fileName); if (!file.open(QFile::ReadOnly)) { QString msg = tr("Failed to open %1\n%2") .arg(fileName) .arg(file.errorString()); QMessageBox::warning(this, tr("Error"), msg); return; } scene = new MyScene(fileName); QGraphicsView *view = new QGraphicsView(scene); view->setMaximumSize(scene->size+QSize(10,10)); //view->show(); //image->setPixmap(QPixmap(fileName)); QVBoxLayout *dockLayout = new QVBoxLayout; dockLayout->addWidget(view); ImageWidget->setLayout(dockLayout); dockImage->show(); }
Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) { ui->setupUi(this); this->setWindowState(this->windowState() ^ Qt::WindowFullScreen); QGraphicsView *view = ui->graphicsView; view->setScene(&scene); view->setWindowFlags (Qt::FramelessWindowHint); view->setAlignment(Qt::AlignLeft | Qt::AlignTop); view->setRenderHint(QPainter::Antialiasing); view->setMaximumSize(size()); view->setVerticalScrollBarPolicy( Qt::ScrollBarAlwaysOff ); view->setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff ); view->setCacheMode(QGraphicsView::CacheBackground); QGMenu *menu = new QGMenu(view->rect()); //menu->setOpacity(0); scene.addItem(menu); int size = 152 + 16; for (int i = 0; i < 20 ;i++) { int r = i / 5; int c = i % 5; QGTile *tile = new QGTile; scene.addItem(tile); tile->moveBy(c * size, r * size); tile->installSceneEventFilter(menu); } menu->setZValue(100); // QGraphicsItem *ball = new QGraphicsEllipseItem(0, 0, 20, 20); // QTimeLine *timer = new QTimeLine(5000); // timer->setFrameRange(0, 100); // QGraphicsItemAnimation *animation = new QGraphicsItemAnimation; // animation->setItem(ball); // animation->setTimeLine(timer); // for (int i = 0; i < 200; ++i) // animation->setPosAt(i / 200.0, QPointF(i, i)); // scene.addItem(ball); // timer->start(); }
void MainWindow::updateGraphics() { // restart scene QGraphicsScene *previousScene = scene; scene = new QGraphicsScene(0, 0, levelPlist.value("level_width").toInt(), levelPlist.value("level_height").toInt()); // Add player object to scene QRect playerRect = spriteSheetLocations.value("Volt1.png"); Q_ASSERT_X(playerRect != QRect(0,0,0,0), "MainWindow::loadFile()", "Could not find sprite location!"); QImage player = spriteSheet.copy(playerRect); QGraphicsPixmapItem *item = scene->addPixmap(QPixmap::fromImage(player)); item->setTransformOriginPoint(item->boundingRect().width()/2, item->boundingRect().height()/2); float scale = levelPlist.value("starting_size").toFloat() / playerRect.width(); item->scale(scale, scale); int startX = levelPlist.value("start_x").toInt() - item->boundingRect().width()*scale/2; int startY = levelPlist.value("level_height").toInt() - (levelPlist.value("start_y").toInt() + item->boundingRect().height()*scale/2); item->setPos(startX, startY); item->setData(1, "player"); // type of object item->setData(2, -1); // object id item->setData(3, false); // is the object rotated? item->setData(4, QPoint(0,0)); // MouseOffset of the object (not used yet) // Add objects to scene for(int i = 0; i < levelObjects.length(); i++) { QRect objRect; objRect = spriteSheetLocations.value(levelObjects.at(i).value("frame_name").toString()); // Make sure we are good to go Q_ASSERT_X(objRect != QRect(0,0,0,0), "MainWindow::loadFile()", "Could not find sprite location!"); QImage img = spriteSheet.copy(objRect); float height = levelObjects.at(i).value("height").toFloat(); float width = levelObjects.at(i).value("width").toFloat(); float x = levelObjects.at(i).value("x").toFloat(); float y = levelObjects.at(i).value("y").toFloat(); img = img.scaled(QSize(width, height), Qt::IgnoreAspectRatio); item = scene->addPixmap(QPixmap::fromImage(img)); float xPos = x - width/2; float yPos = levelPlist.value("level_height").toFloat() - (y + height/2); // Rotate object around its own center as opposed to its top left corner float rotation = levelObjects[i].value("rotation").toFloat(); //float rotationRadians = (3.141592653/180) * rotation; //float xShift = (width/2 * qCos(rotationRadians)) - (height/2 * qSin(rotationRadians)) - width/2; //float yShift = (-width/2 * qSin(rotationRadians)) - (height/2 * qCos(rotationRadians)) + height/2; //item->setPos((int)(xPos - xShift), (int)(yPos + yShift)); item->setPos((int)xPos, (int)yPos); item->setTransformOriginPoint(width/2, height/2); item->setRotation((int)rotation); item->setData(1, "object"); item->setData(2, i); item->setData(3, (rotation != 0 && rotation != 360)); //item->setData(4, QPoint(xShift, yShift)); } // Display a yellow box around whichever objects are selected updateSelectedObjects(scene, false); // Display grid float levelHeight = levelPlist.value("level_height").toFloat(); float levelWidth = levelPlist.value("level_width").toFloat(); for(int i = 0; i < levelHeight; i += GRID_RESOLUTION) { scene->addLine(0, i, levelWidth, i, QPen(QColor(255, 255, 255, 50))); } for(int j = 0; j < levelWidth; j += GRID_RESOLUTION) { scene->addLine(j, 0, j, levelHeight, QPen(QColor(255, 255, 255, 50))); } // Display border of level QColor borderColor = QColor(50, 50, 255, 150); scene->addLine(0, 0, 0, levelHeight, QPen(borderColor)); scene->addLine(levelWidth, levelHeight, 0, levelHeight, QPen(borderColor)); scene->addLine(levelWidth, 0, levelWidth, levelHeight, QPen(borderColor)); scene->addLine(0, 0, levelWidth, 0, QPen(borderColor)); // Setup graphics view with the newly created scene QGraphicsView *view = ui->graphicsView; view->setScene(scene); view->setMaximumSize(levelPlist.value("level_width").toInt() + 2, levelPlist.value("level_height").toInt() + 3); // If we just loaded the level, zoom out all the way. if(justLoaded) { justLoaded = false; view->fitInView(0, 0, levelPlist.value("level_width").toInt(), levelPlist.value("level_height").toInt(), Qt::KeepAspectRatio); } // set background scene->setBackgroundBrush(Qt::black); // avoid memory leak. We can't do this earlier, or the QGraphicsView resets its viewport. delete previousScene; }