Salome HOME
Update copyrights 2014.
[samples/light.git] / src / LIGHTGUI / LIGHTGUI_DataObject.cxx
1 // Copyright (C) 2005-2014  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 // LIGHT : sample (no-corba-engine) SALOME module
21 // File   : LIGHTGUI_DataObject.cxx
22 // Author : Julia DOROVSKIKH
23 //
24 #include "LIGHTGUI_DataObject.h"
25 #include "LIGHTGUI_DataModel.h"
26
27 #include <SUIT_Session.h>
28 #include <SUIT_ResourceMgr.h>
29 #include <QString>
30
31 /*!
32   \class LIGHTGUI_DataObject
33   \brief LIGHT module data object.
34 */
35
36 /*!
37   \brief Constructor.
38   \param id line identifier
39   \param txt text line corresponding to the data object
40   \param parent parent data object
41 */
42 LIGHTGUI_DataObject::LIGHTGUI_DataObject( const int id, 
43                                           const QString& txt, 
44                                           SUIT_DataObject* parent )
45 : CAM_DataObject( parent ),
46   LightApp_DataObject( parent ),
47   myLineTxt( txt ),
48   myId( id )
49 {
50 }
51
52 /*!
53   \brief Destructor.
54 */
55 LIGHTGUI_DataObject::~LIGHTGUI_DataObject()
56 {
57 }
58
59 /*!
60   \brief Get data object name.
61   \return data object name
62 */
63 QString LIGHTGUI_DataObject::name() const
64 {
65   return myLineTxt.trimmed().isEmpty() ? QObject::tr( "LIGHT_PARAGRAPH" ) : myLineTxt;
66 }
67
68 /*!
69   \brief Get data object entry.
70   \return data object entry
71 */
72 QString LIGHTGUI_DataObject::entry() const
73 {
74   return LIGHTGUI_DataModel::entry( id() );
75 }
76
77 /*!
78   \brief Get data object icon.
79   \param index data column index
80   \return data object icon for the specified column
81 */
82 QPixmap LIGHTGUI_DataObject::icon( const int index ) const
83 {
84   if ( index == NameId ) {
85     // show icon only for the "Name" column
86     static QPixmap pxp = SUIT_Session::session()->resourceMgr()->loadPixmap( "LIGHT", QObject::tr( "ICON_PARAGRAPH" ), false );
87     static QPixmap pxl = SUIT_Session::session()->resourceMgr()->loadPixmap( "LIGHT", QObject::tr( "ICON_LINE" ), false );
88     return myLineTxt.trimmed().isEmpty() ? pxp : pxl;
89   }
90   return LightApp_DataObject::icon( index );
91 }
92
93 /*!
94   \brief Get data object tooltip.
95   \param index data column index
96   \return data object tooltip for the specified column
97 */
98 QString LIGHTGUI_DataObject::toolTip( const int /*index*/ ) const
99 {
100   // show the same tooltip for all columns
101   return lineText().trimmed().isEmpty() ?
102     QString( QObject::tr( "LIGHT_PARAGRAPH") + " %1" ).arg( parent()->childPos( this ) + 1 ) :
103     QString( QObject::tr( "LIGHT_LINE" ) + " %1: %2" ).arg( lineNb() ).arg( lineText() );
104 }
105
106 /*!
107   \brief Get text line corresponding to the data object.
108   \return text line (empty lines means paragraph)
109   \sa setLineText()
110 */
111 QString LIGHTGUI_DataObject::lineText() const
112 {
113   return myLineTxt;
114 }
115
116 /*!
117   \brief Set text line corresponding to the data object.
118   \param txt text line (empty lines means paragraph)
119   \sa lineText()
120 */
121 void LIGHTGUI_DataObject::setLineText( const QString& txt )
122 {
123   myLineTxt = txt;
124 }
125
126 /*!
127   \brief Get line number.
128   \return line number
129 */
130 int LIGHTGUI_DataObject::lineNb() const
131 {
132   if ( level() == 1 ) // root object
133     return -1;
134   int pos = 0;
135   if ( parent() ) {
136     SUIT_DataObject* o = (SUIT_DataObject*)this;
137     if ( level() == 3 ) {
138       pos += parent()->childPos( this ) + 1;
139       o = parent();
140     }
141     o = o->prevBrother();
142     while ( o ) {
143       pos += o->childCount() + 1;
144       o = o->prevBrother();
145     }
146   }
147   return pos;
148 }
149
150 /*!
151   \brief Get line identifier.
152   \return line identifier
153 */
154 int LIGHTGUI_DataObject::id() const
155 {
156   return myId;
157 }