File indexing completed on 2024-04-14 03:59:18

0001 /*
0002     KBlackBox - A simple game inspired by an emacs module
0003 
0004     SPDX-FileCopyrightText: 2007 Nicolas Roffet <nicolas-kde@roffet.com>
0005 
0006     SPDX-License-Identifier: GPL-2.0-or-later
0007 */
0008 
0009 
0010 #include "kbbballsgraphicwidget.h"
0011 
0012 #include <QGraphicsScene>
0013 
0014 
0015 #include "kbbgraphicsitem.h"
0016 #include "kbbscalablegraphicwidget.h"
0017 #include "kbbthememanager.h"
0018 
0019 
0020 
0021 //
0022 // Constructor / Destructor
0023 //
0024 
0025 KBBBallsGraphicWidget::KBBBallsGraphicWidget(KBBThemeManager* themeManager)
0026 {
0027     m_themeManager = themeManager;
0028     m_ballsToPlace = 0;
0029     
0030     setAlignment(Qt::AlignHCenter | Qt::AlignBottom);
0031     
0032     m_scene = new QGraphicsScene(this);
0033     setScene(m_scene);
0034     
0035     setPlacedBalls(0);
0036 }
0037 
0038 
0039 KBBBallsGraphicWidget::~KBBBallsGraphicWidget()
0040 {
0041     // This removes all graphic items on the view
0042     setPlacedBalls(m_ballsToPlace);
0043 }
0044 
0045 
0046 
0047 //
0048 // Public
0049 //
0050 
0051 void KBBBallsGraphicWidget::resizeEvent(QResizeEvent*)
0052 {
0053     fitInView(m_scene->sceneRect(), Qt::KeepAspectRatio);
0054 }
0055 
0056 
0057 void KBBBallsGraphicWidget::setBallsToPlace(const int ballsToPlace)
0058 {
0059     if (m_ballsToPlace != ballsToPlace) {
0060         // 1. remove all balls
0061         m_ballsToPlace = 0;
0062         setPlacedBalls(0);
0063         
0064         // 2. set new value
0065         m_ballsToPlace = ballsToPlace;
0066         
0067         // 3. Set the scene size
0068         m_scene->setSceneRect(0, 0, KBBScalableGraphicWidget::RATIO, m_ballsToPlace*KBBScalableGraphicWidget::RATIO);
0069         resizeEvent(NULL);
0070     }
0071 }
0072 
0073 
0074 void KBBBallsGraphicWidget::setPlacedBalls(const int placedBalls)
0075 {
0076     int ballsLeftToPlace = m_ballsToPlace - placedBalls;
0077     
0078     // remove "wrong" player balls
0079     while (((ballsLeftToPlace>=0) && (m_wrongPlayerBalls.count()>0)) || ((ballsLeftToPlace<0) && (m_wrongPlayerBalls.count()>-ballsLeftToPlace))) {
0080         delete m_wrongPlayerBalls.last();
0081         m_wrongPlayerBalls.removeLast();
0082     }
0083     
0084     // remove player balls
0085     while (((ballsLeftToPlace>=0) && (ballsLeftToPlace<m_playerBalls.count())) || ((ballsLeftToPlace<0) && (m_playerBalls.count()>0))) {
0086         delete m_playerBalls.last();
0087         m_playerBalls.removeLast();
0088     }
0089 
0090     // add balls
0091     while (ballsLeftToPlace>m_playerBalls.count()) {
0092         m_playerBalls.append(new KBBGraphicsItem(KBBScalableGraphicWidget::playerBall, m_scene, m_themeManager));
0093         m_playerBalls.last()->setPos(0, (m_ballsToPlace-m_playerBalls.count())*KBBScalableGraphicWidget::RATIO);
0094     }
0095     
0096     // add "wrong" ball
0097     while (-ballsLeftToPlace>m_wrongPlayerBalls.count()) {
0098         m_wrongPlayerBalls.append(new KBBGraphicsItem(KBBScalableGraphicWidget::wrongPlayerBall, m_scene, m_themeManager));
0099         m_wrongPlayerBalls.last()->setPos(0, (m_ballsToPlace-m_wrongPlayerBalls.count())*KBBScalableGraphicWidget::RATIO);
0100     }
0101     
0102     m_scene->update();
0103 }