Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: 31bcf60089d07b4c69930387a23e2e73f3c5cb61 (plain)
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
/*
   mainwindow.cc
   This file is part of the Project::OSiRiON world editor 
   and is distributed under the terms and conditions of 
   the GNU General Public License version 2
*/

#include "mainwindow.h"
#include "editorwindow.h"

#include "config.h"

#include <QMenuBar>
#include <QStatusBar>
#include <QFileDialog>

namespace editor
{

/**
 * @brief MainWindow is the application's main window.
 * The main window contains a QMdiArea that manages ZoneEditor children
 * */
MainWindow::MainWindow()
{
	// set window title
	setWindowTitle(tr("Project::OSiRiON world editor") + " " + PACKAGE_VERSION);
	
	// initialize MDI (multiple document interface) area
	mainwindow_mdiarea = new QMdiArea();
	mainwindow_mdiarea->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
	mainwindow_mdiarea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
	setCentralWidget(mainwindow_mdiarea);
	
	connect(mainwindow_mdiarea, SIGNAL(subWindowActivated(QMdiSubWindow*)), this, SLOT(updateMenus()));

	// initialize actions
	initActions();
	
	// initialize menu bar
	initMenus();
	updateMenus();
	
	// initialize status bar
	initStatus();

}

void MainWindow::initActions()
{
	// File -> New
	action_new = new QAction( tr("&New..."), this);
	action_new->setStatusTip(tr("Create a new zone"));
	action_new->setShortcuts(QKeySequence::New);
	connect(action_new, SIGNAL(triggered()), this, SLOT(slot_new()));
	
	// File -> Open
	action_open = new QAction( tr("&Open..."), this);
	action_open->setShortcuts(QKeySequence::Open);
	action_open->setStatusTip(tr("Open an existing zone .ini file"));
	connect(action_open, SIGNAL(triggered()), this, SLOT(slot_open()));
	
	action_save  = new QAction( tr("&Save"), this);
	action_save->setStatusTip(tr("Save zone to current .ini file"));
	action_save->setShortcuts(QKeySequence::Save);
	connect(action_save, SIGNAL(triggered()), this, SLOT(slot_save()));
	
	action_save_as  = new QAction( tr("Save &As..."), this);
	action_save_as->setStatusTip(tr("Save zone to a new .ini file"));
	action_save_as->setShortcuts(QKeySequence::SaveAs);
	connect(action_save_as, SIGNAL(triggered()), this, SLOT(slot_save_as()));
	
	// File -> Quit
	action_quit = new QAction(tr("&Quit"), this);
	action_quit->setShortcuts(QKeySequence::Quit);
	action_quit->setStatusTip(tr("Exit the application"));
	connect(action_quit, SIGNAL(triggered()), qApp, SLOT(closeAllWindows()));
	
	// Edit -> Add
	action_add = new QAction(tr("&Add"), this);
	action_add->setStatusTip(tr("Add a new entity"));
	connect(action_add, SIGNAL(triggered()), this, SLOT(slot_add()));
	
	action_delete = new QAction(tr("&Delete"), this);
	action_delete->setStatusTip(tr("Delete selected entities"));
	action_delete->setShortcuts(QKeySequence::Delete);
	connect(action_delete, SIGNAL(triggered()), this, SLOT(slot_delete()));
	
	action_duplicate = new QAction(tr("D&uplicate"), this);
	action_duplicate->setStatusTip(tr("Duplicate selected entities"));
	action_duplicate->setShortcut(QKeySequence(Qt::Key_Space));
	connect(action_duplicate, SIGNAL(triggered()), this, SLOT(slot_duplicate()));	
}

void MainWindow::initMenus()
{
	mainwindow_filemenu = menuBar()->addMenu(tr("&File"));
	mainwindow_filemenu->addAction(action_new);
	mainwindow_filemenu->addAction(action_open);
	mainwindow_filemenu->addAction(action_save);
	mainwindow_filemenu->addAction(action_save_as);
	mainwindow_filemenu->addSeparator();
	mainwindow_filemenu->addAction(action_quit);

	mainwindow_editmenu = menuBar()->addMenu(tr("&Edit"));
	mainwindow_editmenu->addAction(action_add);
	mainwindow_editmenu->addAction(action_delete);
	mainwindow_editmenu->addAction(action_duplicate);
}

void MainWindow::initStatus()
{
	setStatusBar(new QStatusBar(this));
	statusBar()->showMessage(tr("Project::OSiRiON world editor") + " " + PACKAGE_VERSION);
}

void MainWindow::updateMenus()
{
	bool active =(mainwindow_mdiarea->activeSubWindow() != 0);
	
	action_save_as->setEnabled(active);
	action_save->setEnabled(active);
	
	action_add->setEnabled(active);
	action_delete->setEnabled(active);
	action_duplicate->setEnabled(active);
}

EditorWindow *MainWindow::active_child()
{
	if (QMdiSubWindow *active_subwindow = mainwindow_mdiarea->activeSubWindow())
		return qobject_cast<EditorWindow *>(active_subwindow->widget());
	return 0;
}

QMdiSubWindow *MainWindow::find_child(const QString &filename)
{

	QString canonical = QFileInfo(filename).canonicalFilePath();

	foreach (QMdiSubWindow *subwindow, mainwindow_mdiarea->subWindowList()) {
		EditorWindow *editorwindow = qobject_cast<EditorWindow *>(subwindow->widget());
		
		if (canonical == QFileInfo(editorwindow->filename()).canonicalFilePath())
			return subwindow;
	}
	return 0;

}

EditorWindow *MainWindow::add_child()
{
	// create a child widget
	EditorWindow *child_widget = new EditorWindow();
	
	// add the widget to the MDI area,
	// this will wrap an QMdiSubWindow around it
	QMdiSubWindow *subwindow = mainwindow_mdiarea->addSubWindow(child_widget);
	//subwindow->resize(768, 512);
	
	child_widget->show();
	
	return child_widget;
}

void MainWindow::slot_new()
{
	add_child();
}

void MainWindow::slot_open()
{
	QString filename = QFileDialog::getOpenFileName(this, tr("Open..."));
     
	if (!filename.isEmpty()) {
		QMdiSubWindow *subwindow = find_child(filename);
		
		if (subwindow) {
			mainwindow_mdiarea->setActiveSubWindow(subwindow);
		} else {
			EditorWindow *editorwindow = add_child();
						
			if (!editorwindow->loadFile(filename)) {
				subwindow = find_child(filename);
				subwindow->close();
			} else {			
				subwindow = find_child(filename);
				if (subwindow) {
					subwindow->setWindowTitle(filename);
				}
			}
		}
	}
}

void MainWindow::slot_save_as()
{
	EditorWindow *editorwindow = active_child();
	if (editorwindow) {
		QString filename = QFileDialog::getSaveFileName(this, tr("Save as..."));
		
		if (!filename.isEmpty()) {
			if (!editorwindow->saveFile(filename))
				return;
			
			QMdiSubWindow *subwindow = find_child(filename);
			if (subwindow) {
				subwindow->setWindowTitle(filename);
			}
		}
	}
}

void MainWindow::slot_save()
{
	if (active_child()) {
		if (active_child()->filename().isEmpty()) {
			slot_save_as();
			return;
		} else {
			active_child()->saveFile(active_child()->filename());
		}
	}
}

void MainWindow::slot_add()
{
	if (active_child()) {
		active_child()->addEntity();
	}
}

void MainWindow::slot_delete()
{
	if (active_child()) {
		active_child()->deleteSelected();
	}
}

void MainWindow::slot_duplicate()
{
	if (active_child()) {
		active_child()->duplicateSelected();
	}
}

}