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

kblackbox

kbblevelconfigurationpreview.cpp

Go to the documentation of this file.
00001 /*
00002 Copyright (c) 2006, 2007, Nicolas Roffet, <nicolas-kde@roffet.com>
00003 
00004 This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
00005 
00006 This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
00007 
00008 You should have received a copy of the GNU Library General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
00009 */
00010 
00011 #include "kbblevelconfigurationpreview.h"
00012 
00013 
00014 
00015 
00016 
00017 #include <krandomsequence.h>
00018 
00019 
00020 #include "kbbgraphicsitem.h"
00021 #include "kbbgraphicsitemblackbox.h"
00022 #include "kbbgraphicsitemset.h"
00023 #include "kbbscalablegraphicwidget.h"
00024 #include "kbbthememanager.h"
00025 
00026 
00027 
00028 KBBLevelConfigurationPreview::KBBLevelConfigurationPreview(QWidget *parent, KBBThemeManager* themeManager) : QGraphicsView(parent)
00029 {
00030     setFrameStyle(QFrame::NoFrame);
00031     setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
00032     setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
00033     setScene(new QGraphicsScene(0, 0, 2*KBBScalableGraphicWidget::BORDER_SIZE, 2*KBBScalableGraphicWidget::BORDER_SIZE, this));
00034     m_blackbox = new KBBGraphicsItemBlackBox(this, scene(), themeManager);
00035     m_themeManager = themeManager;
00036 }
00037 
00038 
00039 void KBBLevelConfigurationPreview::preview(int balls, int columns, int rows)
00040 {
00041     m_blackbox->setSize(columns, rows);
00042     scene()->setSceneRect(0, 0, columns*KBBScalableGraphicWidget::RATIO + 2*KBBScalableGraphicWidget::BORDER_SIZE, rows*KBBScalableGraphicWidget::RATIO + 2*KBBScalableGraphicWidget::BORDER_SIZE);
00043 
00044     // Show balls on the black box
00045     while (m_balls.count()>0) {
00046         delete m_balls.last();
00047         m_balls.removeLast();
00048     }
00049     QList<int> ballPos;
00050     KRandomSequence random;
00051     random.setSeed(0);
00052     int boxPos;
00053     KBBGraphicsItem* item;
00054     for (int i=0;i<balls;i++) {
00055         do {
00056             boxPos = random.getLong(columns*rows);
00057         } while (ballPos.contains(boxPos));
00058         item = new KBBGraphicsItem(KBBScalableGraphicWidget::playerBall, scene(), m_themeManager);
00059         item->setPos(KBBScalableGraphicWidget::BORDER_SIZE + KBBScalableGraphicWidget::RATIO*(boxPos % columns), KBBScalableGraphicWidget::BORDER_SIZE + KBBScalableGraphicWidget::RATIO*(boxPos / columns));
00060         ballPos.append(boxPos);
00061         m_balls.append(item);
00062     }
00063 
00064     resizeEvent(0);
00065 }
00066 
00067 
00068 void KBBLevelConfigurationPreview::drawBackground(QPainter* painter, const QRectF&)
00069 {
00070     QRectF rectBackground;
00071 
00072     // TODO: This is duplication of code from the class KBBScalableGraphicWidget. Try to fix this.
00073     const qreal sW = scene()->width();
00074     const qreal sH = scene()->height();
00075     const qreal wW = width();
00076     const qreal wH = height();
00077     const qreal offset = (sH+sW)/100 ;
00078     if (sH*wW > sW*wH) {
00079         // The widget is larger than the scene
00080         qreal w =  wW*sH/wH;
00081         rectBackground.setRect((sW-w)/2-offset, -offset, w + 2*offset, sH + 2*offset);
00082     } else {
00083         // The scene is larger than the widget (or as large)
00084         qreal h =  wH*sW/wW;
00085         rectBackground.setRect(-offset, (sH-h)/2-offset, sW + 2*offset, h + 2*offset);
00086     }
00087 
00088     m_themeManager->svgRenderer()->render(painter, m_themeManager->elementId(KBBScalableGraphicWidget::background), rectBackground);
00089 }
00090 
00091 
00092 void KBBLevelConfigurationPreview::resizeEvent( QResizeEvent* )
00093 {
00094     fitInView(0.5*KBBScalableGraphicWidget::BORDER_SIZE, 0.5*KBBScalableGraphicWidget::BORDER_SIZE, scene()->width() - 1.5*KBBScalableGraphicWidget::BORDER_SIZE, scene()->height() - 1.5*KBBScalableGraphicWidget::BORDER_SIZE, Qt::KeepAspectRatio);
00095 }
00096 
00097 
00098 #include "kbblevelconfigurationpreview.moc"

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