Go back to Richel Bilderbeek's homepage.

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

 

 

 

 

 

(C++) TestBouncingRectsWidget

 

TestBouncingRectsWidget is a tool to test the BouncingRectsWidget class.

 

 

 

 

 

Downloads

 

Technical facts

 

Operating system(s) or programming environment(s)

IDE(s):

Project type:

C++ standard:

Compiler(s):

Libraries used:

 

 

 

 

 

Qt project file: ./ToolTestBouncingRectsWidget/ToolTestBouncingRectsWidgetDesktop.pro

 

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

include(../../Tools/ToolTestBouncingRectsWidget/ToolTestBouncingRectsWidgetDesktop.pri)

SOURCES += qtmain.cpp

 

 

 

 

 

./ToolTestBouncingRectsWidget/ToolTestBouncingRectsWidgetConsole.pri

 

INCLUDEPATH += ../../Tools/ToolTestBouncingRectsWidget

 

 

 

 

 

./ToolTestBouncingRectsWidget/ToolTestBouncingRectsWidgetDesktop.pri

 

include(../../Tools/ToolTestBouncingRectsWidget/ToolTestBouncingRectsWidgetConsole.pri)

SOURCES += \
    ../../Tools/ToolTestBouncingRectsWidget/qtbouncingrect.cpp \
    ../../Tools/ToolTestBouncingRectsWidget/qtbouncingrectswidget.cpp \
    ../../Tools/ToolTestBouncingRectsWidget/qttooltestbouncingrectswidgetmaindialog.cpp

HEADERS += \
    ../../Tools/ToolTestBouncingRectsWidget/qtbouncingrect.h \
    ../../Tools/ToolTestBouncingRectsWidget/qtbouncingrectswidget.h \
    ../../Tools/ToolTestBouncingRectsWidget/qttooltestbouncingrectswidgetmaindialog.h

FORMS += \
    ../../Tools/ToolTestBouncingRectsWidget/qttooltestbouncingrectswidgetmaindialog.ui

 

 

 

 

 

./ToolTestBouncingRectsWidget/qtbouncingrect.h

 

#ifndef QTBOUNCINGRECT_H
#define QTBOUNCINGRECT_H

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#include <QGraphicsItem>
#pragma GCC diagnostic pop

struct QGraphicsScene;

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

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

  ///Thanks compiler for reminding me to add this member function!
  QRectF boundingRect() 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 // QTBOUNCINGRECT_H

 

 

 

 

 

./ToolTestBouncingRectsWidget/qtbouncingrect.cpp

 

#include <cassert>
#include <cmath>

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#include <QGraphicsItem>
#include <QGraphicsScene>
#include <QList>
#include <QPainter>
#include "qtbouncingrect.h"
#pragma GCC diagnostic pop

QtBouncingRect::QtBouncingRect(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 QtBouncingRect::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 QtBouncingRect::boundingRect() const
{
  return QRectF(-16.0,-16.0,32.0,32.0);
}

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

 

 

 

 

 

./ToolTestBouncingRectsWidget/qtbouncingrectswidget.h

 

#ifndef QTBOUNCINGRECTSWIDGET_H
#define QTBOUNCINGRECTSWIDGET_H

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#include <QGraphicsView>
#pragma GCC diagnostic pop

struct QGraphicsScene;

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


  protected:
  void resizeEvent(QResizeEvent *event);

  private:
  QGraphicsScene * const m_scene;
};

#endif // QTBOUNCINGRECTSWIDGET_H

 

 

 

 

 

./ToolTestBouncingRectsWidget/qtbouncingrectswidget.cpp

 

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#include <QTimer>
#include <QGraphicsScene>
#include "qtbouncingrect.h"
#include "qtbouncingrectswidget.h"
#pragma GCC diagnostic pop

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

  for (int i=0; i!=3; ++i)
  {
    QtBouncingRect * const ball = new QtBouncingRect(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 QtBouncingRectsWidget::resizeEvent(QResizeEvent *)
{
  m_scene->setSceneRect(this->rect());
}

 

 

 

 

 

./ToolTestBouncingRectsWidget/qtmain.cpp

 

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#include <QApplication>
#include "qttooltestbouncingrectswidgetmaindialog.h"
#pragma GCC diagnostic pop

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

 

 

 

 

 

./ToolTestBouncingRectsWidget/qttooltestbouncingrectswidgetmaindialog.h

 

#ifndef QTTOOLTESTBOUNCINGRECTSWIDGETMAINDIALOG_H
#define QTTOOLTESTBOUNCINGRECTSWIDGETMAINDIALOG_H

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#include "qthideandshowdialog.h"
#pragma GCC diagnostic pop

namespace Ui {
class QtToolTestBouncingRectsWidgetMainDialog;
}

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

#endif // QTTOOLTESTBOUNCINGRECTSWIDGETMAINDIALOG_H

 

 

 

 

 

./ToolTestBouncingRectsWidget/qttooltestbouncingrectswidgetmaindialog.cpp

 

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#include "qtbouncingrectswidget.h"
#include "qttooltestbouncingrectswidgetmaindialog.h"
#include "ui_qttooltestbouncingrectswidgetmaindialog.h"
#pragma GCC diagnostic pop

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

QtToolTestBouncingRectsWidgetMainDialog::~QtToolTestBouncingRectsWidgetMainDialog() noexcept
{
  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