From adc6d6e292ed180a8f1f42cb9a145e8515761380 Mon Sep 17 00:00:00 2001 From: vsr Date: Thu, 26 Jul 2007 08:54:27 +0000 Subject: [PATCH] *** empty log message *** --- src/Qtx/QtxTreeView.cxx | 105 +++++++++++++++++++++++++++++++++++++--- src/Qtx/QtxTreeView.h | 7 +++ 2 files changed, 106 insertions(+), 6 deletions(-) diff --git a/src/Qtx/QtxTreeView.cxx b/src/Qtx/QtxTreeView.cxx index ef1801ad4..908ad086d 100644 --- a/src/Qtx/QtxTreeView.cxx +++ b/src/Qtx/QtxTreeView.cxx @@ -35,20 +35,28 @@ class QtxTreeView::Header : public QHeaderView { public: - Header( QWidget* parent = 0 ); + Header( const bool, QWidget* = 0 ); ~Header(); + void setSortMenuEnabled( const bool ); + bool sortMenuEnabled() const; + protected: - void contextMenuEvent( QContextMenuEvent* ); + void contextMenuEvent( QContextMenuEvent* ); + +private: + bool myEnableSortMenu; }; /*! \brief Constructor + \param enableSortMenu show "Sorting" menu if \c true \param parent parent widget \internal */ -QtxTreeView::Header::Header( QWidget* parent ) -: QHeaderView( Qt::Horizontal, parent ) +QtxTreeView::Header::Header( const bool enableSortMenu, QWidget* parent ) +: QHeaderView( Qt::Horizontal, parent ), + myEnableSortMenu( enableSortMenu ) { } @@ -60,6 +68,26 @@ QtxTreeView::Header::~Header() { } +/* + \brief Enable/disable "Sorting" popup menu command for the header. + \param enableSortMenu if \c true, enable "Sorting" menu command + \internal +*/ +void QtxTreeView::Header::setSortMenuEnabled( const bool enableSortMenu ) +{ + myEnableSortMenu = enableSortMenu; +} + +/* + \brief Check if "Sorting" popup menu command for the header is enabled. + \return \c true if "Sorting" menu command is enabled + \internal +*/ +bool QtxTreeView::Header::sortMenuEnabled() const +{ + return myEnableSortMenu; +} + /*! \brief Customize context menu event. \internal @@ -92,10 +120,34 @@ void QtxTreeView::Header::contextMenuEvent( QContextMenuEvent* e ) actionMap.insert( a, i ); } } + QAction* sortAction = 0; + if ( count() > 0 && myEnableSortMenu ) { + menu.addSeparator(); + sortAction = menu.addAction( tr( "Enable sorting" ) ); + sortAction->setCheckable( true ); + sortAction->setChecked( isSortIndicatorShown() ); + } if ( !menu.isEmpty() ) { + Qtx::simplifySeparators( &menu ); QAction* a = menu.exec( e->globalPos() ); - if ( a && actionMap.contains( a ) ) + if ( a && actionMap.contains( a ) ) { setSectionHidden( actionMap[ a ], !isSectionHidden( actionMap[ a ] ) ); + } + else if ( a && a == sortAction ) { + setSortIndicatorShown( a->isChecked() ); + setClickable( a->isChecked() ); + QtxTreeView* view = qobject_cast( parent() ); + if ( view ) { + if ( a->isChecked() ) { + connect( this, SIGNAL( sectionClicked( int ) ), view, SLOT( onHeaderClicked( int ) ) ); + view->sortByColumn( sortIndicatorSection(), sortIndicatorOrder() ); + } + else { + disconnect( this, SIGNAL( sectionClicked( int ) ), view, SLOT( onHeaderClicked( int ) ) ); + view->sortByColumn( 0, Qt::AscendingOrder ); + } + } + } } e->accept(); } @@ -124,7 +176,18 @@ void QtxTreeView::Header::contextMenuEvent( QContextMenuEvent* e ) QtxTreeView::QtxTreeView( QWidget* parent ) : QTreeView( parent ) { - setHeader( new Header() ); + setHeader( new Header( false, this ) ); +} + +/*! + \brief Constructor. + \param enableSortMenu show "Sorting" header menu command if \c true + \param parent parent widget +*/ +QtxTreeView::QtxTreeView( const bool enableSortMenu, QWidget* parent ) +: QTreeView( parent ) +{ + setHeader( new Header( enableSortMenu, this ) ); } /*! @@ -162,6 +225,36 @@ void QtxTreeView::collapseLevels( const int levels ) setOpened( rootIndex(), levels+1, false ); } +/* + \brief Enable/disable "Sorting" popup menu command for the header. + \param enableSortMenu if \c true, enable "Sorting" menu command +*/ +void QtxTreeView::setSortMenuEnabled( const bool enableSortMenu ) +{ + Header* h = dynamic_cast( header() ); + if ( h ) + h->setSortMenuEnabled( enableSortMenu ); +} + +/* + \brief Check if "Sorting" popup menu command for the header is enabled. + \return \c true if "Sorting" menu command is enabled +*/ +bool QtxTreeView::sortMenuEnabled() const +{ + Header* h = dynamic_cast( header() ); + return h ? h->sortMenuEnabled() : false; +} + +/* + \brief Called when the header section is clicked. + \param column header column index +*/ +void QtxTreeView::onHeaderClicked( int column ) +{ + sortByColumn( column, header()->sortIndicatorOrder() ); +} + /*! \brief Expand/collapse the specified item (recursively). \param index model index diff --git a/src/Qtx/QtxTreeView.h b/src/Qtx/QtxTreeView.h index 03e542065..2ad69a91d 100644 --- a/src/Qtx/QtxTreeView.h +++ b/src/Qtx/QtxTreeView.h @@ -39,11 +39,18 @@ class QTX_EXPORT QtxTreeView : public QTreeView public: QtxTreeView( QWidget* = 0 ); + QtxTreeView( const bool, QWidget* = 0 ); virtual ~QtxTreeView(); void expandLevels( const int ); void collapseLevels( const int ); + void setSortMenuEnabled( const bool ); + bool sortMenuEnabled() const; + +protected slots: + void onHeaderClicked( int ); + protected: void setOpened( const QModelIndex&, const int, bool ); }; -- 2.39.2