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