Salome HOME
fe2ba2ed86736dfb8091558eb8b57b433e82d500
[modules/gui.git] / src / Qtx / QtxTable.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 // File:      QtxTable.cxx
23 // Author:    Sergey TELKOV
24 //
25 #include "QtxTable.h"
26
27 #ifndef QT_NO_TABLE
28
29 #include <qlineedit.h>
30
31 /*!
32   Constructor
33 */
34 QtxTable::QtxTable( QWidget* parent, const char* name )
35 : QTable( parent, name ),
36 myHeaderEditor( 0 ),
37 myEditedHeader( 0 ),
38 myEditedSection( -1 )
39 {
40   connect( verticalHeader(), SIGNAL( sizeChange( int, int, int ) ),
41            this, SLOT( onHeaderSizeChange( int, int, int ) ) );
42   connect( horizontalHeader(), SIGNAL( sizeChange( int, int, int ) ),
43            this, SLOT( onHeaderSizeChange( int, int, int ) ) );
44   connect( verticalScrollBar(), SIGNAL( valueChanged( int ) ), this, SLOT( onScrollBarMoved( int ) ) );
45   connect( horizontalScrollBar(), SIGNAL( valueChanged( int ) ), this, SLOT( onScrollBarMoved( int ) ) );
46 }
47
48 /*!
49   Constructor
50 */
51 QtxTable::QtxTable( int numRows, int numCols, QWidget* parent, const char* name )
52 : QTable( numRows, numCols, parent, name ),
53 myHeaderEditor( 0 ),
54 myEditedHeader( 0 ),
55 myEditedSection( -1 )
56 {
57   connect( verticalHeader(), SIGNAL( sizeChange( int, int, int ) ),
58            this, SLOT( onHeaderSizeChange( int, int, int ) ) );
59   connect( horizontalHeader(), SIGNAL( sizeChange( int, int, int ) ),
60            this, SLOT( onHeaderSizeChange( int, int, int ) ) );
61   connect( verticalScrollBar(), SIGNAL( valueChanged( int ) ), this, SLOT( onScrollBarMoved( int ) ) );
62   connect( horizontalScrollBar(), SIGNAL( valueChanged( int ) ), this, SLOT( onScrollBarMoved( int ) ) );
63 }
64
65 /*!
66   Destructor
67 */
68 QtxTable::~QtxTable()
69 {
70 }
71
72 /*!
73   \return true if header is editable
74   \param o - header orientation
75 */
76 bool QtxTable::headerEditable( Orientation o ) const
77 {
78   return myHeaderEditable.contains( o ) ? myHeaderEditable[o] : false;
79 }
80
81 /*!
82   Changes editable state of header
83   \param o - header orientation
84   \param on - new state
85 */
86 void QtxTable::setHeaderEditable( Orientation o, const bool on )
87 {
88   if ( headerEditable( o ) == on )
89     return;
90
91   myHeaderEditable.insert( o, on );
92
93   QHeader* hdr = header( o );
94
95   if ( !on && myEditedHeader == hdr )
96     endHeaderEdit( false );
97
98   if ( on )
99     hdr->installEventFilter( this );
100   else
101     hdr->removeEventFilter( this );
102 }
103
104 /*!
105   Starts edition of header
106   \param o - header orientation
107   \param sec - column/row
108 */
109 bool QtxTable::editHeader( Orientation o, const int sec )
110 {
111   return beginHeaderEdit( o, sec );
112 }
113
114 /*!
115   Finishes edition of header
116   \param accept - whether new value must be accepted
117 */
118 void QtxTable::endEditHeader( const bool accept )
119 {
120   endHeaderEdit( accept );
121 }
122
123 /*!
124   Finishes edition and hides table
125 */
126 void QtxTable::hide()
127 {
128   endHeaderEdit();
129
130   QTable::hide();
131 }
132
133 /*!
134   Custom event filter
135   Starts edition of header by double click
136   Finishes edition by escape/return/enter pressing
137 */
138 bool QtxTable::eventFilter( QObject* o, QEvent* e )
139 {
140   if ( e->type() == QEvent::MouseButtonDblClick )
141   {
142     QMouseEvent* me = (QMouseEvent*)e;
143     if ( o == horizontalHeader() )
144     {
145       beginHeaderEdit( Horizontal, me->pos() );
146       return true;
147     }
148     else if ( o == verticalHeader() )
149     {
150       beginHeaderEdit( Vertical, me->pos() );
151       return true;
152     }
153   }
154
155   if ( o == myHeaderEditor && e->type() == QEvent::KeyPress && isHeaderEditing() )
156   {
157           QKeyEvent* ke = (QKeyEvent*)e;
158     if ( ke->key() == Key_Escape )
159     {
160       endHeaderEdit( false );
161       return true;
162     }
163
164     if ( ke->key() == Key_Return || ke->key() == Key_Enter )
165     {
166       endHeaderEdit( true );
167       return true;
168     }
169
170     return false;
171   }
172
173   if ( o == myHeaderEditor && e->type() == QEvent::FocusOut &&
174        isHeaderEditing() && ((QFocusEvent*)e)->reason() != QFocusEvent::Popup )
175   {
176                 endHeaderEdit();
177                 return true;
178   }
179
180   if ( e->type() == QEvent::Wheel && isHeaderEditing() )
181     return true;
182
183   return QTable::eventFilter( o, e );
184 }
185
186 /*!
187   SLOT: called on scroll
188 */
189 void QtxTable::onScrollBarMoved( int )
190 {
191   updateHeaderEditor();
192 }
193
194 /*!
195   SLOT: called on header size changing
196 */
197 void QtxTable::onHeaderSizeChange( int, int, int )
198 {
199   if ( sender() == myEditedHeader )
200     updateHeaderEditor();
201 }
202
203 /*!
204   Custom resize event handler
205 */
206 void QtxTable::resizeEvent( QResizeEvent* e )
207 {
208   QTable::resizeEvent( e );
209
210   updateHeaderEditor();
211 }
212
213 /*!
214   Starts edition of header
215   \param o - header orientation
216   \param sec - column/row
217 */
218 bool QtxTable::beginHeaderEdit( Orientation o, const int section )
219 {
220   if ( !headerEditable( o ) || !header( o ) || !header( o )->isVisibleTo( this ) )
221     return false;
222
223   endHeaderEdit();
224
225   QHeader* hdr = header( o );
226
227   QRect r = headerSectionRect( hdr, section );
228   if ( !r.isValid() )
229     return false;
230
231   if ( o == Horizontal )
232     r.setLeft( QMAX( r.left(), leftMargin() ) );
233   else
234     r.setTop( QMAX( r.top(), topMargin() ) );
235
236   myHeaderEditor = createHeaderEditor( hdr, section );
237   if ( !myHeaderEditor )
238     return false;
239
240   myEditedHeader = hdr;
241   myEditedSection = section;
242
243   myHeaderEditor->reparent( this, QPoint( 0, 0 ), false );
244
245   updateHeaderEditor();
246
247   myHeaderEditor->show();
248
249   myHeaderEditor->setActiveWindow();
250   myHeaderEditor->setFocus();
251
252   myHeaderEditor->installEventFilter( this );
253
254   return true;
255 }
256
257 /*!
258   Finishes edition of header
259   \param accept - whether new value must be accepted
260 */
261 void QtxTable::endHeaderEdit( const bool accept )
262 {
263   if ( !isHeaderEditing() )
264     return;
265
266   QString oldTxt = myEditedHeader ? myEditedHeader->label( myEditedSection ) : QString();
267
268   if ( accept && myEditedHeader )
269     setHeaderContentFromEditor( myEditedHeader, myEditedSection, myHeaderEditor );
270
271   QString newTxt = myEditedHeader ? myEditedHeader->label( myEditedSection ) : QString();
272
273   int sec = myEditedSection;
274   QHeader* hdr = myEditedHeader;
275
276   myEditedHeader = 0;
277   myEditedSection = -1;
278
279   myHeaderEditor->hide();
280   myHeaderEditor->deleteLater();
281   myHeaderEditor = 0;
282
283   if ( oldTxt != newTxt )
284   {
285     emit headerEdited( hdr, sec );
286     emit headerEdited( hdr == horizontalHeader() ? Horizontal : Vertical, sec );
287   }
288 }
289
290 /*!
291   \return true if header is being edited
292 */
293 bool QtxTable::isHeaderEditing() const
294 {
295   return myHeaderEditor && myEditedHeader && myEditedSection != -1;
296 }
297
298 /*!
299   Creates and \return header editor
300   \param hdr - header
301   \param sec - column/row
302   \param init - init editor with value
303 */
304 QWidget* QtxTable::createHeaderEditor( QHeader* hdr, const int sec, const bool init )
305 {
306   QLineEdit* ed = new QLineEdit( 0 );
307
308   if ( init && hdr )
309     ed->setText( hdr->label( sec ) );
310
311   return ed;
312 }
313
314 /*!
315   Initialize editor with value
316   \param hdr - header
317   \param sec - column/row
318   \param editor - editor
319 */
320 void QtxTable::setHeaderContentFromEditor( QHeader* hdr, const int sec, QWidget* editor )
321 {
322   if ( !hdr || !editor )
323     return;
324
325   if ( editor->inherits( "QLineEdit" ) )
326     hdr->setLabel( sec, ((QLineEdit*)editor)->text() );
327 }
328
329 /*!
330   \return header
331   \param o - orientation
332 */
333 QHeader* QtxTable::header( Orientation o ) const
334 {
335   return o == Horizontal ? horizontalHeader() : verticalHeader();
336 }
337
338 /*!
339   Starts edition of header
340   \param o - header orientation
341   \param p - point
342 */
343 void QtxTable::beginHeaderEdit( Orientation o, const QPoint& p )
344 {
345   QHeader* hdr = header( o );
346   if ( !hdr )
347     return;
348
349   int pos = o == Horizontal ? p.x() : p.y();
350   int sec = hdr->sectionAt( hdr->offset() + pos );
351
352   beginHeaderEdit( o, sec );
353 }
354
355 /*!
356   \return rectangle of header section
357   \param hdr - header
358   \param sec - column/row
359 */
360 QRect QtxTable::headerSectionRect( QHeader* hdr, const int sec ) const
361 {
362   QRect r( -1, -1, -1, -1 );
363
364   if ( !hdr )
365     return r;
366
367   r = hdr->sectionRect( sec );
368   if ( r.isValid() )
369     r = QRect( mapFromGlobal( hdr->mapToGlobal( r.topLeft() ) ), r.size() );
370
371   return r;
372 }
373
374 /*!
375   Updates header editor
376 */
377 void QtxTable::updateHeaderEditor()
378 {
379   if ( !myHeaderEditor || !myEditedHeader || myEditedSection < 0 )
380     return;
381
382   QRect r = headerSectionRect( myEditedHeader, myEditedSection );
383   if ( !r.isValid() )
384     return;
385
386   if ( myEditedHeader == horizontalHeader() )
387   {
388     r.setLeft( QMAX( r.left(), leftMargin() ) );
389     r.setRight( QMIN( r.right(), width() - rightMargin() - 2 ) );
390   }
391   else
392   {
393     r.setTop( QMAX( r.top(), topMargin() ) );
394     r.setBottom( QMIN( r.bottom(), height() - bottomMargin() - 2 ) );
395   }
396
397   myHeaderEditor->resize( r.size() );
398   myHeaderEditor->move( r.topLeft() );
399 }
400
401 #endif