-
Notifications
You must be signed in to change notification settings - Fork 1
/
MainWindow.cpp
380 lines (312 loc) · 12.4 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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
// MapMaker (c) 2016 Andrey Fidrya. MIT license. See LICENSE for more information.
#include <QApplication>
#include <QSplitter>
#include <QMessageBox>
#include <QCloseEvent>
#include <QTimer>
#include <QMenuBar>
#include <QFileDialog>
#include <QStandardPaths>
#include <QUndoStack>
#include "MainWindow.h"
#include "MapView.h"
#include "ObjectBrowser.h"
#include "PropertyBrowser.h"
#include "Controllers/LevelLoader.h"
#include "Controllers/ZoomController.h"
#include "Dialogs/SettingsDialog/SettingsDialog.h"
#include "Models/MapScene.h"
#include "Models/LevelObjectsModel.h"
#include "Utils/Settings.h"
#include "Utils/Utils.h"
#include "Utils/FileUtils.h"
const QString mapFilenameFilter = QObject::tr(
"MapMaker Files (*.mmj);;All files (*.*)");
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, settings_(Settings::sharedInstance())
{
connect(settings_, SIGNAL(mapFilenameChanged(QString)),
this, SLOT(updateWindowTitle()));
updateWindowTitle();
loadWindowSettings();
mapView_ = new MapView(this);
connect(mapView_, SIGNAL(sceneCreated(QGraphicsScene*)),
this, SLOT(onSceneCreated(QGraphicsScene*)));
ObjectBrowser *objectBrowser = new ObjectBrowser(this);
propertyBrowser_ = new PropertyBrowser(this);
QSplitter *rightColumnSplitter = new QSplitter(this);
rightColumnSplitter->setOrientation(Qt::Vertical);
rightColumnSplitter->addWidget(objectBrowser);
rightColumnSplitter->addWidget(propertyBrowser_);
rightColumnSplitter->setSizes(QList<int>() << 350 << 250);
QSplitter *splitter = new QSplitter(this);
splitter->addWidget(mapView_);
splitter->addWidget(rightColumnSplitter);
splitter->setSizes(QList<int>() << 400 << 200);
setCentralWidget(splitter);
onSceneCreated(mapView_->scene());
connect(settings_, SIGNAL(imagesDirectoryChanged(QString)),
this, SLOT(loadImages()));
loadImages();
qdbg << "MainWindow::MainWindow(): mapFilename=" <<
settings_->mapFilename() << Qt::endl;
if (!settings_->mapFilename().isEmpty())
QTimer::singleShot(0, this, SLOT(loadLevel()));
}
MainWindow::~MainWindow()
{
}
void MainWindow::closeEvent(QCloseEvent *event)
{
Q_UNUSED(event);
if (mapView_->mapScene()->modified()) {
QMessageBox::StandardButton result =
QMessageBox::warning(this, "Confirmation",
"Level modified. Save the changes?",
QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel);
if (result == QMessageBox::Yes) {
LevelLoader *levelLoader = LevelLoader::sharedInstance();
if (!levelLoader->saveToFile(
mapView_,
settings_->mapFilename())) {
QMessageBox::critical(this, "Error", levelLoader->lastErrorDescription());
event->ignore();
return;
}
} else if (result == QMessageBox::Cancel){
event->ignore();
return;
}
}
saveWindowSettings();
settings_->save(); // Not done automatically on Cmd-Q
event->accept();
}
void MainWindow::loadImages()
{
LevelObjectsModel *model = LevelObjectsModel::sharedInstance();
model->reset();
if (!settings_->imagesDirectory().isEmpty()) {
model->addImagesFromDirectory(settings_->imagesDirectory());
}
}
void MainWindow::updateWindowTitle()
{
QString title;
if (settings_->mapFilename().isEmpty()) {
title = "New Document | MapMaker";
} else {
QFileInfo fileInfo(settings_->mapFilename());
title = fileInfo.fileName() + " | MapMaker";
}
setWindowTitle(title);
}
void MainWindow::onNew()
{
settings_->resetMapFilename();
mapView_->resetScene();
}
void MainWindow::onOpen()
{
QString lastMapFilename = settings_->mapFilename();
QDir mapDir = QFileInfo(lastMapFilename).absoluteDir();
QString mapPath = mapDir.absolutePath();
QString dataLocation = mapPath;
if (dataLocation.isEmpty()) {
QStringList locations = QStandardPaths::standardLocations(QStandardPaths::HomeLocation);
if (!locations.empty())
dataLocation = locations.first();
}
QString mapFilename = QFileDialog::getOpenFileName(this,
tr("Open Map"), dataLocation,
mapFilenameFilter);
if (mapFilename.isNull())
return;
settings_->setMapFilename(mapFilename);
loadLevel();
}
void MainWindow::onSave()
{
LevelLoader *levelLoader = LevelLoader::sharedInstance();
if (!levelLoader->saveToFile(
mapView_,
settings_->mapFilename())) {
QMessageBox::critical(this, "Error", levelLoader->lastErrorDescription());
return;
}
mapView_->mapScene()->setModified(false);
}
void MainWindow::onSaveAs()
{
//QString lastMapFilename = settings_->mapFilename();
//QDir mapDir = QFileInfo(lastMapFilename).absoluteDir();
QFileDialog dialog;
dialog.setWindowTitle(tr("Save Map As"));
//dialog.setDirectory(mapDir);
dialog.setAcceptMode(QFileDialog::AcceptSave);
dialog.setFileMode(QFileDialog::AnyFile);
dialog.setNameFilter(mapFilenameFilter);
dialog.selectFile(settings_->mapFilename());
//dialog.setOption(QFileDialog::DontUseNativeDialog);
if (dialog.exec() == QDialog::Accepted && !dialog.selectedFiles().isEmpty()) {
QString filename = dialog.selectedFiles().first();
LevelLoader *levelLoader = LevelLoader::sharedInstance();
if (!levelLoader->saveToFile(
mapView_,
filename)) {
QMessageBox::critical(this, "Error", levelLoader->lastErrorDescription());
return;
}
mapView_->mapScene()->setModified(false);
settings_->setMapFilename(filename);
}
}
void MainWindow::onReloadImages()
{
loadImages();
}
void MainWindow::onPreferences()
{
SettingsDialog *dialog = new SettingsDialog(this);
dialog->setWindowModality(Qt::WindowModal);
dialog->setWindowFlags((dialog->windowFlags() | Qt::CustomizeWindowHint) & ~Qt::WindowMaximizeButtonHint);
dialog->setAttribute(Qt::WA_DeleteOnClose);
dialog->exec();
}
void MainWindow::loadLevel()
{
LevelLoader *levelLoader = LevelLoader::sharedInstance();
if (!levelLoader->loadFromFile(mapView_, settings_->mapFilename())) {
/* QMessageBox::StandardButton result = */
QMessageBox::warning(this, "Error",
levelLoader->lastErrorDescription(),
QMessageBox::Ok);
// If loading completed with errors, treat the map as modified
mapView_->mapScene()->setModified(true);
}
}
void MainWindow::onSceneCreated(QGraphicsScene *scene)
{
Q_UNUSED(scene);
menuBar()->clear();
createFileMenu();
createEditMenu();
createMapMenu();
createViewMenu();
connect(scene, SIGNAL(selectedLevelObjectChanged(LevelObject*)),
propertyBrowser_, SLOT(setLevelObject(LevelObject*)));
}
void MainWindow::loadWindowSettings()
{
QByteArray geometry = settings_->geometry();
if (geometry.isEmpty()) {
QScreen *screen = QApplication::primaryScreen();
if (screen) {
QRect rect = screen->availableGeometry();
resize(rect.width() * 2 / 3, rect.height() * 3 / 4);
move((rect.width() - width()) / 2,
(rect.height() - height()) / 2);
}
} else {
restoreGeometry(geometry);
}
}
void MainWindow::saveWindowSettings()
{
settings_->setGeometry(saveGeometry());
}
void MainWindow::createFileMenu()
{
QMenu *fileMenu = menuBar()->addMenu(tr("&File"));
QKeySequence newShortcut(tr("Ctrl+N", "File|New"));
fileMenu->addAction(tr("New"),
this, SLOT(onNew()), newShortcut);
QKeySequence openShortcut(tr("Ctrl+O", "File|Open..."));
fileMenu->addAction(tr("Open..."),
this, SLOT(onOpen()), openShortcut);
fileMenu->addSeparator();
QKeySequence saveShortcut(tr("Ctrl+S", "File|Save"));
fileMenu->addAction(tr("Save"),
this, SLOT(onSave()), saveShortcut);
QKeySequence saveAsShortcut(tr("Shift+Ctrl+S", "File|Save As"));
fileMenu->addAction(tr("Save As"),
this, SLOT(onSaveAs()), saveAsShortcut);
fileMenu->addSeparator();
QKeySequence reloadImagesShortcut(tr("Ctrl+R", "File|Reload Images and Metadata"));
fileMenu->addAction(tr("Reload Images and Metadata"),
this, SLOT(onReloadImages()), reloadImagesShortcut);
}
void MainWindow::createEditMenu()
{
QMenu *editMenu = menuBar()->addMenu(tr("&Edit"));
QUndoStack *undoStack = mapView_->mapScene()->undoStack();
QAction *undoAction = undoStack->createUndoAction(this, tr("&Undo"));
undoAction->setShortcuts(QKeySequence::Undo);
editMenu->addAction(undoAction);
QAction *redoAction = undoStack->createRedoAction(this, tr("&Redo"));
redoAction->setShortcuts(QKeySequence::Redo);
editMenu->addAction(redoAction);
editMenu->addSeparator();
QAction *copyAction = editMenu->addAction(
tr("&Copy"), mapView_, SLOT(copy()), QKeySequence::Copy);
copyAction->setStatusTip(tr("Copy selected objects to the clipboard"));
QAction *pasteAction = editMenu->addAction(
tr("&Paste"), mapView_, SLOT(paste()), QKeySequence::Paste);
pasteAction->setStatusTip(tr("Paste the clipboard's contents"));
editMenu->addAction(
tr("Select &All"), mapView_, SLOT(selectAll()), QKeySequence::SelectAll);
editMenu->addSeparator();
QKeySequence preferencesShortcut(tr("Ctrl+K", "Edit|Preferences..."));
editMenu->addAction(tr("Preferences..."),
this, SLOT(onPreferences()), preferencesShortcut);
}
void MainWindow::createMapMenu()
{
//QMenu *mapMenu = menuBar()->addMenu(tr("&Map"));
}
void MainWindow::createViewMenu()
{
QMenu *viewMenu = menuBar()->addMenu(tr("&View"));
ZoomController *zoomController = ZoomController::sharedInstance();
QKeySequence zoomInShortcut(tr("Ctrl++", "View|Zoom In"));
viewMenu->addAction(
tr("Zoom In"), zoomController, SLOT(nextZoomLevel()), zoomInShortcut);
QKeySequence zoomOutShortcut(tr("Ctrl+-", "View|Zoom Out"));
viewMenu->addAction(
tr("Zoom Out"), zoomController, SLOT(previousZoomLevel()), zoomOutShortcut);
QKeySequence zoom100Shortcut(tr("Ctrl+1", "View|100%"));
viewMenu->addAction(
tr("100%"), settings_, SLOT(setZoom100()), zoom100Shortcut);
QKeySequence zoom200Shortcut(tr("Ctrl+2", "View|200%"));
viewMenu->addAction(
tr("200%"), settings_, SLOT(setZoom200()), zoom200Shortcut);
QKeySequence zoom300Shortcut(tr("Ctrl+3", "View|300%"));
viewMenu->addAction(
tr("300%"), settings_, SLOT(setZoom300()), zoom300Shortcut);
QKeySequence zoom400Shortcut(tr("Ctrl+4", "View|400%"));
viewMenu->addAction(
tr("400%"), settings_, SLOT(setZoom400()), zoom400Shortcut);
viewMenu->addSeparator();
QKeySequence showGridShortcut(tr("Ctrl+'", "View|Show Grid"));
QAction *showGridAction = viewMenu->addAction(
tr("Show Grid"), settings_, SLOT(setShowGrid(bool)), showGridShortcut);
showGridAction->setCheckable(true);
showGridAction->setChecked(settings_->showGrid());
QKeySequence snapToGridShortcut(tr("Ctrl+Shift+;", "View|Snap to Grid"));
QAction *snapToGridAction = viewMenu->addAction(
tr("Snap to Grid"), settings_, SLOT(setSnapToGrid(bool)), snapToGridShortcut);
snapToGridAction->setCheckable(true);
snapToGridAction->setChecked(settings_->snapToGrid());
viewMenu->addSeparator();
QKeySequence showDockPointsShortcut(tr("Ctrl+D", "View|Show Dock Points"));
QAction *showDockPointsAction = viewMenu->addAction(
tr("Show Dock Points"), settings_, SLOT(setShowDockPoints(bool)), showDockPointsShortcut);
showDockPointsAction->setCheckable(true);
showDockPointsAction->setChecked(settings_->showDockPoints());
QKeySequence snapToDockPointsShortcut(tr("Ctrl+Shift+D", "View|Snap to Dock Points"));
QAction *snapToDockPointsAction = viewMenu->addAction(
tr("Snap to Dock Points"), settings_, SLOT(setSnapToDockPoints(bool)), snapToDockPointsShortcut);
snapToDockPointsAction->setCheckable(true);
snapToDockPointsAction->setChecked(settings_->snapToDockPoints());
}