Warning, file /office/calligra/braindump/src/LayoutFactoryRegistry.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002  *  Copyright (c) 2009 Cyrille Berger <cberger@cberger.net>
0003  *
0004  * This library is free software; you can redistribute it and/or
0005  * modify it under the terms of the GNU Lesser General Public
0006  * License as published by the Free Software Foundation;
0007  * either version 2, or (at your option) any later version of the License.
0008  *
0009  * This library is distributed in the hope that it will be useful,
0010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012  * Lesser General Public License for more details.
0013  *
0014  * You should have received a copy of the GNU Lesser General Public License
0015  * along with this library; see the file COPYING.  If not, write to
0016  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017  * Boston, MA 02110-1301, USA.
0018  */
0019 
0020 #include "LayoutFactoryRegistry.h"
0021 
0022 #include <QMap>
0023 
0024 #include "LayoutFactory.h"
0025 #include "Layout.h"
0026 #include "layouts/FreeLayout.h"
0027 #include "layouts/ColumnLayout.h"
0028 
0029 struct LayoutFactoryRegistry::Private {
0030     static LayoutFactoryRegistry* s_instance;
0031     QMap< QString, LayoutFactory* > factories;
0032 };
0033 
0034 LayoutFactoryRegistry* LayoutFactoryRegistry::Private::s_instance = 0;
0035 
0036 LayoutFactoryRegistry::LayoutFactoryRegistry() : d(new Private)
0037 {
0038     addFactory(new FreeLayoutFactory);
0039     addFactory(new ColumnLayoutFactory);
0040 }
0041 
0042 LayoutFactoryRegistry::~LayoutFactoryRegistry()
0043 {
0044     delete d;
0045 }
0046 
0047 LayoutFactoryRegistry* LayoutFactoryRegistry::instance()
0048 {
0049     if(!Private::s_instance) {
0050         Private::s_instance = new LayoutFactoryRegistry;
0051     }
0052     return Private::s_instance;
0053 }
0054 
0055 void LayoutFactoryRegistry::addFactory(LayoutFactory* _factory)
0056 {
0057     d->factories[_factory->id()] = _factory;
0058 }
0059 
0060 Layout* LayoutFactoryRegistry::createLayout(const QString& id) const
0061 {
0062     LayoutFactory* factory = d->factories.value(id);
0063     if(factory) {
0064         Layout* layout = factory->createLayout();
0065         Q_ASSERT(layout->id() == id);
0066         return layout;
0067     } else {
0068         return 0;
0069     }
0070 }
0071 
0072 QList< QPair<QString, QString> > LayoutFactoryRegistry::factories() const
0073 {
0074     QList< QPair<QString, QString> > lists;
0075     foreach(LayoutFactory * factory, d->factories) {
0076         lists.push_back(QPair<QString, QString>(factory->id(), factory->name()));
0077     }
0078     return lists;
0079 }