• Skip to content
  • Skip to link menu
KDE 4.0 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

kblackbox

kbbballsgraphicwidget.cpp

Go to the documentation of this file.
00001 //
00002 // KBlackBox
00003 //
00004 // A simple game inspired by an emacs module
00005 //
00006 /***************************************************************************
00007  *   Copyright (c) 2007, Nicolas Roffet                                    *
00008  *   nicolas-kde@roffet.com                                                *
00009  *                                                                         *
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  *   This program is distributed in the hope that it will be useful,       *
00017  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00018  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00019  *   GNU General Public License for more details.                          *
00020  *                                                                         *
00021  *   You should have received a copy of the GNU General Public License     *
00022  *   along with this program; if not, write to the                         *
00023  *   Free Software Foundation, Inc.,                                       *
00024  *   51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA               *
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 // Constructor / Destructor
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     // This removes all graphic items on the view
00061     setPlacedBalls(m_ballsToPlace);
00062 }
00063 
00064 
00065 
00066 //
00067 // Public
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         // 1. remove all balls
00080         m_ballsToPlace = 0;
00081         setPlacedBalls(0);
00082         
00083         // 2. set new value
00084         m_ballsToPlace = ballsToPlace;
00085         
00086         // 3. Set the scene size
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     // remove "wrong" player balls
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     // remove player balls
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     // add balls
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     // add "wrong" ball
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 }

kblackbox

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

API Reference

Skip menu "API Reference"
  • kblackbox
  • kgoldrunner
  • kmahjongg
  • ksquares
  • libkdegames
  •   highscore
  •   kgame
  •   kggzgames
  •   kggzmod
  •   kggznet
  • libkmahjongg
Generated for API Reference by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal