CMS 3D CMS Logo

IgQtAppMenuService.cc

Go to the documentation of this file.
00001 //<<<<<< INCLUDES                                                       >>>>>>
00002 
00003 #include "Iguana/Studio/interface/IgQtAppMenuService.h"
00004 #include "Iguana/Studio/interface/IgQtAppContextService.h"
00005 #include <classlib/utils/DebugAids.h>
00006 #include <classlib/utils/Callback.h>
00007 
00008 #define INCLUDE_MENUITEM_DEF
00009 #include <qmainwindow.h>
00010 #include <qmenubar.h>
00011 #include <qmenudata.h>
00012 #include <qpopupmenu.h>
00013 #include <qapplication.h>
00014 #include <qnamespace.h>
00015 #include <qobject.h>
00016 #include <string>
00017 
00018 //<<<<<< PRIVATE DEFINES                                                >>>>>>
00019 //<<<<<< PRIVATE CONSTANTS                                              >>>>>>
00020 //<<<<<< PRIVATE TYPES                                                  >>>>>>
00021 //<<<<<< PRIVATE VARIABLE DEFINITIONS                                   >>>>>>
00022 //<<<<<< PUBLIC VARIABLE DEFINITIONS                                    >>>>>>
00023 //<<<<<< CLASS STRUCTURE INITIALIZATION                                 >>>>>>
00024 
00025 IG_DEFINE_STATE_ELEMENT (IgQtAppMenuService, "Services/Qt/App Menu");
00026 
00027 //<<<<<< PRIVATE FUNCTION DEFINITIONS                                   >>>>>>
00028 //<<<<<< PUBLIC FUNCTION DEFINITIONS                                    >>>>>>
00029 //<<<<<< MEMBER FUNCTION DEFINITIONS                                    >>>>>>
00030 
00031 IgQtAppMenuService::IgQtAppMenuService (IgState *state, QWidget *mainWindow)
00032     : m_state (state),
00033       m_mainWindow (dynamic_cast<QMainWindow *> (mainWindow))
00034 {
00035     ASSERT (state);
00036     ASSERT (mainWindow);
00037     ASSERT (m_mainWindow);
00038     state->put (s_key, this);
00039 
00040     QMenuBar    *menubar = m_mainWindow->menuBar ();
00041     QPopupMenu  *filemenu = new QPopupMenu (m_mainWindow);
00042 
00043     menubar->insertItem ("&File", filemenu, MENU_ID_FILE,
00044                          index (menubar, MENU_ID_FILE));
00045     filemenu->setCaption ("File");
00046     filemenu->insertTearOffHandle ();
00047     filemenu->insertItem ("&Quit", qApp, SLOT(quit()),
00048                           Qt::CTRL+Qt::Key_Q, MENU_ID_FILE_QUIT,
00049                           index (filemenu, MENU_ID_FILE_QUIT));
00050     menubar->show ();
00051 }
00052 
00053 IgQtAppMenuService::~IgQtAppMenuService (void)
00054 {
00055     ASSERT (m_state);
00056     ASSERT (m_mainWindow);
00057 
00058     m_state->detach (s_key);
00059     m_mainWindow->menuBar ()->clear ();
00060 }
00061 
00062 QMenuBar *
00063 IgQtAppMenuService::menuBar (void)
00064 {
00065     return m_mainWindow->menuBar ();
00066 }
00067 
00068 QPopupMenu *
00069 IgQtAppMenuService::subMenu (int id)
00070 {
00071     QMenuItem *item = menuBar ()->findItem (id);
00072     if (item)
00073         return item->popup ();
00074     return 0;
00075 }
00076 
00077 QPopupMenu *
00078 IgQtAppMenuService::subMenu (int id1, int id2)
00079 {
00080     QPopupMenu *item1 = subMenu (id1);
00081     if (item1)
00082     {
00083         QMenuItem *item2 = item1->findItem (id2);
00084         if (item2)
00085         {
00086             return item2->popup ();         
00087         }
00088     }
00089 
00090     return 0;
00091 }
00092 
00093 QPopupMenu *
00094 IgQtAppMenuService::findByName (QMenuData *menu, const char *itemName)
00095 {
00096     ASSERT (menu);
00097     
00098     for (unsigned int i = 0; i < menu->count (); i++)
00099     {
00100         QMenuItem *item = menu->findItem (menu->idAt (i));
00101         
00102         if (item->text () == itemName)
00103             return item->popup ();          
00104     }
00105     return 0;    
00106 }
00107 
00108 int
00109 IgQtAppMenuService::index (QMenuData *menu, int id)
00110 {
00111     ASSERT (menu);
00112     
00113     for (int i = 0; i < (int) menu->count (); ++i)
00114         if (menu->idAt (i) >= id)
00115             return i;
00116 
00117     return menu->count ();
00118 }
00119 
00120 int
00121 IgQtAppMenuService::isIdThere (QMenuData *menu, int id)
00122 {
00123     ASSERT (menu);
00124     
00125     for (int i = 0; i < (int) menu->count (); ++i)
00126         if (menu->idAt (i) == id)
00127             return i;
00128     return -1;    
00129 }
00130 
00131 typedef struct 
00132 {
00133     QMenuData   *m_menu;
00134     int         m_id;
00135     QObject     *m_target;
00136     std::string m_slot;    
00137 } MenuConnection;
00138 
00139 void 
00140 connectMenuItem (MenuConnection * data)
00141 {
00142     QMenuData *menu = data->m_menu;
00143     int id = data->m_id;
00144     QObject *target = data->m_target;
00145     const char *slot = data->m_slot.c_str ();    
00146     menu->connectItem (id, target, slot);    
00147     //std::cerr << "Connected:" << target << std::endl;
00148 }
00149 
00150 void 
00151 disconnectMenuItem (MenuConnection *data)
00152 {
00153     QMenuData *menu = data->m_menu;
00154     int id = data->m_id;
00155     QObject *target = data->m_target;
00156     const char * slot = data->m_slot.c_str ();    
00157     menu->disconnectItem (id, target, slot);
00158     //std::cerr << "Disconnected:" << target << std::endl;
00159 }
00160 
00161 // FIXME: add accelerators support.
00162 void
00163 IgQtAppMenuService::insertItem (QMenuData *parentMenu,
00164                                 const QString &text,
00165                                 QObject *target,
00166                                 const char *slot,
00167                                 int id,
00168                                 IgState *state)
00169 {
00170     if (isIdThere (parentMenu, id) == -1)
00171     {
00172         parentMenu->insertItem (text,
00173                                 target,
00174                                 slot,
00175                                 0,
00176                                 id, 
00177                                 IgQtAppMenuService::index (parentMenu, id));    
00178     }
00179     
00180     if (state)
00181     {
00182         
00183         // if a state is passed, than the connection between the
00184         // target and the menuitem is context sensitive.
00185         IgQtAppContextService *cs = IgQtAppContextService::get (state);
00186         MenuConnection *data = new MenuConnection;
00187         ASSERT (cs);
00188         ASSERT (data);
00189         
00190         data->m_menu = parentMenu;
00191         data->m_id = id;
00192         data->m_target = target;
00193         data->m_slot = slot ? slot : "";
00194         
00195         cs->addOnFocusIn (lat::CreateCallback (&connectMenuItem,
00196                                                data));  
00197 
00198         cs->addOnFocusOut (lat::CreateCallback (&disconnectMenuItem,
00199                                                 data));
00200         disconnectMenuItem (data);
00201     }
00202 }
00203 

Generated on Tue Jun 9 17:38:49 2009 for CMSSW by  doxygen 1.5.4