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

« back to all changes in this revision

Viewing changes to src/Gui/DlgCommandsImp.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) 2004 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 "DlgCommandsImp.h"
 
27
#include "Application.h"
 
28
#include "Command.h"
 
29
#include "BitmapFactory.h"
 
30
#include "Widgets.h"
 
31
 
 
32
using namespace Gui::Dialog;
 
33
 
 
34
/* TRANSLATOR Gui::Dialog::DlgCustomCommandsImp */
 
35
 
 
36
/**
 
37
 *  Constructs a DlgCustomCommandsImp which is a child of 'parent', with the
 
38
 *  name 'name' and widget flags set to 'f'
 
39
 *
 
40
 *  The dialog will by default be modeless, unless you set 'modal' to
 
41
 *  TRUE to construct a modal dialog.
 
42
 */
 
43
DlgCustomCommandsImp::DlgCustomCommandsImp( QWidget* parent  )
 
44
  : CustomizeActionPage(parent)
 
45
{
 
46
    this->setupUi(this);
 
47
  
 
48
    // paints for active and inactive the same color
 
49
    QPalette pal = categoryTreeWidget->palette();
 
50
    pal.setColor(QPalette::Inactive, QPalette::Highlight, pal.color(QPalette::Active, QPalette::Highlight));
 
51
    pal.setColor(QPalette::Inactive, QPalette::HighlightedText, pal.color(QPalette::Active, QPalette::HighlightedText));
 
52
    categoryTreeWidget->setPalette( pal );
 
53
 
 
54
    connect(commandTreeWidget, SIGNAL(currentItemChanged(QTreeWidgetItem*, QTreeWidgetItem*)), 
 
55
            this, SLOT(onDescription(QTreeWidgetItem*)));
 
56
    connect(categoryTreeWidget, SIGNAL(currentItemChanged(QTreeWidgetItem*, QTreeWidgetItem*)), 
 
57
            this, SLOT(onGroupActivated(QTreeWidgetItem*)));
 
58
 
 
59
    CommandManager & cCmdMgr = Application::Instance->commandManager();
 
60
    std::map<std::string,Command*> sCommands = cCmdMgr.getCommands();
 
61
 
 
62
    // do a special sort before adding to the tree view
 
63
    QStringList groups;
 
64
    groups << QLatin1String("File") << QLatin1String("Edit")
 
65
           << QLatin1String("View") << QLatin1String("Standard-View")
 
66
           << QLatin1String("Tools") << QLatin1String("Window")
 
67
           << QLatin1String("Help") << QLatin1String("Macros");
 
68
    for (std::map<std::string,Command*>::iterator it = sCommands.begin(); it != sCommands.end(); ++it) {
 
69
        QString group = QLatin1String(it->second->getGroupName());
 
70
        if (!groups.contains(group))
 
71
            groups << group;
 
72
    }
 
73
 
 
74
    QStringList labels; labels << tr("Category");
 
75
    categoryTreeWidget->setHeaderLabels(labels);
 
76
    for ( QStringList::Iterator It = groups.begin(); It != groups.end(); ++It ) {
 
77
        QTreeWidgetItem* item = new QTreeWidgetItem(categoryTreeWidget);
 
78
        item->setText(0, QObject::tr((*It).toAscii()));
 
79
        item->setData(0, Qt::UserRole, QVariant(*It));
 
80
    }
 
81
 
 
82
    labels.clear();
 
83
    labels << tr("Icon") << tr("Command");
 
84
    commandTreeWidget->setHeaderLabels(labels);
 
85
    commandTreeWidget->header()->hide();
 
86
 
 
87
    categoryTreeWidget->setCurrentItem(categoryTreeWidget->topLevelItem(0));
 
88
}
 
89
 
 
90
/** Destroys the object and frees any allocated resources */
 
91
DlgCustomCommandsImp::~DlgCustomCommandsImp()
 
92
{
 
93
}
 
94
 
 
95
/** Shows the description for the corresponding command */
 
96
void DlgCustomCommandsImp::onDescription(QTreeWidgetItem *item)
 
97
{
 
98
    if (item)
 
99
        textLabel->setText(item->toolTip(1));
 
100
    else
 
101
        textLabel->setText(QString());
 
102
}
 
103
 
 
104
/** Shows all commands of this category */
 
105
void DlgCustomCommandsImp::onGroupActivated(QTreeWidgetItem* item)
 
106
{
 
107
    if (!item) 
 
108
        return;
 
109
 
 
110
    QVariant data = item->data(0, Qt::UserRole);
 
111
    QString group = data.toString();
 
112
    commandTreeWidget->clear();
 
113
 
 
114
    CommandManager & cCmdMgr = Application::Instance->commandManager();
 
115
    std::vector<Command*> aCmds = cCmdMgr.getGroupCommands(group.toAscii());
 
116
    for (std::vector<Command*>::iterator it = aCmds.begin(); it != aCmds.end(); ++it) {
 
117
        QTreeWidgetItem* item = new QTreeWidgetItem(commandTreeWidget);
 
118
        item->setText(1, QObject::trUtf8((*it)->getMenuText()));
 
119
        item->setToolTip(1, QObject::trUtf8((*it)->getToolTipText()));
 
120
        item->setData(1, Qt::UserRole, QByteArray((*it)->getName()));
 
121
        item->setSizeHint(0, QSize(32, 32));
 
122
        item->setBackgroundColor(0, Qt::lightGray);
 
123
        if ((*it)->getPixmap())
 
124
            item->setIcon(0, BitmapFactory().pixmap((*it)->getPixmap()));
 
125
    }
 
126
 
 
127
    textLabel->setText(QString());
 
128
    commandTreeWidget->resizeColumnToContents(0);
 
129
}
 
130
 
 
131
void DlgCustomCommandsImp::onAddMacroAction(const QByteArray& macro)
 
132
{
 
133
    QTreeWidgetItem* item = categoryTreeWidget->currentItem();
 
134
    if (!item)
 
135
        return;
 
136
 
 
137
    QVariant data = item->data(0, Qt::UserRole);
 
138
    QString group = data.toString();
 
139
    if (group == QLatin1String("Macros"))
 
140
    {
 
141
        CommandManager & cCmdMgr = Application::Instance->commandManager();
 
142
        Command* pCmd = cCmdMgr.getCommandByName(macro);
 
143
 
 
144
        QTreeWidgetItem* item = new QTreeWidgetItem(commandTreeWidget);
 
145
        item->setText(1, QString::fromUtf8(pCmd->getMenuText()));
 
146
        item->setToolTip(1, QString::fromUtf8(pCmd->getToolTipText()));
 
147
        item->setData(1, Qt::UserRole, macro);
 
148
        item->setSizeHint(0, QSize(32, 32));
 
149
        item->setBackgroundColor(0, Qt::lightGray);
 
150
        if (pCmd->getPixmap())
 
151
            item->setIcon(0, BitmapFactory().pixmap(pCmd->getPixmap()));
 
152
    }
 
153
}
 
154
 
 
155
void DlgCustomCommandsImp::onRemoveMacroAction(const QByteArray& macro)
 
156
{
 
157
    QTreeWidgetItem* item = categoryTreeWidget->currentItem();
 
158
    if (!item)
 
159
        return;
 
160
 
 
161
    QVariant data = item->data(0, Qt::UserRole);
 
162
    QString group = data.toString();
 
163
    if (group == QLatin1String("Macros"))
 
164
    {
 
165
        for (int i=0; i<commandTreeWidget->topLevelItemCount(); i++) {
 
166
            QTreeWidgetItem* item = commandTreeWidget->topLevelItem(i);
 
167
            QByteArray command = item->data(1, Qt::UserRole).toByteArray();
 
168
            if (command == macro) {
 
169
                commandTreeWidget->takeTopLevelItem(i);
 
170
                delete item;
 
171
                break;
 
172
            }
 
173
        }
 
174
    }
 
175
}
 
176
 
 
177
void DlgCustomCommandsImp::onModifyMacroAction(const QByteArray& macro)
 
178
{
 
179
    QTreeWidgetItem* item = categoryTreeWidget->currentItem();
 
180
    if (!item)
 
181
        return;
 
182
 
 
183
    QVariant data = item->data(0, Qt::UserRole);
 
184
    QString group = data.toString();
 
185
    if (group == QLatin1String("Macros"))
 
186
    {
 
187
        CommandManager & cCmdMgr = Application::Instance->commandManager();
 
188
        Command* pCmd = cCmdMgr.getCommandByName(macro);
 
189
        for (int i=0; i<commandTreeWidget->topLevelItemCount(); i++) {
 
190
            QTreeWidgetItem* item = commandTreeWidget->topLevelItem(i);
 
191
            QByteArray command = item->data(1, Qt::UserRole).toByteArray();
 
192
            if (command == macro) {
 
193
                item->setText(1, QString::fromUtf8(pCmd->getMenuText()));
 
194
                item->setToolTip(1, QString::fromUtf8(pCmd->getToolTipText()));
 
195
                item->setData(1, Qt::UserRole, macro);
 
196
                item->setSizeHint(0, QSize(32, 32));
 
197
                item->setBackgroundColor(0, Qt::lightGray);
 
198
                if (pCmd->getPixmap())
 
199
                    item->setIcon(0, BitmapFactory().pixmap(pCmd->getPixmap()));
 
200
                if (commandTreeWidget->isItemSelected(item))
 
201
                    onDescription(item);
 
202
                break;
 
203
            }
 
204
        }
 
205
    }
 
206
}
 
207
 
 
208
#include "moc_DlgCommandsImp.cpp"