WorkbenchManager.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) 2005 Werner Mayer <wmayer[at]users.sourceforge.net>     *
00003  *                                                                         *
00004  *   This file is part of the FreeCAD CAx development system.              *
00005  *                                                                         *
00006  *   This library is free software; you can redistribute it and/or         *
00007  *   modify it under the terms of the GNU Library General Public           *
00008  *   License as published by the Free Software Foundation; either          *
00009  *   version 2 of the License, or (at your option) any later version.      *
00010  *                                                                         *
00011  *   This library  is distributed in the hope that it will be useful,      *
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00014  *   GNU Library General Public License for more details.                  *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU Library General Public     *
00017  *   License along with this library; see the file COPYING.LIB. If not,    *
00018  *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
00019  *   Suite 330, Boston, MA  02111-1307, USA                                *
00020  *                                                                         *
00021  ***************************************************************************/
00022 
00023 
00024 #include "PreCompiled.h"
00025 #ifndef _PreComp_
00026 # include <sstream>
00027 #endif
00028 
00029 #include <Base/Console.h>
00030 #include <Base/Exception.h>
00031 #include "WorkbenchManager.h"
00032 #include "Workbench.h"
00033 #include "MenuManager.h"
00034 #include "ToolBarManager.h"
00035 #include "ToolBoxManager.h"
00036 #include "DockWindowManager.h"
00037 #include "MainWindow.h"
00038 
00039 using namespace Gui;
00040 
00041 WorkbenchManager* WorkbenchManager::_instance = 0;
00042 
00043 WorkbenchManager* WorkbenchManager::instance()
00044 {
00045     if (_instance == 0)
00046         _instance = new WorkbenchManager;
00047     return _instance;
00048 }
00049 
00050 void WorkbenchManager::destruct()
00051 {
00052     delete _instance;
00053     _instance = 0;
00054 }
00055 
00056 WorkbenchManager::WorkbenchManager() : _activeWorkbench(0)
00057 {
00058 }
00059 
00060 WorkbenchManager::~WorkbenchManager()
00061 {
00062     for (std::map<std::string, Workbench*>::iterator it = _workbenches.begin(); it != _workbenches.end(); ++it) {
00063         Workbench* wb = it->second;
00064         delete wb;
00065     }
00066 
00067     MenuManager::destruct();
00068     ToolBarManager::destruct();
00069     //ToolBoxManager::destruct();
00070     DockWindowManager::destruct();
00071 }
00072 
00073 Workbench* WorkbenchManager::createWorkbench (const std::string& name, const std::string& className)
00074 {
00075     Workbench* wb = getWorkbench(name);
00076 
00077     if (!wb) {
00078         // try to create an instance now
00079         Base::BaseClass* base = static_cast<Base::BaseClass*>
00080             (Base::Type::createInstanceByName(className.c_str(),false));
00081         if (base) {
00082             if (!base->getTypeId().isDerivedFrom(Gui::Workbench::getClassTypeId())) {
00083                 delete base;
00084                 std::stringstream str;
00085                 str << "'" << className << "' not a workbench type" << std::ends;
00086                 throw Base::Exception(str.str());
00087             }
00088 
00089             wb = static_cast<Workbench*>(base);
00090             wb->setName(name);
00091             _workbenches[name] = wb;
00092         }
00093         else
00094             Base::Console().Log("WorkbenchManager::createWorkbench(): Can not create "
00095                 "Workbench instance with type: %s\n",className.c_str());
00096     }
00097 
00098     return wb;
00099 }
00100 
00101 void WorkbenchManager::removeWorkbench(const std::string& name)
00102 {
00103     std::map<std::string, Workbench*>::iterator it = _workbenches.find(name);
00104     if (it != _workbenches.end()) {
00105         Workbench* wb = it->second;
00106         _workbenches.erase(it);
00107         if (_activeWorkbench == wb)
00108             _activeWorkbench = 0;
00109         delete wb;
00110     }
00111 }
00112 
00113 Workbench* WorkbenchManager::getWorkbench (const std::string& name) const
00114 {
00115     Workbench* wb=0;
00116 
00117     std::map<std::string, Workbench*>::const_iterator it = _workbenches.find(name);
00118     if (it != _workbenches.end()) {
00119         // returns the already created object
00120         wb = it->second;
00121     }
00122 
00123     return wb;
00124 }
00125 
00126 bool WorkbenchManager::activate(const std::string& name, const std::string& className)
00127 {
00128     Workbench* wb = createWorkbench(name, className);
00129     if (wb) {
00130         _activeWorkbench = wb;
00131         wb->activate();
00132         return true;
00133     }
00134   
00135     return false;
00136 }
00137 
00138 Workbench* WorkbenchManager::active() const
00139 {
00140     return _activeWorkbench;
00141 }
00142 
00143 std::list<std::string> WorkbenchManager::workbenches() const
00144 {
00145     std::list<std::string> wb;
00146     for (std::map<std::string, Workbench*>::const_iterator it = _workbenches.begin(); it != _workbenches.end(); ++it)
00147         wb.push_back(it->first);
00148     return wb;
00149 }

Generated on Wed Nov 23 19:01:12 2011 for FreeCAD by  doxygen 1.6.1