]> SALOME platform Git repositories - modules/gui.git/blob - src/Qtx/QtxListView.cxx
Salome HOME
2ae608b74df0661611d80c4ce2b82120d9c2d87c
[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   QListView::setColumnWidth( c, !myColumns.contains( c ) ? w : 0 );
190 }
191
192 QSize QtxListView::sizeHint() const
193 {
194   QSize sz = QListView::sizeHint();
195
196   if ( myButton && myButton->isVisibleTo( myButton->parentWidget() ) )
197     sz.setWidth( sz.width() + 2 + myButton->width() );
198
199   return sz;
200 }
201
202 QSize QtxListView::minimumSizeHint() const
203 {
204   QSize sz = QListView::minimumSizeHint();
205
206   if ( myButton && myButton->isVisibleTo( myButton->parentWidget() ) )
207     sz.setWidth( sz.width() + 2 + myButton->width() );
208
209   return sz;
210 }
211
212 void QtxListView::onHeaderResized()
213 {
214   if ( myHeaderState == HeaderAuto )
215   {
216     int c = 0;
217     for ( int i = 0; i < columns(); i++ )
218     {
219       if ( !header()->label( i ).isEmpty() ||
220            ( header()->iconSet( i ) && !header()->iconSet( i )->isNull() ) )
221         c++;
222     }
223
224     if ( c > 1 )
225       header()->show();
226     else
227       header()->hide();
228   }
229
230   if ( !myButton || !header()->isVisibleTo( this ) )
231     return;
232
233   int lw = lineWidth();
234   int h = header()->size().height() - 1;
235   myButton->setFixedSize( h, h );
236
237   int x = header()->headerWidth() - header()->offset() + 2;
238   if ( x < header()->width() - h )
239     x = header()->width() - h;
240
241   if ( myHeaderState == HeaderButton )
242   {
243     if ( header()->orientation() == Qt::Horizontal )
244       myButton->move( lw+x, lw );
245     else
246       myButton->move( lw, lw+x );
247   }
248 }
249
250 void QtxListView::onButtonClicked()
251 {
252   if ( myHeaderState != HeaderButton )
253     return;
254
255   myPopup->clear();
256   for ( int i = 0; i < columns(); i++ )
257   {
258     if ( appropriate( i ) )
259     {
260       int id = myPopup->insertItem( header()->label( i ), i );
261       myPopup->setItemChecked( id, isShown( i ) );
262     }
263   }
264   int x = myButton->x(),
265       y = myButton->y() + myButton->height();
266   if ( myPopup->count() )
267     myPopup->exec( mapToGlobal( QPoint( x, y ) ) );
268 }
269
270 void QtxListView::onShowHide( int id )
271 {
272   if ( myHeaderState != HeaderButton )
273     return;
274
275   setShown( id, !isShown( id ) );
276 }
277
278 void QtxListView::viewportResizeEvent( QResizeEvent* e )
279 {
280   QListView::viewportResizeEvent( e );
281   onHeaderResized();
282 }