From a64ffe1153d7b637007c2dde8bd39c559c97a1fe Mon Sep 17 00:00:00 2001 From: Alexis Maiquez Murcia Date: Tue, 19 Feb 2019 16:56:26 +0100 Subject: Added support for sensor leaving events with delay support Here delay means the time the mouse has to be in the sensor for the leave event to trigger the action Signed-off-by: Alexis Maiquez Murcia --- src/sensor.cpp | 70 ++++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 53 insertions(+), 17 deletions(-) (limited to 'src/sensor.cpp') diff --git a/src/sensor.cpp b/src/sensor.cpp index 49fd149..597d4f9 100644 --- a/src/sensor.cpp +++ b/src/sensor.cpp @@ -34,18 +34,32 @@ SOFTWARE. namespace Lead { -Sensor::Sensor(int x, int y, int w, int h, QString action, int interval): +Sensor::Sensor(int x, int y, int w, int h, QString enterAction, QString exitAction, int enterInterval, int exitInterval): QWidget(), - action(action), - interval(interval) + enterAction(enterAction), + exitAction(exitAction), + enterInterval(enterInterval), + exitInterval(exitInterval) { - qDebug() << "lead::Sensor() " << x << "," << y << "," << w << "," << h << " : action=" << action << " : interval=" << interval; + qDebug() + << "lead::Sensor() " << x << "," << y << "," << w << "," << h + << " : enterAction=" << enterAction + << " : exitAction=" << exitAction + << " : enterInterval=" << enterInterval + << " : exitInterval=" << exitInterval; - this->timer = new QTimer(this); - this->timer->setSingleShot(true); - this->timer->setInterval(interval); + this->canTriggerExit = false; - connect(this->timer, SIGNAL(timeout()), this, SLOT(activate())); + this->enterTimer = new QTimer(this); + this->enterTimer->setSingleShot(true); + this->enterTimer->setInterval(enterInterval); + + this->exitTimer = new QTimer (this); + this->exitTimer->setSingleShot (true); + this->exitTimer->setInterval (exitInterval); + + connect(this->enterTimer, SIGNAL(timeout()), this, SLOT(activateEnter())); + connect(this->exitTimer, SIGNAL(timeout()), this, SLOT(activateExit())); //setStyleSheet("background-color:red;"); setGeometry(x, y, w, h); @@ -58,34 +72,56 @@ Sensor::Sensor(int x, int y, int w, int h, QString action, int interval): Sensor::~Sensor() { - delete this->timer; + delete this->enterTimer; + delete this->exitTimer; } void Sensor::enterEvent(QEvent * event) { - qDebug() << "lead::Sensor::enterEvent() " << this->x() << ":" << this->y() << " interval: " << this->interval; + qDebug() + << "lead::Sensor::enterEvent() "<< this->x() << ":" << this->y() + << " enterInterval: " << this->enterTimer + << " exitInterval: " << this->exitTimer; - this->timer->start(); + this->enterTimer->start(); + this->exitTimer->start(); } void Sensor::leaveEvent(QEvent * event) { - qDebug() << "lead::Sensor::leaveEvent() " << this->x() << ":" << this->y() << " interval: " << this->interval; - - this->timer->stop(); + qDebug() + << "lead::Sensor::leaveEvent() "<< this->x() << ":" << this->y() + << " enterInterval: " << this->enterTimer + << " exitInterval: " << this->exitTimer; + + if (this->canTriggerExit) + { + QProcess::startDetached(exitAction); + } + + this->enterTimer->stop(); + this->exitTimer->stop(); + this->canTriggerExit = false; } void -Sensor::activate() +Sensor::activateEnter() +{ + qDebug() << "lead::Sensor::activateEnter() " << this->x() << ":" << this->y() << " action: " << this->enterAction; + + QProcess::startDetached(enterAction); +} + +void Sensor::activateExit() { - qDebug() << "lead::Sensor::activate() " << this->x() << ":" << this->y() << " action: " << this->action; + qDebug () << "lead::Sensor::activateExit() " << this->x() << ":" << this->y() << " action: " << this->exitAction << " can run now"; - QProcess::startDetached(action); + this->canTriggerExit = true; } -- cgit v1.2.3