Salome HOME
Example of light module from Salome tutorial
[samples/atomic.git] / src / ATOMICGUI / ATOMICGUI.cxx
1 //  Copyright (C) 2007-2010  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  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 using namespace std;
23
24 #include "ATOMICGUI.h"
25
26 #include "ATOMICGUI_DataModel.h"
27 #include "ATOMICGUI_DataObject.h"
28 #include "ATOMICGUI_Selection.h"
29 #include "ATOMICGUI_CreateMolOp.h"
30 #include "ATOMICGUI_AddAtomOp.h"
31 #include "ATOMICGUI_ImportExportOp.h"
32 #include "ATOMICGUI_RenameOp.h"
33 #include "ATOMICGUI_DeleteOp.h"
34
35 #include <LightApp_Application.h>
36 #include <LightApp_DataOwner.h>
37 #include <LightApp_SelectionMgr.h>
38
39 #include <SUIT_ResourceMgr.h>
40 #include <SUIT_Session.h>
41 #include <SUIT_Desktop.h>
42
43 #include <qaction.h>
44
45 /*! Constructor */
46 ATOMICGUI::ATOMICGUI()
47 : LightApp_Module( "ATOMICGUI" )
48 {
49 }
50
51 /*! Initialization funciton.  Called only once on first activation of GUI module.  */
52 void ATOMICGUI::initialize ( CAM_Application* app )
53 {
54   LightApp_Module::initialize( app );// call parent's implementation
55
56   SUIT_Desktop* parent = application()->desktop();
57   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
58
59   // create actions
60   createAction( agCreateMol, tr( "TOP_CREATE_MOL" ), resMgr->loadPixmap( "ATOMIC", tr( "ICON_MOLECULE" ) ), 
61                 tr( "MEN_CREATE_MOL" ), tr( "STB_CREATE_MOL" ), 0, parent, false, this, SLOT( onOperation() ) );
62   createAction( agAddAtom,    tr( "TOP_ADD_ATOM" ),    resMgr->loadPixmap( "ATOMIC", tr( "ICON_ATOM" ) ), 
63                 tr( "MEN_ADD_ATOM" ),    tr( "STB_ADD_ATOM" ),    0, parent, false, this, SLOT( onOperation() ) );
64   createAction( agRename,     tr( "TOP_RENAME" ),      resMgr->loadPixmap( "ATOMIC", tr( "ICON_RENAME" ) ),
65                 tr( "MEN_RENAME" ),      tr( "STB_RENAME" ), 0, parent, false, this, SLOT( onOperation() ) );
66   createAction( agDelete,     tr( "TOP_DELETE_OBJ" ),  resMgr->loadPixmap( "ATOMIC", tr( "ICON_REMOVE" ) ),
67                 tr( "MEN_DELETE_OBJ" ),  tr( "STB_DELETE_OBJ" ),  0, parent, false, this, SLOT( onOperation() ) );
68   createAction( agImportXML,  tr( "TOP_IMPORT_XML" ),  QIcon(), tr( "MEN_IMPORT_XML" ),
69                 tr( "STB_IMPORT_XML" ),  0, parent, false, this, SLOT( onOperation() ) );
70   createAction( agExportXML,  tr( "TOP_EXPORT_XML" ),  QIcon(), tr( "MEN_EXPORT_XML" ),
71                 tr( "STB_EXPORT_XML" ),  0, parent, false, this, SLOT( onOperation() ) );
72
73   // init popup menus
74   int aFileMnu = createMenu( tr( "MEN_FILE" ), -1, -1 );
75   createMenu( separator(), aFileMnu, -1, 10 );
76   createMenu( agImportXML, aFileMnu, 10 );
77   createMenu( agExportXML, aFileMnu, 10 );
78   createMenu( separator(), aFileMnu, -1, 10 );
79
80   int aEditMnu = createMenu( tr( "MEN_EDIT" ), -1, -1 );
81   createMenu( separator(), aEditMnu, -1, 10 );
82   createMenu( agRename,    aEditMnu, 10 );
83   createMenu( agDelete,    aEditMnu, 10 );
84   createMenu( separator(), aEditMnu, -1, 10 );
85
86   int aAtomicMnu = createMenu( tr( "MEN_ATOMIC" ), -1, -1, 50 );
87   createMenu( agCreateMol, aAtomicMnu, 10 );
88   createMenu( separator(), aAtomicMnu, -1, 10 );
89   createMenu( agAddAtom,   aAtomicMnu, 10 );
90
91   // create toolbar
92   int tbId = createTool( tr( "MEN_ATOMIC" ) );
93   createTool( agCreateMol, tbId );
94   createTool( agAddAtom,   tbId );
95   createTool( separator(),  tbId );
96   createTool( agRename,     tbId );
97   createTool( separator(),  tbId );
98   createTool( agDelete,     tbId );
99
100   // popup for object browser
101   int parentId = -1;
102   popupMgr()->insert ( separator(),            parentId, 0 );
103   popupMgr()->insert ( action( agCreateMol ),  parentId, 0 );
104   popupMgr()->insert ( action( agAddAtom ),    parentId, 0 );
105   popupMgr()->insert ( separator(),            parentId, 0 );
106   popupMgr()->insert ( action( agRename ),     parentId, 0 );
107   popupMgr()->insert ( action( agDelete ),     parentId, 0 );
108   popupMgr()->insert ( separator(),            parentId, 0 );
109
110   QString rule = "client='ObjectBrowser' and selcount=1 and type='Molecule'";
111   popupMgr()->setRule( action( agAddAtom ),  rule );
112
113   rule = "client='ObjectBrowser' and selcount=1 and type='Root'";
114   popupMgr()->setRule( action( agCreateMol ),rule );
115
116   rule = "($type in {'Molecule' 'Atom'}) and client='ObjectBrowser' and selcount=1";
117   popupMgr()->setRule( action( agRename ),   rule );
118
119   rule = "($type in {'Molecule' 'Atom'}) and client='ObjectBrowser' and selcount>0";
120   popupMgr()->setRule( action( agDelete ),   rule );
121 }
122
123 /*! Returns default icon of a component. */
124 QString ATOMICGUI::iconName() const
125 {
126   return QObject::tr( "ICON_ATOMIC" );
127 }
128
129 /*! Returns list of windows required for this GUI module. */
130 void ATOMICGUI::windows ( QMap<int, int>& aMap ) const
131 {
132   aMap.insert( LightApp_Application::WT_ObjectBrowser, Qt::LeftDockWidgetArea );
133 }
134
135 /*! Returns list of entities of selected objects. */
136 void ATOMICGUI::selected( QStringList& entries, const bool multiple )
137 {
138   LightApp_SelectionMgr* mgr = getApp()->selectionMgr();
139   if( !mgr )
140     return;
141
142   SUIT_DataOwnerPtrList anOwnersList;
143   mgr->selected( anOwnersList );
144     
145   for ( int i = 0; i < anOwnersList.size(); i++ )
146   {
147     const LightApp_DataOwner* owner = dynamic_cast<const LightApp_DataOwner*>( anOwnersList[ i ].get() );
148     QStringList es = owner->entry().split( "_" );
149     if ( es.count() > 1 && es[ 0 ] == "ATOMICGUI" && es[ 1 ] != "root" )
150     {
151       entries.append( owner->entry() );
152       if( !multiple )
153         break;
154     }
155   }
156 }
157
158 /*! Instantiation of a custom Data Model. */
159 CAM_DataModel* ATOMICGUI::createDataModel()
160 {
161   return new ATOMICGUI_DataModel( this );
162 }
163
164 /*! Instantiation of a custom Selection object - manager of parameters for popup construction. */
165 LightApp_Selection* ATOMICGUI::createSelection() const
166 {
167   return new ATOMICGUI_Selection();
168 }
169
170 /*! Activation function. Called on every activation of a GUI module. */
171 bool ATOMICGUI::activateModule ( SUIT_Study* study )
172 {
173   bool isDone = LightApp_Module::activateModule( study );
174   if ( !isDone ) return false;
175
176   setMenuShown( true );
177   setToolShown( true );
178
179   return isDone;
180 }
181
182 /*! Deactivation function. Called on every deactivation of a GUI module. */
183 bool ATOMICGUI::deactivateModule ( SUIT_Study* study )
184 {
185   setMenuShown( false );
186   setToolShown( false );
187
188   return LightApp_Module::deactivateModule( study );
189 }
190
191 /*! slot connected to all functions of the component (create molecule, add atom, etc.) */
192 void ATOMICGUI::onOperation()
193 {
194   if( sender() && sender()->inherits( "QAction" ) )
195   {
196     int id = actionId( ( QAction* )sender() );
197     startOperation( id );
198   }
199 }
200
201 /*! Instantiation of a custom Operation object - component's action manager. */
202 LightApp_Operation* ATOMICGUI::createOperation( const int id ) const
203 {
204   switch( id )
205   {
206   case agImportXML:
207     return new ATOMICGUI_ImportExportOp( true );
208
209   case agExportXML:
210     return new ATOMICGUI_ImportExportOp( false );
211
212   case agCreateMol:
213     return new ATOMICGUI_CreateMolOp();
214
215   case agAddAtom:
216     return new ATOMICGUI_AddAtomOp();
217
218   case agRename:
219     return new ATOMICGUI_RenameOp();
220
221   case agDelete:
222     return new ATOMICGUI_DeleteOp();
223
224   default:
225     return 0;
226   }
227 }
228
229 #if defined(WNT)
230 #define ATOMICGUI_EXPORT __declspec(dllexport)
231 #else   // WNT
232 #define ATOMICGUI_EXPORT
233 #endif  // WNT
234
235 /*! GUI module instantiation function */
236 extern "C" {
237   ATOMICGUI_EXPORT CAM_Module* createModule() {
238     return new ATOMICGUI();
239   }
240 }