FreeCAD
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#endif30
31#include "ToolBoxManager.h"32#include "Application.h"33#include "Command.h"34#include "ToolBarManager.h"35#include "ToolBox.h"36
37
38using namespace Gui;39using DockWnd::ToolBox;40
41ToolBoxManager* ToolBoxManager::_instance=nullptr;42
43ToolBoxManager* ToolBoxManager::getInstance()44{
45if ( !_instance )46_instance = new ToolBoxManager;47return _instance;48}
49
50void ToolBoxManager::destruct()51{
52delete _instance;53_instance = nullptr;54}
55
56ToolBoxManager::ToolBoxManager() = default;57
58ToolBoxManager::~ToolBoxManager() = default;59
60void ToolBoxManager::setToolBox( DockWnd::ToolBox* tb )61{
62_toolBox = tb;63}
64
65void ToolBoxManager::setup( ToolBarItem* toolBar ) const66{
67if ( !toolBar || !_toolBox )68return; // empty tool bar69
70int ct = _toolBox->count();71for ( int i=0; i<ct; i++ )72{73// get always the first item widget74QWidget* w = _toolBox->widget(0);75_toolBox->removeItem(0);76delete w;77}78
79CommandManager& mgr = Application::Instance->commandManager();80QList<ToolBarItem*> items = toolBar->getItems();81
82for ( auto item : items )83{84auto bar = new QToolBar();85bar->setOrientation(Qt::Vertical);86bar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);87std::string toolbarName = item->command();88bar->setObjectName(QString::fromLatin1(item->command().c_str()));89bar->setWindowTitle(QObject::tr(toolbarName.c_str())); // i18n90_toolBox->addItem( bar, bar->windowTitle() );91
92QList<ToolBarItem*> subitems = item->getItems();93for (auto subitem : subitems)94{95if ( subitem->command() == "Separator" ) {96//bar->addSeparator();97} else {98mgr.addTo(subitem->command().c_str(), bar);99}100}101
102// Now set the right size policy for each tool button103QList<QToolButton*> tool = bar->findChildren<QToolButton*>();104for (auto it : tool) {105it->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);106// When setting the horizontal size policy but no icon is set we use the following trick107// to make the button text left aligned.108QIcon icon = it->icon();109if (icon.isNull())110{111// Create an icon filled with the button color112int size = QApplication::style()->pixelMetric(QStyle::PM_SmallIconSize);113QPixmap p(size, size);114p.fill(Qt::transparent);115it->setIcon(p);116}117}118}119}
120
121void ToolBoxManager::retranslate() const122{
123int ct = _toolBox->count();124for (int i=0; i<ct; i++) {125// get always the first item widget126QWidget* w = _toolBox->widget(i);127QByteArray toolbarName = w->objectName().toUtf8();128w->setWindowTitle(QObject::tr(toolbarName.constData()));129_toolBox->setItemText(i, w->windowTitle());130}131}
132