-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindow.cpp
More file actions
156 lines (120 loc) · 6.16 KB
/
MainWindow.cpp
File metadata and controls
156 lines (120 loc) · 6.16 KB
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
/*
* File: MainWindow.cpp
* Created: 2018-11-24
*
* Copyright (c) shecks 2018 <shecks@gmail.com>
* All rights reserved.
*
* This file is part of %QT_PROJECT_NAME%.
*
* %QT_PROJECT_NAME% 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.
*
* %QT_PROJECT_NAME% 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 %QT_PROJECT_NAME%. If not, see <https://www.gnu.org/licenses/>.
*/
#include <QDockWidget>
#include <QMessageBox>
#include <QtDebug>
#include "MainWindow.h"
#include "ScreepStudioApplication.h"
#include "./widgets/PropertiesWidget.h"
#include "./widgets/InvasionWidget.h"
#include "./widgets/ConsoleWidget.h"
#include "./widgets/room/RoomGraphicsView.h"
#include "ui_MainWindow.h"
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// MainWindow
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
MainWindow::MainWindow(QWidget* parent /* = nullptr */)
: QMainWindow(parent),
_ui(new Ui::MainWindow) {
_ui->setupUi(this);
_ui->consoleWidget->setModel(application()->consoleModel());
addDockWidgetMenuItem(_ui->menuWindow, _ui->toolBoxDockWidget, QKeySequence(Qt::CTRL + Qt::Key_T), ":/assets/images/toolbox.svg");
addDockWidgetMenuItem(_ui->menuWindow, _ui->invasionDockWidget, QKeySequence(Qt::CTRL + Qt::Key_N), ":/assets/images/invader.svg");
addDockWidgetMenuItem(_ui->menuWindow, _ui->propertiesDockWidget, QKeySequence(Qt::CTRL + Qt::Key_P), ":/assets/images/properties.svg");
addDockWidgetMenuItem(_ui->menuWindow, _ui->inspectorDockWidget, QKeySequence(Qt::CTRL + Qt::Key_I), ":/assets/images/object.svg");
addDockWidgetMenuItem(_ui->menuWindow, _ui->consoleDockWidget, QKeySequence(Qt::CTRL + Qt::Key_C), ":/assets/images/console.svg");
connect(_ui->actionOpen, &QAction::triggered, this, &MainWindow::unimplementedAction);
connect(_ui->actionZoomIn, &QAction::triggered, this, &MainWindow::unimplementedAction);
connect(_ui->actionZoomOut, &QAction::triggered, this, &MainWindow::unimplementedAction);
connect(_ui->actionAbout, &QAction::triggered, this, &MainWindow::showAboutMessage);
connect(_ui->actionExit, &QAction::triggered, this, &MainWindow::close);
Preferences& prefs = application()->prefs();
prefs.restoreWindowState("MainWindow", this);
_ui->actionShowGrid->setChecked(prefs.showGrid());
}
MainWindow::~MainWindow() {
delete _ui;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void MainWindow::openRoom(const QString& roomName, const QString& shardName /* = QString() */) {
RoomModel::TSharedPtr roomModel;
if((roomModel = application()->createRoomModel(roomName, shardName)) != nullptr) {
RoomGraphicsView* roomGraphicsView = new RoomGraphicsView(roomModel, this);
QWidget* widget = new QWidget();
QLayout* layout = new QVBoxLayout(widget);
// FIXME: There's a memory leak here but this needs to be re-written anyway so it will be fixed then :)
layout->addWidget (roomGraphicsView);
_ui->tabWidget->addTab(widget, QIcon(":/assets/images/object.svg"), roomModel->roomName());
connect(roomGraphicsView, &RoomGraphicsView::itemSelected, _ui->inspectorWidget, &InspectorWidget::setSelectedItem);
connect(_ui->actionShowGrid, &QAction::triggered, roomGraphicsView, &RoomGraphicsView::showGrid);
roomGraphicsView->showGrid(_ui->actionShowGrid->isChecked());
roomGraphicsView->open();
}
}
void MainWindow::unimplementedAction() {
QMessageBox::information (this, tr("Unimplemented Function"), tr("FIXME: This functionality has not been implemented."));
}
void MainWindow::showAboutMessage() {
QMessageBox::about(this,tr("About"),
QString(tr("%1\n%2 (shecks@gmail.com)\n\nNote: This application is in development and not feature complete."))
.arg(application()->applicationName())
.arg(application()->organizationName()));
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void MainWindow::closeEvent(QCloseEvent* event) {
#ifndef QT_DEBUG
int result = QMessageBox::question (this, tr("Exit Application"), tr("Are you sure you want to exit the application?"), QMessageBox::Yes|QMessageBox::No);
#else
int result = QMessageBox::Yes;
#endif // QT_DEBUG
switch(result) {
case QMessageBox::Yes: {
for(int index = 0; index < _ui->tabWidget->count(); ++index) {
QWidget* child = _ui->tabWidget->widget(index);
QLayoutItem* layoutItem = child->layout()->takeAt(0);
RoomGraphicsView* roomGraphicsView = static_cast<RoomGraphicsView *>(layoutItem->widget ());
Q_ASSERT(roomGraphicsView != nullptr);
roomGraphicsView->close();
}
Preferences& prefs = application()->prefs();
prefs.setShowGrid(_ui->actionShowGrid->isChecked());
prefs.saveWindowState("MainWindow", this);
event->accept();
}
break;
default: {
event->ignore();
}
break;
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ScreepStudioApplication* MainWindow::application() const {
return qobject_cast<ScreepStudioApplication *>(QApplication::instance ());
}
void MainWindow::addDockWidgetMenuItem(QMenu* menu, const QDockWidget* dockWidget, QKeySequence shortcut, const QString& icon) {
QAction* action = dockWidget->toggleViewAction();
action->setShortcut(shortcut);
action->setIcon(QIcon(icon));
menu->addAction(action);
}