• 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
kbblevelconfigurationpreview.cpp
Go to the documentation of this file.
1 /*
2 Copyright (c) 2006, 2007, Nicolas Roffet, <nicolas-kde@roffet.com>
3 
4 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.
5 
6 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.
7 
8 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
9 */
10 
11 #include "kbblevelconfigurationpreview.h"
12 
13 
14 
15 
16 
17 #include <krandomsequence.h>
18 
19 
20 #include "kbbgraphicsitem.h"
21 #include "kbbgraphicsitemblackbox.h"
22 #include "kbbgraphicsitemset.h"
23 #include "kbbscalablegraphicwidget.h"
24 #include "kbbthememanager.h"
25 
26 
27 
28 KBBLevelConfigurationPreview::KBBLevelConfigurationPreview(QWidget *parent, KBBThemeManager* themeManager) : QGraphicsView(parent)
29 {
30  setFrameStyle(QFrame::NoFrame);
31  setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
32  setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
33  setScene(new QGraphicsScene(0, 0, 2*KBBScalableGraphicWidget::BORDER_SIZE, 2*KBBScalableGraphicWidget::BORDER_SIZE, this));
34  m_blackbox = new KBBGraphicsItemBlackBox(this, scene(), themeManager);
35  m_themeManager = themeManager;
36 }
37 
38 
39 void KBBLevelConfigurationPreview::preview(int balls, int columns, int rows)
40 {
41  m_blackbox->setSize(columns, rows);
42  scene()->setSceneRect(0, 0, columns*KBBScalableGraphicWidget::RATIO + 2*KBBScalableGraphicWidget::BORDER_SIZE, rows*KBBScalableGraphicWidget::RATIO + 2*KBBScalableGraphicWidget::BORDER_SIZE);
43 
44  // Show balls on the black box
45  while (m_balls.count()>0) {
46  delete m_balls.last();
47  m_balls.removeLast();
48  }
49  QList<int> ballPos;
50  KRandomSequence random;
51  random.setSeed(0);
52  int boxPos;
53  KBBGraphicsItem* item;
54  for (int i=0;i<balls;i++) {
55  do {
56  boxPos = random.getLong(columns*rows);
57  } while (ballPos.contains(boxPos));
58  item = new KBBGraphicsItem(KBBScalableGraphicWidget::playerBall, scene(), m_themeManager);
59  item->setPos(KBBScalableGraphicWidget::BORDER_SIZE + KBBScalableGraphicWidget::RATIO*(boxPos % columns), KBBScalableGraphicWidget::BORDER_SIZE + KBBScalableGraphicWidget::RATIO*(boxPos / columns));
60  ballPos.append(boxPos);
61  m_balls.append(item);
62  }
63 
64  resizeEvent(0);
65 }
66 
67 
68 void KBBLevelConfigurationPreview::drawBackground(QPainter* painter, const QRectF&)
69 {
70  QRectF rectBackground;
71 
72  // TODO: This is duplication of code from the class KBBScalableGraphicWidget. Try to fix this.
73  const qreal sW = scene()->width();
74  const qreal sH = scene()->height();
75  const qreal wW = width();
76  const qreal wH = height();
77  const qreal offset = (sH+sW)/100 ;
78  if (sH*wW > sW*wH) {
79  // The widget is larger than the scene
80  qreal w = wW*sH/wH;
81  rectBackground.setRect((sW-w)/2-offset, -offset, w + 2*offset, sH + 2*offset);
82  } else {
83  // The scene is larger than the widget (or as large)
84  qreal h = wH*sW/wW;
85  rectBackground.setRect(-offset, (sH-h)/2-offset, sW + 2*offset, h + 2*offset);
86  }
87 
88  m_themeManager->svgRenderer()->render(painter, m_themeManager->elementId(KBBScalableGraphicWidget::background), rectBackground);
89 }
90 
91 
92 void KBBLevelConfigurationPreview::resizeEvent( QResizeEvent* )
93 {
94  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);
95 }
96 
97 
98 #include "kbblevelconfigurationpreview.moc"
QResizeEvent
QWidget
QGraphicsScene
kbbgraphicsitem.h
KBBGraphicsItem
Graphic item of the scalable graphic widget.
Definition: kbbgraphicsitem.h:44
QSvgRenderer::render
void render(QPainter *painter)
QGraphicsScene::setSceneRect
void setSceneRect(const QRectF &rect)
KBBGraphicsItemBlackBox
The black box in the scalable graphic widget.
Definition: kbbgraphicsitemblackbox.h:52
QFrame::setFrameStyle
void setFrameStyle(int style)
QGraphicsView::scene
QGraphicsScene * scene() const
QGraphicsScene::height
qreal height() const
kbblevelconfigurationpreview.h
KBBScalableGraphicWidget::playerBall
Definition: kbbscalablegraphicwidget.h:104
QWidget::width
int width() const
QList::count
int count(const T &value) const
QList::append
void append(const T &value)
QGraphicsItem::setPos
void setPos(const QPointF &pos)
QAbstractScrollArea::setHorizontalScrollBarPolicy
void setHorizontalScrollBarPolicy(Qt::ScrollBarPolicy)
QPainter
KBBThemeManager::svgRenderer
QSvgRenderer * svgRenderer()
Get the shared SVG renderer.
Definition: kbbthememanager.cpp:185
kbbscalablegraphicwidget.h
QGraphicsView::setScene
void setScene(QGraphicsScene *scene)
KBBScalableGraphicWidget::BORDER_SIZE
static int const BORDER_SIZE
Distance between the black box and the widget border.
Definition: kbbscalablegraphicwidget.h:74
QList< int >
kbbgraphicsitemblackbox.h
KBBLevelConfigurationPreview::KBBLevelConfigurationPreview
KBBLevelConfigurationPreview(QWidget *parent, KBBThemeManager *themeManager)
Definition: kbblevelconfigurationpreview.cpp:28
QList::contains
bool contains(const T &value) const
KBBThemeManager::elementId
QString elementId(const KBBScalableGraphicWidget::itemType itemType)
Get the XML "id" value of the item.
Definition: kbbthememanager.cpp:81
QRectF
KBBScalableGraphicWidget::RATIO
static int const RATIO
Width and height of a single square on the black box.
Definition: kbbscalablegraphicwidget.h:81
KBBLevelConfigurationPreview::drawBackground
void drawBackground(QPainter *painter, const QRectF &)
Definition: kbblevelconfigurationpreview.cpp:68
QList::last
T & last()
QList::removeLast
void removeLast()
QRectF::setRect
void setRect(qreal x, qreal y, qreal width, qreal height)
KBBThemeManager
Theme manager of the scalable graphic widget.
Definition: kbbthememanager.h:51
KBBScalableGraphicWidget::background
Definition: kbbscalablegraphicwidget.h:90
QAbstractScrollArea::setVerticalScrollBarPolicy
void setVerticalScrollBarPolicy(Qt::ScrollBarPolicy)
KBBLevelConfigurationPreview::preview
void preview(int balls, int columns, int rows)
Definition: kbblevelconfigurationpreview.cpp:39
QGraphicsView
kbbgraphicsitemset.h
QWidget::height
int height() const
kbbthememanager.h
QGraphicsScene::width
qreal width() const
KBBGraphicsItemBlackBox::setSize
void setSize(const int columns, const int rows)
Define the (new) size of the black box.
Definition: kbbgraphicsitemblackbox.cpp:74
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