Implemented context menu for dock widget tab to close or detach tab or to close all other tabs

This commit is contained in:
Uwe Kindler 2018-11-09 10:07:56 +01:00
parent 854f542164
commit f69af82a49
7 changed files with 86 additions and 14 deletions

View File

@ -42,7 +42,7 @@ int main(int argc, char *argv[])
std::shared_ptr<int> b; std::shared_ptr<int> b;
QApplication a(argc, argv); QApplication a(argc, argv);
a.setQuitOnLastWindowClosed(true); a.setQuitOnLastWindowClosed(true);
//qInstallMessageHandler(myMessageOutput); qInstallMessageHandler(myMessageOutput);
qDebug() << "Message handler test"; qDebug() << "Message handler test";
CMainWindow mw; CMainWindow mw;

View File

@ -33,7 +33,6 @@
#include <QScrollBar> #include <QScrollBar>
#include <QDebug> #include <QDebug>
#include <QBoxLayout> #include <QBoxLayout>
#include <QMenu>
#include <QApplication> #include <QApplication>
#include "FloatingDockContainer.h" #include "FloatingDockContainer.h"
@ -43,6 +42,8 @@
#include "DockWidget.h" #include "DockWidget.h"
#include "DockWidgetTab.h" #include "DockWidgetTab.h"
#include <iostream>
namespace ads namespace ads
{ {
@ -288,7 +289,8 @@ void CDockAreaTabBar::insertTab(int Index, CDockWidgetTab* Tab)
{ {
d->TabsLayout->insertWidget(Index, Tab); d->TabsLayout->insertWidget(Index, Tab);
connect(Tab, SIGNAL(clicked()), this, SLOT(onTabClicked())); connect(Tab, SIGNAL(clicked()), this, SLOT(onTabClicked()));
connect(Tab, SIGNAL(closeButtonClicked()), this, SLOT(onTabCloseButtonClicked())); connect(Tab, SIGNAL(closeRequested()), this, SLOT(onTabCloseRequested()));
connect(Tab, SIGNAL(closeOtherTabsRequested()), this, SLOT(onCloseOtherTabsRequested()));
connect(Tab, SIGNAL(moved(const QPoint&)), this, SLOT(onTabWidgetMoved(const QPoint&))); connect(Tab, SIGNAL(moved(const QPoint&)), this, SLOT(onTabWidgetMoved(const QPoint&)));
Tab->installEventFilter(this); Tab->installEventFilter(this);
emit tabInserted(Index); emit tabInserted(Index);
@ -402,9 +404,26 @@ void CDockAreaTabBar::onTabClicked()
//=========================================================================== //===========================================================================
void CDockAreaTabBar::onTabCloseButtonClicked() void CDockAreaTabBar::onTabCloseRequested()
{ {
closeTab(currentIndex()); CDockWidgetTab* Tab = qobject_cast<CDockWidgetTab*>(sender());
int Index = d->TabsLayout->indexOf(Tab);
closeTab(Index);
}
//===========================================================================
void CDockAreaTabBar::onCloseOtherTabsRequested()
{
auto Sender = qobject_cast<CDockWidgetTab*>(sender());
for (int i = 0; i < count(); ++i)
{
auto Tab = tab(i);
if (Tab->isClosable() && !Tab->isHidden() && Tab != Sender)
{
closeTab(i);
}
}
} }
@ -532,6 +551,7 @@ bool CDockAreaTabBar::isTabOpen(int Index) const
return !tab(Index)->isHidden(); return !tab(Index)->isHidden();
} }
} // namespace ads } // namespace ads
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------

View File

@ -54,7 +54,8 @@ private:
private slots: private slots:
void onTabClicked(); void onTabClicked();
void onTabCloseButtonClicked(); void onTabCloseRequested();
void onCloseOtherTabsRequested();
void onTabWidgetMoved(const QPoint& GlobalPos); void onTabWidgetMoved(const QPoint& GlobalPos);
protected: protected:

View File

@ -120,6 +120,7 @@ void DockAreaTitleBarPrivate::createButtons()
QMenu* TabsMenu = new QMenu(TabsMenuButton); QMenu* TabsMenu = new QMenu(TabsMenuButton);
_this->connect(TabsMenu, SIGNAL(aboutToShow()), SLOT(onTabsMenuAboutToShow())); _this->connect(TabsMenu, SIGNAL(aboutToShow()), SLOT(onTabsMenuAboutToShow()));
TabsMenuButton->setMenu(TabsMenu); TabsMenuButton->setMenu(TabsMenu);
TabsMenuButton->setToolTip(QObject::tr("List all tabs"));
TabsMenuButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding); TabsMenuButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
TopLayout->addWidget(TabsMenuButton, 0); TopLayout->addWidget(TabsMenuButton, 0);
_this->connect(TabsMenuButton->menu(), SIGNAL(triggered(QAction*)), _this->connect(TabsMenuButton->menu(), SIGNAL(triggered(QAction*)),
@ -129,6 +130,7 @@ void DockAreaTitleBarPrivate::createButtons()
UndockButton = new tTileBarButton(); UndockButton = new tTileBarButton();
UndockButton->setObjectName("undockButton"); UndockButton->setObjectName("undockButton");
UndockButton->setAutoRaise(true); UndockButton->setAutoRaise(true);
UndockButton->setToolTip(QObject::tr("Detach Group"));
UndockButton->setIcon(_this->style()->standardIcon(QStyle::SP_TitleBarNormalButton)); UndockButton->setIcon(_this->style()->standardIcon(QStyle::SP_TitleBarNormalButton));
UndockButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding); UndockButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
TopLayout->addWidget(UndockButton, 0); TopLayout->addWidget(UndockButton, 0);
@ -145,7 +147,14 @@ void DockAreaTitleBarPrivate::createButtons()
CloseIcon.addPixmap(disabledPixmap, QIcon::Disabled); CloseIcon.addPixmap(disabledPixmap, QIcon::Disabled);
CloseButton->setIcon(CloseIcon); CloseButton->setIcon(CloseIcon);
CloseButton->setToolTip(QObject::tr("Close all tabs")); if (testConfigFlag(CDockManager::DockAreaCloseButtonClosesTab))
{
CloseButton->setToolTip(QObject::tr("Close Active Tab"));
}
else
{
CloseButton->setToolTip(QObject::tr("Close Group"));
}
CloseButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding); CloseButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
TopLayout->addWidget(CloseButton, 0); TopLayout->addWidget(CloseButton, 0);
_this->connect(CloseButton, SIGNAL(clicked()), SLOT(onCloseButtonClicked())); _this->connect(CloseButton, SIGNAL(clicked()), SLOT(onCloseButtonClicked()));

View File

@ -336,10 +336,6 @@ void DockAreaWidgetPrivate::updateCloseButtonState()
return; return;
} }
if (!UpdateCloseButton)
{
return;
}
TitleBar->button(TitleBarButtonClose)->setEnabled( TitleBar->button(TitleBarButtonClose)->setEnabled(
_this->features().testFlag(CDockWidget::DockWidgetClosable)); _this->features().testFlag(CDockWidget::DockWidgetClosable));
UpdateCloseButton = false; UpdateCloseButton = false;
@ -768,7 +764,10 @@ void CDockAreaWidget::toggleView(bool Open)
void CDockAreaWidget::setVisible(bool Visible) void CDockAreaWidget::setVisible(bool Visible)
{ {
Super::setVisible(Visible); Super::setVisible(Visible);
if (d->UpdateCloseButton)
{
d->updateCloseButtonState(); d->updateCloseButtonState();
}
} }

View File

@ -40,6 +40,7 @@
#include <QDebug> #include <QDebug>
#include <QToolButton> #include <QToolButton>
#include <QPushButton> #include <QPushButton>
#include <QMenu>
#include "ads_globals.h" #include "ads_globals.h"
#include "DockWidget.h" #include "DockWidget.h"
@ -163,7 +164,7 @@ void DockWidgetTabPrivate::createLayout()
CloseButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); CloseButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
CloseButton->setVisible(false); CloseButton->setVisible(false);
CloseButton->setToolTip(QObject::tr("Close Tab")); CloseButton->setToolTip(QObject::tr("Close Tab"));
_this->connect(CloseButton, SIGNAL(clicked()), SIGNAL(closeButtonClicked())); _this->connect(CloseButton, SIGNAL(clicked()), SIGNAL(closeRequested()));
QFontMetrics fm(TitleLabel->font()); QFontMetrics fm(TitleLabel->font());
int Spacing = qRound(fm.height() / 4.0); int Spacing = qRound(fm.height() / 4.0);
@ -345,6 +346,23 @@ void CDockWidgetTab::mouseMoveEvent(QMouseEvent* ev)
} }
//============================================================================
void CDockWidgetTab::contextMenuEvent(QContextMenuEvent* ev)
{
ev->accept();
std::cout << "CDockAreaTabBar::onTabContextMenuRequested" << std::endl;
d->DragStartMousePosition = ev->pos();
QMenu Menu(this);
Menu.addAction(tr("Detach"), this, SLOT(onDetachActionTriggered()));
Menu.addSeparator();
auto Action = Menu.addAction(tr("Close"), this, SIGNAL(closeRequested()));
Action->setEnabled(isClosable());
Menu.addAction(tr("Close Others"), this, SIGNAL(closeOtherTabsRequested()));
Menu.exec(mapToGlobal(ev->pos()));
}
//============================================================================ //============================================================================
bool CDockWidgetTab::isActiveTab() const bool CDockWidgetTab::isActiveTab() const
{ {
@ -433,6 +451,7 @@ void CDockWidgetTab::mouseDoubleClickEvent(QMouseEvent *event)
// empty // empty
if (!d->DockArea->dockContainer()->isFloating() || d->DockArea->dockWidgetsCount() > 1) if (!d->DockArea->dockContainer()->isFloating() || d->DockArea->dockWidgetsCount() > 1)
{ {
d->DragStartMousePosition = event->pos();
d->startFloating(); d->startFloating();
} }
@ -448,6 +467,20 @@ void CDockWidgetTab::setVisible(bool visible)
} }
//============================================================================
bool CDockWidgetTab::isClosable() const
{
return d->DockWidget && d->DockWidget->features().testFlag(CDockWidget::DockWidgetClosable);
}
//===========================================================================
void CDockWidgetTab::onDetachActionTriggered()
{
d->DragStartMousePosition = mapFromGlobal(QCursor::pos());
d->startFloating();
}
} // namespace ads } // namespace ads
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------

View File

@ -54,10 +54,14 @@ private:
DockWidgetTabPrivate* d; ///< private data (pimpl) DockWidgetTabPrivate* d; ///< private data (pimpl)
friend struct DockWidgetTabPrivate; friend struct DockWidgetTabPrivate;
private slots:
void onDetachActionTriggered();
protected: protected:
virtual void mousePressEvent(QMouseEvent* ev) override; virtual void mousePressEvent(QMouseEvent* ev) override;
virtual void mouseReleaseEvent(QMouseEvent* ev) override; virtual void mouseReleaseEvent(QMouseEvent* ev) override;
virtual void mouseMoveEvent(QMouseEvent* ev) override; virtual void mouseMoveEvent(QMouseEvent* ev) override;
virtual void contextMenuEvent(QContextMenuEvent* ev) override;
/** /**
* Double clicking the tab widget makes the assigned dock widget floating * Double clicking the tab widget makes the assigned dock widget floating
@ -121,13 +125,19 @@ public:
*/ */
QString text() const; QString text() const;
/**
* This function returns true if the assigned dock widget is closeable
*/
bool isClosable() const;
public slots: public slots:
virtual void setVisible(bool visible); virtual void setVisible(bool visible);
signals: signals:
void activeTabChanged(); void activeTabChanged();
void clicked(); void clicked();
void closeButtonClicked(); void closeRequested();
void closeOtherTabsRequested();
void moved(const QPoint& GlobalPos); void moved(const QPoint& GlobalPos);
}; // class DockWidgetTab }; // class DockWidgetTab
} }