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() ) );
68 header()->installEventFilter( this );
71 myPopup = new QPopupMenu( this );
72 connect( myPopup, SIGNAL( activated( int ) ), this, SLOT( onShowHide( int ) ) );
73 connect( header(), SIGNAL( sizeChange( int, int, int ) ), this, SLOT( onHeaderResized() ) );
76 QtxListView::~QtxListView()
80 int QtxListView::addColumn( const QString& label, int width )
82 int res = QListView::addColumn( label, width );
83 for ( int i = myAppropriate.count(); i <= res; i++ )
84 myAppropriate.append( 1 );
89 int QtxListView::addColumn( const QIconSet& iconset, const QString& label, int width )
91 int res = QListView::addColumn( iconset, label, width );
92 for ( int i = myAppropriate.count(); i <= res; i++ )
93 myAppropriate.append( 1 );
98 void QtxListView::removeColumn( int index )
100 QListView::removeColumn( index );
101 if ( index >= 0 && index < (int)myAppropriate.count() )
102 myAppropriate.remove( myAppropriate.at( index ) );
106 bool QtxListView::appropriate( const int index ) const
108 return index >= 0 && index < (int)myAppropriate.count() && myAppropriate[index];
111 void QtxListView::setAppropriate( const int index, const bool on )
113 if ( index < 0 || index >= (int)myAppropriate.count() )
116 myAppropriate[index] = on ? 1 : 0;
119 void QtxListView::resize( int w, int h )
121 QListView::resize( w, h );
125 void QtxListView::show()
131 void QtxListView::resizeContents( int w, int h )
134 if ( myButton && myButton->isVisibleTo( myButton->parentWidget() ) )
136 if ( header()->orientation() == Qt::Horizontal )
137 w += myButton->width();
139 h += myButton->width();
142 QListView::resizeContents( w, h );
147 void QtxListView::show( int ind )
149 setShown( ind, true );
152 void QtxListView::hide( int ind )
154 setShown( ind, false );
157 bool QtxListView::isShown( int ind ) const
159 if ( ind>=0 && ind<header()->count() )
160 return columnWidth( ind ) > 0 || header()->isResizeEnabled( ind );
165 void QtxListView::setShown( int ind, bool sh )
167 if( ind<0 || ind>=header()->count() || isShown( ind )==sh )
170 ColumnData& data = myColumns[ind];
174 bool resizeable = data.resizeable;
175 myColumns.remove( ind );
177 setColumnWidth( ind, w );
178 header()->setResizeEnabled( resizeable, ind );
182 int w = columnWidth( ind );
183 bool r = header()->isResizeEnabled( ind );
184 setColumnWidth( ind, 0 );
185 header()->setResizeEnabled( false, ind );
192 void QtxListView::setColumnWidth( int c, int w )
194 if ( myColumns.contains( c ) )
195 myColumns[c].width = w;
197 QListView::setColumnWidth( c, !myColumns.contains( c ) ? w : 0 );
200 QSize QtxListView::sizeHint() const
202 QSize sz = QListView::sizeHint();
204 if ( myButton && myButton->isVisibleTo( myButton->parentWidget() ) )
205 sz.setWidth( sz.width() + 2 + myButton->width() );
210 QSize QtxListView::minimumSizeHint() const
212 QSize sz = QListView::minimumSizeHint();
214 if ( myButton && myButton->isVisibleTo( myButton->parentWidget() ) )
215 sz.setWidth( sz.width() + 2 + myButton->width() );
220 void QtxListView::onHeaderResized()
222 if ( myHeaderState == HeaderAuto )
225 for ( int i = 0; i < columns(); i++ )
227 if ( !header()->label( i ).isEmpty() ||
228 ( header()->iconSet( i ) && !header()->iconSet( i )->isNull() ) )
238 if ( !myButton || !header()->isVisibleTo( this ) )
241 int lw = lineWidth();
242 int h = header()->size().height() - 1;
243 myButton->setFixedSize( h, h );
245 int x = header()->headerWidth() - header()->offset() + 2;
246 if ( x < header()->width() - h )
247 x = header()->width() - h;
249 if ( myHeaderState == HeaderButton )
251 if ( header()->orientation() == Qt::Horizontal )
252 myButton->move( lw+x, lw );
254 myButton->move( lw, lw+x );
258 void QtxListView::showPopup( const int x, const int y )
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 ) );
270 if( myPopup->count() )
271 myPopup->exec( mapToGlobal( QPoint( x, y ) ) );
274 void QtxListView::onButtonClicked()
276 if ( myHeaderState != HeaderButton )
279 int x = myButton->x(),
280 y = myButton->y() + myButton->height();
285 void QtxListView::onShowHide( int id )
287 //if ( myHeaderState != HeaderButton )
290 setShown( id, !isShown( id ) );
293 void QtxListView::viewportResizeEvent( QResizeEvent* e )
295 QListView::viewportResizeEvent( e );
299 bool QtxListView::eventFilter( QObject* o, QEvent* e )
301 if( o==header() && e->type()==QEvent::MouseButtonPress )
303 QMouseEvent* me = ( QMouseEvent* )e;
304 if( me->button()==Qt::RightButton )
306 showPopup( me->x()+2, me->y()+2 );
311 return QObject::eventFilter( o, e );