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" |
99 | |