From 1990da2e776f2eae03c4f880718cbf234a4beea2 Mon Sep 17 00:00:00 2001 From: stv Date: Thu, 16 Aug 2007 12:08:56 +0000 Subject: [PATCH] Renaming by user of table columns --- src/Qtx/QtxTable.cxx | 163 ++++++++++++++++++++++++++++++++++++++----- src/Qtx/QtxTable.h | 17 +++-- 2 files changed, 160 insertions(+), 20 deletions(-) diff --git a/src/Qtx/QtxTable.cxx b/src/Qtx/QtxTable.cxx index e2b6fcb4f..097a65934 100644 --- a/src/Qtx/QtxTable.cxx +++ b/src/Qtx/QtxTable.cxx @@ -760,9 +760,10 @@ void QtxTable::setSelectionMode( SelectionMode mode ) \return true if header is editable \param o - header orientation */ -bool QtxTable::headerEditable( Orientation o ) const +bool QtxTable::headerEditable( Orientation o, const int idx ) const { - return myHeaderEditable.contains( o ) ? myHeaderEditable[o] : false; + QHeader* hdr = idx < 0 ? header( o ) : header( o, idx ); + return myHeaderEditable.contains( hdr ) ? myHeaderEditable[hdr] : false; } /*! @@ -770,14 +771,13 @@ bool QtxTable::headerEditable( Orientation o ) const \param o - header orientation \param on - new state */ -void QtxTable::setHeaderEditable( Orientation o, const bool on ) +void QtxTable::setHeaderEditable( Orientation o, const bool on, const int idx ) { - if ( headerEditable( o ) == on ) + if ( headerEditable( o, idx ) == on ) return; - myHeaderEditable.insert( o, on ); - - QHeader* hdr = header( o ); + QHeader* hdr = idx < 0 ? header( o ) : header( o, idx ); + myHeaderEditable.insert( hdr, on ); if ( !on && myEditedHeader == hdr ) endHeaderEdit( false ); @@ -788,6 +788,13 @@ void QtxTable::setHeaderEditable( Orientation o, const bool on ) hdr->removeEventFilter( this ); } +void QtxTable::setHeadersEditable( Orientation o, bool on ) +{ + setHeaderEditable( o, on ); + for ( int i = 0; i < numHeaders( o ); i++ ) + setHeaderEditable( o, on, i ); +} + /*! Starts edition of header \param o - header orientation @@ -819,10 +826,7 @@ bool QtxTable::eventFilter( QObject* o, QEvent* e ) QMouseEvent* me = (QMouseEvent*)e; QHeader* hdr = ::qt_cast( o ); if ( hdr ) - { - beginHeaderEdit( hdr, me->pos() ); - return true; - } + return beginHeaderEdit( hdr, me->pos() ); } if ( o == myHeaderEditor && e->type() == QEvent::KeyPress && isHeaderEditing() ) @@ -856,6 +860,105 @@ bool QtxTable::eventFilter( QObject* o, QEvent* e ) return QTable::eventFilter( o, e ); } +void QtxTable::adjustRow( int row ) +{ + QTable::adjustRow( row ); + + int size = 0; + for ( uint i = 0; i < myVerHeaders.count(); i++ ) + { + QHeader* hdr = myVerHeaders.at( i ); + QString txt = hdr->label( row ); + int h = hdr->iconSet( row ) ? hdr->iconSet( row )->pixmap().height() : 0; + if ( !QStyleSheet::mightBeRichText( txt ) ) + h = QMAX( h, hdr->fontMetrics().height() ); + else + { + QStyleSheet sheet; + QStyleSheetItem* item = sheet.item( "p" ); + if ( item ) + item->setMargin( QStyleSheetItem::MarginAll, 0 ); + + QSimpleRichText rt( txt, hdr->font(), QString::null, &sheet ); + rt.setWidth( hdr->width() ); + h = QMAX( h, rt.height() ); + } + size = QMAX( size, h ); + } + + size = QMAX( size, rowHeight( row ) ); + setRowHeight( row, size ); + +} + +void QtxTable::adjustColumn( int col ) +{ + QTable::adjustColumn( col ); + + int size = 0; + for ( uint i = 0; i < myHorHeaders.count(); i++ ) + { + Header* hdr = (Header*)myHorHeaders.at( i ); + QString txt = hdr->label( col ); + int w = hdr->iconSet( col ) ? + hdr->iconSet( col )->pixmap( QIconSet::Automatic, QIconSet::Normal ).width() + 5 : 0; + int txtW = 0; + if ( !QStyleSheet::mightBeRichText( txt ) ) + txtW += hdr->fontMetrics().width( txt ); + else + { + QStyleSheet sheet; + QStyleSheetItem* item = sheet.item( "p" ); + if ( item ) + item->setMargin( QStyleSheetItem::MarginAll, 0 ); + + QSimpleRichText rt( txt, hdr->font(), QString::null, &sheet ); + txtW += rt.width(); + } + + if ( hdr->horizontalSpan( col ) > 1 ) + txtW = txtW / hdr->horizontalSpan( col ); + + w = w + txtW + 10; + + size = QMAX( size, w ); + } + + for ( int i = 0; i < numRows(); i++ ) + { + QFont fnt = cellFont( i, col ); + int sz = QFontMetrics( fnt ).width( text( i, col ) ) + 10; + size = QMAX( size, sz ); + } + + size = QMAX( size, columnWidth( col ) ); + setColumnWidth( col, size ); +} + +void QtxTable::rowHeightChanged( int row ) +{ + QTable::rowHeightChanged( row ); + + int h = rowHeight( row ); + for ( uint i = 0; i < myVerHeaders.count(); i++ ) + { + QHeader* hdr = myVerHeaders.at( i ); + hdr->resizeSection( row, h ); + } +} + +void QtxTable::columnWidthChanged( int col ) +{ + QTable::columnWidthChanged( col ); + + int w = columnWidth( col ); + for ( uint i = 0; i < myHorHeaders.count(); i++ ) + { + QHeader* hdr = myHorHeaders.at( i ); + hdr->resizeSection( col, w ); + } +} + /*! Reimplemented for internal reasons. Firstly end the edition of header. @@ -1439,7 +1542,7 @@ bool QtxTable::beginHeaderEdit( Orientation o, const int section, const int idx bool QtxTable::beginHeaderEdit( QHeader* hdr, const int s ) { - if ( !hdr || !headerEditable( hdr->orientation() ) || !hdr->isVisibleTo( this ) ) + if ( !hdr || !myHeaderEditable.contains( hdr ) || !myHeaderEditable[hdr] || !hdr->isVisibleTo( this ) ) return false; endHeaderEdit(); @@ -1538,6 +1641,8 @@ QWidget* QtxTable::createHeaderEditor( QHeader* hdr, const int sec, const bool i else { QTextEdit* te = new QTextEdit( 0 ); + te->setVScrollBarMode( QTextEdit::AlwaysOff ); + te->setHScrollBarMode( QTextEdit::AlwaysOff ); QStyleSheet* sheet = new QStyleSheet( te ); QStyleSheetItem* i = sheet->item( "p" ); if ( i ) @@ -1570,7 +1675,7 @@ void QtxTable::setHeaderContentFromEditor( QHeader* hdr, const int sec, QWidget* { QTextEdit* te = ::qt_cast( editor ); if ( te ) - hdr->setLabel( sec, te->text() ); + hdr->setLabel( sec, te->text().remove( "\n" ) ); } } @@ -1588,15 +1693,37 @@ QHeader* QtxTable::header( Orientation o ) const \param o - header orientation \param p - point */ -void QtxTable::beginHeaderEdit( QHeader* hdr, const QPoint& p ) +bool QtxTable::beginHeaderEdit( QHeader* hdr, const QPoint& p ) { if ( !hdr ) - return; + return false; int pos = hdr->orientation() == Horizontal ? p.x() : p.y(); int sec = hdr->sectionAt( hdr->offset() + pos ); + if ( sec < 0 ) + return false; + + bool ok = false; + QPoint pnt = mapFromGlobal( hdr->mapToGlobal( p ) ); + int cur = hdr->orientation() == Horizontal ? pnt.x() : pnt.y(); + + int bid = 0; + QRect sRect = headerSectionRect( hdr, sec, &bid ); + if ( sRect.isValid() ) + { + int m = 4; + int from = hdr->orientation() == Horizontal ? sRect.left() : sRect.top(); + int to = hdr->orientation() == Horizontal ? sRect.right() - m : sRect.bottom() - m; + if ( sec > 0 ) + from += m; - beginHeaderEdit( hdr, sec ); + ok = from <= cur && cur <= to; + } + + if ( ok ) + beginHeaderEdit( hdr, sec ); + + return ok; } /*! @@ -1721,6 +1848,10 @@ void QtxTable::updateHeaders( const Orientation o ) hdr->show(); connect( sb, SIGNAL( valueChanged( int ) ), hdr, SLOT( setOffset( int ) ) ); + if ( o == Horizontal ) + connect( hdr, SIGNAL( sectionHandleDoubleClicked( int ) ), this, SLOT( adjustColumn( int ) ) ); + else + connect( hdr, SIGNAL( sectionHandleDoubleClicked( int ) ), this, SLOT( adjustRow( int ) ) ); } if ( o == Horizontal ) diff --git a/src/Qtx/QtxTable.h b/src/Qtx/QtxTable.h index eb7fee0ec..e95dfc155 100644 --- a/src/Qtx/QtxTable.h +++ b/src/Qtx/QtxTable.h @@ -56,7 +56,7 @@ public: QtxTable( int, int, QWidget* = 0, const char* = 0 ); virtual ~QtxTable(); - bool headerEditable( Orientation ) const; + bool headerEditable( Orientation, const int = -1 ) const; bool editHeader( Orientation, const int ); void endEditHeader( const bool = true ); @@ -107,7 +107,8 @@ public slots: virtual void selectAll(); virtual void setTopMargin( int ); virtual void setLeftMargin( int ); - virtual void setHeaderEditable( Orientation, bool ); + void setHeadersEditable( Orientation, bool ); + virtual void setHeaderEditable( Orientation, bool, const int = -1 ); virtual void insertRows( int, int = 1 ); virtual void insertColumns( int, int = 1 ); @@ -118,6 +119,13 @@ public slots: virtual void setUpdatesEnabled( bool enable ); + virtual void adjustRow( int ); + virtual void adjustColumn( int ); + +protected slots: + virtual void rowHeightChanged( int ); + virtual void columnWidthChanged( int ); + private slots: void onScrollBarMoved( int ); void onHeaderSizeChange( int, int, int ); @@ -144,12 +152,13 @@ private: typedef QMap Properties; typedef QMap PropsMap; typedef QMap CellMap; + typedef QMap HeaderState; typedef QPtrVector HeaderVector; private: void updateHeaderEditor(); bool beginHeaderEdit( QHeader*, const int ); - void beginHeaderEdit( QHeader*, const QPoint& ); + bool beginHeaderEdit( QHeader*, const QPoint& ); QtxStyleWrap* styleWrapper(); HeaderVector* headerVector( const Orientation ) const; @@ -171,7 +180,7 @@ private: QWidget* myHeaderEditor; QHeader* myEditedHeader; int myEditedSection; - QMap myHeaderEditable; + HeaderState myHeaderEditable; QToolButton* mySelectAll; -- 2.39.2