Merge branch 'qt-fixes'

Improvements to wxQt, including the addition of gesture events support.

See https://github.com/wxWidgets/wxWidgets/pull/2195

Closes #19042.
This commit is contained in:
Vadim Zeitlin
2021-01-27 19:06:43 +01:00
10 changed files with 360 additions and 15 deletions

View File

@@ -24,9 +24,6 @@ public:
virtual bool SetCurrent(const wxGLCanvas& win) const wxOVERRIDE;
private:
QGLContext *m_glContext;
wxDECLARE_CLASS(wxGLContext);
};
@@ -57,6 +54,8 @@ public:
const wxString& name = wxGLCanvasName,
const wxPalette& palette = wxNullPalette);
~wxGLCanvas();
bool Create(wxWindow *parent,
const wxGLAttributes& dispAttrs,
wxWindowID id = wxID_ANY,

View File

@@ -40,6 +40,7 @@ public:
virtual QAction *GetHandle() const;
virtual void SetFont(const wxFont& font);
private:
// Qt is using an action instead of a menu item.
wxQtAction *m_qtAction;

View File

@@ -19,6 +19,9 @@
#include "wx/qt/private/converter.h"
#include "wx/qt/private/utils.h"
#include <QtWidgets/QGestureEvent>
#include <QtGui/QCursor>
class QPaintEvent;
template< typename Handler >
@@ -327,6 +330,132 @@ protected:
virtual bool winEvent ( MSG * message, long * result ) { }
virtual bool x11Event ( XEvent * event ) { } */
virtual bool event(QEvent *event)
{
if (event->type() == QEvent::Gesture)
{
return gestureEvent(static_cast<QGestureEvent*>(event), event);
}
return Widget::event(event);
}
bool gestureEvent(QGestureEvent *gesture, QEvent *event)
{
if (QGesture *tah = gesture->gesture(Qt::TapAndHoldGesture))
{
// Set the policy so that accepted gestures are taken by the first window that gets them
tah->setGestureCancelPolicy ( QGesture::CancelAllInContext );
tapandholdTriggered(static_cast<QTapAndHoldGesture *>(tah), event);
}
if (QGesture *pan = gesture->gesture(Qt::PanGesture))
{
panTriggered(static_cast<QPanGesture *>(pan), event);
}
if (QGesture *pinch = gesture->gesture(Qt::PinchGesture))
{
pinchTriggered(static_cast<QPinchGesture *>(pinch), event);
}
return true;
}
void tapandholdTriggered(QTapAndHoldGesture *gesture, QEvent *event)
{
wxWindow *win = wxWindow::QtRetrieveWindowPointer( this );
if (gesture->state() == Qt::GestureFinished)
{
if ( win )
{
wxLongPressEvent ev(win->GetId());
ev.SetPosition( wxQtConvertPoint( gesture->position().toPoint() ) );
ev.SetGestureEnd();
win->ProcessWindowEvent( ev );
event->accept();
}
}
else if (gesture->state() == Qt::GestureStarted)
{
event->accept();
}
else
{
event->accept();
}
}
void panTriggered(QPanGesture *gesture, QEvent *event)
{
wxWindow *win = wxWindow::QtRetrieveWindowPointer( this );
if (win)
{
wxPanGestureEvent evp(win->GetId());
QPoint pos = QCursor::pos();
evp.SetPosition( wxQtConvertPoint( pos ) );
QPoint offset = gesture->offset().toPoint();
QPoint offset_last = gesture->lastOffset().toPoint();
QPoint delta(offset.x() - offset_last.x(), offset.y() - offset_last.y());
evp.SetDelta( wxQtConvertPoint( delta ) );
switch(gesture->state())
{
case Qt::GestureStarted:
evp.SetGestureStart();
break;
case Qt::GestureFinished:
case Qt::GestureCanceled:
evp.SetGestureEnd();
break;
default:
break;
}
win->ProcessWindowEvent( evp );
event->accept();
}
}
void pinchTriggered(QPinchGesture *gesture, QEvent *event)
{
wxWindow *win = wxWindow::QtRetrieveWindowPointer( this );
if (win)
{
qreal this_sf = gesture->scaleFactor();
QPoint center_point = gesture->centerPoint().toPoint();
wxZoomGestureEvent evp(win->GetId());
evp.SetPosition( wxQtConvertPoint( center_point ) );
evp.SetZoomFactor( this_sf);
switch(gesture->state())
{
case Qt::GestureStarted:
evp.SetGestureStart();
break;
case Qt::GestureFinished:
case Qt::GestureCanceled:
evp.SetGestureEnd();
break;
default:
break;
}
win->ProcessWindowEvent( evp );
event->accept();
}
}
};
#endif

View File

@@ -221,6 +221,7 @@ protected:
// itself.
virtual QWidget* QtGetParentWidget() const { return GetHandle(); }
virtual bool EnableTouchEvents(int eventsMask) wxOVERRIDE;
QWidget *m_qtWindow;

View File

@@ -1897,8 +1897,6 @@ wxString GetDateFormatFromLangInfo(wxLocaleInfo index)
/* static */
wxString wxLocale::GetInfo(wxLocaleInfo index, wxLocaleCategory cat)
{
// TODO: as of 2014 Android doesn't has complete locale support (use java api)
#if !(defined(__WXQT__) && defined(__ANDROID__))
lconv * const lc = localeconv();
if ( !lc )
return wxString();
@@ -1940,7 +1938,7 @@ wxString wxLocale::GetInfo(wxLocaleInfo index, wxLocaleCategory cat)
default:
wxFAIL_MSG( "unknown wxLocaleInfo value" );
}
#endif
return wxString();
}

View File

@@ -106,11 +106,6 @@ wxIMPLEMENT_DYNAMIC_CLASS(wxUNIXaddress, wxSockAddress);
#endif
#endif // __WINDOWS__
// we assume that we have gethostbyaddr_r() if and only if we have
// gethostbyname_r() and that it uses the similar conventions to it (see
// comment in configure)
#define HAVE_GETHOSTBYADDR HAVE_GETHOSTBYNAME
#ifdef __ANDROID__
#ifndef HAVE_GETHOSTBYNAME
#define HAVE_GETHOSTBYNAME
@@ -125,7 +120,13 @@ wxIMPLEMENT_DYNAMIC_CLASS(wxUNIXaddress, wxSockAddress);
#ifdef HAVE_FUNC_GETHOSTBYNAME_R_6
#define HAVE_FUNC_GETHOSTBYADDR_R_6
#endif
#endif
#endif // __ANDROID__/!__ANDROID__
// we assume that we have gethostbyaddr_r() if and only if we have
// gethostbyname_r() and that it uses the similar conventions to it (see
// comment in configure)
#define HAVE_GETHOSTBYADDR HAVE_GETHOSTBYNAME
// the _r functions need the extra buffer parameter but unfortunately its type
// differs between different systems and for the systems which use opaque
// structs for it (at least AIX and OpenBSD) it must be zero-filled before

View File

@@ -1256,7 +1256,7 @@ int wxVsscanf(const wxCStrData& str, const wchar_t *format, va_list ap)
#define ANDROID_WCSTO_END \
if(endptr) { \
if(dstendp) \
*endptr = (wchar_t*)(nptr + (dstendp - dst) * sizeof(wchar_t)); \
*endptr = (wchar_t*)(nptr + (dstendp - dst)); \
else \
*endptr = NULL; \
} \

View File

@@ -13,6 +13,8 @@
#include "wx/glcanvas.h"
#include <QtOpenGL/QGLWidget>
#include <QtWidgets/QGestureRecognizer>
#include <QtWidgets/QGestureEvent>
#if defined(__VISUALC__)
#pragma message("OpenGL support is not implemented in wxQt")
@@ -338,7 +340,6 @@ wxIMPLEMENT_CLASS(wxGLContext, wxWindow);
wxGLContext::wxGLContext(wxGLCanvas *WXUNUSED(win), const wxGLContext* WXUNUSED(other), const wxGLContextAttrs *WXUNUSED(ctxAttrs))
{
// m_glContext = win->GetHandle()->context();
}
bool wxGLContext::SetCurrent(const wxGLCanvas&) const
@@ -348,6 +349,29 @@ bool wxGLContext::SetCurrent(const wxGLCanvas&) const
return false;
}
//---------------------------------------------------------------------------
// PanGestureRecognizer - helper class for wxGLCanvas
//---------------------------------------------------------------------------
class PanGestureRecognizer : public QGestureRecognizer
{
private:
static const int MINIMUM_DISTANCE = 10;
typedef QGestureRecognizer parent;
bool IsValidMove(int dx, int dy);
virtual QGesture* create(QObject* pTarget);
virtual QGestureRecognizer::Result recognize(QGesture* pGesture, QObject *pWatched, QEvent *pEvent);
void reset (QGesture *pGesture);
QPointF m_startPoint;
QPointF m_lastPoint;
};
//---------------------------------------------------------------------------
// wxGlCanvas
//---------------------------------------------------------------------------
@@ -378,6 +402,12 @@ wxGLCanvas::wxGLCanvas(wxWindow *parent,
Create(parent, id, pos, size, style, name, attribList, palette);
}
wxGLCanvas::~wxGLCanvas()
{
// Avoid sending further signals (i.e. if deleting the current page)
m_qtWindow->blockSignals(true);
}
bool wxGLCanvas::Create(wxWindow *parent,
const wxGLAttributes& dispAttrs,
wxWindowID id,
@@ -411,6 +441,10 @@ bool wxGLCanvas::Create(wxWindow *parent,
m_qtWindow = new wxQtGLWidget(parent, this, format);
// Create and register a custom pan recognizer, available to all instances of this class.
QGestureRecognizer* pPanRecognizer = new PanGestureRecognizer();
QGestureRecognizer::registerRecognizer(pPanRecognizer);
return wxWindow::Create( parent, id, pos, size, style, name );
}
@@ -425,7 +459,6 @@ bool wxGLCanvas::ConvertWXAttrsToQtGL(const int *wxattrs, QGLFormat &format)
{
if (!wxattrs)
return true;
return true;
// set default parameters to false
format.setDoubleBuffer(false);
@@ -513,6 +546,10 @@ bool wxGLCanvas::ConvertWXAttrsToQtGL(const int *wxattrs, QGLFormat &format)
// can we somehow indicate if it's not supported?
break;
case WX_GL_MAJOR_VERSION:
format.setVersion ( v,0 );
break;
default:
wxLogDebug(wxT("Unsupported OpenGL attribute %d"),
wxattrs[arg]);
@@ -559,4 +596,123 @@ bool wxGLApp::InitGLVisual(const int *attribList)
return false;
}
// -----------------------------------------------------------------------------------------
// We want a private pan gesture recognizer for GL canvas,
// since the Qt standard recognizers do not function well for this window.
// -----------------------------------------------------------------------------------------
bool
PanGestureRecognizer::IsValidMove(int dx, int dy)
{
// The moved distance is to small to count as not just a glitch.
if ((qAbs(dx) < MINIMUM_DISTANCE) && (qAbs(dy) < MINIMUM_DISTANCE))
return false;
return true;
}
// virtual
QGesture*
PanGestureRecognizer::create(QObject* pTarget)
{
return new QPanGesture(pTarget);
}
// virtual
QGestureRecognizer::Result
PanGestureRecognizer::recognize(QGesture* pGesture, QObject *pWatched, QEvent *pEvent)
{
wxUnusedVar(pWatched);
QGestureRecognizer::Result result = QGestureRecognizer::Ignore;
QPanGesture *pPan = static_cast<QPanGesture*>(pGesture);
const QTouchEvent *ev = static_cast<const QTouchEvent *>(pEvent);
switch (pEvent->type())
{
case QEvent::TouchBegin:
{
QTouchEvent::TouchPoint p1 = ev->touchPoints().at(0);
m_startPoint = p1.startScreenPos().toPoint();
m_lastPoint = m_startPoint;
pPan->setLastOffset(QPointF(0,0));
pPan->setOffset(QPointF(0,0));
result = QGestureRecognizer::MayBeGesture;
}
break;
case QEvent::TouchEnd:
{
QTouchEvent::TouchPoint p1 = ev->touchPoints().at(0);
QPointF endPoint = p1.screenPos().toPoint();
pPan->setLastOffset(pPan->offset());
pPan->setOffset(QPointF(p1.pos().x() - p1.startPos().x(),
p1.pos().y() - p1.startPos().y()));
pPan->setHotSpot(p1.startScreenPos());
// process distance and direction
int dx = endPoint.x() - m_startPoint.x();
int dy = endPoint.y() - m_startPoint.y();
if (!IsValidMove(dx, dy))
{
// Just a click, so no gesture.
result = QGestureRecognizer::Ignore;
}
else
{
result = QGestureRecognizer::FinishGesture;
}
}
break;
case QEvent::TouchUpdate:
{
QTouchEvent::TouchPoint p1 = ev->touchPoints().at(0);
QPointF upPoint = p1.screenPos().toPoint();
pPan->setLastOffset(pPan->offset());
pPan->setOffset(QPointF(p1.pos().x() - p1.startPos().x(),
p1.pos().y() - p1.startPos().y()));
pPan->setHotSpot(p1.startScreenPos());
int dx = upPoint.x() - m_lastPoint.x();
int dy = upPoint.y() - m_lastPoint.y();
if( (dx > 2) || (dx < -2) || (dy > 2) || (dy < -2))
{
result = QGestureRecognizer::TriggerGesture;
}
else
{
result = QGestureRecognizer::Ignore;
}
m_lastPoint = upPoint;
}
break;
default:
break;
}
return result;
}
void
PanGestureRecognizer::reset(QGesture *pGesture)
{
pGesture->setProperty("startPoint", QVariant(QVariant::Invalid));
parent::reset(pGesture);
}
#endif // wxUSE_GLCANVAS

View File

@@ -128,6 +128,11 @@ void wxMenuItem::SetBitmap(const wxBitmap& bitmap)
}
}
void wxMenuItem::SetFont(const wxFont& font)
{
m_qtAction->setFont(font.GetHandle());
}
QAction *wxMenuItem::GetHandle() const
{
return m_qtAction;

View File

@@ -85,6 +85,28 @@ bool wxQtScrollArea::event(QEvent *e)
break;
}
}
// QGesture events arrive without mouse capture
else if ( handler )
{
switch ( e->type() )
{
case QEvent::Gesture:
{
QScrollArea::event(e);
if ( QScrollBar *vBar = verticalScrollBar() )
vBar->triggerAction( QAbstractSlider::SliderMove );
if ( QScrollBar *hBar = horizontalScrollBar() )
hBar->triggerAction( QAbstractSlider::SliderMove );
return true;
}
default:
break;
}
}
return QScrollArea::event(e);
}
@@ -309,6 +331,9 @@ wxWindowQt::~wxWindowQt()
DestroyChildren(); // This also destroys scrollbars
if (m_qtWindow)
QtStoreWindowPointer( GetHandle(), NULL );
#if wxUSE_DRAG_AND_DROP
SetDropTarget(NULL);
#endif
@@ -1680,3 +1705,33 @@ QPainter *wxWindowQt::QtGetPainter()
{
return m_qtPainter.get();
}
bool wxWindowQt::EnableTouchEvents(int eventsMask)
{
wxCHECK_MSG( GetHandle(), false, "can't be called before creating the window" );
if ( eventsMask == wxTOUCH_NONE )
{
m_qtWindow->setAttribute(Qt::WA_AcceptTouchEvents, false);
return true;
}
if ( eventsMask & wxTOUCH_PRESS_GESTURES )
{
m_qtWindow->setAttribute(Qt::WA_AcceptTouchEvents, true);
m_qtWindow->grabGesture(Qt::TapAndHoldGesture);
QTapAndHoldGesture::setTimeout ( 1000 );
}
if ( eventsMask & wxTOUCH_PAN_GESTURES )
{
m_qtWindow->setAttribute(Qt::WA_AcceptTouchEvents, true);
m_qtWindow->grabGesture(Qt::PanGesture);
}
if ( eventsMask & wxTOUCH_ZOOM_GESTURE )
{
m_qtWindow->setAttribute(Qt::WA_AcceptTouchEvents, true);
m_qtWindow->grabGesture(Qt::PinchGesture);
}
return true;
}