1
0
mirror of https://github.com/fwbuilder/fwbuilder synced 2026-03-23 11:47:24 +01:00

Mouse and keyboard input blocking for tutorial dialog

This commit is contained in:
Roman Bovsunivskiy 2010-02-24 21:25:05 +00:00
parent 07c116c75f
commit a91930d5a7
9 changed files with 150 additions and 15 deletions

24
src/gui/MouseBlocker.cpp Normal file
View File

@ -0,0 +1,24 @@
#include "MouseBlocker.h"
#include <QCursor>
#include <QTest>
MouseBlocker::MouseBlocker(QObject *parent) :
QThread(parent)
{
this->alive = true;
}
void MouseBlocker::run()
{
while (this->alive)
{
QCursor::setPos(QCursor::pos());
QTest::qSleep(100);
}
}
void MouseBlocker::die()
{
this->alive = false;
while (this->isRunning()) QTest::qSleep(10);
}

20
src/gui/MouseBlocker.h Normal file
View File

@ -0,0 +1,20 @@
#ifndef MOUSEBLOCKER_H
#define MOUSEBLOCKER_H
#include <QThread>
class MouseBlocker : public QThread
{
Q_OBJECT
bool alive;
public:
explicit MouseBlocker(QObject *parent = 0);
void run();
void die();
signals:
public slots:
};
#endif // MOUSEBLOCKER_H

View File

@ -26,19 +26,26 @@ TutorialAnimator::TutorialAnimator(QObject *parent, QString commands) :
//dynamic_cast<QWidget*>(parent)->grabKeyboard();
//dynamic_cast<QWidget*>(parent)->grabMouse();
dynamic_cast<QWidget*>(parent)->hide();
dynamic_cast<QWidget*>(parent)->setWindowModality(Qt::NonModal);
currentCommand = 0;
this->commands = commands.split('\n');
helper = new TutorialHelper();
helper = new TutorialHelper(this);
connect(this, SIGNAL(finished()), this, SLOT(scenarioFinished()));
this->speed = 50;
}
TutorialAnimator::~TutorialAnimator()
{
delete helper;
}
void TutorialAnimator::scenarioFinished()
{
//dynamic_cast<QWidget*>(this->parent())->releaseKeyboard();
//dynamic_cast<QWidget*>(this->parent())->releaseMouse();
dynamic_cast<QWidget*>(parent())->setWindowModality(Qt::ApplicationModal);
dynamic_cast<QWidget*>(this->parent())->show();
/*
dynamic_cast<QWidget*>(this->parent())->grabMouse();
@ -51,11 +58,15 @@ void TutorialAnimator::scenarioFinished()
void TutorialAnimator::run()
{
this->helper->blockMouse(true);
this->helper->blockInput(true);
for (int i=0; i<this->commands.count(); i++)
{
animate(i);
QTest::qWait(speed*20);
}
this->helper->blockMouse(false);
this->helper->blockInput(false);
//w->releaseKeyboard();
//w->releaseMouse();
}

View File

@ -34,6 +34,7 @@ Q_OBJECT
public:
explicit TutorialAnimator(QObject *parent, QString commands);
~TutorialAnimator();
void run();
void setSpeed(int speed);

View File

@ -61,16 +61,26 @@ void TutorialDialog::next()
void TutorialDialog::previous()
{
runScenario(getUndoForPage(currentPage));
currentPage--;
showPage(currentPage);
}
void TutorialDialog::reset()
{
runScenario(getResetForPage(currentPage));
currentPage = 0;
showPage(currentPage);
}
void TutorialDialog::runScenario(QString scenario)
{
if (animator != NULL) animator->deleteLater();
animator = new TutorialAnimator(this, scenario);
animator->setSpeed(ui->speed->value());
animator->start();
}
QString TutorialDialog::getScenarioForPage(int page)
{
QFile src(QString(":/Tutorial/commands/page") + QString::number(page) + ".txt");
@ -78,6 +88,20 @@ QString TutorialDialog::getScenarioForPage(int page)
return QString(src.readAll());
}
QString TutorialDialog::getUndoForPage(int page)
{
QFile src(QString(":/Tutorial/prev-commands/page") + QString::number(page) + ".txt");
src.open(QFile::ReadOnly);
return QString(src.readAll());
}
QString TutorialDialog::getResetForPage(int page)
{
QFile src(QString(":/Tutorial/reset-commands/page") + QString::number(page) + ".txt");
src.open(QFile::ReadOnly);
return QString(src.readAll());
}
void TutorialDialog::showPage(int page)
{
ui->demonstrate->setEnabled(true);
@ -96,14 +120,8 @@ void TutorialDialog::showPage(int page)
void TutorialDialog::demonstrate()
{
ui->demonstrate->setEnabled(false);
if (animator != NULL) delete animator;
animator = new TutorialAnimator(this, getScenarioForPage(currentPage));
animator->setSpeed(ui->speed->value());
animator->start();
runScenario(getScenarioForPage(currentPage));
ui->next->setEnabled(QFile::exists(QString(":/Tutorial/html/page") + QString::number(currentPage+1) + ".html"));
if (QFile::exists(QString(":/Tutorial/html/page") + QString::number(currentPage+1) + ".html"))
{
showPage(++currentPage);
}
//ui->message->setVisible(false);
}

View File

@ -16,6 +16,9 @@ public:
~TutorialDialog();
QString getScenarioForPage(int page);
QString getUndoForPage(int page);
QString getResetForPage(int page);
void runScenario(QString scenario);
void resizeEvent(QResizeEvent *);

View File

@ -10,9 +10,47 @@
#include "FWWindow.h"
#include "TutorialAnimator.h"
TutorialHelper::TutorialHelper()
TutorialHelper::TutorialHelper(QObject* parent): QObject(parent)
{
speed = 50;
widget = dynamic_cast<QWidget*>(parent->parent());//NULL;
mouseBlocker = NULL;
}
void TutorialHelper::blockInput(bool block)
{
if (block)
{
if (this->widget == NULL)
this->widget = new QWidget();
this->widget->grabKeyboard();
this->widget->grabMouse();
}
else
{
if (this->widget == NULL) return;
this->widget->releaseKeyboard();
this->widget->releaseMouse();
}
}
void TutorialHelper::blockMouse(bool block)
{
if (block)
{
if (mouseBlocker == NULL)
mouseBlocker = new MouseBlocker();
mouseBlocker->start();
}
else
{
if (mouseBlocker == NULL) return;
mouseBlocker->die();
mouseBlocker->terminate();
// mouseBlocker->deleteLater();
delete mouseBlocker;
mouseBlocker = NULL;
}
}
void TutorialHelper::moveMouse(QPoint end)
@ -45,17 +83,23 @@ void TutorialHelper::moveMouse(QWidget *w, QPoint userpoint)
void TutorialHelper::clickWidget(QWidget *w)
{
this->blockInput(false);
QTest::mouseClick(w, Qt::LeftButton, Qt::NoModifier, QPoint(), speed*4);
this->blockInput(true);
}
void TutorialHelper::clickMenuItem(QMenu *menu, QPoint pos)
{
this->blockInput(false);
QTest::mouseClick(menu, Qt::LeftButton, Qt::NoModifier, pos, speed);
this->blockInput(true);
}
void TutorialHelper::typeWidget(QWidget *w, QString text)
{
this->blockInput(false);
QTest::keyClicks(w, text, Qt::NoModifier, 50);
this->blockInput(true);
}
QPoint TutorialHelper::findViewItem(QAbstractItemView *view, int id)
@ -79,11 +123,15 @@ void TutorialHelper::selectComboItem(QWidget *widget, int id)
{
QComboBox * combo = dynamic_cast<QComboBox*>(widget);
moveMouse(combo);
this->blockInput(false);
QTest::mouseClick(combo, Qt::LeftButton);
this->blockInput(true);
QPoint itemPos = this->findViewItem(combo->view(), id);
QTest::qWait(speed*4);
moveMouse(combo->view(), itemPos);
this->blockInput(false);
QTest::mouseClick(combo->view(), Qt::LeftButton, 0, itemPos);
this->blockInput(true);
}
void TutorialHelper::selectComboItem(QWidget *widget, QString name)
@ -101,7 +149,9 @@ void TutorialHelper::selectListItem(QWidget *widget, int id)
itemPos = findViewItem(view, id);
moveMouse(view, itemPos);
view->setCurrentIndex(view->indexAt(itemPos));
this->blockInput(false);
QTest::mouseClick(view, Qt::LeftButton, 0, itemPos);
this->blockInput(true);
}
void TutorialHelper::selectListItem(QWidget *widget, QString name)
@ -133,7 +183,6 @@ QPoint findTab(QTabBar *bar, int id)
for (right = bar->width(); right!=0; right--)
if (bar->tabAt(QPoint(right, y)) == id)
break;
qDebug() << left << right << top << bottom;
return QPoint((left+right)/2, (top+bottom)/2);
}
@ -143,7 +192,9 @@ void TutorialHelper::selectTab(QWidget *widget, int id)
QPoint pos = findTab(bar, id);
moveMouse(bar, pos);
QTest::qWait(speed*4);
this->blockInput(false);
QTest::mouseClick(bar, Qt::LeftButton, 0, pos);
this->blockInput(true);
}

View File

@ -4,14 +4,16 @@
#include <QObject>
#include <QMenu>
#include <QComboBox>
#include "MouseBlocker.h"
class TutorialHelper : public QObject
{
Q_OBJECT
QWidget *widget;
MouseBlocker *mouseBlocker;
public:
int speed;
TutorialHelper();
TutorialHelper(QObject *parent = NULL);
public slots:
void clickWidget(QWidget*);
void clickMenuItem(QMenu *menu, QPoint pos);
@ -25,6 +27,9 @@ public slots:
void selectTab(QWidget *widget, QString name);
void selectTab(QWidget *widget, int id);
void blockInput(bool value);
void blockMouse(bool value);
QPoint findViewItem(QAbstractItemView *view, int id);
};

View File

@ -174,10 +174,11 @@ HEADERS += ../../config.h \
ClusterInterfaceWidget.h \
FWCmdRule.h \
UsageResolver.h \
IconSetter.h \
IconSetter.h \
TutorialAnimator.h \
TutorialDialog.h \
TutorialHelper.h
TutorialHelper.h \
MouseBlocker.h
SOURCES += ProjectPanel.cpp \
ProjectPanel_events.cpp \
ProjectPanel_file_ops.cpp \
@ -348,11 +349,12 @@ SOURCES += ProjectPanel.cpp \
ClusterInterfacesSelectorWidget.cpp \
ClusterInterfaceWidget.cpp \
FWCmdRule.cpp \
IconSetter.cpp \
IconSetter.cpp \
UsageResolver.cpp \
TutorialAnimator.cpp \
TutorialDialog.cpp \
TutorialHelper.cpp
TutorialHelper.cpp \
MouseBlocker.cpp
FORMS = FWBMainWindow_q.ui \
compileroutputpanel_q.ui \
customservicedialog_q.ui \