Salome HOME
Columns in object browser
[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     if ( myObject->color( SUIT_DataObject::HighlightedText ).isValid() )
103       colorGrp.setColor( QColorGroup::HighlightedText, myObject->color( SUIT_DataObject::HighlightedText ) );
104   }
105
106   
107   p->fillRect( 0, 0, w, this->height(), colorGrp.brush( QColorGroup::Base ) );
108   int itemW = width( p->fontMetrics(), this->listView(), c );
109     
110   T::paintCell( p, colorGrp, c, itemW,  align );
111 }
112
113 template<class T>
114 void ListItem<T>::paintFocus( QPainter* p, const QColorGroup& cg, const QRect& r )
115 {
116   QRect rect = r;
117   rect.setWidth( width( p->fontMetrics(), this->listView(), 0 ) );
118   T::paintFocus( p, cg, rect );
119 }
120
121 template<class T>
122 void ListItem<T>::setSelected( bool s )
123 {
124   QListView* lv = T::listView();
125   if ( s && lv && lv->inherits( "OB_ListView" ) )
126   {
127     OB_ListView* objlv = (OB_ListView*)lv;
128     s = s && objlv->isOk( this );
129   }
130
131   QListViewItem::setSelected( s );
132 }
133
134 template<class T>
135 void ListItem<T>::update()
136 {
137   SUIT_DataObject* obj = dataObject();
138   if ( !obj )
139     return;
140
141   setText( 0, obj->name() );
142
143   int aIconW = obj->icon().width();
144   if ( aIconW > 0 )
145   {
146     if ( aIconW > 20 )
147     {
148       QWMatrix aM;
149       double aScale = 20.0 / aIconW;
150       aM.scale( aScale, aScale );
151       setPixmap( 0, obj->icon().xForm( aM ) );
152     }
153     else
154       setPixmap( 0, obj->icon() );
155   }
156
157   this->setDragEnabled( obj->isDragable() );
158   this->setDropEnabled( true );
159 }
160
161 /*!
162     Class: OB_ListItem
163     Descr: List view item for OB_Browser.
164 */
165
166 OB_ListItem::OB_ListItem( SUIT_DataObject* obj, QListView* parent )
167 : ListItem<QListViewItem>( obj, parent )
168 {
169 }
170
171 OB_ListItem::OB_ListItem( SUIT_DataObject* obj, QListViewItem* parent )
172 : ListItem<QListViewItem>( obj, parent )
173 {
174 }
175
176 OB_ListItem::OB_ListItem( SUIT_DataObject* obj, QListView* parent, QListViewItem* after )
177 : ListItem<QListViewItem>( obj, parent, after )
178 {
179 }
180
181 OB_ListItem::OB_ListItem( SUIT_DataObject* obj, QListViewItem* parent, QListViewItem* after )
182 : ListItem<QListViewItem>( obj, parent, after )
183 {
184 }
185
186 OB_ListItem::~OB_ListItem()
187 {
188 }
189
190 int OB_ListItem::RTTI()
191 {
192   return 1000;
193 }
194
195 int OB_ListItem::rtti() const
196 {
197   return RTTI();
198 }
199
200 /*!
201     Class: OB_CheckListItem
202     Descr: Check list view item for OB_Browser.
203 */
204
205 OB_CheckListItem::OB_CheckListItem( SUIT_DataObject* obj, QListView* parent, Type type )
206 : ListItem<QCheckListItem>( obj, parent, type )
207 {
208 }
209
210 OB_CheckListItem::OB_CheckListItem( SUIT_DataObject* obj, QListViewItem* parent, Type type )
211 : ListItem<QCheckListItem>( obj, parent, type )
212 {
213 }
214
215 OB_CheckListItem::OB_CheckListItem( SUIT_DataObject* obj, QListView* parent, QListViewItem* after, Type type )
216 : ListItem<QCheckListItem>( obj, parent, after, type )
217 {
218 }
219
220 OB_CheckListItem::OB_CheckListItem( SUIT_DataObject* obj, QListViewItem* parent, QListViewItem* after, Type type )
221 : ListItem<QCheckListItem>( obj, parent, after, type )
222 {
223 }
224
225 OB_CheckListItem::~OB_CheckListItem()
226 {
227 }
228
229 int OB_CheckListItem::RTTI()
230 {
231   return OB_ListItem::RTTI() + 1;
232 }
233
234 int OB_CheckListItem::rtti() const
235 {
236   return RTTI();
237 }
238
239 void OB_CheckListItem::stateChange( bool on )
240 {
241   QCheckListItem::stateChange( on );
242
243   if ( dataObject() )
244     dataObject()->setOn( on );
245 }