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;
QApplication a(argc, argv);
a.setQuitOnLastWindowClosed(true);
//qInstallMessageHandler(myMessageOutput);
qInstallMessageHandler(myMessageOutput);
qDebug() << "Message handler test";
CMainWindow mw;

View File

@ -33,7 +33,6 @@
#include <QScrollBar>
#include <QDebug>
#include <QBoxLayout>
#include <QMenu>
#include <QApplication>
#include "FloatingDockContainer.h"
@ -43,6 +42,8 @@
#include "DockWidget.h"
#include "DockWidgetTab.h"
#include <iostream>
namespace ads
{
@ -288,7 +289,8 @@ void CDockAreaTabBar::insertTab(int Index, CDockWidgetTab* Tab)
{
d->TabsLayout->insertWidget(Index, Tab);
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&)));
Tab->installEventFilter(this);
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();
}
} // namespace ads
//---------------------------------------------------------------------------

View File

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

View File

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

View File

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

View File

@ -40,6 +40,7 @@
#include <QDebug>
#include <QToolButton>
#include <QPushButton>
#include <QMenu>
#include "ads_globals.h"
#include "DockWidget.h"
@ -163,7 +164,7 @@ void DockWidgetTabPrivate::createLayout()
CloseButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
CloseButton->setVisible(false);
CloseButton->setToolTip(QObject::tr("Close Tab"));
_this->connect(CloseButton, SIGNAL(clicked()), SIGNAL(closeButtonClicked()));
_this->connect(CloseButton, SIGNAL(clicked()), SIGNAL(closeRequested()));
QFontMetrics fm(TitleLabel->font());
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
{
@ -433,6 +451,7 @@ void CDockWidgetTab::mouseDoubleClickEvent(QMouseEvent *event)
// empty
if (!d->DockArea->dockContainer()->isFloating() || d->DockArea->dockWidgetsCount() > 1)
{
d->DragStartMousePosition = event->pos();
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
//---------------------------------------------------------------------------

View File

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