1 // Copyright (C) 2005 OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
8 // This library is distributed in the hope that it will be useful
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/
20 #include "QtxListView.h"
23 #include <qpopupmenu.h>
24 #include <qpushbutton.h>
26 static const char* list_xpm[] = {
54 QtxListView::QtxListView( const int state, QWidget* parent, const char* name, WFlags f )
55 : QListView( parent, name, f ),
57 myHeaderState( state )
65 QtxListView::QtxListView( QWidget* parent, const char* name, WFlags f )
66 : QListView( parent, name, f ),
68 myHeaderState( HeaderAuto )
76 void QtxListView::initialize()
78 if ( myHeaderState == HeaderButton )
80 QPixmap p( list_xpm );
82 QPushButton* but = new QPushButton( this );
83 but->setDefault( false );
86 but->setBackgroundPixmap( p );
88 but->setMask( *p.mask() );
91 connect( myButton, SIGNAL( clicked() ), this, SLOT( onButtonClicked() ) );
95 header()->installEventFilter( this );
98 myPopup = new QPopupMenu( this );
99 connect( myPopup, SIGNAL( activated( int ) ), this, SLOT( onShowHide( int ) ) );
100 connect( header(), SIGNAL( sizeChange( int, int, int ) ), this, SLOT( onHeaderResized() ) );
106 QtxListView::~QtxListView()
112 \param label - column title
113 \param width - column width
115 int QtxListView::addColumn( const QString& label, int width )
117 int res = QListView::addColumn( label, width );
118 for ( int i = myAppropriate.count(); i <= res; i++ )
119 myAppropriate.append( 1 );
126 \param iconset - column icon
127 \param label - column title
128 \param width - column width
130 int QtxListView::addColumn( const QIconSet& iconset, const QString& label, int width )
132 int res = QListView::addColumn( iconset, label, width );
133 for ( int i = myAppropriate.count(); i <= res; i++ )
134 myAppropriate.append( 1 );
141 \param index - column index
143 void QtxListView::removeColumn( int index )
145 QListView::removeColumn( index );
146 if ( index >= 0 && index < (int)myAppropriate.count() )
147 myAppropriate.remove( myAppropriate.at( index ) );
152 \return true if column is situated in popup for show/hide columns
154 bool QtxListView::appropriate( const int index ) const
156 return index >= 0 && index < (int)myAppropriate.count() && myAppropriate[index];
160 Sets appropriate state: whether column is situated in popup for show/hide columns
161 \param index - column index
162 \param on - new state
164 void QtxListView::setAppropriate( const int index, const bool on )
166 if ( index < 0 || index >= (int)myAppropriate.count() )
169 myAppropriate[index] = on ? 1 : 0;
173 Resizes list view and header
175 void QtxListView::resize( int w, int h )
177 QListView::resize( w, h );
184 void QtxListView::show()
191 Update on resize contents
193 void QtxListView::resizeContents( int w, int h )
196 if ( myButton && myButton->isVisibleTo( myButton->parentWidget() ) )
198 if ( header()->orientation() == Qt::Horizontal )
199 w += myButton->width();
201 h += myButton->width();
204 QListView::resizeContents( w, h );
211 \param ind - column index
213 void QtxListView::show( int ind )
215 setShown( ind, true );
220 \param ind - column index
222 void QtxListView::hide( int ind )
224 setShown( ind, false );
228 \return true if column is shown
229 \param ind - column index
231 bool QtxListView::isShown( int ind ) const
233 if ( ind>=0 && ind<header()->count() )
234 return columnWidth( ind ) > 0 || header()->isResizeEnabled( ind );
241 \param ind - column index
242 \param sh - new is shown state
244 void QtxListView::setShown( int ind, bool sh )
246 if( ind<0 || ind>=header()->count() || isShown( ind )==sh )
249 ColumnData& data = myColumns[ind];
253 bool resizeable = data.resizeable;
254 myColumns.remove( ind );
256 setColumnWidth( ind, w );
257 header()->setResizeEnabled( resizeable, ind );
261 int w = columnWidth( ind );
262 bool r = header()->isResizeEnabled( ind );
263 setColumnWidth( ind, 0 );
264 header()->setResizeEnabled( false, ind );
273 \param c - column index
276 void QtxListView::setColumnWidth( int c, int w )
278 if ( myColumns.contains( c ) )
279 myColumns[c].width = w;
281 QListView::setColumnWidth( c, !myColumns.contains( c ) ? w : 0 );
285 \return the recommended size for the widget
287 QSize QtxListView::sizeHint() const
289 QSize sz = QListView::sizeHint();
291 if ( myButton && myButton->isVisibleTo( myButton->parentWidget() ) )
292 sz.setWidth( sz.width() + 2 + myButton->width() );
298 \return the recommended minimum size for the widget
300 QSize QtxListView::minimumSizeHint() const
302 QSize sz = QListView::minimumSizeHint();
304 if ( myButton && myButton->isVisibleTo( myButton->parentWidget() ) )
305 sz.setWidth( sz.width() + 2 + myButton->width() );
311 SLOT: called if header is resized
313 void QtxListView::onHeaderResized()
315 if ( myHeaderState == HeaderAuto )
318 for ( int i = 0; i < columns(); i++ )
320 if ( !header()->label( i ).isEmpty() ||
321 ( header()->iconSet( i ) && !header()->iconSet( i )->isNull() ) )
331 if ( !myButton || !header()->isVisibleTo( this ) )
334 int lw = lineWidth();
335 int h = header()->size().height() - 1;
336 myButton->setFixedSize( h, h );
338 int x = header()->headerWidth() - header()->offset() + 2;
339 if ( x < header()->width() - h )
340 x = header()->width() - h;
342 if ( myHeaderState == HeaderButton )
344 if ( header()->orientation() == Qt::Horizontal )
345 myButton->move( lw+x, lw );
347 myButton->move( lw, lw+x );
352 Shows popup filled with column names to show/hide column
353 \param x, y - position of popup
355 void QtxListView::showPopup( const int x, const int y )
358 for ( int i = 0; i < columns(); i++ )
360 if ( appropriate( i ) )
362 int id = myPopup->insertItem( header()->label( i ), i );
363 myPopup->setItemChecked( id, isShown( i ) );
367 if( myPopup->count() )
368 myPopup->exec( mapToGlobal( QPoint( x, y ) ) );
372 SLOT: shows popup on button ".." click
374 void QtxListView::onButtonClicked()
376 if ( myHeaderState != HeaderButton )
379 int x = myButton->x(),
380 y = myButton->y() + myButton->height();
386 SLOT: called on popup action is activated, toggles shown state of column
387 \param id - column index
389 void QtxListView::onShowHide( int id )
391 //if ( myHeaderState != HeaderButton )
394 setShown( id, !isShown( id ) );
398 Receives all resize events sent to the viewport
400 void QtxListView::viewportResizeEvent( QResizeEvent* e )
402 QListView::viewportResizeEvent( e );
407 Custom event filter, shows popup on right button click
409 bool QtxListView::eventFilter( QObject* o, QEvent* e )
411 if( o==header() && e->type()==QEvent::MouseButtonPress )
413 QMouseEvent* me = ( QMouseEvent* )e;
414 if( me->button()==Qt::RightButton )
416 showPopup( me->x()+2, me->y()+2 );
421 return QListView::eventFilter( o, e );