Salome HOME
Initial version
[modules/gui.git] / src / ObjBrowser / OB_ListItem.cxx
1 #include "OB_ListItem.h"
2
3 #include <SUIT_DataObject.h>
4
5 #include <qpainter.h>
6 #include <qwmatrix.h>
7
8 /*!
9     Class: ListItem
10     Descr: base template class
11 */
12 template<class T>
13 ListItem<T>::ListItem( SUIT_DataObject* obj, QListView* parent )
14 : T( parent ),
15 myObject( obj )
16 {
17   update();
18 }
19
20 template<class T>
21 ListItem<T>::ListItem( SUIT_DataObject* obj, QListViewItem* parent )
22 : T( parent ),
23 myObject( obj )
24 {
25   update();
26 }
27
28 template<class T>
29 ListItem<T>::ListItem( SUIT_DataObject* obj, QListView* parent, QListViewItem* after )
30 : T( parent, after ),
31 myObject( obj )
32 {
33   update();
34 }
35
36 template<class T>
37 ListItem<T>::ListItem( SUIT_DataObject* obj, QListViewItem* parent, QListViewItem* after )
38 : T( parent, after ),
39 myObject( obj )
40 {
41   update();
42 }
43
44 template<class T>
45 ListItem<T>::ListItem( SUIT_DataObject* obj, QListView* parent, int type )
46 : T( parent, "", (typename T::Type)type ),
47 myObject( obj )
48 {
49   update();
50 }
51
52 template<class T>
53 ListItem<T>::ListItem( SUIT_DataObject* obj, QListViewItem* parent, int type )
54 : T( parent, "", (typename T::Type)type ),
55 myObject( obj )
56 {
57   update();
58 }
59
60 template<class T>
61 ListItem<T>::ListItem( SUIT_DataObject* obj, QListView* parent, QListViewItem* after, int type )
62 #if defined(QT_VERSION) && QT_VERSION >= 0x030101
63 : T( parent, after, "", (typename T::Type)type ),
64 #else
65 : T( parent, "", (typename T::Type)type ),
66 #endif
67 myObject( obj )
68 {
69   update();
70 }
71
72 template<class T>
73 ListItem<T>::ListItem( SUIT_DataObject* obj, QListViewItem* parent, QListViewItem* after, int type )
74 #if defined(QT_VERSION) && QT_VERSION >= 0x030101
75 : T( parent, after, "", (typename T::Type)type ),
76 #else
77 : T( parent, "", (typename T::Type)type ),
78 #endif
79 myObject( obj )
80 {
81   update();
82 }
83
84 template<class T>
85 void ListItem<T>::paintCell( QPainter* p, const QColorGroup& cg, int c, int w, int align )
86
87   QColorGroup colorGrp( cg );
88   if ( myObject )
89   {
90     if ( myObject->color( SUIT_DataObject::Text ).isValid() )
91       colorGrp.setColor( QColorGroup::Text, myObject->color( SUIT_DataObject::Text ) );
92     if ( myObject->color( SUIT_DataObject::Base ).isValid() )
93       colorGrp.setColor( QColorGroup::Base, myObject->color( SUIT_DataObject::Base ) );
94     if ( myObject->color( SUIT_DataObject::Foreground ).isValid() )
95       colorGrp.setColor( QColorGroup::Foreground, myObject->color( SUIT_DataObject::Foreground ) );
96     if ( myObject->color( SUIT_DataObject::Background ).isValid() )
97       colorGrp.setColor( QColorGroup::Background, myObject->color( SUIT_DataObject::Background ) );
98     if ( myObject->color( SUIT_DataObject::Highlight ).isValid() )
99       colorGrp.setColor( QColorGroup::Highlight, myObject->color( SUIT_DataObject::Highlight ) );
100   }
101
102   
103   p->fillRect( 0, 0, w, this->height(), colorGrp.brush( QColorGroup::Base ) );
104   int itemW = width( p->fontMetrics(), this->listView(), c );
105     
106   T::paintCell( p, colorGrp, c, itemW,  align );
107 }
108
109 template<class T>
110 void ListItem<T>::paintFocus( QPainter* p, const QColorGroup& cg, const QRect& r )
111 {
112   QRect rect = r;
113   rect.setWidth( width( p->fontMetrics(), this->listView(), 0 ) );
114   T::paintFocus( p, cg, rect );
115 }
116
117 template<class T>
118 void ListItem<T>::update()
119 {
120   SUIT_DataObject* obj = dataObject();
121   if ( !obj )
122     return;
123
124   setText( 0, obj->name() );
125
126   int aIconW = obj->icon().width();
127   if ( aIconW > 0 )
128   {
129     if ( aIconW > 20 )
130     {
131       QWMatrix aM;
132       double aScale = 20.0 / aIconW;
133       aM.scale( aScale, aScale );
134       setPixmap( 0, obj->icon().xForm( aM ) );
135     }
136     else
137       setPixmap( 0, obj->icon() );
138   }
139
140   this->setDragEnabled( obj->isDragable() );
141   this->setDropEnabled( true );
142 }
143
144 /*!
145     Class: OB_ListItem
146     Descr: List view item for OB_Browser.
147 */
148
149 OB_ListItem::OB_ListItem( SUIT_DataObject* obj, QListView* parent )
150 : ListItem<QListViewItem>( obj, parent )
151 {
152 }
153
154 OB_ListItem::OB_ListItem( SUIT_DataObject* obj, QListViewItem* parent )
155 : ListItem<QListViewItem>( obj, parent )
156 {
157 }
158
159 OB_ListItem::OB_ListItem( SUIT_DataObject* obj, QListView* parent, QListViewItem* after )
160 : ListItem<QListViewItem>( obj, parent, after )
161 {
162 }
163
164 OB_ListItem::OB_ListItem( SUIT_DataObject* obj, QListViewItem* parent, QListViewItem* after )
165 : ListItem<QListViewItem>( obj, parent, after )
166 {
167 }
168
169 OB_ListItem::~OB_ListItem()
170 {
171 }
172
173 int OB_ListItem::RTTI()
174 {
175   return 1000;
176 }
177
178 int OB_ListItem::rtti() const
179 {
180   return RTTI();
181 }
182
183 /*!
184     Class: OB_CheckListItem
185     Descr: Check list view item for OB_Browser.
186 */
187
188 OB_CheckListItem::OB_CheckListItem( SUIT_DataObject* obj, QListView* parent, Type type )
189 : ListItem<QCheckListItem>( obj, parent, type )
190 {
191 }
192
193 OB_CheckListItem::OB_CheckListItem( SUIT_DataObject* obj, QListViewItem* parent, Type type )
194 : ListItem<QCheckListItem>( obj, parent, type )
195 {
196 }
197
198 OB_CheckListItem::OB_CheckListItem( SUIT_DataObject* obj, QListView* parent, QListViewItem* after, Type type )
199 : ListItem<QCheckListItem>( obj, parent, after, type )
200 {
201 }
202
203 OB_CheckListItem::OB_CheckListItem( SUIT_DataObject* obj, QListViewItem* parent, QListViewItem* after, Type type )
204 : ListItem<QCheckListItem>( obj, parent, after, type )
205 {
206 }
207
208 OB_CheckListItem::~OB_CheckListItem()
209 {
210 }
211
212 int OB_CheckListItem::RTTI()
213 {
214   return OB_ListItem::RTTI() + 1;
215 }
216
217 int OB_CheckListItem::rtti() const
218 {
219   return RTTI();
220 }
221
222 void OB_CheckListItem::stateChange( bool on )
223 {
224   QCheckListItem::stateChange( on );
225
226   if ( dataObject() )
227     dataObject()->setOn( on );
228 }