1 // Copyright (C) 2005 OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
8 // This library is distributed in the hope that it will be useful
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/
20 // Author: Sergey TELKOV
26 #include <qlineedit.h>
28 QtxTable::QtxTable( QWidget* parent, const char* name )
29 : QTable( parent, name ),
34 connect( verticalHeader(), SIGNAL( sizeChange( int, int, int ) ),
35 this, SLOT( onHeaderSizeChange( int, int, int ) ) );
36 connect( horizontalHeader(), SIGNAL( sizeChange( int, int, int ) ),
37 this, SLOT( onHeaderSizeChange( int, int, int ) ) );
38 connect( verticalScrollBar(), SIGNAL( valueChanged( int ) ), this, SLOT( onScrollBarMoved( int ) ) );
39 connect( horizontalScrollBar(), SIGNAL( valueChanged( int ) ), this, SLOT( onScrollBarMoved( int ) ) );
42 QtxTable::QtxTable( int numRows, int numCols, QWidget* parent, const char* name )
43 : QTable( numRows, numCols, parent, name ),
48 connect( verticalHeader(), SIGNAL( sizeChange( int, int, int ) ),
49 this, SLOT( onHeaderSizeChange( int, int, int ) ) );
50 connect( horizontalHeader(), SIGNAL( sizeChange( int, int, int ) ),
51 this, SLOT( onHeaderSizeChange( int, int, int ) ) );
52 connect( verticalScrollBar(), SIGNAL( valueChanged( int ) ), this, SLOT( onScrollBarMoved( int ) ) );
53 connect( horizontalScrollBar(), SIGNAL( valueChanged( int ) ), this, SLOT( onScrollBarMoved( int ) ) );
60 bool QtxTable::headerEditable( Orientation o ) const
62 return myHeaderEditable.contains( o ) ? myHeaderEditable[o] : false;
65 void QtxTable::setHeaderEditable( Orientation o, const bool on )
67 if ( headerEditable( o ) == on )
70 myHeaderEditable.insert( o, on );
72 QHeader* hdr = header( o );
74 if ( !on && myEditedHeader == hdr )
75 endHeaderEdit( false );
78 hdr->installEventFilter( this );
80 hdr->removeEventFilter( this );
83 bool QtxTable::editHeader( Orientation o, const int sec )
85 return beginHeaderEdit( o, sec );
88 void QtxTable::endEditHeader( const bool accept )
90 endHeaderEdit( accept );
100 bool QtxTable::eventFilter( QObject* o, QEvent* e )
102 if ( e->type() == QEvent::MouseButtonDblClick )
104 QMouseEvent* me = (QMouseEvent*)e;
105 if ( o == horizontalHeader() )
107 beginHeaderEdit( Horizontal, me->pos() );
110 else if ( o == verticalHeader() )
112 beginHeaderEdit( Vertical, me->pos() );
117 if ( o == myHeaderEditor && e->type() == QEvent::KeyPress && isHeaderEditing() )
119 QKeyEvent* ke = (QKeyEvent*)e;
120 if ( ke->key() == Key_Escape )
122 endHeaderEdit( false );
126 if ( ke->key() == Key_Return || ke->key() == Key_Enter )
128 endHeaderEdit( true );
135 if ( o == myHeaderEditor && e->type() == QEvent::FocusOut &&
136 isHeaderEditing() && ((QFocusEvent*)e)->reason() != QFocusEvent::Popup )
142 if ( e->type() == QEvent::Wheel && isHeaderEditing() )
145 return QTable::eventFilter( o, e );
148 void QtxTable::onScrollBarMoved( int )
150 updateHeaderEditor();
153 void QtxTable::onHeaderSizeChange( int, int, int )
155 if ( sender() == myEditedHeader )
156 updateHeaderEditor();
159 void QtxTable::resizeEvent( QResizeEvent* e )
161 QTable::resizeEvent( e );
163 updateHeaderEditor();
166 bool QtxTable::beginHeaderEdit( Orientation o, const int section )
168 if ( !headerEditable( o ) || !header( o ) || !header( o )->isVisibleTo( this ) )
173 QHeader* hdr = header( o );
175 QRect r = headerSectionRect( hdr, section );
179 if ( o == Horizontal )
180 r.setLeft( QMAX( r.left(), leftMargin() ) );
182 r.setTop( QMAX( r.top(), topMargin() ) );
184 myHeaderEditor = createHeaderEditor( hdr, section );
185 if ( !myHeaderEditor )
188 myEditedHeader = hdr;
189 myEditedSection = section;
191 myHeaderEditor->reparent( this, QPoint( 0, 0 ), false );
193 updateHeaderEditor();
195 myHeaderEditor->show();
197 myHeaderEditor->setActiveWindow();
198 myHeaderEditor->setFocus();
200 myHeaderEditor->installEventFilter( this );
205 void QtxTable::endHeaderEdit( const bool accept )
207 if ( !isHeaderEditing() )
210 QString oldTxt = myEditedHeader ? myEditedHeader->label( myEditedSection ) : QString::null;
212 if ( accept && myEditedHeader )
213 setHeaderContentFromEditor( myEditedHeader, myEditedSection, myHeaderEditor );
215 QString newTxt = myEditedHeader ? myEditedHeader->label( myEditedSection ) : QString::null;
217 int sec = myEditedSection;
218 QHeader* hdr = myEditedHeader;
221 myEditedSection = -1;
223 myHeaderEditor->hide();
224 myHeaderEditor->deleteLater();
227 if ( oldTxt != newTxt )
229 emit headerEdited( hdr, sec );
230 emit headerEdited( hdr == horizontalHeader() ? Horizontal : Vertical, sec );
234 bool QtxTable::isHeaderEditing() const
236 return myHeaderEditor && myEditedHeader && myEditedSection != -1;
239 QWidget* QtxTable::createHeaderEditor( QHeader* hdr, const int sec, const bool init )
241 QLineEdit* ed = new QLineEdit( 0 );
244 ed->setText( hdr->label( sec ) );
249 void QtxTable::setHeaderContentFromEditor( QHeader* hdr, const int sec, QWidget* editor )
251 if ( !hdr || !editor )
254 if ( editor->inherits( "QLineEdit" ) )
255 hdr->setLabel( sec, ((QLineEdit*)editor)->text() );
258 QHeader* QtxTable::header( Orientation o ) const
260 return o == Horizontal ? horizontalHeader() : verticalHeader();
263 void QtxTable::beginHeaderEdit( Orientation o, const QPoint& p )
265 QHeader* hdr = header( o );
269 int pos = o == Horizontal ? p.x() : p.y();
270 int sec = hdr->sectionAt( hdr->offset() + pos );
272 beginHeaderEdit( o, sec );
275 QRect QtxTable::headerSectionRect( QHeader* hdr, const int sec ) const
277 QRect r( -1, -1, -1, -1 );
282 r = hdr->sectionRect( sec );
284 r = QRect( mapFromGlobal( hdr->mapToGlobal( r.topLeft() ) ), r.size() );
289 void QtxTable::updateHeaderEditor()
291 if ( !myHeaderEditor || !myEditedHeader || myEditedSection < 0 )
294 QRect r = headerSectionRect( myEditedHeader, myEditedSection );
298 if ( myEditedHeader == horizontalHeader() )
300 r.setLeft( QMAX( r.left(), leftMargin() ) );
301 r.setRight( QMIN( r.right(), width() - rightMargin() - 2 ) );
305 r.setTop( QMAX( r.top(), topMargin() ) );
306 r.setBottom( QMIN( r.bottom(), height() - bottomMargin() - 2 ) );
309 myHeaderEditor->resize( r.size() );
310 myHeaderEditor->move( r.topLeft() );