1
/***************************************************************************
2
* Copyright (c) 2005 Werner Mayer <werner.wm.mayer@gmx.de> *
4
* This file is part of the FreeCAD CAx development system. *
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. *
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. *
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 *
21
***************************************************************************/
24
#include "PreCompiled.h"
26
#include "ToolBarManager.h"
27
#include "MainWindow.h"
28
#include "Application.h"
30
#include "BitmapFactory.h"
34
ToolBarItem::ToolBarItem()
38
ToolBarItem::ToolBarItem(ToolBarItem* item)
41
item->appendItem(this);
44
ToolBarItem::~ToolBarItem()
49
void ToolBarItem::setCommand(const std::string& name)
54
std::string ToolBarItem::command() const
59
bool ToolBarItem::hasItems() const
61
return _items.count() > 0;
64
ToolBarItem* ToolBarItem::findItem(const std::string& name)
66
if ( _name == name ) {
69
for ( QList<ToolBarItem*>::ConstIterator it = _items.begin(); it != _items.end(); ++it ) {
70
if ( (*it)->_name == name ) {
79
ToolBarItem* ToolBarItem::copy() const
81
ToolBarItem* root = new ToolBarItem;
82
root->setCommand( command() );
84
QList<ToolBarItem*> items = getItems();
85
for ( QList<ToolBarItem*>::ConstIterator it = items.begin(); it != items.end(); ++it ) {
86
root->appendItem( (*it)->copy() );
92
uint ToolBarItem::count() const
94
return _items.count();
97
void ToolBarItem::appendItem(ToolBarItem* item)
99
_items.push_back( item );
102
bool ToolBarItem::insertItem( ToolBarItem* before, ToolBarItem* item)
104
int pos = _items.indexOf(before);
106
_items.insert(pos, item);
112
void ToolBarItem::removeItem(ToolBarItem* item)
114
int pos = _items.indexOf(item);
116
_items.removeAt(pos);
119
void ToolBarItem::clear()
121
for ( QList<ToolBarItem*>::Iterator it = _items.begin(); it != _items.end(); ++it ) {
128
ToolBarItem& ToolBarItem::operator << (ToolBarItem* item)
134
ToolBarItem& ToolBarItem::operator << (const std::string& command)
136
ToolBarItem* item = new ToolBarItem(this);
137
item->setCommand(command);
141
QList<ToolBarItem*> ToolBarItem::getItems() const
146
// -----------------------------------------------------------
148
ToolBarManager* ToolBarManager::_instance=0;
150
ToolBarManager* ToolBarManager::getInstance()
153
_instance = new ToolBarManager;
157
void ToolBarManager::destruct()
163
ToolBarManager::ToolBarManager()
167
ToolBarManager::~ToolBarManager()
171
void ToolBarManager::setup(ToolBarItem* toolBarItems)
174
return; // empty menu bar
177
this->toolbarNames.clear();
179
ParameterGrp::handle hPref = App::GetApplication().GetUserParameter().GetGroup("BaseApp")
180
->GetGroup("MainWindow")->GetGroup("Toolbars");
181
QList<ToolBarItem*> items = toolBarItems->getItems();
182
QList<QToolBar*> toolbars = toolBars();
183
for (QList<ToolBarItem*>::ConstIterator it = items.begin(); it != items.end(); ++it) {
184
// search for the toolbar
185
this->toolbarNames << QString::fromUtf8((*it)->command().c_str());
186
QToolBar* toolbar = findToolBar(toolbars, QString::fromAscii((*it)->command().c_str()));
187
std::string toolbarName = (*it)->command();
188
bool visible = hPref->GetBool(toolbarName.c_str(), true);
191
toolbar = getMainWindow()->addToolBar(QObject::trUtf8(toolbarName.c_str())); // i18n
192
toolbar->setObjectName(QString::fromAscii((*it)->command().c_str()));
193
toolbar->setVisible(visible);
195
toolbar->setVisible(visible);
196
toolbar->toggleViewAction()->setVisible(true);
197
int index = toolbars.indexOf(toolbar);
198
toolbars.removeAt(index);
205
// hide all unneeded toolbars
206
for (QList<QToolBar*>::Iterator it = toolbars.begin(); it != toolbars.end(); ++it) {
207
// ignore toolbars which do not belong to the previously active workbench
208
QByteArray toolbarName = (*it)->objectName().toUtf8();
209
if (!(*it)->toggleViewAction()->isVisible())
211
hPref->SetBool(toolbarName.constData(), (*it)->isVisible());
213
(*it)->toggleViewAction()->setVisible(false);
217
void ToolBarManager::setup(ToolBarItem* item, QToolBar* toolbar) const
219
CommandManager& mgr = Application::Instance->commandManager();
220
QList<ToolBarItem*> items = item->getItems();
221
QList<QAction*> actions = toolbar->actions();
222
for (QList<ToolBarItem*>::ConstIterator it = items.begin(); it != items.end(); ++it) {
223
// search for the action item
224
QAction* action = findAction(actions, QString::fromAscii((*it)->command().c_str()));
226
if ((*it)->command() == "Separator") {
227
action = toolbar->addSeparator();
229
// Check if action was added successfully
230
if (mgr.addTo((*it)->command().c_str(), toolbar))
231
action = toolbar->actions().last();
234
// set the tool button user data
235
if (action) action->setData(QString::fromAscii((*it)->command().c_str()));
237
// Note: For toolbars we do not remove and readd the actions
238
// because this causes flicker effects. So, it could happen that the order of
239
// buttons doesn't match with the order of commands in the workbench.
240
int index = actions.indexOf(action);
241
actions.removeAt(index);
245
// remove all tool buttons which we don't need for the moment
246
for (QList<QAction*>::Iterator it = actions.begin(); it != actions.end(); ++it) {
247
toolbar->removeAction(*it);
251
void ToolBarManager::saveState() const
253
ParameterGrp::handle hPref = App::GetApplication().GetUserParameter().GetGroup("BaseApp")
254
->GetGroup("MainWindow")->GetGroup("Toolbars");
256
QList<QToolBar*> toolbars = toolBars();
257
for (QStringList::ConstIterator it = this->toolbarNames.begin(); it != this->toolbarNames.end(); ++it) {
258
QToolBar* toolbar = findToolBar(toolbars, *it);
260
QByteArray toolbarName = toolbar->objectName().toUtf8();
261
hPref->SetBool(toolbarName.constData(), toolbar->isVisible());
266
void ToolBarManager::restoreState() const
268
ParameterGrp::handle hPref = App::GetApplication().GetUserParameter().GetGroup("BaseApp")
269
->GetGroup("MainWindow")->GetGroup("Toolbars");
271
QList<QToolBar*> toolbars = toolBars();
272
for (QStringList::ConstIterator it = this->toolbarNames.begin(); it != this->toolbarNames.end(); ++it) {
273
QToolBar* toolbar = findToolBar(toolbars, *it);
275
QByteArray toolbarName = toolbar->objectName().toUtf8();
276
toolbar->setVisible(hPref->GetBool(toolbarName.constData(), toolbar->isVisible()));
281
void ToolBarManager::retranslate() const
283
QList<QToolBar*> toolbars = toolBars();
284
for (QList<QToolBar*>::Iterator it = toolbars.begin(); it != toolbars.end(); ++it) {
285
QByteArray toolbarName = (*it)->objectName().toUtf8();
286
(*it)->setWindowTitle(QObject::trUtf8(toolbarName.constData()));
290
QToolBar* ToolBarManager::findToolBar(const QList<QToolBar*>& toolbars, const QString& item) const
292
for (QList<QToolBar*>::ConstIterator it = toolbars.begin(); it != toolbars.end(); ++it) {
293
if ((*it)->objectName() == item)
297
return 0; // no item with the user data found
300
QAction* ToolBarManager::findAction(const QList<QAction*>& acts, const QString& item) const
302
for (QList<QAction*>::ConstIterator it = acts.begin(); it != acts.end(); ++it) {
303
if ((*it)->data().toString() == item)
307
return 0; // no item with the user data found
310
QList<QToolBar*> ToolBarManager::toolBars() const
312
QWidget* mw = getMainWindow();
314
QList<QToolBar*> bars = getMainWindow()->findChildren<QToolBar*>();
315
for (QList<QToolBar*>::ConstIterator it = bars.begin(); it != bars.end(); ++it) {
316
if ((*it)->parentWidget() == mw)