|
| 1 | +/* |
| 2 | + Copyright (C) 1997-2026 Sam Lantinga <slouken@libsdl.org> |
| 3 | +
|
| 4 | + This software is provided 'as-is', without any express or implied |
| 5 | + warranty. In no event will the authors be held liable for any damages |
| 6 | + arising from the use of this software. |
| 7 | +
|
| 8 | + Permission is granted to anyone to use this software for any purpose, |
| 9 | + including commercial applications, and to alter it and redistribute it |
| 10 | + freely. |
| 11 | +*/ |
| 12 | +#include <QtWidgets> |
| 13 | + |
| 14 | +#include "mainwindow.h" |
| 15 | +#include "QSDLCanvas.h" |
| 16 | + |
| 17 | +MainWindow::MainWindow() |
| 18 | +{ |
| 19 | + QWidget *widget = new QWidget; |
| 20 | + setCentralWidget(widget); |
| 21 | + |
| 22 | + m_canvas = new QSDLCanvas(this); |
| 23 | + |
| 24 | + QVBoxLayout *layout = new QVBoxLayout; |
| 25 | + layout->setContentsMargins(0, 4, 0, 0); |
| 26 | + layout->addWidget(m_canvas); |
| 27 | + widget->setLayout(layout); |
| 28 | + |
| 29 | + createMenus(); |
| 30 | + |
| 31 | + setWindowTitle(tr("SDL Qt Demo")); |
| 32 | + setMinimumSize(160, 160); |
| 33 | + resize(640, 480); |
| 34 | +} |
| 35 | + |
| 36 | +void MainWindow::createMenus() |
| 37 | +{ |
| 38 | + // Create "File" menu |
| 39 | + QMenu *fileMenu = menuBar()->addMenu(tr("&File")); |
| 40 | +#if QT_VERSION >= QT_VERSION_CHECK(6, 7, 0) |
| 41 | + QAction *exitAct = new QAction(QIcon::fromTheme(QIcon::ThemeIcon::ApplicationExit), tr("E&xit"), this); |
| 42 | +#else |
| 43 | + QAction *exitAct = new QAction(tr("E&xit"), this); |
| 44 | +#endif |
| 45 | + exitAct->setShortcuts(QKeySequence::Quit); |
| 46 | + connect(exitAct, &QAction::triggered, this, &QWidget::close); |
| 47 | + fileMenu->addAction(exitAct); |
| 48 | + |
| 49 | + // Create Renderer menu |
| 50 | + QMenu *rendererMenu = menuBar()->addMenu(tr("&Renderer")); |
| 51 | + QActionGroup* renderGroup = new QActionGroup(this); |
| 52 | + renderGroup->setExclusive(true); |
| 53 | + { |
| 54 | + QAction *defaultDriverAction = new QAction(tr("(default)"), this); |
| 55 | + defaultDriverAction->setCheckable(true); |
| 56 | + defaultDriverAction->setChecked(true); |
| 57 | + renderGroup->addAction(defaultDriverAction); |
| 58 | + rendererMenu->addAction(defaultDriverAction); |
| 59 | + connect(defaultDriverAction, &QAction::triggered, [this] { m_canvas->changeRenderer(""); }); |
| 60 | + } |
| 61 | + int count_render_drivers = SDL_GetNumRenderDrivers(); |
| 62 | + for (int i = 0; i < count_render_drivers; i++) { |
| 63 | + QString driver_name = SDL_GetRenderDriver(i); |
| 64 | + QAction *driverAction = new QAction(driver_name, this); |
| 65 | + driverAction->setCheckable(true); |
| 66 | + driverAction->setChecked(false); |
| 67 | + renderGroup->addAction(driverAction); |
| 68 | + rendererMenu->addAction(driverAction); |
| 69 | + connect(driverAction, &QAction::triggered, [this, driver_name] { m_canvas->changeRenderer(driver_name); }); |
| 70 | + } |
| 71 | + |
| 72 | + // Create Help menu |
| 73 | + QMenu *helpMenu = menuBar()->addMenu(tr("&Help")); |
| 74 | + QAction *aboutQtAction = new QAction(tr("About &Qt"), this); |
| 75 | + connect(aboutQtAction, &QAction::triggered, qApp, &QApplication::aboutQt); |
| 76 | + helpMenu->addAction(aboutQtAction); |
| 77 | +} |
0 commit comments