diff options
Diffstat (limited to 'examples/widgets/graphicsview')
13 files changed, 52 insertions, 64 deletions
diff --git a/examples/widgets/graphicsview/boxes/glbuffers.cpp b/examples/widgets/graphicsview/boxes/glbuffers.cpp index 1481292e76..851cd17796 100644 --- a/examples/widgets/graphicsview/boxes/glbuffers.cpp +++ b/examples/widgets/graphicsview/boxes/glbuffers.cpp @@ -50,11 +50,11 @@ #include "glbuffers.h" #include <QtGui/qmatrix4x4.h> - +#include <QtCore/qmath.h> void qgluPerspective(GLdouble fovy, GLdouble aspect, GLdouble zNear, GLdouble zFar) { - const GLdouble ymax = zNear * tan(fovy * M_PI / 360.0); + const GLdouble ymax = zNear * tan(qDegreesToRadians(fovy) / 2.0); const GLdouble ymin = -ymax; const GLdouble xmin = ymin * aspect; const GLdouble xmax = ymax * aspect; diff --git a/examples/widgets/graphicsview/boxes/qtbox.cpp b/examples/widgets/graphicsview/boxes/qtbox.cpp index 9a19985fac..3a184dd0b6 100644 --- a/examples/widgets/graphicsview/boxes/qtbox.cpp +++ b/examples/widgets/graphicsview/boxes/qtbox.cpp @@ -414,7 +414,7 @@ void QtBox::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWi CircleItem::CircleItem(int size, int x, int y) : ItemBase(size, x, y) { - m_color = QColor::fromHsv(rand() % 360, 255, 255); + m_color = QColor::fromHsv(QRandomGenerator::global()->bounded(360), 255, 255); } void CircleItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) diff --git a/examples/widgets/graphicsview/boxes/scene.cpp b/examples/widgets/graphicsview/boxes/scene.cpp index 9ac429c667..f51cad99ac 100644 --- a/examples/widgets/graphicsview/boxes/scene.cpp +++ b/examples/widgets/graphicsview/boxes/scene.cpp @@ -50,9 +50,10 @@ #include <QDebug> #include "scene.h" +#include <QtCore/QRandomGenerator> #include <QtGui/qmatrix4x4.h> #include <QtGui/qvector3d.h> -#include <cmath> +#include <qmath.h> #include "3rdparty/fbm.h" @@ -868,11 +869,12 @@ void Scene::renderCubemaps() QVector3D center; + const float eachAngle = 2 * M_PI / m_cubemaps.size(); for (int i = m_frame % N; i < m_cubemaps.size(); i += N) { if (0 == m_cubemaps[i]) continue; - float angle = 2.0f * PI * i / m_cubemaps.size(); + float angle = i * eachAngle; center = m_trackBalls[1].rotation().rotatedVector(QVector3D(std::cos(angle), std::sin(angle), 0.0f)); @@ -1071,13 +1073,16 @@ void Scene::newItem(ItemDialog::ItemType type) QSize size = sceneRect().size().toSize(); switch (type) { case ItemDialog::QtBoxItem: - addItem(new QtBox(64, rand() % (size.width() - 64) + 32, rand() % (size.height() - 64) + 32)); + addItem(new QtBox(64, QRandomGenerator::global()->bounded(size.width() - 64) + 32, + QRandomGenerator::global()->bounded(size.height() - 64) + 32)); break; case ItemDialog::CircleItem: - addItem(new CircleItem(64, rand() % (size.width() - 64) + 32, rand() % (size.height() - 64) + 32)); + addItem(new CircleItem(64, QRandomGenerator::global()->bounded(size.width() - 64) + 32, + QRandomGenerator::global()->bounded(size.height() - 64) + 32)); break; case ItemDialog::SquareItem: - addItem(new SquareItem(64, rand() % (size.width() - 64) + 32, rand() % (size.height() - 64) + 32)); + addItem(new SquareItem(64, QRandomGenerator::global()->bounded(size.width() - 64) + 32, + QRandomGenerator::global()->bounded(size.height() - 64) + 32)); break; default: break; diff --git a/examples/widgets/graphicsview/boxes/scene.h b/examples/widgets/graphicsview/boxes/scene.h index a2ba1d0b5a..ccb6f368cd 100644 --- a/examples/widgets/graphicsview/boxes/scene.h +++ b/examples/widgets/graphicsview/boxes/scene.h @@ -63,8 +63,6 @@ #include "glbuffers.h" #include "qtbox.h" -#define PI 3.14159265358979 - QT_BEGIN_NAMESPACE class QMatrix4x4; QT_END_NAMESPACE diff --git a/examples/widgets/graphicsview/boxes/trackball.cpp b/examples/widgets/graphicsview/boxes/trackball.cpp index 15f3af77d1..794ce7ac37 100644 --- a/examples/widgets/graphicsview/boxes/trackball.cpp +++ b/examples/widgets/graphicsview/boxes/trackball.cpp @@ -50,6 +50,7 @@ #include "trackball.h" #include "scene.h" +#include <qmath.h> #include <cmath> //============================================================================// @@ -101,10 +102,11 @@ void TrackBall::move(const QPointF& p, const QQuaternion &transformation) case Plane: { QLineF delta(m_lastPos, p); - m_angularVelocity = 180*delta.length() / (PI*msecs); + const float angleDelta = qRadiansToDegrees(float(delta.length())); + m_angularVelocity = angleDelta / msecs; m_axis = QVector3D(-delta.dy(), delta.dx(), 0.0f).normalized(); m_axis = transformation.rotatedVector(m_axis); - m_rotation = QQuaternion::fromAxisAndAngle(m_axis, 180 / PI * delta.length()) * m_rotation; + m_rotation = QQuaternion::fromAxisAndAngle(m_axis, angleDelta) * m_rotation; } break; case Sphere: @@ -124,7 +126,7 @@ void TrackBall::move(const QPointF& p, const QQuaternion &transformation) currentPos3D.normalize(); m_axis = QVector3D::crossProduct(lastPos3D, currentPos3D); - float angle = 180 / PI * std::asin(std::sqrt(QVector3D::dotProduct(m_axis, m_axis))); + float angle = qRadiansToDegrees(std::asin(m_axis.length())); m_angularVelocity = angle / msecs; m_axis.normalize(); diff --git a/examples/widgets/graphicsview/collidingmice/main.cpp b/examples/widgets/graphicsview/collidingmice/main.cpp index a0659b9bc1..91aee70b86 100644 --- a/examples/widgets/graphicsview/collidingmice/main.cpp +++ b/examples/widgets/graphicsview/collidingmice/main.cpp @@ -60,7 +60,6 @@ static const int MouseCount = 7; int main(int argc, char **argv) { QApplication app(argc, argv); - qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime())); //! [0] //! [1] diff --git a/examples/widgets/graphicsview/collidingmice/mouse.cpp b/examples/widgets/graphicsview/collidingmice/mouse.cpp index b94c650c18..14f887f6e3 100644 --- a/examples/widgets/graphicsview/collidingmice/mouse.cpp +++ b/examples/widgets/graphicsview/collidingmice/mouse.cpp @@ -52,12 +52,12 @@ #include <QGraphicsScene> #include <QPainter> +#include <QRandomGenerator> #include <QStyleOption> +#include <qmath.h> -#include <math.h> - -static const double Pi = 3.14159265358979323846264338327950288419717; -static double TwoPi = 2.0 * Pi; +const qreal Pi = M_PI; +const qreal TwoPi = 2 * M_PI; static qreal normalizeAngle(qreal angle) { @@ -71,9 +71,9 @@ static qreal normalizeAngle(qreal angle) //! [0] Mouse::Mouse() : angle(0), speed(0), mouseEyeDirection(0), - color(qrand() % 256, qrand() % 256, qrand() % 256) + color(QRandomGenerator::global()->bounded(256), QRandomGenerator::global()->bounded(256), QRandomGenerator::global()->bounded(256)) { - setRotation(qrand() % (360 * 16)); + setRotation(QRandomGenerator::global()->bounded(360 * 16)); } //! [0] @@ -140,9 +140,7 @@ void Mouse::advance(int step) //! [5] QLineF lineToCenter(QPointF(0, 0), mapFromScene(0, 0)); if (lineToCenter.length() > 150) { - qreal angleToCenter = ::acos(lineToCenter.dx() / lineToCenter.length()); - if (lineToCenter.dy() < 0) - angleToCenter = TwoPi - angleToCenter; + qreal angleToCenter = std::atan2(lineToCenter.dy(), lineToCenter.dx()); angleToCenter = normalizeAngle((Pi - angleToCenter) + Pi / 2); if (angleToCenter < Pi && angleToCenter > Pi / 4) { @@ -171,9 +169,7 @@ void Mouse::advance(int step) continue; QLineF lineToMouse(QPointF(0, 0), mapFromItem(item, 0, 0)); - qreal angleToMouse = ::acos(lineToMouse.dx() / lineToMouse.length()); - if (lineToMouse.dy() < 0) - angleToMouse = TwoPi - angleToMouse; + qreal angleToMouse = std::atan2(lineToMouse.dy(), lineToMouse.dx()); angleToMouse = normalizeAngle((Pi - angleToMouse) + Pi / 2); if (angleToMouse >= 0 && angleToMouse < Pi / 2) { @@ -190,16 +186,16 @@ void Mouse::advance(int step) // Add some random movement //! [10] - if (dangerMice.size() > 1 && (qrand() % 10) == 0) { - if (qrand() % 1) - angle += (qrand() % 100) / 500.0; + if (dangerMice.size() > 1 && QRandomGenerator::global()->bounded(10) == 0) { + if (QRandomGenerator::global()->bounded(1)) + angle += QRandomGenerator::global()->bounded(1 / 500.0); else - angle -= (qrand() % 100) / 500.0; + angle -= QRandomGenerator::global()->bounded(1 / 500.0); } //! [10] //! [11] - speed += (-50 + qrand() % 100) / 100.0; + speed += (-50 + QRandomGenerator::global()->bounded(100)) / 100.0; qreal dx = ::sin(angle) * 10; mouseEyeDirection = (qAbs(dx / 5) < 1) ? 0 : dx / 5; diff --git a/examples/widgets/graphicsview/diagramscene/arrow.cpp b/examples/widgets/graphicsview/diagramscene/arrow.cpp index 012b9ea2ed..88160d9399 100644 --- a/examples/widgets/graphicsview/diagramscene/arrow.cpp +++ b/examples/widgets/graphicsview/diagramscene/arrow.cpp @@ -51,13 +51,10 @@ #include "arrow.h" -#include <math.h> - +#include <qmath.h> #include <QPen> #include <QPainter> -const qreal Pi = 3.14; - //! [0] Arrow::Arrow(DiagramItem *startItem, DiagramItem *endItem, QGraphicsItem *parent) : QGraphicsLineItem(parent) @@ -132,14 +129,12 @@ void Arrow::paint(QPainter *painter, const QStyleOptionGraphicsItem *, setLine(QLineF(intersectPoint, myStartItem->pos())); //! [5] //! [6] - double angle = ::acos(line().dx() / line().length()); - if (line().dy() >= 0) - angle = (Pi * 2) - angle; + double angle = std::atan2(-line().dy(), line().dx()); - QPointF arrowP1 = line().p1() + QPointF(sin(angle + Pi / 3) * arrowSize, - cos(angle + Pi / 3) * arrowSize); - QPointF arrowP2 = line().p1() + QPointF(sin(angle + Pi - Pi / 3) * arrowSize, - cos(angle + Pi - Pi / 3) * arrowSize); + QPointF arrowP1 = line().p1() + QPointF(sin(angle + M_PI / 3) * arrowSize, + cos(angle + M_PI / 3) * arrowSize); + QPointF arrowP2 = line().p1() + QPointF(sin(angle + M_PI - M_PI / 3) * arrowSize, + cos(angle + M_PI - M_PI / 3) * arrowSize); arrowHead.clear(); arrowHead << line().p1() << arrowP1 << arrowP2; diff --git a/examples/widgets/graphicsview/dragdroprobot/coloritem.cpp b/examples/widgets/graphicsview/dragdroprobot/coloritem.cpp index 64a715d31f..262e18a317 100644 --- a/examples/widgets/graphicsview/dragdroprobot/coloritem.cpp +++ b/examples/widgets/graphicsview/dragdroprobot/coloritem.cpp @@ -54,7 +54,7 @@ //! [0] ColorItem::ColorItem() - : color(qrand() % 256, qrand() % 256, qrand() % 256) + : color(QRandomGenerator::global()->bounded(256), QRandomGenerator::global()->bounded(256), QRandomGenerator::global()->bounded(256)) { setToolTip(QString("QColor(%1, %2, %3)\n%4") .arg(color.red()).arg(color.green()).arg(color.blue()) @@ -107,7 +107,7 @@ void ColorItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) //! [6] static int n = 0; - if (n++ > 2 && (qrand() % 3) == 0) { + if (n++ > 2 && QRandomGenerator::global()->bounded(3) == 0) { QImage image(":/images/head.png"); mime->setImageData(image); diff --git a/examples/widgets/graphicsview/dragdroprobot/main.cpp b/examples/widgets/graphicsview/dragdroprobot/main.cpp index 20cec92d26..045e184569 100644 --- a/examples/widgets/graphicsview/dragdroprobot/main.cpp +++ b/examples/widgets/graphicsview/dragdroprobot/main.cpp @@ -73,7 +73,6 @@ int main(int argc, char **argv) { QApplication app(argc, argv); - qsrand(QTime(0,0,0).secsTo(QTime::currentTime())); //! [0] //! [1] QGraphicsScene scene(-200, -200, 400, 400); diff --git a/examples/widgets/graphicsview/elasticnodes/edge.cpp b/examples/widgets/graphicsview/elasticnodes/edge.cpp index e794e803cf..aec12b4225 100644 --- a/examples/widgets/graphicsview/elasticnodes/edge.cpp +++ b/examples/widgets/graphicsview/elasticnodes/edge.cpp @@ -51,13 +51,9 @@ #include "edge.h" #include "node.h" -#include <math.h> - +#include <qmath.h> #include <QPainter> -static const double Pi = 3.14159265358979323846264338327950288419717; -static double TwoPi = 2.0 * Pi; - //! [0] Edge::Edge(Node *sourceNode, Node *destNode) : arrowSize(10) @@ -139,18 +135,16 @@ void Edge::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *) //! [6] // Draw the arrows - double angle = ::acos(line.dx() / line.length()); - if (line.dy() >= 0) - angle = TwoPi - angle; - - QPointF sourceArrowP1 = sourcePoint + QPointF(sin(angle + Pi / 3) * arrowSize, - cos(angle + Pi / 3) * arrowSize); - QPointF sourceArrowP2 = sourcePoint + QPointF(sin(angle + Pi - Pi / 3) * arrowSize, - cos(angle + Pi - Pi / 3) * arrowSize); - QPointF destArrowP1 = destPoint + QPointF(sin(angle - Pi / 3) * arrowSize, - cos(angle - Pi / 3) * arrowSize); - QPointF destArrowP2 = destPoint + QPointF(sin(angle - Pi + Pi / 3) * arrowSize, - cos(angle - Pi + Pi / 3) * arrowSize); + double angle = std::atan2(-line.dy(), line.dx()); + + QPointF sourceArrowP1 = sourcePoint + QPointF(sin(angle + M_PI / 3) * arrowSize, + cos(angle + M_PI / 3) * arrowSize); + QPointF sourceArrowP2 = sourcePoint + QPointF(sin(angle + M_PI - M_PI / 3) * arrowSize, + cos(angle + M_PI - M_PI / 3) * arrowSize); + QPointF destArrowP1 = destPoint + QPointF(sin(angle - M_PI / 3) * arrowSize, + cos(angle - M_PI / 3) * arrowSize); + QPointF destArrowP2 = destPoint + QPointF(sin(angle - M_PI + M_PI / 3) * arrowSize, + cos(angle - M_PI + M_PI / 3) * arrowSize); painter->setBrush(Qt::black); painter->drawPolygon(QPolygonF() << line.p1() << sourceArrowP1 << sourceArrowP2); diff --git a/examples/widgets/graphicsview/elasticnodes/graphwidget.cpp b/examples/widgets/graphicsview/elasticnodes/graphwidget.cpp index 844c8f8aac..4259aab803 100644 --- a/examples/widgets/graphicsview/elasticnodes/graphwidget.cpp +++ b/examples/widgets/graphicsview/elasticnodes/graphwidget.cpp @@ -55,6 +55,7 @@ #include <math.h> #include <QKeyEvent> +#include <QRandomGenerator> //! [0] GraphWidget::GraphWidget(QWidget *parent) @@ -247,7 +248,7 @@ void GraphWidget::shuffle() { foreach (QGraphicsItem *item, scene()->items()) { if (qgraphicsitem_cast<Node *>(item)) - item->setPos(-150 + qrand() % 300, -150 + qrand() % 300); + item->setPos(-150 + QRandomGenerator::global()->bounded(300), -150 + QRandomGenerator::global()->bounded(300)); } } diff --git a/examples/widgets/graphicsview/elasticnodes/main.cpp b/examples/widgets/graphicsview/elasticnodes/main.cpp index 75cc4b0f69..1e372a9f6d 100644 --- a/examples/widgets/graphicsview/elasticnodes/main.cpp +++ b/examples/widgets/graphicsview/elasticnodes/main.cpp @@ -57,7 +57,6 @@ int main(int argc, char **argv) { QApplication app(argc, argv); - qsrand(QTime(0,0,0).secsTo(QTime::currentTime())); GraphWidget *widget = new GraphWidget; |