FreeCAD

Форк
0
/
ToolBoxManager.cpp 
131 строка · 4.7 Кб
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
#include "PreCompiled.h"
24
#ifndef _PreComp_
25
# include <QApplication>
26
# include <QStyle>
27
# include <QToolBar>
28
# include <QToolButton>
29
#endif
30

31
#include "ToolBoxManager.h"
32
#include "Application.h"
33
#include "Command.h"
34
#include "ToolBarManager.h"
35
#include "ToolBox.h"
36

37

38
using namespace Gui;
39
using DockWnd::ToolBox;
40

41
ToolBoxManager* ToolBoxManager::_instance=nullptr;
42

43
ToolBoxManager* ToolBoxManager::getInstance()
44
{
45
    if ( !_instance )
46
        _instance = new ToolBoxManager;
47
    return _instance;
48
}
49

50
void ToolBoxManager::destruct()
51
{
52
    delete _instance;
53
    _instance = nullptr;
54
}
55

56
ToolBoxManager::ToolBoxManager() = default;
57

58
ToolBoxManager::~ToolBoxManager() = default;
59

60
void ToolBoxManager::setToolBox( DockWnd::ToolBox* tb )
61
{
62
    _toolBox = tb;
63
}
64

65
void ToolBoxManager::setup( ToolBarItem* toolBar ) const
66
{
67
    if ( !toolBar || !_toolBox )
68
        return; // empty tool bar
69

70
    int ct = _toolBox->count();
71
    for ( int i=0; i<ct; i++ )
72
    {
73
        // get always the first item widget
74
        QWidget* w = _toolBox->widget(0);
75
        _toolBox->removeItem(0);
76
        delete w;
77
    }
78

79
    CommandManager& mgr = Application::Instance->commandManager();
80
    QList<ToolBarItem*> items = toolBar->getItems();
81

82
    for ( auto item : items )
83
    {
84
        auto bar = new QToolBar();
85
        bar->setOrientation(Qt::Vertical);
86
        bar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
87
        std::string toolbarName = item->command();
88
        bar->setObjectName(QString::fromLatin1(item->command().c_str()));
89
        bar->setWindowTitle(QObject::tr(toolbarName.c_str())); // i18n
90
        _toolBox->addItem( bar, bar->windowTitle() );
91

92
        QList<ToolBarItem*> subitems = item->getItems();
93
        for (auto subitem : subitems)
94
        {
95
            if ( subitem->command() == "Separator" ) {
96
                //bar->addSeparator();
97
            } else {
98
                mgr.addTo(subitem->command().c_str(), bar);
99
            }
100
        }
101

102
        // Now set the right size policy for each tool button
103
        QList<QToolButton*> tool = bar->findChildren<QToolButton*>();
104
        for (auto it : tool) {
105
            it->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
106
            // When setting the horizontal size policy but no icon is set we use the following trick
107
            // to make the button text left aligned.
108
            QIcon icon = it->icon();
109
            if (icon.isNull())
110
            {
111
                // Create an icon filled with the button color
112
                int size = QApplication::style()->pixelMetric(QStyle::PM_SmallIconSize);
113
                QPixmap p(size, size);
114
                p.fill(Qt::transparent);
115
                it->setIcon(p);
116
            }
117
        }
118
    }
119
}
120

121
void ToolBoxManager::retranslate() const
122
{
123
    int ct = _toolBox->count();
124
    for (int i=0; i<ct; i++) {
125
        // get always the first item widget
126
        QWidget* w = _toolBox->widget(i);
127
        QByteArray toolbarName = w->objectName().toUtf8();
128
        w->setWindowTitle(QObject::tr(toolbarName.constData()));
129
        _toolBox->setItemText(i, w->windowTitle());
130
    }
131
}
132

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

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

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

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