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