Debian Codegraph @

src/Gui/ToolBoxManager.cpp

raw
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 <QApplication>
25 #include <QStyle>
26 #include <QToolBar>
27 #include <QToolButton>
28
29
30 #include "ToolBoxManager.h"
31 #include "Application.h"
32 #include "Command.h"
33 #include "ToolBarManager.h"
34 #include "ToolBox.h"
35
36
37 using namespace Gui;
38 using DockWnd::ToolBox;
39
40 ToolBoxManager* ToolBoxManager::_instance = nullptr;
41
42 ToolBoxManager* ToolBoxManager::getInstance()
43 {
44 if (!_instance) {
45 _instance = new ToolBoxManager;
46 }
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
71 int ct = _toolBox->count();
72 for (int i = 0; i < ct; i++) {
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 auto bar = new QToolBar();
84 bar->setOrientation(Qt::Vertical);
85 bar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
86 std::string toolbarName = item->command();
87 bar->setObjectName(QString::fromLatin1(item->command().c_str()));
88 bar->setWindowTitle(QObject::tr(toolbarName.c_str())); // i18n
89 _toolBox->addItem(bar, bar->windowTitle());
90
91 QList<ToolBarItem*> subitems = item->getItems();
92 for (auto subitem : subitems) {
93 if (subitem->command() == "Separator") {
94 // bar->addSeparator();
95 }
96 else {
97 mgr.addTo(subitem->command().c_str(), bar);
98 }
99 }
100
101 // Now set the right size policy for each tool button
102 QList<QToolButton*> tool = bar->findChildren<QToolButton*>();
103 for (auto it : tool) {
104 it->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
105 // When setting the horizontal size policy but no icon is set we use the following trick
106 // to make the button text left aligned.
107 QIcon icon = it->icon();
108 if (icon.isNull()) {
109 // Create an icon filled with the button color
110 int size = QApplication::style()->pixelMetric(QStyle::PM_SmallIconSize);
111 QPixmap p(size, size);
112 p.fill(Qt::transparent);
113 it->setIcon(p);
114 }
115 }
116 }
117 }
118
119 void ToolBoxManager::retranslate() const
120 {
121 int ct = _toolBox->count();
122 for (int i = 0; i < ct; i++) {
123 // get always the first item widget
124 QWidget* w = _toolBox->widget(i);
125 QByteArray toolbarName = w->objectName().toUtf8();
126 w->setWindowTitle(QObject::tr(toolbarName.constData()));
127 _toolBox->setItemText(i, w->windowTitle());
128 }
129 }