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