66#include < QApplication>
77#include < QClipboard>
88#include < QFileDialog>
9+ #include < QEvent>
910#include < QMenu>
1011#include < QMessageBox>
1112#include < QMouseEvent>
@@ -42,6 +43,7 @@ namespace {
4243
4344QMareTextureWidget::QMareTextureWidget (QWidget* parent) : QWidget{parent} {
4445 this ->setContextMenuPolicy (Qt::CustomContextMenu);
46+ this ->setAttribute (Qt::WA_AcceptTouchEvents);
4547
4648 auto * contextMenu = new QMenu{this };
4749
@@ -231,7 +233,7 @@ uint8_t QMareTextureWidget::getCurrentMip() const {
231233}
232234
233235void QMareTextureWidget::setCurrentMip (uint8_t mip) {
234- this ->currentMip = std::clamp <uint8_t >(mip, 0 , this ->vtf .getMipCount () - 1 );
236+ this ->currentMip = qBound <uint8_t >(0 , mip , this ->vtf .getMipCount () - 1 );
235237 this ->reloadCurrentTexture ();
236238}
237239
@@ -240,7 +242,7 @@ uint16_t QMareTextureWidget::getCurrentFrame() const {
240242}
241243
242244void QMareTextureWidget::setCurrentFrame (uint16_t frame) {
243- this ->currentFrame = std::clamp <uint16_t >(frame, 0 , this ->vtf .getFrameCount () - 1 );
245+ this ->currentFrame = qBound <uint16_t >(0 , frame , this ->vtf .getFrameCount () - 1 );
244246 this ->reloadCurrentTexture ();
245247}
246248
@@ -249,7 +251,7 @@ uint8_t QMareTextureWidget::getCurrentFace() const {
249251}
250252
251253void QMareTextureWidget::setCurrentFace (uint8_t face) {
252- this ->currentFace = std::clamp <uint8_t >(face, 0 , this ->vtf .getFaceCount () - 1 );
254+ this ->currentFace = qBound <uint8_t >(0 , face , this ->vtf .getFaceCount () - 1 );
253255 this ->reloadCurrentTexture ();
254256}
255257
@@ -258,7 +260,7 @@ uint16_t QMareTextureWidget::getCurrentDepth() const {
258260}
259261
260262void QMareTextureWidget::setCurrentDepth (uint16_t depth) {
261- this ->currentDepth = std::clamp <uint16_t >(depth, 0 , this ->vtf .getDepth () - 1 );
263+ this ->currentDepth = qBound <uint16_t >(0 , depth , this ->vtf .getDepth () - 1 );
262264 this ->reloadCurrentTexture ();
263265}
264266
@@ -320,6 +322,31 @@ QMareTextureWidget::operator bool() const {
320322 return !this ->path .isEmpty () && !this ->textureCurrent .isNull ();
321323}
322324
325+ bool QMareTextureWidget::event (QEvent* e) {
326+ if (
327+ QTouchEvent* touch;
328+ (e->type () == QEvent::TouchBegin || e->type () == QEvent::TouchUpdate || e->type () == QEvent::TouchEnd) && ((touch = dynamic_cast <QTouchEvent*>(e)))
329+ ) {
330+ if (const auto & points = touch->points (); points.length () >= 2 ) {
331+ const auto point0 = points[0 ].position ();
332+ const auto point1 = points[1 ].position ();
333+ const auto rawDistance = static_cast <float >(qSqrt (qPow (point1.x () - point0.x (), 2 ) + qPow (point1.y () - point0.y (), 2 )));
334+
335+ if (e->type () == QEvent::TouchBegin) {
336+ this ->previousDistance = rawDistance;
337+ }
338+ const auto distance = qBound (0 .5f , rawDistance / this ->previousDistance , 5 .f );
339+ this ->previousDistance = rawDistance;
340+ this ->textureZoom *= distance;
341+
342+ this ->update ();
343+ e->accept ();
344+ return true ;
345+ }
346+ }
347+ return this ->QWidget ::event (e);
348+ }
349+
323350void QMareTextureWidget::mouseMoveEvent (QMouseEvent* e) {
324351 if (!(e->buttons () & Qt::LeftButton || e->buttons () & Qt::MiddleButton)) {
325352 return ;
0 commit comments