1 // Object Viewer Qt - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
2 // Copyright (C) 2011 Dzmitry KAMIAHIN (dnk-88) <dnk-88@tut.by>
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>
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/>.
22 #include "context_manager.h"
24 #include "main_window.h"
27 #include <nel/misc/debug.h>
30 #include <QtGui/QTabWidget>
31 #include <QtGui/QGridLayout>
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()
66 Core::IContext
*ContextManager::currentContext() const
68 int currentIndex
= d
->m_tabWidget
->currentIndex();
71 return d
->m_contexts
.at(currentIndex
);
74 Core::IContext
*ContextManager::context(const QString
&id
) const
76 const int index
= indexOf(id
);
78 return d
->m_contexts
.at(index
);
82 void ContextManager::registerUndoStack(QUndoStack
*stack
)
85 d
->m_mainWindow
->undoGroup()->addStack(stack
);
88 void ContextManager::unregisterUndoStack(QUndoStack
*stack
)
91 d
->m_mainWindow
->undoGroup()->removeStack(stack
);
94 void ContextManager::activateContext(const QString
&id
)
96 const int index
= indexOf(id
);
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
);
110 addContextObject(context
);
113 void ContextManager::aboutToRemoveObject(QObject
*obj
)
115 IContext
*context
= qobject_cast
<IContext
*>(obj
);
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
);
144 void ContextManager::currentTabChanged(int index
)
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
)
162 nlwarning(QString("Warning, no such context: %1").arg(id
).toUtf8().constData());
166 } /* namespace Core */