Salome HOME
Example of light module from Salome tutorial
[samples/atomic.git] / src / ATOMICGUI / ATOMICGUI_DataObject.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 #include "ATOMICGUI_DataObject.h"
23 #include "ATOMICGUI_Data.h"
24
25 #include <SUIT_Session.h>
26 #include <SUIT_ResourceMgr.h>
27 #include <CAM_DataModel.h>
28 #include <CAM_Module.h>
29
30 /*! Constructor */
31 ATOMICGUI_DataObject::ATOMICGUI_DataObject( SUIT_DataObject* parent,
32                                             ATOMICGUI_AtomicMolecule* mol, 
33                                             int idx )
34   : LightApp_DataObject( parent ),
35     CAM_DataObject( parent),
36     myMolecule( mol ),
37     myIndex( idx )
38 {
39 }
40
41 /*! Destructor */
42 ATOMICGUI_DataObject::~ATOMICGUI_DataObject()
43 {
44 }
45
46 /*! Returns unique object ID */
47 QString ATOMICGUI_DataObject::entry() const
48 {
49   QString id = "root";
50   if ( myMolecule )
51     id = QString::number( myMolecule->id() ); 
52   if ( myIndex >= 0 )
53     id += QString( "_%1" ).arg( QString::number( myMolecule->atomId( myIndex ) ) ); 
54   return QString( "ATOMICGUI_%1" ).arg( id );
55 }
56
57 /*! Returns a name of the Data Object. */
58 QString ATOMICGUI_DataObject::name() const
59 {
60   QString n;
61   if ( myMolecule )
62     n = myIndex < 0 ? myMolecule->name() 
63                     : myMolecule->atomName( myIndex );
64   return n;
65 }
66
67 /*! Returns an icon of the Data Object. */
68 QPixmap ATOMICGUI_DataObject::icon(const int column) const
69 {
70   static QPixmap pxmole = SUIT_Session::session()->resourceMgr()->loadPixmap( "ATOMIC", QObject::tr( "ICON_MOLECULE" ), false );
71   static QPixmap pxatom = SUIT_Session::session()->resourceMgr()->loadPixmap( "ATOMIC", QObject::tr( "ICON_ATOM" ), false );
72   static QPixmap nullpx;
73   return column == NameId ? ( myIndex < 0 ? pxmole : pxatom ) : nullpx;
74 }
75
76 /*! Returns a tooltip for the object (to be displayed in the Object Browser). */
77 QString ATOMICGUI_DataObject::toolTip(const int) const
78 {
79   QString tt;
80   if ( myMolecule )
81     tt = myIndex < 0 ? QString( QObject::tr( "ATOMIC_MOLECULE" ) + ": %1" ).arg( myMolecule->name() )
82       : QString( QObject::tr( "ATOMIC_ATOM" ) + ": %1: %2,%3,%4" ).
83       arg( myMolecule->atomName( myIndex ) ).
84       arg( myMolecule->atomX( myIndex ) ).
85       arg( myMolecule->atomY( myIndex ) ).
86       arg( myMolecule->atomZ( myIndex ) );
87   return tt;
88 }
89
90 /*! Returns true if the Data Object corresponds to a Molecule. */
91 bool ATOMICGUI_DataObject::isMolecule() const
92 {
93   return myMolecule && myIndex < 0;
94 }
95
96 /*! Returns true if the Data Object corresponds to an Atom. */
97 bool ATOMICGUI_DataObject::isAtom() const
98 {
99   return myMolecule && myIndex >= 0;
100 }
101
102 /*! Constructor */
103 ATOMICGUI_ModuleObject::ATOMICGUI_ModuleObject( CAM_DataModel* dm, SUIT_DataObject* parent )
104 : ATOMICGUI_DataObject( parent ),
105   LightApp_ModuleObject( dm, parent ),
106   CAM_DataObject( parent )
107 {
108 }
109
110 /*! Returns a name of the root object. */
111 QString ATOMICGUI_ModuleObject::name() const
112 {
113   return CAM_ModuleObject::name();
114 }
115
116 /*! Returns an icon of the root object. */
117 QPixmap ATOMICGUI_ModuleObject::icon(const int column) const
118 {
119   QPixmap px;
120   if ( column == NameId && dataModel() ) {
121     QString anIconName = dataModel()->module()->iconName();
122     if ( !anIconName.isEmpty() )
123       px = SUIT_Session::session()->resourceMgr()->loadPixmap( "ATOMIC", anIconName, false );
124   }
125   return px;
126 }
127
128 /*! Returns a tooltip of the root object. */
129 QString ATOMICGUI_ModuleObject::toolTip(const int) const
130 {
131   return QObject::tr( "ATOMIC_ROOT_TOOLTIP" );
132 }