• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeedu API Reference
  • KDE Home
  • Contact Us
 

parley

  • sources
  • kde-4.14
  • kdeedu
  • parley
  • src
  • practice
boxeswidget.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  Copyright 2009-2010 Daniel Laidig <d.laidig@gmx.de>
3  ***************************************************************************/
4 
5 /***************************************************************************
6  * *
7  * This program is free software; you can redistribute it and/or modify *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation; either version 2 of the License, or *
10  * (at your option) any later version. *
11  * *
12  ***************************************************************************/
13 
14 #include "boxeswidget.h"
15 
16 #include "themedbackgroundrenderer.h"
17 
18 #include <keduvoctext.h>
19 #include <kdebug.h>
20 
21 #include <QtGui/QPainter>
22 #include <QBrush>
23 #include <QEvent>
24 
25 using namespace Practice;
26 
27 BoxesWidget::BoxesWidget(QWidget* parent)
28  : ImageWidget(parent), m_boxCount(1), m_currentBox(-1), m_lastBox(-1), m_renderer(0), m_fixedSize(true), m_arrowHint(0), m_spacingHint(0)
29 {
30  setBoxCount(KV_MAX_GRADE);
31  setScalingEnabled(false);
32  setAlignment(Qt::AlignLeft | Qt::AlignBottom);
33 }
34 
35 void BoxesWidget::setRenderer(ThemedBackgroundRenderer *renderer)
36 {
37  m_renderer = renderer;
38  m_rect = m_renderer->getRectForId("boxes-noscale").toRect();
39  m_fixedSize = true;
40  if (!m_rect.isValid()) {
41  m_rect = m_renderer->getRectForId("boxes").toRect();
42  m_fixedSize = false;
43  }
44  updatePixmap();
45 }
46 
47 void BoxesWidget::setBoxCount(int boxCount)
48 {
49  m_boxCount = boxCount;
50  updatePixmap();
51 }
52 
53 void BoxesWidget::setBoxes(int currentBox, int lastBox)
54 {
55  if (currentBox == m_currentBox && lastBox == m_lastBox)
56  return;
57  m_currentBox = currentBox;
58  m_lastBox = lastBox;
59  updatePixmap();
60 }
61 
62 QSize BoxesWidget::minimumSizeHint() const
63 {
64  if (m_fixedSize) {
65  return m_rect.size();
66  }
67  return ImageWidget::minimumSizeHint();
68 }
69 
70 void BoxesWidget::updatePixmap()
71 {
72  if (!m_renderer)
73  return;
74 
75  QSize imageSize = m_rect.size();
76  if (!m_fixedSize) {
77  imageSize.scale(size(), Qt::KeepAspectRatio);
78  }
79  QImage image(imageSize, QImage::Format_ARGB32_Premultiplied);
80  image.fill(QColor(Qt::transparent).rgba());
81  QPainter p(&image);
82 
83  for (int i = 0; i < m_boxCount; i++) {
84  QString id = "box-" + QString::number(i + 1);
85  if (i + 1 == m_currentBox) {
86  id += "-active";
87  }
88  drawElement(&p, id);
89  }
90  if (m_lastBox != -1 && m_currentBox != -1 && m_lastBox != m_currentBox) {
91  drawElement(&p, "arrow-" + QString::number(m_lastBox) + "-" + QString::number(m_currentBox));
92  }
93  setPixmap(QPixmap::fromImage(image));
94 }
95 
96 bool BoxesWidget::event(QEvent *e)
97 {
98  if (e->type() == QEvent::Resize)
99  updatePixmap();
100  return ImageWidget::event(e);
101 }
102 
103 void BoxesWidget::drawElement(QPainter *p, const QString& id)
104 {
105  QRect rect = m_renderer->getRectForId(id).toRect();
106  QPoint pos = rect.topLeft() - m_rect.topLeft();
107  if (!m_fixedSize) {
108  QSize scaledSize = m_rect.size();
109  scaledSize.scale(size(), Qt::KeepAspectRatio);
110  qreal scale = qreal(scaledSize.width()) / m_rect.width();
111  rect.setWidth(rect.width()*scale);
112  rect.setHeight(rect.height()*scale);
113  pos.setX(pos.x()*scale);
114  pos.setY(pos.y()*scale);
115  }
116 
117  p->drawPixmap(pos, m_renderer->getPixmapForId(id, rect.size()));
118 }
QEvent
QWidget
QRect::size
QSize size() const
QEvent::type
Type type() const
QSize::width
int width() const
QRectF::toRect
QRect toRect() const
Practice::BoxesWidget::minimumSizeHint
virtual QSize minimumSizeHint() const
Definition: boxeswidget.cpp:62
Practice::BoxesWidget::updatePixmap
void updatePixmap()
Definition: boxeswidget.cpp:70
Practice::BoxesWidget::setBoxCount
void setBoxCount(int boxCount)
Definition: boxeswidget.cpp:47
QPixmap::fromImage
QPixmap fromImage(const QImage &image, QFlags< Qt::ImageConversionFlag > flags)
QRect::height
int height() const
QSize::scale
void scale(int width, int height, Qt::AspectRatioMode mode)
QWidget::minimumSizeHint
virtual QSize minimumSizeHint() const
QPoint
Practice::ImageWidget::setAlignment
void setAlignment(Qt::Alignment alignment)
Definition: imagewidget.cpp:240
QPoint::x
int x() const
QPoint::y
int y() const
Practice::ImageWidget::setScalingEnabled
void setScalingEnabled(bool scaling, bool onlyDownscaling=true)
Definition: imagewidget.cpp:224
QWidget::size
QSize size() const
QRect
QString::number
QString number(int n, int base)
QImage::fill
void fill(uint pixelValue)
QPainter::drawPixmap
void drawPixmap(const QRectF &target, const QPixmap &pixmap, const QRectF &source)
QPainter
QRect::setWidth
void setWidth(int width)
QWidget::pos
QPoint pos() const
Practice::ImageWidget::setPixmap
void setPixmap(const QPixmap &pixmap)
Definition: imagewidget.cpp:200
QString
QColor
QWidget::rect
QRect rect() const
Practice::ThemedBackgroundRenderer::getRectForId
QRectF getRectForId(const QString &id)
Definition: themedbackgroundrenderer.cpp:161
Practice::BoxesWidget::BoxesWidget
BoxesWidget(QWidget *parent=0)
Definition: boxeswidget.cpp:27
QSize
QRect::isValid
bool isValid() const
QImage
Practice::ThemedBackgroundRenderer
Definition: themedbackgroundrenderer.h:31
Practice::BoxesWidget::event
bool event(QEvent *e)
Definition: boxeswidget.cpp:96
QRect::width
int width() const
Practice::ThemedBackgroundRenderer::getPixmapForId
QPixmap getPixmapForId(const QString &id, QSize size=QSize())
Definition: themedbackgroundrenderer.cpp:168
QRect::setHeight
void setHeight(int height)
boxeswidget.h
QRect::topLeft
QPoint topLeft() const
QPoint::setX
void setX(int x)
QPoint::setY
void setY(int y)
Practice::ImageWidget
Definition: imagewidget.h:24
Practice::BoxesWidget::setRenderer
void setRenderer(ThemedBackgroundRenderer *renderer)
Definition: boxeswidget.cpp:35
QWidget::event
virtual bool event(QEvent *event)
themedbackgroundrenderer.h
Practice::BoxesWidget::setBoxes
void setBoxes(int currentBox, int lastBox=-1)
Definition: boxeswidget.cpp:53
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:15:56 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

parley

Skip menu "parley"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • marble
  • parley
  • rocs
  •   App
  •   RocsCore
  •   VisualEditor
  •   stepcore

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