forked from endless-sky/endless-sky-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.cpp
266 lines (188 loc) · 6.96 KB
/
MainWindow.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
/* MainWindow.cpp
Copyright (c) 2015 by Michael Zahniser
Endless Sky 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 3 of the License, or (at your option) any later version.
Endless Sky 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.
*/
#include "MainWindow.h"
#include "DetailView.h"
#include "GalaxyView.h"
#include "Map.h"
#include "PlanetView.h"
#include "SystemView.h"
#include <QAction>
#include <QDragEnterEvent>
#include <QFileDialog>
#include <QHBoxLayout>
#include <QMenu>
#include <QMenuBar>
#include <QMessageBox>
#include <QMimeData>
#include <QSizePolicy>
#include <QTabWidget>
using namespace std;
MainWindow::MainWindow(Map &map, QWidget *parent)
: QMainWindow(parent), map(map)
{
CreateWidgets();
CreateMenus();
setAcceptDrops(true);
resize(1200, 900);
show();
}
MainWindow::~MainWindow()
{
}
void MainWindow::DoOpen(const QString &path)
{
if(path.isEmpty())
return;
map.Load(path);
galaxyView->Center();
systemView->Select(nullptr);
planetView->Reinitialize();
tabs->setCurrentWidget(galaxyView);
update();
galaxyView->update();
}
void MainWindow::Open()
{
// TODO: ask about saving changes.
QString dir = map.DataDirectory();
QString path = QFileDialog::getOpenFileName(this, "Open map file", dir);
if(!path.isEmpty())
DoOpen(path);
}
void MainWindow::Save()
{
QString dir = map.DataDirectory();
QString path = QFileDialog::getSaveFileName(this, "Save map file", dir);
if(!path.isEmpty())
map.Save(path);
}
void MainWindow::Quit()
{
close();
}
void MainWindow::TabChanged(int)
{
if(tabs)
{
galaxyMenu->setEnabled(tabs->currentWidget() == galaxyView);
systemMenu->setEnabled(tabs->currentWidget() == systemView);
}
}
void MainWindow::keyPressEvent(QKeyEvent *event)
{
if(tabs)
{
if(tabs->currentWidget() == galaxyView)
galaxyView->KeyPress(event);
}
}
void MainWindow::closeEvent(QCloseEvent * /*event*/)
{
if(map.IsChanged())
{
QMessageBox::StandardButton button = QMessageBox::question(this, "Save changes?",
"Save changes to the map file before quitting?");
if(button == QMessageBox::Yes)
Save();
}
}
void MainWindow::dragEnterEvent(QDragEnterEvent *event)
{
const QMimeData *mime = event->mimeData();
if(mime->hasUrls() && mime->urls().size() == 1 && mime->text().startsWith("file://"))
event->acceptProposedAction();
}
void MainWindow::dropEvent(QDropEvent *event)
{
QString path = event->mimeData()->urls().at(0).path();
#ifdef _WIN32
// Clean up Windows paths.
if(path.startsWith("/"))
path = path.mid(1);
#endif
DoOpen(path);
}
void MainWindow::CreateWidgets()
{
QWidget *box = new QWidget(this);
QHBoxLayout *layout = new QHBoxLayout(box);
setCentralWidget(box);
tabs = new QTabWidget(box);
QSizePolicy policy(QSizePolicy::Expanding, QSizePolicy::Expanding);
tabs->setSizePolicy(policy);
galaxyView = new GalaxyView(map, tabs, tabs);
detailView = new DetailView(map, galaxyView, box);
detailView->setMinimumWidth(300);
detailView->setMaximumWidth(300);
systemView = new SystemView(map, detailView, tabs, tabs);
auto it = map.Systems().find("Sol");
if(it != map.Systems().end())
systemView->Select(&it->second);
galaxyView->SetSystemView(systemView);
planetView = new PlanetView(map, tabs);
systemView->SetPlanetView(planetView);
layout->addWidget(detailView);
tabs->addTab(galaxyView, "Galaxy");
tabs->addTab(systemView, "System");
tabs->addTab(planetView, "Planet");
layout->addWidget(tabs);
connect(tabs, SIGNAL(currentChanged(int)), this, SLOT(TabChanged(int)));
}
void MainWindow::CreateMenus()
{
QMenu *fileMenu = menuBar()->addMenu("File");
QAction *openAction = fileMenu->addAction("Open...", this, SLOT(Open()));
openAction->setShortcut(QKeySequence::Open);
QAction *saveAction = fileMenu->addAction("Save...", this, SLOT(Save()));
saveAction->setShortcut(QKeySequence::Save);
fileMenu->addSeparator();
QAction *quitAction = fileMenu->addAction("Quit", this, SLOT(Quit()));
quitAction->setShortcut(QKeySequence::Quit);
galaxyMenu = menuBar()->addMenu("Galaxy");
QAction *deleteSystemAction = galaxyMenu->addAction("Delete System");
connect(deleteSystemAction, SIGNAL(triggered()), galaxyView, SLOT(DeleteSystem()));
deleteSystemAction->setShortcut(QKeySequence(Qt::Key_Backspace));
systemMenu = menuBar()->addMenu("System");
QAction *randomInhabited = systemMenu->addAction("Randomize (Inhabited)");
connect(randomInhabited, SIGNAL(triggered()), systemView, SLOT(RandomizeInhabited()));
randomInhabited->setShortcut(QKeySequence("I"));
QAction *randomSystem = systemMenu->addAction("Randomize");
connect(randomSystem, SIGNAL(triggered()), systemView, SLOT(Randomize()));
randomSystem->setShortcut(QKeySequence("R"));
QAction *randomUninhabited = systemMenu->addAction("Randomize (Uninhabited)");
randomUninhabited->setShortcut(QKeySequence("U"));
connect(randomUninhabited, SIGNAL(triggered()), systemView, SLOT(RandomizeUninhabited()));
systemMenu->addSeparator();
QAction *changeAsteroids = systemMenu->addAction("Change Asteroids");
connect(changeAsteroids, SIGNAL(triggered()), systemView, SLOT(ChangeAsteroids()));
changeAsteroids->setShortcut(QKeySequence("A"));
systemMenu->addSeparator();
QAction *changeStar = systemMenu->addAction("Change Star");
connect(changeStar, SIGNAL(triggered()), systemView, SLOT(ChangeStar()));
changeStar->setShortcut(QKeySequence("C"));
QAction *addPlanet = systemMenu->addAction("Add/Change Planet");
connect(addPlanet, SIGNAL(triggered()), systemView, SLOT(ChangePlanet()));
addPlanet->setShortcut(QKeySequence("P"));
QAction *addMoon = systemMenu->addAction("Add/Change Moon");
connect(addMoon, SIGNAL(triggered()), systemView, SLOT(ChangeMoon()));
addMoon->setShortcut(QKeySequence("M"));
QAction *addStation = systemMenu->addAction("Add/Change Station");
connect(addStation, SIGNAL(triggered()), systemView, SLOT(ChangeStation()));
addStation->setShortcut(QKeySequence("S"));
QAction *deleteObject = systemMenu->addAction("Delete Object");
connect(deleteObject, SIGNAL(triggered()), systemView, SLOT(DeleteObject()));
deleteObject->setShortcut(QKeySequence("X"));
systemMenu->addSeparator();
QAction *pause = systemMenu->addAction("Pause/Unpause");
connect(pause, SIGNAL(triggered()), systemView, SLOT(Pause()));
pause->setShortcut(QKeySequence(Qt::Key_Space));
// Activate only the menu for the current tab.
TabChanged(0);
}