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 )
131 if ( myButton && myButton->isVisibleTo( myButton->parentWidget() ) )
133 if ( header()->orientation() == Qt::Horizontal )
134 w += myButton->width();
136 h += myButton->width();
139 QListView::resizeContents( w, h );
144 void QtxListView::show( int ind )
146 setShown( ind, true );
149 void QtxListView::hide( int ind )
151 setShown( ind, false );
154 bool QtxListView::isShown( int ind ) const
156 if ( ind>=0 && ind<header()->count() )
157 return columnWidth( ind ) > 0 || header()->isResizeEnabled( ind );
162 void QtxListView::setShown( int ind, bool sh )
164 if( ind<0 || ind>=header()->count() || isShown( ind )==sh )
167 ColumnData& data = myColumns[ind];
171 bool resizeable = data.resizeable;
172 myColumns.remove( ind );
174 setColumnWidth( ind, w );
175 header()->setResizeEnabled( resizeable, ind );
179 int w = columnWidth( ind );
180 bool r = header()->isResizeEnabled( ind );
181 setColumnWidth( ind, 0 );
182 header()->setResizeEnabled( false, ind );
189 void QtxListView::setColumnWidth( int c, int w )
191 if ( myColumns.contains( c ) )
192 myColumns[c].width = w;
194 QListView::setColumnWidth( c, !myColumns.contains( c ) ? w : 0 );
197 QSize QtxListView::sizeHint() const
199 QSize sz = QListView::sizeHint();
201 if ( myButton && myButton->isVisibleTo( myButton->parentWidget() ) )
202 sz.setWidth( sz.width() + 2 + myButton->width() );
207 QSize QtxListView::minimumSizeHint() const
209 QSize sz = QListView::minimumSizeHint();
211 if ( myButton && myButton->isVisibleTo( myButton->parentWidget() ) )
212 sz.setWidth( sz.width() + 2 + myButton->width() );
217 void QtxListView::onHeaderResized()
219 if ( myHeaderState == HeaderAuto )
222 for ( int i = 0; i < columns(); i++ )
224 if ( !header()->label( i ).isEmpty() ||
225 ( header()->iconSet( i ) && !header()->iconSet( i )->isNull() ) )
235 if ( !myButton || !header()->isVisibleTo( this ) )
238 int lw = lineWidth();
239 int h = header()->size().height() - 1;
240 myButton->setFixedSize( h, h );
242 int x = header()->headerWidth() - header()->offset() + 2;
243 if ( x < header()->width() - h )
244 x = header()->width() - h;
246 if ( myHeaderState == HeaderButton )
248 if ( header()->orientation() == Qt::Horizontal )
249 myButton->move( lw+x, lw );
251 myButton->move( lw, lw+x );
255 void QtxListView::onButtonClicked()
257 if ( myHeaderState != HeaderButton )
261 for ( int i = 0; i < columns(); i++ )
263 if ( appropriate( i ) )
265 int id = myPopup->insertItem( header()->label( i ), i );
266 myPopup->setItemChecked( id, isShown( i ) );
269 int x = myButton->x(),
270 y = myButton->y() + myButton->height();
271 if ( myPopup->count() )
272 myPopup->exec( mapToGlobal( QPoint( x, y ) ) );
275 void QtxListView::onShowHide( int id )
277 if ( myHeaderState != HeaderButton )
280 setShown( id, !isShown( id ) );
283 void QtxListView::viewportResizeEvent( QResizeEvent* e )
285 QListView::viewportResizeEvent( e );