Salome HOME
Update copyrights 2014.
[tools/yacsgen.git] / module_generator / hxx_tmpl_gui.py
1 # Copyright (C) 2009-2014  EDF R&D
2 #
3 # This library is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU Lesser General Public
5 # License as published by the Free Software Foundation; either
6 # version 2.1 of the License, or (at your option) any later version.
7 #
8 # This library is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 # Lesser General Public License for more details.
12 #
13 # You should have received a copy of the GNU Lesser General Public
14 # License along with this library; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 #
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 #
19
20 try:
21   from string import Template
22 except:
23   from compat import Template,set
24
25 hxxgui_cxx="""
26 #include "${component_name}GUI.h"
27
28 #include <SUIT_MessageBox.h>
29 #include <SUIT_ResourceMgr.h>
30 #include <SUIT_Desktop.h>
31 #include <SUIT_Session.h>
32 #include <SalomeApp_Application.h>
33 #include <LightApp_Preferences.h>
34
35 #include <SALOME_LifeCycleCORBA.hxx>
36
37 #define COMPONENT_NAME "${component_name}"
38
39 using namespace std;
40
41 // Constructor
42 ${component_name}GUI::${component_name}GUI() :
43   SalomeApp_Module( COMPONENT_NAME ) // Module name
44 {
45   // Initializations
46   default_bool = false;
47   default_int = 0;
48   default_spinInt = 0;
49   default_spinDbl = 0.;
50   default_selection = QString("");
51   
52   // List for the selector
53   selector_strings.clear();
54   selector_strings.append( tr( "PREF_LIST_TEXT_0" ) );
55   selector_strings.append( tr( "PREF_LIST_TEXT_1" ) );
56   selector_strings.append( tr( "PREF_LIST_TEXT_2" ) );
57 }
58
59 // Gets a reference to the module's engine
60 ${component_name}_ORB::${component_name}_Gen_ptr ${component_name}GUI::Init${component_name}Gen( SalomeApp_Application* app )
61 {
62   Engines::EngineComponent_var comp = app->lcc()->FindOrLoad_Component( "FactoryServer",COMPONENT_NAME );
63   ${component_name}_ORB::${component_name}_Gen_ptr clr = ${component_name}_ORB::${component_name}_Gen::_narrow(comp);
64   ASSERT(!CORBA::is_nil(clr));
65   return clr;
66 }
67
68 // Module's initialization
69 void ${component_name}GUI::initialize( CAM_Application* app )
70 {
71   // Get handle to Application, Desktop and Resource Manager
72   SalomeApp_Module::initialize( app );
73
74   Init${component_name}Gen( dynamic_cast<SalomeApp_Application*>( app ) );
75
76   QWidget* aParent = app->desktop();
77   
78   SUIT_ResourceMgr* aResourceMgr = application()->resourceMgr();
79   
80   // GUI items
81   // --> Create actions: 190 is linked to item in "File" menu 
82   //     and 901 is linked to both specific menu and toolbar
83   createAction( 190, tr( "TLT_MY_NEW_ITEM" ), QIcon(), tr( "MEN_MY_NEW_ITEM" ), tr( "STS_MY_NEW_ITEM" ), 0, aParent, false,
84                 this, SLOT( OnMyNewItem() ) );
85
86   QPixmap aPixmap = aResourceMgr->loadPixmap( COMPONENT_NAME,tr( "ICON_${component_name}" ) );
87   createAction( 901, tr( "TLT_${component_name}_ACTION" ), QIcon( aPixmap ), tr( "MEN_${component_name}_ACTION" ), tr( "STS_${component_name}_ACTION" ), 0, aParent, false,
88                 this, SLOT( OnCallAction() ) );
89
90   // --> Create item in "File" menu
91   int aMenuId;
92   aMenuId = createMenu( tr( "MEN_FILE" ), -1, -1 );
93   createMenu( separator(), aMenuId, -1, 10 );
94   aMenuId = createMenu( tr( "MEN_FILE_${component_name}" ), aMenuId, -1, 10 );
95   createMenu( 190, aMenuId );
96
97   // --> Create specific menu
98   aMenuId = createMenu( tr( "MEN_${component_name}" ), -1, -1, 30 );
99   createMenu( 901, aMenuId, 10 );
100
101   // --> Create toolbar item
102   int aToolId = createTool ( tr( "TOOL_${component_name}" ) );
103   createTool( 901, aToolId );
104 }
105
106 // Module's engine IOR
107 QString ${component_name}GUI::engineIOR() const
108 {
109   CORBA::String_var anIOR = getApp()->orb()->object_to_string( Init${component_name}Gen( getApp() ) );
110   return QString( anIOR.in() );
111 }
112
113 // Module's activation
114 bool ${component_name}GUI::activateModule( SUIT_Study* theStudy )
115 {
116   bool bOk = SalomeApp_Module::activateModule( theStudy );
117
118   setMenuShown( true );
119   setToolShown( true );
120
121   return bOk;
122 }
123
124 // Module's deactivation
125 bool ${component_name}GUI::deactivateModule( SUIT_Study* theStudy )
126 {
127   setMenuShown( false );
128   setToolShown( false );
129
130   return SalomeApp_Module::deactivateModule( theStudy );
131 }
132
133 // Default windows
134 void ${component_name}GUI::windows( QMap<int, int>& theMap ) const
135 {
136   theMap.clear();
137   theMap.insert( SalomeApp_Application::WT_ObjectBrowser, Qt::LeftDockWidgetArea );
138   theMap.insert( SalomeApp_Application::WT_PyConsole,     Qt::BottomDockWidgetArea );
139 }
140
141 // Action slot: Launched with action 190
142 void ${component_name}GUI::OnMyNewItem()
143 {
144   SUIT_MessageBox::warning( getApp()->desktop(),tr( "INF_${component_name}_TITLE" ), tr( "INF_${component_name}_TEXT" ), tr( "BUT_OK" ) );
145 }
146
147 // Action slot: Launched with action 901
148 void ${component_name}GUI::OnCallAction()
149 {
150   // Create a ${component_name} component
151   ${component_name}_ORB::${component_name}_Gen_ptr ${component_name}gen = ${component_name}GUI::Init${component_name}Gen( getApp() );
152   
153   // Do the job...
154   //
155   // ${component_name}gen->method( arg1, arg2, ... );
156   
157   // Open a dialog showing Preferences values (just to display something)
158   
159   // ****** Direct access to preferences: implementation at 12/12/05 ******
160   // Comment out this section when "preferencesChanged" called back
161   SUIT_ResourceMgr* mgr = SUIT_Session::session()->resourceMgr();
162   
163   default_bool = mgr->booleanValue(COMPONENT_NAME, "default_bool", false);
164
165   default_int = mgr->integerValue(COMPONENT_NAME, "default_integer", 3);
166
167   default_spinInt = mgr->integerValue(COMPONENT_NAME, "default_spinint", 4);
168
169   default_spinDbl = mgr->doubleValue(COMPONENT_NAME, "default_spindbl", 4.5);
170
171   int selectorIndex = mgr->integerValue(COMPONENT_NAME, "default_selector");
172   default_selection = (0<=selectorIndex && selectorIndex<=selector_strings.count() ? selector_strings[selectorIndex]: QString("None"));
173   // ****** End of section to be commented out ******
174   
175   QString SUC = ( default_bool ? QString( tr ("INF_${component_name}_CHECK") ) : QString( tr("INF_${component_name}_UNCHECK") ) ) ;
176     
177   QString textResult = QString( tr( "RES_${component_name}_TEXT" ) ).arg(SUC).arg(default_int).arg(default_spinInt).arg(default_spinDbl).arg(default_selection);
178   SUIT_MessageBox::information( getApp()->desktop(), tr( "RES_${component_name}_TITLE" ), textResult, tr( "BUT_OK" ) );
179 }
180
181 void ${component_name}GUI::createPreferences()
182 {
183   // A sample preference dialog
184   
185   // One only tab
186   int genTab = addPreference( tr( "PREF_TAB_GENERAL" ) );
187
188   // One only group
189   int defaultsGroup = addPreference( tr( "PREF_GROUP_DEFAULTS" ), genTab );
190   
191   // A checkbox
192   addPreference( tr( "PREF_DEFAULT_BOOL" ), defaultsGroup, LightApp_Preferences::Bool, COMPONENT_NAME, "default_bool" );
193   
194   // An entry for integer
195   addPreference( tr( "PREF_DEFAULT_INTEGER" ), defaultsGroup, LightApp_Preferences::Integer, COMPONENT_NAME, "default_integer" );
196
197   // An integer changed by spinbox
198   int spinInt = addPreference( tr( "PREF_DEFAULT_SPININT" ), defaultsGroup, LightApp_Preferences::IntSpin, COMPONENT_NAME, "default_spinint" );
199   setPreferenceProperty( spinInt, "min", 0 );
200   setPreferenceProperty( spinInt, "max", 20 );
201   setPreferenceProperty( spinInt, "step", 2 );
202
203   // A Double changed by spinbox
204   int spinDbl = addPreference( tr( "PREF_DEFAULT_SPINDBL" ), defaultsGroup, LightApp_Preferences::DblSpin, COMPONENT_NAME, "default_spindbl" );
205   setPreferenceProperty( spinDbl, "min", 1 );
206   setPreferenceProperty( spinDbl, "max", 10 );
207   setPreferenceProperty( spinDbl, "step", 0.1 );
208
209   // A choice in a list
210   int options = addPreference( tr( "PREF_DEFAULT_SELECTOR" ), defaultsGroup, LightApp_Preferences::Selector, COMPONENT_NAME, "default_selector" );
211   QList<QVariant> indices;
212   indices.append( 0 );
213   indices.append( 1 );
214   indices.append( 2 );
215   setPreferenceProperty( options, "strings", selector_strings );
216   setPreferenceProperty( options, "indexes", indices );
217 }
218
219 void ${component_name}GUI::preferencesChanged( const QString& sect, const QString& name )
220 {
221 // ****** This is normal way: Not yet called back at 12/12/05 ******
222   SUIT_ResourceMgr* mgr = SUIT_Session::session()->resourceMgr();
223   if( sect==COMPONENT_NAME )
224   {
225     if( name=="default_bool" )
226         default_bool = mgr->booleanValue(COMPONENT_NAME, "default_bool", false);
227     if( name=="default_integer" )
228         default_int = mgr->integerValue(COMPONENT_NAME, "default_integer", 3);
229     if( name=="default_spinint" )
230         default_spinInt = mgr->integerValue(COMPONENT_NAME, "default_spinint", 4);
231     if( name=="default_spindbl" )
232         default_spinDbl = mgr->doubleValue(COMPONENT_NAME, "default_spindbl", 4.5);
233     if( name=="default_selector" )
234     {
235         int selectorIndex = mgr->integerValue(COMPONENT_NAME, "default_selector");
236         default_selection = (0<=selectorIndex && selectorIndex<=selector_strings.count() ? selector_strings[selectorIndex]: QString("None"));
237     }
238   }
239 }
240
241 // Export the module
242 extern "C" {
243   CAM_Module* createModule()
244   {
245     return new ${component_name}GUI();
246   }
247 }
248 """
249 hxxgui_cxx=Template(hxxgui_cxx)
250
251 hxxgui_h="""
252 #ifndef _${component_name}GUI_H_
253 #define _${component_name}GUI_H_
254
255 #include <SalomeApp_Module.h>
256
257 #include <SALOMEconfig.h>
258 #include CORBA_CLIENT_HEADER(${component_name})
259
260 class SalomeApp_Application;
261 class ${component_name}GUI: public SalomeApp_Module
262 {
263   Q_OBJECT
264
265 public:
266   ${component_name}GUI();
267
268   void    initialize( CAM_Application* );
269   QString engineIOR() const;
270   void    windows( QMap<int, int>& ) const;
271
272   static ${component_name}_ORB::${component_name}_Gen_ptr Init${component_name}Gen( SalomeApp_Application* );
273
274   virtual void                createPreferences();
275   virtual void                preferencesChanged( const QString&, const QString& );
276
277 public slots:
278   bool    deactivateModule( SUIT_Study* );
279   bool    activateModule( SUIT_Study* );
280
281 protected slots:
282   void            OnMyNewItem();
283   void            OnCallAction();
284
285 private:
286   bool default_bool;
287   int default_int;
288   int default_spinInt;
289   double default_spinDbl;
290   QString default_selection;
291   
292   QStringList selector_strings;
293   
294 };
295
296 #endif
297 """
298 hxxgui_h=Template(hxxgui_h)
299 hxxgui_icon_ts="""
300 <!DOCTYPE TS>
301 <TS version="1.1" >
302     <context>
303         <name>@default</name>
304         <message>
305             <source>ICON_${component_name}</source>
306             <translation>Exec${component_name}.png</translation>
307         </message>
308     </context>
309 </TS>
310 """
311 hxxgui_icon_ts=Template(hxxgui_icon_ts)
312 hxxgui_message_en="""
313 <!DOCTYPE TS>
314 <TS version="1.1" >
315     <context>
316         <name>@default</name>
317         <message>
318             <source>TLT_MY_NEW_ITEM</source>
319             <translation>A ${component_name} owned menu item</translation>
320         </message>
321         <message>
322             <source>MEN_MY_NEW_ITEM</source>
323             <translation>My menu</translation>
324         </message>
325         <message>
326             <source>STS_MY_NEW_ITEM</source>
327             <translation>Display a simple dialog</translation>
328         </message>
329         <message>
330             <source>TLT_${component_name}_ACTION</source>
331             <translation>Open ${component_name} dialog</translation>
332         </message>
333         <message>
334             <source>MEN_FILE</source>
335             <translation>File</translation>
336         </message>
337         <message>
338             <source>MEN_FILE_${component_name}</source>
339             <translation>${component_name} menu</translation>
340         </message>
341         <message>
342             <source>MEN_${component_name}</source>
343             <translation>${component_name}</translation>
344         </message>
345         <message>
346             <source>TOOL_${component_name}</source>
347             <translation>${component_name}</translation>
348         </message>
349     </context>
350     <context>
351         <name>${component_name}GUI</name>
352         <message>
353             <source>BUT_OK</source>
354             <translation>OK</translation>
355         </message>
356         <message>
357             <source>BUT_CANCEL</source>
358             <translation>Cancel</translation>
359         </message>
360         <message>
361             <source>INF_${component_name}_TITLE</source>
362             <translation>${component_name} Information</translation>
363         </message>
364         <message>
365             <source>INF_${component_name}_TEXT</source>
366             <translation>This is just a test</translation>
367         </message>
368         <message>
369             <source>INF_${component_name}_CHECK</source>
370             <translation>checked</translation>
371         </message>
372         <message>
373             <source>INF_${component_name}_UNCHECK</source>
374             <translation>Unchecked</translation>
375         </message>
376         <message>
377             <source>RES_${component_name}_TITLE</source>
378             <translation>Sample ${component_name} dialog</translation>
379         </message>
380         <message>
381             <source>RES_${component_name}_TEXT</source>
382             <translation>Preferences are: \n\tCheckbox: %1\n\tInteger: %2\n\tInteger2: %3\n\tDouble: %4\n\tText: %5</translation>
383         </message>
384         <message>
385             <source>PREF_TAB_GENERAL</source>
386             <translation>General</translation>
387         </message>
388         <message>
389             <source>PREF_GROUP_DEFAULTS</source>
390             <translation>Default Values</translation>
391         </message>
392         <message>
393             <source>PREF_DEFAULT_BOOL</source>
394             <translation>Check me</translation>
395         </message>
396         <message>
397             <source>PREF_DEFAULT_INTEGER</source>
398             <translation>Enter an integer :</translation>
399         </message>
400         <message>
401             <source>PREF_DEFAULT_SPININT</source>
402             <translation>Click arrows (integer) :</translation>
403         </message>
404         <message>
405             <source>PREF_DEFAULT_SPINDBL</source>
406             <translation>Click arrows (double)</translation>
407         </message>
408         <message>
409             <source>PREF_DEFAULT_SELECTOR</source>
410             <translation>Select an option</translation>
411         </message>
412         <message>
413             <source>PREF_LIST_TEXT_0</source>
414             <translation>first option</translation>
415         </message>
416         <message>
417             <source>PREF_LIST_TEXT_1</source>
418             <translation>second option</translation>
419         </message>
420         <message>
421             <source>PREF_LIST_TEXT_2</source>
422             <translation>third option</translation>
423         </message>
424     </context>
425 </TS>
426 """
427 hxxgui_message_en=Template(hxxgui_message_en)
428 hxxgui_message_fr="""
429 <!DOCTYPE TS>
430 <TS version="1.1" >
431     <context>
432         <name>@default</name>
433         <message>
434             <source>TLT_MY_NEW_ITEM</source>
435             <translation>Un article de menu propre a ${component_name}</translation>
436         </message>
437         <message>
438             <source>MEN_MY_NEW_ITEM</source>
439             <translation>Mon menu</translation>
440         </message>
441         <message>
442             <source>STS_MY_NEW_ITEM</source>
443             <translation>Affiche une boite de dialogue simple</translation>
444         </message>
445         <message>
446             <source>TLT_${component_name}_ACTION</source>
447             <translation>Ouvre la boite de dialogue de ${component_name}</translation>
448         </message>
449         <message>
450             <source>MEN_FILE</source>
451             <translation>File</translation>
452         </message>
453         <message>
454             <source>MEN_FILE_${component_name}</source>
455             <translation>Menu de ${component_name}</translation>
456         </message>
457         <message>
458             <source>MEN_${component_name}</source>
459             <translation>${component_name}</translation>
460         </message>
461         <message>
462             <source>TOOL_${component_name}</source>
463             <translation>${component_name}</translation>
464         </message>
465     </context>
466     <context>
467         <name>${component_name}GUI</name>
468         <message>
469             <source>BUT_OK</source>
470             <translation>OK</translation>
471         </message>
472         <message>
473             <source>BUT_CANCEL</source>
474             <translation>Annuler</translation>
475         </message>
476         <message>
477             <source>INF_${component_name}_TITLE</source>
478             <translation>Information ${component_name}</translation>
479         </message>
480         <message>
481             <source>INF_${component_name}_TEXT</source>
482             <translation>Ceci est un simple test</translation>
483         </message>
484         <message>
485             <source>INF_${component_name}_CHECK</source>
486             <translation>coche</translation>
487         </message>
488         <message>
489             <source>INF_${component_name}_UNCHECK</source>
490             <translation>decoche</translation>
491         </message>
492         <message>
493             <source>RES_${component_name}_TITLE</source>
494             <translation>Dialogue example de ${component_name}</translation>
495         </message>
496         <message>
497             <source>RES_${component_name}_TEXT</source>
498             <translation>Les preferences sont : \n\tCase a cocher : %1\n\tEntier : %2\n\tEntier2 : %3\n\tDouble : %4\n\tTexte : %5</translation>
499         </message>
500         <message>
501             <source>PREF_TAB_GENERAL</source>
502             <translation>General</translation>
503         </message>
504         <message>
505             <source>PREF_GROUP_DEFAULTS</source>
506             <translation>valeur par defaut</translation>
507         </message>
508         <message>
509             <source>PREF_DEFAULT_BOOL</source>
510             <translation>Cochez-moi</translation>
511         </message>
512         <message>
513             <source>PREF_DEFAULT_INTEGER</source>
514             <translation>Entrez un entier :</translation>
515         </message>
516         <message>
517             <source>PREF_DEFAULT_SPININT</source>
518             <translation>cliquez sur les fleches (entier)</translation>
519         </message>
520         <message>
521             <source>PREF_DEFAULT_SPINDBL</source>
522             <translation>cliquez sur les fleches (double)</translation>
523         </message>
524         <message>
525             <source>PREF_DEFAULT_SELECTOR</source>
526             <translation>Choisissez une option</translation>
527         </message>
528         <message>
529             <source>PREF_LIST_TEXT_0</source>
530             <translation>premiere option</translation>
531         </message>
532         <message>
533             <source>PREF_LIST_TEXT_1</source>
534             <translation>deuxieme option</translation>
535         </message>
536         <message>
537             <source>PREF_LIST_TEXT_2</source>
538             <translation>troisieme option</translation>
539         </message>
540     </context>
541 </TS>
542 """
543 hxxgui_message_fr=Template(hxxgui_message_fr)
544 hxxgui_config="""
545 language=en
546 """
547 hxxgui_config=Template(hxxgui_config)
548 hxxgui_xml_en="""
549 <?xml version='1.0' encoding='us-ascii'?>
550 <!DOCTYPE application PUBLIC "" "desktop.dtd">
551 <application title="${component_name} component" date="9/12/2001" author="C Caremoli" appId="${component_name}" >
552 <desktop>
553 <!-- ### MENUBAR ###  -->
554 <menubar>
555
556  <menu-item label-id="File" item-id="1" pos-id="">
557   <submenu label-id="Hello" item-id="19" pos-id="9">
558    <popup-item item-id="190" pos-id="" label-id="MyNewItem" icon-id="" tooltip-id="" accel-id="" toggle-id="" execute-action=""/>
559   </submenu>
560   <endsubmenu />
561  </menu-item>
562
563  <menu-item label-id="${component_name}" item-id="90" pos-id="3">
564   <popup-item item-id="901" label-id="Get banner" icon-id="" tooltip-id="Get ${component_name} banner" accel-id="" toggle-id="" execute-action=""/>
565
566  </menu-item>
567 </menubar>
568 <!-- ### TOOLBAR ###  -->
569 <toolbar label-id="${component_name}">
570  <toolbutton-item item-id="901" label-id="Get banner" icon-id="Exec${component_name}.png" tooltip-id="Get ${component_name} banner" accel-id="" toggle-id="" execute-action=""/>
571 </toolbar>
572 </desktop>
573 </application>
574 """
575 hxxgui_xml_en=Template(hxxgui_xml_en)
576 hxxgui_xml_fr="""
577 <?xml version='1.0' encoding='us-ascii'?>
578 <!DOCTYPE application PUBLIC "" "desktop.dtd">
579 <application title="${component_name} component" date="9/12/2001" author="C Caremoli" appId="${component_name}" >
580 <desktop>
581 <!-- ### MENUBAR ###  -->
582 <menubar>
583  <menu-item label-id="File" item-id="1" pos-id="">
584   <submenu label-id="Hello" item-id="19" pos-id="9">
585    <popup-item item-id="190" pos-id="" label-id="MyNewItem" icon-id="" tooltip-id="" accel-id="" toggle-id="" execute-action=""/>
586   </submenu>
587   <endsubmenu />
588  </menu-item>
589  <menu-item label-id="${component_name}" item-id="90" pos-id="3">
590   <popup-item item-id="941" label-id="Lancer IHM" icon-id="" tooltip-id="Lancer IHM ${component_name}" accel-id="" toggle-id="" execute-action=""/>
591  </menu-item>
592 </menubar>
593 <!-- ### TOOLBAR ###  -->
594 <toolbar label-id="${component_name}">
595  <toolbutton-item item-id="941" label-id="Lancer IHM" icon-id="Exec${component_name}.png" tooltip-id="Lancer IHM ${component_name}" accel-id="" toggle-id="" execute-action=""/>
596 </toolbar>
597 </desktop>
598 </application>
599 """
600 hxxgui_xml_fr=Template(hxxgui_xml_fr)
601