Skip to content

Commit

Permalink
Whiteboard example: Add configurable target rectangle
Browse files Browse the repository at this point in the history
  • Loading branch information
thp committed Sep 5, 2012
1 parent 388ed44 commit e183565
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 16 deletions.
49 changes: 39 additions & 10 deletions examples/labs/whiteboard/mainwindow.cpp
Expand Up @@ -4,6 +4,8 @@
#include <QPainter>
#include <QMouseEvent>

#define DEFAULT_BORDER 20

MainWindow::MainWindow(QWidget *parent) :
QWidget(parent),
m_mousePos(0, 0),
Expand All @@ -13,18 +15,21 @@ MainWindow::MainWindow(QWidget *parent) :
m_move(psmove_connect()),
m_tracker(psmove_tracker_new()),
m_timer(),
m_path()
m_path(),
m_rect(rect()),
m_rectOffset(0)
{
m_points[0] = QPointF(1, 1);
m_points[1] = QPointF(640, 0);
m_points[2] = QPointF(640, 480);
m_points[3] = QPointF(0, 480);
m_mapping.set(m_points);
m_mapping.setSize(width(), height());

setMouseTracking(true);

while (psmove_tracker_enable(m_tracker, m_move) != Tracker_CALIBRATED);
if (m_move) {
while (psmove_tracker_enable(m_tracker, m_move) != Tracker_CALIBRATED);
}

QObject::connect(&m_timer, SIGNAL(timeout()),
this, SLOT(timeout()));
Expand All @@ -38,6 +43,7 @@ void MainWindow::paintEvent(QPaintEvent *event)

painter.setPen(Qt::blue);
painter.drawEllipse(m_mapping.map(m_mousePos), 5, 5);
painter.drawRect(m_mapping.rect());

painter.setPen(Qt::red);
painter.drawEllipse(m_mousePos.toPoint(), 5, 5);
Expand All @@ -50,21 +56,39 @@ void MainWindow::paintEvent(QPaintEvent *event)

void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
//m_mousePos = event->posF();
//update();
m_mousePos = event->posF();
update();
}

void MainWindow::resizeEvent(QResizeEvent *event)
{
m_mapping.setSize(width(), height());
m_rect = rect().adjusted(DEFAULT_BORDER, DEFAULT_BORDER,
-DEFAULT_BORDER, -DEFAULT_BORDER);
syncRect();
}

void MainWindow::mousePressEvent(QMouseEvent *event)
{
//m_points[m_pointsOffset] = event->posF();
//m_pointsOffset = (m_pointsOffset + 1) % 4;
//m_mapping.set(m_points);
//update();
switch (event->button()) {
case Qt::LeftButton:
m_points[m_pointsOffset] = event->posF();
m_pointsOffset = (m_pointsOffset + 1) % 4;
m_mapping.set(m_points);
break;
case Qt::RightButton:
if (m_rectOffset == 0) {
m_rect.setTopLeft(event->pos());
} else {
m_rect.setBottomRight(event->pos());
}
m_rectOffset = (m_rectOffset + 1) % 2;
syncRect();
break;
default:
break;
}

update();
}

MainWindow::~MainWindow()
Expand Down Expand Up @@ -116,3 +140,8 @@ void MainWindow::timeout()
}

}

void MainWindow::syncRect()
{
m_mapping.setRect(m_rect);
}
6 changes: 6 additions & 0 deletions examples/labs/whiteboard/mainwindow.h
Expand Up @@ -25,6 +25,9 @@ class MainWindow : public QWidget
private slots:
void timeout();

private:
void syncRect();

private:
QPointF m_mousePos;
Mapping m_mapping;
Expand All @@ -38,6 +41,9 @@ private slots:
QTimer m_timer;

QPainterPath m_path;

QRect m_rect;
int m_rectOffset;
};

#endif // MAINWINDOW_H
17 changes: 12 additions & 5 deletions examples/labs/whiteboard/mapping.h
Expand Up @@ -2,21 +2,26 @@
#define MAPPING_H

#include <QPointF>
#include <QRect>

#include <qmath.h>

class Mapping
{
public:
Mapping()
: a(0, 0), b(1, 0), c(1, 1), d(0, 1), w(1), h(1)
: a(0, 0), b(1, 0), c(1, 1), d(0, 1),
m_rect(0, 0, 1, 1)
{
}

void setSize(float width, float height)
void setRect(QRect rect)
{
w = width;
h = height;
m_rect = rect;
}

QRect rect() {
return m_rect;
}

void set(QPointF *m)
Expand Down Expand Up @@ -45,12 +50,14 @@ class Mapping

double v = (px - p1x) / (p2x - p1x);

return QPointF(u*w, v*h);
return QPointF(m_rect.x() + u*m_rect.width(),
m_rect.y() + v*m_rect.height());
}

private:
QPointF a, b, c, d;
float w, h;
QRect m_rect;
};

#endif // MAPPING_H
2 changes: 1 addition & 1 deletion examples/labs/whiteboard/whiteboard.pro
Expand Up @@ -6,7 +6,7 @@ SOURCES += main.cpp
SOURCES += mainwindow.cpp
HEADERS += mainwindow.h

HEADERS += Mapping.h
HEADERS += mapping.h

INCLUDEPATH += ../../../include/
LIBS += -L../../../build/ -lpsmoveapi -lpsmoveapi_tracker
Expand Down

0 comments on commit e183565

Please sign in to comment.