defuze.me  Client
modulefactory.cpp
00001 /**************************************************************************
00002 ** defuze.me Epitech Innovative Project
00003 **
00004 ** Copyright 2010-2011
00005 **   Athena Calmettes - Jocelyn De La Rosa - Francois Gaillard
00006 **   Adrien Jarthon - Alexandre Moore - Luc Peres - Arnaud Sellier
00007 **
00008 ** All rights reserved.
00009 **************************************************************************/
00010 
00011 #include "modulefactory.hpp"
00012 #include "exception.hpp"
00013 #include <QVariant>
00014 
00015 using namespace Gui;
00016 
00017 QHash<QString, Module*> ModuleFactory::modules = QHash<QString, Module*>();
00018 GuiCore* ModuleFactory::guiCore = 0;
00019 
00020 ModuleFactory::ModuleFactory()
00021 {
00022 }
00023 
00024 Module* ModuleFactory::create(const QString uniqName, QPoint position, ModuleWidget *widget, int tabIndex)
00025 {
00026     if (!guiCore)
00027         throw_exception(0x01, QObject::tr("Cannot create a new Module: Factory not initialized."));
00028     QString normalizedName = uniqName.toLower();
00029     if (normalizedName.isEmpty() || modules.contains(normalizedName))
00030         throw_exception(0x02, QObject::tr("Cannot create a new Module."));
00031     Module *module = new Module(guiCore);
00032     module->setWidget(widget);
00033     QString uniqId = normalizedName;
00034     int i = 0;
00035     while (modules.contains(uniqId))
00036     {
00037         ++i;
00038         uniqId = normalizedName + "_" + QVariant(i).toString();
00039     }
00040     module->setUniqId(uniqId);
00041     module->setPosition(position);
00042     module->setTabIndex(tabIndex);
00043     modules[uniqId] = module;
00044     return module;
00045 }
00046 
00047 Module* ModuleFactory::getModule(const QString &uniqId)
00048 {
00049     return modules[uniqId];
00050 }
00051 
00052 void ModuleFactory::initModuleFactory(GuiCore *_guiCore)
00053 {
00054     guiCore = _guiCore;
00055 }