2 // File : QAD_Popup.cxx
4 // Descr : Popup menu management
9 // Copyright : Open CASCADE
13 #include "QAD_Popup.h"
14 #include "utilities.h"
16 /*****************************************************************************
17 ** Class QAD_PopupClientServer
18 *****************************************************************************/
23 QAD_PopupServer::QAD_PopupServer() :
30 Enables/disables creation of popup.
31 Useful when user doesn't want to create popup
32 provided by server by default and does not
33 want to inherit from the server
35 void QAD_PopupServer::enablePopup( bool enable )
37 myEnablePopup = enable;
41 Called by popup client when it wants
44 QPopupMenu* QAD_PopupServer::createPopup()
50 myPopup = new QPopupMenu;
51 onCreatePopup(); /* add specific menu items */
56 Called by popup client when popup
59 void QAD_PopupServer::destroyPopup()
61 if ( myPopup ) myPopup->clear();
67 QAD_PopupServer::~QAD_PopupServer()
69 delete (QPopupMenu*) myPopup;
72 /*****************************************************************************
73 ** Class QAD_PopupClient
74 *****************************************************************************/
79 QAD_PopupClient::QAD_PopupClient() :
87 QAD_PopupClient::~QAD_PopupClient()
92 Sets a popup server for this client
94 void QAD_PopupClient::setPopupServer ( QAD_PopupServer* server)
96 myPopupServer = server;
100 Returns the popup server for this client
102 QAD_PopupServer* QAD_PopupClient::getPopupServer () const
104 return myPopupServer;
107 /*****************************************************************************
108 ** Class QAD_PopupClientServer
109 *****************************************************************************/
114 QAD_PopupClientServer::QAD_PopupClientServer( bool separateItems, bool smartSeparator ) :
115 myOnlyServer( false ),
116 mySeparateItems( separateItems ),
117 mySmartSeparator( smartSeparator )
124 QAD_PopupClientServer::~QAD_PopupClientServer()
129 Called by popup client when it wants
130 to activate popup. The result is the
131 popup returned by this object's server
132 ( client role ) + the appended popup
133 provided by itself ( server role ).
135 QPopupMenu* QAD_PopupClientServer::createPopup()
137 if ( !myEnablePopup )
140 QPopupMenu* popupMenu = 0;
142 { /* get the popup provided by my server */
143 popupMenu = myPopupServer->createPopup();
147 { /* there is no popup from my server */
148 if ( !myPopup ) myPopup = new QPopupMenu;
154 if ( myPopup ) myPopup->clear();
156 myOnlyServer = false;
159 /* Attach my popup to the popup of my server */
160 if ( popupMenu->count() && mySeparateItems )
162 /* Separate my items only if I will really
165 int sepId, numBefore, numAfter;
166 sepId = mySmartSeparator ? popupMenu->insertSeparator(0) : popupMenu->insertSeparator();
167 numBefore = popupMenu->count();
168 int sepPosBefore = mySmartSeparator ? 0 : numBefore - 1;
173 numAfter = popupMenu->count();
174 int sepPosAfter = popupMenu->indexOf(sepId);
175 if ( numAfter > numBefore || ( sepPosAfter != -1 && sepPosAfter && sepPosAfter != numAfter - 1 ) ) {
176 myIDs.append( sepId );
178 else if ( sepPosAfter != -1 )
179 popupMenu->removeItem( sepId );
182 onCreatePopup(); /* add items */
188 Deactivates the popup
190 void QAD_PopupClientServer::destroyPopup()
194 /* remove all my items */
195 QValueList<int>::ConstIterator it;
196 for( it = myIDs.begin(); it != myIDs.end(); ++it )
197 myPopup->removeItem ( *it );
199 /* clear list of IDs */
203 { /* popup must be empty now */
204 QAD_ASSERT_DEBUG_ONLY ( !myPopup->count() );
210 /* dispatch to my server */
212 myPopupServer->destroyPopup();