• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdegames API Reference
  • KDE Home
  • Contact Us
 

kblackbox

  • sources
  • kde-4.14
  • kdegames
  • kblackbox
kbbballsgraphicwidget.cpp
Go to the documentation of this file.
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 }
QResizeEvent
QGraphicsScene
kbbgraphicsitem.h
KBBGraphicsItem
Graphic item of the scalable graphic widget.
Definition: kbbgraphicsitem.h:44
KBBBallsGraphicWidget::KBBBallsGraphicWidget
KBBBallsGraphicWidget(KBBThemeManager *themeManager)
Constructor.
Definition: kbbballsgraphicwidget.cpp:44
QGraphicsScene::sceneRect
sceneRect
KBBScalableGraphicWidget::playerBall
Definition: kbbscalablegraphicwidget.h:104
QList::count
int count(const T &value) const
KBBBallsGraphicWidget::resizeEvent
void resizeEvent(QResizeEvent *)
Event: widget has been resized This happens for instance when the main window has been resized...
Definition: kbbballsgraphicwidget.cpp:70
QList::append
void append(const T &value)
kbbscalablegraphicwidget.h
QGraphicsView::setScene
void setScene(QGraphicsScene *scene)
kbbballsgraphicwidget.h
KBBBallsGraphicWidget::setBallsToPlace
void setBallsToPlace(const int ballsToPlace)
Define the number of balls to place on the black box.
Definition: kbbballsgraphicwidget.cpp:76
KBBScalableGraphicWidget::RATIO
static int const RATIO
Width and height of a single square on the black box.
Definition: kbbscalablegraphicwidget.h:81
QList::last
T & last()
QGraphicsScene::update
void update(qreal x, qreal y, qreal w, qreal h)
QList::removeLast
void removeLast()
KBBThemeManager
Theme manager of the scalable graphic widget.
Definition: kbbthememanager.h:51
KBBBallsGraphicWidget::setPlacedBalls
void setPlacedBalls(const int placedBalls)
Define the number of balls the player already placed on the board.
Definition: kbbballsgraphicwidget.cpp:93
KBBBallsGraphicWidget::~KBBBallsGraphicWidget
~KBBBallsGraphicWidget()
Destructor Remove all items displayed on the scene.
Definition: kbbballsgraphicwidget.cpp:58
kbbthememanager.h
KBBScalableGraphicWidget::wrongPlayerBall
Definition: kbbscalablegraphicwidget.h:106
QGraphicsView::setAlignment
void setAlignment(QFlags< Qt::AlignmentFlag > alignment)
QGraphicsView::fitInView
void fitInView(const QRectF &rect, Qt::AspectRatioMode aspectRatioMode)
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:18:20 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kblackbox

Skip menu "kblackbox"
  • Main Page
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdegames API Reference

Skip menu "kdegames API Reference"
  • granatier
  • kapman
  • kblackbox
  • kgoldrunner
  • kigo
  • kmahjongg
  • KShisen
  • ksquares
  • libkdegames
  •   highscore
  •   libkdegamesprivate
  •     kgame
  • libkmahjongg
  • palapeli
  •   libpala

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal