DlgActionsImp.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 <QDir>
00027 # include <QFileDialog>
00028 # include <QFileInfo>
00029 # include <QHeaderView>
00030 # include <QImageReader>
00031 # include <QMessageBox>
00032 # include <QTextStream>
00033 #endif
00034 
00035 #include "DlgActionsImp.h"
00036 #include "Action.h"
00037 #include "Application.h"
00038 #include "Command.h"
00039 #include "BitmapFactory.h"
00040 #include "Widgets.h"
00041 #include "ui_DlgChooseIcon.h"
00042 
00043 using namespace Gui::Dialog;
00044 
00045 /* TRANSLATOR Gui::Dialog::DlgCustomActionsImp */
00046 
00054 DlgCustomActionsImp::DlgCustomActionsImp( QWidget* parent )
00055   : CustomizeActionPage(parent), bShown( false )
00056 {
00057     this->setupUi(this);
00058     // search for all macros
00059     std::string cMacroPath = App::GetApplication().
00060         GetParameterGroupByPath("User parameter:BaseApp/Preferences/Macro")
00061         ->GetASCII("MacroPath",App::Application::getUserAppDataDir().c_str());
00062 
00063     QDir d(QString::fromUtf8(cMacroPath.c_str()), QLatin1String("*.FCMacro"));
00064     actionMacros->insertItems(0, d.entryList());
00065 
00066     QStringList labels; labels << tr("Icons") << tr("Macros");
00067     actionListWidget->setHeaderLabels(labels);
00068     actionListWidget->header()->hide();
00069     showActions();
00070 }
00071 
00073 DlgCustomActionsImp::~DlgCustomActionsImp()
00074 {
00075 }
00076 
00081 void DlgCustomActionsImp::showEvent(QShowEvent* e)
00082 {
00083     QWidget::showEvent(e);
00084     if (actionMacros->count() == 0 && bShown == false)
00085     {
00086         bShown = true;
00087         QMessageBox::warning(this, tr("No macro"),tr("No macros found."));
00088     }
00089 }
00090 
00091 bool DlgCustomActionsImp::event(QEvent* e)
00092 {
00093     bool ok = QWidget::event(e);
00094 
00095     if (e->type() == QEvent::ParentChange || e->type() == QEvent::ParentAboutToChange)
00096     {
00097         QWidget* topLevel = this->parentWidget();
00098         while (topLevel && !topLevel->inherits("QDialog"))
00099             topLevel = topLevel->parentWidget();
00100         if ( topLevel )
00101         {
00102             int index = topLevel->metaObject()->indexOfSignal( QMetaObject::normalizedSignature("addMacroAction(const QByteArray&)") );
00103             if ( index >= 0 ) {
00104                 if ( e->type() == QEvent::ParentChange ) {
00105                     connect(this, SIGNAL(addMacroAction( const QByteArray& )), 
00106                             topLevel, SIGNAL(addMacroAction( const QByteArray& )));
00107                     connect(this, SIGNAL(removeMacroAction( const QByteArray& )), 
00108                             topLevel, SIGNAL(removeMacroAction( const QByteArray& )));
00109                     connect(this, SIGNAL(modifyMacroAction( const QByteArray& )), 
00110                             topLevel, SIGNAL(modifyMacroAction( const QByteArray& )));
00111                 } else {
00112                     disconnect(this, SIGNAL(addMacroAction( const QByteArray& )), 
00113                                topLevel, SIGNAL(addMacroAction( const QByteArray& )));
00114                     disconnect(this, SIGNAL(removeMacroAction( const QByteArray& )), 
00115                                topLevel, SIGNAL(removeMacroAction( const QByteArray& )));
00116                     disconnect(this, SIGNAL(modifyMacroAction( const QByteArray& )), 
00117                                topLevel, SIGNAL(modifyMacroAction( const QByteArray& )));
00118                 }
00119             }
00120         }
00121     }
00122 
00123     return ok;
00124 }
00125 
00126 void DlgCustomActionsImp::onAddMacroAction(const QByteArray&)
00127 {
00128   // does nothing
00129 }
00130 
00131 void DlgCustomActionsImp::onRemoveMacroAction(const QByteArray&)
00132 {
00133   // does nothing
00134 }
00135 
00136 void DlgCustomActionsImp::onModifyMacroAction(const QByteArray&)
00137 {
00138   // does nothing
00139 }
00140 
00141 void DlgCustomActionsImp::showActions()
00142 {
00143     CommandManager& rclMan = Application::Instance->commandManager();
00144     std::vector<Command*> aclCurMacros = rclMan.getGroupCommands("Macros");
00145     for (std::vector<Command*>::iterator it = aclCurMacros.begin(); it != aclCurMacros.end(); ++it)
00146     {
00147         QTreeWidgetItem* item = new QTreeWidgetItem(actionListWidget);
00148         QByteArray actionName = (*it)->getName();
00149         item->setData(1, Qt::UserRole, actionName);
00150         item->setText(1, QString::fromUtf8((*it)->getMenuText()));
00151         item->setSizeHint(0, QSize(32, 32));
00152         item->setBackgroundColor(0, Qt::lightGray);
00153         if ( (*it)->getPixmap() )
00154             item->setIcon(0, BitmapFactory().pixmap((*it)->getPixmap()));
00155     }
00156 
00157     actionListWidget->resizeColumnToContents(0);
00158 }
00159 
00160 void DlgCustomActionsImp::on_actionListWidget_itemActivated(QTreeWidgetItem *item)
00161 {
00162     if (!item) 
00163         return; // no valid item
00164 
00165     // search for the command in the manager and if necessary in the temporary created ones
00166     QByteArray actionName = item->data(1, Qt::UserRole).toByteArray();
00167     CommandManager& rclMan = Application::Instance->commandManager();
00168     Command* pCmd = rclMan.getCommandByName(actionName.constData());
00169     MacroCommand* pScript = dynamic_cast<MacroCommand*>(pCmd);
00170 
00171     // if valid command
00172     if ( pScript )
00173     {
00174         bool bFound = false;
00175         QString scriptName = QString::fromUtf8(pScript->getScriptName());
00176         for (int i = 0; i<actionMacros->count(); i++)
00177         {
00178             if (actionMacros->itemText(i).startsWith(scriptName, Qt::CaseSensitive))
00179             {
00180                 bFound = true;
00181                 actionMacros->setCurrentIndex(i);
00182                 break;
00183             }
00184         }
00185 
00186         if (!bFound)
00187         {
00188             QMessageBox::critical(this, tr("Macro not found"), 
00189                     tr("Sorry, couldn't find macro file '%1'.").arg(scriptName));
00190         }
00191 
00192         // fill up labels with the command's data
00193         actionWhatsThis -> setText(QString::fromUtf8(pScript->getWhatsThis()));
00194         actionMenu      -> setText(QString::fromUtf8(pScript->getMenuText()));
00195         actionToolTip   -> setText(QString::fromUtf8(pScript->getToolTipText()));
00196         actionStatus    -> setText(QString::fromUtf8(pScript->getStatusTip()));
00197         actionAccel     -> setText(QString::fromAscii(pScript->getAccel()));
00198         pixmapLabel->clear();
00199         m_sPixmap = QString::null;
00200         const char* name = pScript->getPixmap();
00201         if (name && std::strlen(name) > 2)
00202         {
00203             QPixmap p = Gui::BitmapFactory().pixmap(pScript->getPixmap());
00204             pixmapLabel->setPixmap(p);
00205             m_sPixmap = QString::fromUtf8(name); // can also be a path
00206         }
00207     }
00208 }
00209 
00210 void DlgCustomActionsImp::on_buttonAddAction_clicked()
00211 {
00212     if (actionMacros-> currentText().isEmpty())
00213     {
00214         QMessageBox::warning(this, tr("Empty macro"),tr("Please specify the macro first."));
00215         return;
00216     }
00217 
00218     if (actionMenu->text().isEmpty())
00219     {
00220         QMessageBox::warning(this, tr("Empty text"),tr("Please specify the menu text first."));
00221         return;
00222     }
00223 
00224     // search for the command in the manager
00225     QByteArray actionName = newActionName().toAscii();
00226     CommandManager& rclMan = Application::Instance->commandManager();
00227     MacroCommand* macro = new MacroCommand(actionName);
00228     rclMan.addCommand( macro );
00229 
00230     // add new action
00231     QTreeWidgetItem* item = new QTreeWidgetItem(actionListWidget);
00232     item->setData(1, Qt::UserRole, actionName);
00233     item->setText(1, actionMenu->text());
00234     item->setSizeHint(0, QSize(32, 32));
00235     item->setBackgroundColor(0, Qt::lightGray);
00236     if (pixmapLabel->pixmap())
00237         item->setIcon(0, *pixmapLabel->pixmap());
00238 
00239     // Convert input text into utf8
00240     if (!actionWhatsThis->text().isEmpty())
00241         macro->setWhatsThis(actionWhatsThis->text().toUtf8());
00242     actionWhatsThis->clear();
00243   
00244     if (!actionMacros-> currentText().isEmpty())
00245         macro->setScriptName(actionMacros->currentText().toUtf8());
00246   
00247     if (!actionMenu->text().isEmpty())
00248         macro->setMenuText(actionMenu->text().toUtf8());
00249     actionMenu->clear();
00250 
00251     if (!actionToolTip->text().isEmpty())
00252         macro->setToolTipText(actionToolTip->text().toUtf8());
00253     actionToolTip->clear();
00254 
00255     if (!actionStatus->text().isEmpty())
00256         macro->setStatusTip(actionStatus->text().toUtf8());
00257     actionStatus->clear();
00258 
00259     if (!m_sPixmap.isEmpty())
00260         macro->setPixmap(m_sPixmap.toAscii());
00261     pixmapLabel->clear();
00262     m_sPixmap = QString::null;
00263 
00264     if (!actionAccel->text().isEmpty()) {
00265       macro->setAccel(actionAccel->text().toAscii());
00266     }
00267     actionAccel->clear();
00268 
00269     // emit signal to notify the container widget
00270     addMacroAction(actionName);
00271 }
00272 
00273 void DlgCustomActionsImp::on_buttonReplaceAction_clicked()
00274 {
00275     QTreeWidgetItem* item = actionListWidget->currentItem();
00276     if (!item)
00277     {
00278         QMessageBox::warning(this, tr("No item selected"),tr("Please select a macro item first."));
00279         return;
00280     }
00281 
00282     if (actionMenu->text().isEmpty())
00283     {
00284         QMessageBox::warning(this, tr("Empty text"),tr("Please specify the menu text first."));
00285         return;
00286     }
00287 
00288     // search for the command in the manager
00289     QByteArray actionName = item->data(1, Qt::UserRole).toByteArray();
00290     item->setText(1, actionMenu->text());
00291     CommandManager& rclMan = Application::Instance->commandManager();
00292     Command* pCmd = rclMan.getCommandByName(actionName.constData());
00293     MacroCommand* macro = dynamic_cast<MacroCommand*>(pCmd);
00294 
00295     if (!actionWhatsThis->text().isEmpty())
00296         macro->setWhatsThis(actionWhatsThis->text().toUtf8());
00297     actionWhatsThis->clear();
00298   
00299     if (!actionMacros-> currentText().isEmpty())
00300         macro->setScriptName(actionMacros->currentText().toUtf8());
00301   
00302     if (!actionMenu->text().isEmpty())
00303         macro->setMenuText(actionMenu->text().toUtf8());
00304     actionMenu->clear();
00305 
00306     if (!actionToolTip->text().isEmpty())
00307         macro->setToolTipText(actionToolTip->text().toUtf8());
00308     actionToolTip->clear();
00309 
00310     if (!actionStatus->text().isEmpty())
00311         macro->setStatusTip(actionStatus->text().toUtf8());
00312     actionStatus->clear();
00313 
00314     if (!m_sPixmap.isEmpty())
00315         macro->setPixmap(m_sPixmap.toAscii());
00316     pixmapLabel->clear();
00317     m_sPixmap = QString::null;
00318 
00319     if (!actionAccel->text().isEmpty()) {
00320         macro->setAccel(actionAccel->text().toAscii());
00321     }
00322     actionAccel->clear();
00323 
00324     // check whether the macro is already in use
00325     Action* action = macro->getAction();
00326     if (action)
00327     {
00328         // does all the text related stuff
00329         action->setText(QString::fromUtf8(macro->getMenuText()));
00330         action->setToolTip(QString::fromUtf8(macro->getToolTipText()));
00331         action->setWhatsThis(QString::fromUtf8(macro->getWhatsThis()));
00332         action->setStatusTip(QString::fromUtf8(macro->getStatusTip()));
00333         if( macro->getPixmap() )
00334             action->setIcon(Gui::BitmapFactory().pixmap(macro->getPixmap()));
00335         action->setShortcut(QString::fromAscii(macro->getAccel()));
00336     }
00337 
00338     // emit signal to notify the container widget
00339     modifyMacroAction(actionName);
00340 
00341     // call this at the end because it internally invokes the highlight method
00342     if (macro->getPixmap())
00343         item->setIcon(0, Gui::BitmapFactory().pixmap(macro->getPixmap()));
00344 }
00345 
00346 void DlgCustomActionsImp::on_buttonRemoveAction_clicked()
00347 {
00348     // remove item from list view
00349     QTreeWidgetItem* item = actionListWidget->currentItem();
00350     if (!item) 
00351         return;
00352     int current = actionListWidget->indexOfTopLevelItem(item);
00353     actionListWidget->takeTopLevelItem(current);
00354     QByteArray actionName = item->data(1, Qt::UserRole).toByteArray();
00355     delete item;
00356 
00357     // if the command is registered in the manager just remove it
00358     CommandManager& rclMan = Application::Instance->commandManager();
00359     std::vector<Command*> aclCurMacros = rclMan.getGroupCommands("Macros");
00360     for (std::vector<Command*>::iterator it2 = aclCurMacros.begin(); it2!= aclCurMacros.end(); ++it2)
00361     {
00362         if (actionName == (*it2)->getName())
00363         {
00364             // emit signal to notify the container widget
00365             removeMacroAction(actionName);
00366             // remove from manager and delete it immediately
00367             rclMan.removeCommand(*it2);
00368             break;
00369         }
00370     }
00371 }
00372 
00373 IconDialog::IconDialog(QWidget* parent)
00374   : QDialog(parent), ui(new Ui_DlgChooseIcon)
00375 {
00376     ui->setupUi(this);
00377     ui->listWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
00378     // signals and slots connections
00379     connect(ui->listWidget, SIGNAL(itemClicked (QListWidgetItem *)),
00380             this, SLOT(accept()));
00381     connect(ui->addButton, SIGNAL(clicked()),
00382             this, SLOT(onAddIconPath()));
00383 
00384     QListWidgetItem* item;
00385     QStringList names = BitmapFactory().findIconFiles();
00386     for (QStringList::Iterator it = names.begin(); it != names.end(); ++it) {
00387         item = new QListWidgetItem(ui->listWidget);
00388         item->setIcon(QIcon(*it));
00389         item->setText(QFileInfo(*it).baseName());
00390         item->setToolTip(*it);
00391     }
00392 }
00393 
00394 IconDialog::~IconDialog()
00395 {
00396     delete ui;
00397 }
00398 
00399 QListWidgetItem* IconDialog::currentItem() const
00400 {
00401     return ui->listWidget->currentItem();
00402 }
00403 
00404 void IconDialog::resizeEvent(QResizeEvent*)
00405 {
00406     ui->listWidget->setFlow(QListView::LeftToRight);
00407 }
00408 
00409 void IconDialog::onAddIconPath()
00410 {
00411     QString dir = QFileDialog::getExistingDirectory(this, IconDialog::tr("Add icon"), QString());
00412     if (!dir.isEmpty()) {
00413         QStringList filters;
00414         QList<QByteArray> formats = QImageReader::supportedImageFormats();
00415         for (QList<QByteArray>::iterator it = formats.begin(); it != formats.end(); ++it)
00416             filters << QString::fromAscii("*.%1").arg(QString::fromAscii(*it).toLower());
00417         QDir d(dir);
00418         d.setNameFilters(filters);
00419         QFileInfoList fi = d.entryInfoList();
00420         for (QFileInfoList::iterator it = fi.begin(); it != fi.end(); ++it) {
00421             QListWidgetItem* item;
00422             QString file = it->absoluteFilePath();
00423             item = new QListWidgetItem(ui->listWidget);
00424             item->setIcon(QIcon(file));
00425             item->setText(it->baseName());
00426             item->setToolTip(file);
00427         }
00428 
00429         BitmapFactory().addPath(dir);
00430         BitmapFactory().addCustomPath(dir);
00431     }
00432 }
00433 
00434 void DlgCustomActionsImp::on_buttonChoosePixmap_clicked()
00435 {
00436     // create a dialog showing all pixmaps
00437     Gui::Dialog::IconDialog dlg(this);
00438     dlg.setModal(true);
00439     dlg.exec();
00440 
00441     pixmapLabel->clear();
00442     m_sPixmap = QString::null;
00443     if (dlg.result() == QDialog::Accepted) {
00444         QListWidgetItem* item = dlg.currentItem();
00445         if (item) {
00446             m_sPixmap = item->text();
00447             pixmapLabel->setPixmap(item->icon().pixmap(QSize(32,32)));
00448         }
00449     }
00450 }
00451 
00452 QString DlgCustomActionsImp::newActionName()
00453 {
00454     int id = 0;
00455     QString sName;
00456     bool bUsed;
00457 
00458     CommandManager& rclMan = Application::Instance->commandManager();
00459     std::vector<Command*> aclCurMacros = rclMan.getGroupCommands("Macros");
00460 
00461     do
00462     {
00463         bUsed = false;
00464         sName = QString::fromAscii("Std_Macro_%1").arg( id++ );
00465 
00466         std::vector<Command*>::iterator it;
00467         for ( it = aclCurMacros.begin(); it!= aclCurMacros.end(); ++it )
00468         {
00469             if (sName == QLatin1String((*it)->getName()))
00470             {
00471                 bUsed = true;
00472                 break;
00473             }
00474         }
00475     } while ( bUsed );
00476 
00477     return sName;
00478 }
00479 
00480 void DlgCustomActionsImp::changeEvent(QEvent *e)
00481 {
00482     if (e->type() == QEvent::LanguageChange) {
00483         this->retranslateUi(this);
00484         actionListWidget->clear();
00485         showActions();
00486         actionAccel->setText(qApp->translate("Gui::AccelLineEdit", "none"));
00487     }
00488     QWidget::changeEvent(e);
00489 }
00490 
00491 
00492 #include "moc_DlgActionsImp.cpp" 

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