DlgCommandsImp.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) 2004 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 <QHeaderView>
00027 # include <QTreeWidgetItemIterator>
00028 # include <algorithm>
00029 # include <vector>
00030 #endif
00031 
00032 #include "DlgCommandsImp.h"
00033 #include "Application.h"
00034 #include "Command.h"
00035 #include "BitmapFactory.h"
00036 #include "Widgets.h"
00037 
00038 using namespace Gui::Dialog;
00039 
00040 namespace Gui { namespace Dialog {
00041 typedef std::vector< std::pair<QLatin1String, QString> > GroupMap;
00042 
00043 struct GroupMap_find {
00044     const QLatin1String& item;
00045     GroupMap_find(const QLatin1String& item) : item(item) {}
00046     bool operator () (const std::pair<QLatin1String, QString>& elem) const
00047     {
00048         return elem.first == item;
00049     }
00050 };
00051 }
00052 }
00053 
00054 /* TRANSLATOR Gui::Dialog::DlgCustomCommandsImp */
00055 
00063 DlgCustomCommandsImp::DlgCustomCommandsImp( QWidget* parent  )
00064   : CustomizeActionPage(parent)
00065 {
00066     this->setupUi(this);
00067 
00068     // paints for active and inactive the same color
00069     QPalette pal = categoryTreeWidget->palette();
00070     pal.setColor(QPalette::Inactive, QPalette::Highlight, pal.color(QPalette::Active, QPalette::Highlight));
00071     pal.setColor(QPalette::Inactive, QPalette::HighlightedText, pal.color(QPalette::Active, QPalette::HighlightedText));
00072     categoryTreeWidget->setPalette( pal );
00073 
00074     connect(commandTreeWidget, SIGNAL(currentItemChanged(QTreeWidgetItem*, QTreeWidgetItem*)), 
00075             this, SLOT(onDescription(QTreeWidgetItem*)));
00076     connect(categoryTreeWidget, SIGNAL(currentItemChanged(QTreeWidgetItem*, QTreeWidgetItem*)), 
00077             this, SLOT(onGroupActivated(QTreeWidgetItem*)));
00078 
00079     CommandManager & cCmdMgr = Application::Instance->commandManager();
00080     std::map<std::string,Command*> sCommands = cCmdMgr.getCommands();
00081 
00082     GroupMap groupMap;
00083     groupMap.push_back(std::make_pair(QLatin1String("File"), QString()));
00084     groupMap.push_back(std::make_pair(QLatin1String("Edit"), QString()));
00085     groupMap.push_back(std::make_pair(QLatin1String("View"), QString()));
00086     groupMap.push_back(std::make_pair(QLatin1String("Standard-View"), QString()));
00087     groupMap.push_back(std::make_pair(QLatin1String("Tools"), QString()));
00088     groupMap.push_back(std::make_pair(QLatin1String("Window"), QString()));
00089     groupMap.push_back(std::make_pair(QLatin1String("Help"), QString()));
00090     groupMap.push_back(std::make_pair(QLatin1String("Macros"), qApp->translate("Gui::MacroCommand", "Macros")));
00091 
00092     for (std::map<std::string,Command*>::iterator it = sCommands.begin(); it != sCommands.end(); ++it) {
00093         QLatin1String group(it->second->getGroupName());
00094         QString text = qApp->translate(it->second->className(), it->second->getGroupName());
00095         GroupMap::iterator jt;
00096         jt = std::find_if(groupMap.begin(), groupMap.end(), GroupMap_find(group));
00097         if (jt != groupMap.end())
00098             jt->second = text;
00099         else
00100             groupMap.push_back(std::make_pair(group, text));
00101     }
00102 
00103     QStringList labels; labels << tr("Category");
00104     categoryTreeWidget->setHeaderLabels(labels);
00105     for (GroupMap::iterator it = groupMap.begin(); it != groupMap.end(); ++it) {
00106         QTreeWidgetItem* item = new QTreeWidgetItem(categoryTreeWidget);
00107         item->setText(0, it->second);
00108         item->setData(0, Qt::UserRole, QVariant(it->first));
00109     }
00110 
00111     labels.clear();
00112     labels << tr("Icon") << tr("Command");
00113     commandTreeWidget->setHeaderLabels(labels);
00114     commandTreeWidget->header()->hide();
00115 
00116     categoryTreeWidget->setCurrentItem(categoryTreeWidget->topLevelItem(0));
00117 }
00118 
00120 DlgCustomCommandsImp::~DlgCustomCommandsImp()
00121 {
00122 }
00123 
00125 void DlgCustomCommandsImp::onDescription(QTreeWidgetItem *item)
00126 {
00127     if (item)
00128         textLabel->setText(item->toolTip(1));
00129     else
00130         textLabel->setText(QString());
00131 }
00132 
00134 void DlgCustomCommandsImp::onGroupActivated(QTreeWidgetItem* item)
00135 {
00136     if (!item) 
00137         return;
00138 
00139     QVariant data = item->data(0, Qt::UserRole);
00140     QString group = data.toString();
00141     commandTreeWidget->clear();
00142 
00143     CommandManager & cCmdMgr = Application::Instance->commandManager();
00144     std::vector<Command*> aCmds = cCmdMgr.getGroupCommands(group.toAscii());
00145     for (std::vector<Command*>::iterator it = aCmds.begin(); it != aCmds.end(); ++it) {
00146         QTreeWidgetItem* item = new QTreeWidgetItem(commandTreeWidget);
00147         item->setText(1, qApp->translate((*it)->className(), (*it)->getMenuText()));
00148         item->setToolTip(1, qApp->translate((*it)->className(), (*it)->getToolTipText()));
00149         item->setData(1, Qt::UserRole, QByteArray((*it)->getName()));
00150         item->setSizeHint(0, QSize(32, 32));
00151         item->setBackgroundColor(0, Qt::lightGray);
00152         if ((*it)->getPixmap())
00153             item->setIcon(0, BitmapFactory().pixmap((*it)->getPixmap()));
00154     }
00155 
00156     textLabel->setText(QString());
00157     commandTreeWidget->resizeColumnToContents(0);
00158 }
00159 
00160 void DlgCustomCommandsImp::onAddMacroAction(const QByteArray& macro)
00161 {
00162     QTreeWidgetItem* item = categoryTreeWidget->currentItem();
00163     if (!item)
00164         return;
00165 
00166     QVariant data = item->data(0, Qt::UserRole);
00167     QString group = data.toString();
00168     if (group == QLatin1String("Macros"))
00169     {
00170         CommandManager & cCmdMgr = Application::Instance->commandManager();
00171         Command* pCmd = cCmdMgr.getCommandByName(macro);
00172 
00173         QTreeWidgetItem* item = new QTreeWidgetItem(commandTreeWidget);
00174         item->setText(1, QString::fromUtf8(pCmd->getMenuText()));
00175         item->setToolTip(1, QString::fromUtf8(pCmd->getToolTipText()));
00176         item->setData(1, Qt::UserRole, macro);
00177         item->setSizeHint(0, QSize(32, 32));
00178         item->setBackgroundColor(0, Qt::lightGray);
00179         if (pCmd->getPixmap())
00180             item->setIcon(0, BitmapFactory().pixmap(pCmd->getPixmap()));
00181     }
00182 }
00183 
00184 void DlgCustomCommandsImp::onRemoveMacroAction(const QByteArray& macro)
00185 {
00186     QTreeWidgetItem* item = categoryTreeWidget->currentItem();
00187     if (!item)
00188         return;
00189 
00190     QVariant data = item->data(0, Qt::UserRole);
00191     QString group = data.toString();
00192     if (group == QLatin1String("Macros"))
00193     {
00194         for (int i=0; i<commandTreeWidget->topLevelItemCount(); i++) {
00195             QTreeWidgetItem* item = commandTreeWidget->topLevelItem(i);
00196             QByteArray command = item->data(1, Qt::UserRole).toByteArray();
00197             if (command == macro) {
00198                 commandTreeWidget->takeTopLevelItem(i);
00199                 delete item;
00200                 break;
00201             }
00202         }
00203     }
00204 }
00205 
00206 void DlgCustomCommandsImp::onModifyMacroAction(const QByteArray& macro)
00207 {
00208     QTreeWidgetItem* item = categoryTreeWidget->currentItem();
00209     if (!item)
00210         return;
00211 
00212     QVariant data = item->data(0, Qt::UserRole);
00213     QString group = data.toString();
00214     if (group == QLatin1String("Macros"))
00215     {
00216         CommandManager & cCmdMgr = Application::Instance->commandManager();
00217         Command* pCmd = cCmdMgr.getCommandByName(macro);
00218         for (int i=0; i<commandTreeWidget->topLevelItemCount(); i++) {
00219             QTreeWidgetItem* item = commandTreeWidget->topLevelItem(i);
00220             QByteArray command = item->data(1, Qt::UserRole).toByteArray();
00221             if (command == macro) {
00222                 item->setText(1, QString::fromUtf8(pCmd->getMenuText()));
00223                 item->setToolTip(1, QString::fromUtf8(pCmd->getToolTipText()));
00224                 item->setData(1, Qt::UserRole, macro);
00225                 item->setSizeHint(0, QSize(32, 32));
00226                 item->setBackgroundColor(0, Qt::lightGray);
00227                 if (pCmd->getPixmap())
00228                     item->setIcon(0, BitmapFactory().pixmap(pCmd->getPixmap()));
00229                 if (commandTreeWidget->isItemSelected(item))
00230                     onDescription(item);
00231                 break;
00232             }
00233         }
00234     }
00235 }
00236 
00237 void DlgCustomCommandsImp::changeEvent(QEvent *e)
00238 {
00239     if (e->type() == QEvent::LanguageChange) {
00240         this->retranslateUi(this);
00241         QStringList labels; labels << tr("Category");
00242         categoryTreeWidget->setHeaderLabels(labels);
00243 
00244         CommandManager & cCmdMgr = Application::Instance->commandManager();
00245         QTreeWidgetItemIterator it(categoryTreeWidget);
00246         while (*it) {
00247             QVariant data = (*it)->data(0, Qt::UserRole);
00248             std::vector<Command*> aCmds = cCmdMgr.getGroupCommands(data.toByteArray());
00249             if (!aCmds.empty()) {
00250                 QString text = qApp->translate(aCmds[0]->className(), aCmds[0]->getGroupName());
00251                 (*it)->setText(0, text);
00252             }
00253             ++it;
00254         }
00255         onGroupActivated(categoryTreeWidget->topLevelItem(0));
00256     }
00257     QWidget::changeEvent(e);
00258 }
00259 
00260 #include "moc_DlgCommandsImp.cpp"

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