Warning: That file was not part of the compilation database. It may have many parsing errors.
1 | // |
---|---|
2 | // KBlackBox |
3 | // |
4 | // A simple game inspired by an emacs module |
5 | // |
6 | /*************************************************************************** |
7 | * Copyright (c) 2007, Nicolas Roffet * |
8 | * nicolas-kde@roffet.com * |
9 | * * |
10 | * * |
11 | * This program is free software; you can redistribute it and/or modify * |
12 | * it under the terms of the GNU General Public License as published by * |
13 | * the Free Software Foundation; either version 2 of the License, or * |
14 | * (at your option) any later version. * |
15 | * * |
16 | * This program is distributed in the hope that it will be useful, * |
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
19 | * GNU General Public License for more details. * |
20 | * * |
21 | * You should have received a copy of the GNU General Public License * |
22 | * along with this program; if not, write to the * |
23 | * Free Software Foundation, Inc., * |
24 | * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA * |
25 | ***************************************************************************/ |
26 | |
27 | |
28 | #include "kbbballsgraphicwidget.h" |
29 | |
30 | #include <QGraphicsScene> |
31 | #include <QGraphicsView> |
32 | |
33 | |
34 | #include "kbbgraphicsitem.h" |
35 | #include "kbbscalablegraphicwidget.h" |
36 | #include "kbbthememanager.h" |
37 | |
38 | |
39 | |
40 | // |
41 | // Constructor / Destructor |
42 | // |
43 | |
44 | KBBBallsGraphicWidget::KBBBallsGraphicWidget(KBBThemeManager* themeManager) |
45 | { |
46 | m_themeManager = themeManager; |
47 | m_ballsToPlace = 0; |
48 | |
49 | setAlignment(Qt::AlignHCenter | Qt::AlignBottom); |
50 | |
51 | m_scene = new QGraphicsScene(this); |
52 | setScene(m_scene); |
53 | |
54 | setPlacedBalls(0); |
55 | } |
56 | |
57 | |
58 | KBBBallsGraphicWidget::~KBBBallsGraphicWidget() |
59 | { |
60 | // This removes all graphic items on the view |
61 | setPlacedBalls(m_ballsToPlace); |
62 | } |
63 | |
64 | |
65 | |
66 | // |
67 | // Public |
68 | // |
69 | |
70 | void KBBBallsGraphicWidget::resizeEvent(QResizeEvent*) |
71 | { |
72 | fitInView(m_scene->sceneRect(), Qt::KeepAspectRatio); |
73 | } |
74 | |
75 | |
76 | void KBBBallsGraphicWidget::setBallsToPlace(const int ballsToPlace) |
77 | { |
78 | if (m_ballsToPlace != ballsToPlace) { |
79 | // 1. remove all balls |
80 | m_ballsToPlace = 0; |
81 | setPlacedBalls(0); |
82 | |
83 | // 2. set new value |
84 | m_ballsToPlace = ballsToPlace; |
85 | |
86 | // 3. Set the scene size |
87 | m_scene->setSceneRect(0, 0, KBBScalableGraphicWidget::RATIO, m_ballsToPlace*KBBScalableGraphicWidget::RATIO); |
88 | resizeEvent(NULL); |
89 | } |
90 | } |
91 | |
92 | |
93 | void KBBBallsGraphicWidget::setPlacedBalls(const int placedBalls) |
94 | { |
95 | int ballsLeftToPlace = m_ballsToPlace - placedBalls; |
96 | |
97 | // remove "wrong" player balls |
98 | while (((ballsLeftToPlace>=0) && (m_wrongPlayerBalls.count()>0)) || ((ballsLeftToPlace<0) && (m_wrongPlayerBalls.count()>-ballsLeftToPlace))) { |
99 | delete m_wrongPlayerBalls.last(); |
100 | m_wrongPlayerBalls.removeLast(); |
101 | } |
102 | |
103 | // remove player balls |
104 | while (((ballsLeftToPlace>=0) && (ballsLeftToPlace<m_playerBalls.count())) || ((ballsLeftToPlace<0) && (m_playerBalls.count()>0))) { |
105 | delete m_playerBalls.last(); |
106 | m_playerBalls.removeLast(); |
107 | } |
108 | |
109 | // add balls |
110 | while (ballsLeftToPlace>m_playerBalls.count()) { |
111 | m_playerBalls.append(new KBBGraphicsItem(KBBScalableGraphicWidget::playerBall, m_scene, m_themeManager)); |
112 | m_playerBalls.last()->setPos(0, (m_ballsToPlace-m_playerBalls.count())*KBBScalableGraphicWidget::RATIO); |
113 | } |
114 | |
115 | // add "wrong" ball |
116 | while (-ballsLeftToPlace>m_wrongPlayerBalls.count()) { |
117 | m_wrongPlayerBalls.append(new KBBGraphicsItem(KBBScalableGraphicWidget::wrongPlayerBall, m_scene, m_themeManager)); |
118 | m_wrongPlayerBalls.last()->setPos(0, (m_ballsToPlace-m_wrongPlayerBalls.count())*KBBScalableGraphicWidget::RATIO); |
119 | } |
120 | |
121 | m_scene->update(); |
122 | } |
123 |
Warning: That file was not part of the compilation database. It may have many parsing errors.