]> SALOME platform Git repositories - modules/gui.git/blob - src/ObjBrowser/OB_ListItem.cxx
Salome HOME
1147ae5da529db8c3242f80cfe69f714059766fc
[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   Constructor
33 */
34 template<class T>
35 ListItemF<T>::ListItemF( T* theT, SUIT_DataObject* obj ) :
36 myT( theT ),
37 myObject( obj )
38 {
39 }
40
41 /*!
42   Paints item
43 */
44 template<class T>
45 void ListItemF<T>::paintC( QPainter* p, QColorGroup& cg, int c, int w, int align )
46
47 //  QColorGroup colorGrp( cg );
48   if ( myObject )
49   {
50     if ( myObject->color( SUIT_DataObject::Text ).isValid() )
51       cg.setColor( QColorGroup::Text, myObject->color( SUIT_DataObject::Text ) );
52     if ( myObject->color( SUIT_DataObject::Base ).isValid() )
53       cg.setColor( QColorGroup::Base, myObject->color( SUIT_DataObject::Base ) );
54     if ( myObject->color( SUIT_DataObject::Foreground ).isValid() )
55       cg.setColor( QColorGroup::Foreground, myObject->color( SUIT_DataObject::Foreground ) );
56     if ( myObject->color( SUIT_DataObject::Background ).isValid() )
57       cg.setColor( QColorGroup::Background, myObject->color( SUIT_DataObject::Background ) );
58     if ( myObject->color( SUIT_DataObject::Highlight ).isValid() )
59       cg.setColor( QColorGroup::Highlight, myObject->color( SUIT_DataObject::Highlight ) );
60     if ( myObject->color( SUIT_DataObject::HighlightedText ).isValid() )
61       cg.setColor( QColorGroup::HighlightedText, myObject->color( SUIT_DataObject::HighlightedText ) );
62   }
63
64   
65   p->fillRect( 0, 0, w, myT->height(), cg.brush( QColorGroup::Base ) );
66   //int itemW = myT.width( p->fontMetrics(), myT.listView(), c );
67     
68   //myT.paintCell( p, colorGrp, c, itemW,  align );
69 }
70
71 /*!
72   Paints focused item
73 */
74 template<class T>
75 void ListItemF<T>::paintFoc( QPainter* p, QColorGroup& cg, const QRect& r )
76 {
77   QRect rect = r;
78   rect.setWidth( myT->width( p->fontMetrics(), myT->listView(), 0 ) );
79   //myT.paintFocus( p, cg, rect );
80 }
81
82 /*!
83   Set selection of item
84   \param s - selected state
85 */
86 template<class T>
87 void ListItemF<T>::setSel( bool s )
88 {
89   QListView* lv = myT->listView();
90   if ( s && lv && lv->inherits( "OB_ListView" ) )
91   {
92     OB_ListView* objlv = (OB_ListView*)lv;
93     s = s && objlv->isOk( myT );
94   }
95
96   //myT.setSelected( s );
97 }
98
99 /*!
100   Updates name and pixmap of item
101 */
102 template<class T>
103 void ListItemF<T>::update()
104 {
105   SUIT_DataObject* obj = dataObject();
106   if ( !obj )
107     return;
108
109   QString n = obj->name();
110   if( myT->text( 0 )!=n )
111     myT->setText( 0, n );
112
113   QPixmap p = obj->icon();
114   int aIconW = p.width();
115   if( aIconW > 0 )
116   {
117     if( aIconW > 20 )
118     {
119       QWMatrix aM;
120       double aScale = 20.0 / aIconW;
121       aM.scale( aScale, aScale );
122       myT->setPixmap( 0, p.xForm( aM ) );
123     }
124     else
125       myT->setPixmap( 0, p );
126   }
127
128   myT->setDragEnabled( obj->isDragable() );
129   myT->setDropEnabled( true );
130 }
131
132 /*!
133   Constructor
134 */
135 OB_ListItem::OB_ListItem( SUIT_DataObject* obj, QListView* parent )
136 : ListItemF<QListViewItem>( this, obj ),
137  QListViewItem(parent)
138 {
139   update();
140 }
141
142 /*!
143   Constructor
144 */
145 OB_ListItem::OB_ListItem( SUIT_DataObject* obj, QListViewItem* parent )
146 : ListItemF<QListViewItem>( this, obj ),
147  QListViewItem(parent)
148 {
149   update();
150 }
151
152 /*!
153   Constructor
154 */
155 OB_ListItem::OB_ListItem( SUIT_DataObject* obj, QListView* parent, QListViewItem* after )
156 : ListItemF<QListViewItem>( this, obj),
157 QListViewItem(parent, after )
158 {
159   update();
160 }
161
162 /*!
163   Constructor
164 */
165 OB_ListItem::OB_ListItem( SUIT_DataObject* obj, QListViewItem* parent, QListViewItem* after )
166 : ListItemF<QListViewItem>( this,obj),
167 QListViewItem(parent, after )
168 {
169   update();
170 }
171
172 /*!
173   Destructor
174 */
175 OB_ListItem::~OB_ListItem()
176 {
177 }
178
179 /*!
180   Sets selection state of item
181   \param s - new state
182 */
183 void OB_ListItem::setSelected( bool s )
184 {
185   setSel( s );
186   QListViewItem::setSelected( s );
187 }
188
189 /*!
190   Paints focus
191   \param p - painter
192   \param cg - color group
193   \param r - focus rectangle 
194 */
195 void OB_ListItem::paintFocus( QPainter* p, const QColorGroup& cg, const QRect& r )
196 {
197   QColorGroup col_group( cg );
198         paintFoc( p, col_group, r );
199
200   QRect R( r );
201   if ( listView() && !listView()->allColumnsShowFocus() )
202     R.setWidth( width( p->fontMetrics(), listView(), 0 ) );
203
204   QListViewItem::paintFocus( p, col_group, R );
205 }
206
207 /*!
208   Paints item
209   \param p - painter
210   \param cg - color group
211   \param c - not used
212   \param w - width of item
213   \param align - alignment
214 */
215 void OB_ListItem::paintCell( QPainter* p, const QColorGroup& cg, int c, int w, int align )
216 {
217   QColorGroup col_group( cg );
218         paintC( p, col_group, c ,w, align );
219
220   int W = w;
221   if ( listView() && !listView()->allColumnsShowFocus() )
222     W = width( p->fontMetrics(), listView(), c );
223
224         QListViewItem::paintCell( p, col_group, c, min( W, w ), align );
225 }
226
227 /*!
228   \return custom RTTI info
229 */
230 int OB_ListItem::RTTI()
231 {
232   return 1000;
233 }
234
235 /*!
236   \return custom RTTI info
237 */
238 int OB_ListItem::rtti() const
239 {
240   return RTTI();
241 }
242
243 /*!
244   Sets item text
245   \param column - column index
246   \param text - new text
247 */
248 void OB_ListItem::setText( int column, const QString& text )
249 {
250   QListViewItem::setText( column, text );
251   QFontMetrics fm = listView()->fontMetrics();
252   int necessary = width( fm, listView(), column ),
253       current = listView()->columnWidth( column );
254
255   if( listView()->columnWidthMode( column )==QListView::Maximum && necessary>current )
256     listView()->setColumnWidth( column, necessary );
257 }
258
259 /*!
260   Constructor
261 */
262 OB_CheckListItem::OB_CheckListItem( SUIT_DataObject* obj, QListView* parent, Type type )
263 : ListItemF<QCheckListItem>( this, obj),
264 QCheckListItem( parent, "", type )
265 {
266   update();
267 }
268
269 /*!
270   Constructor
271 */
272 OB_CheckListItem::OB_CheckListItem( SUIT_DataObject* obj, QListViewItem* parent, Type type )
273 : ListItemF<QCheckListItem>( this, obj),
274 QCheckListItem( parent, "", type )
275 {
276   update();
277 }
278
279 /*!
280   Constructor
281 */
282 OB_CheckListItem::OB_CheckListItem( SUIT_DataObject* obj, QListView* parent, QListViewItem* after, Type type )
283 : ListItemF<QCheckListItem>( this, obj),
284 #if defined(QT_VERSION) && QT_VERSION >= 0x030101
285  QCheckListItem( parent, after, "", type )
286 #else
287  QCheckListItem( parent, "", type )
288 #endif
289 {
290   update();
291 }
292
293 /*!
294   Constructor
295 */
296 OB_CheckListItem::OB_CheckListItem( SUIT_DataObject* obj, QListViewItem* parent, QListViewItem* after, Type type )
297 : ListItemF<QCheckListItem>( this, obj),
298 #if defined(QT_VERSION) && QT_VERSION >= 0x030101
299  QCheckListItem( parent, after, "", type )
300 #else
301  QCheckListItem( parent, "", type )
302 #endif
303 {
304   update();
305 }
306
307 /*!
308   Destructor
309 */
310 OB_CheckListItem::~OB_CheckListItem()
311 {
312 }
313
314 /*!
315   Sets selection state of item
316   \param s - new state
317 */
318 void OB_CheckListItem::setSelected( bool s )
319 {
320         setSel( s );
321         QCheckListItem::setSelected( s );
322 }
323
324 /*!
325   Paints focus
326   \param p - painter
327   \param cg - color group
328   \param r - focus rectangle 
329 */
330 void OB_CheckListItem::paintFocus( QPainter* p, const QColorGroup& cg, const QRect& r )
331 {
332   QColorGroup col_group( cg );
333         paintFoc( p, col_group, r );
334
335   QRect R( r );
336   if ( listView() && !listView()->allColumnsShowFocus() )
337     R.setWidth( width( p->fontMetrics(), listView(), 0 ) );
338
339         QCheckListItem::paintFocus( p, col_group, R );
340 }
341
342 /*!
343   Paints item
344   \param p - painter
345   \param cg - color group
346   \param c - not used
347   \param w - width of item
348   \param align - alignment
349 */
350 void OB_CheckListItem::paintCell( QPainter* p, const QColorGroup& cg, int c, int w, int align )
351 {
352   QColorGroup col_group( cg );
353         paintC( p, col_group, c ,w, align );
354
355   int W = w;
356   if ( listView() && !listView()->allColumnsShowFocus() )
357     W = width( p->fontMetrics(), listView(), c );
358
359   QCheckListItem::paintCell( p, col_group, c, min( W, w ), align );
360 }
361
362 /*!
363   \return custom RTTI info
364 */
365 int OB_CheckListItem::RTTI()
366 {
367   return OB_ListItem::RTTI() + 1;
368 }
369
370 /*!
371   \return custom RTTI info
372 */
373 int OB_CheckListItem::rtti() const
374 {
375   return RTTI();
376 }
377
378 /*!
379   SLOT: called if checked state is changed, changes corresponding state of SUIT object
380 */
381 void OB_CheckListItem::stateChange( bool on )
382 {
383   QCheckListItem::stateChange( on );
384
385   if ( dataObject() )
386     dataObject()->setOn( on );
387 }