Salome HOME
49ad75741da39d1611b64a3f09737cc5a0b8f47a
[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   QString n = obj->name();
99   if( myT->text( 0 )!=n )
100     myT->setText( 0, n );
101
102   QPixmap p = obj->icon();
103   int aIconW = p.width();
104   if( aIconW > 0 )
105   {
106     if( aIconW > 20 )
107     {
108       QWMatrix aM;
109       double aScale = 20.0 / aIconW;
110       aM.scale( aScale, aScale );
111       myT->setPixmap( 0, p.xForm( aM ) );
112     }
113     else
114       myT->setPixmap( 0, p );
115   }
116
117   myT->setDragEnabled( obj->isDragable() );
118   myT->setDropEnabled( true );
119 }
120
121 /*!
122     Class: OB_ListItem
123     Descr: List view item for OB_Browser.
124 */
125
126 OB_ListItem::OB_ListItem( SUIT_DataObject* obj, QListView* parent )
127 : ListItemF<QListViewItem>( this, obj ),
128  QListViewItem(parent)
129 {
130   update();
131 }
132
133 OB_ListItem::OB_ListItem( SUIT_DataObject* obj, QListViewItem* parent )
134 : ListItemF<QListViewItem>( this, obj ),
135  QListViewItem(parent)
136 {
137   update();
138 }
139
140 OB_ListItem::OB_ListItem( SUIT_DataObject* obj, QListView* parent, QListViewItem* after )
141 : ListItemF<QListViewItem>( this, obj),
142 QListViewItem(parent, after )
143 {
144   update();
145 }
146
147 OB_ListItem::OB_ListItem( SUIT_DataObject* obj, QListViewItem* parent, QListViewItem* after )
148 : ListItemF<QListViewItem>( this,obj),
149 QListViewItem(parent, after )
150 {
151   update();
152 }
153
154 OB_ListItem::~OB_ListItem()
155 {
156 }
157
158 void OB_ListItem::setSelected( bool s )
159 {
160   setSel( s );
161   QListViewItem::setSelected( s );
162 }
163
164 void OB_ListItem::paintFocus( QPainter* p, const QColorGroup& cg, const QRect& r )
165 {
166   QColorGroup col_group( cg );
167         paintFoc( p, col_group, r );
168
169   QRect R( r );
170   if ( listView() && !listView()->allColumnsShowFocus() )
171     R.setWidth( width( p->fontMetrics(), listView(), 0 ) );
172
173   QListViewItem::paintFocus( p, col_group, R );
174 }
175
176 void OB_ListItem::paintCell( QPainter* p, const QColorGroup& cg, int c, int w, int align )
177 {
178   QColorGroup col_group( cg );
179         paintC( p, col_group, c ,w, align );
180
181   int W = w;
182   if ( listView() && !listView()->allColumnsShowFocus() )
183     W = width( p->fontMetrics(), listView(), c );
184
185         QListViewItem::paintCell( p, col_group, c, W, align );
186 }
187
188 int OB_ListItem::RTTI()
189 {
190   return 1000;
191 }
192
193 int OB_ListItem::rtti() const
194 {
195   return RTTI();
196 }
197
198 void OB_ListItem::setText( int column, const QString& text )
199 {
200   QListViewItem::setText( column, text );
201   QFontMetrics fm = listView()->fontMetrics();
202   int necessary = width( fm, listView(), column ),
203       current = listView()->columnWidth( column );
204
205   if( listView()->columnWidthMode( column )==QListView::Maximum && necessary>current )
206     listView()->setColumnWidth( column, necessary );
207 }
208
209 /*!
210     Class: OB_CheckListItem
211     Descr: Check list view item for OB_Browser.
212 */
213
214 OB_CheckListItem::OB_CheckListItem( SUIT_DataObject* obj, QListView* parent, Type type )
215 : ListItemF<QCheckListItem>( this, obj),
216 QCheckListItem( parent, "", type )
217 {
218   update();
219 }
220
221 OB_CheckListItem::OB_CheckListItem( SUIT_DataObject* obj, QListViewItem* parent, Type type )
222 : ListItemF<QCheckListItem>( this, obj),
223 QCheckListItem( parent, "", type )
224 {
225   update();
226 }
227
228 OB_CheckListItem::OB_CheckListItem( SUIT_DataObject* obj, QListView* parent, QListViewItem* after, Type type )
229 : ListItemF<QCheckListItem>( this, obj),
230 #if defined(QT_VERSION) && QT_VERSION >= 0x030101
231  QCheckListItem( parent, after, "", type )
232 #else
233  QCheckListItem( parent, "", type )
234 #endif
235 {
236   update();
237 }
238
239 OB_CheckListItem::OB_CheckListItem( SUIT_DataObject* obj, QListViewItem* parent, QListViewItem* after, Type type )
240 : ListItemF<QCheckListItem>( this, obj),
241 #if defined(QT_VERSION) && QT_VERSION >= 0x030101
242  QCheckListItem( parent, after, "", type )
243 #else
244  QCheckListItem( parent, "", type )
245 #endif
246 {
247   update();
248 }
249
250 OB_CheckListItem::~OB_CheckListItem()
251 {
252 }
253
254 void OB_CheckListItem::setSelected( bool s )
255 {
256         setSel( s );
257         QCheckListItem::setSelected( s );
258 }
259
260 void OB_CheckListItem::paintFocus( QPainter* p, const QColorGroup& cg, const QRect& r )
261 {
262   QColorGroup col_group( cg );
263         paintFoc( p, col_group, r );
264
265   QRect R( r );
266   if ( listView() && !listView()->allColumnsShowFocus() )
267     R.setWidth( width( p->fontMetrics(), listView(), 0 ) );
268
269         QCheckListItem::paintFocus( p, col_group, R );
270 }
271
272 void OB_CheckListItem::paintCell( QPainter* p, const QColorGroup& cg, int c, int w, int align )
273 {
274   QColorGroup col_group( cg );
275         paintC( p, col_group, c ,w, align );
276
277   int W = w;
278   if ( listView() && !listView()->allColumnsShowFocus() )
279     W = width( p->fontMetrics(), listView(), c );
280
281   QCheckListItem::paintCell( p, col_group, c, W, align );
282 }
283
284 int OB_CheckListItem::RTTI()
285 {
286   return OB_ListItem::RTTI() + 1;
287 }
288
289 int OB_CheckListItem::rtti() const
290 {
291   return RTTI();
292 }
293
294 void OB_CheckListItem::stateChange( bool on )
295 {
296   QCheckListItem::stateChange( on );
297
298   if ( dataObject() )
299     dataObject()->setOn( on );
300 }