Back to index

scribus-ng  1.3.4.dfsg+svn20071115
scmenu.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 <qptrlist.h>
00022 #include <qobject.h>
00023 #include <qmetaobject.h>
00024 #include <qpopupmenu.h>
00025 #include "scmenu.h"
00026 #include "scmenu.moc"
00027 #include "scraction.h"
00028 #include "util.h"
00029 
00030 ScrPopupMenu::ScrPopupMenu ( QWidget * parent, const char * name, const QString pMenuName, const QString pMenuText, const QString parentName, bool pCheckable ) : QObject(parent, name)
00031 {
00032        parentMenuName=parentName;
00033        parentMenuID=-1;
00034        menuText=pMenuText;
00035        menuName=pMenuName;
00036        menuBarID=-1;
00037        menuItemList.clear();
00038        localPopupMenu=new QPopupMenu(parent, name);
00039 //     localPopupMenu->setCheckable(pCheckable);
00040        enabled=true;
00041        checkable = pCheckable;
00042 }
00043 
00044 ScrPopupMenu::ScrPopupMenu ( QWidget * parent, const char * name, const QString pMenuName, const QIconSet pMenuIcon, const QString pMenuText, const QString parentName, bool pCheckable ) : QObject(parent, name)
00045 {
00046        parentMenuName=parentName;
00047        parentMenuID=-1;
00048        menuIcon=pMenuIcon;
00049        menuText=pMenuText;
00050        menuName=pMenuName;
00051        menuBarID=-1;
00052        menuItemList.clear();
00053        localPopupMenu=new QPopupMenu(parent, name);
00054 //     localPopupMenu->setCheckable(pCheckable);
00055        enabled=true;
00056        checkable=pCheckable;
00057 }
00058 
00059 ScrPopupMenu::~ScrPopupMenu()
00060 {
00061        delete localPopupMenu;
00062 }
00063 
00064 const QString ScrPopupMenu::getMenuText()
00065 {
00066        return menuText;
00067 }
00068 
00069 void ScrPopupMenu::setMenuText(const QString pMenuText)
00070 {
00071        menuText=pMenuText;
00072 }
00073 
00074 const QIconSet ScrPopupMenu::getMenuIcon()
00075 {
00076        return menuIcon;
00077 }
00078 
00079 void ScrPopupMenu::setMenuIcon(const QIconSet pMenuIcon)
00080 {
00081        menuIcon=pMenuIcon;
00082 }
00083 
00084 const QString ScrPopupMenu::getMenuName()
00085 {
00086        return menuName;
00087 }
00088 
00089 const QString ScrPopupMenu::getParentMenuName()
00090 {
00091        return parentMenuName;
00092 }
00093 
00094 void ScrPopupMenu::setMenuBarID(int id)
00095 {
00096        menuBarID=id;
00097 }
00098 
00099 int ScrPopupMenu::getMenuBarID()
00100 {
00101        return menuBarID;
00102 }
00103 
00104 QPopupMenu *ScrPopupMenu::getLocalPopupMenu()
00105 {
00106        return localPopupMenu;
00107 }
00108 
00109 bool ScrPopupMenu::hasSubMenu(ScrPopupMenu* subMenu)
00110 {
00111        return menuItemList.contains(QGuardedPtr<QObject>(subMenu));
00112 }
00113 
00114 void ScrPopupMenu::setParentMenuID(int id)
00115 {
00116        parentMenuID=id;
00117 }
00118 
00119 
00120 const int ScrPopupMenu::getParentMenuID()
00121 {
00122        return parentMenuID;
00123 }
00124 
00125 bool ScrPopupMenu::insertSubMenu(ScrPopupMenu* newSubMenu)
00126 {
00127        if (newSubMenu)
00128        {
00129               menuItemList.append(newSubMenu);
00130               localPopupMenu->insertItem(newSubMenu->getMenuIcon(), newSubMenu->getMenuText(), newSubMenu->getLocalPopupMenu());
00131               return true;
00132        }
00133        else
00134               return false;
00135 }
00136 
00137 bool ScrPopupMenu::removeSubMenu(ScrPopupMenu* subMenu)
00138 {
00139        if (menuItemList.remove(subMenu))
00140        {
00141               repopulateLocalMenu();
00142               return true;
00143        }
00144        return false;
00145 }
00146 
00147 bool ScrPopupMenu::insertMenuItem(ScrAction *newMenuAction)
00148 {
00149        if (newMenuAction)
00150        {
00151               
00152 #ifdef Q_WS_MAC
00153               bool menuListHasNoIcons = true;
00154               // look for ScrAction or ScrPopupMenu from the end of the list
00155               QValueList< QGuardedPtr<QObject> >::Iterator it = menuItemList.end(); 
00156               for (--it; it != menuItemList.end(); --it) {
00157                      QObject * menuItem = *it;
00158                      QString menuItemListClassName = menuItem->className();
00159                      if (menuItemListClassName == "ScrAction")
00160                      {
00161                             ScrAction * act = dynamic_cast<ScrAction *>(menuItem);
00162                             menuListHasNoIcons = act->iconSet().isNull();
00163                             break;
00164                      }
00165                      else if (menuItemListClassName == "ScrPopupMenu")
00166                      {
00167                             ScrPopupMenu * men = dynamic_cast<ScrPopupMenu *>(menuItem);
00168                             menuListHasNoIcons = men->getMenuIcon().isNull();
00169                             break;
00170                      }
00171                      
00172               }
00173               if (newMenuAction->iconSet().isNull() && ! menuListHasNoIcons)
00174                      newMenuAction->setIconSet(loadIcon("noicon.xpm"));
00175 #endif
00176               
00177               menuItemList.append(newMenuAction);
00178               newMenuAction->addTo(localPopupMenu);                   
00179               return true;
00180        }
00181        else 
00182               return false;
00183 }
00184 
00185 bool ScrPopupMenu::insertMenuItem(QWidget *widget)
00186 {
00187        if (widget)
00188        {
00189               ScrAction *widgetAction = new ScrAction( NULL, "widget_action" );
00190               menuItemList.append(widgetAction);
00191               localPopupMenu->insertItem(widget);
00192               return true;
00193        }
00194        else 
00195               return false;
00196 }
00197 
00198 bool ScrPopupMenu::insertMenuItemAfter(ScrAction *newMenuAction, ScrAction *afterMenuAction)
00199 {
00200        //Allow duplicate menu items ???
00201        //if (menuItemList.findRef(newMenuAction)!=-1)
00202        //     return false;
00203        
00204 #ifdef Q_WS_MAC
00205        if (newMenuAction->iconSet().isNull() && ! (afterMenuAction->iconSet().isNull()))
00206               newMenuAction->setIconSet(loadIcon("noicon.xpm"));
00207 #endif
00208               
00209        QValueList< QGuardedPtr<QObject> >::Iterator it=menuItemList.find(QGuardedPtr<QObject>(afterMenuAction));
00210        menuItemList.insert(++it, QGuardedPtr<QObject>(newMenuAction));
00211        /*
00212        if (index==-1)
00213               return false;
00214        int newIndex=++index;
00215        bool insSuccess=menuItemList.insert(newIndex, newMenuAction);
00216 
00217        if (!insSuccess)
00218               return false;
00219        */
00220        return repopulateLocalMenu();
00221 }
00222 
00223 bool ScrPopupMenu::removeMenuItem(ScrAction *menuAction)
00224 {
00225        if (menuItemList.remove(menuAction))
00226        {
00227               repopulateLocalMenu();
00228               return true;
00229        }
00230        return false;
00231 }
00232 /*
00233 bool ScrPopupMenu::insertMenuItemAfter(ScrAction *newMenuAction, const QString afterMenuName)
00234 {
00235        bool retVal=true;
00236        bool found=false;
00237        QPtrListIterator<QObject> menuItemListIt(menuItemList);
00238        QObject *object;
00239        while ( (object = menuItemListIt.current()) != 0  ) 
00240        {
00241               QString menuItemListClassName=object->className();
00242               int index=-1;
00243               //index=menuItemListIt.at();
00244               if (index=-1)
00245               {
00246                      retVal=false;
00247                      break;
00248               }
00249               if (menuItemListClassName=="ScrAction")
00250               {
00251                      qDebug("ScrAction found");
00252                      if(QString(object->name())==QString("separator_action"))
00253                             bool blah=true;//localPopupMenu->insertSeparator();
00254                      else
00255                      {
00256                             //Grab the action from the list, break and call the other after insert function
00257                             
00258                             int newIndex=++index;
00259                             bool insSuccess=menuItemList.insert(newIndex, newMenuAction);
00260                             if (!insSuccess)
00261                                    return false;
00262                             
00263                      }
00264               }
00265        }
00266        if (!found)
00267               return false;
00268        if (retVal==false)
00269               return false;
00270        
00271        return repopulateLocalMenu();
00272 }
00273 */
00274 
00275 //CB TODO Does NOT handle rebuilding with widgets
00276 bool ScrPopupMenu::repopulateLocalMenu()
00277 {
00278        localPopupMenu->clear();
00279        QValueList< QGuardedPtr<QObject> >::Iterator menuItemListIt = menuItemList.begin();
00280        while (menuItemListIt!=menuItemList.end())
00281        {
00282               QObject *listObj=(*menuItemListIt);
00283               if (listObj==NULL)
00284               {
00285                      QValueList< QGuardedPtr<QObject> >::Iterator menuItemListItToDelete = menuItemListIt;
00286                      ++menuItemListIt;
00287                      menuItemList.remove(*menuItemListItToDelete);
00288                      continue;
00289               }
00290               
00291               QString menuItemListClassName=listObj->className();
00292               if (menuItemListClassName=="ScrAction")
00293               {
00294                      //qDebug("ScrAction found");
00295                      if(QString(listObj->name())==QString("separator_action"))
00296                             localPopupMenu->insertSeparator();
00297                      else {
00298                             ScrAction * act = dynamic_cast<ScrAction *>(listObj);
00299                             act->addTo(localPopupMenu);
00300                      }
00301               }
00302               else 
00303               {
00304                      if (menuItemListClassName=="ScrPopupMenu")
00305                      {
00306                             ScrPopupMenu * men = dynamic_cast<ScrPopupMenu *>(listObj);
00307                             localPopupMenu->insertItem(men->getMenuIcon(), men->getMenuText(), men->getLocalPopupMenu());
00308                      }
00309                      else
00310                             sDebug(QString("Alien found: %1").arg((*menuItemListIt)->className()));
00311               }
00312               ++menuItemListIt;
00313        }
00314        return true;
00315 }
00316 
00317 bool ScrPopupMenu::generateEntryList(QStringList *actNames)
00318 {
00319        QValueList< QGuardedPtr<QObject> >::Iterator menuItemListIt = menuItemList.begin();
00320        while (menuItemListIt!=menuItemList.end())
00321        {
00322               QObject *listObj=(*menuItemListIt);
00323               if (listObj==NULL)
00324               {
00325                      QValueList< QGuardedPtr<QObject> >::Iterator menuItemListItToDelete = menuItemListIt;
00326                      ++menuItemListIt;
00327                      menuItemList.remove(*menuItemListItToDelete);
00328                      continue;
00329               }
00330               
00331               QString menuItemListClassName=listObj->className();
00332               if (menuItemListClassName=="ScrAction")
00333               {
00334                      if(QString(listObj->name())==QString("separator_action"))
00335                             ;
00336                      else
00337                      {
00338                             ScrAction *sca=dynamic_cast<ScrAction *>(listObj);
00339                             if (sca)
00340                                    actNames->append(sca->name());
00341                      }
00342               }
00343               else 
00344               if (menuItemListClassName=="ScrPopupMenu")
00345               {
00346                      ScrPopupMenu *scp=dynamic_cast<ScrPopupMenu *>(listObj);
00347                      if (scp)
00348                             scp->generateEntryList(actNames);
00349               }
00350               ++menuItemListIt;
00351        }
00352        return true;
00353        
00354 }
00355 
00356 bool ScrPopupMenu::clear()
00357 {
00358        menuItemList.clear(); //CB TODO leaking separators here ?
00359        localPopupMenu->clear();
00360        return true;
00361 }
00362 
00363 bool ScrPopupMenu::insertMenuSeparator()
00364 {
00365        ScrAction *sepAction = new ScrAction( NULL, "separator_action" );
00366        menuItemList.append(sepAction);
00367        localPopupMenu->insertSeparator();
00368        return true;
00369 }
00370 
00371 void ScrPopupMenu::setEnabled(bool menuEnabled)
00372 {
00373        enabled=menuEnabled;
00374        localPopupMenu->setEnabled(enabled);
00375 }
00376 
00377 void ScrPopupMenu::setParentMenuName(const QString& newParentMenuName)
00378 {
00379        if (!parentMenuName.isEmpty())
00380               parentMenuName=newParentMenuName;
00381 }