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