Salome HOME
Columns in object browser
[modules/gui.git] / src / Qtx / QtxListView.cxx
1
2 #include "QtxListView.h"
3
4 #include <qheader.h>
5 #include <qpopupmenu.h>
6 #include <qpushbutton.h>
7
8 static const char* list_xpm[] = {
9 "16 16 6 1",
10 ". c None",
11 "a c #E3E9EB",
12 "b c #798391",
13 "c c #EBEBEB",
14 "d c #ABB4BE",
15 "e c #030E1F",
16 "................",
17 "................",
18 "................",
19 "...aaaaaaaaaa...",
20 "..abbbbbbbbbbe..",
21 "..abecbecbecbe..",
22 "..abbbbbbbbbbe..",
23 "..abecbecbecbe..",
24 "..abecaaaaaaaa..",
25 "..abeccdbbbbbb..",
26 "..abecccdbbbbe..",
27 "..abbbbe.dbbe...",
28 "...eeeee..de....",
29 "................",
30 "................",
31 "................" };
32
33 QtxListView::QtxListView( const int state, QWidget* parent, const char* name, WFlags f )
34 : QListView( parent, name, f ),
35 myButton( 0 ),
36 myHeaderState( state )
37 {
38   initialize();
39 }
40
41 QtxListView::QtxListView( QWidget* parent, const char* name, WFlags f )
42 : QListView( parent, name, f ),
43 myButton( 0 ),
44 myHeaderState( HeaderAuto )
45 {
46   initialize();
47 }
48
49 void QtxListView::initialize()
50 {
51   if ( myHeaderState == HeaderButton )
52   {
53     QPixmap p( list_xpm );
54
55     QPushButton* but = new QPushButton( this );
56     but->setDefault( false );
57     but->setFlat( true );
58     but->setIconSet( p );
59     but->setBackgroundPixmap( p );
60     if ( p.mask() )
61             but->setMask( *p.mask() );
62     myButton = but;
63
64     connect( myButton, SIGNAL( clicked() ), this, SLOT( onButtonClicked() ) );
65
66     myPopup = new QPopupMenu( this );
67     connect( myPopup, SIGNAL( activated( int ) ), this, SLOT( onShowHide( int ) ) );
68   }
69
70   connect( header(), SIGNAL( sizeChange( int, int, int ) ), this, SLOT( onHeaderResized() ) );
71 }
72
73 QtxListView::~QtxListView()
74 {
75 }
76
77 int QtxListView::addColumn( const QString& label, int width )
78 {
79   int res = QListView::addColumn( label, width );
80   for ( int i = myAppropriate.count(); i <= res; i++ )
81     myAppropriate.append( 1 );
82   onHeaderResized();
83   return res;
84 }
85
86 int QtxListView::addColumn( const QIconSet& iconset, const QString& label, int width ) 
87 {
88   int res = QListView::addColumn( iconset, label, width );
89   for ( int i = myAppropriate.count(); i <= res; i++ )
90     myAppropriate.append( 1 );
91   onHeaderResized();
92   return res;
93 }
94
95 void QtxListView::removeColumn( int index ) 
96 {
97   QListView::removeColumn( index );
98   if ( index >= 0 && index < (int)myAppropriate.count() )
99     myAppropriate.remove( myAppropriate.at( index ) );
100   onHeaderResized();
101 }
102
103 bool QtxListView::appropriate( const int index ) const
104 {
105   return index >= 0 && index < (int)myAppropriate.count() && myAppropriate[index];
106 }
107
108 void QtxListView::setAppropriate( const int index, const bool on )
109 {
110   if ( index < 0 || index >= (int)myAppropriate.count() )
111     return;
112
113   myAppropriate[index] = on ? 1 : 0;
114 }
115
116 void QtxListView::resize( int w, int h )
117 {
118   QListView::resize( w, h );
119   onHeaderResized();
120 }
121
122 void QtxListView::show()
123 {
124   QListView::show();
125   onHeaderResized();
126 }
127
128 void QtxListView::resizeContents( int w, int h )
129 {
130 /*
131   if ( myButton && myButton->isVisibleTo( myButton->parentWidget() ) )
132   {
133     if ( header()->orientation() == Qt::Horizontal )
134       w += myButton->width();
135     else
136       h += myButton->width();
137   }
138 */
139   QListView::resizeContents( w, h );
140
141   onHeaderResized();
142 }
143
144 void QtxListView::show( int ind )
145 {
146   setShown( ind, true );
147 }
148
149 void QtxListView::hide( int ind )
150 {
151   setShown( ind, false );
152 }
153
154 bool QtxListView::isShown( int ind ) const
155 {
156   if ( ind>=0 && ind<header()->count() )
157     return columnWidth( ind ) > 0 || header()->isResizeEnabled( ind );
158   else
159     return false;
160 }
161
162 void QtxListView::setShown( int ind, bool sh )
163 {
164   if( ind<0 || ind>=header()->count() || isShown( ind )==sh )
165     return;
166
167   ColumnData& data = myColumns[ind];
168   if ( sh )
169   {
170     int w = data.width;
171     bool resizeable = data.resizeable;
172     myColumns.remove( ind );
173
174     setColumnWidth( ind, w );
175     header()->setResizeEnabled( resizeable, ind );
176   }
177   else
178   {
179     data.width = columnWidth( ind );
180     data.resizeable = header()->isResizeEnabled( ind );
181     setColumnWidth( ind, 0 );
182     header()->setResizeEnabled( false, ind );
183   }
184   updateContents();
185 }
186
187 void QtxListView::setColumnWidth( int c, int w )
188 {
189   if ( myColumns.contains( c ) )
190     myColumns[c].width = w;
191
192   QListView::setColumnWidth( c, !myColumns.contains( c ) ? w : 0 );
193 }
194
195 QSize QtxListView::sizeHint() const
196 {
197   QSize sz = QListView::sizeHint();
198
199   if ( myButton && myButton->isVisibleTo( myButton->parentWidget() ) )
200     sz.setWidth( sz.width() + 2 + myButton->width() );
201
202   return sz;
203 }
204
205 QSize QtxListView::minimumSizeHint() const
206 {
207   QSize sz = QListView::minimumSizeHint();
208
209   if ( myButton && myButton->isVisibleTo( myButton->parentWidget() ) )
210     sz.setWidth( sz.width() + 2 + myButton->width() );
211
212   return sz;
213 }
214
215 void QtxListView::onHeaderResized()
216 {
217   if ( myHeaderState == HeaderAuto )
218   {
219     int c = 0;
220     for ( int i = 0; i < columns(); i++ )
221     {
222       if ( !header()->label( i ).isEmpty() ||
223            ( header()->iconSet( i ) && !header()->iconSet( i )->isNull() ) )
224         c++;
225     }
226
227     if ( c > 1 )
228       header()->show();
229     else
230       header()->hide();
231   }
232
233   if ( !myButton || !header()->isVisibleTo( this ) )
234     return;
235
236   int lw = lineWidth();
237   int h = header()->size().height() - 1;
238   myButton->setFixedSize( h, h );
239
240   int x = header()->headerWidth() - header()->offset() + 2;
241   if ( x < header()->width() - h )
242     x = header()->width() - h;
243
244   if ( myHeaderState == HeaderButton )
245   {
246     if ( header()->orientation() == Qt::Horizontal )
247       myButton->move( lw+x, lw );
248     else
249       myButton->move( lw, lw+x );
250   }
251 }
252
253 void QtxListView::onButtonClicked()
254 {
255   if ( myHeaderState != HeaderButton )
256     return;
257
258   myPopup->clear();
259   for ( int i = 0; i < columns(); i++ )
260   {
261     if ( appropriate( i ) )
262     {
263       int id = myPopup->insertItem( header()->label( i ), i );
264       myPopup->setItemChecked( id, isShown( i ) );
265     }
266   }
267   int x = myButton->x(),
268       y = myButton->y() + myButton->height();
269   if ( myPopup->count() )
270     myPopup->exec( mapToGlobal( QPoint( x, y ) ) );
271 }
272
273 void QtxListView::onShowHide( int id )
274 {
275   if ( myHeaderState != HeaderButton )
276     return;
277
278   setShown( id, !isShown( id ) );
279 }
280
281 void QtxListView::viewportResizeEvent( QResizeEvent* e )
282 {
283   QListView::viewportResizeEvent( e );
284   onHeaderResized();
285 }