int main(int argc, char *argv[]) { Q_INIT_RESOURCE(embeddeddialogs); QApplication app(argc, argv); QGraphicsScene scene; scene.setStickyFocus(true); const int gridSize = 10; for (int y = 0; y < gridSize; ++y) { for (int x = 0; x < gridSize; ++x) { CustomProxy *proxy = new CustomProxy(0, Qt::Window); proxy->setWidget(new EmbeddedDialog); QRectF rect = proxy->boundingRect(); proxy->setPos(x * rect.width() * 1.05, y * rect.height() * 1.05); proxy->setCacheMode(QGraphicsItem::DeviceCoordinateCache); scene.addItem(proxy); } } scene.setSceneRect(scene.itemsBoundingRect()); QGraphicsView view(&scene); view.scale(0.5, 0.5); view.setRenderHints(view.renderHints() | QPainter::Antialiasing | QPainter::SmoothPixmapTransform); view.setBackgroundBrush(QPixmap(":/No-Ones-Laughing-3.jpg")); view.setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate); view.show(); view.setWindowTitle("Embedded Dialogs Example"); return app.exec(); }
void UIDebugVDP2Viewer::on_cbScreen_currentIndexChanged ( int index ) { if (!Vdp2Regs) return; if (vdp2texture) free(vdp2texture); vdp2texture = Vdp2DebugTexture(index, &width, &height); pbSaveAsBitmap->setEnabled(vdp2texture ? true : false); // Redraw screen QGraphicsScene *scene = gvScreen->scene(); #ifdef USE_RGB_555 QImage img((uchar *)vdp2texture, width, height, QImage::Format_RGB555); #elif USE_RGB_565 QImage img((uchar *)vdp2texture, width, height, QImage::Format_RGB16); #else QImage img((uchar *)vdp2texture, width, height, QImage::Format_ARGB32); #endif QPixmap pixmap = QPixmap::fromImage(img.rgbSwapped()); scene->clear(); scene->addPixmap(pixmap); scene->setSceneRect(scene->itemsBoundingRect()); gvScreen->fitInView(scene->sceneRect()); gvScreen->invalidateScene(); }
void UIDebugVDP1::on_lwCommandList_itemSelectionChanged () { int cursel = lwCommandList->currentRow(); char tempstr[1024]; Vdp1DebugCommand(cursel, tempstr); pteCommandInfo->clear(); pteCommandInfo->appendPlainText(QtYabause::translate(tempstr)); pteCommandInfo->moveCursor(QTextCursor::Start); if (vdp1texture) free(vdp1texture); vdp1texture = Vdp1DebugTexture(cursel, &vdp1texturew, &vdp1textureh); pbSaveBitmap->setEnabled(vdp1texture ? true : false); // Redraw texture QGraphicsScene *scene = gvTexture->scene(); QImage img((uchar *)vdp1texture, vdp1texturew, vdp1textureh, QImage::Format_ARGB32); QPixmap pixmap = QPixmap::fromImage(img.rgbSwapped()); scene->clear(); scene->addPixmap(pixmap); scene->setSceneRect(scene->itemsBoundingRect()); gvTexture->fitInView(scene->sceneRect()); gvTexture->invalidateScene(); }
int main(int argc, char **argv) { QApplication a(argc, argv); QGraphicsScene scene; scene.setStickyFocus(true); for (int x = 0; x < 10; x++) { for (int y = 0; y < 4; y++) { proxy* p = new proxy(0, Qt::Window); p->setWidget(new Controller()); QRectF rect = p->boundingRect(); p->setPos(x * rect.width() * 1.05, y * rect.height() * 1.05); p->setCacheMode(QGraphicsItem::DeviceCoordinateCache); scene.addItem(p); } } scene.setSceneRect(scene.itemsBoundingRect()); QGraphicsView view(&scene); view.setRenderHints(view.renderHints()); view.setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate); view.show(); view.setWindowTitle("More controllers for God controllers!"); return a.exec(); }
void MyQGraphicsView::saveImage() { QGraphicsScene *ascene = this->scene(); ascene->clearSelection(); // Selections would also render to the file ascene->setSceneRect(ascene->itemsBoundingRect()); // Re-shrink the scene to it's bounding contents QImage image(ascene->sceneRect().size().toSize(), QImage::Format_ARGB32); // Create the image with the exact size of the shrunk scene image.fill(Qt::transparent); // Start all pixels transparent QPainter painter(&image); ascene->render(&painter); // char filename[] = "view.png"; // ifield++; // char numstr[5]; // sprintf(numstr,"%04d",hour); // for (int i=0; i<4; i++) // filename[11+i] = numstr[i]; QString fileName = QFileDialog::getSaveFileName(this, tr("Image File Name"), ".", tr("Image Files (*.png)")); if (fileName.compare("") != 0) { image.save(fileName); } }
void StateMachineViewerWidget::repopulateView() { clearView(); m_graph->applyLayout(); QGraphicsScene *scene = m_ui->graphicsView->scene(); Q_FOREACH (const GVNodePair &nodePair, m_graph->gvNodes()) { const NodeId &id = nodePair.first; const GVNode &node = nodePair.second; GVNodeItem *item = new GVNodeItem(node); item->setData(KEY_STATETYPE, QVariant::fromValue(m_nodeTypeMap.value(id, OtherState))); scene->addItem(item); m_nodeItemMap.insert(id, item); } Q_FOREACH (const GVEdgePair &edgePair, m_graph->gvEdges()) { const EdgeId &id = edgePair.first; const GVEdge &edge = edgePair.second; GVEdgeItem *item = new GVEdgeItem(edge); scene->addItem(item); m_edgeItemMap.insert(id, item); } Q_FOREACH (const GVSubGraphPair &graphPair, m_graph->gvSubGraphs()) { const GraphId &id = graphPair.first; const GVSubGraph &graph = graphPair.second; GVGraphItem *item = new GVGraphItem(graph); scene->addItem(item); m_graphItemMap.insert(id, item); } updateStateItems(); updateTransitionItems(); // correctly set the scene rect scene->setSceneRect(scene->itemsBoundingRect()); }
//! [0] PadNavigator::PadNavigator(const QSize &size, QWidget *parent) : QGraphicsView(parent) { //! [0] //! [1] // Splash item SplashItem *splash = new SplashItem; splash->setZValue(1); //! [1] //! [2] // Pad item FlippablePad *pad = new FlippablePad(size); QGraphicsRotation *flipRotation = new QGraphicsRotation(pad); QGraphicsRotation *xRotation = new QGraphicsRotation(pad); QGraphicsRotation *yRotation = new QGraphicsRotation(pad); flipRotation->setAxis(Qt::YAxis); xRotation->setAxis(Qt::YAxis); yRotation->setAxis(Qt::XAxis); pad->setTransformations(QList<QGraphicsTransform *>() << flipRotation << xRotation << yRotation); //! [2] //! [3] // Back (proxy widget) item QGraphicsProxyWidget *backItem = new QGraphicsProxyWidget(pad); QWidget *widget = new QWidget; form.setupUi(widget); form.hostName->setFocus(); backItem->setWidget(widget); backItem->setVisible(false); backItem->setFocus(); backItem->setCacheMode(QGraphicsItem::ItemCoordinateCache); const QRectF r = backItem->rect(); backItem->setTransform(QTransform() .rotate(180, Qt::YAxis) .translate(-r.width()/2, -r.height()/2)); //! [3] //! [4] // Selection item RoundRectItem *selectionItem = new RoundRectItem(QRectF(-60, -60, 120, 120), Qt::gray, pad); selectionItem->setZValue(0.5); //! [4] //! [5] // Splash animations QPropertyAnimation *smoothSplashMove = new QPropertyAnimation(splash, "y"); QPropertyAnimation *smoothSplashOpacity = new QPropertyAnimation(splash, "opacity"); smoothSplashMove->setEasingCurve(QEasingCurve::InQuad); smoothSplashMove->setDuration(250); smoothSplashOpacity->setDuration(250); //! [5] //! [6] // Selection animation QPropertyAnimation *smoothXSelection = new QPropertyAnimation(selectionItem, "x"); QPropertyAnimation *smoothYSelection = new QPropertyAnimation(selectionItem, "y"); QPropertyAnimation *smoothXRotation = new QPropertyAnimation(xRotation, "angle"); QPropertyAnimation *smoothYRotation = new QPropertyAnimation(yRotation, "angle"); smoothXSelection->setDuration(125); smoothYSelection->setDuration(125); smoothXRotation->setDuration(125); smoothYRotation->setDuration(125); smoothXSelection->setEasingCurve(QEasingCurve::InOutQuad); smoothYSelection->setEasingCurve(QEasingCurve::InOutQuad); smoothXRotation->setEasingCurve(QEasingCurve::InOutQuad); smoothYRotation->setEasingCurve(QEasingCurve::InOutQuad); //! [6] //! [7] // Flip animation setup QPropertyAnimation *smoothFlipRotation = new QPropertyAnimation(flipRotation, "angle"); QPropertyAnimation *smoothFlipScale = new QPropertyAnimation(pad, "scale"); QPropertyAnimation *smoothFlipXRotation = new QPropertyAnimation(xRotation, "angle"); QPropertyAnimation *smoothFlipYRotation = new QPropertyAnimation(yRotation, "angle"); QParallelAnimationGroup *flipAnimation = new QParallelAnimationGroup(this); smoothFlipScale->setDuration(500); smoothFlipRotation->setDuration(500); smoothFlipXRotation->setDuration(500); smoothFlipYRotation->setDuration(500); smoothFlipScale->setEasingCurve(QEasingCurve::InOutQuad); smoothFlipRotation->setEasingCurve(QEasingCurve::InOutQuad); smoothFlipXRotation->setEasingCurve(QEasingCurve::InOutQuad); smoothFlipYRotation->setEasingCurve(QEasingCurve::InOutQuad); smoothFlipScale->setKeyValueAt(0, qvariant_cast<qreal>(1.0)); smoothFlipScale->setKeyValueAt(0.5, qvariant_cast<qreal>(0.7)); smoothFlipScale->setKeyValueAt(1, qvariant_cast<qreal>(1.0)); flipAnimation->addAnimation(smoothFlipRotation); flipAnimation->addAnimation(smoothFlipScale); flipAnimation->addAnimation(smoothFlipXRotation); flipAnimation->addAnimation(smoothFlipYRotation); //! [7] //! [8] // Flip animation delayed property assignment QSequentialAnimationGroup *setVariablesSequence = new QSequentialAnimationGroup; QPropertyAnimation *setFillAnimation = new QPropertyAnimation(pad, "fill"); QPropertyAnimation *setBackItemVisibleAnimation = new QPropertyAnimation(backItem, "visible"); QPropertyAnimation *setSelectionItemVisibleAnimation = new QPropertyAnimation(selectionItem, "visible"); setFillAnimation->setDuration(0); setBackItemVisibleAnimation->setDuration(0); setSelectionItemVisibleAnimation->setDuration(0); setVariablesSequence->addPause(250); setVariablesSequence->addAnimation(setBackItemVisibleAnimation); setVariablesSequence->addAnimation(setSelectionItemVisibleAnimation); setVariablesSequence->addAnimation(setFillAnimation); flipAnimation->addAnimation(setVariablesSequence); //! [8] //! [9] // Build the state machine QStateMachine *stateMachine = new QStateMachine(this); QState *splashState = new QState(stateMachine); QState *frontState = new QState(stateMachine); QHistoryState *historyState = new QHistoryState(frontState); QState *backState = new QState(stateMachine); //! [9] //! [10] frontState->assignProperty(pad, "fill", false); frontState->assignProperty(splash, "opacity", 0.0); frontState->assignProperty(backItem, "visible", false); frontState->assignProperty(flipRotation, "angle", qvariant_cast<qreal>(0.0)); frontState->assignProperty(selectionItem, "visible", true); backState->assignProperty(pad, "fill", true); backState->assignProperty(backItem, "visible", true); backState->assignProperty(xRotation, "angle", qvariant_cast<qreal>(0.0)); backState->assignProperty(yRotation, "angle", qvariant_cast<qreal>(0.0)); backState->assignProperty(flipRotation, "angle", qvariant_cast<qreal>(180.0)); backState->assignProperty(selectionItem, "visible", false); stateMachine->addDefaultAnimation(smoothXRotation); stateMachine->addDefaultAnimation(smoothYRotation); stateMachine->addDefaultAnimation(smoothXSelection); stateMachine->addDefaultAnimation(smoothYSelection); stateMachine->setInitialState(splashState); //! [10] //! [11] // Transitions QEventTransition *anyKeyTransition = new QEventTransition(this, QEvent::KeyPress, splashState); anyKeyTransition->setTargetState(frontState); anyKeyTransition->addAnimation(smoothSplashMove); anyKeyTransition->addAnimation(smoothSplashOpacity); //! [11] //! [12] QKeyEventTransition *enterTransition = new QKeyEventTransition(this, QEvent::KeyPress, Qt::Key_Enter, backState); QKeyEventTransition *returnTransition = new QKeyEventTransition(this, QEvent::KeyPress, Qt::Key_Return, backState); QKeyEventTransition *backEnterTransition = new QKeyEventTransition(this, QEvent::KeyPress, Qt::Key_Enter, frontState); QKeyEventTransition *backReturnTransition = new QKeyEventTransition(this, QEvent::KeyPress, Qt::Key_Return, frontState); enterTransition->setTargetState(historyState); returnTransition->setTargetState(historyState); backEnterTransition->setTargetState(backState); backReturnTransition->setTargetState(backState); enterTransition->addAnimation(flipAnimation); returnTransition->addAnimation(flipAnimation); backEnterTransition->addAnimation(flipAnimation); backReturnTransition->addAnimation(flipAnimation); //! [12] //! [13] // Create substates for each icon; store in temporary grid. int columns = size.width(); int rows = size.height(); QVector< QVector< QState * > > stateGrid; stateGrid.resize(rows); for (int y = 0; y < rows; ++y) { stateGrid[y].resize(columns); for (int x = 0; x < columns; ++x) stateGrid[y][x] = new QState(frontState); } frontState->setInitialState(stateGrid[0][0]); selectionItem->setPos(pad->iconAt(0, 0)->pos()); //! [13] //! [14] // Enable key navigation using state transitions for (int y = 0; y < rows; ++y) { for (int x = 0; x < columns; ++x) { QState *state = stateGrid[y][x]; QKeyEventTransition *rightTransition = new QKeyEventTransition(this, QEvent::KeyPress, Qt::Key_Right, state); QKeyEventTransition *leftTransition = new QKeyEventTransition(this, QEvent::KeyPress, Qt::Key_Left, state); QKeyEventTransition *downTransition = new QKeyEventTransition(this, QEvent::KeyPress, Qt::Key_Down, state); QKeyEventTransition *upTransition = new QKeyEventTransition(this, QEvent::KeyPress, Qt::Key_Up, state); rightTransition->setTargetState(stateGrid[y][(x + 1) % columns]); leftTransition->setTargetState(stateGrid[y][((x - 1) + columns) % columns]); downTransition->setTargetState(stateGrid[(y + 1) % rows][x]); upTransition->setTargetState(stateGrid[((y - 1) + rows) % rows][x]); //! [14] //! [15] RoundRectItem *icon = pad->iconAt(x, y); state->assignProperty(xRotation, "angle", -icon->x() / 6.0); state->assignProperty(yRotation, "angle", icon->y() / 6.0); state->assignProperty(selectionItem, "x", icon->x()); state->assignProperty(selectionItem, "y", icon->y()); frontState->assignProperty(icon, "visible", true); backState->assignProperty(icon, "visible", false); QPropertyAnimation *setIconVisibleAnimation = new QPropertyAnimation(icon, "visible"); setIconVisibleAnimation->setDuration(0); setVariablesSequence->addAnimation(setIconVisibleAnimation); } } //! [15] //! [16] // Scene QGraphicsScene *scene = new QGraphicsScene(this); scene->setBackgroundBrush(QPixmap(":/images/blue_angle_swirl.jpg")); scene->setItemIndexMethod(QGraphicsScene::NoIndex); scene->addItem(pad); scene->setSceneRect(scene->itemsBoundingRect()); setScene(scene); //! [16] //! [17] // Adjust splash item to scene contents const QRectF sbr = splash->boundingRect(); splash->setPos(-sbr.width() / 2, scene->sceneRect().top() - 2); frontState->assignProperty(splash, "y", splash->y() - 100.0); scene->addItem(splash); //! [17] //! [18] // View setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); setMinimumSize(50, 50); setViewportUpdateMode(FullViewportUpdate); setCacheMode(CacheBackground); setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform | QPainter::TextAntialiasing); #ifndef QT_NO_OPENGL setViewport(new QOpenGLWidget); #endif stateMachine->start(); //! [18] }
//----------------------------------------------------------------------------------------- // New version, site/cell size is fixed, the blob grows //----------------------------------------------------------------------------------------- void Field::displayField(int hr, int *res) { QGraphicsScene* scene = new QGraphicsScene(QRect(0, 0, CANVAS_WIDTH, CANVAS_WIDTH)); QBrush brush; int i, xindex, yindex, ix, iy, w, rgbcol[3]; double xp, yp, d0, d, volume, scale, cmin, cmax, rmax; double a, b, Wc; int Nc; bool growthRate; // LOG_MSG("displayField"); *res = 0; hour = hr; if (slice_changed) { get_fieldinfo(&NX, &axis, &fraction, &nsites, &nconst, const_used, res); if (*res != 0) return; this->data = (FIELD_DATA *)malloc(nsites*sizeof(FIELD_DATA)); get_fielddata(&axis, &fraction, &nsites, &nconst, this->data, res); if (*res != 0) return; slice_changed = false; } if (constituent == GROWTH_RATE) growthRate = true; else growthRate = false; if (axis == X_AXIS) { // Y-Z plane xindex = 1; yindex = 2; } else if (axis == Y_AXIS) { // X-Z plane xindex = 0; yindex = 2; } else if (axis == Z_AXIS) { // X-Y plane xindex = 0; yindex = 1; } /* NX = size of lattice Nc = # of sites to fill the canvas from side to side (or top to bottom) = (2/3)NX Wc = canvas width (pixels) w = site width = Wc/Nc xp = a.ix + b yp = a.iy + b blob centre at (NX/2,NX/2) maps to canvas centre at (Wc/2,Wc/2) => Wc/2 = a.NX/2 + b The width of Nc sites maps to the canvas width => Wc = a.Nc => a = Wc/Nc, b = Wc/2 - a.NX/2 */ Nc = (2*NX)/3; Wc = CANVAS_WIDTH; w = Wc/Nc; a = w; b = Wc/2 - a*NX/2; d0 = w*dfraction; cmin = 1.0e10; cmax = 0; rmax = 0; for (i=0; i<nsites; i++) { rmax = MAX(rmax,data[i].dVdt); cmin = MIN(MAX(cmin,0),data[i].conc[constituent]); cmax = MAX(cmax,data[i].conc[constituent]); } brush.setStyle(Qt::SolidPattern); brush.setColor(QColor(0,0,0)); scene->addRect(0,0,CANVAS_WIDTH,CANVAS_WIDTH,Qt::NoPen, brush); view->setScene(scene); view->setGeometry(QRect(0, 0, 700, 700)); if (cmax == 0) { view->show(); return; } for (i=0; i<nsites; i++) { ix = this->data[i].site[xindex]; iy = this->data[i].site[yindex]; xp = int(a*ix + b - w); yp = int(a*iy + b - w); chooseFieldColor(data[i].conc[constituent],cmin,cmax,use_log,rgbcol); brush.setColor(QColor(rgbcol[0],rgbcol[1],rgbcol[2])); scene->addRect(xp,yp,w,w,Qt::NoPen, brush); } for (i=0; i<nsites; i++) { ix = this->data[i].site[xindex]; iy = this->data[i].site[yindex]; xp = int(a*ix + b - w); yp = int(a*iy + b - w); volume = this->data[i].volume; // = 0 if there is no cell if (volume > 0) { scale = pow(volume,0.3333); d = scale*d0; // fix this - need to change d0 double f; if (rmax > 0) { f = data[i].dVdt/rmax; } else { f = 0.01; } chooseRateColor(f,rgbcol); brush.setColor(QColor(rgbcol[0],rgbcol[1],rgbcol[2])); scene->addEllipse(xp+(w-d)/2,yp+(w-d)/2,d,d,Qt::NoPen, brush); } } view->show(); if (save_images) { scene->clearSelection(); // Selections would also render to the file scene->setSceneRect(scene->itemsBoundingRect()); // Re-shrink the scene to it's bounding contents QImage image(scene->sceneRect().size().toSize(), QImage::Format_ARGB32); // Create the image with the exact size of the shrunk scene image.fill(Qt::transparent); // Start all pixels transparent QPainter painter(&image); scene->render(&painter); ifield++; char filename[] = "image/field0000.png"; char numstr[5]; sprintf(numstr,"%04d",hour); for (int i=0; i<4; i++) filename[11+i] = numstr[i]; image.save(filename); } }
void ChartPage::DrawChart() { // create a scene and add it your view QGraphicsScene* scene = new QGraphicsScene(); ui->graphicsView->setScene(scene); static int height = 300; static int width = 1000; boost::filesystem::path pathDebug = GetDataDir() / "debugblc.log"; int line = CountLine(pathDebug.string().c_str()); double *rawdata = new double[line * 2]; double *drawdata = new double[line * 2 + 4]{0}; int lineDraw = 1; int max; double maxdb; GetRawMiningDataFromFile(rawdata, (char*)pathDebug.string().c_str()); GetDrawInfo(line, height, width, &lineDraw, &maxdb, &max, rawdata, drawdata); QPen pen(Qt::green, 2, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin); QPen pen2(Qt::black, 1, Qt::DashLine, Qt::RoundCap, Qt::RoundJoin); QPen pen3(Qt::red, 1, Qt::SolidLine, Qt::FlatCap, Qt::MiterJoin); //draw horizontal line for (int y = 60; y <= height + 60; y += 60) { scene->addLine(0, -y, width, -y, pen2); if(y != 0 && y != 360) { QGraphicsTextItem * item; item = ui->graphicsView->scene()->addText (QString::number(maxdb * (y / 60) / 5 / COIN)); item->setPos(0, -y - 20); } } QGraphicsTextItem * item; item = ui->graphicsView->scene()->addText ("0"); item->setPos(0, -20); scene->addLine(0, 0, width, 0, pen3); scene->addLine(0, 0, 0, -height - 60, pen3); item = ui->graphicsView->scene()->addText ("24h"); item->setPos(0, 0); //draw vertical line for (int x = 125; x < width; x+=125) { if(x != width) scene->addLine(x, 0, x, -height - 60, pen2); QGraphicsTextItem * item; item = ui->graphicsView->scene()->addText (QString::number((8 - x / 125) * 3) + "h"); item->setPos(x, 0); } //draw chart for (int x = 0; x < lineDraw - 1; x++) { int x1 = drawdata[x * 2]; int y1 = -drawdata[x * 2 + 1]; int x2 = drawdata[x * 2 + 2]; int y2 = -drawdata[x * 2 + 3]; scene->addLine(x1, y1, x2, y2, pen); } ui->graphicsView->fitInView(scene->itemsBoundingRect());//,Qt::KeepAspectRatio); delete(rawdata); delete(drawdata); }
PadNavigator::PadNavigator( QWidget *parent) : QGraphicsView(parent) { QSize size(6,2); // Pad item this->pad = new FlippablePad(size); // Selection item RoundRectItem *selectionItem = new RoundRectItem(QRectF(-110, -110, 220, 220), Qt::gray, pad); selectionItem->setZValue(0.5); // Selection animation QPropertyAnimation *smoothXSelection = new QPropertyAnimation(selectionItem, "x"); QPropertyAnimation *smoothYSelection = new QPropertyAnimation(selectionItem, "y"); smoothXSelection->setDuration(100); smoothYSelection->setDuration(100); smoothXSelection->setEasingCurve(QEasingCurve::InCurve); smoothYSelection->setEasingCurve(QEasingCurve::InCurve); // Build the state machine QStateMachine *stateMachine = new QStateMachine(this); QState *frontState = new QState(stateMachine); frontState->assignProperty(pad, "fill", false); frontState->assignProperty(selectionItem, "visible", true); stateMachine->addDefaultAnimation(smoothXSelection); stateMachine->addDefaultAnimation(smoothYSelection); stateMachine->setInitialState(frontState); // Create substates for each icon; store in temporary grid. int columns = size.width(); int rows = size.height(); QVector< QVector< QState * > > stateGrid; stateGrid.resize(rows); for (int y = 0; y < rows; ++y) { stateGrid[y].resize(columns); for (int x = 0; x < columns; ++x) stateGrid[y][x] = new QState(frontState); } frontState->setInitialState(stateGrid[0][0]); selectionItem->setPos(pad->iconAt(0, 0)->pos()); // Enable key navigation using state transitions for (int y = 0; y < rows; ++y) { for (int x = 0; x < columns; ++x) { QState *state = stateGrid[y][x]; QKeyEventTransition *rightTransition = new QKeyEventTransition(this, QEvent::KeyPress, Qt::Key_Right, state); QKeyEventTransition *leftTransition = new QKeyEventTransition(this, QEvent::KeyPress, Qt::Key_Left, state); QKeyEventTransition *downTransition = new QKeyEventTransition(this, QEvent::KeyPress, Qt::Key_Down, state); QKeyEventTransition *upTransition = new QKeyEventTransition(this, QEvent::KeyPress, Qt::Key_Up, state); rightTransition->setTargetState(stateGrid[y][(x + 1) % columns]); leftTransition->setTargetState(stateGrid[y][((x - 1) + columns) % columns]); downTransition->setTargetState(stateGrid[(y + 1) % rows][x]); upTransition->setTargetState(stateGrid[((y - 1) + rows) % rows][x]); RoundRectItem *icon = pad->iconAt(x, y); state->assignProperty(selectionItem, "x", icon->x()); state->assignProperty(selectionItem, "y", icon->y()); } } // Scene QGraphicsScene *scene = new QGraphicsScene(this); scene->setItemIndexMethod(QGraphicsScene::NoIndex); scene->addItem(pad); scene->setSceneRect(scene->itemsBoundingRect()); setScene(scene); // View setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); setMinimumSize(50, 50); setCacheMode(CacheBackground); setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform | QPainter::TextAntialiasing); stateMachine->start(); }
Creator::Creator(QApplication &app) { //播放音乐文件 media = new Phonon::MediaObject(); audioOutput = new Phonon::AudioOutput(Phonon::VideoCategory);//声音 Phonon::createPath(media, audioOutput); sourceList.append(QString("music/infinity.wma")); //建立pad pad = new RoundRectItem(QRectF(QPointF(0,0),QPointF(0,0)),QColor(0,0,255,0)); pad->setZValue(0); //设置backwindow backWindow = new QGraphicsProxyWidget(pad); backWidget = new BackWidget(); backWindow->setX(0); backWindow->setY(0); // backWindow->widget()->setWindowOpacity(1); backWindow->setWidget(backWidget); backWindow->setZValue(0); //设置开始窗口按钮 beginWindow = new QGraphicsProxyWidget(pad); beginWidget = new BeginBottum; beginWindow->setWidget(beginWidget); beginWindow->setX(backWindow->x()+400); beginWindow->setY(backWindow->y()+200); beginWindow->widget()->setWindowOpacity(1); beginWindow->setZValue(0.5); //设置音乐按键 musicWindow = new QGraphicsProxyWidget(pad); musicWidget = new ClickBottum; musicWindow->setWidget(musicWidget); musicWindow->setX(backWindow->widget()->width()-120); musicWindow->setY(backWindow->y()+20); musicWindow->widget()->setWindowOpacity(1); musicWindow->setZValue(0.9); //设置AI对战窗口 aiWindow = new QGraphicsProxyWidget(pad); aiWidget = new AIvsAI(); aiWindow->setWidget(aiWidget); aiWindow->setX(backWindow->x()); aiWindow->setY(backWindow->y()); aiWindow->widget()->setWindowOpacity(0); aiWindow->setZValue(0.5); //按钮控件 singleWindow = new QGraphicsProxyWidget(pad); singleWidget = new widgetssingle; singleWindow->setWidget(singleWidget); singleWindow->setX(backWindow->x()+400); singleWindow->setY(backWindow->y()+200); singleWindow->widget()->setWindowOpacity(0); singleWindow->setZValue(0.5); //回放器 replayerWindow = new QGraphicsProxyWidget(pad); replayerWidget = new ReplayWindow(); replayerWindow->setWidget(replayerWidget); replayerWindow->setX(backWindow->x()); replayerWindow->setY(backWindow->y()); replayerWindow->widget()->setWindowOpacity(0); replayerWindow->setZValue(0.5); //地图编辑器 mapEditWindow = new QGraphicsProxyWidget(pad); mapWideget = new MapEditor(); mapEditWindow->setWidget(mapWideget); mapEditWindow->setX(backWindow->x()); mapEditWindow->setY(backWindow->y()); mapEditWindow->widget()->setWindowOpacity(0); mapEditWindow->setZValue(0.5); //人机对战 humanaiWindow = new QGraphicsProxyWidget(pad); humanaiWidget = new humanai(); humanaiWindow->setX(backWindow->x()); humanaiWindow->setY(backWindow->y()); humanaiWindow->setWidget(humanaiWidget); humanaiWindow->widget()->setWindowOpacity(0); humanaiWindow->setZValue(0.5); //制作团队 teamMeneWindow = new QGraphicsProxyWidget(pad); teamMeneWideget = new TeamMenu(); teamMeneWindow->setWidget(teamMeneWideget); teamMeneWindow->setX(backWindow->x()); teamMeneWindow->setY(backWindow->y()); teamMeneWindow->widget()->setWindowOpacity(1); teamMeneWindow->setZValue(0.5); teamWindow = new QGraphicsProxyWidget(pad); teamWideget = new ProductionTeam(); teamWindow->setWidget(teamWideget); teamWindow->widget()->setWindowOpacity(1); teamWindow->setZValue(0.5); teamWideget->setAutoFillBackground(true); QPalette Tpalette = teamWindow->palette(); Tpalette.setBrush(QPalette::Window, QBrush(Qt::black)); teamWindow->setPalette(Tpalette); //登陆 LogInWindow = new QGraphicsProxyWidget(pad); logInwidget = new LogInWidget(); LogInWindow->setWidget(logInwidget); LogInWindow->widget()->setWindowOpacity(0); LogInWindow->setZValue(0.5); LogInWindow->setX(0); LogInWindow->setY(0); //测试赛 TestWindow = new QGraphicsProxyWidget(pad); testwidget = new TestWidget(); TestWindow->setWidget(testwidget); TestWindow->widget()->setWindowOpacity(0); TestWindow->setZValue(0.5); TestWindow->setX(0); TestWindow->setY(0); // beginWindow->close(); aiWindow->close(); singleWindow->close(); replayerWindow->close(); teamMeneWindow->close(); teamWindow->close(); mapEditWindow->close(); humanaiWindow->close(); LogInWindow->close(); TestWindow->close(); QObject::connect(beginWidget->returnUi()->exitmain,SIGNAL(clicked()),this,SLOT(close())); QObject::connect(beginWidget->returnUi()->startsingle,SIGNAL(clicked()),this,SLOT(BeginToSingle())); connect(this->singleWidget->ui->returnpre,SIGNAL(clicked()),this,SLOT(SingleToBegin())); connect(this->singleWidget->ui->aivsai,SIGNAL(clicked()),this,SLOT(SingleToAi())); connect(this->aiWidget->ui->ReturnPre,SIGNAL(clicked()),this,SLOT(AiToSingle())); connect(this->singleWidget->ui->replay,SIGNAL(clicked()),this,SLOT(SingleToReplayer())); connect(this->replayerWidget->returnUi()->pushButton,SIGNAL(clicked()),this,SLOT(ReplayerToSingle())); connect(this->beginWidget->returnUi()->team,SIGNAL(clicked()),this,SLOT(BeginToTeam())); connect(this->teamMeneWideget->returnUi()->pushButton,SIGNAL(clicked()),this,SLOT(TeamToBegin())); connect(this->musicWidget->ui->checkBox,SIGNAL(clicked()),this,SLOT(Music())); connect(this->replayerWidget->returnUi()->pushButton,SIGNAL(clicked()),this->replayerWidget,SLOT(GoInto())); connect(this->mapWideget->returnUi()->pushButton_5,SIGNAL(clicked()),this,SLOT(MapToSingle())); connect(this->singleWidget->ui->mapedit,SIGNAL(clicked()), this, SLOT(SingLeToMap())); connect(this->singleWidget->ui->playervsai,SIGNAL(clicked()),this,SLOT(SingleToHumanai())); connect(this->singleWidget->ui->playervsai, SIGNAL(clicked()), humanaiWidget, SLOT(initEmpty())); connect(this->humanaiWidget->returnUi()->Button_back, SIGNAL(clicked()), this, SLOT(HumanaiToSingle())); connect(this->media,SIGNAL(aboutToFinish()),this,SLOT(continueMusic())); connect(this->singleWidget->ui->levelmode,SIGNAL(clicked()),this,SLOT(SingleToLogIn())); connect(this->logInwidget->returnUi()->pushButton_2,SIGNAL(clicked()),this,SLOT(LogInToSingle())); connect(this->logInwidget,SIGNAL(login_success(QString)),this,SLOT(LogInToTest(QString))); connect(this->testwidget->returnUi()->pushButton,SIGNAL(clicked()),this,SLOT(TestToLogIn())); // connect(this->singleWidget->ui->replay,SIGNAL(clicked()),this->replayerWidget,SLOT(GoInto())); //设置界面背景 QGraphicsScene *scene = new QGraphicsScene(this); scene->addItem(pad); scene->setBackgroundBrush(QBrush(QColor(0,0,0,255))); scene->setSceneRect(scene->itemsBoundingRect()); setScene(scene); showFullScreen(); //建立状态 stateMachine = new QStateMachine(this); MainState = new QState(stateMachine); TeamState = new QState(stateMachine); BeginState = new QState(stateMachine); TestState = new QState(stateMachine); ReplayerState = new QState(stateMachine); WebState = new QState(stateMachine); MapState = new QState(stateMachine); HumanaiState = new QState(stateMachine); ChatState = new QState(stateMachine); WidState = new QState(stateMachine); BeginMenuState= new QState(stateMachine); SingleState= new QState(stateMachine); OldMenuState = new QState(stateMachine); CheckMenuState = new QState(stateMachine); TeamMenuState = new QState(stateMachine); LogState = new QState(stateMachine); MainState->assignProperty(MainState, "z", 0); MainState->assignProperty(beginWindow->widget(), "windowOpacity", 1); stateMachine->setInitialState(MainState); stateMachine->start(); }