From 14f618c7c6567604cc3d0b28ba1858bd5a7f73b6 Mon Sep 17 00:00:00 2001 From: stv Date: Thu, 21 Jul 2005 06:26:36 +0000 Subject: [PATCH] QtxTable added --- src/Qtx/Makefile.in | 3 + src/Qtx/QtxTable.cxx | 231 +++++++++++++++++++++++++++++++++++++++++++ src/Qtx/QtxTable.h | 62 ++++++++++++ 3 files changed, 296 insertions(+) create mode 100644 src/Qtx/QtxTable.cxx create mode 100644 src/Qtx/QtxTable.h diff --git a/src/Qtx/Makefile.in b/src/Qtx/Makefile.in index 186179a77..ffcc7f2e7 100755 --- a/src/Qtx/Makefile.in +++ b/src/Qtx/Makefile.in @@ -32,6 +32,7 @@ EXPORT_HEADERS= Qtx.h \ QtxPopupMgr.h \ QtxResourceMgr.h \ QtxStdOperations.h \ + QtxTable.h \ QtxToolBar.h \ QtxToolTip.h \ QtxParser.h \ @@ -75,6 +76,7 @@ LIB_SRC= \ QtxPopupMgr.cxx \ QtxResourceMgr.cxx \ QtxStdOperations.cxx \ + QtxTable.cxx \ QtxToolBar.cxx \ QtxToolTip.cxx \ QtxOperations.cxx \ @@ -108,6 +110,7 @@ LIB_MOC = \ QtxPathDialog.h \ QtxPopupMgr.h \ QtxPopupMenu.h \ + QtxTable.h \ QtxToolBar.h \ QtxToolTip.h \ QtxActionMgr.h \ diff --git a/src/Qtx/QtxTable.cxx b/src/Qtx/QtxTable.cxx new file mode 100644 index 000000000..6c56918d6 --- /dev/null +++ b/src/Qtx/QtxTable.cxx @@ -0,0 +1,231 @@ +// File: QtxTable.cxx +// Author: Sergey TELKOV + +#include "QtxTable.h" + +#ifndef QT_NO_TABLE + +#include + +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 diff --git a/src/Qtx/QtxTable.h b/src/Qtx/QtxTable.h new file mode 100644 index 000000000..644c8a2e4 --- /dev/null +++ b/src/Qtx/QtxTable.h @@ -0,0 +1,62 @@ +// File: QtxTable.h +// Author: Sergey TELKOV + +#ifndef QTX_TABLE_H +#define QTX_TABLE_H + +#include "Qtx.h" + +#include + +#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 myHeaderEditable; +}; + +#endif + +#endif -- 2.39.2