diff options
Diffstat (limited to 'examples/widgets')
20 files changed, 104 insertions, 142 deletions
diff --git a/examples/widgets/animation/stickman/stickman.cpp b/examples/widgets/animation/stickman/stickman.cpp index 7a4629c4d2..b7a2d87ada 100644 --- a/examples/widgets/animation/stickman/stickman.cpp +++ b/examples/widgets/animation/stickman/stickman.cpp @@ -53,13 +53,7 @@ #include <QPainter> #include <QTimer> - -#define _USE_MATH_DEFINES -#include <math.h> - -#ifndef M_PI -#define M_PI 3.14159265358979323846 -#endif +#include <qmath.h> static const qreal Coords[NodeCount * 2] = { 0.0, -150.0, // head, #0 @@ -300,7 +294,7 @@ void StickMan::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidge QPointF dist = node2->pos() - node1->pos(); qreal sinAngle = dist.x() / sqrt(pow(dist.x(), 2) + pow(dist.y(), 2)); - qreal angle = asin(sinAngle) * 180.0 / M_PI; + qreal angle = qRadiansToDegrees(asin(sinAngle)); QPointF headPos = node1->pos(); painter->translate(headPos); diff --git a/examples/widgets/doc/src/painterpaths.qdoc b/examples/widgets/doc/src/painterpaths.qdoc index 2cc5ff410a..bd821ac1bf 100644 --- a/examples/widgets/doc/src/painterpaths.qdoc +++ b/examples/widgets/doc/src/painterpaths.qdoc @@ -98,14 +98,9 @@ \section1 Window Class Implementation - In the implementation of the \c Window class we first declare the - constant \c Pi with six significant figures: - - \snippet painting/painterpaths/window.cpp 0 - - In the constructor, we then define the various painter paths and - create corresponding \c RenderArea widgets which will render the - graphical shapes: + In the \c Window constructor, we define the various painter paths + and create corresponding \c RenderArea widgets which will render + the graphical shapes: \snippet painting/painterpaths/window.cpp 1 diff --git a/examples/widgets/doc/src/tablet.qdoc b/examples/widgets/doc/src/tablet.qdoc index 88fdefa68f..2b11020c07 100644 --- a/examples/widgets/doc/src/tablet.qdoc +++ b/examples/widgets/doc/src/tablet.qdoc @@ -206,8 +206,7 @@ \snippet widgets/tablet/tabletcanvas.cpp 0 - In the constructor we initialize our class variables. We need - to draw the background of our pixmap, as the default is gray. + In the constructor we initialize most of our class variables. Here is the implementation of \c saveImage(): @@ -247,7 +246,15 @@ \snippet widgets/tablet/tabletcanvas.cpp 4 - We simply draw the pixmap to the top left of the widget. + The first time Qt calls paintEvent(), m_pixmap is default-constructed, so + QPixmap::isNull() returns \c true. Now that we know which screen we will be + rendering to, we can create a pixmap with the appropriate resolution. + The size of the pixmap with which we fill the window depends on the screen + resolution, as the example does not support zoom; and it may be that one + screen is \l {High DPI Displays}{high DPI} while another is not. We need to + draw the background too, as the default is gray. + + After that, we simply draw the pixmap to the top left of the widget. Here is the implementation of \c paintPixmap(): diff --git a/examples/widgets/effects/blurpicker/blurpicker.cpp b/examples/widgets/effects/blurpicker/blurpicker.cpp index 43823bed30..a00af2144a 100644 --- a/examples/widgets/effects/blurpicker/blurpicker.cpp +++ b/examples/widgets/effects/blurpicker/blurpicker.cpp @@ -52,13 +52,9 @@ #include <QtWidgets> #include <QtCore/qmath.h> - +#include <qmath.h> #include "blureffect.h" -#ifndef M_PI -#define M_PI 3.14159265358979323846 -#endif - BlurPicker::BlurPicker(QWidget *parent): QGraphicsView(parent), m_index(0.0), m_animation(this, "index") { setBackgroundBrush(QPixmap(":/images/background.jpg")); @@ -84,9 +80,10 @@ void BlurPicker::setIndex(qreal index) m_index = index; qreal baseline = 0; + const qreal iconAngle = 2 * M_PI / m_icons.count(); for (int i = 0; i < m_icons.count(); ++i) { QGraphicsItem *icon = m_icons[i]; - qreal a = ((i + m_index) * 2 * M_PI) / m_icons.count(); + qreal a = (i + m_index) * iconAngle; qreal xs = 170 * qSin(a); qreal ys = 100 * qCos(a); QPointF pos(xs, ys); diff --git a/examples/widgets/effects/lighting/lighting.cpp b/examples/widgets/effects/lighting/lighting.cpp index 68350f32b7..1ba7dd9ce7 100644 --- a/examples/widgets/effects/lighting/lighting.cpp +++ b/examples/widgets/effects/lighting/lighting.cpp @@ -49,14 +49,9 @@ ****************************************************************************/ #include "lighting.h" - #include <QtWidgets> #include <QtCore/qmath.h> -#ifndef M_PI -#define M_PI 3.14159265358979323846 -#endif - Lighting::Lighting(QWidget *parent): QGraphicsView(parent), angle(0.0) { setScene(&m_scene); @@ -120,7 +115,7 @@ void Lighting::setupScene() void Lighting::animate() { - angle += (M_PI / 30); + angle += qDegreesToRadians(qreal(6)); qreal xs = 200 * qSin(angle) - 40 + 25; qreal ys = 200 * qCos(angle) - 40 + 25; m_lightSource->setPos(xs, ys); 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/scene.cpp b/examples/widgets/graphicsview/boxes/scene.cpp index 9ac429c667..31b9553c75 100644 --- a/examples/widgets/graphicsview/boxes/scene.cpp +++ b/examples/widgets/graphicsview/boxes/scene.cpp @@ -52,7 +52,7 @@ #include "scene.h" #include <QtGui/qmatrix4x4.h> #include <QtGui/qvector3d.h> -#include <cmath> +#include <qmath.h> #include "3rdparty/fbm.h" @@ -868,11 +868,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)); 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/mouse.cpp b/examples/widgets/graphicsview/collidingmice/mouse.cpp index b94c650c18..298e4aae64 100644 --- a/examples/widgets/graphicsview/collidingmice/mouse.cpp +++ b/examples/widgets/graphicsview/collidingmice/mouse.cpp @@ -53,11 +53,10 @@ #include <QGraphicsScene> #include <QPainter> #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) { @@ -140,9 +139,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 +168,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) { 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/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/itemviews/chart/pieview.cpp b/examples/widgets/itemviews/chart/pieview.cpp index fb439fae67..3f85e397ee 100644 --- a/examples/widgets/itemviews/chart/pieview.cpp +++ b/examples/widgets/itemviews/chart/pieview.cpp @@ -49,12 +49,8 @@ ****************************************************************************/ #include <QtWidgets> +#include <qmath.h> #include <cmath> - -#ifndef M_PI -#define M_PI 3.1415927 -#endif - #include "pieview.h" PieView::PieView(QWidget *parent) @@ -125,9 +121,9 @@ QModelIndex PieView::indexAt(const QPoint &point) const return QModelIndex(); // Determine the angle of the point. - double angle = (180 / M_PI) * std::acos(cx / d); - if (cy < 0) - angle = 360 - angle; + double angle = qRadiansToDegrees(std::atan2(cy, cx)); + if (angle < 0) + angle = 360 + angle; // Find the relevant slice of the pie. double startAngle = 0.0; diff --git a/examples/widgets/itemviews/storageview/storagemodel.cpp b/examples/widgets/itemviews/storageview/storagemodel.cpp index b7c594f8f7..1395c9f208 100644 --- a/examples/widgets/itemviews/storageview/storagemodel.cpp +++ b/examples/widgets/itemviews/storageview/storagemodel.cpp @@ -52,25 +52,10 @@ #include "storagemodel.h" #include <QDir> +#include <QLocale> #include <qmath.h> #include <cmath> -static QString sizeToString(qint64 size) -{ - static const char *const strings[] = { "b", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" }; - - if (size <= 0) - return StorageModel::tr("0 b"); - - double power = std::log((double)size)/std::log(1024.0); - int intPower = (int)power; - intPower = intPower >= 8 ? 8 - 1 : intPower; - - double normSize = size / std::pow(1024.0, intPower); - //: this should expand to "1.23 GB" - return StorageModel::tr("%1 %2").arg(normSize, 0, 'f', intPower > 0 ? 2 : 0).arg(strings[intPower]); -} - StorageModel::StorageModel(QObject *parent) : QAbstractTableModel(parent), m_volumes(QStorageInfo::mountedVolumes()) @@ -106,11 +91,11 @@ QVariant StorageModel::data(const QModelIndex &index, int role) const case ColumnFileSystemName: return volume.fileSystemType(); case ColumnTotal: - return sizeToString(volume.bytesTotal()); + return QLocale().formattedDataSize(volume.bytesTotal()); case ColumnFree: - return sizeToString(volume.bytesFree()); + return QLocale().formattedDataSize(volume.bytesFree()); case ColumnAvailable: - return sizeToString(volume.bytesAvailable()); + return QLocale().formattedDataSize(volume.bytesAvailable()); case ColumnIsReady: return volume.isReady(); case ColumnIsReadOnly: @@ -121,6 +106,7 @@ QVariant StorageModel::data(const QModelIndex &index, int role) const break; } } else if (role == Qt::ToolTipRole) { + QLocale locale; const QStorageInfo &volume = m_volumes.at(index.row()); return tr("Root path : %1\n" "Name: %2\n" @@ -140,9 +126,9 @@ QVariant StorageModel::data(const QModelIndex &index, int role) const arg(volume.displayName()). arg(QString::fromUtf8(volume.device())). arg(QString::fromUtf8(volume.fileSystemType())). - arg(sizeToString(volume.bytesTotal())). - arg(sizeToString(volume.bytesFree())). - arg(sizeToString(volume.bytesAvailable())). + arg(locale.formattedDataSize(volume.bytesTotal())). + arg(locale.formattedDataSize(volume.bytesFree())). + arg(locale.formattedDataSize(volume.bytesAvailable())). arg(volume.isReady() ? tr("true") : tr("false")). arg(volume.isReadOnly() ? tr("true") : tr("false")). arg(volume.isValid() ? tr("true") : tr("false")). diff --git a/examples/widgets/mainwindows/mainwindow/mainwindow.cpp b/examples/widgets/mainwindows/mainwindow/mainwindow.cpp index 10e3dd045a..afceddfca1 100644 --- a/examples/widgets/mainwindows/mainwindow/mainwindow.cpp +++ b/examples/widgets/mainwindows/mainwindow/mainwindow.cpp @@ -63,7 +63,6 @@ #include <QFileDialog> #include <QDialogButtonBox> #include <QMessageBox> -#include <QSignalMapper> #include <QApplication> #include <QPainter> #include <QMouseEvent> diff --git a/examples/widgets/painting/composition/composition.cpp b/examples/widgets/painting/composition/composition.cpp index d5d674fd83..0b57d3c7d3 100644 --- a/examples/widgets/painting/composition/composition.cpp +++ b/examples/widgets/painting/composition/composition.cpp @@ -464,7 +464,7 @@ void CompositionRenderer::paint(QPainter *painter) drawBase(p); } - memcpy(m_buffer.bits(), m_base_buffer.bits(), m_buffer.byteCount()); + memcpy(m_buffer.bits(), m_base_buffer.bits(), m_buffer.sizeInBytes()); { QPainter p(&m_buffer); diff --git a/examples/widgets/painting/painterpaths/window.cpp b/examples/widgets/painting/painterpaths/window.cpp index 7122411a6c..a987937b39 100644 --- a/examples/widgets/painting/painterpaths/window.cpp +++ b/examples/widgets/painting/painterpaths/window.cpp @@ -53,11 +53,7 @@ #include <QtWidgets> -#include <cmath> - -//! [0] -const float Pi = 3.14159f; -//! [0] +#include <qmath.h> //! [1] Window::Window() @@ -133,8 +129,8 @@ Window::Window() QPainterPath starPath; starPath.moveTo(90, 50); for (int i = 1; i < 5; ++i) { - starPath.lineTo(50 + 40 * std::cos(0.8 * i * Pi), - 50 + 40 * std::sin(0.8 * i * Pi)); + starPath.lineTo(50 + 40 * std::cos(0.8 * i * M_PI), + 50 + 40 * std::sin(0.8 * i * M_PI)); } starPath.closeSubpath(); //! [9] diff --git a/examples/widgets/tools/plugandpaint/plugins/basictools/basictoolsplugin.cpp b/examples/widgets/tools/plugandpaint/plugins/basictools/basictoolsplugin.cpp index c929ff6922..740d007d44 100644 --- a/examples/widgets/tools/plugandpaint/plugins/basictools/basictoolsplugin.cpp +++ b/examples/widgets/tools/plugandpaint/plugins/basictools/basictoolsplugin.cpp @@ -50,13 +50,11 @@ #include <QtWidgets> -#include <cmath> +#include <qmath.h> #include <stdlib.h> #include "basictoolsplugin.h" -const float Pi = 3.14159f; - //! [0] QStringList BasicToolsPlugin::brushes() const { @@ -149,8 +147,8 @@ QPainterPath BasicToolsPlugin::generateShape(const QString &shape, } else if (shape == tr("Star")) { path.moveTo(90, 50); for (int i = 1; i < 5; ++i) { - path.lineTo(50 + 40 * std::cos(0.8 * i * Pi), - 50 + 40 * std::sin(0.8 * i * Pi)); + path.lineTo(50 + 40 * std::cos(0.8 * i * M_PI), + 50 + 40 * std::sin(0.8 * i * M_PI)); } path.closeSubpath(); } else if (shape == tr("Text...")) { diff --git a/examples/widgets/widgets/tablet/tabletcanvas.cpp b/examples/widgets/widgets/tablet/tabletcanvas.cpp index 90b3222970..4b11568dfe 100644 --- a/examples/widgets/widgets/tablet/tabletcanvas.cpp +++ b/examples/widgets/widgets/tablet/tabletcanvas.cpp @@ -65,21 +65,9 @@ TabletCanvas::TabletCanvas() , m_deviceDown(false) { resize(500, 500); - initPixmap(); setAutoFillBackground(true); setAttribute(Qt::WA_TabletTracking); } - -void TabletCanvas::initPixmap() -{ - QPixmap newPixmap = QPixmap(width(), height()); - newPixmap.fill(Qt::white); - QPainter painter(&newPixmap); - if (!m_pixmap.isNull()) - painter.drawPixmap(0, 0, m_pixmap); - painter.end(); - m_pixmap = newPixmap; -} //! [0] //! [1] @@ -105,23 +93,26 @@ bool TabletCanvas::loadImage(const QString &file) //! [3] void TabletCanvas::tabletEvent(QTabletEvent *event) { - switch (event->type()) { case QEvent::TabletPress: if (!m_deviceDown) { m_deviceDown = true; lastPoint.pos = event->posF(); + lastPoint.pressure = event->pressure(); lastPoint.rotation = event->rotation(); } break; case QEvent::TabletMove: +#ifndef Q_OS_IOS if (event->device() == QTabletEvent::RotationStylus) updateCursor(event); +#endif if (m_deviceDown) { updateBrush(event); QPainter painter(&m_pixmap); paintPixmap(painter, event); lastPoint.pos = event->posF(); + lastPoint.pressure = event->pressure(); lastPoint.rotation = event->rotation(); } break; @@ -138,8 +129,23 @@ void TabletCanvas::tabletEvent(QTabletEvent *event) //! [3] //! [4] +void TabletCanvas::initPixmap() +{ + qreal dpr = devicePixelRatioF(); + QPixmap newPixmap = QPixmap(width() * dpr, height() * dpr); + newPixmap.setDevicePixelRatio(dpr); + newPixmap.fill(Qt::white); + QPainter painter(&newPixmap); + if (!m_pixmap.isNull()) + painter.drawPixmap(0, 0, m_pixmap); + painter.end(); + m_pixmap = newPixmap; +} + void TabletCanvas::paintEvent(QPaintEvent *) { + if (m_pixmap.isNull()) + initPixmap(); QPainter painter(this); painter.drawPixmap(0, 0, m_pixmap); } @@ -171,13 +177,14 @@ void TabletCanvas::paintPixmap(QPainter &painter, QTabletEvent *event) painter.setPen(Qt::NoPen); painter.setBrush(m_brush); QPolygonF poly; - qreal halfWidth = m_pen.widthF(); - QPointF brushAdjust(qSin(qDegreesToRadians(lastPoint.rotation)) * halfWidth, - qCos(qDegreesToRadians(lastPoint.rotation)) * halfWidth); + qreal halfWidth = pressureToWidth(lastPoint.pressure); + QPointF brushAdjust(qSin(qDegreesToRadians(-lastPoint.rotation)) * halfWidth, + qCos(qDegreesToRadians(-lastPoint.rotation)) * halfWidth); poly << lastPoint.pos + brushAdjust; poly << lastPoint.pos - brushAdjust; - brushAdjust = QPointF(qSin(qDegreesToRadians(event->rotation())) * halfWidth, - qCos(qDegreesToRadians(event->rotation())) * halfWidth); + halfWidth = m_pen.widthF(); + brushAdjust = QPointF(qSin(qDegreesToRadians(-event->rotation())) * halfWidth, + qCos(qDegreesToRadians(-event->rotation())) * halfWidth); poly << event->posF() - brushAdjust; poly << event->posF() + brushAdjust; painter.drawConvexPolygon(poly); @@ -215,6 +222,11 @@ void TabletCanvas::paintPixmap(QPainter &painter, QTabletEvent *event) } //! [5] +qreal TabletCanvas::pressureToWidth(qreal pressure) +{ + return pressure * 10 + 1; +} + //! [7] void TabletCanvas::updateBrush(const QTabletEvent *event) { @@ -260,7 +272,7 @@ void TabletCanvas::updateBrush(const QTabletEvent *event) //! [9] //! [10] switch (m_lineWidthValuator) { case PressureValuator: - m_pen.setWidthF(event->pressure() * 10 + 1); + m_pen.setWidthF(pressureToWidth(event->pressure())); break; case TiltValuator: m_pen.setWidthF(maximum(abs(vValue - 127), abs(hValue - 127)) / 12); @@ -305,7 +317,7 @@ void TabletCanvas::updateCursor(const QTabletEvent *event) QPainter painter(&img); QTransform transform = painter.transform(); transform.translate(16, 16); - transform.rotate(-event->rotation()); + transform.rotate(event->rotation()); painter.setTransform(transform); painter.setCompositionMode(QPainter::CompositionMode_DestinationIn); painter.drawImage(-24, -24, origImg); diff --git a/examples/widgets/widgets/tablet/tabletcanvas.h b/examples/widgets/widgets/tablet/tabletcanvas.h index 1784e05916..a1b31c65bf 100644 --- a/examples/widgets/widgets/tablet/tabletcanvas.h +++ b/examples/widgets/widgets/tablet/tabletcanvas.h @@ -103,6 +103,7 @@ private: void initPixmap(); void paintPixmap(QPainter &painter, QTabletEvent *event); Qt::BrushStyle brushPattern(qreal value); + qreal pressureToWidth(qreal pressure); void updateBrush(const QTabletEvent *event); void updateCursor(const QTabletEvent *event); @@ -117,6 +118,7 @@ private: struct Point { QPointF pos; + qreal pressure; qreal rotation; } lastPoint; }; |