Salome HOME
This commit was generated by cvs2git to create branch 'V6_main'.
[tools/tutorial.git] / 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_AddAtomDlg.h"
29
30 #include <LightApp_Application.h>
31 #include <LightApp_DataOwner.h>
32 #include <LightApp_SelectionMgr.h>
33
34 #include <SUIT_ResourceMgr.h>
35 #include <SUIT_Session.h>
36 #include <SUIT_Desktop.h>
37
38 #include <qaction.h>
39 #include <qmenu.h>
40
41 /*! Constructor */
42 ATOMICGUI::ATOMICGUI()
43 : LightApp_Module( "ATOMICGUI" )
44 {
45 }
46
47 /*! Initialization funciton.  Called only once on first activation of GUI module.  */
48 void ATOMICGUI::initialize ( CAM_Application* app )
49 {
50   LightApp_Module::initialize( app );// call parent's implementation
51
52   SUIT_Desktop* parent = application()->desktop();
53   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
54
55   // create actions
56   createAction( agCreateMol, tr("TOP_CREATE_MOL"), resMgr->loadPixmap("ATOMIC", tr("ICON_MOLECULE")),
57                 tr("MEN_CREATE_MOL"), tr("STB_CREATE_MOL"), 0, parent, false, this, SLOT(onOperation()) );
58   createAction( agAddAtom,   tr("TOP_ADD_ATOM"),   resMgr->loadPixmap("ATOMIC", tr("ICON_ATOM")),
59                 tr("MEN_ADD_ATOM"),   tr("STB_ADD_ATOM"),   0, parent, false, this, SLOT(onOperation()) );
60
61   // init popup menus
62   int aAtomicMnu = createMenu( tr( "MEN_ATOMIC" ), -1, -1, 50 );
63   createMenu( agCreateMol, aAtomicMnu, 10 );
64   createMenu( separator(), aAtomicMnu, -1, 10 );
65   createMenu( agAddAtom,   aAtomicMnu, 10 );
66
67   // create toolbar
68   int tbId = createTool( tr( "MEN_ATOMIC" ) );
69   createTool( agCreateMol, tbId );
70   createTool( agAddAtom,   tbId );
71 }
72
73 /*! Returns default icon of a component. */
74 QString ATOMICGUI::iconName() const
75 {
76   return QObject::tr( "ICON_ATOMIC" );
77 }
78
79 /*! Returns list of windows required for this GUI module. */
80 void ATOMICGUI::windows ( QMap<int, int>& aMap ) const
81 {
82   aMap.insert( LightApp_Application::WT_ObjectBrowser, Qt::LeftDockWidgetArea );
83 }
84
85 /*! Returns list of entities of selected objects. */
86 void ATOMICGUI::selected( QStringList& entries, const bool multiple )
87 {
88   LightApp_SelectionMgr* mgr = getApp()->selectionMgr();
89   if( !mgr )
90     return;
91
92   SUIT_DataOwnerPtrList anOwnersList;
93   mgr->selected( anOwnersList );
94
95   for ( int i = 0; i < anOwnersList.size(); i++ )
96   {
97     const LightApp_DataOwner* owner = dynamic_cast<const LightApp_DataOwner*>( anOwnersList[ i ].get() );
98     QStringList es = owner->entry().split( "_" );
99     if ( es.count() > 1 && es[ 0 ] == "ATOMICGUI" && es[ 1 ] != "root" )
100     {
101       entries.append( owner->entry() );
102       if( !multiple )
103         break;
104     }
105   }
106 }
107
108 /*! Instantiation of a custom Data Model. */
109 CAM_DataModel* ATOMICGUI::createDataModel()
110 {
111   return new ATOMICGUI_DataModel( this );
112 }
113
114 /*! Activation function. Called on every activation of a GUI module. */
115 bool ATOMICGUI::activateModule ( SUIT_Study* study )
116 {
117   bool isDone = LightApp_Module::activateModule( study );
118   if ( !isDone ) return false;
119
120   setMenuShown( true );
121   setToolShown( true );
122
123   return isDone;
124 }
125
126 /*! Deactivation function. Called on every deactivation of a GUI module. */
127 bool ATOMICGUI::deactivateModule ( SUIT_Study* study )
128 {
129   setMenuShown( false );
130   setToolShown( false );
131
132   return LightApp_Module::deactivateModule( study );
133 }
134
135 /*! slot connected to all functions of the component (create molecule, add atom, etc.) */
136 void ATOMICGUI::onOperation()
137 {
138   if( sender() && sender()->inherits( "QAction" ) )
139   {
140     int id = actionId( ( QAction* )sender() );
141     if ( id == agCreateMol ) {
142       ATOMICGUI_DataModel* dm = dynamic_cast<ATOMICGUI_DataModel*>( dataModel() );
143       if ( dm ) {
144         dm->createMolecule();
145         getApp()->updateObjectBrowser();
146       }
147     }
148     else if ( id == agAddAtom ) {
149       QStringList entries;
150       selected( entries, false );
151       ATOMICGUI_AddAtomDlg dlg ( getApp()->desktop() );
152       int res = dlg.exec();
153       ATOMICGUI_DataModel* dm = dynamic_cast<ATOMICGUI_DataModel*>( dataModel() );
154       if( dm && res == QDialog::Accepted && dlg.acceptData( entries ) ) {
155         QString name;
156         double x, y, z;
157         dlg.data( name, x, y, z );
158         dm->addAtom( entries.first(), name, x, y, z );
159         getApp()->updateObjectBrowser();
160       }
161     }
162   }
163 }
164
165 /*! Called on popup menu request by LightApp_Application. */
166 void ATOMICGUI::contextMenuPopup( const QString& client, QMenu* menu, QString& /*title*/ )
167 {
168   if ( client == "ObjectBrowser" ) {
169     QStringList entries;
170     selected( entries, false );
171     if ( entries.size() ) {
172       QStringList es = entries.first().split( "_" );
173       if ( es.count() == 2 && es[ 0 ] == "ATOMICGUI" ) { // selected object belongs to ATOMICGUI
174                                                          // and it is a molecule object
175         menu->addAction(action( agAddAtom ));
176       }
177     }
178   }
179 }
180
181
182 #if defined(WNT)
183 #define ATOMICGUI_EXPORT __declspec(dllexport)
184 #else   // WNT
185 #define ATOMICGUI_EXPORT
186 #endif  // WNT
187
188 /*! GUI module instantiation function */
189 extern "C" {
190   ATOMICGUI_EXPORT CAM_Module* createModule() {
191     return new ATOMICGUI();
192   }
193 }