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