FreeCAD

Форк
0
/
WorkbenchManager.cpp 
136 строк · 4.4 Кб
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 "PreCompiled.h"
25

26
#include <Base/Console.h>
27

28
#include "WorkbenchManager.h"
29
#include "Workbench.h"
30
#include "DockWindowManager.h"
31
#include "MenuManager.h"
32
#include "ToolBarManager.h"
33

34
using namespace Gui;
35

36
WorkbenchManager* WorkbenchManager::_instance = nullptr;
37

38
WorkbenchManager* WorkbenchManager::instance()
39
{
40
    if (!_instance)
41
        _instance = new WorkbenchManager;
42
    return _instance;
43
}
44

45
void WorkbenchManager::destruct()
46
{
47
    delete _instance;
48
    _instance = nullptr;
49
}
50

51
WorkbenchManager::WorkbenchManager() = default;
52

53
WorkbenchManager::~WorkbenchManager()
54
{
55
    for (auto & it : _workbenches) {
56
        Workbench* wb = it.second;
57
        delete wb;
58
    }
59

60
    MenuManager::destruct();
61
    ToolBarManager::destruct();
62
    //ToolBoxManager::destruct();
63
    DockWindowManager::destruct();
64
}
65

66
Workbench* WorkbenchManager::createWorkbench (const std::string& name, const std::string& className)
67
{
68
    Workbench* wb = getWorkbench(name);
69

70
    if (!wb) {
71
        // try to create an instance now
72
        Base::Type type = 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
        delete wb;
97
    }
98
}
99

100
Workbench* WorkbenchManager::getWorkbench (const std::string& name) const
101
{
102
    Workbench* wb=nullptr;
103

104
    std::map<std::string, Workbench*>::const_iterator it = _workbenches.find(name);
105
    if (it != _workbenches.end()) {
106
        // returns the already created object
107
        wb = it->second;
108
    }
109

110
    return wb;
111
}
112

113
bool WorkbenchManager::activate(const std::string& name, const std::string& className)
114
{
115
    Workbench* wb = createWorkbench(name, className);
116
    if (wb) {
117
        _activeWorkbench = wb;
118
        wb->activate();
119
        return true;
120
    }
121

122
    return false;
123
}
124

125
Workbench* WorkbenchManager::active() const
126
{
127
    return _activeWorkbench;
128
}
129

130
std::list<std::string> WorkbenchManager::workbenches() const
131
{
132
    std::list<std::string> wb;
133
    for (const auto & it : _workbenches)
134
        wb.push_back(it.first);
135
    return wb;
136
}
137

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.