1 // Copyright (C) 2007-2016 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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, or (at your option) any later version.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
24 // Author: Sergey TELKOV
30 #include <qlineedit.h>
35 QtxTable::QtxTable( QWidget* parent, const char* name )
36 : QTable( parent, name ),
41 connect( verticalHeader(), SIGNAL( sizeChange( int, int, int ) ),
42 this, SLOT( onHeaderSizeChange( int, int, int ) ) );
43 connect( horizontalHeader(), SIGNAL( sizeChange( int, int, int ) ),
44 this, SLOT( onHeaderSizeChange( int, int, int ) ) );
45 connect( verticalScrollBar(), SIGNAL( valueChanged( int ) ), this, SLOT( onScrollBarMoved( int ) ) );
46 connect( horizontalScrollBar(), SIGNAL( valueChanged( int ) ), this, SLOT( onScrollBarMoved( int ) ) );
52 QtxTable::QtxTable( int numRows, int numCols, QWidget* parent, const char* name )
53 : QTable( numRows, numCols, parent, name ),
58 connect( verticalHeader(), SIGNAL( sizeChange( int, int, int ) ),
59 this, SLOT( onHeaderSizeChange( int, int, int ) ) );
60 connect( horizontalHeader(), SIGNAL( sizeChange( int, int, int ) ),
61 this, SLOT( onHeaderSizeChange( int, int, int ) ) );
62 connect( verticalScrollBar(), SIGNAL( valueChanged( int ) ), this, SLOT( onScrollBarMoved( int ) ) );
63 connect( horizontalScrollBar(), SIGNAL( valueChanged( int ) ), this, SLOT( onScrollBarMoved( int ) ) );
74 \return true if header is editable
75 \param o - header orientation
77 bool QtxTable::headerEditable( Orientation o ) const
79 return myHeaderEditable.contains( o ) ? myHeaderEditable[o] : false;
83 Changes editable state of header
84 \param o - header orientation
87 void QtxTable::setHeaderEditable( Orientation o, const bool on )
89 if ( headerEditable( o ) == on )
92 myHeaderEditable.insert( o, on );
94 QHeader* hdr = header( o );
96 if ( !on && myEditedHeader == hdr )
97 endHeaderEdit( false );
100 hdr->installEventFilter( this );
102 hdr->removeEventFilter( this );
106 Starts edition of header
107 \param o - header orientation
108 \param sec - column/row
110 bool QtxTable::editHeader( Orientation o, const int sec )
112 return beginHeaderEdit( o, sec );
116 Finishes edition of header
117 \param accept - whether new value must be accepted
119 void QtxTable::endEditHeader( const bool accept )
121 endHeaderEdit( accept );
125 Finishes edition and hides table
127 void QtxTable::hide()
136 Starts edition of header by double click
137 Finishes edition by escape/return/enter pressing
139 bool QtxTable::eventFilter( QObject* o, QEvent* e )
141 if ( e->type() == QEvent::MouseButtonDblClick )
143 QMouseEvent* me = (QMouseEvent*)e;
144 if ( o == horizontalHeader() )
146 beginHeaderEdit( Horizontal, me->pos() );
149 else if ( o == verticalHeader() )
151 beginHeaderEdit( Vertical, me->pos() );
156 if ( o == myHeaderEditor && e->type() == QEvent::KeyPress && isHeaderEditing() )
158 QKeyEvent* ke = (QKeyEvent*)e;
159 if ( ke->key() == Key_Escape )
161 endHeaderEdit( false );
165 if ( ke->key() == Key_Return || ke->key() == Key_Enter )
167 endHeaderEdit( true );
174 if ( o == myHeaderEditor && e->type() == QEvent::FocusOut &&
175 isHeaderEditing() && ((QFocusEvent*)e)->reason() != QFocusEvent::Popup )
181 if ( e->type() == QEvent::Wheel && isHeaderEditing() )
184 return QTable::eventFilter( o, e );
188 SLOT: called on scroll
190 void QtxTable::onScrollBarMoved( int )
192 updateHeaderEditor();
196 SLOT: called on header size changing
198 void QtxTable::onHeaderSizeChange( int, int, int )
200 if ( sender() == myEditedHeader )
201 updateHeaderEditor();
205 Custom resize event handler
207 void QtxTable::resizeEvent( QResizeEvent* e )
209 QTable::resizeEvent( e );
211 updateHeaderEditor();
215 Starts edition of header
216 \param o - header orientation
217 \param sec - column/row
219 bool QtxTable::beginHeaderEdit( Orientation o, const int section )
221 if ( !headerEditable( o ) || !header( o ) || !header( o )->isVisibleTo( this ) )
226 QHeader* hdr = header( o );
228 QRect r = headerSectionRect( hdr, section );
232 if ( o == Horizontal )
233 r.setLeft( QMAX( r.left(), leftMargin() ) );
235 r.setTop( QMAX( r.top(), topMargin() ) );
237 myHeaderEditor = createHeaderEditor( hdr, section );
238 if ( !myHeaderEditor )
241 myEditedHeader = hdr;
242 myEditedSection = section;
244 myHeaderEditor->reparent( this, QPoint( 0, 0 ), false );
246 updateHeaderEditor();
248 myHeaderEditor->show();
250 myHeaderEditor->setActiveWindow();
251 myHeaderEditor->setFocus();
253 myHeaderEditor->installEventFilter( this );
259 Finishes edition of header
260 \param accept - whether new value must be accepted
262 void QtxTable::endHeaderEdit( const bool accept )
264 if ( !isHeaderEditing() )
267 QString oldTxt = myEditedHeader ? myEditedHeader->label( myEditedSection ) : QString();
269 if ( accept && myEditedHeader )
270 setHeaderContentFromEditor( myEditedHeader, myEditedSection, myHeaderEditor );
272 QString newTxt = myEditedHeader ? myEditedHeader->label( myEditedSection ) : QString();
274 int sec = myEditedSection;
275 QHeader* hdr = myEditedHeader;
278 myEditedSection = -1;
280 myHeaderEditor->hide();
281 myHeaderEditor->deleteLater();
284 if ( oldTxt != newTxt )
286 emit headerEdited( hdr, sec );
287 emit headerEdited( hdr == horizontalHeader() ? Horizontal : Vertical, sec );
292 \return true if header is being edited
294 bool QtxTable::isHeaderEditing() const
296 return myHeaderEditor && myEditedHeader && myEditedSection != -1;
300 Creates and \return header editor
302 \param sec - column/row
303 \param init - init editor with value
305 QWidget* QtxTable::createHeaderEditor( QHeader* hdr, const int sec, const bool init )
307 QLineEdit* ed = new QLineEdit( 0 );
310 ed->setText( hdr->label( sec ) );
316 Initialize editor with value
318 \param sec - column/row
319 \param editor - editor
321 void QtxTable::setHeaderContentFromEditor( QHeader* hdr, const int sec, QWidget* editor )
323 if ( !hdr || !editor )
326 if ( editor->inherits( "QLineEdit" ) )
327 hdr->setLabel( sec, ((QLineEdit*)editor)->text() );
332 \param o - orientation
334 QHeader* QtxTable::header( Orientation o ) const
336 return o == Horizontal ? horizontalHeader() : verticalHeader();
340 Starts edition of header
341 \param o - header orientation
344 void QtxTable::beginHeaderEdit( Orientation o, const QPoint& p )
346 QHeader* hdr = header( o );
350 int pos = o == Horizontal ? p.x() : p.y();
351 int sec = hdr->sectionAt( hdr->offset() + pos );
353 beginHeaderEdit( o, sec );
357 \return rectangle of header section
359 \param sec - column/row
361 QRect QtxTable::headerSectionRect( QHeader* hdr, const int sec ) const
363 QRect r( -1, -1, -1, -1 );
368 r = hdr->sectionRect( sec );
370 r = QRect( mapFromGlobal( hdr->mapToGlobal( r.topLeft() ) ), r.size() );
376 Updates header editor
378 void QtxTable::updateHeaderEditor()
380 if ( !myHeaderEditor || !myEditedHeader || myEditedSection < 0 )
383 QRect r = headerSectionRect( myEditedHeader, myEditedSection );
387 if ( myEditedHeader == horizontalHeader() )
389 r.setLeft( QMAX( r.left(), leftMargin() ) );
390 r.setRight( QMIN( r.right(), width() - rightMargin() - 2 ) );
394 r.setTop( QMAX( r.top(), topMargin() ) );
395 r.setBottom( QMIN( r.bottom(), height() - bottomMargin() - 2 ) );
398 myHeaderEditor->resize( r.size() );
399 myHeaderEditor->move( r.topLeft() );