Fix game:addSpawnShapesByZone
[ryzomcore.git] / studio / src / plugins / core / main_window.cpp
blob95b3e2320eccf38b06618e2bb35916157d57dace
1 // Object Viewer Qt - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2011 Dzmitry KAMIAHIN (dnk-88) <dnk-88@tut.by>
3 //
4 // This source file has been modified by the following contributors:
5 // Copyright (C) 2010 Winch Gate Property Limited
6 // Copyright (C) 2014 Laszlo KIS-ADAM (dfighter) <dfighter1985@gmail.com>
7 //
8 // This program is free software: you can redistribute it and/or modify
9 // it under the terms of the GNU Affero General Public License as
10 // published by the Free Software Foundation, either version 3 of the
11 // License, or (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU Affero General Public License for more details.
18 // You should have received a copy of the GNU Affero General Public License
19 // along with this program. If not, see <http://www.gnu.org/licenses/>.
21 // Project includes
22 #include "main_window.h"
23 #include "icontext.h"
24 #include "icore_listener.h"
25 #include "menu_manager.h"
26 #include "context_manager.h"
27 #include "core.h"
28 #include "core_constants.h"
29 #include "settings_dialog.h"
31 // NeL includes
32 #include <nel/misc/debug.h>
34 // Qt includes
35 #include <QtCore/QCoreApplication>
36 #include <QtGui/QUndoView>
37 #include <QtGui/QtGui>
39 namespace Core
42 MainWindow::MainWindow(ExtensionSystem::IPluginManager *pluginManager, QWidget *parent)
43 : QMainWindow(parent),
44 m_pluginManager(0),
45 m_menuManager(0),
46 m_contextManager(0),
47 m_coreImpl(0),
48 m_lastDir("."),
49 m_undoGroup(0),
50 m_settings(0)
52 QCoreApplication::setApplicationName(QLatin1String("Studio"));
53 QCoreApplication::setApplicationVersion(QLatin1String(Core::Constants::OVQT_VERSION_LONG));
54 QCoreApplication::setOrganizationName(QLatin1String("RyzomCore"));
56 setObjectName(Constants::MAIN_WINDOW);
57 setWindowIcon(QIcon(Constants::ICON_PILL));
58 setWindowTitle(tr("Ryzom Core Studio"));
60 m_pluginManager = pluginManager;
61 m_settings = m_pluginManager->settings();
62 m_coreImpl = new CoreImpl(this);
64 #ifdef Q_WS_MAC
65 m_menuBar = new QMenuBar(0);
66 #else
67 m_menuBar = new QMenuBar(this);
68 setMenuBar(m_menuBar);
69 #endif
71 m_menuManager = new MenuManager(m_menuBar, this);
73 m_tabWidget = new QTabWidget(this);
74 m_tabWidget->setTabPosition(QTabWidget::South);
75 m_tabWidget->setMovable(false);
76 m_tabWidget->setDocumentMode(true);
77 setCentralWidget(m_tabWidget);
79 m_contextManager = new ContextManager(this, m_tabWidget);
81 setDockNestingEnabled(true);
82 m_originalPalette = QApplication::palette();
83 m_undoGroup = new QUndoGroup(this);
85 createDialogs();
86 createActions();
87 createMenus();
88 createStatusBar();
89 resize(1024, 768);
92 MainWindow::~MainWindow()
94 m_pluginManager->removeObject(m_coreImpl);
95 m_pluginManager->removeObject(m_menuManager);
97 delete m_coreImpl;
98 m_coreImpl = 0;
101 bool MainWindow::initialize(QString *errorString)
103 Q_UNUSED(errorString);
104 m_pluginManager->addObject(m_coreImpl);
105 m_pluginManager->addObject(m_menuManager);
106 return true;
109 void MainWindow::extensionsInitialized()
111 readSettings();
112 connect(m_contextManager, SIGNAL(currentContextChanged(Core::IContext *)),
113 this, SLOT(updateContext(Core::IContext *)));
115 Core::IContext *context = m_contextManager->currentContext();
116 if (context != NULL)
118 updateContext(context);
119 context->onActivated();
122 show();
125 MenuManager *MainWindow::menuManager() const
127 return m_menuManager;
130 ContextManager *MainWindow::contextManager() const
132 return m_contextManager;
135 QSettings *MainWindow::settings() const
137 return m_settings;
140 QUndoGroup *MainWindow::undoGroup() const
142 return m_undoGroup;
145 ExtensionSystem::IPluginManager *MainWindow::pluginManager() const
147 return m_pluginManager;
150 void MainWindow::addContextObject(IContext *context)
152 QUndoStack *stack = context->undoStack();
153 if (stack)
154 m_undoGroup->addStack(stack);
157 void MainWindow::removeContextObject(IContext *context)
159 QUndoStack *stack = context->undoStack();
160 if (stack)
161 m_undoGroup->removeStack(stack);
164 void MainWindow::open()
166 m_contextManager->currentContext()->open();
169 void MainWindow::newFile()
171 m_contextManager->currentContext()->newDocument();
174 void MainWindow::save()
176 m_contextManager->currentContext()->save();
179 void MainWindow::saveAs()
181 m_contextManager->currentContext()->saveAs();
184 void MainWindow::saveAll()
188 void MainWindow::closeDocument()
190 m_contextManager->currentContext()->close();
194 void MainWindow::cut()
198 void MainWindow::copy()
202 void MainWindow::paste()
206 void MainWindow::del()
210 void MainWindow::find()
214 void MainWindow::gotoPos()
218 void MainWindow::setFullScreen(bool enabled)
220 if (bool(windowState() & Qt::WindowFullScreen) == enabled)
221 return;
222 if (enabled)
223 setWindowState(windowState() | Qt::WindowFullScreen);
224 else
225 setWindowState(windowState() & ~Qt::WindowFullScreen);
228 bool MainWindow::showOptionsDialog(const QString &group,
229 const QString &page,
230 QWidget *parent)
232 if (!parent)
233 parent = this;
234 SettingsDialog settingsDialog(m_pluginManager, group, page, parent);
235 settingsDialog.show();
236 bool ok = settingsDialog.execDialog();
237 if (ok)
238 Q_EMIT m_coreImpl->changeSettings();
239 return ok;
242 void MainWindow::about()
244 QMessageBox::about(this, tr("About Ryzom Core Studio"),
245 tr("<h2>Ryzom Core Studio</h2>"
246 "<p> Ryzom Core team <p>Compiled on %1 %2").arg(__DATE__).arg(__TIME__));
249 void MainWindow::updateContext(Core::IContext *context)
251 m_undoGroup->setActiveStack(context->undoStack());
254 void MainWindow::closeEvent(QCloseEvent *event)
256 QList<ICoreListener *> listeners = m_pluginManager->getObjects<ICoreListener>();
257 Q_FOREACH(ICoreListener *listener, listeners)
259 if (!listener->closeMainWindow())
261 event->ignore();
262 return;
265 Q_EMIT m_coreImpl->closeMainWindow();
267 writeSettings();
268 event->accept();
271 void MainWindow::createActions()
273 m_newAction = new QAction(tr("&New"), this);
274 m_newAction->setIcon(QIcon(Constants::ICON_NEW));
275 m_newAction->setShortcut(QKeySequence::New);
276 menuManager()->registerAction(m_newAction, Constants::NEW);
277 connect(m_newAction, SIGNAL(triggered()), this, SLOT(newFile()));
278 m_newAction->setEnabled(false);
280 m_openAction = new QAction(tr("&Open..."), this);
281 m_openAction->setIcon(QIcon(Constants::ICON_OPEN));
282 m_openAction->setShortcut(QKeySequence::Open);
283 m_openAction->setStatusTip(tr("Open an existing file"));
284 menuManager()->registerAction(m_openAction, Constants::OPEN);
285 connect(m_openAction, SIGNAL(triggered()), this, SLOT(open()));
287 m_saveAction = new QAction(tr("&Save"), this);
288 m_saveAction->setIcon(QIcon(Constants::ICON_SAVE));
289 m_saveAction->setShortcut(QKeySequence::Save);
290 menuManager()->registerAction(m_saveAction, Constants::SAVE);
291 connect(m_saveAction, SIGNAL(triggered()), this, SLOT(save()));
292 m_saveAction->setEnabled(false);
294 m_saveAsAction = new QAction(tr("Save &As..."), this);
295 m_saveAsAction->setIcon(QIcon(Constants::ICON_SAVE_AS));
296 m_saveAsAction->setShortcut(QKeySequence::SaveAs);
297 menuManager()->registerAction(m_saveAsAction, Constants::SAVE_AS);
298 connect(m_saveAsAction, SIGNAL(triggered()), this, SLOT(saveAs()));
299 m_saveAsAction->setEnabled(false);
301 m_saveAllAction = new QAction(tr("&Save A&ll"), this);
302 m_saveAllAction->setShortcut(QKeySequence::SelectAll);
303 menuManager()->registerAction(m_saveAllAction, Constants::SAVE_ALL);
304 connect(m_saveAllAction, SIGNAL(triggered()), this, SLOT(saveAll()));
305 m_saveAllAction->setEnabled(false);
307 m_closeAction = new QAction(tr("Close"), this);
308 m_closeAction->setShortcut(QKeySequence::Close);
309 menuManager()->registerAction(m_closeAction, Constants::CLOSE);
310 connect(m_closeAction, SIGNAL(triggered()), this, SLOT(closeDocument()));
311 m_closeAction->setEnabled(false);
313 m_exitAction = new QAction(tr("E&xit"), this);
314 m_exitAction->setShortcut(QKeySequence(tr("Ctrl+Q")));
315 m_exitAction->setStatusTip(tr("Exit the application"));
316 menuManager()->registerAction(m_exitAction, Constants::EXIT);
317 connect(m_exitAction, SIGNAL(triggered()), this, SLOT(close()));
319 m_cutAction = new QAction(tr("Cu&t"), this);
320 m_cutAction->setShortcut(QKeySequence::Cut);
321 menuManager()->registerAction(m_cutAction, Constants::CUT);
322 connect(m_cutAction, SIGNAL(triggered()), this, SLOT(cut()));
323 m_cutAction->setEnabled(false);
325 m_copyAction = new QAction(tr("&Copy"), this);
326 m_copyAction->setShortcut(QKeySequence::Copy);
327 menuManager()->registerAction(m_copyAction, Constants::COPY);
328 connect(m_copyAction, SIGNAL(triggered()), this, SLOT(copy()));
329 m_copyAction->setEnabled(false);
331 m_pasteAction = new QAction(tr("&Paste"), this);
332 m_pasteAction->setShortcut(QKeySequence::Paste);
333 menuManager()->registerAction(m_pasteAction, Constants::PASTE);
334 connect(m_pasteAction, SIGNAL(triggered()), this, SLOT(paste()));
335 m_pasteAction->setEnabled(false);
337 m_delAction = new QAction(tr("&Delete"), this);
338 m_delAction->setShortcut(QKeySequence::Delete);
339 menuManager()->registerAction(m_delAction, Constants::DEL);
340 connect(m_delAction, SIGNAL(triggered()), this, SLOT(del()));
341 m_delAction->setEnabled(false);
343 m_selectAllAction = new QAction(tr("Select &All"), this);
344 m_selectAllAction->setShortcut(QKeySequence::SelectAll);
345 menuManager()->registerAction(m_selectAllAction, Constants::SELECT_ALL);
346 connect(m_selectAllAction, SIGNAL(triggered()), this, SLOT(selectAll()));
347 m_selectAllAction->setEnabled(false);
349 m_findAction = new QAction(tr("&Find"), this);
350 m_findAction->setShortcut(QKeySequence::Find);
351 menuManager()->registerAction(m_findAction, Constants::FIND);
352 connect(m_findAction, SIGNAL(triggered()), this, SLOT(find()));
353 m_findAction->setEnabled(false);
355 m_gotoAction = new QAction(tr("&Go To.."), this);
356 m_gotoAction->setShortcut(QKeySequence(tr("Ctrl+G")));
357 menuManager()->registerAction(m_gotoAction, Constants::GOTO_POS);
358 connect(m_gotoAction, SIGNAL(triggered()), this, SLOT(gotoPos()));
359 m_gotoAction->setEnabled(false);
361 m_fullscreenAction = new QAction(tr("Fullscreen"), this);
362 m_fullscreenAction->setCheckable(true);
363 m_fullscreenAction->setShortcut(QKeySequence(tr("Ctrl+Shift+F11")));
364 menuManager()->registerAction(m_fullscreenAction, Constants::TOGGLE_FULLSCREEN);
365 connect(m_fullscreenAction, SIGNAL(triggered(bool)), this, SLOT(setFullScreen(bool)));
367 m_settingsAction = new QAction(tr("&Settings"), this);
368 m_settingsAction->setIcon(QIcon(":/images/preferences.png"));
369 m_settingsAction->setShortcut(QKeySequence::Preferences);
370 m_settingsAction->setStatusTip(tr("Open the settings dialog"));
371 menuManager()->registerAction(m_settingsAction, Constants::SETTINGS);
372 connect(m_settingsAction, SIGNAL(triggered()), this, SLOT(showOptionsDialog()));
374 m_aboutAction = new QAction(tr("&About"), this);
375 m_aboutAction->setStatusTip(tr("Show the application's About box"));
376 menuManager()->registerAction(m_aboutAction, Constants::ABOUT);
377 connect(m_aboutAction, SIGNAL(triggered()), this, SLOT(about()));
379 m_aboutQtAction = new QAction(tr("About &Qt"), this);
380 m_aboutQtAction->setStatusTip(tr("Show the Qt library's About box"));
381 menuManager()->registerAction(m_aboutQtAction, Constants::ABOUT_QT);
382 connect(m_aboutQtAction, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
384 m_pluginViewAction = new QAction(tr("About &Plugins"), this);
385 m_pluginViewAction->setStatusTip(tr("Show the plugin view dialog"));
386 menuManager()->registerAction(m_pluginViewAction, Constants::ABOUT_PLUGINS);
387 connect(m_pluginViewAction, SIGNAL(triggered()), m_pluginView, SLOT(show()));
389 #ifdef Q_WS_MAC
390 m_exitAction->setMenuRole(QAction::QuitRole);
391 m_settingsAction->setMenuRole(QAction::PreferencesRole);
392 m_aboutAction->setMenuRole(QAction::AboutRole);
393 m_aboutQtAction->setMenuRole(QAction::AboutQtRole);
394 m_pluginViewAction->setMenuRole(QAction::ApplicationSpecificRole);
395 #endif
398 void MainWindow::createMenus()
400 m_fileMenu = m_menuBar->addMenu(tr("&File"));
401 menuManager()->registerMenu(m_fileMenu, Constants::M_FILE);
402 m_fileMenu->addAction(m_newAction);
403 m_fileMenu->addAction(m_openAction);
404 m_fileMenu->addSeparator();
405 m_fileMenu->addAction(m_saveAction);
406 m_fileMenu->addAction(m_saveAsAction);
407 m_fileMenu->addAction(m_saveAllAction);
408 m_fileMenu->addAction(m_closeAction);
409 m_fileMenu->addSeparator();
411 m_recentFilesMenu = m_fileMenu->addMenu(tr("Recent &Files"));
412 m_recentFilesMenu->setEnabled(false);
413 menuManager()->registerMenu(m_recentFilesMenu, Constants::M_FILE_RECENTFILES);
415 m_fileMenu->addSeparator();
416 m_fileMenu->addAction(m_exitAction);
418 m_editMenu = m_menuBar->addMenu(tr("&Edit"));
419 QAction *undoAction = m_undoGroup->createUndoAction(this);
420 menuManager()->registerAction(undoAction, Constants::UNDO);
421 undoAction->setIcon(QIcon(Constants::ICON_UNDO));
422 undoAction->setShortcut(QKeySequence::Undo);
423 QAction *redoAction = m_undoGroup->createRedoAction(this);
424 menuManager()->registerAction(redoAction, Constants::REDO);
425 redoAction->setIcon(QIcon(Constants::ICON_REDO));
426 redoAction->setShortcut(QKeySequence::Redo);
427 m_editMenu->addAction(undoAction);
428 m_editMenu->addAction(redoAction);
430 m_editMenu->addSeparator();
431 m_editMenu->addAction(m_cutAction);
432 m_editMenu->addAction(m_copyAction);
433 m_editMenu->addAction(m_pasteAction);
434 m_editMenu->addAction(m_delAction);
435 m_editMenu->addSeparator();
436 m_editMenu->addAction(m_selectAllAction);
437 m_editMenu->addSeparator();
438 m_editMenu->addAction(m_findAction);
439 m_editMenu->addAction(m_gotoAction);
440 menuManager()->registerMenu(m_editMenu, Constants::M_EDIT);
442 m_viewMenu = m_menuBar->addMenu(tr("&View"));
443 m_viewMenu->addAction(m_fullscreenAction);
444 m_viewMenu->addAction(m_dockWidget->toggleViewAction());
445 menuManager()->registerMenu(m_viewMenu, Constants::M_VIEW);
447 m_toolsMenu = m_menuBar->addMenu(tr("&Tools"));
448 menuManager()->registerMenu(m_toolsMenu, Constants::M_TOOLS);
450 // m_toolsMenu->addSeparator();
452 m_toolsMenu->addAction(m_settingsAction);
454 m_menuBar->addSeparator();
456 m_helpMenu = m_menuBar->addMenu(tr("&Help"));
457 menuManager()->registerMenu(m_helpMenu, Constants::M_HELP);
458 m_helpMenu->addAction(m_aboutAction);
459 m_helpMenu->addAction(m_aboutQtAction);
460 m_helpMenu->addAction(m_pluginViewAction);
462 m_sheetMenu = m_menuBar->addMenu(tr("&Sheet"));
463 menuManager()->registerMenu(m_sheetMenu, Constants::M_SHEET);
466 void MainWindow::createStatusBar()
468 statusBar()->showMessage(tr("StatusReady"));
471 void MainWindow::createDialogs()
473 m_pluginView = new PluginView(m_pluginManager, this);
475 // Create undo/redo command list
476 m_dockWidget = new QDockWidget("Command List", this);
477 m_dockWidget->setObjectName(QString::fromUtf8("UndoRedoCommandDockWidget"));
478 QUndoView *undoView = new QUndoView(m_undoGroup, m_dockWidget);
479 m_dockWidget->setWidget(undoView);
480 addDockWidget(Qt::RightDockWidgetArea, m_dockWidget);
483 void MainWindow::readSettings()
485 m_settings->beginGroup(Constants::MAIN_WINDOW_SECTION);
486 restoreState(m_settings->value(Constants::MAIN_WINDOW_STATE).toByteArray());
487 restoreGeometry(m_settings->value(Constants::MAIN_WINDOW_GEOMETRY).toByteArray());
488 m_settings->endGroup();
491 void MainWindow::writeSettings()
493 m_settings->beginGroup(Constants::MAIN_WINDOW_SECTION);
494 m_settings->setValue(Constants::MAIN_WINDOW_STATE, saveState());
495 m_settings->setValue(Constants::MAIN_WINDOW_GEOMETRY, saveGeometry());
496 m_settings->endGroup();
499 } /* namespace Core */
501 /* end of file */