| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2005 Werner Mayer <wmayer[at]users.sourceforge.net> * |
| 3 |
* * |
| 4 |
* This file is part of the FreeCAD CAx development system. * |
| 5 |
* * |
| 6 |
* This library is free software; you can redistribute it and/or * |
| 7 |
* modify it under the terms of the GNU Library General Public * |
| 8 |
* License as published by the Free Software Foundation; either * |
| 9 |
* version 2 of the License, or (at your option) any later version. * |
| 10 |
* * |
| 11 |
* This library is distributed in the hope that it will be useful, * |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 |
* GNU Library General Public License for more details. * |
| 15 |
* * |
| 16 |
* You should have received a copy of the GNU Library General Public * |
| 17 |
* License along with this library; see the file COPYING.LIB. If not, * |
| 18 |
* write to the Free Software Foundation, Inc., 59 Temple Place, * |
| 19 |
* Suite 330, Boston, MA 02111-1307, USA * |
| 20 |
* * |
| 21 |
***************************************************************************/ |
| 22 |
|
| 23 |
|
| 24 |
#include <Base/Console.h> |
| 25 |
|
| 26 |
#include "WorkbenchManager.h" |
| 27 |
#include "Workbench.h" |
| 28 |
#include "DockWindowManager.h" |
| 29 |
#include "MenuManager.h" |
| 30 |
#include "ToolBarManager.h" |
| 31 |
|
| 32 |
using namespace Gui; |
| 33 |
|
| 34 |
WorkbenchManager* WorkbenchManager::_instance = nullptr; |
| 35 |
|
| 36 |
WorkbenchManager* WorkbenchManager::instance() |
| 37 |
{ |
| 38 |
if (!_instance) { |
| 39 |
_instance = new WorkbenchManager; |
| 40 |
} |
| 41 |
return _instance; |
| 42 |
} |
| 43 |
|
| 44 |
void WorkbenchManager::destruct() |
| 45 |
{ |
| 46 |
delete _instance; |
| 47 |
_instance = nullptr; |
| 48 |
} |
| 49 |
|
| 50 |
WorkbenchManager::WorkbenchManager() = default; |
| 51 |
|
| 52 |
WorkbenchManager::~WorkbenchManager() |
| 53 |
{ |
| 54 |
for (auto& it : _workbenches) { |
| 55 |
Workbench* wb = it.second; |
| 56 |
delete wb; |
| 57 |
} |
| 58 |
|
| 59 |
MenuManager::destruct(); |
| 60 |
ToolBarManager::destruct(); |
| 61 |
// ToolBoxManager::destruct(); |
| 62 |
DockWindowManager::destruct(); |
| 63 |
} |
| 64 |
|
| 65 |
Workbench* WorkbenchManager::createWorkbench(const std::string& name, const std::string& className) |
| 66 |
{ |
| 67 |
Workbench* wb = getWorkbench(name); |
| 68 |
|
| 69 |
if (!wb) { |
| 70 |
// try to create an instance now |
| 71 |
Base::Type type |
| 72 |
= Base::Type::getTypeIfDerivedFrom(className.c_str(), Workbench::getClassTypeId(), false); |
| 73 |
wb = static_cast<Workbench*>(type.createInstance()); |
| 74 |
// createInstance could return a null pointer |
| 75 |
if (!wb) { |
| 76 |
std::stringstream str; |
| 77 |
str << "'" << className << "' not a workbench type" << std::ends; |
| 78 |
throw Base::TypeError(str.str()); |
| 79 |
} |
| 80 |
|
| 81 |
wb->setName(name); |
| 82 |
_workbenches[name] = wb; |
| 83 |
} |
| 84 |
|
| 85 |
return wb; |
| 86 |
} |
| 87 |
|
| 88 |
void WorkbenchManager::removeWorkbench(const std::string& name) |
| 89 |
{ |
| 90 |
std::map<std::string, Workbench*>::iterator it = _workbenches.find(name); |
| 91 |
if (it != _workbenches.end()) { |
| 92 |
Workbench* wb = it->second; |
| 93 |
_workbenches.erase(it); |
| 94 |
if (_activeWorkbench == wb) { |
| 95 |
_activeWorkbench = nullptr; |
| 96 |
} |
| 97 |
delete wb; |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
Workbench* WorkbenchManager::getWorkbench(const std::string& name) const |
| 102 |
{ |
| 103 |
Workbench* wb = nullptr; |
| 104 |
|
| 105 |
std::map<std::string, Workbench*>::const_iterator it = _workbenches.find(name); |
| 106 |
if (it != _workbenches.end()) { |
| 107 |
// returns the already created object |
| 108 |
wb = it->second; |
| 109 |
} |
| 110 |
|
| 111 |
return wb; |
| 112 |
} |
| 113 |
|
| 114 |
bool WorkbenchManager::activate(const std::string& name, const std::string& className) |
| 115 |
{ |
| 116 |
Workbench* wb = createWorkbench(name, className); |
| 117 |
if (wb) { |
| 118 |
_activeWorkbench = wb; |
| 119 |
wb->activate(); |
| 120 |
return true; |
| 121 |
} |
| 122 |
|
| 123 |
return false; |
| 124 |
} |
| 125 |
|
| 126 |
Workbench* WorkbenchManager::active() const |
| 127 |
{ |
| 128 |
return _activeWorkbench; |
| 129 |
} |
| 130 |
|
| 131 |
std::string WorkbenchManager::activeName() const |
| 132 |
{ |
| 133 |
std::string activeWbName = ""; |
| 134 |
if (_activeWorkbench) { |
| 135 |
activeWbName = _activeWorkbench->name(); |
| 136 |
} |
| 137 |
return activeWbName; |
| 138 |
} |
| 139 |
|
| 140 |
std::list<std::string> WorkbenchManager::workbenches() const |
| 141 |
{ |
| 142 |
std::list<std::string> wb; |
| 143 |
for (const auto& it : _workbenches) { |
| 144 |
wb.push_back(it.first); |
| 145 |
} |
| 146 |
return wb; |
| 147 |
} |