Salome HOME
Copyright update 2021
[samples/atomic.git] / src / ATOMICGUI / ATOMICGUI.cxx
1 // Copyright (C) 2007-2021  CEA/DEN, EDF R&D, OPEN CASCADE
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 "ATOMICGUI.h"
21
22 #include "ATOMICGUI_DataModel.h"
23 #include "ATOMICGUI_DataObject.h"
24 #include "ATOMICGUI_Selection.h"
25 #include "ATOMICGUI_CreateMolOp.h"
26 #include "ATOMICGUI_AddAtomOp.h"
27 #include "ATOMICGUI_ImportExportOp.h"
28 #include "ATOMICGUI_RenameOp.h"
29 #include "ATOMICGUI_DeleteOp.h"
30
31 #include <ATOMIC_version.h>
32
33 #include <LightApp_Application.h>
34 #include <LightApp_DataOwner.h>
35 #include <LightApp_SelectionMgr.h>
36
37 #include <SUIT_ResourceMgr.h>
38 #include <SUIT_Session.h>
39 #include <SUIT_Desktop.h>
40
41 #include <qaction.h>
42
43 using namespace std;
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" ), QString( "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   aMap.insert( LightApp_Application::WT_PyConsole, Qt::BottomDockWidgetArea);
134 }
135
136 /*! Returns list of entities of selected objects. */
137 void ATOMICGUI::selected( QStringList& entries, const bool multiple )
138 {
139   LightApp_SelectionMgr* mgr = getApp()->selectionMgr();
140   if( !mgr )
141     return;
142
143   SUIT_DataOwnerPtrList anOwnersList;
144   mgr->selected( anOwnersList );
145     
146   for ( int i = 0; i < anOwnersList.size(); i++ )
147   {
148     const LightApp_DataOwner* owner = dynamic_cast<const LightApp_DataOwner*>( anOwnersList[ i ].get() );
149     QStringList es = owner->entry().split( "_" );
150     if ( es.count() > 1 && es[ 0 ] == "ATOMICGUI" && es[ 1 ] != "root" )
151     {
152       entries.append( owner->entry() );
153       if( !multiple )
154         break;
155     }
156   }
157 }
158
159 /*! Instantiation of a custom Data Model. */
160 CAM_DataModel* ATOMICGUI::createDataModel()
161 {
162   return new ATOMICGUI_DataModel( this );
163 }
164
165 /*! Instantiation of a custom Selection object - manager of parameters for popup construction. */
166 LightApp_Selection* ATOMICGUI::createSelection() const
167 {
168   return new ATOMICGUI_Selection();
169 }
170
171 /*! Activation function. Called on every activation of a GUI module. */
172 bool ATOMICGUI::activateModule ( SUIT_Study* study )
173 {
174   bool isDone = LightApp_Module::activateModule( study );
175   if ( !isDone ) return false;
176
177   setMenuShown( true );
178   setToolShown( true );
179
180   return isDone;
181 }
182
183 /*! Deactivation function. Called on every deactivation of a GUI module. */
184 bool ATOMICGUI::deactivateModule ( SUIT_Study* study )
185 {
186   setMenuShown( false );
187   setToolShown( false );
188
189   return LightApp_Module::deactivateModule( study );
190 }
191
192 /*! slot connected to all functions of the component (create molecule, add atom, etc.) */
193 void ATOMICGUI::onOperation()
194 {
195   if( sender() && sender()->inherits( "QAction" ) )
196   {
197     int id = actionId( ( QAction* )sender() );
198     startOperation( id );
199   }
200 }
201
202 /*! Instantiation of a custom Operation object - component's action manager. */
203 LightApp_Operation* ATOMICGUI::createOperation( const int id ) const
204 {
205   switch( id )
206   {
207   case agImportXML:
208     return new ATOMICGUI_ImportExportOp( true );
209
210   case agExportXML:
211     return new ATOMICGUI_ImportExportOp( false );
212
213   case agCreateMol:
214     return new ATOMICGUI_CreateMolOp();
215
216   case agAddAtom:
217     return new ATOMICGUI_AddAtomOp();
218
219   case agRename:
220     return new ATOMICGUI_RenameOp();
221
222   case agDelete:
223     return new ATOMICGUI_DeleteOp();
224
225   default:
226     return 0;
227   }
228 }
229
230 #if defined(WIN32)
231 #define ATOMICGUI_EXPORT __declspec(dllexport)
232 #else   // WIN32
233 #define ATOMICGUI_EXPORT
234 #endif  // WIN32
235
236 /*! GUI module instantiation function */
237 extern "C" {
238   ATOMICGUI_EXPORT CAM_Module* createModule() {
239     return new ATOMICGUI();
240   }
241   
242   ATOMICGUI_EXPORT char* getModuleVersion() {
243     return (char*)ATOMIC_VERSION_STR;
244   }
245 }