forked from Harinlen/Robot-Emulator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
robotmanagement.cpp
145 lines (130 loc) · 4.47 KB
/
robotmanagement.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
* Copyright (C) Kreogist Dev Team
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <QStackedLayout>
#include <QMessageBox>
#include "ground.h"
#include "robot.h"
#include "menubar.h"
#include "robotaddwidget.h"
#include "groundpreviewwidget.h"
#include "robotmanagement.h"
#include <QDebug>
RobotManagement::RobotManagement(QWidget *parent) :
QWidget(parent),
m_ground(nullptr),
m_groundPreview(new GroundPreviewWidget(this)),
m_stackLayout(new QStackedLayout),
m_robotAdd(new RobotAddWidget(this))
{
QBoxLayout *mainLayout=new QBoxLayout(QBoxLayout::LeftToRight,
this);
setLayout(mainLayout);
mainLayout->addWidget(m_groundPreview);
mainLayout->addLayout(m_stackLayout, 1);
//If we are using Mac OS X, using the sheet window flag.
#ifdef Q_OS_MACX
setWindowFlags(Qt::Sheet);
#else
setWindowFlags(Qt::Dialog);
#endif
//Initial the actions.
for(int i=0; i<RobotManagementActionsCount; i++)
{
m_actions[i]=new QAction(this);
}
//Set shortcuts.
m_actions[AddRobot]->setShortcut(QKeySequence(Qt::CTRL+Qt::Key_D));
m_actions[ManageRobot]->setShortcut(QKeySequence(Qt::CTRL+Qt::Key_A));
//Link the slots.
connect(m_actions[AddRobot],
static_cast<void (QAction::*)(bool)>(&QAction::triggered),
[=]{addRobot();});
connect(m_actions[ManageRobot],
static_cast<void (QAction::*)(bool)>(&QAction::triggered),
[=]{manageRobot();});
//Generate the add robot widget.
connect(m_robotAdd, &RobotAddWidget::requireClose,
this, &RobotManagement::close);
connect(m_robotAdd, &RobotAddWidget::requirePreviewRobot,
m_groundPreview, &GroundPreviewWidget::previewRobot);
connect(m_robotAdd, &RobotAddWidget::requireAddRobot,
this, &RobotManagement::onActionAddRobot);
m_stackLayout->addWidget(m_robotAdd);
//Retranslate.
retranslate();
}
GroundBase *RobotManagement::ground()
{
return m_ground;
}
void RobotManagement::addRobot()
{
//Show the add robot widget.
m_stackLayout->setCurrentWidget(m_robotAdd);
//Make the preview show the preview robot.
m_groundPreview->setShowPreviewPoint(true);
//Update the range.
QRectF barracksBoundingRect=m_ground->barracks().boundingRect();
m_robotAdd->updateXAndYRange(barracksBoundingRect.left(),
barracksBoundingRect.top(),
barracksBoundingRect.right(),
barracksBoundingRect.bottom());
//Show the dialog.
show();
}
void RobotManagement::manageRobot()
{
//Hide the preview robot.
m_groundPreview->setShowPreviewPoint(false);
//Show the dialog.
show();
}
void RobotManagement::setGround(GroundBase *ground)
{
//Save the ground.
m_ground = ground;
//Give the ground to the preview.
m_groundPreview->setGround(m_ground);
}
void RobotManagement::setMenuBar(MenuBar *menuBar)
{
menuBar->addCategoryAction(MenuBar::Robot, m_actions[AddRobot]);
menuBar->addCategoryAction(MenuBar::Robot, m_actions[ManageRobot]);
}
void RobotManagement::retranslate()
{
m_actions[AddRobot]->setText(tr("Add robot"));
m_actions[ManageRobot]->setText(tr("Manage robots"));
}
void RobotManagement::onActionAddRobot(const QPointF &position,
const qreal &angle)
{
//Generate a robot.
Robot *robot=new Robot(position);
robot->setAngle(angle);
//Try to add the robot, if sucessful, close the dialog.
if(m_ground->addRobot(robot))
{
close();
return;
}
//Or else, display an error information.
QMessageBox::warning(this,
tr("Add Robot Failed"),
tr("The robot is not in the barracks, you cannot add this robot"));
}