kblackbox
kbbballsgraphicwidget.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include "kbbballsgraphicwidget.h"
00029
00030 #include <QGraphicsScene>
00031 #include <QGraphicsView>
00032
00033
00034 #include "kbbgraphicsitem.h"
00035 #include "kbbscalablegraphicwidget.h"
00036 #include "kbbthememanager.h"
00037
00038
00039
00040
00041
00042
00043
00044 KBBBallsGraphicWidget::KBBBallsGraphicWidget(KBBThemeManager* themeManager)
00045 {
00046 m_themeManager = themeManager;
00047 m_ballsToPlace = 0;
00048
00049 setAlignment(Qt::AlignHCenter | Qt::AlignBottom);
00050
00051 m_scene = new QGraphicsScene(this);
00052 setScene(m_scene);
00053
00054 setPlacedBalls(0);
00055 }
00056
00057
00058 KBBBallsGraphicWidget::~KBBBallsGraphicWidget()
00059 {
00060
00061 setPlacedBalls(m_ballsToPlace);
00062 }
00063
00064
00065
00066
00067
00068
00069
00070 void KBBBallsGraphicWidget::resizeEvent(QResizeEvent*)
00071 {
00072 fitInView(m_scene->sceneRect(), Qt::KeepAspectRatio);
00073 }
00074
00075
00076 void KBBBallsGraphicWidget::setBallsToPlace(const int ballsToPlace)
00077 {
00078 if (m_ballsToPlace != ballsToPlace) {
00079
00080 m_ballsToPlace = 0;
00081 setPlacedBalls(0);
00082
00083
00084 m_ballsToPlace = ballsToPlace;
00085
00086
00087 m_scene->setSceneRect(0, 0, KBBScalableGraphicWidget::RATIO, m_ballsToPlace*KBBScalableGraphicWidget::RATIO);
00088 resizeEvent(NULL);
00089 }
00090 }
00091
00092
00093 void KBBBallsGraphicWidget::setPlacedBalls(const int placedBalls)
00094 {
00095 int ballsLeftToPlace = m_ballsToPlace - placedBalls;
00096
00097
00098 while (((ballsLeftToPlace>=0) && (m_wrongPlayerBalls.count()>0)) || ((ballsLeftToPlace<0) && (m_wrongPlayerBalls.count()>-ballsLeftToPlace))) {
00099 delete m_wrongPlayerBalls.last();
00100 m_wrongPlayerBalls.removeLast();
00101 }
00102
00103
00104 while (((ballsLeftToPlace>=0) && (ballsLeftToPlace<m_playerBalls.count())) || ((ballsLeftToPlace<0) && (m_playerBalls.count()>0))) {
00105 delete m_playerBalls.last();
00106 m_playerBalls.removeLast();
00107 }
00108
00109
00110 while (ballsLeftToPlace>m_playerBalls.count()) {
00111 m_playerBalls.append(new KBBGraphicsItem(KBBScalableGraphicWidget::playerBall, m_scene, m_themeManager));
00112 m_playerBalls.last()->setPos(0, (m_ballsToPlace-m_playerBalls.count())*KBBScalableGraphicWidget::RATIO);
00113 }
00114
00115
00116 while (-ballsLeftToPlace>m_wrongPlayerBalls.count()) {
00117 m_wrongPlayerBalls.append(new KBBGraphicsItem(KBBScalableGraphicWidget::wrongPlayerBall, m_scene, m_themeManager));
00118 m_wrongPlayerBalls.last()->setPos(0, (m_ballsToPlace-m_wrongPlayerBalls.count())*KBBScalableGraphicWidget::RATIO);
00119 }
00120
00121 m_scene->update();
00122 }