Salome HOME
This commit was generated by cvs2git to track changes on a CVS vendor
[modules/kernel.git] / src / SALOMEGUI / QAD_Popup.cxx
1 using namespace std;
2 //  File      : QAD_Popup.cxx
3 //  Created   : 22.09.00
4 //  Descr     : Popup menu management
5
6 //  Author    : UI team
7 //  Project   : SALOME
8 //  Module    : SALOMEGUI 
9 //  Copyright : Open CASCADE
10 //  $Header$
11
12 #include "QAD.h"
13 #include "QAD_Popup.h"
14 #include "utilities.h"
15
16 /*****************************************************************************
17 **  Class QAD_PopupClientServer
18 *****************************************************************************/
19
20 /*!
21     Constructor
22 */
23 QAD_PopupServer::QAD_PopupServer() :
24 myPopup( NULL ),
25 myEnablePopup( true )
26 {
27 }
28
29 /*!
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
34 */
35 void QAD_PopupServer::enablePopup( bool enable )
36 {
37     myEnablePopup = enable;
38 }
39
40 /*!
41     Called by popup client when it wants
42     to activate popup
43 */
44 QPopupMenu* QAD_PopupServer::createPopup()
45 {
46   if ( !myEnablePopup )
47     return 0;
48   
49   if ( !myPopup ) 
50     myPopup = new QPopupMenu;
51   onCreatePopup();      /* add specific menu items */
52   return myPopup;       
53 }
54
55 /*!
56     Called by popup client when popup
57     is deactivated.
58 */
59 void QAD_PopupServer::destroyPopup()
60 {
61   if ( myPopup ) myPopup->clear();
62 }
63
64 /*!
65     Destructor
66 */
67 QAD_PopupServer::~QAD_PopupServer()
68 {
69   delete (QPopupMenu*) myPopup;
70 }
71
72 /*****************************************************************************
73 **  Class QAD_PopupClient
74 *****************************************************************************/
75
76 /*!
77     Constructor
78 */
79 QAD_PopupClient::QAD_PopupClient() :
80 myPopupServer( 0 )
81 {
82 }
83
84 /*!
85     Destructor
86 */
87 QAD_PopupClient::~QAD_PopupClient() 
88 {
89 }
90
91 /*!
92     Sets a popup server for this client
93 */
94 void QAD_PopupClient::setPopupServer ( QAD_PopupServer* server)
95 {
96   myPopupServer = server;
97 }
98
99 /*!
100     Returns the popup server for this client
101 */
102 QAD_PopupServer* QAD_PopupClient::getPopupServer () const
103 {
104   return myPopupServer;
105 }
106
107 /*****************************************************************************
108 **  Class QAD_PopupClientServer
109 *****************************************************************************/
110
111 /*!
112     Constructor
113 */
114 QAD_PopupClientServer::QAD_PopupClientServer( bool separateItems, bool smartSeparator ) :
115   myOnlyServer( false ),
116   mySeparateItems( separateItems ),
117   mySmartSeparator( smartSeparator )
118 {
119 }
120
121 /*!
122     Destructor
123 */
124 QAD_PopupClientServer::~QAD_PopupClientServer()
125 {
126 }
127
128 /*!
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 ).
134 */
135 QPopupMenu*     QAD_PopupClientServer::createPopup()
136 {
137   if ( !myEnablePopup )
138     return 0;
139   
140   QPopupMenu* popupMenu = 0;
141   if ( myPopupServer )
142     {   /* get the popup provided by my server */
143       popupMenu = myPopupServer->createPopup();
144     }
145   
146   if ( !popupMenu ) 
147     {   /* there is no popup from my server */
148       if ( !myPopup ) myPopup = new QPopupMenu;
149       popupMenu = myPopup;
150       myOnlyServer = true;
151     }
152   else 
153     {
154       if ( myPopup ) myPopup->clear();  
155       myPopup = popupMenu;                              
156       myOnlyServer = false;
157     }
158   
159   /* Attach my popup to the popup of my server */ 
160   if ( popupMenu->count() && mySeparateItems )
161     {   
162       /* Separate my items only if I will really 
163          add some items 
164       */
165       int sepId, numBefore, numAfter;
166       sepId = mySmartSeparator ? popupMenu->insertSeparator(0) : popupMenu->insertSeparator();
167       numBefore = popupMenu->count();
168       int sepPosBefore = mySmartSeparator ? 0 : numBefore - 1;
169
170       /* add items */
171       onCreatePopup();    
172         
173       numAfter = popupMenu->count();
174       int sepPosAfter = popupMenu->indexOf(sepId);
175       if ( numAfter > numBefore || ( sepPosAfter != -1 && sepPosAfter && sepPosAfter != numAfter - 1 ) ) { 
176         myIDs.append( sepId );
177       }
178       else if ( sepPosAfter != -1 )
179         popupMenu->removeItem( sepId );
180     }
181   else
182     onCreatePopup();    /* add items */
183   
184   return popupMenu;                                             
185 }
186
187 /*!
188     Deactivates the popup
189 */
190 void QAD_PopupClientServer::destroyPopup()
191 {
192   if ( myPopup ) 
193     {   
194       /* remove all my items */
195       QValueList<int>::ConstIterator it;
196       for( it = myIDs.begin(); it != myIDs.end(); ++it )            
197         myPopup->removeItem ( *it );    
198       
199       /* clear list of IDs */
200       myIDs.clear();
201
202       if ( myOnlyServer )                                       
203         {       /* popup must be empty now */            
204           QAD_ASSERT_DEBUG_ONLY ( !myPopup->count() );
205           return;
206         }
207       myPopup = 0;      
208     }
209   
210   /* dispatch to my server */
211   if ( myPopupServer )
212     myPopupServer->destroyPopup();
213 }