]> SALOME platform Git repositories - modules/yacs.git/blob - src/salomegui/Yacsgui.cxx
Salome HOME
mergefrom branch BR_V511_PR tag mergeto_trunk_03feb09
[modules/yacs.git] / src / salomegui / Yacsgui.cxx
1 //  Copyright (C) 2006-2008  CEA/DEN, 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.
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 #include "Yacsgui.hxx"
20 #include "Yacsgui_DataModel.hxx"
21
22 #include <SUIT_MessageBox.h>
23 #include <SUIT_ResourceMgr.h>
24 #include <SUIT_Desktop.h>
25 #include <SUIT_ViewManager.h>
26 #include <SUIT_ViewWindow.h>
27 #include <SalomeApp_Application.h>
28 #include <QxScene_ViewManager.h>
29 #include <QxScene_ViewModel.h>
30 #include <QxScene_ViewWindow.h>
31
32 #include <SalomeApp_DataObject.h>
33 #include <SalomeApp_Study.h>
34 #include <SalomeApp_Module.h>
35 #include <SUIT_DataBrowser.h>
36 #include <QtxTreeView.h>
37 #include <SUIT_DataObject.h>
38
39 #include <SALOME_LifeCycleCORBA.hxx>
40 #include <QInputDialog>
41 #include <QIcon>
42 #include <cassert>
43
44 #include "GenericGui.hxx"
45
46 //#define _DEVDEBUG_
47 #include "YacsTrace.hxx"
48
49 using namespace std;
50 using namespace YACS::HMI;
51
52 Yacsgui::Yacsgui() :
53   SalomeWrap_Module( "YACS" ) // default name
54 {
55   _wrapper = 0;
56   _genericGui = 0;
57   _selectFromTree = false;
58 }
59
60 void Yacsgui::initialize( CAM_Application* app )
61 {
62   DEBTRACE("Yacsgui::initialize");
63   SalomeApp_Module::initialize( app );
64
65   QWidget* aParent = application()->desktop();
66   DEBTRACE(app << "  " << application() << " " << application()->desktop() << " " << aParent);
67
68   SUIT_ResourceMgr* aResourceMgr = app->resourceMgr();
69   _wrapper = new SuitWrapper(this);
70   _genericGui = new GenericGui(_wrapper, app->desktop());
71
72   if ( app && app->desktop() )
73     {
74       connect( app->desktop(), SIGNAL( windowActivated( SUIT_ViewWindow* ) ),
75                this, SLOT(onWindowActivated( SUIT_ViewWindow* )) );
76
77       connect( getApp()->objectBrowser()->treeView(),
78                SIGNAL( doubleClicked(const QModelIndex&) ), 
79                this,
80                SLOT  ( onDblClick(const QModelIndex&) ) );
81
82       connect( getApp(),
83                SIGNAL(studyClosed()),
84                _genericGui,
85                SLOT  (onCleanOnExit()));
86     }
87   _genericGui->createActions();
88   _genericGui->createMenus();
89   _genericGui->createTools();
90
91   if (createSComponent()) updateObjBrowser();
92 }
93
94 void Yacsgui::viewManagers( QStringList& list ) const
95 {
96   DEBTRACE("Yacsgui::viewManagers");
97   list.append( QxScene_Viewer::Type() );
98 }
99
100 bool Yacsgui::activateModule( SUIT_Study* theStudy )
101 {
102   DEBTRACE("Yacsgui::activateModule");
103   bool bOk = SalomeApp_Module::activateModule( theStudy );
104
105   setMenuShown( true );
106   setToolShown( true );
107   _genericGui->showDockWidgets(true);
108
109   return bOk;
110 }
111
112 bool Yacsgui::deactivateModule( SUIT_Study* theStudy )
113 {
114   DEBTRACE("Yacsgui::deactivateModule");
115
116   setMenuShown( false );
117   setToolShown( false );
118   _genericGui->showDockWidgets(false);
119
120   SUIT_ViewManager *svm = getApp()->getViewManager(QxScene_Viewer::Type(), true);
121   assert(svm);
122   SUIT_ViewWindow* svw = svm->getActiveView();
123   QxScene_ViewWindow *aView = 0;
124   if (svw) aView = dynamic_cast<QxScene_ViewWindow*>(svw);
125   DEBTRACE("aView " << aView);
126
127   return SalomeApp_Module::deactivateModule( theStudy );
128 }
129
130 // --- Default windows
131
132 void Yacsgui::windows( QMap<int, int>& theMap ) const
133 {
134   DEBTRACE("Yacsgui::windows");
135   theMap.clear();
136   theMap.insert( SalomeApp_Application::WT_ObjectBrowser, Qt::LeftDockWidgetArea );
137 //   theMap.insert( SalomeApp_Application::WT_PyConsole,     Qt::BottomDockWidgetArea );
138 }
139
140 QString  Yacsgui::engineIOR() const
141 {
142   DEBTRACE("Yacsgui::engineIOR");
143   return getApp()->defaultEngineIOR();
144 }
145
146 void Yacsgui::onDblClick(const QModelIndex& index)
147 {
148   DEBTRACE("Yacsgui::onDblClick");
149   DataObjectList dol =getApp()->objectBrowser()->getSelected();
150   if (dol.isEmpty()) return;
151
152   SalomeApp_DataObject* item = dynamic_cast<SalomeApp_DataObject*>(dol[0]);
153   if (!item) return;
154
155   DEBTRACE(item->name().toStdString());
156   SalomeWrap_DataModel *model = dynamic_cast<SalomeWrap_DataModel*>(dataModel());
157   if (!model) return;
158   DEBTRACE(item->entry().toStdString());
159   QWidget * viewWindow = model->getViewWindow(item->entry().toStdString());
160   if (!_genericGui) return;
161   if (!viewWindow) return;
162   DEBTRACE("--- " << viewWindow << " "  << item->entry().toStdString());
163
164   _selectFromTree = true;
165   viewWindow->setFocus();
166   _selectFromTree = false;
167 }
168
169 void Yacsgui::onWindowActivated( SUIT_ViewWindow* svw)
170 {
171   DEBTRACE("Yacsgui::onWindowActivated");
172   QxScene_ViewWindow* viewWindow = dynamic_cast<QxScene_ViewWindow*>(svw);
173   if (!viewWindow) return;
174   DEBTRACE("viewWindow " << viewWindow);
175   DEBTRACE("activeModule()->moduleName() " << getApp()->activeModule()->moduleName().toStdString());
176   if (getApp()->activeModule()->moduleName().compare("YACS") != 0)
177     getApp()->activateModule("YACS");
178
179   assert(_genericGui);
180   _genericGui->switchContext(viewWindow);
181
182   if (_selectFromTree) return;
183   SalomeWrap_DataModel *model = dynamic_cast<SalomeWrap_DataModel*>(dataModel());
184   if (!model) return;
185   model->setSelected(svw);
186 }
187
188 void Yacsgui::onWindowClosed( SUIT_ViewWindow* svw)
189 {
190   DEBTRACE("Yacsgui::onWindowClosed");
191 }
192
193 CAM_DataModel* Yacsgui::createDataModel()
194 {
195   return new Yacsgui_DataModel(this);
196 }
197
198 bool Yacsgui::createSComponent()
199 {
200   DEBTRACE("Yacsgui::createSComponent");
201   _PTR(Study)            aStudy = (( SalomeApp_Study* )(getApp()->activeStudy()))->studyDS();
202   _PTR(StudyBuilder)     aBuilder (aStudy->NewBuilder());
203   _PTR(GenericAttribute) anAttr;
204   _PTR(AttributeName)    aName;
205
206   // --- Find or create "YACS" SComponent in the study
207
208   _PTR(SComponent) aComponent = aStudy->FindComponent("YACS");
209   if ( !aComponent )
210     {
211       aComponent = aBuilder->NewComponent("YACS");
212       anAttr = aBuilder->FindOrCreateAttribute(aComponent, "AttributeName");
213       aName = _PTR(AttributeName) (anAttr);
214       aName->SetValue(getApp()->moduleTitle("YACS").toStdString());
215       
216       anAttr = aBuilder->FindOrCreateAttribute(aComponent, "AttributePixMap");
217       _PTR(AttributePixMap) aPixmap(anAttr);
218       aPixmap->SetPixMap("share/salome/resources/yacs/ModuleYacs.png");
219       
220       aBuilder->DefineComponentInstance(aComponent, getApp()->defaultEngineIOR().toStdString());
221
222       return true;
223     }
224   return false;
225 }
226
227 // --- Export the module
228
229 extern "C"
230 {
231   CAM_Module* createModule()
232   {
233     return new Yacsgui();
234   }
235 }