-
Notifications
You must be signed in to change notification settings - Fork 2
/
enemymanagewidget.cpp
271 lines (247 loc) · 8.8 KB
/
enemymanagewidget.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*
* 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 <QBoxLayout>
#include <QPushButton>
#include <QStandardItemModel>
#include <QGridLayout>
#include <QMessageBox>
#include <QTreeView>
#include <QGroupBox>
#include "enemy.h"
#include "groundbase.h"
#include "languagemanager.h"
#include "enemymanagewidget.h"
#include <QDebug>
EnemyManageWidget::EnemyManageWidget(QWidget *parent) :
QWidget(parent),
m_okay(new QPushButton(this)),
m_cancel(new QPushButton(this)),
m_deletePoint(new QPushButton(this)),
m_enemyInitialDataView(new QTreeView(this)),
m_enemyInitialDataModel(new QStandardItemModel(this)),
m_ground(nullptr),
m_manageBox(new QGroupBox(this))
{
//Set the palette.
QPalette pal=m_deletePoint->palette();
pal.setColor(QPalette::Button, QColor(255,0,0));
pal.setColor(QPalette::ButtonText, QColor(255,255,255));
m_deletePoint->setPalette(pal);
//Configure robot list.
m_enemyInitialDataView->setSelectionMode(QAbstractItemView::SingleSelection);
m_enemyInitialDataView->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
m_enemyInitialDataView->setModel(m_enemyInitialDataModel);
connect(m_enemyInitialDataView->selectionModel(),
&QItemSelectionModel::currentChanged,
[=](const QModelIndex ¤t)
{
if(current.row()<m_enemyList.size())
{
emit requireSelectEnemy(m_enemyList.at(current.row()));
}
});
connect(m_enemyInitialDataModel, &QStandardItemModel::itemChanged,
[=](QStandardItem *item)
{
//Check item is valid first.
if(item!=nullptr)
{
//If the item is from angle row.
if(2==item->column())
{
emit requireUpdateAngleList();
}
else
{
//Or else, it will be position list.
emit requireUpdatePositionList();
}
}
});
//Initial layouts.
QBoxLayout *mainLayout=new QBoxLayout(QBoxLayout::LeftToRight,
this);
mainLayout->setContentsMargins(0,0,0,0);
setLayout(mainLayout);
//Generate the editor layout.
QBoxLayout *robotViewLayout=new QBoxLayout(QBoxLayout::TopToBottom,
mainLayout->widget());
m_manageBox->setLayout(robotViewLayout);
robotViewLayout->addWidget(m_enemyInitialDataView, 1);
robotViewLayout->addWidget(m_deletePoint);
mainLayout->addWidget(m_manageBox, 1);
//Generate the button layout.
QBoxLayout *buttonLayout=new QBoxLayout(QBoxLayout::TopToBottom,
mainLayout->widget());
buttonLayout->setSpacing(2);
buttonLayout->addWidget(m_okay);
m_cancel->setShortcut(QKeySequence(Qt::Key_Escape));
buttonLayout->addWidget(m_cancel);
buttonLayout->addStretch();
mainLayout->addLayout(buttonLayout);
connect(m_deletePoint,
static_cast<void (QPushButton::*)(bool)>(&QPushButton::clicked),
[=]
{
//Check if current index is vaild.
if(m_enemyInitialDataView->selectionModel()->hasSelection() &&
m_enemyInitialDataView->currentIndex().isValid())
{
int enemyIndex=m_enemyInitialDataView->currentIndex().row();
//Remove the robot from the list.
m_enemyList.removeAt(enemyIndex);
//Remove the initial data from the model.
m_enemyInitialDataModel->removeRow(enemyIndex);
//Update all.
emit requireUpdateAllList();
}
});
connect(m_okay,
static_cast<void (QPushButton::*)(bool)>(&QPushButton::clicked),
[=]
{
//Check if ground is not nullptr.
if(m_ground)
{
//Check the position of all the robots.
QList<QPointF> positions=positionList();
for(int i=0; i<positions.size(); i++)
{
if(m_ground->border().containsPoint(positions.at(i),
Qt::WindingFill))
{
QMessageBox::information(this,
tr("Invalid positions"),
tr("The selected enemy's position is inside of the border, please change the position."));
return;
}
}
//Now, we should ask the ground to sync the data.
m_ground->syncEnemyData(m_enemyList,
positions);
//Close the management dialog.
emit requireClose();
}
});
connect(m_cancel,
static_cast<void (QPushButton::*)(bool)>(&QPushButton::clicked),
[=]{emit requireClose();});
connect(LanguageManager::instance(), SIGNAL(languageChanged()),
this, SLOT(retranslate()));
retranslate();
}
void EnemyManageWidget::showEvent(QShowEvent *event)
{
//Show the widget.
QWidget::showEvent(event);
//Update the robot list.
updateEnemyList();
}
void EnemyManageWidget::retranslate()
{
m_okay->setText(tr("Ok"));
m_cancel->setText(tr("Cancel"));
m_deletePoint->setText(tr("Delete"));
m_manageBox->setTitle(tr("Enemies Force"));
updateHeader();
}
inline qreal EnemyManageWidget::getModelData(int row, int column, int role)
{
return m_enemyInitialDataModel->data(
m_enemyInitialDataModel->index(row, column), role).toDouble();
}
void EnemyManageWidget::updateHeader()
{
QStringList header;
header << tr("X") << tr("Y");
m_enemyInitialDataModel->setHorizontalHeaderLabels(header);
}
void EnemyManageWidget::updateEnemyList()
{
//Reset the model.
m_enemyInitialDataModel->clear();
//Reset all the cache information.
m_enemyList.clear();
updateHeader();
//Check ground first.
if(m_ground==nullptr)
{
//Update all the information.
emit requireUpdateAllList();
return;
}
//Get the robots information.
m_enemyList=m_ground->enemyList();
QList<QPointF> positionList=m_ground->enemyInitialPosition();
//Generate the robot row.
for(int i=0; i<m_enemyList.size(); i++)
{
//Get the robot info.
const QPointF &robotPos=positionList.at(i);
//Generate the robot row.
QList<QStandardItem *> robotRow;
QStandardItem *item=new QStandardItem(QString::number(robotPos.x()));
robotRow.append(item);
item=new QStandardItem(QString::number(robotPos.y()));
robotRow.append(item);
//Add the robot row to model.
m_enemyInitialDataModel->appendRow(robotRow);
}
//Check the row count.
if(m_enemyInitialDataModel->rowCount()>0)
{
m_enemyInitialDataView->setCurrentIndex(
m_enemyInitialDataModel->index(0,0));
}
//Update all the information.
emit requireUpdateAllList();
}
GroundBase *EnemyManageWidget::ground() const
{
return m_ground;
}
QList<Enemy *> EnemyManageWidget::enemyList()
{
return m_enemyList;
}
QList<QPointF> EnemyManageWidget::positionList()
{
//Generate the position list.
QList<QPointF> pointList;
for(int i=0; i<m_enemyInitialDataModel->rowCount(); i++)
{
pointList.append(QPointF(getModelData(i, 0),
getModelData(i, 1)));
}
return pointList;
}
void EnemyManageWidget::enabledWidget()
{
m_okay->setDefault(true);
setEnabled(true);
}
void EnemyManageWidget::disabledWidget()
{
m_okay->setDefault(false);
setEnabled(false);
}
void EnemyManageWidget::setGround(GroundBase *ground)
{
//Save the new ground.
m_ground = ground;
}