File indexing completed on 2024-04-28 04:01:51

0001 /*
0002     SPDX-FileCopyrightText: 2007-2008 Thomas Gallinari <tg8187@yahoo.fr>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "gameview.h"
0008 #include "gamescene.h"
0009 
0010 GameView::GameView(Game *p_game, const KGameTheme *theme)
0011     : QGraphicsView(new GameScene(p_game, theme))
0012 {
0013     setFrameStyle(QFrame::NoFrame);
0014     setFocusPolicy(Qt::StrongFocus);
0015     // Forward the key press events to the Game instance
0016     connect(this, &GameView::keyPressed, p_game, &Game::keyPressEvent);
0017 }
0018 
0019 GameView::~GameView() = default;
0020 
0021 void GameView::resizeEvent(QResizeEvent *)
0022 {
0023     fitInView(sceneRect(), Qt::KeepAspectRatio);
0024 }
0025 
0026 void GameView::focusOutEvent(QFocusEvent *)
0027 {
0028     // Pause the game if it is not already paused
0029     if (((GameScene *)scene())->getGame()->getTimer()->isActive()) {
0030         ((GameScene *)scene())->getGame()->switchPause();
0031     }
0032 }
0033 
0034 void GameView::keyPressEvent(QKeyEvent *p_event)
0035 {
0036     Q_EMIT keyPressed(p_event);
0037 }
0038 
0039 #include "moc_gameview.cpp"