--- /dev/null
+// File: QtxTable.cxx
+// Author: Sergey TELKOV
+
+#include "QtxTable.h"
+
+#ifndef QT_NO_TABLE
+
+#include <qlineedit.h>
+
+QtxTable::QtxTable( QWidget* parent, const char* name )
+: QTable( parent, name ),
+myHeaderEditor( 0 ),
+myEditedHeader( 0 ),
+myEditedSection( -1 )
+{
+}
+
+QtxTable::QtxTable( int numRows, int numCols, QWidget* parent, const char* name )
+: QTable( numRows, numCols, parent, name ),
+myHeaderEditor( 0 ),
+myEditedHeader( 0 ),
+myEditedSection( -1 )
+{
+}
+
+QtxTable::~QtxTable()
+{
+}
+
+bool QtxTable::headerEditable( Orientation o ) const
+{
+ return myHeaderEditable.contains( o ) ? myHeaderEditable[o] : false;
+}
+
+void QtxTable::setHeaderEditable( Orientation o, const bool on )
+{
+ if ( headerEditable( o ) == on )
+ return;
+
+ myHeaderEditable.insert( o, on );
+
+ QHeader* hdr = header( o );
+
+ if ( !on && myEditedHeader == hdr )
+ endHeaderEdit( false );
+
+ if ( on )
+ hdr->installEventFilter( this );
+ else
+ hdr->removeEventFilter( this );
+}
+
+bool QtxTable::editHeader( Orientation o, const int sec )
+{
+ return beginHeaderEdit( o, sec );
+}
+
+void QtxTable::endEditHeader( const bool accept )
+{
+ endHeaderEdit( accept );
+}
+
+bool QtxTable::eventFilter( QObject* o, QEvent* e )
+{
+ if ( e->type() == QEvent::MouseButtonDblClick )
+ {
+ QMouseEvent* me = (QMouseEvent*)e;
+ if ( o == horizontalHeader() )
+ {
+ beginHeaderEdit( Horizontal, me->pos() );
+ return true;
+ }
+ else if ( o == verticalHeader() )
+ {
+ beginHeaderEdit( Vertical, me->pos() );
+ return true;
+ }
+ }
+
+ if ( o == myHeaderEditor && e->type() == QEvent::KeyPress && isHeaderEditing() )
+ {
+ QKeyEvent* ke = (QKeyEvent*)e;
+ if ( ke->key() == Key_Escape )
+ {
+ endHeaderEdit( false );
+ return true;
+ }
+
+ if ( ke->key() == Key_Return || ke->key() == Key_Enter )
+ {
+ endHeaderEdit( true );
+ return true;
+ }
+
+ return false;
+ }
+
+ if ( o == myHeaderEditor && e->type() == QEvent::FocusOut &&
+ isHeaderEditing() && ((QFocusEvent*)e)->reason() != QFocusEvent::Popup )
+ {
+ endHeaderEdit();
+ return true;
+ }
+
+ if ( e->type() == QEvent::Wheel && isHeaderEditing() )
+ return true;
+
+ return QTable::eventFilter( o, e );
+}
+
+bool QtxTable::beginHeaderEdit( Orientation o, const int section )
+{
+ if ( !headerEditable( o ) || !header( o ) || !header( o )->isVisibleTo( this ) )
+ return false;
+
+ endHeaderEdit();
+
+ QHeader* hdr = header( o );
+
+ QRect r = headerSectionRect( hdr, section );
+ if ( !r.isValid() )
+ return false;
+
+ if ( o == Horizontal )
+ r.setLeft( QMAX( r.left(), leftMargin() ) );
+ else
+ r.setTop( QMAX( r.top(), topMargin() ) );
+
+ myHeaderEditor = createHeaderEditor( hdr, section );
+ if ( !myHeaderEditor )
+ return false;
+
+ myEditedHeader = hdr;
+ myEditedSection = section;
+
+ myHeaderEditor->reparent( this, QPoint( 0, 0 ), false );
+ myHeaderEditor->resize( r.size() );
+ myHeaderEditor->move( r.topLeft() );
+ myHeaderEditor->show();
+ myHeaderEditor->setActiveWindow();
+ myHeaderEditor->setFocus();
+ myHeaderEditor->installEventFilter( this );
+
+ return true;
+}
+
+void QtxTable::endHeaderEdit( const bool accept )
+{
+ if ( !isHeaderEditing() )
+ return;
+
+ QString oldTxt = myEditedHeader ? myEditedHeader->label( myEditedSection ) : QString::null;
+
+ if ( accept && myEditedHeader )
+ setHeaderContentFromEditor( myEditedHeader, myEditedSection, myHeaderEditor );
+
+ QString newTxt = myEditedHeader ? myEditedHeader->label( myEditedSection ) : QString::null;
+
+ int sec = myEditedSection;
+ QHeader* hdr = myEditedHeader;
+
+ myEditedHeader = 0;
+ myEditedSection = -1;
+
+ myHeaderEditor->hide();
+ delete myHeaderEditor;
+ myHeaderEditor = 0;
+
+ if ( oldTxt != newTxt )
+ {
+ emit headerEdited( hdr, sec );
+ emit headerEdited( hdr == horizontalHeader() ? Horizontal : Vertical, sec );
+ }
+}
+
+bool QtxTable::isHeaderEditing() const
+{
+ return myHeaderEditor && myEditedHeader && myEditedSection != -1;
+}
+
+QWidget* QtxTable::createHeaderEditor( QHeader* hdr, const int sec, const bool init )
+{
+ QLineEdit* ed = new QLineEdit( 0 );
+
+ if ( init && hdr )
+ ed->setText( hdr->label( sec ) );
+
+ return ed;
+}
+
+void QtxTable::setHeaderContentFromEditor( QHeader* hdr, const int sec, QWidget* editor )
+{
+ if ( !hdr || !editor )
+ return;
+
+ if ( editor->inherits( "QLineEdit" ) )
+ hdr->setLabel( sec, ((QLineEdit*)editor)->text() );
+}
+
+QHeader* QtxTable::header( Orientation o ) const
+{
+ return o == Horizontal ? horizontalHeader() : verticalHeader();
+}
+
+void QtxTable::beginHeaderEdit( Orientation o, const QPoint& p )
+{
+ QHeader* hdr = header( o );
+ if ( !hdr )
+ return;
+
+ int pos = o == Horizontal ? p.x() : p.y();
+ int sec = hdr->sectionAt( hdr->offset() + pos );
+
+ beginHeaderEdit( o, sec );
+}
+
+QRect QtxTable::headerSectionRect( QHeader* hdr, const int sec ) const
+{
+ QRect r( -1, -1, -1, -1 );
+
+ if ( !hdr )
+ return r;
+
+ r = hdr->sectionRect( sec );
+ if ( r.isValid() )
+ r = QRect( mapFromGlobal( hdr->mapToGlobal( r.topLeft() ) ), r.size() );
+
+ return r;
+}
+
+#endif
--- /dev/null
+// File: QtxTable.h
+// Author: Sergey TELKOV
+
+#ifndef QTX_TABLE_H
+#define QTX_TABLE_H
+
+#include "Qtx.h"
+
+#include <qtable.h>
+
+#ifndef QT_NO_TABLE
+
+class QHeader;
+
+class QTX_EXPORT QtxTable : public QTable
+{
+ Q_OBJECT
+
+ class HeaderEditor;
+
+public:
+ QtxTable( QWidget* = 0, const char* = 0 );
+ QtxTable( int, int, QWidget* = 0, const char* = 0 );
+ virtual ~QtxTable();
+
+ bool headerEditable( Orientation ) const;
+
+ bool editHeader( Orientation, const int );
+ void endEditHeader( const bool = true );
+
+ virtual bool eventFilter( QObject*, QEvent* );
+
+signals:
+ void headerEdited( QHeader*, int );
+ void headerEdited( Orientation, int );
+
+public slots:
+ virtual void setHeaderEditable( Orientation, bool );
+
+protected:
+ virtual bool beginHeaderEdit( Orientation, const int );
+ virtual void endHeaderEdit( const bool = true );
+ bool isHeaderEditing() const;
+ virtual QWidget* createHeaderEditor( QHeader*, const int, const bool = true );
+ virtual void setHeaderContentFromEditor( QHeader*, const int, QWidget* );
+
+ QHeader* header( Orientation o ) const;
+
+private:
+ void beginHeaderEdit( Orientation, const QPoint& );
+ QRect headerSectionRect( QHeader*, const int ) const;
+
+private:
+ QWidget* myHeaderEditor;
+ QHeader* myEditedHeader;
+ int myEditedSection;
+ QMap<int, bool> myHeaderEditable;
+};
+
+#endif
+
+#endif