summaryrefslogtreecommitdiffstats
path: root/qtbotsim
diff options
context:
space:
mode:
Diffstat (limited to 'qtbotsim')
-rw-r--r--qtbotsim/main.cpp44
-rw-r--r--qtbotsim/qtbotsim.pro22
-rw-r--r--qtbotsim/robot.cpp212
-rw-r--r--qtbotsim/robot.h99
-rw-r--r--qtbotsim/robotwidget.cpp88
-rw-r--r--qtbotsim/robotwidget.h64
-rw-r--r--qtbotsim/widget.cpp101
-rw-r--r--qtbotsim/widget.h65
-rw-r--r--qtbotsim/widget.ui65
9 files changed, 760 insertions, 0 deletions
diff --git a/qtbotsim/main.cpp b/qtbotsim/main.cpp
new file mode 100644
index 0000000..475d438
--- /dev/null
+++ b/qtbotsim/main.cpp
@@ -0,0 +1,44 @@
+/*
+ * In the original BSD license, both occurrences of the phrase "COPYRIGHT
+ * HOLDERS AND CONTRIBUTORS" in the disclaimer read "REGENTS AND CONTRIBUTORS".
+ *
+ * Here is the license template:
+ *
+ * Copyright (c) 2011, Johan Thelin <[email protected]>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ * Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * Neither the name of Johan Thelin nor the names of its contributors may be
+ * used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <QtGui/QApplication>
+#include "widget.h"
+
+int main(int argc, char *argv[])
+{
+ QApplication a(argc, argv);
+ Widget w;
+ w.show();
+
+ return a.exec();
+}
diff --git a/qtbotsim/qtbotsim.pro b/qtbotsim/qtbotsim.pro
new file mode 100644
index 0000000..3c7ecf1
--- /dev/null
+++ b/qtbotsim/qtbotsim.pro
@@ -0,0 +1,22 @@
+#-------------------------------------------------
+#
+# Project created by QtCreator 2011-11-13T15:52:13
+#
+#-------------------------------------------------
+
+QT += core gui declarative
+
+TARGET = qtbotsim
+TEMPLATE = app
+
+
+SOURCES += main.cpp\
+ widget.cpp \
+ robotwidget.cpp \
+ robot.cpp
+
+HEADERS += widget.h \
+ robotwidget.h \
+ robot.h
+
+FORMS += widget.ui
diff --git a/qtbotsim/robot.cpp b/qtbotsim/robot.cpp
new file mode 100644
index 0000000..8265a3b
--- /dev/null
+++ b/qtbotsim/robot.cpp
@@ -0,0 +1,212 @@
+/*
+ * In the original BSD license, both occurrences of the phrase "COPYRIGHT
+ * HOLDERS AND CONTRIBUTORS" in the disclaimer read "REGENTS AND CONTRIBUTORS".
+ *
+ * Here is the license template:
+ *
+ * Copyright (c) 2011, Johan Thelin <[email protected]>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ * Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * Neither the name of Johan Thelin nor the names of its contributors may be
+ * used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "robot.h"
+
+#include <QPainterPath>
+#include <math.h>
+
+Robot::Robot(QObject *parent)
+ : QObject(parent)
+{
+ // All is reset in the reset method called first
+}
+
+void Robot::step(QPixmap *pixmap)
+{
+ QPointF start = position();
+ setPosition(position()+movementChange());
+ QPointF end = position();
+ setDirection(direction()+directionChange());
+
+ if (m_penActive)
+ {
+ QPainter p(pixmap);
+ p.setRenderHint(QPainter::Antialiasing);
+ p.setPen(Qt::red);
+ p.drawLine(start, end);
+ }
+
+ updateSensors(*pixmap);
+}
+
+void Robot::drawOverlay(QPainter &painter)
+{
+ QPainterPath path;
+
+ path.addRect(QRectF(-QPointF(10, 20), QSizeF(20, 40)));
+ path.addEllipse(QPointF(0, 15), 2, 2);
+
+ painter.setPen(Qt::black);
+ painter.setBrush(Qt::lightGray);
+
+ painter.translate(position());
+ painter.rotate(direction());
+
+ painter.drawPath(path);
+}
+
+bool Robot::brightForward() const
+{
+ return m_brightForward;
+}
+
+bool Robot::penActive() const
+{
+ return m_penActive;
+}
+
+qreal Robot::leftEngineSpeed() const
+{
+ return m_leftEngineSpeed;
+}
+
+qreal Robot::rightEngineSpeed() const
+{
+ return m_rightEngineSpeed;
+}
+
+void Robot::reset(const QPixmap &pixmap)
+{
+ setPenActive(true);
+ setLeftEngineSpeed(0);
+ setRightEngineSpeed(0);
+ setDirection(270);
+ setPosition(QPointF(pixmap.width()/2, pixmap.height()/2));
+ updateSensors(pixmap);
+}
+
+void Robot::setPenActive(bool pa)
+{
+ if (pa != m_penActive)
+ {
+ m_penActive = pa;
+ emit penActiveChanged(m_penActive);
+ }
+}
+
+void Robot::setLeftEngineSpeed(qreal s)
+{
+ s = qBound(-1.0, s, 1.0);
+
+ if (s != m_leftEngineSpeed)
+ {
+ m_leftEngineSpeed = s;
+ emit leftEngineSpeedChanged(m_leftEngineSpeed);
+ }
+}
+
+void Robot::setRightEngineSpeed(qreal s)
+{
+ s = qBound(-1.0, s, 1.0);
+
+ if (s != m_rightEngineSpeed)
+ {
+ m_rightEngineSpeed = s;
+ emit rightEngineSpeedChanged(m_rightEngineSpeed);
+ }
+}
+
+QPointF Robot::position() const
+{
+ return m_position;
+}
+
+void Robot::setPosition(const QPointF &p)
+{
+ m_position = p;
+}
+
+qreal Robot::direction() const
+{
+ return m_direction;
+}
+
+void Robot::setDirection(qreal d)
+{
+ while (d>360.0)
+ d -= 360.0;
+
+ m_direction = d;
+}
+
+void Robot::setBrightForward(bool value)
+{
+ if (value != m_brightForward)
+ {
+ m_brightForward = value;
+ emit brightForwardChanged(m_brightForward);
+ }
+}
+
+void Robot::updateSensors(const QPixmap &paper)
+{
+ QTransform t;
+ t.rotate(direction());
+ QPointF sensorPos = position() + t.map(QPointF(0, 15));
+
+ setBrightForward(qGray(paper.toImage().pixel(sensorPos.toPoint())) > 20);
+}
+
+QPointF Robot::movementChange() const
+{
+ QTransform t;
+ t.rotate(direction()+directionChange());
+
+ return t.map(QPointF(0, (leftEngineSpeed() + rightEngineSpeed())/2));
+}
+
+qreal Robot::directionChange() const
+{
+ /*
+
+ <----+----> lwS
+ | .
+ | .
+ | .
+ | .
+ |a x----------------------
+ |-. .... ) a
+ | . ....
+ |. ....
+ |. ....
+ <----+----> rwR
+
+
+ */
+
+ qreal tangens = (leftEngineSpeed()-rightEngineSpeed())/20.0;
+ qreal angle = atan(tangens);
+
+ return angle*180.0/M_PI;
+}
diff --git a/qtbotsim/robot.h b/qtbotsim/robot.h
new file mode 100644
index 0000000..4da6337
--- /dev/null
+++ b/qtbotsim/robot.h
@@ -0,0 +1,99 @@
+/*
+ * In the original BSD license, both occurrences of the phrase "COPYRIGHT
+ * HOLDERS AND CONTRIBUTORS" in the disclaimer read "REGENTS AND CONTRIBUTORS".
+ *
+ * Here is the license template:
+ *
+ * Copyright (c) 2011, Johan Thelin <[email protected]>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ * Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * Neither the name of Johan Thelin nor the names of its contributors may be
+ * used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef ROBOT_H
+#define ROBOT_H
+
+#include <QObject>
+
+#include <QPixmap>
+#include <QPainter>
+
+class Robot : public QObject
+{
+ Q_OBJECT
+
+ Q_PROPERTY(bool penActive READ penActive WRITE setPenActive NOTIFY penActiveChanged)
+ Q_PROPERTY(bool brightForward READ brightForward NOTIFY brightForwardChanged)
+ Q_PROPERTY(qreal leftEngineSpeed READ leftEngineSpeed WRITE setLeftEngineSpeed NOTIFY leftEngineSpeedChanged)
+ Q_PROPERTY(qreal rightEngineSpeed READ rightEngineSpeed WRITE setRightEngineSpeed NOTIFY rightEngineSpeedChanged)
+
+public:
+ explicit Robot(QObject *parent = 0);
+
+ void step(QPixmap *pixmap);
+ void drawOverlay(QPainter &painter);
+
+ bool brightForward() const;
+ bool penActive() const;
+ qreal leftEngineSpeed() const;
+ qreal rightEngineSpeed() const;
+
+signals:
+ void brightForwardChanged(bool);
+ void penActiveChanged(bool);
+
+ void leftEngineSpeedChanged(qreal);
+ void rightEngineSpeedChanged(qreal);
+
+public slots:
+ void reset(const QPixmap &pixmap);
+
+ void setPenActive(bool);
+
+ void setLeftEngineSpeed(qreal);
+ void setRightEngineSpeed(qreal);
+
+private:
+ void setPosition(const QPointF &p);
+ QPointF position() const;
+
+ void setDirection(qreal d);
+ qreal direction() const;
+
+ void setBrightForward(bool);
+
+ void updateSensors(const QPixmap &);
+ QPointF movementChange() const;
+ qreal directionChange() const;
+
+ bool m_brightForward;
+ bool m_penActive;
+ qreal m_leftEngineSpeed;
+ qreal m_rightEngineSpeed;
+
+ QPointF m_position;
+ qreal m_direction;
+};
+
+#endif // ROBOT_H
diff --git a/qtbotsim/robotwidget.cpp b/qtbotsim/robotwidget.cpp
new file mode 100644
index 0000000..e0ef072
--- /dev/null
+++ b/qtbotsim/robotwidget.cpp
@@ -0,0 +1,88 @@
+/*
+ * In the original BSD license, both occurrences of the phrase "COPYRIGHT
+ * HOLDERS AND CONTRIBUTORS" in the disclaimer read "REGENTS AND CONTRIBUTORS".
+ *
+ * Here is the license template:
+ *
+ * Copyright (c) 2011, Johan Thelin <[email protected]>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ * Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * Neither the name of Johan Thelin nor the names of its contributors may be
+ * used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "robotwidget.h"
+
+#include <QPainter>
+
+#include "robot.h"
+
+RobotWidget::RobotWidget(QWidget *parent)
+ : QWidget(parent)
+ , m_robot(0)
+{
+ m_paper = QPixmap(500, 500);
+ reset();
+
+ setMinimumSize(sizeHint());
+ setMaximumSize(sizeHint());
+}
+
+void RobotWidget::setRobot(Robot *robot)
+{
+ m_robot = robot;
+ reset();
+}
+
+QSize RobotWidget::sizeHint()
+{
+ return m_paper.size();
+}
+
+void RobotWidget::reset()
+{
+ m_paper.fill(Qt::white);
+ QPainter p(&m_paper);
+ QPen pen(Qt::black);
+ pen.setWidth(10);
+ p.setPen(pen);
+ p.drawEllipse(100, 100, 300, 300);
+ if (m_robot)
+ m_robot->reset(m_paper);
+ update();
+}
+
+void RobotWidget::step()
+{
+ m_robot->step(&m_paper);
+ update();
+}
+
+void RobotWidget::paintEvent(QPaintEvent *)
+{
+ QPainter p(this);
+ p.drawPixmap(0, 0, m_paper);
+ p.setRenderHint(QPainter::Antialiasing);
+ if (m_robot)
+ m_robot->drawOverlay(p);
+}
diff --git a/qtbotsim/robotwidget.h b/qtbotsim/robotwidget.h
new file mode 100644
index 0000000..46ae2b0
--- /dev/null
+++ b/qtbotsim/robotwidget.h
@@ -0,0 +1,64 @@
+/*
+ * In the original BSD license, both occurrences of the phrase "COPYRIGHT
+ * HOLDERS AND CONTRIBUTORS" in the disclaimer read "REGENTS AND CONTRIBUTORS".
+ *
+ * Here is the license template:
+ *
+ * Copyright (c) 2011, Johan Thelin <[email protected]>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ * Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * Neither the name of Johan Thelin nor the names of its contributors may be
+ * used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef ROBOTWIDGET_H
+#define ROBOTWIDGET_H
+
+#include <QWidget>
+#include <QPixmap>
+
+class Robot;
+
+class RobotWidget : public QWidget
+{
+ Q_OBJECT
+public:
+ RobotWidget(QWidget *parent = 0);
+
+ void setRobot(Robot *m_robot);
+
+ QSize sizeHint();
+
+public slots:
+ void reset();
+ void step();
+
+protected:
+ void paintEvent(QPaintEvent*);
+
+private:
+ QPixmap m_paper;
+ Robot *m_robot;
+};
+
+#endif // ROBOTWIDGET_H
diff --git a/qtbotsim/widget.cpp b/qtbotsim/widget.cpp
new file mode 100644
index 0000000..8f180f7
--- /dev/null
+++ b/qtbotsim/widget.cpp
@@ -0,0 +1,101 @@
+/*
+ * In the original BSD license, both occurrences of the phrase "COPYRIGHT
+ * HOLDERS AND CONTRIBUTORS" in the disclaimer read "REGENTS AND CONTRIBUTORS".
+ *
+ * Here is the license template:
+ *
+ * Copyright (c) 2011, Johan Thelin <[email protected]>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ * Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * Neither the name of Johan Thelin nor the names of its contributors may be
+ * used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "widget.h"
+#include "ui_widget.h"
+
+#include <QTimer>
+
+#include <QDeclarativeEngine>
+#include <QDeclarativeContext>
+#include <QDeclarativeComponent>
+
+#include "robot.h"
+
+Widget::Widget(QWidget *parent) :
+ QWidget(parent),
+ ui(new Ui::Widget)
+{
+ m_timer = new QTimer(this);
+ m_timer->setInterval(50);
+
+ connect(m_timer, SIGNAL(timeout()), this, SLOT(on_stepButton_clicked()));
+
+ ui->setupUi(this);
+ m_robot = new Robot(this);
+
+ ui->widget->setRobot(m_robot);
+
+ QDeclarativeEngine *engine = new QDeclarativeEngine(this);
+ engine->rootContext()->setContextProperty("robot", m_robot);
+ QDeclarativeComponent component(engine);
+ component.setData("import QtQuick 1.0\n"
+ "StateGroup {\n"
+ " states: [State { name: \"turn\"\n"
+ " PropertyChanges { target: robot; leftEngineSpeed: -0.5; rightEngineSpeed: 1.0; } },\n"
+ " State { name: \"forward\"\n"
+ " PropertyChanges { target: robot; leftEngineSpeed: 0.5; rightEngineSpeed: 0.5; } } ]\n"
+ " state: robot.brightForward?\"forward\":\"turn\"\n"
+ "}\n"
+ , QUrl());
+ QObject *controller = component.create();
+
+ foreach(QDeclarativeError e, component.errors())
+ qDebug("ERROR: %d %s", e.line(), qPrintable(e.description()));
+
+ controller->setParent(this);
+}
+
+Widget::~Widget()
+{
+ delete ui;
+}
+
+void Widget::on_clearButton_clicked()
+{
+ m_timer->stop();
+ ui->widget->reset();
+}
+
+void Widget::on_stepButton_clicked()
+{
+ ui->widget->step();
+}
+
+void Widget::on_runButton_clicked()
+{
+ if (m_timer->isActive())
+ m_timer->stop();
+ else
+ m_timer->start();
+}
diff --git a/qtbotsim/widget.h b/qtbotsim/widget.h
new file mode 100644
index 0000000..ccf210a
--- /dev/null
+++ b/qtbotsim/widget.h
@@ -0,0 +1,65 @@
+/*
+ * In the original BSD license, both occurrences of the phrase "COPYRIGHT
+ * HOLDERS AND CONTRIBUTORS" in the disclaimer read "REGENTS AND CONTRIBUTORS".
+ *
+ * Here is the license template:
+ *
+ * Copyright (c) 2011, Johan Thelin <[email protected]>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ * Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * Neither the name of Johan Thelin nor the names of its contributors may be
+ * used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef WIDGET_H
+#define WIDGET_H
+
+#include <QWidget>
+
+class Robot;
+class QTimer;
+
+namespace Ui {
+class Widget;
+}
+
+class Widget : public QWidget
+{
+ Q_OBJECT
+
+public:
+ explicit Widget(QWidget *parent = 0);
+ ~Widget();
+
+private slots:
+ void on_clearButton_clicked();
+ void on_stepButton_clicked();
+ void on_runButton_clicked();
+
+private:
+ Ui::Widget *ui;
+ Robot *m_robot;
+ QTimer *m_timer;
+};
+
+#endif // WIDGET_H
diff --git a/qtbotsim/widget.ui b/qtbotsim/widget.ui
new file mode 100644
index 0000000..ee157da
--- /dev/null
+++ b/qtbotsim/widget.ui
@@ -0,0 +1,65 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<ui version="4.0">
+ <class>Widget</class>
+ <widget class="QWidget" name="Widget">
+ <property name="geometry">
+ <rect>
+ <x>0</x>
+ <y>0</y>
+ <width>400</width>
+ <height>300</height>
+ </rect>
+ </property>
+ <property name="windowTitle">
+ <string>Widget</string>
+ </property>
+ <layout class="QVBoxLayout" name="verticalLayout">
+ <item>
+ <widget class="RobotWidget" name="widget" native="true">
+ <property name="sizePolicy">
+ <sizepolicy hsizetype="Expanding" vsizetype="Expanding">
+ <horstretch>0</horstretch>
+ <verstretch>0</verstretch>
+ </sizepolicy>
+ </property>
+ </widget>
+ </item>
+ <item>
+ <layout class="QHBoxLayout" name="horizontalLayout">
+ <item>
+ <widget class="QPushButton" name="clearButton">
+ <property name="text">
+ <string>Clear</string>
+ </property>
+ </widget>
+ </item>
+ <item>
+ <widget class="QPushButton" name="stepButton">
+ <property name="text">
+ <string>Step</string>
+ </property>
+ </widget>
+ </item>
+ <item>
+ <widget class="QPushButton" name="runButton">
+ <property name="text">
+ <string>Run/Stop</string>
+ </property>
+ </widget>
+ </item>
+ </layout>
+ </item>
+ </layout>
+ </widget>
+ <layoutdefault spacing="6" margin="11"/>
+ <customwidgets>
+ <customwidget>
+ <class>RobotWidget</class>
+ <extends>QWidget</extends>
+ <header location="global">robotwidget.h</header>
+ <container>1</container>
+ </customwidget>
+ </customwidgets>
+ <resources/>
+ <connections/>
+</ui>