~ubuntu-branches/ubuntu/maverick/freecad/maverick

« back to all changes in this revision

Viewing changes to src/Gui/ToolBarManager.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Teemu Ikonen
  • Date: 2009-07-16 18:37:41 UTC
  • Revision ID: james.westby@ubuntu.com-20090716183741-oww9kcxqrk991i1n
Tags: upstream-0.8.2237
ImportĀ upstreamĀ versionĀ 0.8.2237

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *   Copyright (c) 2005 Werner Mayer <werner.wm.mayer@gmx.de>              *
 
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 "PreCompiled.h"
 
25
 
 
26
#include "ToolBarManager.h"
 
27
#include "MainWindow.h"
 
28
#include "Application.h"
 
29
#include "Command.h"
 
30
#include "BitmapFactory.h"
 
31
 
 
32
using namespace Gui;
 
33
 
 
34
ToolBarItem::ToolBarItem()
 
35
{
 
36
}
 
37
 
 
38
ToolBarItem::ToolBarItem(ToolBarItem* item)
 
39
{
 
40
    if ( item )
 
41
        item->appendItem(this);
 
42
}
 
43
 
 
44
ToolBarItem::~ToolBarItem()
 
45
{
 
46
    clear();
 
47
}
 
48
 
 
49
void ToolBarItem::setCommand(const std::string& name)
 
50
{
 
51
    _name = name;
 
52
}
 
53
 
 
54
std::string ToolBarItem::command() const
 
55
{
 
56
    return _name;
 
57
}
 
58
 
 
59
bool ToolBarItem::hasItems() const
 
60
{
 
61
    return _items.count() > 0;
 
62
}
 
63
 
 
64
ToolBarItem* ToolBarItem::findItem(const std::string& name)
 
65
{
 
66
    if ( _name == name ) {
 
67
        return this;
 
68
    } else {
 
69
        for ( QList<ToolBarItem*>::ConstIterator it = _items.begin(); it != _items.end(); ++it ) {
 
70
            if ( (*it)->_name == name ) {
 
71
                return *it;
 
72
            }
 
73
        }
 
74
    }
 
75
 
 
76
    return 0;
 
77
}
 
78
 
 
79
ToolBarItem* ToolBarItem::copy() const
 
80
{
 
81
    ToolBarItem* root = new ToolBarItem;
 
82
    root->setCommand( command() );
 
83
 
 
84
    QList<ToolBarItem*> items = getItems();
 
85
    for ( QList<ToolBarItem*>::ConstIterator it = items.begin(); it != items.end(); ++it ) {
 
86
        root->appendItem( (*it)->copy() );
 
87
    }
 
88
 
 
89
    return root;
 
90
}
 
91
 
 
92
uint ToolBarItem::count() const
 
93
{
 
94
    return _items.count();
 
95
}
 
96
 
 
97
void ToolBarItem::appendItem(ToolBarItem* item)
 
98
{
 
99
    _items.push_back( item );
 
100
}
 
101
 
 
102
bool ToolBarItem::insertItem( ToolBarItem* before, ToolBarItem* item)
 
103
{
 
104
    int pos = _items.indexOf(before);
 
105
    if (pos != -1) {
 
106
        _items.insert(pos, item);
 
107
        return true;
 
108
    } else
 
109
        return false;
 
110
}
 
111
 
 
112
void ToolBarItem::removeItem(ToolBarItem* item)
 
113
{
 
114
    int pos = _items.indexOf(item);
 
115
    if (pos != -1)
 
116
        _items.removeAt(pos);
 
117
}
 
118
 
 
119
void ToolBarItem::clear()
 
120
{
 
121
    for ( QList<ToolBarItem*>::Iterator it = _items.begin(); it != _items.end(); ++it ) {
 
122
        delete *it;
 
123
    }
 
124
 
 
125
    _items.clear();
 
126
}
 
127
 
 
128
ToolBarItem& ToolBarItem::operator << (ToolBarItem* item)
 
129
{
 
130
    appendItem(item);
 
131
    return *this;
 
132
}
 
133
 
 
134
ToolBarItem& ToolBarItem::operator << (const std::string& command)
 
135
{
 
136
    ToolBarItem* item = new ToolBarItem(this);
 
137
    item->setCommand(command);
 
138
    return *this;
 
139
}
 
140
 
 
141
QList<ToolBarItem*> ToolBarItem::getItems() const
 
142
{
 
143
    return _items;
 
144
}
 
145
 
 
146
// -----------------------------------------------------------
 
147
 
 
148
ToolBarManager* ToolBarManager::_instance=0;
 
149
 
 
150
ToolBarManager* ToolBarManager::getInstance()
 
151
{
 
152
    if ( !_instance )
 
153
        _instance = new ToolBarManager;
 
154
    return _instance;
 
155
}
 
156
 
 
157
void ToolBarManager::destruct()
 
158
{
 
159
    delete _instance;
 
160
    _instance = 0;
 
161
}
 
162
 
 
163
ToolBarManager::ToolBarManager()
 
164
{
 
165
}
 
166
 
 
167
ToolBarManager::~ToolBarManager()
 
168
{
 
169
}
 
170
 
 
171
void ToolBarManager::setup(ToolBarItem* toolBarItems)
 
172
{
 
173
    if (!toolBarItems)
 
174
        return; // empty menu bar
 
175
 
 
176
    saveState();
 
177
    this->toolbarNames.clear();
 
178
    
 
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);
 
189
 
 
190
        if (!toolbar) {
 
191
            toolbar = getMainWindow()->addToolBar(QObject::trUtf8(toolbarName.c_str())); // i18n
 
192
            toolbar->setObjectName(QString::fromAscii((*it)->command().c_str()));
 
193
            toolbar->setVisible(visible);
 
194
        } else {
 
195
            toolbar->setVisible(visible);
 
196
            toolbar->toggleViewAction()->setVisible(true);
 
197
            int index = toolbars.indexOf(toolbar);
 
198
            toolbars.removeAt(index);
 
199
        }
 
200
 
 
201
        // setup the toolbar
 
202
        setup(*it, toolbar);
 
203
    }
 
204
 
 
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())
 
210
            continue;
 
211
        hPref->SetBool(toolbarName.constData(), (*it)->isVisible());
 
212
        (*it)->hide();
 
213
        (*it)->toggleViewAction()->setVisible(false);
 
214
    }
 
215
}
 
216
 
 
217
void ToolBarManager::setup(ToolBarItem* item, QToolBar* toolbar) const
 
218
{
 
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()));
 
225
        if (!action) {
 
226
            if ((*it)->command() == "Separator") {
 
227
                action = toolbar->addSeparator();
 
228
            } else {
 
229
                // Check if action was added successfully
 
230
                if (mgr.addTo((*it)->command().c_str(), toolbar))
 
231
                    action = toolbar->actions().last();
 
232
            }
 
233
 
 
234
            // set the tool button user data
 
235
            if (action) action->setData(QString::fromAscii((*it)->command().c_str()));
 
236
        } else {
 
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);
 
242
        }
 
243
    }
 
244
 
 
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);
 
248
    }
 
249
}
 
250
 
 
251
void ToolBarManager::saveState() const
 
252
{
 
253
    ParameterGrp::handle hPref = App::GetApplication().GetUserParameter().GetGroup("BaseApp")
 
254
                               ->GetGroup("MainWindow")->GetGroup("Toolbars");
 
255
 
 
256
    QList<QToolBar*> toolbars = toolBars();
 
257
    for (QStringList::ConstIterator it = this->toolbarNames.begin(); it != this->toolbarNames.end(); ++it) {
 
258
        QToolBar* toolbar = findToolBar(toolbars, *it);
 
259
        if (toolbar) {
 
260
            QByteArray toolbarName = toolbar->objectName().toUtf8();
 
261
            hPref->SetBool(toolbarName.constData(), toolbar->isVisible());
 
262
        }
 
263
    }
 
264
}
 
265
 
 
266
void ToolBarManager::restoreState() const
 
267
{
 
268
    ParameterGrp::handle hPref = App::GetApplication().GetUserParameter().GetGroup("BaseApp")
 
269
                               ->GetGroup("MainWindow")->GetGroup("Toolbars");
 
270
 
 
271
    QList<QToolBar*> toolbars = toolBars();
 
272
    for (QStringList::ConstIterator it = this->toolbarNames.begin(); it != this->toolbarNames.end(); ++it) {
 
273
        QToolBar* toolbar = findToolBar(toolbars, *it);
 
274
        if (toolbar) {
 
275
            QByteArray toolbarName = toolbar->objectName().toUtf8();
 
276
            toolbar->setVisible(hPref->GetBool(toolbarName.constData(), toolbar->isVisible()));
 
277
        }
 
278
    }
 
279
}
 
280
 
 
281
void ToolBarManager::retranslate() const
 
282
{
 
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()));
 
287
    }
 
288
}
 
289
 
 
290
QToolBar* ToolBarManager::findToolBar(const QList<QToolBar*>& toolbars, const QString& item) const
 
291
{
 
292
    for (QList<QToolBar*>::ConstIterator it = toolbars.begin(); it != toolbars.end(); ++it) {
 
293
        if ((*it)->objectName() == item)
 
294
            return *it;
 
295
    }
 
296
 
 
297
    return 0; // no item with the user data found
 
298
}
 
299
 
 
300
QAction* ToolBarManager::findAction(const QList<QAction*>& acts, const QString& item) const
 
301
{
 
302
    for (QList<QAction*>::ConstIterator it = acts.begin(); it != acts.end(); ++it) {
 
303
        if ((*it)->data().toString() == item)
 
304
            return *it;
 
305
    }
 
306
 
 
307
    return 0; // no item with the user data found
 
308
}
 
309
 
 
310
QList<QToolBar*> ToolBarManager::toolBars() const
 
311
{
 
312
    QWidget* mw = getMainWindow();
 
313
    QList<QToolBar*> tb;
 
314
    QList<QToolBar*> bars = getMainWindow()->findChildren<QToolBar*>();
 
315
    for (QList<QToolBar*>::ConstIterator it = bars.begin(); it != bars.end(); ++it) {
 
316
        if ((*it)->parentWidget() == mw)
 
317
            tb.push_back(*it);
 
318
    }
 
319
 
 
320
    return tb;
 
321
}