1 // Copyright (C) 2007-2008 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.
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
23 // Author: Sergey TELKOV
29 #include <qlineedit.h>
34 QtxTable::QtxTable( QWidget* parent, const char* name )
35 : QTable( parent, name ),
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 ) ) );
51 QtxTable::QtxTable( int numRows, int numCols, QWidget* parent, const char* name )
52 : QTable( numRows, numCols, parent, name ),
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 ) ) );
73 \return true if header is editable
74 \param o - header orientation
76 bool QtxTable::headerEditable( Orientation o ) const
78 return myHeaderEditable.contains( o ) ? myHeaderEditable[o] : false;
82 Changes editable state of header
83 \param o - header orientation
86 void QtxTable::setHeaderEditable( Orientation o, const bool on )
88 if ( headerEditable( o ) == on )
91 myHeaderEditable.insert( o, on );
93 QHeader* hdr = header( o );
95 if ( !on && myEditedHeader == hdr )
96 endHeaderEdit( false );
99 hdr->installEventFilter( this );
101 hdr->removeEventFilter( this );
105 Starts edition of header
106 \param o - header orientation
107 \param sec - column/row
109 bool QtxTable::editHeader( Orientation o, const int sec )
111 return beginHeaderEdit( o, sec );
115 Finishes edition of header
116 \param accept - whether new value must be accepted
118 void QtxTable::endEditHeader( const bool accept )
120 endHeaderEdit( accept );
124 Finishes edition and hides table
126 void QtxTable::hide()
135 Starts edition of header by double click
136 Finishes edition by escape/return/enter pressing
138 bool QtxTable::eventFilter( QObject* o, QEvent* e )
140 if ( e->type() == QEvent::MouseButtonDblClick )
142 QMouseEvent* me = (QMouseEvent*)e;
143 if ( o == horizontalHeader() )
145 beginHeaderEdit( Horizontal, me->pos() );
148 else if ( o == verticalHeader() )
150 beginHeaderEdit( Vertical, me->pos() );
155 if ( o == myHeaderEditor && e->type() == QEvent::KeyPress && isHeaderEditing() )
157 QKeyEvent* ke = (QKeyEvent*)e;
158 if ( ke->key() == Key_Escape )
160 endHeaderEdit( false );
164 if ( ke->key() == Key_Return || ke->key() == Key_Enter )
166 endHeaderEdit( true );
173 if ( o == myHeaderEditor && e->type() == QEvent::FocusOut &&
174 isHeaderEditing() && ((QFocusEvent*)e)->reason() != QFocusEvent::Popup )
180 if ( e->type() == QEvent::Wheel && isHeaderEditing() )
183 return QTable::eventFilter( o, e );
187 SLOT: called on scroll
189 void QtxTable::onScrollBarMoved( int )
191 updateHeaderEditor();
195 SLOT: called on header size changing
197 void QtxTable::onHeaderSizeChange( int, int, int )
199 if ( sender() == myEditedHeader )
200 updateHeaderEditor();
204 Custom resize event handler
206 void QtxTable::resizeEvent( QResizeEvent* e )
208 QTable::resizeEvent( e );
210 updateHeaderEditor();
214 Starts edition of header
215 \param o - header orientation
216 \param sec - column/row
218 bool QtxTable::beginHeaderEdit( Orientation o, const int section )
220 if ( !headerEditable( o ) || !header( o ) || !header( o )->isVisibleTo( this ) )
225 QHeader* hdr = header( o );
227 QRect r = headerSectionRect( hdr, section );
231 if ( o == Horizontal )
232 r.setLeft( QMAX( r.left(), leftMargin() ) );
234 r.setTop( QMAX( r.top(), topMargin() ) );
236 myHeaderEditor = createHeaderEditor( hdr, section );
237 if ( !myHeaderEditor )
240 myEditedHeader = hdr;
241 myEditedSection = section;
243 myHeaderEditor->reparent( this, QPoint( 0, 0 ), false );
245 updateHeaderEditor();
247 myHeaderEditor->show();
249 myHeaderEditor->setActiveWindow();
250 myHeaderEditor->setFocus();
252 myHeaderEditor->installEventFilter( this );
258 Finishes edition of header
259 \param accept - whether new value must be accepted
261 void QtxTable::endHeaderEdit( const bool accept )
263 if ( !isHeaderEditing() )
266 QString oldTxt = myEditedHeader ? myEditedHeader->label( myEditedSection ) : QString();
268 if ( accept && myEditedHeader )
269 setHeaderContentFromEditor( myEditedHeader, myEditedSection, myHeaderEditor );
271 QString newTxt = myEditedHeader ? myEditedHeader->label( myEditedSection ) : QString();
273 int sec = myEditedSection;
274 QHeader* hdr = myEditedHeader;
277 myEditedSection = -1;
279 myHeaderEditor->hide();
280 myHeaderEditor->deleteLater();
283 if ( oldTxt != newTxt )
285 emit headerEdited( hdr, sec );
286 emit headerEdited( hdr == horizontalHeader() ? Horizontal : Vertical, sec );
291 \return true if header is being edited
293 bool QtxTable::isHeaderEditing() const
295 return myHeaderEditor && myEditedHeader && myEditedSection != -1;
299 Creates and \return header editor
301 \param sec - column/row
302 \param init - init editor with value
304 QWidget* QtxTable::createHeaderEditor( QHeader* hdr, const int sec, const bool init )
306 QLineEdit* ed = new QLineEdit( 0 );
309 ed->setText( hdr->label( sec ) );
315 Initialize editor with value
317 \param sec - column/row
318 \param editor - editor
320 void QtxTable::setHeaderContentFromEditor( QHeader* hdr, const int sec, QWidget* editor )
322 if ( !hdr || !editor )
325 if ( editor->inherits( "QLineEdit" ) )
326 hdr->setLabel( sec, ((QLineEdit*)editor)->text() );
331 \param o - orientation
333 QHeader* QtxTable::header( Orientation o ) const
335 return o == Horizontal ? horizontalHeader() : verticalHeader();
339 Starts edition of header
340 \param o - header orientation
343 void QtxTable::beginHeaderEdit( Orientation o, const QPoint& p )
345 QHeader* hdr = header( o );
349 int pos = o == Horizontal ? p.x() : p.y();
350 int sec = hdr->sectionAt( hdr->offset() + pos );
352 beginHeaderEdit( o, sec );
356 \return rectangle of header section
358 \param sec - column/row
360 QRect QtxTable::headerSectionRect( QHeader* hdr, const int sec ) const
362 QRect r( -1, -1, -1, -1 );
367 r = hdr->sectionRect( sec );
369 r = QRect( mapFromGlobal( hdr->mapToGlobal( r.topLeft() ) ), r.size() );
375 Updates header editor
377 void QtxTable::updateHeaderEditor()
379 if ( !myHeaderEditor || !myEditedHeader || myEditedSection < 0 )
382 QRect r = headerSectionRect( myEditedHeader, myEditedSection );
386 if ( myEditedHeader == horizontalHeader() )
388 r.setLeft( QMAX( r.left(), leftMargin() ) );
389 r.setRight( QMIN( r.right(), width() - rightMargin() - 2 ) );
393 r.setTop( QMAX( r.top(), topMargin() ) );
394 r.setBottom( QMIN( r.bottom(), height() - bottomMargin() - 2 ) );
397 myHeaderEditor->resize( r.size() );
398 myHeaderEditor->move( r.topLeft() );