]> SALOME platform Git repositories - modules/gui.git/blob - src/Qtx/QtxListView.cxx
Salome HOME
Multi columns support
[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   for ( int i = myAppropriate.count(); i <= res; i++ )
81     myAppropriate.append( 1 );
82   onHeaderResized();
83   return res;
84 }
85
86 int QtxListView::addColumn( const QIconSet& iconset, const QString& label, int width ) 
87 {
88   int res = QListView::addColumn( iconset, label, width );
89   for ( int i = myAppropriate.count(); i <= res; i++ )
90     myAppropriate.append( 1 );
91   onHeaderResized();
92   return res;
93 }
94
95 void QtxListView::removeColumn( int index ) 
96 {
97   QListView::removeColumn( index );
98   if ( index >= 0 && index < (int)myAppropriate.count() )
99     myAppropriate.remove( myAppropriate.at( index ) );
100   onHeaderResized();
101 }
102
103 bool QtxListView::appropriate( const int index ) const
104 {
105   return index >= 0 && index < (int)myAppropriate.count() && myAppropriate[index];
106 }
107
108 void QtxListView::setAppropriate( const int index, const bool on )
109 {
110   if ( index < 0 || index >= (int)myAppropriate.count() )
111     return;
112
113   myAppropriate[index] = on ? 1 : 0;
114 }
115
116 void QtxListView::resize( int w, int h )
117 {
118   QListView::resize( w, h );
119   onHeaderResized();
120 }
121
122 void QtxListView::show()
123 {
124   QListView::show();
125   onHeaderResized();
126 }
127
128 void QtxListView::resizeContents( int w, int h )
129 {
130   QListView::resizeContents( w, h );
131   onHeaderResized();
132 }
133
134 void QtxListView::show( int ind )
135 {
136   setShown( ind, true );
137 }
138
139 void QtxListView::hide( int ind )
140 {
141   setShown( ind, false );
142 }
143
144 bool QtxListView::isShown( int ind ) const
145 {
146   if( ind>=0 && ind<header()->count() )
147     return columnWidth( ind ) > 0 || header()->isResizeEnabled( ind );
148   else
149     return false;
150 }
151
152 void QtxListView::setShown( int ind, bool sh )
153 {
154   if( ind<0 || ind>=header()->count() || isShown( ind )==sh )
155     return;
156
157   ColumnData& data = myColumns[ ind ];
158   if( sh )
159   {
160     setColumnWidth( ind, data.width );
161     header()->setResizeEnabled( data.resizeable, ind );
162     myColumns.remove( ind );
163   }
164   else
165   {
166     data.width = columnWidth( ind );
167     data.resizeable = header()->isResizeEnabled( ind );
168     setColumnWidth( ind, 0 );
169     header()->setResizeEnabled( false, ind );
170   }
171   updateContents();
172 }
173
174 QSize QtxListView::sizeHint() const
175 {
176   QSize sz = QListView::sizeHint();
177
178   if ( myButton && myButton->isVisibleTo( myButton->parentWidget() ) )
179     sz.setWidth( sz.width() + 2 + myButton->width() );
180
181   return sz;
182 }
183
184 QSize QtxListView::minimumSizeHint() const
185 {
186   QSize sz = QListView::minimumSizeHint();
187
188   if ( myButton && myButton->isVisibleTo( myButton->parentWidget() ) )
189     sz.setWidth( sz.width() + 2 + myButton->width() );
190
191   return sz;
192 }
193
194 void QtxListView::onHeaderResized()
195 {
196   if ( myHeaderState == HeaderAuto )
197   {
198     int c = 0;
199     for ( int i = 0; i < columns(); i++ )
200     {
201       if ( !header()->label( i ).isEmpty() ||
202            ( header()->iconSet( i ) && !header()->iconSet( i )->isNull() ) )
203         c++;
204     }
205
206     if ( c > 1 )
207       header()->show();
208     else
209       header()->hide();
210   }
211
212   if ( !myButton || !header()->isVisibleTo( this ) )
213     return;
214
215   int lw = lineWidth();
216   int h = header()->size().height() - 1;
217   myButton->setFixedSize( h, h );
218
219   int x = header()->headerWidth() - header()->offset() + 2;
220   if ( x < header()->width() - h )
221     x = header()->width() - h;
222
223   if ( myHeaderState == HeaderButton )
224   {
225     if( header()->orientation() == Qt::Horizontal )
226       myButton->move( lw+x, lw );
227     else
228       myButton->move( lw, lw+x );
229   }
230 }
231
232 void QtxListView::onButtonClicked()
233 {
234   if ( myHeaderState != HeaderButton )
235     return;
236
237   myPopup->clear();
238   for ( int i = 0; i < columns(); i++ )
239   {
240     if ( appropriate( i ) )
241     {
242       int id = myPopup->insertItem( header()->label( i ), i );
243       myPopup->setItemChecked( id, isShown( i ) );
244     }
245   }
246   int x = myButton->x(),
247       y = myButton->y() + myButton->height();
248   if ( myPopup->count() )
249     myPopup->exec( mapToGlobal( QPoint( x, y ) ) );
250 }
251
252 void QtxListView::onShowHide( int id )
253 {
254   if ( myHeaderState != HeaderButton )
255     return;
256
257   setShown( id, !isShown( id ) );
258 }
259
260 void QtxListView::viewportResizeEvent( QResizeEvent* e )
261 {
262   QListView::viewportResizeEvent( e );
263   onHeaderResized();
264 }