Salome HOME
Update copyrights 2014.
[tools/yacsgen.git] / Examples / cppgui1 / cppcomposGUI.cxx
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 #include "cppcomposGUI.h"
21 #include <SUIT_MessageBox.h>
22 #include <SUIT_ResourceMgr.h>
23 #include <SUIT_Desktop.h>
24 #include <SUIT_Study.h>
25 #include <SalomeApp_Application.h>
26 #include <SALOME_LifeCycleCORBA.hxx>
27
28 #include <SALOMEconfig.h>
29 #include CORBA_CLIENT_HEADER(cppcompos)
30 #include CORBA_CLIENT_HEADER(SALOMEDS)
31 #include CORBA_CLIENT_HEADER(SALOMEDS_Attributes)
32
33 // QT Includes
34 #include <QInputDialog>
35 #include <QIcon>
36
37 // Export the module
38 extern "C" {
39   CAM_Module* createModule()
40   {
41     return new cppcomposGUI();
42   }
43 }
44
45 // Constructor
46 cppcomposGUI::cppcomposGUI() :
47   SalomeApp_Module( "cppcompos" ) // default name
48 {
49 }
50
51 static cppcompos_ORB::cppcompos_var engine;
52
53 // Module's initialization
54 void cppcomposGUI::initialize( CAM_Application* app )
55 {
56
57   SalomeApp_Module::initialize( app );
58
59   Engines::EngineComponent_var comp = dynamic_cast<SalomeApp_Application*>(app)->lcc()->FindOrLoad_Component( "FactoryServer","cppcompos" );
60   engine = cppcompos_ORB::cppcompos::_narrow(comp);
61
62   QWidget* aParent = application()->desktop();
63   SUIT_ResourceMgr* aResourceMgr = app->resourceMgr();
64
65   // create actions
66   QPixmap aPixmap = aResourceMgr->loadPixmap( "cppcompos","exec.png" );
67   createAction( 901, "Banner", QIcon( aPixmap ), "Banner", "Banner", 0, aParent, false, this, SLOT( OnGetBanner() ) );
68   createAction( 902, "Designer", QIcon( aPixmap ), "Designer", "Designer", 0, aParent, false, this, SLOT( OnDesigner() ) );
69
70   // create menus
71   int aMenuId;
72   aMenuId = createMenu( "cppcompos", -1, -1, 30 );
73   createMenu( 901, aMenuId, 10 );
74
75   // create toolbars
76   int aToolId = createTool ( "cppcompos" );
77   createTool( 901, aToolId );
78   createTool( 902, aToolId );
79 }
80
81 // Get compatible dockable windows.
82 void cppcomposGUI::windows( QMap<int, int>& theMap ) const
83 {
84   theMap.clear();
85   theMap.insert( SalomeApp_Application::WT_ObjectBrowser, Qt::LeftDockWidgetArea );
86   theMap.insert( SalomeApp_Application::WT_PyConsole,     Qt::BottomDockWidgetArea );
87 }
88
89 // Module's engine IOR
90 QString cppcomposGUI::engineIOR() const
91 {
92   return "bidon";
93 }
94
95 // Module's activation
96 bool cppcomposGUI::activateModule( SUIT_Study* theStudy )
97 {
98   bool bOk = SalomeApp_Module::activateModule( theStudy );
99
100   setMenuShown( true );
101   setToolShown( true );
102
103   SALOME_NamingService *aNamingService = SalomeApp_Application::namingService();
104   CORBA::Object_var aSMObject = aNamingService->Resolve("/myStudyManager");
105   SALOMEDS::StudyManager_var aStudyManager = SALOMEDS::StudyManager::_narrow(aSMObject);
106   SALOMEDS::Study_var aDSStudy = aStudyManager->GetStudyByID(theStudy->id());
107
108   SALOMEDS::SComponent_var aFather = aDSStudy->FindComponent("cppcompos");
109   if (aFather->_is_nil())
110     {
111       SALOMEDS::StudyBuilder_var aStudyBuilder = aDSStudy->NewBuilder();
112       aFather = aStudyBuilder->NewComponent("cppcompos");
113       SALOMEDS::GenericAttribute_var anAttr = aStudyBuilder->FindOrCreateAttribute(aFather, "AttributeName");
114       SALOMEDS::AttributeName_var aName = SALOMEDS::AttributeName::_narrow(anAttr);
115       aName->SetValue("cppcompos");
116       aName->UnRegister();
117       aStudyBuilder->DefineComponentInstance(aFather, engine);
118     }
119   CORBA::Boolean valid;
120   engine->DumpPython(aDSStudy,1,0,valid);
121
122   return bOk;
123 }
124
125 // Module's deactivation
126 bool cppcomposGUI::deactivateModule( SUIT_Study* theStudy )
127 {
128   setMenuShown( false );
129   setToolShown( false );
130
131   return SalomeApp_Module::deactivateModule( theStudy );
132 }
133
134 // Action slot
135 void cppcomposGUI::OnGetBanner()
136 {
137   // Dialog to get the Name
138   bool ok = FALSE;
139   QString myName = QInputDialog::getText( getApp()->desktop(), "label", "name", QLineEdit::Normal, QString::null, &ok );
140
141   if ( ok && !myName.isEmpty()) 
142   {
143     ::CORBA::Double c;
144     engine->s1(1.,2.,c);
145     std::cerr << c << std::endl;
146     QString banner = "Hello " + myName;
147     SUIT_MessageBox::information( getApp()->desktop(), "info", banner, "OK" );
148   }
149 }
150
151 // Action slot
152 void cppcomposGUI::OnDesigner()
153 {
154   QWidget* wid= new MyDemo(getApp()->desktop());
155   wid->show();
156 }
157
158 MyDemo::MyDemo(QWidget *parent)
159         :QDialog(parent)
160 {
161   ui.setupUi(this);
162 }