]> SALOME platform Git repositories - modules/gui.git/blob - src/ObjBrowser/OB_ListItem.cxx
Salome HOME
fb821308f2d2d6c8bcf8c9f87f5b3f19d3356762
[modules/gui.git] / src / ObjBrowser / OB_ListItem.cxx
1 // Copyright (C) 2005  OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
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/
18 //
19 #include "OB_ListItem.h"
20
21 #include "OB_ListView.h"
22
23 #include <SUIT_DataObject.h>
24
25 #include <qpainter.h>
26 #include <qwmatrix.h>
27
28 #include <iostream>
29 using namespace std;
30
31 /*!
32     Class: ListItem
33     Descr: base template class
34 */
35
36 template<class T>
37 ListItemF<T>::ListItemF( T& theT, SUIT_DataObject* obj ) :
38 myT( theT ),
39 myObject( obj )
40 {
41 }
42
43 template<class T>
44 void ListItemF<T>::paintC( QPainter* p, QColorGroup& cg, int c, int w, int align )
45
46 //  QColorGroup colorGrp( cg );
47   if ( myObject )
48   {
49     if ( myObject->color( SUIT_DataObject::Text ).isValid() )
50       cg.setColor( QColorGroup::Text, myObject->color( SUIT_DataObject::Text ) );
51     if ( myObject->color( SUIT_DataObject::Base ).isValid() )
52       cg.setColor( QColorGroup::Base, myObject->color( SUIT_DataObject::Base ) );
53     if ( myObject->color( SUIT_DataObject::Foreground ).isValid() )
54       cg.setColor( QColorGroup::Foreground, myObject->color( SUIT_DataObject::Foreground ) );
55     if ( myObject->color( SUIT_DataObject::Background ).isValid() )
56       cg.setColor( QColorGroup::Background, myObject->color( SUIT_DataObject::Background ) );
57     if ( myObject->color( SUIT_DataObject::Highlight ).isValid() )
58       cg.setColor( QColorGroup::Highlight, myObject->color( SUIT_DataObject::Highlight ) );
59     if ( myObject->color( SUIT_DataObject::HighlightedText ).isValid() )
60       cg.setColor( QColorGroup::HighlightedText, myObject->color( SUIT_DataObject::HighlightedText ) );
61   }
62
63   
64   p->fillRect( 0, 0, w, myT.height(), cg.brush( QColorGroup::Base ) );
65   //int itemW = myT.width( p->fontMetrics(), myT.listView(), c );
66     
67   //myT.paintCell( p, colorGrp, c, itemW,  align );
68 }
69
70 template<class T>
71 void ListItemF<T>::paintFoc( QPainter* p, QColorGroup& cg, const QRect& r )
72 {
73   QRect rect = r;
74   rect.setWidth( myT.width( p->fontMetrics(), myT.listView(), 0 ) );
75   //myT.paintFocus( p, cg, rect );
76 }
77
78 template<class T>
79 void ListItemF<T>::setSel( bool s )
80 {
81   QListView* lv = myT.listView();
82   if ( s && lv && lv->inherits( "OB_ListView" ) )
83   {
84     OB_ListView* objlv = (OB_ListView*)lv;
85     s = s && objlv->isOk( &myT );
86   }
87
88   //myT.setSelected( s );
89 }
90
91 template<class T>
92 void ListItemF<T>::update()
93 {
94   SUIT_DataObject* obj = dataObject();
95   if ( !obj )
96     return;
97
98   myT.setText( 0, obj->name() );
99
100   int aIconW = obj->icon().width();
101   if ( aIconW > 0 )
102   {
103     if ( aIconW > 20 )
104     {
105       QWMatrix aM;
106       double aScale = 20.0 / aIconW;
107       aM.scale( aScale, aScale );
108       myT.setPixmap( 0, obj->icon().xForm( aM ) );
109     }
110     else
111       myT.setPixmap( 0, obj->icon() );
112   }
113
114   myT.setDragEnabled( obj->isDragable() );
115   myT.setDropEnabled( true );
116 }
117
118 /*!
119     Class: OB_ListItem
120     Descr: List view item for OB_Browser.
121 */
122
123 OB_ListItem::OB_ListItem( SUIT_DataObject* obj, QListView* parent )
124 : ListItemF<QListViewItem>( *this, obj ),
125  QListViewItem(parent)
126 {
127         update();
128 }
129
130 OB_ListItem::OB_ListItem( SUIT_DataObject* obj, QListViewItem* parent )
131 : ListItemF<QListViewItem>( *this, obj),
132  QListViewItem(parent)
133 {
134         update();
135 }
136
137 OB_ListItem::OB_ListItem( SUIT_DataObject* obj, QListView* parent, QListViewItem* after )
138 : ListItemF<QListViewItem>( *this, obj),
139 QListViewItem(parent, after )
140 {
141         update();
142 }
143
144 OB_ListItem::OB_ListItem( SUIT_DataObject* obj, QListViewItem* parent, QListViewItem* after )
145 : ListItemF<QListViewItem>( *this,obj),
146 QListViewItem(parent, after )
147 {
148         update();
149 }
150
151 OB_ListItem::~OB_ListItem()
152 {
153 }
154
155 void OB_ListItem::setSelected( bool s )
156 {
157         setSel( s );
158         QListViewItem::setSelected( s );
159 }
160
161 void OB_ListItem::paintFocus( QPainter* p, const QColorGroup& cg, const QRect& r )
162 {
163   QColorGroup col_group( cg );
164         paintFoc( p, col_group, r );
165
166   QRect R( r );
167   if ( listView() && !listView()->allColumnsShowFocus() )
168     R.setWidth( width( p->fontMetrics(), listView(), 0 ) );
169
170   QListViewItem::paintFocus( p, col_group, R );
171 }
172
173 void OB_ListItem::paintCell( QPainter* p, const QColorGroup& cg, int c, int w, int align )
174 {
175   QColorGroup col_group( cg );
176         paintC( p, col_group, c ,w, align );
177
178   int W = w;
179   if ( listView() && !listView()->allColumnsShowFocus() )
180     W = width( p->fontMetrics(), listView(), c );
181
182         QListViewItem::paintCell( p, col_group, c, W, align );
183 }
184
185 int OB_ListItem::RTTI()
186 {
187   return 1000;
188 }
189
190 int OB_ListItem::rtti() const
191 {
192   return RTTI();
193 }
194
195 /*!
196     Class: OB_CheckListItem
197     Descr: Check list view item for OB_Browser.
198 */
199
200 OB_CheckListItem::OB_CheckListItem( SUIT_DataObject* obj, QListView* parent, Type type )
201 : ListItemF<QCheckListItem>( *this, obj),
202 QCheckListItem( parent, "", type )
203 {
204         update();
205 }
206
207 OB_CheckListItem::OB_CheckListItem( SUIT_DataObject* obj, QListViewItem* parent, Type type )
208 : ListItemF<QCheckListItem>( *this, obj),
209 QCheckListItem( parent, "", type )
210 {
211         update();
212 }
213
214 OB_CheckListItem::OB_CheckListItem( SUIT_DataObject* obj, QListView* parent, QListViewItem* after, Type type )
215 : ListItemF<QCheckListItem>( *this, obj),
216 #if defined(QT_VERSION) && QT_VERSION >= 0x030101
217  QCheckListItem( parent, after, "", type )
218 #else
219  QCheckListItem( parent, "", type )
220 #endif
221 {
222         update();
223 }
224
225 OB_CheckListItem::OB_CheckListItem( SUIT_DataObject* obj, QListViewItem* parent, QListViewItem* after, Type type )
226 : ListItemF<QCheckListItem>( *this, obj),
227 #if defined(QT_VERSION) && QT_VERSION >= 0x030101
228  QCheckListItem( parent, after, "", type )
229 #else
230  QCheckListItem( parent, "", type )
231 #endif
232 {
233         update();
234 }
235
236 OB_CheckListItem::~OB_CheckListItem()
237 {
238 }
239
240 void OB_CheckListItem::setSelected( bool s )
241 {
242         setSel( s );
243         QCheckListItem::setSelected( s );
244 }
245
246 void OB_CheckListItem::paintFocus( QPainter* p, const QColorGroup& cg, const QRect& r )
247 {
248   QColorGroup col_group( cg );
249         paintFoc( p, col_group, r );
250
251   QRect R( r );
252   if ( listView() && !listView()->allColumnsShowFocus() )
253     R.setWidth( width( p->fontMetrics(), listView(), 0 ) );
254
255         QCheckListItem::paintFocus( p, col_group, R );
256 }
257
258 void OB_CheckListItem::paintCell( QPainter* p, const QColorGroup& cg, int c, int w, int align )
259 {
260   QColorGroup col_group( cg );
261         paintC( p, col_group, c ,w, align );
262
263   int W = w;
264   if ( listView() && !listView()->allColumnsShowFocus() )
265     W = width( p->fontMetrics(), listView(), c );
266
267   QCheckListItem::paintCell( p, col_group, c, W, align );
268 }
269
270 int OB_CheckListItem::RTTI()
271 {
272   return OB_ListItem::RTTI() + 1;
273 }
274
275 int OB_CheckListItem::rtti() const
276 {
277   return RTTI();
278 }
279
280 void OB_CheckListItem::stateChange( bool on )
281 {
282   QCheckListItem::stateChange( on );
283
284   if ( dataObject() )
285     dataObject()->setOn( on );
286 }