• 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
kbbgraphicsitemballrepository.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 #include "kbbgraphicsitemballrepository.h"
28 
29 
30 
31 #include <QGraphicsScene>
32 
33 
34 #include "kbbgraphicsitemonbox.h"
35 #include "kbbgraphicsitemset.h"
36 #include "kbbscalablegraphicwidget.h"
37 
38 
39 
40 //
41 // Constructor / Destructor
42 //
43 
44 KBBGraphicsItemBallRepository::KBBGraphicsItemBallRepository(KBBScalableGraphicWidget* parent, KBBThemeManager* themeManager) : KBBGraphicsItem(KBBScalableGraphicWidget::informationBackground, parent->scene(), themeManager)
45 {
46  m_parent = parent;
47  m_themeManager = themeManager;
48  m_width = 1;
49  m_height = 1;
50  m_ballToPlace = 0;
51 
52  m_random.setSeed(0);
53 
54  m_ballsOutside = new KBBGraphicsItemSet(parent->scene());
55 }
56 
57 
58 KBBGraphicsItemBallRepository::~KBBGraphicsItemBallRepository()
59 {
60  delete m_ballsOutside;
61 }
62 
63 
64 
65 //
66 // Public
67 //
68 
69 int KBBGraphicsItemBallRepository::ballToTake() const
70 {
71  return m_ballsOutside->anyItemPosition();
72 }
73 
74 
75 void KBBGraphicsItemBallRepository::fillBallsOutside(int placed)
76 {
77  int i = m_columns*m_rows;
78  while ((m_ballsOutside->count()+placed)<m_ballToPlace) {
79  if (!(m_ballsOutside->containsVisible(i))) {
80  KBBGraphicsItemOnBox* b = new KBBGraphicsItemOnBox(KBBScalableGraphicWidget::playerBall, m_parent, m_themeManager, i, m_columns, m_rows);
81  m_ballsOutside->insert(b);
82  b->setPos(x() + ((i - m_columns*m_rows) / m_height)*KBBScalableGraphicWidget::RATIO, y() + ((i - m_columns*m_rows) % m_height)*KBBScalableGraphicWidget::RATIO);
83  }
84  i++;
85  }
86 }
87 
88 
89 void KBBGraphicsItemBallRepository::newGame(int columns, int rows, int balls)
90 {
91  m_columns = columns;
92  m_rows = rows;
93 
94  m_ballsOutside->clear();
95 
96  m_ballToPlace = balls;
97  scale(1./m_width, 1./m_height);
98 
99 
100  m_height = (rows/2);
101  if (rows % 2 > 0)
102  m_height++;
103  m_width = balls/m_height;
104  if (balls % m_height > 0)
105  m_width++;
106 
107  scale(m_width/1., m_height/1.);
108 
109  setPos((-(m_width+1)*KBBScalableGraphicWidget::RATIO), KBBScalableGraphicWidget::RATIO);
110 
111  fillBallsOutside(0);
112 }
113 
114 
115 void KBBGraphicsItemBallRepository::removeBall(int outsidePosition)
116 {
117  m_ballsOutside->remove(outsidePosition);
118 }
119 
120 #include "kbbgraphicsitemballrepository.moc"
QGraphicsItem::x
qreal x() const
QGraphicsItem::y
qreal y() const
KBBGraphicsItemOnBox
Item on the box on the scalable graphic widget.
Definition: kbbgraphicsitemonbox.h:50
KBBGraphicsItemSet::count
int count() const
Number of items.
Definition: kbbgraphicsitemset.cpp:72
KBBGraphicsItemSet::containsVisible
bool containsVisible(int position)
If an element is not visible, it is not contained.
Definition: kbbgraphicsitemset.cpp:92
KBBGraphicsItem
Graphic item of the scalable graphic widget.
Definition: kbbgraphicsitem.h:44
KBBGraphicsItemSet::remove
void remove(int position)
Remove item at given position.
Definition: kbbgraphicsitemset.cpp:127
KBBScalableGraphicWidget::scene
QGraphicsScene * scene()
Definition: kbbscalablegraphicwidget.cpp:379
KBBGraphicsItemSet
Set of graphic items with positions.
Definition: kbbgraphicsitemset.h:46
kbbgraphicsitemonbox.h
KBBGraphicsItemBallRepository::ballToTake
int ballToTake() const
Definition: kbbgraphicsitemballrepository.cpp:69
KBBScalableGraphicWidget
Scalable graphic central widget for KBlackBox.
Definition: kbbscalablegraphicwidget.h:62
KBBGraphicsItemSet::anyItemPosition
int anyItemPosition()
A position of an item (anyone of them)
Definition: kbbgraphicsitemset.cpp:63
KBBScalableGraphicWidget::playerBall
Definition: kbbscalablegraphicwidget.h:104
kbbgraphicsitemballrepository.h
KBBGraphicsItemSet::clear
void clear()
Remove all items.
Definition: kbbgraphicsitemset.cpp:78
KBBGraphicsItemBallRepository::fillBallsOutside
void fillBallsOutside(int placed)
Definition: kbbgraphicsitemballrepository.cpp:75
KBBGraphicsItemSet::insert
void insert(KBBItemWithPosition *item)
Insert an item in the list.
Definition: kbbgraphicsitemset.cpp:106
QGraphicsItem::setPos
void setPos(const QPointF &pos)
kbbscalablegraphicwidget.h
KBBGraphicsItemBallRepository::~KBBGraphicsItemBallRepository
~KBBGraphicsItemBallRepository()
Definition: kbbgraphicsitemballrepository.cpp:58
KBBScalableGraphicWidget::RATIO
static int const RATIO
Width and height of a single square on the black box.
Definition: kbbscalablegraphicwidget.h:81
KBBGraphicsItemBallRepository::newGame
void newGame(int columns, int rows, int balls)
Definition: kbbgraphicsitemballrepository.cpp:89
KBBGraphicsItemBallRepository::removeBall
void removeBall(int outsidePosition)
Definition: kbbgraphicsitemballrepository.cpp:115
KBBThemeManager
Theme manager of the scalable graphic widget.
Definition: kbbthememanager.h:51
KBBGraphicsItemBallRepository::KBBGraphicsItemBallRepository
KBBGraphicsItemBallRepository(KBBScalableGraphicWidget *parent, KBBThemeManager *themeManager)
Definition: kbbgraphicsitemballrepository.cpp:44
QObject::parent
QObject * parent() const
kbbgraphicsitemset.h
QGraphicsItem::scale
qreal scale() const
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