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