From 16295296c07fe407990f7059f68fcb2dc55826e5 Mon Sep 17 00:00:00 2001 From: sbh Date: Wed, 17 Jul 2013 13:52:58 +0000 Subject: [PATCH] New behavior TabBar of the QtxWorkstack: show close (cross) button on each tab --- src/Qtx/QtxWorkstack.cxx | 129 ++------------------------------------- src/Qtx/QtxWorkstack.h | 3 +- 2 files changed, 6 insertions(+), 126 deletions(-) diff --git a/src/Qtx/QtxWorkstack.cxx b/src/Qtx/QtxWorkstack.cxx index d8621f0d0..7c7911cbb 100644 --- a/src/Qtx/QtxWorkstack.cxx +++ b/src/Qtx/QtxWorkstack.cxx @@ -251,121 +251,6 @@ void QtxWorkstackDrag::startDrawRect() } -/* - \class CloseButton - \brief Workstack area close button. - \internal -*/ -class CloseButton : public QAbstractButton -{ -public: - CloseButton( QWidget* ); - - QSize sizeHint() const; - QSize minimumSizeHint() const; - - void enterEvent( QEvent* ); - void leaveEvent( QEvent* ); - void paintEvent( QPaintEvent* ); -}; - -/*! - \brief Constructor - \internal - \param parent parent widget -*/ -CloseButton::CloseButton( QWidget* parent ) -: QAbstractButton( parent ) -{ - setFocusPolicy( Qt::NoFocus ); -} - -/*! - \brief Get appropriate size for the button. - \internal - \return size value -*/ -QSize CloseButton::sizeHint() const -{ - ensurePolished(); - int dim = 0; - if( !icon().isNull() ) - { - const QPixmap pm = icon().pixmap( style()->pixelMetric( QStyle::PM_SmallIconSize ), - QIcon::Normal ); - dim = qMax( pm.width(), pm.height() ); - } - return QSize( dim + 4, dim + 4 ); -} - -/*! - \brief Get minimum appropriate size for the button. - \internal - \return minimum size value -*/ -QSize CloseButton::minimumSizeHint() const -{ - return sizeHint(); -} - -/*! - \brief Process mouse enter event. - \internal - \param event mouse enter event -*/ -void CloseButton::enterEvent( QEvent *event ) -{ - if ( isEnabled() ) - update(); - QAbstractButton::enterEvent( event ); -} - -/*! - \brief Process mouse leave event. - \internal - \param event mouse leave event -*/ -void CloseButton::leaveEvent( QEvent *event ) -{ - if( isEnabled() ) - update(); - QAbstractButton::leaveEvent( event ); -} - -/*! - \brief Process paint event. - \internal - \param event paint event -*/ -void CloseButton::paintEvent( QPaintEvent* ) -{ - QPainter p( this ); - - QRect r = rect(); - QStyleOption opt; - opt.init( this ); - opt.state |= QStyle::State_AutoRaise; - if ( isEnabled() && underMouse() && !isChecked() && !isDown() ) - opt.state |= QStyle::State_Raised; - if ( isChecked() ) - opt.state |= QStyle::State_On; - if ( isDown() ) - opt.state |= QStyle::State_Sunken; - style()->drawPrimitive( QStyle::PE_PanelButtonTool, &opt, &p, this ); - - int shiftHorizontal = opt.state & QStyle::State_Sunken ? style()->pixelMetric( QStyle::PM_ButtonShiftHorizontal, &opt, this ) : 0; - int shiftVertical = opt.state & QStyle::State_Sunken ? style()->pixelMetric( QStyle::PM_ButtonShiftVertical, &opt, this ) : 0; - - r.adjust( 2, 2, -2, -2 ); - r.translate( shiftHorizontal, shiftVertical ); - - QPixmap pm = icon().pixmap( style()->pixelMetric( QStyle::PM_SmallIconSize ), isEnabled() ? - underMouse() ? QIcon::Active : QIcon::Normal - : QIcon::Disabled, - isDown() ? QIcon::On : QIcon::Off ); - style()->drawItemPixmap( &p, r, Qt::AlignCenter, pm ); -} - /*! \class QtxWorkstackArea \internal @@ -393,17 +278,13 @@ QtxWorkstackArea::QtxWorkstackArea( QWidget* parent ) myBar = new QtxWorkstackTabBar( top ); tl->addWidget( myBar, 1 ); - - CloseButton* close = new CloseButton( top ); - close->setIcon( style()->standardIcon( QStyle::SP_TitleBarCloseButton ) ); - myClose = close; - tl->addWidget( myClose ); + myBar->setTabsClosable( true ); myStack = new QStackedWidget( this ); base->addWidget( myStack, 1 ); - connect( myClose, SIGNAL( clicked() ), this, SLOT( onClose() ) ); + connect( myBar, SIGNAL( tabCloseRequested( int ) ), this, SLOT( onClose( int ) ) ); connect( myBar, SIGNAL( currentChanged( int ) ), this, SLOT( onCurrentChanged( int ) ) ); connect( myBar, SIGNAL( dragActiveTab() ), this, SLOT( onDragActiveTab() ) ); connect( myBar, SIGNAL( contextMenuRequested( QPoint ) ), this, SLOT( onContextMenuRequested( QPoint ) ) ); @@ -665,7 +546,7 @@ bool QtxWorkstackArea::eventFilter( QObject* o, QEvent* e ) if ( e->type() == QEvent::FocusIn || e->type() == QEvent::MouseButtonPress ) { bool ok = false; - while ( !ok && wid && wid != myClose ) + while ( !ok && wid ) { ok = wid == this; wid = wid->parentWidget(); @@ -800,9 +681,9 @@ void QtxWorkstackArea::mousePressEvent( QMouseEvent* e ) /*! \brief Called when user presses "Close" button. */ -void QtxWorkstackArea::onClose() +void QtxWorkstackArea::onClose( int idx ) { - QWidget* wid = activeWidget(); + QWidget* wid = widget( myBar->tabId( idx ) ); if ( wid ) wid->close(); } diff --git a/src/Qtx/QtxWorkstack.h b/src/Qtx/QtxWorkstack.h index 7037ae655..c00401deb 100644 --- a/src/Qtx/QtxWorkstack.h +++ b/src/Qtx/QtxWorkstack.h @@ -214,7 +214,7 @@ public slots: virtual void setVisible( bool ); private slots: - void onClose(); + void onClose( int ); void onCurrentChanged( int ); void onWidgetDestroyed(); @@ -272,7 +272,6 @@ private: private: QtxWorkstackTabBar* myBar; //!< workarea tab bar header - QAbstractButton* myClose; //!< close button QStackedWidget* myStack; //!< widget stack QWidgetList myList; //!< child widgets list -- 2.39.2