Salome HOME
NRI : Merge from V1_2.
[modules/kernel.git] / src / SALOMEGUI / QAD_Popup.cxx
1 //  SALOME SALOMEGUI : implementation of desktop and GUI kernel
2 //
3 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
5 // 
6 //  This library is free software; you can redistribute it and/or 
7 //  modify it under the terms of the GNU Lesser General Public 
8 //  License as published by the Free Software Foundation; either 
9 //  version 2.1 of the License. 
10 // 
11 //  This library is distributed in the hope that it will be useful, 
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of 
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
14 //  Lesser General Public License for more details. 
15 // 
16 //  You should have received a copy of the GNU Lesser General Public 
17 //  License along with this library; if not, write to the Free Software 
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
19 // 
20 //  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : QAD_Popup.cxx
25 //  Author : UI team
26 //  Module : SALOME
27 //  $Header$
28
29 using namespace std;
30 #include "QAD.h"
31 #include "QAD_Popup.h"
32 #include "utilities.h"
33
34 /*****************************************************************************
35 **  Class QAD_PopupClientServer
36 *****************************************************************************/
37
38 /*!
39     Constructor
40 */
41 QAD_PopupServer::QAD_PopupServer() :
42 myPopup( NULL ),
43 myEnablePopup( true )
44 {
45 }
46
47 /*!
48     Enables/disables creation of popup.
49     Useful when user doesn't want to create popup
50     provided by server by default and does not 
51     want to inherit from the server
52 */
53 void QAD_PopupServer::enablePopup( bool enable )
54 {
55     myEnablePopup = enable;
56 }
57
58 /*!
59     Called by popup client when it wants
60     to activate popup
61 */
62 QPopupMenu* QAD_PopupServer::createPopup()
63 {
64   if ( !myEnablePopup )
65     return 0;
66   
67   if ( !myPopup ) 
68     myPopup = new QPopupMenu;
69   onCreatePopup();      /* add specific menu items */
70   return myPopup;       
71 }
72
73 /*!
74     Called by popup client when popup
75     is deactivated.
76 */
77 void QAD_PopupServer::destroyPopup()
78 {
79   if ( myPopup ) myPopup->clear();
80 }
81
82 /*!
83     Destructor
84 */
85 QAD_PopupServer::~QAD_PopupServer()
86 {
87   delete (QPopupMenu*) myPopup;
88 }
89
90 /*****************************************************************************
91 **  Class QAD_PopupClient
92 *****************************************************************************/
93
94 /*!
95     Constructor
96 */
97 QAD_PopupClient::QAD_PopupClient() :
98 myPopupServer( 0 )
99 {
100 }
101
102 /*!
103     Destructor
104 */
105 QAD_PopupClient::~QAD_PopupClient() 
106 {
107 }
108
109 /*!
110     Sets a popup server for this client
111 */
112 void QAD_PopupClient::setPopupServer ( QAD_PopupServer* server)
113 {
114   myPopupServer = server;
115 }
116
117 /*!
118     Returns the popup server for this client
119 */
120 QAD_PopupServer* QAD_PopupClient::getPopupServer () const
121 {
122   return myPopupServer;
123 }
124
125 /*****************************************************************************
126 **  Class QAD_PopupClientServer
127 *****************************************************************************/
128
129 /*!
130     Constructor
131 */
132 QAD_PopupClientServer::QAD_PopupClientServer( bool separateItems, bool smartSeparator ) :
133   myOnlyServer( false ),
134   mySeparateItems( separateItems ),
135   mySmartSeparator( smartSeparator )
136 {
137 }
138
139 /*!
140     Destructor
141 */
142 QAD_PopupClientServer::~QAD_PopupClientServer()
143 {
144 }
145
146 /*!
147     Called by popup client when it wants
148     to activate popup. The result is the
149     popup returned by this object's server
150     ( client role ) + the appended popup 
151     provided by itself ( server role ).
152 */
153 QPopupMenu*     QAD_PopupClientServer::createPopup()
154 {
155   if ( !myEnablePopup )
156     return 0;
157   
158   QPopupMenu* popupMenu = 0;
159   if ( myPopupServer )
160     {   /* get the popup provided by my server */
161       popupMenu = myPopupServer->createPopup();
162     }
163   
164   if ( !popupMenu ) 
165     {   /* there is no popup from my server */
166       if ( !myPopup ) myPopup = new QPopupMenu;
167       popupMenu = myPopup;
168       myOnlyServer = true;
169     }
170   else 
171     {
172       if ( myPopup ) myPopup->clear();  
173       myPopup = popupMenu;                              
174       myOnlyServer = false;
175     }
176   
177   /* Attach my popup to the popup of my server */ 
178   if ( popupMenu->count() && mySeparateItems )
179     {   
180       /* Separate my items only if I will really 
181          add some items 
182       */
183       int sepId, numBefore, numAfter;
184       sepId = mySmartSeparator ? popupMenu->insertSeparator(0) : popupMenu->insertSeparator();
185       numBefore = popupMenu->count();
186       int sepPosBefore = mySmartSeparator ? 0 : numBefore - 1;
187
188       /* add items */
189       onCreatePopup();    
190         
191       numAfter = popupMenu->count();
192       int sepPosAfter = popupMenu->indexOf(sepId);
193       if ( numAfter > numBefore || ( sepPosAfter != -1 && sepPosAfter && sepPosAfter != numAfter - 1 ) ) { 
194         myIDs.append( sepId );
195       }
196       else if ( sepPosAfter != -1 )
197         popupMenu->removeItem( sepId );
198     }
199   else
200     onCreatePopup();    /* add items */
201   
202   return popupMenu;                                             
203 }
204
205 /*!
206     Deactivates the popup
207 */
208 void QAD_PopupClientServer::destroyPopup()
209 {
210   if ( myPopup ) 
211     {   
212       /* remove all my items */
213       QValueList<int>::ConstIterator it;
214       for( it = myIDs.begin(); it != myIDs.end(); ++it )            
215         myPopup->removeItem ( *it );    
216       
217       /* clear list of IDs */
218       myIDs.clear();
219
220       if ( myOnlyServer )                                       
221         {       /* popup must be empty now */            
222           QAD_ASSERT_DEBUG_ONLY ( !myPopup->count() );
223           return;
224         }
225       myPopup = 0;      
226     }
227   
228   /* dispatch to my server */
229   if ( myPopupServer )
230     myPopupServer->destroyPopup();
231 }