File indexing completed on 2024-03-24 04:04:43
0001 /* 0002 SPDX-FileCopyrightText: 2006, 2007 Nicolas Roffet <nicolas-kde@roffet.com> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "kbblevelconfigurationpreview.h" 0008 0009 #include "kbbgraphicsitem.h" 0010 #include "kbbgraphicsitemblackbox.h" 0011 #include "kbbgraphicsitemset.h" 0012 #include "kbbscalablegraphicwidget.h" 0013 #include "kbbthememanager.h" 0014 // Qt 0015 #include <QRandomGenerator> 0016 0017 0018 KBBLevelConfigurationPreview::KBBLevelConfigurationPreview(QWidget *parent, KBBThemeManager* themeManager) : QGraphicsView(parent) 0019 { 0020 setFrameStyle(QFrame::NoFrame); 0021 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); 0022 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); 0023 setScene(new QGraphicsScene(0, 0, 2*KBBScalableGraphicWidget::BORDER_SIZE, 2*KBBScalableGraphicWidget::BORDER_SIZE, this)); 0024 m_blackbox = new KBBGraphicsItemBlackBox(this, scene(), themeManager, true); 0025 m_themeManager = themeManager; 0026 } 0027 0028 0029 void KBBLevelConfigurationPreview::preview(int balls, int columns, int rows) 0030 { 0031 m_blackbox->setSize(columns, rows); 0032 scene()->setSceneRect(0, 0, columns*KBBScalableGraphicWidget::RATIO + 2*KBBScalableGraphicWidget::BORDER_SIZE, rows*KBBScalableGraphicWidget::RATIO + 2*KBBScalableGraphicWidget::BORDER_SIZE); 0033 0034 // Show balls on the black box 0035 while (m_balls.count()>0) { 0036 delete m_balls.last(); 0037 m_balls.removeLast(); 0038 } 0039 QList<int> ballPos; 0040 QRandomGenerator random(QRandomGenerator::global()->generate()); 0041 int boxPos; 0042 KBBGraphicsItem* item; 0043 for (int i=0;i<balls;i++) { 0044 do { 0045 boxPos = random.bounded(columns*rows); 0046 } while (ballPos.contains(boxPos)); 0047 item = new KBBGraphicsItem(KBBScalableGraphicWidget::playerBall, scene(), m_themeManager); 0048 item->setPos(KBBScalableGraphicWidget::BORDER_SIZE + KBBScalableGraphicWidget::RATIO*(boxPos % columns), KBBScalableGraphicWidget::BORDER_SIZE + KBBScalableGraphicWidget::RATIO*(boxPos / columns)); 0049 ballPos.append(boxPos); 0050 m_balls.append(item); 0051 } 0052 0053 resizeEvent(nullptr); 0054 } 0055 0056 0057 void KBBLevelConfigurationPreview::drawBackground(QPainter* painter, const QRectF&) 0058 { 0059 QRectF rectBackground; 0060 0061 // TODO: This is duplication of code from the class KBBScalableGraphicWidget. Try to fix this. 0062 const qreal sW = scene()->width(); 0063 const qreal sH = scene()->height(); 0064 const qreal wW = width(); 0065 const qreal wH = height(); 0066 const qreal offset = (sH+sW)/100 ; 0067 if (sH*wW > sW*wH) { 0068 // The widget is larger than the scene 0069 qreal w = wW*sH/wH; 0070 rectBackground.setRect((sW-w)/2-offset, -offset, w + 2*offset, sH + 2*offset); 0071 } else { 0072 // The scene is larger than the widget (or as large) 0073 qreal h = wH*sW/wW; 0074 rectBackground.setRect(-offset, (sH-h)/2-offset, sW + 2*offset, h + 2*offset); 0075 } 0076 0077 m_themeManager->svgRenderer()->render(painter, m_themeManager->elementId(KBBScalableGraphicWidget::background), rectBackground); 0078 } 0079 0080 0081 void KBBLevelConfigurationPreview::resizeEvent( QResizeEvent* ) 0082 { 0083 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); 0084 } 0085 0086 #include "moc_kbblevelconfigurationpreview.cpp"