Back to index

scribus-ng  1.3.4.dfsg+svn20071115
menumanager.cpp
Go to the documentation of this file.
00001 /*
00002 For general Scribus (>=1.3.2) copyright and licensing information please refer
00003 to the COPYING file provided with the program. Following this notice may exist
00004 a copyright and/or license notice that predates the release of Scribus 1.3.2
00005 for which a new license (GPL+exception) is in place.
00006 */
00007 /***************************************************************************
00008     begin                : Jan 2005
00009     copyright            : (C) 2005 by Craig Bradney
00010     email                : cbradney@zip.com.au
00011  ***************************************************************************/
00012 
00013 /***************************************************************************
00014  *                                                                         *
00015  *   This program is free software; you can redistribute it and/or modify  *
00016  *   it under the terms of the GNU General Public License as published by  *
00017  *   the Free Software Foundation; either version 2 of the License, or     *
00018  *   (at your option) any later version.                                   *
00019  *                                                                         *
00020  ***************************************************************************/
00021 #include <qdict.h>
00022 #include <qmenubar.h>
00023 #include <qpopupmenu.h>
00024 #include "scribus.h"
00025 #include "menumanager.h"
00026 #include "menumanager.moc"
00027 #include "scmenu.h"
00028 #include "scraction.h"
00029 
00030 
00031 
00032 MenuManager::MenuManager(QMenuBar* mb, QObject *parent, const char *name) : QObject(parent, name)
00033 {
00034        scribusMenuBar=mb;
00035        menuList.clear();
00036 }
00037 
00038 MenuManager::~MenuManager()
00039 {
00040 }
00041 
00042 bool MenuManager::createMenu(const QString &menuName, const QString &menuText, const QString parent, bool checkable)
00043 {
00044        bool retVal=false;
00045               
00046        ScrPopupMenu *newMenu = new ScrPopupMenu(NULL, NULL, menuName, menuText, parent, checkable);
00047        if (newMenu)
00048        {
00049               menuList.insert(menuName, newMenu);
00050               if (!parent.isNull() && menuList[parent])
00051                      retVal=menuList[parent]->insertSubMenu(newMenu);
00052        }
00053        return retVal;
00054 }
00055 
00056 
00057 bool MenuManager::createMenu(const QString &menuName, const QIconSet menuIcon, const QString &menuText, const QString parent, bool checkable)
00058 {
00059        bool retVal=false;
00060        ScrPopupMenu *newMenu = new ScrPopupMenu(NULL, NULL, menuName, menuIcon, menuText, parent, checkable);
00061        if (newMenu)
00062        {
00063               menuList.insert(menuName, newMenu);
00064               if (!parent.isNull() && menuList[parent])
00065                      retVal=menuList[parent]->insertSubMenu(newMenu);
00066        }
00067        return retVal;
00068 }
00069 
00070 bool MenuManager::addMenuToMenu(const QString &child, const QString &parent)
00071 {
00072        bool retVal=false;
00073        if (child==parent)
00074        {
00075               qDebug("%s", QString("Cannot add %1 menu to %2 menu (itself)").arg(child, parent).ascii());
00076               return false; 
00077        }
00078        if ((menuList.contains(child) && menuList[child]!=NULL) &&
00079               (menuList.contains(parent) && menuList[parent]!=NULL))
00080        {
00081               menuList.insert(child, menuList[child]);
00082               retVal=menuList[parent]->insertSubMenu(menuList[child]);
00083        }
00084        
00085        return retVal;
00086 }
00087 
00088 bool MenuManager::clearMenu(const QString &menuName)
00089 {
00090        bool retVal=false;
00091        if (menuList.contains(menuName) && menuList[menuName]!=NULL)
00092        {
00093               menuList[menuName]->clear();
00094               retVal=true;
00095        }
00096        return retVal;
00097 }
00098 
00099 void MenuManager::setMenuText(const QString &menuName, const QString &menuText)
00100 {
00101        if (menuList.contains(menuName) && menuList[menuName]!=NULL)
00102        {
00103               menuList[menuName]->setMenuText(menuText);
00104               QString parent=menuList[menuName]->getParentMenuName();
00105               if (!parent.isNull())
00106                      menuList[parent]->repopulateLocalMenu();
00107               
00108               int id=menuList[menuName]->getMenuBarID();
00109               if (id!=-1) {
00110                      QIconSet menuIcon = menuList[menuName]->getMenuIcon();
00111                      scribusMenuBar->changeItem(id, menuIcon, menuText);
00112               }
00113        }
00114 }
00115 
00116 void MenuManager::setMenuIcon(const QString &menuName, const QIconSet &menuIcon)
00117 {
00118        if (menuList.contains(menuName) && menuList[menuName]!=NULL)
00119        {
00120               menuList[menuName]->setMenuIcon(menuIcon);
00121               QString parent=menuList[menuName]->getParentMenuName();
00122               if (!parent.isNull())
00123                      menuList[parent]->repopulateLocalMenu();
00124               
00125               int id=menuList[menuName]->getMenuBarID();
00126               if (id!=-1) {
00127                      QString menuText = menuList[menuName]->getMenuText();
00128                      scribusMenuBar->changeItem(id, menuIcon, menuText);
00129               }
00130        }
00131 }
00132 
00133 QPopupMenu *MenuManager::getLocalPopupMenu(const QString &menuName)
00134 {
00135        if (menuList.contains(menuName) && menuList[menuName]!=NULL)
00136               return menuList[menuName]->getLocalPopupMenu();
00137        return NULL;
00138 }
00139 
00140 bool MenuManager::deleteMenu(const QString &menuName, const QString &parent)
00141 {
00142        if (!parent.isNull())
00143        {
00144               if (menuList[parent] && menuList[parent]->hasSubMenu(menuList[menuName]))
00145                      menuList[parent]->removeSubMenu(menuList[menuName]);
00146        }
00147        menuList.remove(menuName);
00148        return true;
00149 }
00150 
00151 void MenuManager::setMenuEnabled(const QString &menuName, const bool enabled)
00152 {
00153        if (menuList.contains(menuName) && menuList[menuName]!=NULL)
00154        {
00155               menuList[menuName]->setEnabled(enabled);
00156               int mainID=menuList[menuName]->getMenuBarID();
00157               if (mainID!=-1)
00158                      scribusMenuBar->setItemEnabled(mainID, enabled);
00159        }
00160 }
00161 
00162 bool MenuManager::addMenuToMenuBar(const QString &menuName)
00163 {
00164        bool retVal=false;
00165        if (menuList.contains(menuName) && menuList[menuName]!=NULL)
00166        {
00167               int id=scribusMenuBar->insertItem( menuList[menuName]->getMenuIcon(), menuList[menuName]->getMenuText(), menuList[menuName]->getLocalPopupMenu());
00168               menuList[menuName]->setMenuBarID(id);
00169               retVal=true;
00170        }
00171        return retVal;
00172 }
00173 
00174 bool MenuManager::addMenuToMenuBarAfter(const QString &menuName, const QString &afterMenuName)
00175 {
00176        bool retVal=false;
00177        if (menuList.contains(menuName) && menuList[menuName]!=NULL)
00178        {
00179               if (menuList[afterMenuName])
00180               {
00181                      int afterID=menuList[afterMenuName]->getMenuBarID();
00182                      if (afterID!=-1)
00183                      {
00184                             int id=scribusMenuBar->insertItem( menuList[menuName]->getMenuIcon(), menuList[menuName]->getMenuText(), menuList[menuName]->getLocalPopupMenu(), scribusMenuBar->indexOf(afterID)+1, scribusMenuBar->indexOf(afterID)+1);
00185                             menuList[menuName]->setMenuBarID(id);
00186                             retVal=true;
00187                      }
00188               }
00189        }
00190        return retVal;
00191 }
00192 
00193 bool MenuManager::removeMenuFromMenuBar(const QString &menuName)
00194 {
00195        bool retVal=false;
00196        if (menuList.contains(menuName) && menuList[menuName]!=NULL)
00197        {
00198               int id=menuList[menuName]->getMenuBarID();
00199               if (id!=-1)
00200                      scribusMenuBar->removeItem( id );
00201               retVal=true;
00202        }
00203        return retVal;
00204 }
00205 
00206 bool MenuManager::addMenuToWidgetOfAction(const QString &menuName, ScrAction *action)
00207 {
00208        bool retVal=false;
00209        if (menuList.contains(menuName) && menuList[menuName]!=NULL && action!=NULL)
00210        {
00211               QWidget *w=action->getWidgetAddedTo();
00212               if (w)
00213               {
00214                      QString menuItemListClassName=w->className();
00215                      if (menuItemListClassName=="QToolButton")
00216                      {
00217                             QToolButton *toolButton=dynamic_cast<QToolButton *>(w);
00218                             if (toolButton!=NULL)
00219                             {
00220                                    toolButton->setPopup(menuList[menuName]->getLocalPopupMenu());
00221                                    retVal=true;
00222                             }
00223                      }
00224               }
00225        }
00226        return retVal;
00227 }
00228 
00229 bool MenuManager::addMenuItem(ScrAction *menuAction, const QString &parent)
00230 {
00231        bool retVal=false;
00232        if (menuList.contains(parent) && menuList[parent]!=NULL)
00233               retVal=menuList[parent]->insertMenuItem(menuAction);
00234        return retVal;
00235 }
00236 
00237 bool MenuManager::addMenuItem(QWidget *widget, const QString &parent)
00238 {
00239        bool retVal=false;
00240        if (menuList.contains(parent) && menuList[parent]!=NULL)
00241               retVal=menuList[parent]->insertMenuItem(widget);
00242        return retVal;
00243 }
00244 
00245 bool MenuManager::addMenuItemAfter(ScrAction *menuAction, const QString &parent, ScrAction *afterMenuAction)
00246 {
00247        bool retVal=false;
00248        if (menuList.contains(parent) && menuList[parent]!=NULL)
00249               retVal=menuList[parent]->insertMenuItemAfter(menuAction, afterMenuAction);
00250        return retVal;
00251 }
00252 
00253 // bool MenuManager::addMenuItemAfter(ScrAction *menuAction, const QString &parent, const QString &afterMenuName)
00254 // {
00255 //     ScrAction *actionFromName=NULL;
00256 //     //quick hack to make this work for existing plugins for now
00257 // //  if (parent=="File" && afterMenuName=="New")
00258 // //         actionFromName=m_ScMW->scrActions["fileNew"];
00259 // //  if (parent=="File" && afterMenuName=="Print")
00260 // //         actionFromName=m_ScMW->scrActions["filePrint"];
00261 // //  if (parent=="File" && afterMenuName=="SaveAs")
00262 // //         actionFromName=m_ScMW->scrActions["fileSaveAs"];
00263 // //  if (parent=="Help" && afterMenuName=="Manual")
00264 // //         actionFromName=m_ScMW->scrActions["helpManual"];
00265 //     bool retVal=false;
00266 //     if (menuList.contains(parent) && menuList[parent]!=NULL)
00267 //            retVal=menuList[parent]->insertMenuItemAfter(menuAction, actionFromName);
00268 //     return retVal;
00269 // }
00270 
00271 
00272 bool MenuManager::addMenuSeparator(const QString &parent)
00273 {
00274        bool retVal=false;
00275        if (menuList.contains(parent) && menuList[parent]!=NULL)
00276               retVal=menuList[parent]->insertMenuSeparator();
00277        return retVal;
00278        
00279 }
00280 
00281 bool MenuManager::removeMenuItem(ScrAction *menuAction, const QString &parent)
00282 {
00283        bool retVal=false;
00284        if (menuList.contains(parent) && menuList[parent]!=NULL)
00285               retVal=menuList[parent]->removeMenuItem(menuAction);
00286        return retVal;
00287 }
00288 
00289 void MenuManager::runMenuAtPos(const QString &menuName, const QPoint position)
00290 {
00291        if (menuList.contains(menuName) && menuList[menuName]!=NULL)
00292        {      
00293               QPopupMenu *popupmenu=menuList[menuName]->getLocalPopupMenu();
00294               if (popupmenu)
00295                      popupmenu->exec(position);
00296        }
00297 }
00298 
00299 // Used to generate a list of entries from the menu system into the keyboard shortcut manager
00300 void MenuManager::generateKeyManList(QStringList *actionNames)
00301 {
00302        if (actionNames)
00303        {
00304               if (scribusMenuBar)
00305               {
00306                      QMap<QString, ScrPopupMenu *>::Iterator menuListIt;
00307                      for (uint menuBarCount=0; menuBarCount<scribusMenuBar->count(); ++menuBarCount)
00308                      {
00309                             int menuBarMenuID=scribusMenuBar->idAt(menuBarCount);
00310                             bool menuBarItemFound=false;
00311                             for ( menuListIt = menuList.begin(); menuListIt!=menuList.end(); ++menuListIt)
00312                             {
00313                                    if(menuListIt.data()->getMenuBarID()==menuBarMenuID)
00314                                    {
00315                                           menuBarItemFound=true;
00316                                           break;
00317                                    }
00318                             }
00319                             if (menuBarItemFound)
00320                             {
00321                                    if (menuListIt.data())
00322                                    {
00323                                           ScrPopupMenu *currentMenu=menuListIt.data();
00324                                           currentMenu->generateEntryList(actionNames);
00325                                    }
00326                             }
00327                      }
00328               }
00329        }
00330 }
00331 
00332 bool MenuManager::empty()
00333 {
00334        return menuList.empty();
00335 }