123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- #include "JMouseMonitorThread.h"
- #include <qDebug>
- JMouseMonitorThread::JMouseMonitorThread(QObject* parent )
- : QThread(parent),
- m_locked(false),
- m_running(true)
- {
- }
- void JMouseMonitorThread::setLockCenter(const QPoint center)
- {
- QMutexLocker locker(&m_mutex);
- m_lockCenter = center;
- m_locked = true;
- }
- void JMouseMonitorThread::unlock()
- {
- QMutexLocker locker(&m_mutex);
- m_locked = false;
- }
- void JMouseMonitorThread::stop()
- {
- m_running = false;
- wait();
- }
- void JMouseMonitorThread::ConvertUIClickToImagePixel(int uiX, int uiY, int& imageX, int& imageY)
- {
- int uiwidth = 493;
- int uiheight = 493;
- int imagewidth = 640;
- int imageheight = 480;
-
- float uiX_topLeft = uiX + uiwidth / 2.0f;
- float uiY_topLeft = uiY + uiheight / 2.0f;
-
- float scaleX = (float)imagewidth / uiwidth;
- float scaleY = (float)imageheight / uiheight;
-
- int imageXA = (int)(uiX_topLeft * scaleX);
- int imageYA = (int)(uiY_topLeft * scaleY);
-
- imageX = std::max(0, std::min(imagewidth - 1, imageXA));
- imageY = std::max(0, std::min(imageheight - 1, imageYA));
- }
- void JMouseMonitorThread::ConvertUIClickToImagePixel2(int uiX, int uiY, int& imageX, int& imageY)
- {
- const int uiWidth = 493;
- const int uiHeight = 493;
- const int imgWidth = 640;
- const int imgHeight = 480;
-
- const float scale = std::min(
- static_cast<float>(uiWidth) / imgWidth,
- static_cast<float>(uiHeight) / imgHeight
- );
-
- const int displayWidth = static_cast<int>(imgWidth * scale);
- const int displayHeight = static_cast<int>(imgHeight * scale);
-
- const int offsetX = (uiWidth - displayWidth) / 2;
- const int offsetY = (uiHeight - displayHeight) / 2;
-
- const int uiClickX = uiX + uiWidth / 2;
- const int uiClickY = uiY + uiHeight / 2;
-
- const int displayX = std::max(0, std::min(displayWidth - 1, uiClickX - offsetX));
- const int displayY = std::max(0, std::min(displayHeight - 1, uiClickY - offsetY));
-
- imageX = static_cast<int>(displayX / scale);
- imageY = static_cast<int>(displayY / scale);
-
- imageX = std::max(0, std::min(imgWidth - 1, imageX));
- imageY = std::max(0, std::min(imgHeight - 1, imageY));
- }
- void JMouseMonitorThread::run()
- {
- QPoint lastPos = QCursor::pos();
- while (m_running)
- {
- if (m_locked)
- {
- QPoint currentPos;
- QPoint center;
- {
- QMutexLocker locker(&m_mutex);
- center = m_lockCenter;
- }
- currentPos = QCursor::pos();
- QPoint delta = currentPos - center;
- if (delta != QPoint(0, 0))
- {
- int imageX, imageY;
- ConvertUIClickToImagePixel2(delta.x(), delta.y(),imageX, imageY);
-
- delta.setX(imageX);
- delta.setY(imageY);
- emit MouseMovedSlg(delta);
- emit RequestCursorMoveSlg(center);
- }
- }
- msleep(15);
- }
- }
|