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