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 data.width = columnWidth( ind );
180 data.resizeable = header()->isResizeEnabled( ind );
181 setColumnWidth( ind, 0 );
182 header()->setResizeEnabled( false, ind );
187 void QtxListView::setColumnWidth( int c, int w )
189 QListView::setColumnWidth( c, !myColumns.contains( c ) ? w : 0 );
192 QSize QtxListView::sizeHint() const
194 QSize sz = QListView::sizeHint();
196 if ( myButton && myButton->isVisibleTo( myButton->parentWidget() ) )
197 sz.setWidth( sz.width() + 2 + myButton->width() );
202 QSize QtxListView::minimumSizeHint() const
204 QSize sz = QListView::minimumSizeHint();
206 if ( myButton && myButton->isVisibleTo( myButton->parentWidget() ) )
207 sz.setWidth( sz.width() + 2 + myButton->width() );
212 void QtxListView::onHeaderResized()
214 if ( myHeaderState == HeaderAuto )
217 for ( int i = 0; i < columns(); i++ )
219 if ( !header()->label( i ).isEmpty() ||
220 ( header()->iconSet( i ) && !header()->iconSet( i )->isNull() ) )
230 if ( !myButton || !header()->isVisibleTo( this ) )
233 int lw = lineWidth();
234 int h = header()->size().height() - 1;
235 myButton->setFixedSize( h, h );
237 int x = header()->headerWidth() - header()->offset() + 2;
238 if ( x < header()->width() - h )
239 x = header()->width() - h;
241 if ( myHeaderState == HeaderButton )
243 if ( header()->orientation() == Qt::Horizontal )
244 myButton->move( lw+x, lw );
246 myButton->move( lw, lw+x );
250 void QtxListView::onButtonClicked()
252 if ( myHeaderState != HeaderButton )
256 for ( int i = 0; i < columns(); i++ )
258 if ( appropriate( i ) )
260 int id = myPopup->insertItem( header()->label( i ), i );
261 myPopup->setItemChecked( id, isShown( i ) );
264 int x = myButton->x(),
265 y = myButton->y() + myButton->height();
266 if ( myPopup->count() )
267 myPopup->exec( mapToGlobal( QPoint( x, y ) ) );
270 void QtxListView::onShowHide( int id )
272 if ( myHeaderState != HeaderButton )
275 setShown( id, !isShown( id ) );
278 void QtxListView::viewportResizeEvent( QResizeEvent* e )
280 QListView::viewportResizeEvent( e );