From 3e188f5047946aa9f538ad4addb099aeed31fd4a Mon Sep 17 00:00:00 2001 From: stv Date: Fri, 8 Sep 2006 12:39:52 +0000 Subject: [PATCH] QDS_Table - table supporting QDS_Datum as editors --- src/QDS/QDS_Table.cxx | 324 ++++++++++++++++++++++++++++++++++++++++++ src/QDS/QDS_Table.h | 93 ++++++++++++ 2 files changed, 417 insertions(+) create mode 100644 src/QDS/QDS_Table.cxx create mode 100644 src/QDS/QDS_Table.h diff --git a/src/QDS/QDS_Table.cxx b/src/QDS/QDS_Table.cxx new file mode 100644 index 000000000..4b63ec73a --- /dev/null +++ b/src/QDS/QDS_Table.cxx @@ -0,0 +1,324 @@ +// Copyright (C) 2005 CEA/DEN, EDF R&D, OPEN CASCADE, PRINCIPIA R&D +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License. +// +// This library is distributed in the hope that it will be useful +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// + +#include "QDS_Table.h" + +class QDS_Table::DeleteFilter : public QObject +{ +public: + DeleteFilter( QObject* = 0 ); + virtual ~DeleteFilter(); + + virtual bool eventFilter( QObject*, QEvent* ); +}; + +QDS_Table::DeleteFilter::DeleteFilter( QObject* parent ) +: QObject( parent ) +{ +} + +QDS_Table::DeleteFilter::~DeleteFilter() +{ +} + +bool QDS_Table::DeleteFilter::eventFilter( QObject* o, QEvent* e ) +{ + if ( e->type() == QEvent::DeferredDelete ) + return true; + else + return false; +} + +QDS_Table::QDS_Table( QWidget* parent, const char* name ) +: QtxTable( parent, name ), +myHorEdit( 0 ), +myVerEdit( 0 ), +myTableEdit( 0 ) +{ +} + +QDS_Table::QDS_Table( int r, int c, QWidget* parent, const char* name ) +: QtxTable( r, c, parent, name ), +myHorEdit( 0 ), +myVerEdit( 0 ), +myTableEdit( 0 ) +{ + myRowEdit.resize( r ); + myColEdit.resize( c ); +} + +QDS_Table::~QDS_Table() +{ +} + +QDS_Datum* QDS_Table::horizontalHeaderEditor() const +{ + return myHorEdit; +} + +QDS_Datum* QDS_Table::verticalHeaderEditor() const +{ + return myVerEdit; +} + +QDS_Datum* QDS_Table::headerEditor( const Orientation o ) const +{ + return o == Horizontal ? myHorEdit : myVerEdit; +} + +void QDS_Table::setVerticalHeaderEditor( QDS_Datum* dat ) +{ + setHeaderEditor( Vertical, dat ); +} + +void QDS_Table::setHorizontalHeaderEditor( QDS_Datum* dat ) +{ + setHeaderEditor( Horizontal, dat ); +} + +void QDS_Table::setHeaderEditor( QDS_Datum* dat ) +{ + setHeaderEditor( Vertical, dat ); + setHeaderEditor( Horizontal, dat ); +} + +void QDS_Table::setHeaderEditor( const Orientation o, QDS_Datum* dat ) +{ + if ( headerEditor( o ) == dat ) + return; + + if ( isHeaderEditing() ) + endEditHeader(); + + if ( o == Horizontal ) + myHorEdit = dat; + else + myVerEdit = dat; + + initEditor( dat ); +} + +QDS_Datum* QDS_Table::tableEditor() const +{ + return myTableEdit; +} + +QDS_Datum* QDS_Table::rowEditor( const int row ) const +{ + if ( row < 0 || row >= (int)myRowEdit.size() ) + return 0; + + return myRowEdit.at( row ); +} + +QDS_Datum* QDS_Table::columnEditor( const int col ) const +{ + if ( col < 0 || col >= (int)myColEdit.size() ) + return 0; + + return myColEdit.at( col ); +} + +QDS_Datum* QDS_Table::cellEditor( const int row, const int col ) const +{ + if ( !myCellEdit.contains( row ) ) + return 0; + + const DatumMap& map = myCellEdit[row]; + return map.contains( col ) ? map[col] : 0; +} + +void QDS_Table::setTableEditor( QDS_Datum* dat ) +{ + if ( tableEditor() == dat ) + return; + + if ( isEditing() && !cellEditor( currEditRow(), currEditCol() ) && + !columnEditor( currEditCol() ) && !rowEditor( currEditRow() ) ) + endEdit( currEditRow(), currEditCol(), false, false ); + + myTableEdit = dat; + initEditor( dat ); +} + +void QDS_Table::setRowEditor( const int row, QDS_Datum* dat ) +{ + if ( row < 0 || row >= (int)myRowEdit.size() || rowEditor( row ) == dat ) + return; + + if ( isEditing() && row == currEditRow()&& + !cellEditor( currEditRow(), currEditCol() ) ) + endEdit( currEditRow(), currEditCol(), false, false ); + + myRowEdit.insert( row, dat ); + initEditor( dat ); +} + +void QDS_Table::setColumnEditor( const int col, QDS_Datum* dat ) +{ + if ( col < 0 || col >= (int)myColEdit.size() || columnEditor( col ) == dat ) + return; + + if ( isEditing() && col == currEditCol()&& + !cellEditor( currEditRow(), currEditCol() ) ) + endEdit( currEditRow(), currEditCol(), false, false ); + + myColEdit.insert( col, dat ); + initEditor( dat ); +} + +void QDS_Table::setCellEditor( const int row, const int col, QDS_Datum* dat ) +{ + if ( row < 0 || row >= numRows() || col < 0 || col >= numCols() || cellEditor( row, col ) == dat ) + return; + + if ( isEditing() && currEditRow() == row && currEditCol() == col && actualCellEditor( row, col ) != dat ) + endEdit( currEditRow(), currEditCol(), false, false ); + + if ( !myCellEdit.contains( row ) ) + myCellEdit.insert( row, DatumMap() ); + + myCellEdit[row].insert( col, dat ); + initEditor( dat ); +} + +QDS_Datum* QDS_Table::actualCellEditor( const int row, const int col ) const +{ + QDS_Datum* dat = cellEditor( row, col ); + if ( !dat ) + dat = columnEditor( col ); + if ( !dat ) + dat = rowEditor( row ); + if ( !dat ) + dat = tableEditor(); + return dat; +} + +void QDS_Table::setNumRows( int r ) +{ + int old = numRows(); + QtxTable::setNumRows( r ); + myRowEdit.resize( r ); + + for ( int i = r + 1; i <= old; i++ ) + myCellEdit.remove( i ); +} + +void QDS_Table::setNumCols( int c ) +{ + int old = numCols(); + QtxTable::setNumCols( c ); + myColEdit.resize( c ); + + for ( CellMap::Iterator it = myCellEdit.begin(); it != myCellEdit.end(); ++it ) + { + DatumMap& map = it.data(); + for ( int i = c + 1; i <= old; i++ ) + map.remove( i ); + } +} + +void QDS_Table::clearCellWidget( int row, int col ) +{ + QDS_Datum* dat = actualCellEditor( row, col ); + if ( dat ) + dat->hide(); + + QtxTable::clearCellWidget( row, col ); +} + +QWidget* QDS_Table::createHeaderEditor( QHeader* header, const int sect, const bool init ) +{ + if ( !header ) + return 0; + + QDS_Datum* dat = headerEditor( header->orientation() ); + QWidget* wid = dat ? dat->widget( QDS::Control ) : 0; + if ( wid ) + { + if ( init ) + dat->setStringValue( header->label( sect ) ); + else + dat->clear(); +// dat->selectAll(); + } + else + wid = QtxTable::createHeaderEditor( header, sect, init ); + + return wid; +} + +QWidget* QDS_Table::createEditor( int row, int col, bool init ) const +{ + QDS_Datum* dat = actualCellEditor( row, col ); + QWidget* wid = dat ? dat->widget( QDS::Control ) : 0; + if ( wid ) + { + if ( init ) + dat->setStringValue( text( row, col ) ); + else + dat->clear(); +// dat->selectAll(); + } + else + wid = QtxTable::createEditor( row, col, init ); + + return wid; +} + +void QDS_Table::endEdit( int row, int col, bool accept, bool ) +{ + QtxTable::endEdit( row, col, accept, true ); +} + +void QDS_Table::setCellContentFromEditor( int row, int col ) +{ + QDS_Datum* dat = actualCellEditor( row, col ); + if ( dat ) + setText( row, col, dat->stringValue() ); + else + QtxTable::setCellContentFromEditor( row, col ); +} + +void QDS_Table::setHeaderContentFromEditor( QHeader* header, const int sect, QWidget* editor ) +{ + if ( !header ) + return; + + QDS_Datum* dat = headerEditor( header->orientation() ); + if ( dat ) + header->setLabel( sect, dat->stringValue() ); + else + QtxTable::setHeaderContentFromEditor( header, sect, editor ); +} + +void QDS_Table::initEditor( QDS_Datum* dat ) +{ + if ( !dat ) + return; + + dat->hide(); + + static QGuardedPtr _filter = 0; + if ( !_filter ) + _filter = new DeleteFilter( 0 ); + + if ( dat->widget( QDS::Control ) ) + dat->widget( QDS::Control )->installEventFilter( _filter ); +} diff --git a/src/QDS/QDS_Table.h b/src/QDS/QDS_Table.h new file mode 100644 index 000000000..89a2cad98 --- /dev/null +++ b/src/QDS/QDS_Table.h @@ -0,0 +1,93 @@ +// Copyright (C) 2005 CEA/DEN, EDF R&D, OPEN CASCADE, PRINCIPIA R&D +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License. +// +// This library is distributed in the hope that it will be useful +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// +#ifndef QDS_TABLE_H +#define QDS_TABLE_H + +#include "QDS_Datum.h" + +#include + +#include +#include + +class QDS_EXPORT QDS_Table : public QtxTable +{ + class DeleteFilter; + + Q_OBJECT + +public: + QDS_Table( QWidget* = 0, const char* = 0 ); + QDS_Table( int, int, QWidget* = 0, const char* = 0 ); + virtual ~QDS_Table(); + + QDS_Datum* verticalHeaderEditor() const; + QDS_Datum* horizontalHeaderEditor() const; + QDS_Datum* headerEditor( const Orientation ) const; + + void setVerticalHeaderEditor( QDS_Datum* ); + void setHorizontalHeaderEditor( QDS_Datum* ); + + void setHeaderEditor( QDS_Datum* ); + virtual void setHeaderEditor( const Orientation, QDS_Datum* ); + + QDS_Datum* tableEditor() const; + virtual void setTableEditor( QDS_Datum* ); + + QDS_Datum* rowEditor( const int ) const; + QDS_Datum* columnEditor( const int ) const; + + virtual void setRowEditor( const int, QDS_Datum* ); + virtual void setColumnEditor( const int, QDS_Datum* ); + + QDS_Datum* cellEditor( const int, const int ) const; + virtual void setCellEditor( const int, const int, QDS_Datum* ); + + QDS_Datum* actualCellEditor( const int, const int ) const; + + virtual void setNumRows( int ); + virtual void setNumCols( int ); + virtual void clearCellWidget( int, int ); + +protected: + virtual QWidget* createHeaderEditor( QHeader*, const int, const bool = true ); + virtual QWidget* createEditor( int, int, bool ) const; + virtual void endEdit( int, int, bool, bool ); + virtual void setCellContentFromEditor( int, int ); + virtual void setHeaderContentFromEditor( QHeader*, const int, QWidget* ); + +private: + void initEditor( QDS_Datum* ); + QDS_Datum* datum( const QWidget* ) const; + +private: + typedef QPtrVector DatumVector; + typedef QMap DatumMap; + typedef QMap CellMap; + +private: + QDS_Datum* myHorEdit; + QDS_Datum* myVerEdit; + DatumVector myRowEdit; + DatumVector myColEdit; + CellMap myCellEdit; + QDS_Datum* myTableEdit; +}; + +#endif -- 2.39.2