Qt 4.8
Public Functions | Protected Functions | Properties | List of all members
QWSCalibratedMouseHandler Class Reference

The QWSCalibratedMouseHandler class provides mouse calibration and noise reduction in Qt for Embedded Linux. More...

#include <qmouse_qws.h>

Inheritance diagram for QWSCalibratedMouseHandler:
QWSMouseHandler QIntMouseHandler QWSLinuxInputMouseHandler QWSLinuxTPMouseHandler QWSTslibMouseHandler

Public Functions

virtual void calibrate (const QWSPointerCalibrationData *)
 Updates the calibration parameters based on coordinate mapping of the given data. More...
 
virtual void clearCalibration ()
 Clears the current calibration, i.e., makes the mouse driver return mouse events in raw device coordinates instead of screen coordinates. More...
 
virtual void getCalibration (QWSPointerCalibrationData *) const
 Fills cd with the device coordinates corresponding to the given screen coordinates. More...
 
 QWSCalibratedMouseHandler (const QString &driver=QString(), const QString &device=QString())
 
- Public Functions inherited from QWSMouseHandler
void limitToScreen (QPoint &pt)
 Ensures that the given position is within the screen's boundaries, changing the position if necessary. More...
 
void mouseChanged (const QPoint &pos, int bstate, int wheel=0)
 Notifies the system of a new mouse event. More...
 
const QPointpos () const
 Returns the current mouse position. More...
 
 QWSMouseHandler (const QString &driver=QString(), const QString &device=QString())
 Constructs a mouse driver. More...
 
virtual void resume ()=0
 Implement this function to resume reading and handling mouse events, e. More...
 
void setScreen (const QScreen *screen)
 Sets the screen for this mouse driver to be the given screen. More...
 
virtual void suspend ()=0
 Implement this function to suspend reading and handling of mouse events, e. More...
 
virtual ~QWSMouseHandler ()
 Destroys this mouse driver. More...
 

Protected Functions

void readCalibration ()
 Reads previously written calibration parameters which are stored in /etc/pointercal (separated by whitespace and in alphabetical order). More...
 
bool sendFiltered (const QPoint &, int button)
 Notifies the system of a new mouse event after applying a noise reduction filter. More...
 
void setFilterSize (int)
 Sets the size of the filter used in noise reduction to the given size. More...
 
QPoint transform (const QPoint &)
 Transforms the given position from device coordinates to screen coordinates, and returns the transformed position. More...
 
void writeCalibration ()
 Saves the current calibration parameters in /etc/pointercal (separated by whitespace and in alphabetical order). More...
 

Properties

int a
 
int b
 
int c
 
int currSample
 
int d
 
int e
 
int f
 
int numSamples
 
int s
 
QPolygon samples
 

Additional Inherited Members

- Protected Variables inherited from QWSMouseHandler
QWSMouseHandlerPrivated_ptr
 
QPointmousePos
 

Detailed Description

The QWSCalibratedMouseHandler class provides mouse calibration and noise reduction in Qt for Embedded Linux.

Note that this class is only available in Qt for Embedded Linux.

Qt for Embedded Linux provides ready-made drivers for several mouse protocols, see the Qt for Embedded Linux Pointer Handling{pointer handling} documentation for details. In general, custom mouse drivers can be implemented by subclassing the QWSMouseHandler class. But when the system device does not have a fixed mapping between device and screen coordinates and/or produces noisy events (e.g., a touchscreen), you should derive from the QWSCalibratedMouseHandler class instead to take advantage of its calibration functionality. As always, you must also create a mouse driver plugin (derived from QMouseDriverPlugin); the implementation of the QMouseDriverFactory class will then automatically detect the plugin, and load the driver into the server application at run-time using Qt's How to Create Qt Plugins{plugin system}.

QWSCalibratedMouseHandler provides an implementation of the calibrate() function to update the calibration parameters based on coordinate mapping of the given calibration data. The calibration data is represented by an QWSPointerCalibrationData object. The linear transformation between device coordinates and screen coordinates is performed by calling the transform() function explicitly on the points passed to the QWSMouseHandler::mouseChanged() function. Use the clearCalibration() function to make the mouse driver return mouse events in raw device coordinates and not in screen coordinates.

The calibration parameters are recalculated whenever calibrate() is called, and they can be stored using the writeCalibration() function. Previously written parameters can be retrieved at any time using the readCalibration() function (calibration parameters are always read when the class is instantiated). Note that the calibration parameters is written to and read from the file currently specified by the POINTERCAL_FILE environment variable; the default file is /etc/pointercal.

To achieve noise reduction, QWSCalibratedMouseHandler provides the sendFiltered() function. Use this function instead of mouseChanged() whenever a mouse event occurs. The filter's size can be manipulated using the setFilterSize() function.

See also
QWSMouseHandler, QWSPointerCalibrationData, {Mouse Calibration Example}

Definition at line 92 of file qmouse_qws.h.

Constructors and Destructors

◆ QWSCalibratedMouseHandler()

QWSCalibratedMouseHandler::QWSCalibratedMouseHandler ( const QString driver = QString(),
const QString device = QString() 
)
explicit
Warning
This function is not part of the public interface.

Definition at line 382 of file qmouse_qws.cpp.

383  : samples(5), currSample(0), numSamples(0)
384 {
386  readCalibration();
387 }
void readCalibration()
Reads previously written calibration parameters which are stored in /etc/pointercal (separated by whi...
Definition: qmouse_qws.cpp:469
virtual void clearCalibration()
Clears the current calibration, i.e., makes the mouse driver return mouse events in raw device coordi...
Definition: qmouse_qws.cpp:416

Functions

◆ calibrate()

void QWSCalibratedMouseHandler::calibrate ( const QWSPointerCalibrationData data)
virtual

Updates the calibration parameters based on coordinate mapping of the given data.

Create an instance of the QWSPointerCalibrationData class, fill in the device and screen coordinates and pass that object to the mouse driver using this function.

See also
clearCalibration(), transform()

Reimplemented from QWSMouseHandler.

Reimplemented in QIntMouseHandler, and QWSTslibMouseHandler.

Definition at line 526 of file qmouse_qws.cpp.

Referenced by QIntMouseHandler::calibrate().

527 {
528  // Algorithm derived from
529  // "How To Calibrate Touch Screens" by Carlos E. Vidales,
530  // printed in Embedded Systems Programming, Vol. 15 no 6, June 2002
531  // URL: http://www.embedded.com/showArticle.jhtml?articleID=9900629
532 
539 
540  const qint64 xd0 = pd0.x();
541  const qint64 xd1 = pd1.x();
542  const qint64 xd2 = pd2.x();
543  const qint64 yd0 = pd0.y();
544  const qint64 yd1 = pd1.y();
545  const qint64 yd2 = pd2.y();
546  const qint64 x0 = p0.x();
547  const qint64 x1 = p1.x();
548  const qint64 x2 = p2.x();
549  const qint64 y0 = p0.y();
550  const qint64 y1 = p1.y();
551  const qint64 y2 = p2.y();
552 
553  qint64 scale = ((xd0 - xd2)*(yd1 - yd2) - (xd1 - xd2)*(yd0 - yd2));
554  int shift = 0;
555  qint64 absScale = qAbs(scale);
556  // use maximum 16 bit precision to reduce risk of integer overflow
557  if (absScale > (1 << 16)) {
558  shift = ilog2(absScale >> 16) + 1;
559  scale >>= shift;
560  }
561 
562  s = scale;
563  a = ((x0 - x2)*(yd1 - yd2) - (x1 - x2)*(yd0 - yd2)) >> shift;
564  b = ((xd0 - xd2)*(x1 - x2) - (x0 - x2)*(xd1 - xd2)) >> shift;
565  c = (yd0*(xd2*x1 - xd1*x2) + yd1*(xd0*x2 - xd2*x0) + yd2*(xd1*x0 - xd0*x1)) >> shift;
566  d = ((y0 - y2)*(yd1 - yd2) - (y1 - y2)*(yd0 - yd2)) >> shift;
567  e = ((xd0 - xd2)*(y1 - y2) - (y0 - y2)*(xd1 - xd2)) >> shift;
568  f = (yd0*(xd2*y1 - xd1*y2) + yd1*(xd0*y2 - xd2*y0) + yd2*(xd1*y0 - xd0*y1)) >> shift;
569 
571 }
static ShiftResult shift(const QBezier *orig, QBezier *shifted, qreal offset, qreal threshold)
Definition: qbezier.cpp:289
QPoint devPoints[5]
the raw device coordinates for each value of the Location enum.
Definition: qmouse_qws.h:62
QPoint screenPoints[5]
the logical screen coordinates for each value of the Location enum.
Definition: qmouse_qws.h:63
Q_DECL_CONSTEXPR T qAbs(const T &t)
Definition: qglobal.h:1201
static int ilog2(quint32 n)
Definition: qmouse_qws.cpp:491
void writeCalibration()
Saves the current calibration parameters in /etc/pointercal (separated by whitespace and in alphabeti...
Definition: qmouse_qws.cpp:437
__int64 qint64
Definition: qglobal.h:942
The QPoint class defines a point in the plane using integer precision.
Definition: qpoint.h:53
int y() const
Returns the y coordinate of this point.
Definition: qpoint.h:131
int x() const
Returns the x coordinate of this point.
Definition: qpoint.h:128

◆ clearCalibration()

void QWSCalibratedMouseHandler::clearCalibration ( )
virtual

Clears the current calibration, i.e., makes the mouse driver return mouse events in raw device coordinates instead of screen coordinates.

See also
calibrate()

Reimplemented from QWSMouseHandler.

Reimplemented in QIntMouseHandler, and QWSTslibMouseHandler.

Definition at line 416 of file qmouse_qws.cpp.

Referenced by QIntMouseHandler::clearCalibration(), QWSCalibratedMouseHandler(), and readCalibration().

417 {
418  a = 1;
419  b = 0;
420  c = 0;
421  d = 0;
422  e = 1;
423  f = 0;
424  s = 1;
425 }

◆ getCalibration()

void QWSCalibratedMouseHandler::getCalibration ( QWSPointerCalibrationData cd) const
virtual

Fills cd with the device coordinates corresponding to the given screen coordinates.

Warning
This function is not part of the public interface.

Reimplemented from QWSMouseHandler.

Definition at line 395 of file qmouse_qws.cpp.

396 {
397  const qint64 scale = qint64(a) * qint64(e) - qint64(b) * qint64(d);
398  const qint64 xOff = qint64(b) * qint64(f) - qint64(c) * qint64(e);
399  const qint64 yOff = qint64(c) * qint64(d) - qint64(a) * qint64(f);
400  for (int i = 0; i <= QWSPointerCalibrationData::LastLocation; ++i) {
401  const qint64 sX = cd->screenPoints[i].x();
402  const qint64 sY = cd->screenPoints[i].y();
403  const qint64 dX = (s*(e*sX - b*sY) + xOff) / scale;
404  const qint64 dY = (s*(a*sY - d*sX) + yOff) / scale;
405  cd->devPoints[i] = QPoint(dX, dY);
406  }
407 }
QPoint devPoints[5]
the raw device coordinates for each value of the Location enum.
Definition: qmouse_qws.h:62
QPoint screenPoints[5]
the logical screen coordinates for each value of the Location enum.
Definition: qmouse_qws.h:63
__int64 qint64
Definition: qglobal.h:942
The QPoint class defines a point in the plane using integer precision.
Definition: qpoint.h:53
int y() const
Returns the y coordinate of this point.
Definition: qpoint.h:131
int x() const
Returns the x coordinate of this point.
Definition: qpoint.h:128

◆ readCalibration()

void QWSCalibratedMouseHandler::readCalibration ( )
protected

Reads previously written calibration parameters which are stored in /etc/pointercal (separated by whitespace and in alphabetical order).

You can override the default /etc/pointercal by specifying another file using the POINTERCAL_FILE environment variable.

See also
writeCalibration()

Definition at line 469 of file qmouse_qws.cpp.

Referenced by QWSCalibratedMouseHandler().

470 {
471  QString calFile = QString::fromLocal8Bit(qgetenv("POINTERCAL_FILE"));
472  if (calFile.isEmpty())
473  calFile = QLatin1String("/etc/pointercal");
474 
475 #ifndef QT_NO_TEXTSTREAM
476  QFile file(calFile);
477  if (file.open(QIODevice::ReadOnly)) {
478  QTextStream t(&file);
479  t >> a >> b >> c >> d >> e >> f >> s;
480  if (s == 0 || t.status() != QTextStream::Ok) {
481  qCritical("Corrupt calibration data");
483  }
484  } else
485 #endif
486  {
487  qDebug() << "Could not read calibration:" <<calFile;
488  }
489 }
Q_CORE_EXPORT QByteArray qgetenv(const char *varName)
static QString fromLocal8Bit(const char *, int size=-1)
Returns a QString initialized with the first size characters of the 8-bit string str.
Definition: qstring.cpp:4245
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QString class provides a Unicode character string.
Definition: qstring.h:83
virtual void clearCalibration()
Clears the current calibration, i.e., makes the mouse driver return mouse events in raw device coordi...
Definition: qmouse_qws.cpp:416
Q_CORE_EXPORT void qDebug(const char *,...)
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
The QFile class provides an interface for reading from and writing to files.
Definition: qfile.h:65
The QTextStream class provides a convenient interface for reading and writing text.
Definition: qtextstream.h:73
Q_CORE_EXPORT void qCritical(const char *,...)

◆ sendFiltered()

bool QWSCalibratedMouseHandler::sendFiltered ( const QPoint position,
int  state 
)
protected

Notifies the system of a new mouse event after applying a noise reduction filter.

Returns true if the filtering process is successful; otherwise returns false. Note that if the filtering process failes, the system is not notified about the event.

The given position is the global position of the mouse. The state parameter is a bitmask of the Qt::MouseButton enum's values indicating which mouse buttons are pressed.

The noice is reduced by calculating an average position from a collection of mouse event positions and then calling the mouseChanged() function with the new position. The number of positions that is used is determined by the filter size.

See also
mouseChanged(), setFilterSize()

Definition at line 639 of file qmouse_qws.cpp.

Referenced by QIntMouseHandler::readMouseData(), and QWSTslibMouseHandlerPrivate::readMouseData().

640 {
641  if (!button) {
642  if (numSamples >= samples.count())
643  mouseChanged(transform(position), 0);
644  currSample = 0;
645  numSamples = 0;
646  return true;
647  }
648 
649  bool sent = false;
651  numSamples++;
652  if (numSamples >= samples.count()) {
653 
654  int ignore = -1;
655  if (samples.count() > 2) { // throw away the "worst" sample
656  int maxd = 0;
657  for (int i = 0; i < samples.count(); i++) {
658  int d = (mousePos - samples[i]).manhattanLength();
659  if (d > maxd) {
660  maxd = d;
661  ignore = i;
662  }
663  }
664  }
665 
666  // average the rest
667  QPoint pos(0, 0);
668  int numAveraged = 0;
669  for (int i = 0; i < samples.count(); i++) {
670  if (ignore == i)
671  continue;
672  pos += samples[i];
673  ++numAveraged;
674  }
675  if (numAveraged)
676  pos /= numAveraged;
677 
678  mouseChanged(transform(pos), button);
679  sent = true;
680  }
681  currSample++;
682  if (currSample >= samples.count())
683  currSample = 0;
684 
685  return sent;
686 }
QPoint transform(const QPoint &)
Transforms the given position from device coordinates to screen coordinates, and returns the transfor...
Definition: qmouse_qws.cpp:589
int count(const T &t) const
Returns the number of occurrences of value in the vector.
Definition: qvector.h:742
static qreal position(QGraphicsObject *item, QDeclarativeAnchorLine::AnchorLine anchorLine)
static bool ignore(const char *test, const char *const *table)
Definition: qaxserver.cpp:660
QPoint & mousePos
Definition: qmouse_qws.h:87
const QPoint & pos() const
Returns the current mouse position.
Definition: qmouse_qws.h:82
void mouseChanged(const QPoint &pos, int bstate, int wheel=0)
Notifies the system of a new mouse event.
Definition: qmouse_qws.cpp:285
The QPoint class defines a point in the plane using integer precision.
Definition: qpoint.h:53

◆ setFilterSize()

void QWSCalibratedMouseHandler::setFilterSize ( int  size)
protected

Sets the size of the filter used in noise reduction to the given size.

The sendFiltered() function reduces noice by calculating an average position from a collection of mouse event positions. The filter size determines the number of positions that forms the basis for these calculations.

See also
sendFiltered()

Definition at line 610 of file qmouse_qws.cpp.

611 {
612  samples.resize(qMax(1, size));
613  numSamples = 0;
614  currSample = 0;
615 }
Q_DECL_CONSTEXPR const T & qMax(const T &a, const T &b)
Definition: qglobal.h:1217
void resize(int size)
Sets the size of the vector to size.
Definition: qvector.h:342

◆ transform()

QPoint QWSCalibratedMouseHandler::transform ( const QPoint position)
protected

Transforms the given position from device coordinates to screen coordinates, and returns the transformed position.

This function is typically called explicitly on the points passed to the QWSMouseHandler::mouseChanged() function.

This implementation is a linear transformation using 7 parameters (a, b, c, d, e, f and s) to transform the device coordinates (Xd, Yd) into screen coordinates (Xs, Ys) using the following equations:

s*Xs = a*Xd + b*Yd + c
s*Ys = d*Xd + e*Yd + f
See also
mouseChanged()

Definition at line 589 of file qmouse_qws.cpp.

Referenced by QIntMouseHandler::QIntMouseHandler(), QWSLinuxInputMousePrivate::readMouseData(), QIntMouseHandler::readMouseData(), QWSLinuxTPMouseHandlerPrivate::readMouseData(), and sendFiltered().

590 {
591  QPoint tp;
592 
593  tp.setX((a * position.x() + b * position.y() + c) / s);
594  tp.setY((d * position.x() + e * position.y() + f) / s);
595 
596  return tp;
597 }
void setY(int y)
Sets the y coordinate of this point to the given y coordinate.
Definition: qpoint.h:137
The QPoint class defines a point in the plane using integer precision.
Definition: qpoint.h:53
int y() const
Returns the y coordinate of this point.
Definition: qpoint.h:131
int x() const
Returns the x coordinate of this point.
Definition: qpoint.h:128
void setX(int x)
Sets the x coordinate of this point to the given x coordinate.
Definition: qpoint.h:134

◆ writeCalibration()

void QWSCalibratedMouseHandler::writeCalibration ( )
protected

Saves the current calibration parameters in /etc/pointercal (separated by whitespace and in alphabetical order).

You can override the default /etc/pointercal by specifying another file using the POINTERCAL_FILE environment variable.

See also
readCalibration()

Definition at line 437 of file qmouse_qws.cpp.

Referenced by calibrate().

438 {
439  QString calFile;
440  calFile = QString::fromLocal8Bit(qgetenv("POINTERCAL_FILE"));
441  if (calFile.isEmpty())
442  calFile = QLatin1String("/etc/pointercal");
443 
444 #ifndef QT_NO_TEXTSTREAM
445  QFile file(calFile);
446  if (file.open(QIODevice::WriteOnly)) {
447  QTextStream t(&file);
448  t << a << ' ' << b << ' ' << c << ' ';
449  t << d << ' ' << e << ' ' << f << ' ' << s << endl;
450  } else
451 #endif
452  {
453  qCritical("QWSCalibratedMouseHandler::writeCalibration: "
454  "Could not save calibration into %s", qPrintable(calFile));
455  }
456 }
Q_CORE_EXPORT QByteArray qgetenv(const char *varName)
static QString fromLocal8Bit(const char *, int size=-1)
Returns a QString initialized with the first size characters of the 8-bit string str.
Definition: qstring.cpp:4245
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QString class provides a Unicode character string.
Definition: qstring.h:83
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
The QFile class provides an interface for reading from and writing to files.
Definition: qfile.h:65
The QTextStream class provides a convenient interface for reading and writing text.
Definition: qtextstream.h:73
#define qPrintable(string)
Definition: qglobal.h:1750
Q_CORE_EXPORT void qCritical(const char *,...)
Q_CORE_EXPORT QTextStream & endl(QTextStream &s)

Properties

◆ a

int QWSCalibratedMouseHandler::a
private

◆ b

int QWSCalibratedMouseHandler::b
private

◆ c

int QWSCalibratedMouseHandler::c
private

◆ currSample

int QWSCalibratedMouseHandler::currSample
private

Definition at line 115 of file qmouse_qws.h.

Referenced by sendFiltered(), and setFilterSize().

◆ d

int QWSCalibratedMouseHandler::d
private

◆ e

int QWSCalibratedMouseHandler::e
private

◆ f

int QWSCalibratedMouseHandler::f
private

◆ numSamples

int QWSCalibratedMouseHandler::numSamples
private

Definition at line 116 of file qmouse_qws.h.

Referenced by sendFiltered(), and setFilterSize().

◆ s

int QWSCalibratedMouseHandler::s
private

◆ samples

QPolygon QWSCalibratedMouseHandler::samples
private

Definition at line 114 of file qmouse_qws.h.

Referenced by sendFiltered(), and setFilterSize().


The documentation for this class was generated from the following files: