1 // Copyright (C) 2005 OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
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.
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.
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
17 // See http://www.salome-platform.org/
20 #include "QtxListView.h"
23 #include <qpopupmenu.h>
24 #include <qpushbutton.h>
26 static const char* list_xpm[] = {
51 QtxListView::QtxListView( const int state, QWidget* parent, const char* name, WFlags f )
52 : QListView( parent, name, f ),
54 myHeaderState( state )
59 QtxListView::QtxListView( QWidget* parent, const char* name, WFlags f )
60 : QListView( parent, name, f ),
62 myHeaderState( HeaderAuto )
67 void QtxListView::initialize()
69 if ( myHeaderState == HeaderButton )
71 QPixmap p( list_xpm );
73 QPushButton* but = new QPushButton( this );
74 but->setDefault( false );
77 but->setBackgroundPixmap( p );
79 but->setMask( *p.mask() );
82 connect( myButton, SIGNAL( clicked() ), this, SLOT( onButtonClicked() ) );
86 header()->installEventFilter( this );
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() ) );
94 QtxListView::~QtxListView()
98 int QtxListView::addColumn( const QString& label, int width )
100 int res = QListView::addColumn( label, width );
101 for ( int i = myAppropriate.count(); i <= res; i++ )
102 myAppropriate.append( 1 );
107 int QtxListView::addColumn( const QIconSet& iconset, const QString& label, int width )
109 int res = QListView::addColumn( iconset, label, width );
110 for ( int i = myAppropriate.count(); i <= res; i++ )
111 myAppropriate.append( 1 );
116 void QtxListView::removeColumn( int index )
118 QListView::removeColumn( index );
119 if ( index >= 0 && index < (int)myAppropriate.count() )
120 myAppropriate.remove( myAppropriate.at( index ) );
124 bool QtxListView::appropriate( const int index ) const
126 return index >= 0 && index < (int)myAppropriate.count() && myAppropriate[index];
129 void QtxListView::setAppropriate( const int index, const bool on )
131 if ( index < 0 || index >= (int)myAppropriate.count() )
134 myAppropriate[index] = on ? 1 : 0;
137 void QtxListView::resize( int w, int h )
139 QListView::resize( w, h );
143 void QtxListView::show()
149 void QtxListView::resizeContents( int w, int h )
152 if ( myButton && myButton->isVisibleTo( myButton->parentWidget() ) )
154 if ( header()->orientation() == Qt::Horizontal )
155 w += myButton->width();
157 h += myButton->width();
160 QListView::resizeContents( w, h );
165 void QtxListView::show( int ind )
167 setShown( ind, true );
170 void QtxListView::hide( int ind )
172 setShown( ind, false );
175 bool QtxListView::isShown( int ind ) const
177 if ( ind>=0 && ind<header()->count() )
178 return columnWidth( ind ) > 0 || header()->isResizeEnabled( ind );
183 void QtxListView::setShown( int ind, bool sh )
185 if( ind<0 || ind>=header()->count() || isShown( ind )==sh )
188 ColumnData& data = myColumns[ind];
192 bool resizeable = data.resizeable;
193 myColumns.remove( ind );
195 setColumnWidth( ind, w );
196 header()->setResizeEnabled( resizeable, ind );
200 int w = columnWidth( ind );
201 bool r = header()->isResizeEnabled( ind );
202 setColumnWidth( ind, 0 );
203 header()->setResizeEnabled( false, ind );
210 void QtxListView::setColumnWidth( int c, int w )
212 if ( myColumns.contains( c ) )
213 myColumns[c].width = w;
215 QListView::setColumnWidth( c, !myColumns.contains( c ) ? w : 0 );
218 QSize QtxListView::sizeHint() const
220 QSize sz = QListView::sizeHint();
222 if ( myButton && myButton->isVisibleTo( myButton->parentWidget() ) )
223 sz.setWidth( sz.width() + 2 + myButton->width() );
228 QSize QtxListView::minimumSizeHint() const
230 QSize sz = QListView::minimumSizeHint();
232 if ( myButton && myButton->isVisibleTo( myButton->parentWidget() ) )
233 sz.setWidth( sz.width() + 2 + myButton->width() );
238 void QtxListView::onHeaderResized()
240 if ( myHeaderState == HeaderAuto )
243 for ( int i = 0; i < columns(); i++ )
245 if ( !header()->label( i ).isEmpty() ||
246 ( header()->iconSet( i ) && !header()->iconSet( i )->isNull() ) )
256 if ( !myButton || !header()->isVisibleTo( this ) )
259 int lw = lineWidth();
260 int h = header()->size().height() - 1;
261 myButton->setFixedSize( h, h );
263 int x = header()->headerWidth() - header()->offset() + 2;
264 if ( x < header()->width() - h )
265 x = header()->width() - h;
267 if ( myHeaderState == HeaderButton )
269 if ( header()->orientation() == Qt::Horizontal )
270 myButton->move( lw+x, lw );
272 myButton->move( lw, lw+x );
276 void QtxListView::showPopup( const int x, const int y )
279 for ( int i = 0; i < columns(); i++ )
281 if ( appropriate( i ) )
283 int id = myPopup->insertItem( header()->label( i ), i );
284 myPopup->setItemChecked( id, isShown( i ) );
288 if( myPopup->count() )
289 myPopup->exec( mapToGlobal( QPoint( x, y ) ) );
292 void QtxListView::onButtonClicked()
294 if ( myHeaderState != HeaderButton )
297 int x = myButton->x(),
298 y = myButton->y() + myButton->height();
303 void QtxListView::onShowHide( int id )
305 //if ( myHeaderState != HeaderButton )
308 setShown( id, !isShown( id ) );
311 void QtxListView::viewportResizeEvent( QResizeEvent* e )
313 QListView::viewportResizeEvent( e );
317 bool QtxListView::eventFilter( QObject* o, QEvent* e )
319 if( o==header() && e->type()==QEvent::MouseButtonPress )
321 QMouseEvent* me = ( QMouseEvent* )e;
322 if( me->button()==Qt::RightButton )
324 showPopup( me->x()+2, me->y()+2 );
329 return QListView::eventFilter( o, e );