Warning, file /education/parley/src/practice/boxeswidget.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001 /* 0002 SPDX-FileCopyrightText: 2009-2010 Daniel Laidig <d.laidig@gmx.de> 0003 SPDX-License-Identifier: GPL-2.0-or-later 0004 */ 0005 0006 #include "boxeswidget.h" 0007 0008 #include "themedbackgroundrenderer.h" 0009 0010 #include <KEduVocText> 0011 #include <QDebug> 0012 0013 #include <QBrush> 0014 #include <QEvent> 0015 #include <QPainter> 0016 0017 using namespace Practice; 0018 0019 BoxesWidget::BoxesWidget(QWidget *parent) 0020 : ImageWidget(parent) 0021 { 0022 setBoxCount(KV_MAX_GRADE); 0023 setScalingEnabled(false); 0024 setAlignment(Qt::AlignLeft | Qt::AlignBottom); 0025 } 0026 0027 void BoxesWidget::setRenderer(ThemedBackgroundRenderer *renderer) 0028 { 0029 m_renderer = renderer; 0030 m_rect = m_renderer->getRectForId(QStringLiteral("boxes-noscale")).toRect(); 0031 m_fixedSize = true; 0032 if (!m_rect.isValid()) { 0033 m_rect = m_renderer->getRectForId(QStringLiteral("boxes")).toRect(); 0034 m_fixedSize = false; 0035 } 0036 updatePixmap(); 0037 } 0038 0039 void BoxesWidget::setBoxCount(int boxCount) 0040 { 0041 m_boxCount = boxCount; 0042 updatePixmap(); 0043 } 0044 0045 void BoxesWidget::setBoxes(int currentBox, int lastBox) 0046 { 0047 if (currentBox == m_currentBox && lastBox == m_lastBox) { 0048 return; 0049 } 0050 m_currentBox = currentBox; 0051 m_lastBox = lastBox; 0052 updatePixmap(); 0053 } 0054 0055 QSize BoxesWidget::minimumSizeHint() const 0056 { 0057 if (m_fixedSize) { 0058 return m_rect.size(); 0059 } 0060 return ImageWidget::minimumSizeHint(); 0061 } 0062 0063 void BoxesWidget::updatePixmap() 0064 { 0065 if (!m_renderer) 0066 return; 0067 0068 QSize imageSize = m_rect.size(); 0069 if (!m_fixedSize) { 0070 imageSize.scale(size(), Qt::KeepAspectRatio); 0071 } 0072 QImage image(imageSize, QImage::Format_ARGB32_Premultiplied); 0073 image.fill(QColor(Qt::transparent).rgba()); 0074 QPainter p(&image); 0075 0076 for (int i = 0; i < m_boxCount; i++) { 0077 QString id = "box-" + QString::number(i + 1); 0078 if (i + 1 == m_currentBox) { 0079 id += QLatin1String("-active"); 0080 } 0081 drawElement(&p, id); 0082 } 0083 if (m_lastBox != -1 && m_currentBox != -1 && m_lastBox != m_currentBox) { 0084 drawElement(&p, "arrow-" + QString::number(m_lastBox) + '-' + QString::number(m_currentBox)); 0085 } 0086 setPixmap(QPixmap::fromImage(image)); 0087 } 0088 0089 bool BoxesWidget::event(QEvent *e) 0090 { 0091 if (e->type() == QEvent::Resize) 0092 updatePixmap(); 0093 return ImageWidget::event(e); 0094 } 0095 0096 void BoxesWidget::drawElement(QPainter *p, const QString &id) 0097 { 0098 QRect rect = m_renderer->getRectForId(id).toRect(); 0099 QPoint pos = rect.topLeft() - m_rect.topLeft(); 0100 if (!m_fixedSize) { 0101 QSize scaledSize = m_rect.size(); 0102 scaledSize.scale(size(), Qt::KeepAspectRatio); 0103 qreal scale = qreal(scaledSize.width()) / m_rect.width(); 0104 rect.setWidth(rect.width() * scale); 0105 rect.setHeight(rect.height() * scale); 0106 pos.setX(pos.x() * scale); 0107 pos.setY(pos.y() * scale); 0108 } 0109 0110 p->drawPixmap(pos, m_renderer->getPixmapForId(id, rect.size())); 0111 }