Skip to content

Commit 094fa37

Browse files
Implemented supprt for customdock area title bar buttons
1 parent 6a8b26f commit 094fa37

File tree

7 files changed

+109
-4
lines changed

7 files changed

+109
-4
lines changed

demo/MainWindow.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
#include <QScreen>
5757
#include <QStyle>
5858
#include <QMessageBox>
59+
#include <QMenu>
5960

6061
#ifdef Q_OS_WIN
6162
#include <QAxWidget>
@@ -129,7 +130,7 @@ static void appendFeaturStringToWindowTitle(ads::CDockWidget* DockWidget)
129130
*/
130131
static QIcon svgIcon(const QString& File)
131132
{
132-
// This is a workaround, because because in item views SVG icons are not
133+
// This is a workaround, because in item views SVG icons are not
133134
// properly scaled an look blurry or pixelate
134135
QIcon SvgIcon(File);
135136
SvgIcon.addPixmap(SvgIcon.pixmap(92));
@@ -180,6 +181,18 @@ static ads::CDockWidget* createEditorWidget(QMenu* ViewMenu)
180181
DockWidget->setIcon(svgIcon(":/adsdemo/images/edit.svg"));
181182
DockWidget->setFeature(ads::CDockWidget::CustomCloseHandling, true);
182183
ViewMenu->addAction(DockWidget->toggleViewAction());
184+
185+
QMenu* OptionsMenu = new QMenu(DockWidget);
186+
OptionsMenu->setTitle(QObject::tr("Options"));
187+
OptionsMenu->setToolTip(OptionsMenu->title());
188+
OptionsMenu->setIcon(svgIcon(":/adsdemo/images/custom-menu-button.svg"));
189+
auto MenuAction = OptionsMenu->menuAction();
190+
// The object name of the action will be set for the QToolButton that
191+
// is created in the dock area title bar. You can use this name for CSS
192+
// styling
193+
MenuAction->setObjectName("optionsMenu");
194+
DockWidget->setTitleBarActions({OptionsMenu->menuAction()});
195+
183196
return DockWidget;
184197
}
185198

demo/demo.qrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@
99
<file>images/date_range.svg</file>
1010
<file>images/edit.svg</file>
1111
<file>images/grid_on.svg</file>
12+
<file>images/custom-menu-button.svg</file>
1213
</qresource>
1314
</RCC>

demo/images/custom-menu-button.svg

Lines changed: 32 additions & 0 deletions
Loading

src/DockAreaTabBar.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,6 @@ void CDockAreaTabBar::closeTab(int Index)
576576
{
577577
return;
578578
}
579-
//Tab->hide();
580579
emit tabCloseRequested(Index);
581580
}
582581

@@ -643,5 +642,6 @@ eDragState CDockAreaTabBar::dragState() const
643642

644643
} // namespace ads
645644

645+
646646
//---------------------------------------------------------------------------
647647
// EOF DockAreaTabBar.cpp

src/DockAreaTitleBar.cpp

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ struct DockAreaTitleBarPrivate
7070
CDockAreaTabBar* TabBar;
7171
bool MenuOutdated = true;
7272
QMenu* TabsMenu;
73+
QList<tTitleBarButton*> DockWidgetActionsButtons;
7374

7475
/**
7576
* Private data constructor
@@ -188,7 +189,6 @@ void DockAreaTitleBarPrivate::createButtons()
188189
_this->connect(TabsMenuButton->menu(), SIGNAL(triggered(QAction*)),
189190
SLOT(onTabsMenuActionTriggered(QAction*)));
190191

191-
192192
// Undock button
193193
UndockButton = new CTitleBarButton(testConfigFlag(CDockManager::DockAreaHasUndockButton));
194194
UndockButton->setObjectName("undockButton");
@@ -361,11 +361,39 @@ void CDockAreaTitleBar::onCurrentTabChanged(int Index)
361361
return;
362362
}
363363

364+
CDockWidget* DockWidget = d->TabBar->tab(Index)->dockWidget();
364365
if (d->testConfigFlag(CDockManager::DockAreaCloseButtonClosesTab))
365366
{
366-
CDockWidget* DockWidget = d->TabBar->tab(Index)->dockWidget();
367367
d->CloseButton->setEnabled(DockWidget->features().testFlag(CDockWidget::DockWidgetClosable));
368368
}
369+
370+
if (!d->DockWidgetActionsButtons.isEmpty())
371+
{
372+
for (auto Button : d->DockWidgetActionsButtons)
373+
{
374+
d->TopLayout->removeWidget(Button);
375+
delete Button;
376+
}
377+
d->DockWidgetActionsButtons.clear();
378+
}
379+
380+
auto Actions = DockWidget->titleBarActions();
381+
if (Actions.isEmpty())
382+
{
383+
return;
384+
}
385+
386+
int InsertIndex = 2;
387+
for (auto Action : Actions)
388+
{
389+
auto Button = new CTitleBarButton(true, this);
390+
Button->setDefaultAction(Action);
391+
Button->setAutoRaise(true);
392+
Button->setPopupMode(QToolButton::InstantPopup);
393+
Button->setObjectName(Action->objectName());
394+
d->TopLayout->insertWidget(InsertIndex++, Button, 0);
395+
d->DockWidgetActionsButtons.append(Button);
396+
}
369397
}
370398

371399

src/DockWidget.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ struct DockWidgetPrivate
8080
QSize ToolBarIconSizeDocked = QSize(16, 16);
8181
QSize ToolBarIconSizeFloating = QSize(24, 24);
8282
bool IsFloatingTopLevel = false;
83+
QList<QAction*> TitleBarActions;
8384

8485
/**
8586
* Private data constructor
@@ -817,6 +818,20 @@ bool CDockWidget::closeDockWidgetInternal(bool ForceClose)
817818
}
818819

819820

821+
//============================================================================
822+
void CDockWidget::setTitleBarActions(QList<QAction*> actions)
823+
{
824+
d->TitleBarActions = actions;
825+
}
826+
827+
828+
//============================================================================
829+
QList<QAction*> CDockWidget::titleBarActions() const
830+
{
831+
return d->TitleBarActions;
832+
}
833+
834+
820835
} // namespace ads
821836

822837
//---------------------------------------------------------------------------

src/DockWidget.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,22 @@ private slots:
401401
*/
402402
QSize toolBarIconSize(eState State) const;
403403

404+
/**
405+
* Set the actions that will be shown in the dock area title bar
406+
* if this dock widget is the active tab.
407+
* You should not add to many actions to the title bar, because this
408+
* will remove the available space for the tabs. If you have a number
409+
* of actions, just add an action with a menu to show a popup menu
410+
* button in the title bar.
411+
*/
412+
void setTitleBarActions(QList<QAction*> actions);
413+
414+
/**
415+
* Returns a list of actions that will be inserted into the dock area title
416+
* bar if this dock widget becomes the current widget
417+
*/
418+
virtual QList<QAction*> titleBarActions() const;
419+
404420

405421
#ifndef QT_NO_TOOLTIP
406422
/**

0 commit comments

Comments
 (0)