Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) TestBouncingBallsWidget

 

TestBouncingBallsWidget is a tool to test the BouncingBallsWidget class.

 

 

 

 

 

Downloads

 

Technical facts

 

Operating system(s) or programming environment(s)

IDE(s):

Project type:

C++ standard:

Compiler(s):

Libraries used:

 

 

 

 

 

Qt project file: ./ToolTestBouncingBallsWidget/ToolTestBouncingBallsWidgetDesktop.pro

 

include(../../DesktopApplication.pri)
include(../../Libraries/GeneralConsole.pri)
include(../../Libraries/GeneralDesktop.pri)

include(../../Tools/ToolTestBouncingBallsWidget/ToolTestBouncingBallsWidgetDesktop.pri)

SOURCES += qtmain.cpp

 

 

 

 

 

./ToolTestBouncingBallsWidget/ToolTestBouncingBallsWidgetConsole.pri

 

INCLUDEPATH +=  ../../Tools/ToolTestBouncingBallsWidget

 

 

 

 

 

./ToolTestBouncingBallsWidget/ToolTestBouncingBallsWidgetDesktop.pri

 

include(../../Tools/ToolTestBouncingBallsWidget/ToolTestBouncingBallsWidgetConsole.pri)

SOURCES += \
    ../../Tools/ToolTestBouncingBallsWidget/qtbouncingball.cpp \
    ../../Tools/ToolTestBouncingBallsWidget/qtbouncingballswidget.cpp \
    ../../Tools/ToolTestBouncingBallsWidget/qttooltestbouncingballswidgetmaindialog.cpp

HEADERS  += \
    ../../Tools/ToolTestBouncingBallsWidget/qtbouncingball.h \
    ../../Tools/ToolTestBouncingBallsWidget/qtbouncingballswidget.h \
    ../../Tools/ToolTestBouncingBallsWidget/qttooltestbouncingballswidgetmaindialog.h

FORMS  += ../../Tools/ToolTestBouncingBallsWidget/qttooltestbouncingballswidgetmaindialog.ui

 

 

 

 

 

./ToolTestBouncingBallsWidget/qtbouncingball.h

 

#ifndef QTBOUNCINGBALL_H
#define QTBOUNCINGBALL_H

#include <QGraphicsItem>

struct QGraphicsScene;

struct QtBouncingBall : public QGraphicsItem
{
  QtBouncingBall(QGraphicsItem *parent, QGraphicsScene *scene);
  QtBouncingBall(const QtBouncingBall&) = delete;
  QtBouncingBall& operator=(const QtBouncingBall&) = delete;

  ///Make the balls move
  void advance(int phase);

  ///Thanks compiler for reminding me to add this member function!
  QRectF boundingRect() const;

  ///The real collision shape
  QPainterPath shape() const;

  ///Thanks compiler for reminding me to add this member function!
  void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);


  private:
  ///Use simplified physics in this example
  ///Horizontal speed (delta x)
  double m_dx;

  ///Use simplified physics in this example
  ///Vertical speed (delta y)
  double m_dy;

  ///The scene this class is in
  QGraphicsScene * const m_scene;
};

#endif // QTBOUNCINGBALL_H

 

 

 

 

 

./ToolTestBouncingBallsWidget/qtbouncingball.cpp

 

#include <cassert>
#include <cmath>
#include <QGraphicsItem>
#include <QGraphicsScene>
#include <QList>
#include <QPainter>
#include "qtbouncingball.h"

QtBouncingBall::QtBouncingBall(QGraphicsItem *parent, QGraphicsScene *scene)
: QGraphicsItem(parent),
   m_dx(1.0), m_dy(1.0), m_scene(scene)
{
  m_scene->addItem(this);
  assert(m_scene && "An initialized QGraphicsScene must be supplied");
}

void QtBouncingBall::advance(int /* phase */)
{
  if (x() + m_dx + (boundingRect().width() * 0.5) > m_scene->width()) m_dx = -std::abs(m_dx);
  else if (x() + m_dx - (boundingRect().width() * 0.5) < 0.0) m_dx = std::abs(m_dx);
  if (y() + m_dy + (boundingRect().height() * 0.5) > m_scene->height()) m_dy = -std::abs(m_dy);
  else if (y() + m_dy - (boundingRect().width() * 0.5) < 0.0) m_dy = std::abs(m_dy);
  this->setPos(x() + m_dx, y() + m_dy);

  //Respond to collision with other item
  const QList<QGraphicsItem *> others = collidingItems();
  if (others.isEmpty()) return;
  const QGraphicsItem * const other = others[0];
  if (this->x() < other->x()) m_dx = -std::abs(m_dx);
  else if (this->x() > other->x()) m_dx =  std::abs(m_dx);
  if (this->y() < other->y()) m_dy = -std::abs(m_dy);
  else if (this->y() > other->y()) m_dy =  std::abs(m_dy);
  this->setPos(x() + m_dx, y() + m_dy);
  this->setPos(x() + m_dx, y() + m_dy);

}

QRectF QtBouncingBall::boundingRect() const
{
  return QRectF(-16.0,-16.0,32.0,32.0);
}

void QtBouncingBall::paint(
  QPainter *painter,
  const QStyleOptionGraphicsItem * /* option */,
  QWidget * /* widget */)
{
  painter->drawEllipse(this->boundingRect());
}

QPainterPath QtBouncingBall::shape() const
{
  QPainterPath p;
  p.addEllipse(boundingRect());
  return p;
}

 

 

 

 

 

./ToolTestBouncingBallsWidget/qtbouncingballswidget.h

 

#ifndef QTBOUNCINGBALLSWIDGET_H
#define QTBOUNCINGBALLSWIDGET_H

#include <QGraphicsView>

struct QGraphicsScene;

struct QtBouncingBallsWidget : public QGraphicsView
{
  QtBouncingBallsWidget(QWidget *parent = 0);
  QtBouncingBallsWidget(const QtBouncingBallsWidget&) = delete;
  QtBouncingBallsWidget& operator=(const QtBouncingBallsWidget&) = delete;


  protected:
  void resizeEvent(QResizeEvent *event);

  private:
  QGraphicsScene * const m_scene;
};

#endif // QTBOUNCINGBALLSWIDGET_H

 

 

 

 

 

./ToolTestBouncingBallsWidget/qtbouncingballswidget.cpp

 

#include <QTimer>
#include <QGraphicsScene>
#include "qtbouncingball.h"
#include "qtbouncingballswidget.h"


QtBouncingBallsWidget::QtBouncingBallsWidget(QWidget *parent)
  : QGraphicsView(parent),
    m_scene(new QGraphicsScene(this->rect(),this))
{
  this->setScene(m_scene);

  for (int i=0; i!=3; ++i)
  {
    QtBouncingBall * const ball = new QtBouncingBall(0,m_scene);
    ball->setPos(
      static_cast<double>((i - 1) * 32) + (0.5 * static_cast<double>(width())),
      static_cast<double>((i - 1) * 32) + (0.5 * static_cast<double>(height())));
  }

  {
    QTimer * const timer = new QTimer(this);
    QObject::connect(timer,SIGNAL(timeout()),m_scene,SLOT(advance()));
    timer->setInterval(20);
    timer->start();
  }

  //Turn off the scrollbars, as they look ugly
  this->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  this->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
}


void QtBouncingBallsWidget::resizeEvent(QResizeEvent *)
{
  m_scene->setSceneRect(this->rect());
}

 

 

 

 

 

./ToolTestBouncingBallsWidget/qtmain.cpp

 

#include <QApplication>
#include "qttooltestbouncingballswidgetmaindialog.h"

int main(int argc, char *argv[])
{
  QApplication a(argc, argv);
  QtToolTestBouncingBallsWidgetMainDialog w;
  w.show();
  return a.exec();
}

 

 

 

 

 

./ToolTestBouncingBallsWidget/qttooltestbouncingballswidgetmaindialog.h

 

#ifndef QTTOOLTESTBOUNCINGBALLSWIDGETMAINDIALOG_H
#define QTTOOLTESTBOUNCINGBALLSWIDGETMAINDIALOG_H

#include "qthideandshowdialog.h"

namespace Ui {
class QtToolTestBouncingBallsWidgetMainDialog;
}

class QtToolTestBouncingBallsWidgetMainDialog : public ribi::QtHideAndShowDialog
{
    Q_OBJECT
    
public:
    explicit QtToolTestBouncingBallsWidgetMainDialog(QWidget *parent = 0);
    QtToolTestBouncingBallsWidgetMainDialog(const QtToolTestBouncingBallsWidgetMainDialog&) = delete;
    QtToolTestBouncingBallsWidgetMainDialog& operator=(const QtToolTestBouncingBallsWidgetMainDialog&) = delete;
    ~QtToolTestBouncingBallsWidgetMainDialog();
    
private:
    Ui::QtToolTestBouncingBallsWidgetMainDialog *ui;
};

#endif // QTTOOLTESTBOUNCINGBALLSWIDGETMAINDIALOG_H

 

 

 

 

 

./ToolTestBouncingBallsWidget/qttooltestbouncingballswidgetmaindialog.cpp

 

#include "qttooltestbouncingballswidgetmaindialog.h"
#include "qtbouncingballswidget.h"
#include "ui_qttooltestbouncingballswidgetmaindialog.h"

QtToolTestBouncingBallsWidgetMainDialog::QtToolTestBouncingBallsWidgetMainDialog(QWidget *parent) :
    QtHideAndShowDialog(parent),
    ui(new Ui::QtToolTestBouncingBallsWidgetMainDialog)
{
  ui->setupUi(this);
  ui->layout->addWidget(new QtBouncingBallsWidget(this));
}

QtToolTestBouncingBallsWidgetMainDialog::~QtToolTestBouncingBallsWidgetMainDialog()
{
  delete ui;
}

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict

This page has been created by the tool CodeToHtml