Skip to content

Commit 11e5f9c

Browse files
author
Uwe Kindler
committed
Properly implemented DockAreaTitle bar to encapsulate title bar functionality
1 parent 9bfb3fb commit 11e5f9c

File tree

7 files changed

+53
-186
lines changed

7 files changed

+53
-186
lines changed

src/DockAreaTabBar.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -467,16 +467,12 @@ bool CDockAreaTabBar::eventFilter(QObject *watched, QEvent *event)
467467
return Result;
468468
}
469469

470-
if (event->type() == QEvent::Hide)
471-
{
472-
return Result;
473-
}
474-
475-
int TabIndex = d->TabsLayout->indexOf(Tab);
476470
switch (event->type())
477471
{
478-
case QEvent::Hide: emit tabClosed(TabIndex); break;
479-
case QEvent::Show: emit tabOpened(TabIndex); break;
472+
case QEvent::Hide:
473+
emit tabClosed(d->TabsLayout->indexOf(Tab)); break;
474+
case QEvent::Show:
475+
emit tabOpened(d->TabsLayout->indexOf(Tab)); break;
480476
default:
481477
break;
482478
}

src/DockAreaTitleBar.cpp

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ void DockAreaTitleBarPrivate::createButtons()
8383
_this->connect(TabsMenu, SIGNAL(aboutToShow()), SLOT(onTabsMenuAboutToShow()));
8484
TabsMenuButton->setMenu(TabsMenu);
8585
TopLayout->addWidget(TabsMenuButton, 0);
86-
TabsMenuButton->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
86+
TabsMenuButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
8787
_this->connect(TabsMenuButton->menu(), SIGNAL(triggered(QAction*)),
8888
SLOT(onTabsMenuActionTriggered(QAction*)));
8989

@@ -92,7 +92,7 @@ void DockAreaTitleBarPrivate::createButtons()
9292
CloseButton->setFlat(true);
9393
CloseButton->setIcon(_this->style()->standardIcon(QStyle::SP_TitleBarCloseButton));
9494
CloseButton->setToolTip(QObject::tr("Close"));
95-
CloseButton->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
95+
CloseButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
9696
TopLayout->addWidget(CloseButton, 0);
9797
_this->connect(CloseButton, SIGNAL(clicked()), SLOT(onCloseButtonClicked()));
9898
}
@@ -103,10 +103,12 @@ void DockAreaTitleBarPrivate::createTabBar()
103103
{
104104
TabBar = new CDockAreaTabBar(DockArea);
105105
TopLayout->addWidget(TabBar);
106-
_this->connect(TabBar, SIGNAL(tabClosed()), SLOT(markTabsMenuOutdated()));
107-
_this->connect(TabBar, SIGNAL(tabOpened()), SLOT(markTabsMenuOutdated()));
108-
_this->connect(TabBar, SIGNAL(tabInserted()), SLOT(markTabsMenuOutdated()));
109-
_this->connect(TabBar, SIGNAL(tabRemoved()), SLOT(markTabsMenuOutdated()));
106+
_this->connect(TabBar, SIGNAL(tabClosed(int)), SLOT(markTabsMenuOutdated()));
107+
_this->connect(TabBar, SIGNAL(tabOpened(int)), SLOT(markTabsMenuOutdated()));
108+
_this->connect(TabBar, SIGNAL(tabInserted(int)), SLOT(markTabsMenuOutdated()));
109+
_this->connect(TabBar, SIGNAL(removingTab(int)), SLOT(markTabsMenuOutdated()));
110+
_this->connect(TabBar, SIGNAL(tabMoved(int, int)), SLOT(markTabsMenuOutdated()));
111+
_this->connect(TabBar, SIGNAL(currentChanged(int)), SLOT(onCurrentTabChanged(int)));
110112
}
111113

112114

@@ -193,6 +195,14 @@ void CDockAreaTitleBar::onTabsMenuActionTriggered(QAction* Action)
193195
}
194196

195197

198+
//============================================================================
199+
void CDockAreaTitleBar::onCurrentTabChanged(int Index)
200+
{
201+
CDockWidget* DockWidget = d->TabBar->tab(Index)->dockWidget();
202+
d->CloseButton->setVisible(DockWidget->features().testFlag(CDockWidget::DockWidgetClosable));
203+
}
204+
205+
196206
} // namespace ads
197207

198208
//---------------------------------------------------------------------------

src/DockAreaTitleBar.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ private slots:
3333
void onTabsMenuAboutToShow();
3434
void onCloseButtonClicked();
3535
void onTabsMenuActionTriggered(QAction* Action);
36+
void onCurrentTabChanged(int Index);
3637

3738
public:
3839
using Super = QFrame;

src/DockAreaWidget.cpp

Lines changed: 28 additions & 158 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
#include "DockOverlay.h"
5353
#include "DockAreaTabBar.h"
5454
#include "DockSplitter.h"
55+
#include "DockAreaTitleBar.h"
5556

5657
#include <iostream>
5758

@@ -209,14 +210,9 @@ struct DockAreaWidgetPrivate
209210
{
210211
CDockAreaWidget* _this;
211212
QBoxLayout* Layout;
212-
QFrame* TitleBar;
213-
QBoxLayout* TopLayout;
214213
DockAreaLayout* ContentsLayout;
215-
CDockAreaTabBar* TabBar;
216-
QPushButton* TabsMenuButton;
217-
QPushButton* CloseButton;
214+
CDockAreaTitleBar* TitleBar;
218215
CDockManager* DockManager = nullptr;
219-
bool MenuOutdated = true;
220216

221217
/**
222218
* Private data constructor
@@ -226,7 +222,7 @@ struct DockAreaWidgetPrivate
226222
/**
227223
* Creates the layout for top area with tabs and close button
228224
*/
229-
void createTabBar();
225+
void createTitleBar();
230226

231227
/**
232228
* Returns the dock widget with the given index
@@ -244,13 +240,6 @@ struct DockAreaWidgetPrivate
244240
return dockWidgetAt(index)->tabWidget();
245241
}
246242

247-
/**
248-
* Adds a tabs menu entry for the given dock widget
249-
* If menu is 0, a menu entry is added to the menu of the TabsMenuButton
250-
* member. If menu is a valid menu pointer, the entry will be added to
251-
* the given menu
252-
*/
253-
void addTabsMenuEntry(CDockWidget* DockWidget, int Index = -1, QMenu* menu = 0);
254243

255244
/**
256245
* Returns the tab action of the given dock widget
@@ -268,22 +257,19 @@ struct DockAreaWidgetPrivate
268257
return DockWidget->property(INDEX_PROPERTY).toInt();
269258
}
270259

271-
/**
272-
* Update the tabs menu if dock widget order changed or if dock widget has
273-
* been removed
274-
*/
275-
void markTabsMenuOutdated();
276-
277-
/**
278-
* Updates the tabs menu if it is outdated
279-
*/
280-
void updateTabsMenu();
281-
282260
/**
283261
* Updates the tab bar visibility depending on the number of dock widgets
284262
* in this area
285263
*/
286264
void updateTabBar();
265+
266+
/**
267+
* Convenience function for tabbar access
268+
*/
269+
CDockAreaTabBar* tabBar() const
270+
{
271+
return TitleBar->tabBar();
272+
}
287273
};
288274
// struct DockAreaWidgetPrivate
289275

@@ -297,43 +283,14 @@ DockAreaWidgetPrivate::DockAreaWidgetPrivate(CDockAreaWidget* _public) :
297283

298284

299285
//============================================================================
300-
void DockAreaWidgetPrivate::createTabBar()
286+
void DockAreaWidgetPrivate::createTitleBar()
301287
{
302-
TitleBar = new QFrame(_this);
303-
TitleBar->setObjectName("dockAreaTitleBar");
304-
TopLayout = new QBoxLayout(QBoxLayout::LeftToRight);
305-
TopLayout->setContentsMargins(0, 0, 0, 0);
306-
TopLayout->setSpacing(0);
307-
TitleBar->setLayout(TopLayout);
288+
TitleBar = new CDockAreaTitleBar(_this);
308289
Layout->addWidget(TitleBar);
309-
TitleBar->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
310-
311-
TabBar = new CDockAreaTabBar(_this);
312-
TopLayout->addWidget(TabBar, 1);
313-
_this->connect(TabBar, SIGNAL(currentChanged(int)), SLOT(setCurrentIndex(int)));
314-
_this->connect(TabBar, SIGNAL(tabMoved(int, int)), SLOT(reorderDockWidget(int, int)));
315-
316-
TabsMenuButton = new QPushButton();
317-
TabsMenuButton->setObjectName("tabsMenuButton");
318-
TabsMenuButton->setFlat(true);
319-
TabsMenuButton->setIcon(_this->style()->standardIcon(QStyle::SP_TitleBarUnshadeButton));
320-
TabsMenuButton->setMaximumWidth(TabsMenuButton->iconSize().width());
321-
QMenu* TabsMenu = new QMenu(TabsMenuButton);
322-
_this->connect(TabsMenu, SIGNAL(aboutToShow()), SLOT(onTabsMenuAboutToShow()));
323-
TabsMenuButton->setMenu(TabsMenu);
324-
TopLayout->addWidget(TabsMenuButton, 0);
325-
TabsMenuButton->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
326-
_this->connect(TabsMenuButton->menu(), SIGNAL(triggered(QAction*)),
327-
SLOT(onTabsMenuActionTriggered(QAction*)));
328-
329-
CloseButton = new QPushButton();
330-
CloseButton->setObjectName("closeButton");
331-
CloseButton->setFlat(true);
332-
CloseButton->setIcon(_this->style()->standardIcon(QStyle::SP_TitleBarCloseButton));
333-
CloseButton->setToolTip(_this->tr("Close"));
334-
CloseButton->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
335-
TopLayout->addWidget(CloseButton, 0);
336-
_this->connect(CloseButton, SIGNAL(clicked()), SLOT(onCloseButtonClicked()));
290+
_this->connect(tabBar(), SIGNAL(tabCloseRequested(int)),
291+
SLOT(onTabCloseRequested(int)));
292+
_this->connect(tabBar(), SIGNAL(tabBarClicked(int)),
293+
SLOT(setCurrentIndex(int)));
337294
}
338295

339296

@@ -350,57 +307,6 @@ void DockAreaWidgetPrivate::updateTabBar()
350307
}
351308

352309

353-
//============================================================================
354-
void DockAreaWidgetPrivate::addTabsMenuEntry(CDockWidget* DockWidget,
355-
int Index, QMenu* menu)
356-
{
357-
menu = menu ? menu : TabsMenuButton->menu();
358-
QAction* Action;
359-
if (Index >= 0 && Index < menu->actions().count())
360-
{
361-
Action = new QAction(DockWidget->icon(), DockWidget->windowTitle());
362-
menu->insertAction(menu->actions().at(Index), Action);
363-
}
364-
else
365-
{
366-
Action = menu->addAction(DockWidget->icon(), DockWidget->windowTitle());
367-
}
368-
Action->setProperty(DOCKWIDGET_PROPERTY, QVariant::fromValue(DockWidget));
369-
QVariant vAction = QVariant::fromValue(Action);
370-
DockWidget->setProperty(ACTION_PROPERTY, vAction);
371-
}
372-
373-
374-
//============================================================================
375-
void DockAreaWidgetPrivate::markTabsMenuOutdated()
376-
{
377-
MenuOutdated = true;
378-
}
379-
380-
381-
//============================================================================
382-
void DockAreaWidgetPrivate::updateTabsMenu()
383-
{
384-
if (!MenuOutdated)
385-
{
386-
return;
387-
}
388-
389-
QMenu* menu = TabsMenuButton->menu();
390-
menu->clear();
391-
for (int i = 0; i < ContentsLayout->count(); ++i)
392-
{
393-
if (dockWidgetAt(i)->isClosed())
394-
{
395-
continue;
396-
}
397-
addTabsMenuEntry(dockWidgetAt(i), APPEND, menu);
398-
}
399-
400-
MenuOutdated = false;
401-
}
402-
403-
404310
//============================================================================
405311
CDockAreaWidget::CDockAreaWidget(CDockManager* DockManager, CDockContainerWidget* parent) :
406312
QFrame(parent),
@@ -412,7 +318,7 @@ CDockAreaWidget::CDockAreaWidget(CDockManager* DockManager, CDockContainerWidget
412318
d->Layout->setSpacing(0);
413319
setLayout(d->Layout);
414320

415-
d->createTabBar();
321+
d->createTitleBar();
416322
d->ContentsLayout = new DockAreaLayout(d->Layout);
417323
}
418324

@@ -453,10 +359,9 @@ void CDockAreaWidget::insertDockWidget(int index, CDockWidget* DockWidget,
453359
d->ContentsLayout->insertWidget(index, DockWidget);
454360
DockWidget->tabWidget()->setDockAreaWidget(this);
455361
auto TabWidget = DockWidget->tabWidget();
456-
d->TabBar->insertTab(index, TabWidget);
362+
d->tabBar()->insertTab(index, TabWidget);
457363
TabWidget->setVisible(!DockWidget->isClosed());
458364
DockWidget->setProperty(INDEX_PROPERTY, index);
459-
d->markTabsMenuOutdated();
460365
if (Activate)
461366
{
462367
setCurrentIndex(index);
@@ -474,11 +379,10 @@ void CDockAreaWidget::removeDockWidget(CDockWidget* DockWidget)
474379
d->ContentsLayout->removeWidget(DockWidget);
475380
auto TabWidget = DockWidget->tabWidget();
476381
TabWidget->hide();
477-
d->TabBar->removeTab(TabWidget);
382+
d->tabBar()->removeTab(TabWidget);
478383
if (NextOpenDockWidget)
479384
{
480385
setCurrentDockWidget(NextOpenDockWidget);
481-
d->markTabsMenuOutdated();
482386
}
483387
else if (d->ContentsLayout->isEmpty())
484388
{
@@ -541,9 +445,10 @@ void CDockAreaWidget::hideAreaIfNoVisibleContent()
541445

542446

543447
//============================================================================
544-
void CDockAreaWidget::onCloseButtonClicked()
448+
void CDockAreaWidget::onTabCloseRequested(int Index)
545449
{
546-
currentDockWidget()->toggleView(false);
450+
qDebug() << "CDockAreaWidget::onTabCloseRequested " << Index;
451+
dockWidget(Index)->toggleView(false);
547452
}
548453

549454

@@ -576,18 +481,15 @@ void CDockAreaWidget::setCurrentDockWidget(CDockWidget* DockWidget)
576481
void CDockAreaWidget::setCurrentIndex(int index)
577482
{
578483
std::cout << "CDockAreaWidget::setCurrentIndex " << index << std::endl;
579-
if (index < 0 || index > (d->TabBar->count() - 1))
484+
auto TabBar = d->tabBar();
485+
if (index < 0 || index > (TabBar->count() - 1))
580486
{
581487
qWarning() << Q_FUNC_INFO << "Invalid index" << index;
582488
return;
583489
}
584490

585491
emit currentChanging(index);
586-
d->TabBar->setCurrentIndex(index);
587-
CDockWidgetTab* CurrentTab = d->TabBar->currentTab();
588-
auto Features = CurrentTab->dockWidget()->features();
589-
d->CloseButton->setVisible(Features.testFlag(CDockWidget::DockWidgetClosable));
590-
492+
TabBar->setCurrentIndex(index);
591493
d->ContentsLayout->setCurrentIndex(index);
592494
d->ContentsLayout->currentWidget()->show();
593495
emit currentChanged(index);
@@ -602,9 +504,9 @@ int CDockAreaWidget::currentIndex() const
602504

603505

604506
//============================================================================
605-
QRect CDockAreaWidget::titleAreaGeometry() const
507+
QRect CDockAreaWidget::titleBarGeometry() const
606508
{
607-
return d->TopLayout->geometry();
509+
return d->TitleBar->geometry();
608510
}
609511

610512
//============================================================================
@@ -664,21 +566,6 @@ QList<CDockWidget*> CDockAreaWidget::openedDockWidgets() const
664566
}
665567

666568

667-
//============================================================================
668-
int CDockAreaWidget::indexOfContentByTitlePos(const QPoint& p, QWidget* exclude) const
669-
{
670-
for (int i = 0; i < d->ContentsLayout->count(); ++i)
671-
{
672-
auto TabWidget = d->tabWidgetAt(i);
673-
if (TabWidget->isVisible() && TabWidget->geometry().contains(p) && (!exclude || TabWidget != exclude))
674-
{
675-
return i;
676-
}
677-
}
678-
return -1;
679-
}
680-
681-
682569
//============================================================================
683570
int CDockAreaWidget::dockWidgetsCount() const
684571
{
@@ -716,16 +603,6 @@ void CDockAreaWidget::toggleDockWidgetView(CDockWidget* DockWidget, bool Open)
716603
Q_UNUSED(DockWidget);
717604
Q_UNUSED(Open);
718605
updateTabBarVisibility();
719-
d->markTabsMenuOutdated();
720-
}
721-
722-
723-
//============================================================================
724-
void CDockAreaWidget::onTabsMenuActionTriggered(QAction* Action)
725-
{
726-
QVariant vDockWidget = Action->property(DOCKWIDGET_PROPERTY);
727-
CDockWidget* DockWidget = vDockWidget.value<CDockWidget*>();
728-
setCurrentDockWidget(DockWidget);
729606
}
730607

731608

@@ -777,13 +654,6 @@ CDockWidget* CDockAreaWidget::nextOpenDockWidget(CDockWidget* DockWidget) const
777654
}
778655
}
779656

780-
781-
//============================================================================
782-
void CDockAreaWidget::onTabsMenuAboutToShow()
783-
{
784-
d->updateTabsMenu();
785-
}
786-
787657
} // namespace ads
788658

789659
//---------------------------------------------------------------------------

0 commit comments

Comments
 (0)