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 );
80 for ( int i = myAppropriate.count(); i <= res; i++ )
81 myAppropriate.append( 1 );
86 int QtxListView::addColumn( const QIconSet& iconset, const QString& label, int width )
88 int res = QListView::addColumn( iconset, label, width );
89 for ( int i = myAppropriate.count(); i <= res; i++ )
90 myAppropriate.append( 1 );
95 void QtxListView::removeColumn( int index )
97 QListView::removeColumn( index );
98 if ( index >= 0 && index < (int)myAppropriate.count() )
99 myAppropriate.remove( myAppropriate.at( index ) );
103 bool QtxListView::appropriate( const int index ) const
105 return index >= 0 && index < (int)myAppropriate.count() && myAppropriate[index];
108 void QtxListView::setAppropriate( const int index, const bool on )
110 if ( index < 0 || index >= (int)myAppropriate.count() )
113 myAppropriate[index] = on ? 1 : 0;
116 void QtxListView::resize( int w, int h )
118 QListView::resize( w, h );
122 void QtxListView::show()
128 void QtxListView::resizeContents( int w, int h )
130 QListView::resizeContents( w, h );
134 void QtxListView::show( int ind )
136 setShown( ind, true );
139 void QtxListView::hide( int ind )
141 setShown( ind, false );
144 bool QtxListView::isShown( int ind ) const
146 if( ind>=0 && ind<header()->count() )
147 return columnWidth( ind ) > 0 || header()->isResizeEnabled( ind );
152 void QtxListView::setShown( int ind, bool sh )
154 if( ind<0 || ind>=header()->count() || isShown( ind )==sh )
157 ColumnData& data = myColumns[ ind ];
160 setColumnWidth( ind, data.width );
161 header()->setResizeEnabled( data.resizeable, ind );
162 myColumns.remove( ind );
166 data.width = columnWidth( ind );
167 data.resizeable = header()->isResizeEnabled( ind );
168 setColumnWidth( ind, 0 );
169 header()->setResizeEnabled( false, ind );
174 QSize QtxListView::sizeHint() const
176 QSize sz = QListView::sizeHint();
178 if ( myButton && myButton->isVisibleTo( myButton->parentWidget() ) )
179 sz.setWidth( sz.width() + 2 + myButton->width() );
184 QSize QtxListView::minimumSizeHint() const
186 QSize sz = QListView::minimumSizeHint();
188 if ( myButton && myButton->isVisibleTo( myButton->parentWidget() ) )
189 sz.setWidth( sz.width() + 2 + myButton->width() );
194 void QtxListView::onHeaderResized()
196 if ( myHeaderState == HeaderAuto )
199 for ( int i = 0; i < columns(); i++ )
201 if ( !header()->label( i ).isEmpty() ||
202 ( header()->iconSet( i ) && !header()->iconSet( i )->isNull() ) )
212 if ( !myButton || !header()->isVisibleTo( this ) )
215 int lw = lineWidth();
216 int h = header()->size().height() - 1;
217 myButton->setFixedSize( h, h );
219 int x = header()->headerWidth() - header()->offset() + 2;
220 if ( x < header()->width() - h )
221 x = header()->width() - h;
223 if ( myHeaderState == HeaderButton )
225 if( header()->orientation() == Qt::Horizontal )
226 myButton->move( lw+x, lw );
228 myButton->move( lw, lw+x );
232 void QtxListView::onButtonClicked()
234 if ( myHeaderState != HeaderButton )
238 for ( int i = 0; i < columns(); i++ )
240 if ( appropriate( i ) )
242 int id = myPopup->insertItem( header()->label( i ), i );
243 myPopup->setItemChecked( id, isShown( i ) );
246 int x = myButton->x(),
247 y = myButton->y() + myButton->height();
248 if ( myPopup->count() )
249 myPopup->exec( mapToGlobal( QPoint( x, y ) ) );
252 void QtxListView::onShowHide( int id )
254 if ( myHeaderState != HeaderButton )
257 setShown( id, !isShown( id ) );
260 void QtxListView::viewportResizeEvent( QResizeEvent* e )
262 QListView::viewportResizeEvent( e );