Skip to content

Commit

Permalink
1.0 Release Candidate IV
Browse files Browse the repository at this point in the history
Add coordinate switcher.
  • Loading branch information
Harinlen committed Jun 4, 2015
1 parent f300a3c commit 20b7066
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 10 deletions.
47 changes: 38 additions & 9 deletions gridwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@

GridWidget::GridWidget(QWidget *parent) :
QWidget(parent),
m_widget(nullptr)
m_widget(nullptr),
m_coordinate(false)
{
setAutoFillBackground(true);
}
Expand All @@ -47,23 +48,35 @@ void GridWidget::paintEvent(QPaintEvent *event)
painter.drawLine(i, 0, i-height(), height());
//Draw left top to right bottom lines.
painter.drawLine(i, 0, i+height(), height());
int horizon=i-m_gridSize+8;
for(int j=-m_gridSize-2; j<height()+m_gridStep; j+=m_gridSize)
{
painter.drawStaticText(horizon,
j,
QStaticText("("+QString::number(horizon)+", "+QString::number(j)+")"));
horizon-=m_gridSize;
}
}
stopWidth=-height();
for(int i=0; i>=stopWidth; i-=m_gridStep)
{
painter.drawLine(i, 0, i+height(), height());
}
//Draw captions.
if(m_coordinate)
{
for(CoordinateItem item : m_captions)
{
painter.drawText(item.x, item.y, item.text);
}
}
}

bool GridWidget::coordinate() const
{
return m_coordinate;
}

void GridWidget::setCoordinate(bool coordinate)
{
m_coordinate = coordinate;
update();
}

int GridWidget::m_gridStep=100;

int GridWidget::m_gridSize=50;

void GridWidget::setGridStep(int gridStep)
Expand Down Expand Up @@ -92,5 +105,21 @@ void GridWidget::resizeEvent(QResizeEvent *event)
{
m_widget->resize(size());
}
//Update the items.
m_captions.clear();
int stopWidth=width()+height()+m_gridStep;
for(int i=0; i<stopWidth; i+=m_gridStep)
{
int horizon=i-m_gridSize+8;
for(int j=-m_gridSize-2; j<height()+m_gridStep; j+=m_gridSize)
{
CoordinateItem item;
item.x=horizon;
item.y=j;
item.text="("+QString::number(horizon)+", "+QString::number(j)+")";
m_captions.append(item);
horizon-=m_gridSize;
}
}
}

13 changes: 12 additions & 1 deletion gridwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ class GridWidget : public QWidget
static int gridStep();
static void setGridStep(int gridStep);

bool coordinate() const;

signals:

public slots:
Expand All @@ -56,6 +58,7 @@ public slots:
* \param widget The prefer central widget.
*/
void setWidget(QWidget *widget);
void setCoordinate(bool coordinate);

protected:
/*!
Expand All @@ -69,8 +72,16 @@ public slots:
void paintEvent(QPaintEvent *event);

private:
QWidget *m_widget;
struct CoordinateItem
{
int x;
int y;
QString text;
};
QList<CoordinateItem> m_captions;

QWidget *m_widget;
bool m_coordinate;
static int m_gridSize;
static int m_gridStep;
};
Expand Down
10 changes: 10 additions & 0 deletions ground.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ Ground::Ground(QWidget *parent) :

m_actions[ShowDirection]->setCheckable(true);
m_actions[ShowDirection]->setChecked(true);

m_actions[ShowCoordinate]->setCheckable(true);
m_actions[ShowCoordinate]->setChecked(false);
//Set the key sequence.
m_actions[New]->setShortcut(QKeySequence(Qt::CTRL+Qt::Key_N));
m_actions[Open]->setShortcut(QKeySequence(Qt::CTRL+Qt::Key_O));
Expand Down Expand Up @@ -274,6 +277,7 @@ void Ground::retranslate()
m_actions[RobotDetectRangeColor]->setText(tr("Set robot detect range color"));
m_actions[ShowDirection]->setText(tr("Show Robot Direction"));
m_actions[ShowDetectRange]->setText(tr("Show Detect Range"));
m_actions[ShowCoordinate]->setText(tr("Show Coordinate"));
}

void Ground::onActionUpdateRobot()
Expand Down Expand Up @@ -870,6 +874,7 @@ void Ground::setMenuBar(MenuBar *menuBar)
menuBar->addCategoryAction(MenuBar::Ground, m_actions[RobotDetectRangeColor]);
menuBar->addCategoryAction(MenuBar::Ground, m_actions[ShowDirection]);
menuBar->addCategoryAction(MenuBar::Ground, m_actions[ShowDetectRange]);
menuBar->addCategoryAction(MenuBar::Ground, m_actions[ShowCoordinate]);
}

void Ground::setGenerator(GenerateGroundBase *generator)
Expand Down Expand Up @@ -903,6 +908,11 @@ void Ground::syncRobotData(const QList<Robot *> &robots,
reset();
}

QAction *Ground::showCoordinate()
{
return m_actions[ShowCoordinate];
}

void Ground::setSpeed(const int &speed)
{
m_timeline->setInterval(speed);
Expand Down
6 changes: 6 additions & 0 deletions ground.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ class Ground : public GroundBase
const QList<QPointF> &initialPosition,
const QList<qreal> &initialAngle);

/*!
* \brief Reimplemented from GroundBase::showCoordinate().
*/
QAction *showCoordinate();

signals:

public slots:
Expand Down Expand Up @@ -177,6 +182,7 @@ private slots:
RobotDetectRangeColor,
ShowDirection,
ShowDetectRange,
ShowCoordinate,
GroundActionsCount
};
QAction *m_actions[GroundActionsCount];
Expand Down
6 changes: 6 additions & 0 deletions groundbase.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,12 @@ class GroundBase : public QWidget
const QList<QPointF> &initialPosition,
const QList<qreal> &initialAngle)=0;

/*!
* \brief Get the show coordinate action.
* \return The show coordinate action.
*/
virtual QAction *showCoordinate()=0;

signals:
/*!
* \brief When the user change the border, this signal will be emitted.
Expand Down
3 changes: 3 additions & 0 deletions mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ MainWindow::MainWindow(QWidget *parent) :
setMinimumSize(500, 309);
//Initial the grid widget.
GridWidget *gridWidget=new GridWidget(this);
connect(m_ground->showCoordinate(),
static_cast<void (QAction::*)(bool)>(&QAction::triggered),
gridWidget, &GridWidget::setCoordinate);
setCentralWidget(gridWidget);
//Initial the scroll area.
QScrollArea *groundArea=new QScrollArea(this);
Expand Down

0 comments on commit 20b7066

Please sign in to comment.