2 #include "QtxListView.h"
5 #include <qpopupmenu.h>
6 #include <qpushbutton.h>
8 static const char* list_xpm[] = {
33 QtxListView::QtxListView( const int state, QWidget* parent, const char* name, WFlags f )
34 : QListView( parent, name, f ),
36 myHeaderState( state )
41 QtxListView::QtxListView( QWidget* parent, const char* name, WFlags f )
42 : QListView( parent, name, f ),
44 myHeaderState( HeaderAuto )
49 void QtxListView::initialize()
51 if ( myHeaderState == HeaderButton )
53 QPixmap p( list_xpm );
55 QPushButton* but = new QPushButton( this );
56 but->setDefault( false );
59 but->setBackgroundPixmap( p );
61 but->setMask( *p.mask() );
64 connect( myButton, SIGNAL( clicked() ), this, SLOT( onButtonClicked() ) );
66 myPopup = new QPopupMenu( this );
67 connect( myPopup, SIGNAL( activated( int ) ), this, SLOT( onShowHide( int ) ) );
70 connect( header(), SIGNAL( sizeChange( int, int, int ) ), this, SLOT( onHeaderResized() ) );
73 QtxListView::~QtxListView()
77 int QtxListView::addColumn( const QString& label, int width )
79 int res = QListView::addColumn( label, width );
84 int QtxListView::addColumn( const QIconSet& iconset, const QString& label, int width )
86 int res = QListView::addColumn( iconset, label, width );
91 void QtxListView::removeColumn( int index )
93 QListView::removeColumn( index );
97 void QtxListView::resize( int w, int h )
99 QListView::resize( w, h );
103 void QtxListView::show()
109 void QtxListView::resizeContents( int w, int h )
111 QListView::resizeContents( w, h );
115 void QtxListView::show( int ind )
117 setShown( ind, true );
120 void QtxListView::hide( int ind )
122 setShown( ind, false );
125 bool QtxListView::isShown( int ind ) const
127 if( ind>=0 && ind<header()->count() )
128 return columnWidth( ind ) > 0 || header()->isResizeEnabled( ind );
133 void QtxListView::setShown( int ind, bool sh )
135 if( ind<0 || ind>=header()->count() || isShown( ind )==sh )
138 ColumnData& data = myColumns[ ind ];
141 setColumnWidth( ind, data.width );
142 header()->setResizeEnabled( data.resizeable, ind );
143 myColumns.remove( ind );
147 data.width = columnWidth( ind );
148 data.resizeable = header()->isResizeEnabled( ind );
149 setColumnWidth( ind, 0 );
150 header()->setResizeEnabled( false, ind );
155 QSize QtxListView::sizeHint() const
157 QSize sz = QListView::sizeHint();
159 if ( myButton && myButton->isVisibleTo( myButton->parentWidget() ) )
160 sz.setWidth( sz.width() + 2 + myButton->width() );
165 QSize QtxListView::minimumSizeHint() const
167 QSize sz = QListView::minimumSizeHint();
169 if ( myButton && myButton->isVisibleTo( myButton->parentWidget() ) )
170 sz.setWidth( sz.width() + 2 + myButton->width() );
175 void QtxListView::onHeaderResized()
177 if ( myHeaderState == HeaderAuto )
180 for ( int i = 0; i < columns(); i++ )
182 if ( !header()->label( i ).isEmpty() ||
183 ( header()->iconSet( i ) && !header()->iconSet( i )->isNull() ) )
193 if ( !myButton || !header()->isVisibleTo( this ) )
196 int lw = lineWidth();
197 int h = header()->size().height() - 1;
198 myButton->setFixedSize( h, h );
200 int x = header()->headerWidth() - header()->offset() + 2;
201 if ( x < header()->width() - h )
202 x = header()->width() - h;
204 if ( myHeaderState == HeaderButton )
206 if( header()->orientation() == Qt::Horizontal )
207 myButton->move( lw+x, lw );
209 myButton->move( lw, lw+x );
213 void QtxListView::onButtonClicked()
215 if ( myHeaderState != HeaderButton )
219 for( int i=0, n=header()->count(); i<n; i++ )
221 int id = myPopup->insertItem( header()->label( i ) );
222 myPopup->setItemChecked( id, isShown( i ) );
224 int x = myButton->x(),
225 y = myButton->y() + myButton->height();
226 myPopup->exec( mapToGlobal( QPoint( x, y ) ) );
229 void QtxListView::onShowHide( int id )
231 if ( myHeaderState != HeaderButton )
234 int ind = myPopup->indexOf( id );
235 setShown( ind, !isShown( ind ) );
238 void QtxListView::viewportResizeEvent( QResizeEvent* e )
240 QListView::viewportResizeEvent( e );