ToolBoxManager.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) 2005 Werner Mayer <wmayer[at]users.sourceforge.net>     *
00003  *                                                                         *
00004  *   This file is part of the FreeCAD CAx development system.              *
00005  *                                                                         *
00006  *   This library is free software; you can redistribute it and/or         *
00007  *   modify it under the terms of the GNU Library General Public           *
00008  *   License as published by the Free Software Foundation; either          *
00009  *   version 2 of the License, or (at your option) any later version.      *
00010  *                                                                         *
00011  *   This library  is distributed in the hope that it will be useful,      *
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00014  *   GNU Library General Public License for more details.                  *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU Library General Public     *
00017  *   License along with this library; see the file COPYING.LIB. If not,    *
00018  *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
00019  *   Suite 330, Boston, MA  02111-1307, USA                                *
00020  *                                                                         *
00021  ***************************************************************************/
00022 
00023 
00024 #include "PreCompiled.h"
00025 #ifndef _PreComp_
00026 # include <QApplication>
00027 # include <QStyle>
00028 # include <QToolBar>
00029 # include <QToolButton>
00030 #endif
00031 
00032 #include "ToolBoxManager.h"
00033 #include "ToolBarManager.h"
00034 #include "Application.h"
00035 #include "Command.h"
00036 #include "ToolBox.h"
00037 
00038 using namespace Gui;
00039 using DockWnd::ToolBox;
00040 
00041 ToolBoxManager* ToolBoxManager::_instance=0;
00042 
00043 ToolBoxManager* ToolBoxManager::getInstance()
00044 {
00045     if ( !_instance )
00046         _instance = new ToolBoxManager;
00047     return _instance;
00048 }
00049 
00050 void ToolBoxManager::destruct()
00051 {
00052     delete _instance;
00053     _instance = 0;
00054 }
00055 
00056 ToolBoxManager::ToolBoxManager() : _toolBox(0L)
00057 {
00058 }
00059 
00060 ToolBoxManager::~ToolBoxManager()
00061 {
00062 }
00063 
00064 void ToolBoxManager::setToolBox( DockWnd::ToolBox* tb )
00065 {
00066     _toolBox = tb;
00067 }
00068 
00069 void ToolBoxManager::setup( ToolBarItem* toolBar ) const
00070 {
00071     if ( !toolBar || !_toolBox )
00072         return; // empty tool bar
00073 
00074     int ct = _toolBox->count();
00075     for ( int i=0; i<ct; i++ )
00076     {
00077         // get always the first item widget
00078         QWidget* w = _toolBox->widget(0);
00079         _toolBox->removeItem(0);
00080         delete w;
00081     }
00082 
00083     CommandManager& mgr = Application::Instance->commandManager();
00084     QList<ToolBarItem*> items = toolBar->getItems();
00085 
00086     for ( QList<ToolBarItem*>::ConstIterator item = items.begin(); item != items.end(); ++item )
00087     {
00088         QToolBar* bar = new QToolBar();
00089         bar->setOrientation(Qt::Vertical);
00090         bar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
00091         std::string toolbarName = (*item)->command();
00092         bar->setObjectName(QString::fromAscii((*item)->command().c_str()));
00093         bar->setWindowTitle(QObject::trUtf8(toolbarName.c_str())); // i18n
00094         _toolBox->addItem( bar, bar->windowTitle() );
00095 
00096         QList<ToolBarItem*> subitems = (*item)->getItems();
00097         for ( QList<ToolBarItem*>::ConstIterator subitem = subitems.begin(); subitem != subitems.end(); ++subitem )
00098         {
00099             if ( (*subitem)->command() == "Separator" ) {
00100                 //bar->addSeparator();
00101             } else {
00102                 mgr.addTo((*subitem)->command().c_str(), bar);
00103             }
00104         }
00105 
00106         // Now set the right size policy for each tool button
00107         QList<QToolButton*> tool = bar->findChildren<QToolButton*>();
00108         for (QList<QToolButton*>::Iterator it = tool.begin(); it != tool.end(); ++it) {
00109             (*it)->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
00110             // When setting the horizontal size policy but no icon is set we use the following trick
00111             // to make the button text left aligned.
00112             QIcon icon = (*it)->icon();
00113             if (icon.isNull())
00114             {
00115                 // Create an icon filled with the button color
00116                 int size = QApplication::style()->pixelMetric(QStyle::PM_SmallIconSize);
00117                 QPixmap p(size, size);
00118                 p.fill(Qt::transparent);
00119                 (*it)->setIcon(p);
00120             }
00121         }
00122     }
00123 }
00124 
00125 void ToolBoxManager::retranslate() const
00126 {
00127     int ct = _toolBox->count();
00128     for (int i=0; i<ct; i++) {
00129         // get always the first item widget
00130         QWidget* w = _toolBox->widget(i);
00131         QByteArray toolbarName = w->objectName().toUtf8();
00132         w->setWindowTitle(QObject::trUtf8(toolbarName.constData()));
00133         _toolBox->setItemText(i, w->windowTitle());
00134     }
00135 }

Generated on Wed Nov 23 19:00:45 2011 for FreeCAD by  doxygen 1.6.1