Skip to content

Commit

Permalink
Add robot manager.
Browse files Browse the repository at this point in the history
  • Loading branch information
Harinlen committed Jun 1, 2015
1 parent c828fb2 commit a88b8d8
Show file tree
Hide file tree
Showing 17 changed files with 732 additions and 16 deletions.
6 changes: 4 additions & 2 deletions Robot.pro
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ SOURCES += \
groundpreviewer.cpp \
groundrealtimepreviewer.cpp \
groundglobal.cpp \
polygoneditor.cpp
polygoneditor.cpp \
robotmanagewidget.cpp

HEADERS += \
mainwindow.h \
Expand All @@ -37,4 +38,5 @@ HEADERS += \
groundpreviewer.h \
groundrealtimepreviewer.h \
groundglobal.h \
polygoneditor.h
polygoneditor.h \
robotmanagewidget.h
43 changes: 43 additions & 0 deletions ground.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ void Ground::paintEvent(QPaintEvent *event)
//Draw all the robot.
for(Robot *robot : m_robotList)
{
robot->paintRobotParameter(&painter);
robot->paintRobot(&painter);
}
}
Expand Down Expand Up @@ -675,6 +676,31 @@ void Ground::setGenerator(GenerateGroundBase *generator)
m_generator=generator;
}

void Ground::syncRobotData(const QList<Robot *> &robots,
const QList<QPointF> &initialPosition,
const QList<qreal> &initialAngle)
{
//Search all the robots should be removed.
while(!m_robotList.isEmpty())
{
Robot *currentRobot=m_robotList.takeLast();
//Check the robot is still in the list.
if(!robots.contains(currentRobot))
{
//Delete the robot.
delete currentRobot;
}
}
//Save the new robot list, initial position list and initial angle list.
m_robotList=robots;
m_robotInitialPosition=initialPosition;
m_robotInitialAngle=initialAngle;
//Set the change flag.
m_changed=true;
//Reset the ground.
reset();
}

void Ground::setBarracks(const QPolygonF &barracks)
{
//Check if the barracks is all in the border.
Expand Down Expand Up @@ -731,10 +757,27 @@ void Ground::reset()
Robot *robot=m_robotList.at(i);
//Clear the guardian line data.
robot->resetGuardianLine();
//Clear the robot cache data.
robot->resetDetectList();
//Reset the robot position and angle.
robot->setPos(m_robotInitialPosition.at(i));
robot->setAngle(m_robotInitialAngle.at(i));
}
//Update the ground.
update();
}

QList<Robot *> Ground::robotList() const
{
return m_robotList;
}

QList<QPointF> Ground::robotInitialPosition() const
{
return m_robotInitialPosition;
}

QList<qreal> Ground::robotInitialAngle() const
{
return m_robotInitialAngle;
}
23 changes: 23 additions & 0 deletions ground.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ class Ground : public GroundBase
* \brief Reimplemented from GroundBase::setGenerator().
*/
void setGenerator(GenerateGroundBase *generator);

/*!
* \brief syncRobotData.
*/
void syncRobotData(const QList<Robot *> &robots,
const QList<QPointF> &initialPosition,
const QList<qreal> &initialAngle);

signals:

public slots:
Expand Down Expand Up @@ -99,6 +107,21 @@ public slots:
*/
void reset();

/*!
* \brief Reimplemented from GroundBase::robotList().
*/
QList<Robot *> robotList() const;

/*!
* \brief Reimplemented from GroundBase::robotInitialPosition().
*/
QList<QPointF> robotInitialPosition() const;

/*!
* \brief Reimplemented from GroundBase::robotInitialAngle().
*/
QList<qreal> robotInitialAngle() const;

protected:
/*!
* \brief This event handler can be reimplemented in a subclass to receive
Expand Down
28 changes: 28 additions & 0 deletions groundbase.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,34 @@ class GroundBase : public QWidget
*/
virtual void setGenerator(GenerateGroundBase *generator)=0;

/*!
* \brief Get all the robots added to the ground.
* \return The robot QList.
*/
virtual QList<Robot *> robotList() const=0;

/*!
* \brief Get the initial position of all the robots recorded by ground.
* \return The initial position list.
*/
virtual QList<QPointF> robotInitialPosition() const=0;

/*!
* \brief Get the initial angle of all the robots recorded by ground.
* \return The initial angle list.
*/
virtual QList<qreal> robotInitialAngle() const=0;

/*!
* \brief Set the robots information to the given parameters, remove the
* \param robots
* \param initialPosition
* \param initialAngle
*/
virtual void syncRobotData(const QList<Robot *> &robots,
const QList<QPointF> &initialPosition,
const QList<qreal> &initialAngle)=0;

signals:
/*!
* \brief When the user change the border, this signal will be emitted.
Expand Down
7 changes: 6 additions & 1 deletion groundpreviewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,18 @@ void GroundPreviewer::paintEvent(QPaintEvent *event)
if(m_showPreviewPoint)
{
//Paint the preview robot.
m_previewRobot->paintRobotParameter(&painter);
m_previewRobot->paintRobot(&painter);
}
}

inline QPointF GroundPreviewer::pointFromGround(const QPointF &groundPoint)
QPointF GroundPreviewer::pointFromGround(const QPointF &groundPoint)
{
return QPointF((groundPoint.x()+m_xOffset)/m_groundParameter*width(),
(groundPoint.y()+m_yOffset)/m_groundParameter*height());
}


bool GroundPreviewer::showPreviewPoint() const
{
return m_showPreviewPoint;
Expand Down Expand Up @@ -153,5 +155,8 @@ void GroundPreviewer::setPreviewBarracks(const QPolygonF &groundBarracks)

void GroundPreviewer::setShowPreviewPoint(bool showPreviewPoint)
{
//Save the show point settings.
m_showPreviewPoint = showPreviewPoint;
//Update.
update();
}
11 changes: 10 additions & 1 deletion groundpreviewer.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,19 @@ public slots:
void previewRobot(QPointF position, qreal angle);

protected:
/*!
* \brief Reimplemented from QWidget::paintEvent().
*/
void paintEvent(QPaintEvent *event);

/*!
* \brief Get the point in preview mode mapped from the Ground class.
* \param groundPoint The point on the Ground class.
* \return The point mapped in the preview.
*/
QPointF pointFromGround(const QPointF &groundPoint);

private:
inline QPointF pointFromGround(const QPointF &groundPoint);
QPolygonF m_previewGround, m_previewBarracks;

qreal m_xOffset, m_yOffset, m_groundParameter;
Expand Down
101 changes: 100 additions & 1 deletion groundrealtimepreviewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,26 @@
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <QPainter>
#include "groundbase.h"
#include "robot.h"

#include "groundrealtimepreviewer.h"

GroundRealtimePreviewer::GroundRealtimePreviewer(QWidget *parent) :
GroundPreviewer(parent),
m_ground(nullptr)
m_proxyRobot(new Robot()),
m_ground(nullptr),
m_displayRobots(false),
m_selectedRobot(nullptr)
{
}

GroundRealtimePreviewer::~GroundRealtimePreviewer()
{
delete m_proxyRobot;
}

void GroundRealtimePreviewer::setGround(GroundBase *ground)
{
//Save the ground.
Expand Down Expand Up @@ -57,3 +67,92 @@ void GroundRealtimePreviewer::onActionBarracksChanged()
//Set the barracks polygon.
setPreviewBarracks(m_ground->barracks());
}

bool GroundRealtimePreviewer::displayRobots() const
{
return m_displayRobots;
}

void GroundRealtimePreviewer::setDisplayRobots(bool displayRobots)
{
//Save the display robot settings.
m_displayRobots=displayRobots;
//Update.
update();
}

void GroundRealtimePreviewer::setSelectedRobot(Robot *selectedRobot)
{
//Save the selected robot.
m_selectedRobot=selectedRobot;
//Update the previewer.
update();
}

void GroundRealtimePreviewer::setPositionList(const QList<QPointF> &positions)
{
//The size of position should be the same as robots.
if(positions.size()!=m_robots.size())
{
return;
}
//Save the positions.
m_positions=positions;
//Update the previewer.
update();
}

void GroundRealtimePreviewer::setAngleList(const QList<qreal> &angles)
{
//The size of angle should be the same as robots.
if(angles.size()!=m_robots.size())
{
return;
}
//Save the angle list.
m_angles=angles;
//Update the previewer.
update();
}

void GroundRealtimePreviewer::setRobotsPreviewList(const QList<Robot *> &robots,
const QList<QPointF> &positions,
const QList<qreal> &angles)
{
//Save all the informations.
m_robots=robots;
m_positions=positions;
m_angles=angles;
//Update the previewer.
update();
}

void GroundRealtimePreviewer::paintEvent(QPaintEvent *event)
{
//Draw the map.
GroundPreviewer::paintEvent(event);
//If the user wants to draw the exist robot, then paint the robot.
if(m_displayRobots)
{
//Initial painter.
QPainter painter(this);
painter.setRenderHints(QPainter::Antialiasing |
QPainter::TextAntialiasing |
QPainter::SmoothPixmapTransform, true);
for(int i=0; i<m_positions.size(); i++)
{
//Set the preview pos to the preview robot.
m_proxyRobot->setPos(pointFromGround(m_positions.at(i)));
//Draw the robot.
m_proxyRobot->paintRobot(&painter);
//Check the selected robot
if(m_robots.at(i)==m_selectedRobot)
{
//Draw the parameter.
m_proxyRobot->setAngle(m_angles.at(i));
m_proxyRobot->paintRobotParameter(&painter);
}
}
}
}

Loading

0 comments on commit a88b8d8

Please sign in to comment.