1 // Copyright (C) 2005 CEA/DEN, EDF R&D, OPEN CASCADE, 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/ or email : webmaster.salome@opencascade.com
20 #include "QDS_Table.h"
22 #include "QDS_LineEdit.h"
24 class QDS_Table::DeleteFilter : public QObject
27 DeleteFilter( QObject* = 0 );
28 virtual ~DeleteFilter();
30 virtual bool eventFilter( QObject*, QEvent* );
33 QDS_Table::DeleteFilter::DeleteFilter( QObject* parent )
38 QDS_Table::DeleteFilter::~DeleteFilter()
42 bool QDS_Table::DeleteFilter::eventFilter( QObject* o, QEvent* e )
44 if ( e->type() == QEvent::DeferredDelete )
50 QDS_Table::QDS_Table( QWidget* parent, const char* name )
51 : QtxTable( parent, name ),
59 QDS_Table::QDS_Table( int r, int c, QWidget* parent, const char* name )
60 : QtxTable( r, c, parent, name ),
66 myRowEdit.resize( r );
67 myColEdit.resize( c );
70 QDS_Table::~QDS_Table()
74 QDS_Datum* QDS_Table::horizontalHeaderEditor() const
79 QDS_Datum* QDS_Table::verticalHeaderEditor() const
84 QDS_Datum* QDS_Table::headerEditor( const Orientation o ) const
86 return o == Horizontal ? myHorEdit : myVerEdit;
89 void QDS_Table::setVerticalHeaderEditor( QDS_Datum* dat )
91 setHeaderEditor( Vertical, dat );
94 void QDS_Table::setHorizontalHeaderEditor( QDS_Datum* dat )
96 setHeaderEditor( Horizontal, dat );
99 void QDS_Table::setHeaderEditor( QDS_Datum* dat )
101 setHeaderEditor( Vertical, dat );
102 setHeaderEditor( Horizontal, dat );
105 void QDS_Table::setHeaderEditor( const Orientation o, QDS_Datum* dat )
107 if ( headerEditor( o ) == dat )
110 if ( isHeaderEditing() )
113 if ( o == Horizontal )
121 QDS_Datum* QDS_Table::tableEditor() const
126 QDS_Datum* QDS_Table::rowEditor( const int row ) const
128 if ( row < 0 || row >= (int)myRowEdit.size() )
131 return myRowEdit.at( row );
134 QDS_Datum* QDS_Table::columnEditor( const int col ) const
136 if ( col < 0 || col >= (int)myColEdit.size() )
139 return myColEdit.at( col );
142 QDS_Datum* QDS_Table::cellEditor( const int row, const int col ) const
144 if ( !myCellEdit.contains( row ) )
147 const DatumMap& map = myCellEdit[row];
148 return map.contains( col ) ? map[col] : 0;
151 void QDS_Table::setTableEditor( QDS_Datum* dat )
153 if ( tableEditor() == dat )
156 if ( isEditing() && !cellEditor( currEditRow(), currEditCol() ) &&
157 !columnEditor( currEditCol() ) && !rowEditor( currEditRow() ) )
158 endEdit( currEditRow(), currEditCol(), false, false );
164 void QDS_Table::setRowEditor( const int row, QDS_Datum* dat )
166 if ( row < 0 || row >= (int)myRowEdit.size() || rowEditor( row ) == dat )
169 if ( isEditing() && row == currEditRow()&&
170 !cellEditor( currEditRow(), currEditCol() ) )
171 endEdit( currEditRow(), currEditCol(), false, false );
173 myRowEdit.insert( row, dat );
177 void QDS_Table::setColumnEditor( const int col, QDS_Datum* dat )
179 if ( col < 0 || col >= (int)myColEdit.size() || columnEditor( col ) == dat )
182 if ( isEditing() && col == currEditCol()&&
183 !cellEditor( currEditRow(), currEditCol() ) )
184 endEdit( currEditRow(), currEditCol(), false, false );
186 myColEdit.insert( col, dat );
190 void QDS_Table::setCellEditor( const int row, const int col, QDS_Datum* dat )
192 if ( row < 0 || row >= numRows() || col < 0 || col >= numCols() || cellEditor( row, col ) == dat )
195 if ( isEditing() && currEditRow() == row && currEditCol() == col && actualCellEditor( row, col ) != dat )
196 endEdit( currEditRow(), currEditCol(), false, false );
198 if ( !myCellEdit.contains( row ) )
199 myCellEdit.insert( row, DatumMap() );
201 myCellEdit[row].insert( col, dat );
205 QDS_Datum* QDS_Table::actualCellEditor( const int row, const int col ) const
207 QDS_Datum* dat = cellEditor( row, col );
209 dat = columnEditor( col );
211 dat = rowEditor( row );
217 void QDS_Table::setNumRows( int r )
220 QtxTable::setNumRows( r );
222 if ( isKeepEditors() )
223 myRowEdit.resize( QMAX( (int)myRowEdit.size(), r ) );
226 myRowEdit.resize( r );
227 for ( int i = r + 1; i <= old; i++ )
228 myCellEdit.remove( i );
232 void QDS_Table::setNumCols( int c )
235 QtxTable::setNumCols( c );
237 if ( isKeepEditors() )
238 myColEdit.resize( QMAX( (int)myColEdit.size(), c ) );
241 myColEdit.resize( c );
242 for ( CellMap::Iterator it = myCellEdit.begin(); it != myCellEdit.end(); ++it )
244 DatumMap& map = it.data();
245 for ( int i = c + 1; i <= old; i++ )
251 void QDS_Table::clearCellWidget( int row, int col )
253 QDS_Datum* dat = actualCellEditor( row, col );
257 QtxTable::clearCellWidget( row, col );
260 bool QDS_Table::isKeepEditors() const
265 void QDS_Table::setKeepEditors( const bool on )
270 QWidget* QDS_Table::createHeaderEditor( QHeader* header, const int sect, const bool init )
275 QDS_Datum* dat = headerEditor( header->orientation() );
276 QWidget* wid = dat ? dat->widget( QDS::Control ) : 0;
280 dat->setStringValue( header->label( sect ) );
283 dat->setProperty( "Selection", true );
286 wid = QtxTable::createHeaderEditor( header, sect, init );
291 QWidget* QDS_Table::createEditor( int row, int col, bool init ) const
293 QDS_Datum* dat = actualCellEditor( row, col );
294 QWidget* wid = dat ? dat->widget( QDS::Control ) : 0;
298 dat->setStringValue( text( row, col ) );
301 dat->setProperty( "Selection", true );
304 wid = QtxTable::createEditor( row, col, init );
309 void QDS_Table::endEdit( int row, int col, bool accept, bool )
311 QtxTable::endEdit( row, col, accept, true );
314 void QDS_Table::setCellContentFromEditor( int row, int col )
316 QDS_Datum* dat = actualCellEditor( row, col );
318 setText( row, col, dat->stringValue() );
320 QtxTable::setCellContentFromEditor( row, col );
323 void QDS_Table::setHeaderContentFromEditor( QHeader* header, const int sect, QWidget* editor )
328 QDS_Datum* dat = headerEditor( header->orientation() );
330 header->setLabel( sect, dat->stringValue() );
332 QtxTable::setHeaderContentFromEditor( header, sect, editor );
335 void QDS_Table::initEditor( QDS_Datum* dat )
342 static QGuardedPtr<DeleteFilter> _filter = 0;
344 _filter = new DeleteFilter( 0 );
346 if ( dat->widget( QDS::Control ) )
347 dat->widget( QDS::Control )->installEventFilter( _filter );