summaryrefslogtreecommitdiffstats
path: root/examples/corelib/threads/mandelbrot/mandelbrotwidget.cpp
diff options
context:
space:
mode:
authorNicholas Bennett <nicholas.bennett@qt.io>2022-05-25 11:03:05 +0300
committerRami Potinkara <rami.potinkara@qt.io>2022-07-21 08:38:44 +0300
commitfc5482cac6e69364c9d09fb8d994065e2a5fb542 (patch)
tree9966a986452209566b4f5ff5fd13230dded51157 /examples/corelib/threads/mandelbrot/mandelbrotwidget.cpp
parenteb9ace1cee4bb48005797b0b2d2d3d576a4cb4ce (diff)
Support pinch zoom gesture in the Mandelbrot example
Used QGesture for this as per the gesture example. Moved the help and info text to separate lines. Enabled word wrap to prevent text going off screen. As a drive-by, the code to center the window at a size that's a fraction of the screen is replaced with a simple sizeHint() override. The user should have control over placement, particularly now that it should be placed manually onto a touchscreen if the user has one. Done-with: Shawn Rutledge Fixes: QTBUG-96702 Pick-to: 6.4 Change-Id: I8dba8b09bed474f585341e9a7a8c71fb60293eee Reviewed-by: Rami Potinkara <rami.potinkara@qt.io> Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io>
Diffstat (limited to 'examples/corelib/threads/mandelbrot/mandelbrotwidget.cpp')
-rw-r--r--examples/corelib/threads/mandelbrot/mandelbrotwidget.cpp55
1 files changed, 45 insertions, 10 deletions
diff --git a/examples/corelib/threads/mandelbrot/mandelbrotwidget.cpp b/examples/corelib/threads/mandelbrot/mandelbrotwidget.cpp
index 9ab8936c85..e0f33a2b0b 100644
--- a/examples/corelib/threads/mandelbrot/mandelbrotwidget.cpp
+++ b/examples/corelib/threads/mandelbrot/mandelbrotwidget.cpp
@@ -3,8 +3,9 @@
#include "mandelbrotwidget.h"
-#include <QPainter>
+#include <QGesture>
#include <QKeyEvent>
+#include <QPainter>
#include <math.h>
@@ -26,8 +27,7 @@ MandelbrotWidget::MandelbrotWidget(QWidget *parent) :
pixmapScale(DefaultScale),
curScale(DefaultScale)
{
- help = tr("Use mouse wheel or the '+' and '-' keys to zoom. "
- "Press and hold left mouse button to scroll.");
+ help = tr("Zoom with mouse wheel, +/- keys or pinch. Scroll with arrow keys or by dragging.");
connect(&thread, &RenderThread::renderedImage,
this, &MandelbrotWidget::updatePixmap);
@@ -46,7 +46,7 @@ void MandelbrotWidget::paintEvent(QPaintEvent * /* event */)
if (pixmap.isNull()) {
painter.setPen(Qt::white);
- painter.drawText(rect(), Qt::AlignCenter, tr("Rendering initial image, please wait..."));
+ painter.drawText(rect(), Qt::AlignCenter|Qt::TextWordWrap, tr("Rendering initial image, please wait..."));
//! [2] //! [3]
return;
//! [3] //! [4]
@@ -80,17 +80,31 @@ void MandelbrotWidget::paintEvent(QPaintEvent * /* event */)
}
//! [8] //! [9]
- QString text = help;
- if (!info.isEmpty())
- text += ' ' + info;
QFontMetrics metrics = painter.fontMetrics();
- int textWidth = metrics.horizontalAdvance(text);
+ if (!info.isEmpty()){
+ int infoWidth = metrics.horizontalAdvance(info);
+ int infoHeight = metrics.height();
+
+ painter.setPen(Qt::NoPen);
+ painter.setBrush(QColor(0, 0, 0, 127));
+ infoHeight = (infoWidth/width()+1) * (infoHeight + 5);
+ painter.drawRect((width() - infoWidth) / 2 - 5, 0, infoWidth + 10, infoHeight);
+
+ painter.setPen(Qt::white);
+ painter.drawText(rect(), Qt::AlignHCenter|Qt::AlignTop|Qt::TextWordWrap, info);
+ }
+
+ int helpWidth = metrics.horizontalAdvance(help);
+ int helpHeight = metrics.height();
painter.setPen(Qt::NoPen);
painter.setBrush(QColor(0, 0, 0, 127));
- painter.drawRect((width() - textWidth) / 2 - 5, 0, textWidth + 10, metrics.lineSpacing() + 5);
+ helpHeight = (helpWidth/width()+1) * (helpHeight + 5);
+ painter.drawRect((width() - helpWidth) / 2 - 5, height()-helpHeight, helpWidth + 10, helpHeight);
+
painter.setPen(Qt::white);
- painter.drawText((width() - textWidth) / 2, metrics.leading() + metrics.ascent(), text);
+ painter.drawText(rect(), Qt::AlignHCenter|Qt::AlignBottom|Qt::TextWordWrap, help);
+
}
//! [9]
@@ -211,3 +225,24 @@ void MandelbrotWidget::scroll(int deltaX, int deltaY)
thread.render(centerX, centerY, curScale, size(), devicePixelRatio());
}
//! [18]
+
+//! [gesture1]
+#ifndef QT_NO_GESTURES
+bool MandelbrotWidget::gestureEvent(QGestureEvent *event)
+{
+ if (auto *pinch = static_cast<QPinchGesture *>(event->gesture(Qt::PinchGesture))) {
+ if (pinch->changeFlags().testFlag(QPinchGesture::ScaleFactorChanged))
+ zoom(1.0 / pinch->scaleFactor());
+ return true;
+ }
+ return false;
+}
+
+bool MandelbrotWidget::event(QEvent *event)
+{
+ if (event->type() == QEvent::Gesture)
+ return gestureEvent(static_cast<QGestureEvent*>(event));
+ return QWidget::event(event);
+}
+#endif
+//! [gesture1]