qmouse_qws.cpp
来自「QT 开发环境里面一个很重要的文件」· C++ 代码 · 共 577 行 · 第 1/2 页
CPP
577 行
transformation between device coordinates and screen coordinates is performed by calling the transform() function explicitly on the points passed to the QWSMouseHandler::mouseChanged() function. The calibration parameters are recalculated whenever calibrate() is called. They can be saved using the writeCalibration() function, and are stored in \c /etc/pointercal (separated by whitespace and in alphabetical order). The calibration parameters are read when the class is instantiated, but previously written parameters can be retrieved at any time using the readCalibration() function. Use the clearCalibration() function to make the mouse handler return mouse events in raw device coordinates and not in screen coordinates. 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. \sa QWSMouseHandler, QWSPointerCalibrationData, {qtopiacore/mousecalibration}{Mouse Calibration Example}*//*! \internal */QWSCalibratedMouseHandler::QWSCalibratedMouseHandler(const QString &, const QString &) : samples(5), currSample(0), numSamples(0){ clearCalibration(); readCalibration();}/*! \internal*/void QWSCalibratedMouseHandler::getCalibration(QWSPointerCalibrationData *cd) const{ QPoint screen_tl = cd->screenPoints[QWSPointerCalibrationData::TopLeft]; QPoint screen_br = cd->screenPoints[QWSPointerCalibrationData::BottomRight]; int tlx = (s * screen_tl.x() - c) / a; int tly = (s * screen_tl.y() - f) / e; cd->devPoints[QWSPointerCalibrationData::TopLeft] = QPoint(tlx,tly); cd->devPoints[QWSPointerCalibrationData::BottomRight] = QPoint(tlx - (s * (screen_tl.x() - screen_br.x()) / a), tly - (s * (screen_tl.y() - screen_br.y()) / e));}/*! Clears the current calibration, i.e. makes the mouse handler return mouse events in raw device coordinates instead of screen coordinates. \sa calibrate()*/void QWSCalibratedMouseHandler::clearCalibration(){ a = 1; b = 0; c = 0; d = 0; e = 1; f = 0; s = 1;}/*! Saves the current calibration parameters in \c /etc/pointercal (separated by whitespace and in alphabetical order). You can override the default /etc/pointercal by defining an environment variable POINTERCAL_FILE with a different file to use. \sa readCalibration()*/void QWSCalibratedMouseHandler::writeCalibration(){ QString calFile; calFile = QString::fromLocal8Bit(qgetenv("POINTERCAL_FILE")); if (calFile.isEmpty()) calFile = QLatin1String("/etc/pointercal");#ifndef QT_NO_TEXTSTREAM QFile file(calFile); if (file.open(QIODevice::WriteOnly)) { QTextStream t(&file); t << a << " " << b << " " << c << " "; t << d << " " << e << " " << f << " " << s; } else#endif { qCritical("QWSCalibratedMouseHandler::writeCalibration: " "Could not save calibration into %s", qPrintable(calFile)); }}/*! Reads previously written calibration parameters which are stored in \c /etc/pointercal (separated by whitespace and in alphabetical order). \sa writeCalibration()*/void QWSCalibratedMouseHandler::readCalibration(){ QString calFile = QString::fromLocal8Bit(qgetenv("POINTERCAL_FILE")); if (calFile.isEmpty()) calFile = QLatin1String("/etc/pointercal");#ifndef QT_NO_TEXTSTREAM QFile file(calFile); if (file.open(QIODevice::ReadOnly)) { QTextStream t(&file); t >> a >> b >> c >> d >> e >> f >> s; } else#endif { qDebug() << "Could not read calibration:" <<calFile; }}/*! Updates the calibration parameters based on coordinate mapping of the given \a data. Create an instance of the QWSPointerCalibrationData class, fill in the device and screen coordinates and pass the object to the mouse handler using this function. \sa clearCalibration(), transform()*/void QWSCalibratedMouseHandler::calibrate(const QWSPointerCalibrationData *data){ // Algorithm derived from // "How To Calibrate Touch Screens" by Carlos E. Vidales, // printed in Embedded Systems Programming, Vol. 15 no 6, June 2002 // URL: http://www.embedded.com/showArticle.jhtml?articleID=9900629 const QPoint pd0 = data->devPoints[QWSPointerCalibrationData::TopLeft]; const QPoint pd1 = data->devPoints[QWSPointerCalibrationData::TopRight]; const QPoint pd2 = data->devPoints[QWSPointerCalibrationData::BottomRight]; const QPoint p0 = data->screenPoints[QWSPointerCalibrationData::TopLeft]; const QPoint p1 = data->screenPoints[QWSPointerCalibrationData::TopRight]; const QPoint p2 = data->screenPoints[QWSPointerCalibrationData::BottomRight]; const int xd0 = pd0.x(); const int xd1 = pd1.x(); const int xd2 = pd2.x(); const int yd0 = pd0.y(); const int yd1 = pd1.y(); const int yd2 = pd2.y(); const int x0 = p0.x(); const int x1 = p1.x(); const int x2 = p2.x(); const int y0 = p0.y(); const int y1 = p1.y(); const int y2 = p2.y(); s = ((xd0 - xd2)*(yd1 - yd2) - (xd1 - xd2)*(yd0 - yd2)); a = ((x0 - x2)*(yd1 - yd2) - (x1 - x2)*(yd0 - yd2)); b = ((xd0 - xd2)*(x1 - x2) - (x0 - x2)*(xd1 - xd2)); c = (yd0*(xd2*x1 - xd1*x2) + yd1*(xd0*x2 - xd2*x0) + yd2*(xd1*x0 - xd0*x1)); d = ((y0 - y2)*(yd1 - yd2) - (y1 - y2)*(yd0 - yd2)); e = ((xd0 - xd2)*(y1 - y2) - (y0 - y2)*(xd1 - xd2)); f = (yd0*(xd2*y1 - xd1*y2) + yd1*(xd0*y2 - xd2*y0) + yd2*(xd1*y0 - xd0*y1)); writeCalibration();}/*! Transforms the given \a position from device coordinates to screen coordinates, and returns the transformed position. Typically, the transform() function is called explicitly on the points passed to the QWSMouseHandler::mouseChanged() function. This implementation is a linear transformation using 7 parameters (\c a, \c b, \c c, \c d, \c e, \c f and \c s) to transform the device coordinates (\c Xd, \c Yd) into screen coordinates (\c Xs, \c Ys) using the following equations: \code s*Xs = a*Xd + b*Yd + c s*Ys = d*Xd + e*Yd + f \endcode \sa mouseChanged()*/QPoint QWSCalibratedMouseHandler::transform(const QPoint &position){ QPoint tp; tp.setX((a * position.x() + b * position.y() + c) / s); tp.setY((d * position.x() + e * position.y() + f) / s); return tp;}/*! Sets the size of the filter used in noise reduction to the given \a 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 the calculations. \sa sendFiltered()*/void QWSCalibratedMouseHandler::setFilterSize(int size){ samples.resize(qMax(1, size)); numSamples = 0; currSample = 0;}/*! \fn bool QWSCalibratedMouseHandler::sendFiltered(const QPoint &position, int state) Notifies the system of a new mouse event \e 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 \a position is the global position of the mouse. The \a state parameter is a bitmask of the Qt::MouseButton enum's values indicating which mouse buttons are pressed. The sendFiltered() function reduces noice by calculating an average position from a collection of mouse event positions, and then call the mouseChanged() function with the new position. The number of positions that is used is determined by the filter size. \sa mouseChanged(), setFilterSize()*/bool QWSCalibratedMouseHandler::sendFiltered(const QPoint &position, int button){ if (!button) { if (numSamples >= samples.count()) mouseChanged(transform(position), 0); currSample = 0; numSamples = 0; return true; } bool sent = false; samples[currSample] = position; numSamples++; if (numSamples >= samples.count()) { int ignore = -1; if (samples.count() > 2) { // throw away the "worst" sample int maxd = 0; for (int i = 0; i < samples.count(); i++) { int d = (mousePos - samples[i]).manhattanLength(); if (d > maxd) { maxd = d; ignore = i; } } } // average the rest QPoint pos(0, 0); int numAveraged = 0; for (int i = 0; i < samples.count(); i++) { if (ignore == i) continue; pos += samples[i]; ++numAveraged; } if (numAveraged) pos /= numAveraged; mouseChanged(transform(pos), button); sent = true; } currSample++; if (currSample >= samples.count()) currSample = 0; return sent;}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?