Salome HOME
Popup item "Refresh"
[modules/gui.git] / src / Qtx / QtxListView.cxx
1
2 #include "QtxListView.h"
3
4 #include <qheader.h>
5 #include <qpopupmenu.h>
6 #include <qpushbutton.h>
7
8 static const char* list_xpm[] = {
9 "16 16 6 1",
10 ". c None",
11 "a c #E3E9EB",
12 "b c #798391",
13 "c c #EBEBEB",
14 "d c #ABB4BE",
15 "e c #030E1F",
16 "................",
17 "................",
18 "................",
19 "...aaaaaaaaaa...",
20 "..abbbbbbbbbbe..",
21 "..abecbecbecbe..",
22 "..abbbbbbbbbbe..",
23 "..abecbecbecbe..",
24 "..abecaaaaaaaa..",
25 "..abeccdbbbbbb..",
26 "..abecccdbbbbe..",
27 "..abbbbe.dbbe...",
28 "...eeeee..de....",
29 "................",
30 "................",
31 "................" };
32
33 QtxListView::QtxListView( const int state, QWidget* parent, const char* name, WFlags f )
34 : QListView( parent, name, f ),
35 myButton( 0 ),
36 myHeaderState( state )
37 {
38   initialize();
39 }
40
41 QtxListView::QtxListView( QWidget* parent, const char* name, WFlags f )
42 : QListView( parent, name, f ),
43 myButton( 0 ),
44 myHeaderState( HeaderAuto )
45 {
46   initialize();
47 }
48
49 void QtxListView::initialize()
50 {
51   if ( myHeaderState == HeaderButton )
52   {
53     QPixmap p( list_xpm );
54
55     QPushButton* but = new QPushButton( this );
56     but->setDefault( false );
57     but->setFlat( true );
58     but->setIconSet( p );
59     but->setBackgroundPixmap( p );
60     if ( p.mask() )
61             but->setMask( *p.mask() );
62     myButton = but;
63
64     connect( myButton, SIGNAL( clicked() ), this, SLOT( onButtonClicked() ) );
65
66     myPopup = new QPopupMenu( this );
67     connect( myPopup, SIGNAL( activated( int ) ), this, SLOT( onShowHide( int ) ) );
68   }
69
70   connect( header(), SIGNAL( sizeChange( int, int, int ) ), this, SLOT( onHeaderResized() ) );
71 }
72
73 QtxListView::~QtxListView()
74 {
75 }
76
77 int QtxListView::addColumn( const QString& label, int width )
78 {
79   int res = QListView::addColumn( label, width );
80   onHeaderResized();
81   return res;
82 }
83
84 int QtxListView::addColumn( const QIconSet& iconset, const QString& label, int width ) 
85 {
86   int res = QListView::addColumn( iconset, label, width );
87   onHeaderResized();
88   return res;
89 }
90
91 void QtxListView::removeColumn( int index ) 
92 {
93   QListView::removeColumn( index );
94   onHeaderResized();
95 }
96
97 void QtxListView::resize( int w, int h )
98 {
99   QListView::resize( w, h );
100   onHeaderResized();
101 }
102
103 void QtxListView::show()
104 {
105   QListView::show();
106   onHeaderResized();
107 }
108
109 void QtxListView::resizeContents( int w, int h )
110 {
111   QListView::resizeContents( w, h );
112   onHeaderResized();
113 }
114
115 void QtxListView::show( int ind )
116 {
117   setShown( ind, true );
118 }
119
120 void QtxListView::hide( int ind )
121 {
122   setShown( ind, false );
123 }
124
125 bool QtxListView::isShown( int ind ) const
126 {
127   if( ind>=0 && ind<header()->count() )
128     return columnWidth( ind ) > 0 || header()->isResizeEnabled( ind );
129   else
130     return false;
131 }
132
133 void QtxListView::setShown( int ind, bool sh )
134 {
135   if( ind<0 || ind>=header()->count() || isShown( ind )==sh )
136     return;
137
138   ColumnData& data = myColumns[ ind ];
139   if( sh )
140   {
141     setColumnWidth( ind, data.width );
142     header()->setResizeEnabled( data.resizeable, ind );
143     myColumns.remove( ind );
144   }
145   else
146   {
147     data.width = columnWidth( ind );
148     data.resizeable = header()->isResizeEnabled( ind );
149     setColumnWidth( ind, 0 );
150     header()->setResizeEnabled( false, ind );
151   }
152   updateContents();
153 }
154
155 QSize QtxListView::sizeHint() const
156 {
157   QSize sz = QListView::sizeHint();
158
159   if ( myButton && myButton->isVisibleTo( myButton->parentWidget() ) )
160     sz.setWidth( sz.width() + 2 + myButton->width() );
161
162   return sz;
163 }
164
165 QSize QtxListView::minimumSizeHint() const
166 {
167   QSize sz = QListView::minimumSizeHint();
168
169   if ( myButton && myButton->isVisibleTo( myButton->parentWidget() ) )
170     sz.setWidth( sz.width() + 2 + myButton->width() );
171
172   return sz;
173 }
174
175 void QtxListView::onHeaderResized()
176 {
177   if ( myHeaderState == HeaderAuto )
178   {
179     int c = 0;
180     for ( int i = 0; i < columns(); i++ )
181     {
182       if ( !header()->label( i ).isEmpty() ||
183            ( header()->iconSet( i ) && !header()->iconSet( i )->isNull() ) )
184         c++;
185     }
186
187     if ( c > 1 )
188       header()->show();
189     else
190       header()->hide();
191   }
192
193   if ( !myButton || !header()->isVisibleTo( this ) )
194     return;
195
196   int lw = lineWidth();
197   int h = header()->size().height() - 1;
198   myButton->setFixedSize( h, h );
199
200   int x = header()->headerWidth() - header()->offset() + 2;
201   if ( x < header()->width() - h )
202     x = header()->width() - h;
203
204   if ( myHeaderState == HeaderButton )
205   {
206     if( header()->orientation() == Qt::Horizontal )
207       myButton->move( lw+x, lw );
208     else
209       myButton->move( lw, lw+x );
210   }
211 }
212
213 void QtxListView::onButtonClicked()
214 {
215   if ( myHeaderState != HeaderButton )
216     return;
217
218   myPopup->clear();
219   for( int i=0, n=header()->count(); i<n; i++ )
220   {
221     int id = myPopup->insertItem( header()->label( i ) );
222     myPopup->setItemChecked( id, isShown( i ) );
223   }
224   int x = myButton->x(),
225       y = myButton->y() + myButton->height();
226   myPopup->exec( mapToGlobal( QPoint( x, y ) ) );
227 }
228
229 void QtxListView::onShowHide( int id )
230 {
231   if ( myHeaderState != HeaderButton )
232     return;
233
234   int ind = myPopup->indexOf( id );
235   setShown( ind, !isShown( ind ) );
236 }
237
238 void QtxListView::viewportResizeEvent( QResizeEvent* e )
239 {
240   QListView::viewportResizeEvent( e );
241   onHeaderResized();
242 }