Linux multi-monitor fullscreen support
[ryzomcore.git] / studio / src / plugins / core / context_manager.cpp
bloba44cb41e4b566af7b9d738770a32cefef4a66555
1 // Object Viewer Qt - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
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 "context_manager.h"
23 #include "icontext.h"
24 #include "main_window.h"
26 // NeL includes
27 #include <nel/misc/debug.h>
29 // Qt includes
30 #include <QtGui/QTabWidget>
31 #include <QtGui/QGridLayout>
33 namespace Core
36 struct ContextManagerPrivate
38 explicit ContextManagerPrivate(Core::MainWindow *mainWindow, QTabWidget *tabWidget);
39 Core::MainWindow *m_mainWindow;
40 QTabWidget *m_tabWidget;
41 QVector<IContext *> m_contexts;
44 ContextManagerPrivate::ContextManagerPrivate(Core::MainWindow *mainWindow, QTabWidget *tabWidget)
45 : m_mainWindow(mainWindow),
46 m_tabWidget(tabWidget)
50 ContextManager::ContextManager(Core::MainWindow *mainWindow, QTabWidget *tabWidget)
51 : d(new ContextManagerPrivate(mainWindow, tabWidget))
53 QObject::connect(d->m_mainWindow->pluginManager(), SIGNAL(objectAdded(QObject *)),
54 this, SLOT(objectAdded(QObject *)));
55 QObject::connect(d->m_mainWindow->pluginManager(), SIGNAL(aboutToRemoveObject(QObject *)),
56 this, SLOT(aboutToRemoveObject(QObject *)));
58 QObject::connect(d->m_tabWidget, SIGNAL(currentChanged(int)), this, SLOT(currentTabChanged(int)));
61 ContextManager::~ContextManager()
63 delete d;
66 Core::IContext *ContextManager::currentContext() const
68 int currentIndex = d->m_tabWidget->currentIndex();
69 if (currentIndex < 0)
70 return 0;
71 return d->m_contexts.at(currentIndex);
74 Core::IContext *ContextManager::context(const QString &id) const
76 const int index = indexOf(id);
77 if (index >= 0)
78 return d->m_contexts.at(index);
79 return 0;
82 void ContextManager::registerUndoStack(QUndoStack *stack)
84 nlassert(stack);
85 d->m_mainWindow->undoGroup()->addStack(stack);
88 void ContextManager::unregisterUndoStack(QUndoStack *stack)
90 nlassert(stack);
91 d->m_mainWindow->undoGroup()->removeStack(stack);
94 void ContextManager::activateContext(const QString &id)
96 const int index = indexOf(id);
97 if (index >= 0)
98 d->m_tabWidget->setCurrentIndex(index);
101 void ContextManager::updateCurrentContext()
103 d->m_mainWindow->updateContext(currentContext());
106 void ContextManager::objectAdded(QObject *obj)
108 IContext *context = qobject_cast<IContext *>(obj);
109 if (context)
110 addContextObject(context);
113 void ContextManager::aboutToRemoveObject(QObject *obj)
115 IContext *context = qobject_cast<IContext *>(obj);
116 if (context)
117 removeContextObject(context);
120 void ContextManager::addContextObject(IContext *context)
122 d->m_contexts.push_back(context);
123 d->m_mainWindow->addContextObject(context);
125 QWidget *tabWidget = new QWidget(d->m_tabWidget);
126 d->m_tabWidget->addTab(tabWidget, context->icon(), context->trName());
127 QGridLayout *gridLayout = new QGridLayout(tabWidget);
128 gridLayout->setObjectName(QString::fromUtf8("gridLayout_") + context->id());
129 gridLayout->setContentsMargins(0, 0, 0, 0);
130 gridLayout->addWidget(context->widget(), 0, 0, 1, 1);
133 void ContextManager::removeContextObject(IContext *context)
135 d->m_mainWindow->removeContextObject(context);
137 const int index = indexOf(context->id());
138 QWidget *widget = d->m_tabWidget->widget(index);
139 d->m_contexts.remove(index);
140 d->m_tabWidget->removeTab(index);
141 delete widget;
144 void ContextManager::currentTabChanged(int index)
146 if (index >= 0)
148 IContext *context = d->m_contexts.at(index);
149 context->onActivated();
151 Q_EMIT currentContextChanged(context);
155 int ContextManager::indexOf(const QString &id) const
157 for (int i = 0; i < d->m_contexts.count(); ++i)
159 if (d->m_contexts.at(i)->id() == id)
160 return i;
162 nlwarning(QString("Warning, no such context: %1").arg(id).toUtf8().constData());
163 return -1;
166 } /* namespace Core */