2022-09-05 17:29:42 +08:00
|
|
|
/*******************************************************************************
|
|
|
|
** Qt Advanced Docking System
|
|
|
|
** Copyright (C) 2017 Uwe Kindler
|
|
|
|
**
|
|
|
|
** This library is free software; you can redistribute it and/or
|
|
|
|
** modify it under the terms of the GNU Lesser General Public
|
|
|
|
** License as published by the Free Software Foundation; either
|
|
|
|
** version 2.1 of the License, or (at your option) any later version.
|
|
|
|
**
|
|
|
|
** This library is distributed in the hope that it will be useful,
|
|
|
|
** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
** Lesser General Public License for more details.
|
|
|
|
**
|
|
|
|
** You should have received a copy of the GNU Lesser General Public
|
|
|
|
** License along with this library; If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
//============================================================================
|
|
|
|
/// \file DockWidgetTab.h
|
|
|
|
/// \author Syarif Fakhri
|
|
|
|
/// \date 05.09.2022
|
|
|
|
/// \brief Implementation of CSideTabBar class
|
|
|
|
//============================================================================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//============================================================================
|
|
|
|
// INCLUDES
|
|
|
|
//============================================================================
|
|
|
|
#include "SideTabBar.h"
|
|
|
|
|
|
|
|
#include <QBoxLayout>
|
2022-09-14 14:17:28 +08:00
|
|
|
#include <QStyleOption>
|
|
|
|
#include <QPainter>
|
2022-10-31 02:44:33 +08:00
|
|
|
#include <QXmlStreamWriter>
|
2022-09-05 17:29:42 +08:00
|
|
|
|
2022-10-27 19:25:40 +08:00
|
|
|
#include "DockContainerWidget.h"
|
|
|
|
#include "DockWidgetSideTab.h"
|
|
|
|
#include "DockWidgetTab.h"
|
|
|
|
#include "DockFocusController.h"
|
|
|
|
#include "AutoHideDockContainer.h"
|
2022-10-31 02:44:33 +08:00
|
|
|
#include "DockAreaWidget.h"
|
2022-10-27 19:25:40 +08:00
|
|
|
|
2022-09-05 17:29:42 +08:00
|
|
|
namespace ads
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Private data class of CSideTabBar class (pimpl)
|
|
|
|
*/
|
|
|
|
struct SideTabBarPrivate
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Private data constructor
|
|
|
|
*/
|
|
|
|
SideTabBarPrivate(CSideTabBar* _public);
|
|
|
|
|
|
|
|
CSideTabBar* _this;
|
|
|
|
CDockContainerWidget* ContainerWidget;
|
|
|
|
QBoxLayout* TabsLayout;
|
2022-09-13 10:42:58 +08:00
|
|
|
Qt::Orientation Orientation;
|
2022-10-27 16:22:28 +08:00
|
|
|
SideBarLocation SideTabArea = SideBarLocation::Left;
|
2022-10-26 15:50:16 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Convenience function to check if this is a horizontal side bar
|
|
|
|
*/
|
|
|
|
bool isHorizontal() const
|
|
|
|
{
|
|
|
|
return Qt::Horizontal == Orientation;
|
|
|
|
}
|
2022-09-05 17:29:42 +08:00
|
|
|
}; // struct SideTabBarPrivate
|
|
|
|
|
|
|
|
//============================================================================
|
|
|
|
SideTabBarPrivate::SideTabBarPrivate(CSideTabBar* _public) :
|
|
|
|
_this(_public)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//============================================================================
|
2022-10-27 16:22:28 +08:00
|
|
|
CSideTabBar::CSideTabBar(CDockContainerWidget* parent, SideBarLocation area) :
|
2022-10-26 15:50:16 +08:00
|
|
|
Super(parent),
|
2022-09-05 17:29:42 +08:00
|
|
|
d(new SideTabBarPrivate(this))
|
|
|
|
{
|
2022-10-26 15:50:16 +08:00
|
|
|
d->SideTabArea = area;
|
2022-09-05 17:29:42 +08:00
|
|
|
d->ContainerWidget = parent;
|
2022-10-27 16:22:28 +08:00
|
|
|
d->Orientation = (area == SideBarLocation::Bottom || area == SideBarLocation::Top)
|
2022-10-26 15:50:16 +08:00
|
|
|
? Qt::Horizontal : Qt::Vertical;
|
2022-09-05 17:29:42 +08:00
|
|
|
|
2022-09-14 15:52:34 +08:00
|
|
|
auto mainLayout = new QBoxLayout(d->Orientation == Qt::Vertical ? QBoxLayout::TopToBottom : QBoxLayout::LeftToRight);
|
|
|
|
|
2022-09-13 10:42:58 +08:00
|
|
|
d->TabsLayout = new QBoxLayout(d->Orientation == Qt::Vertical ? QBoxLayout::TopToBottom : QBoxLayout::LeftToRight);
|
2022-09-05 17:29:42 +08:00
|
|
|
d->TabsLayout->setContentsMargins(0, 0, 0, 0);
|
|
|
|
d->TabsLayout->setSpacing(0);
|
2022-09-14 15:52:34 +08:00
|
|
|
mainLayout->addLayout(d->TabsLayout);
|
|
|
|
mainLayout->setContentsMargins(0, 0, 0, 0);
|
|
|
|
mainLayout->setSpacing(0);
|
|
|
|
mainLayout->addStretch(1);
|
|
|
|
setLayout(mainLayout);
|
2022-10-26 15:50:16 +08:00
|
|
|
|
2022-09-05 17:29:42 +08:00
|
|
|
setFocusPolicy(Qt::NoFocus);
|
2022-10-26 15:50:16 +08:00
|
|
|
if (d->isHorizontal())
|
|
|
|
{
|
|
|
|
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
|
|
|
|
}
|
|
|
|
|
|
|
|
hide();
|
2022-09-05 17:29:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//============================================================================
|
|
|
|
CSideTabBar::~CSideTabBar()
|
|
|
|
{
|
2022-10-28 17:11:35 +08:00
|
|
|
qDebug() << "~CSideTabBar() ";
|
2022-10-27 16:23:11 +08:00
|
|
|
// The SideTabeBar is not the owner of the tabs and to prevent deletion
|
|
|
|
// we set the parent here to nullptr to remove it from the children
|
|
|
|
auto Tabs = findChildren<CDockWidgetSideTab*>(QString(), Qt::FindDirectChildrenOnly);
|
|
|
|
for (auto Tab : Tabs)
|
|
|
|
{
|
|
|
|
Tab->setParent(nullptr);
|
|
|
|
}
|
2022-09-05 17:29:42 +08:00
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//============================================================================
|
|
|
|
void CSideTabBar::insertSideTab(int Index, CDockWidgetSideTab* SideTab)
|
|
|
|
{
|
2022-10-28 17:11:35 +08:00
|
|
|
SideTab->installEventFilter(this);
|
2022-09-05 17:29:42 +08:00
|
|
|
SideTab->setSideTabBar(this);
|
2022-10-28 21:20:56 +08:00
|
|
|
SideTab->updateOrientationForArea(d->SideTabArea);
|
|
|
|
d->TabsLayout->insertWidget(Index, SideTab);
|
2022-10-26 15:50:16 +08:00
|
|
|
show();
|
2022-09-05 17:29:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-27 19:25:40 +08:00
|
|
|
//============================================================================
|
|
|
|
CAutoHideDockContainer* CSideTabBar::insertDockWidget(int Index, CDockWidget* DockWidget)
|
|
|
|
{
|
2022-10-28 22:28:23 +08:00
|
|
|
/*
|
|
|
|
* sideTabBar(area)->insertSideTab(insertOrder == CDockWidget::First ? 0 : -1, DockWidget->sideTabWidget());
|
|
|
|
DockWidget->sideTabWidget()->show();
|
|
|
|
|
|
|
|
const auto AutoHideContainer = new CAutoHideDockContainer(DockWidget, area, this);
|
2022-10-27 19:25:40 +08:00
|
|
|
AutoHideContainer->hide();
|
2022-10-28 22:28:23 +08:00
|
|
|
d->DockManager->dockFocusController()->clearDockWidgetFocus(DockWidget);
|
|
|
|
return AutoHideContainer;*/
|
2022-10-27 19:25:40 +08:00
|
|
|
|
2022-10-28 22:28:23 +08:00
|
|
|
auto AutoHideContainer = new CAutoHideDockContainer(DockWidget, d->SideTabArea, d->ContainerWidget);
|
|
|
|
DockWidget->dockManager()->dockFocusController()->clearDockWidgetFocus(DockWidget);
|
|
|
|
auto Tab = AutoHideContainer->sideTab();
|
|
|
|
insertSideTab(Index, Tab);
|
2022-10-27 19:25:40 +08:00
|
|
|
return AutoHideContainer;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//============================================================================
|
|
|
|
void CSideTabBar::removeDockWidget(CDockWidget* DockWidget)
|
|
|
|
{
|
2022-10-28 17:14:04 +08:00
|
|
|
Q_UNUSED(DockWidget);
|
|
|
|
// TODO implement
|
2022-10-27 19:25:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-05 17:29:42 +08:00
|
|
|
//============================================================================
|
|
|
|
void CSideTabBar::removeSideTab(CDockWidgetSideTab* SideTab)
|
|
|
|
{
|
2022-10-27 16:22:28 +08:00
|
|
|
qDebug() << "CSideTabBar::removeSideTab " << SideTab->text();
|
2022-10-28 17:11:35 +08:00
|
|
|
SideTab->removeEventFilter(this);
|
2022-09-05 17:29:42 +08:00
|
|
|
d->TabsLayout->removeWidget(SideTab);
|
2022-10-26 15:50:16 +08:00
|
|
|
if (d->TabsLayout->isEmpty())
|
|
|
|
{
|
|
|
|
hide();
|
|
|
|
}
|
2022-09-05 17:29:42 +08:00
|
|
|
}
|
2022-09-14 14:17:28 +08:00
|
|
|
|
2022-09-14 14:25:11 +08:00
|
|
|
|
2022-10-28 17:11:35 +08:00
|
|
|
//============================================================================
|
|
|
|
bool CSideTabBar::event(QEvent* e)
|
|
|
|
{
|
|
|
|
switch (e->type())
|
|
|
|
{
|
|
|
|
case QEvent::ChildRemoved:
|
|
|
|
if (d->TabsLayout->isEmpty())
|
|
|
|
{
|
|
|
|
hide();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case QEvent::Resize:
|
|
|
|
if (d->TabsLayout->count())
|
|
|
|
{
|
|
|
|
auto ev = static_cast<QResizeEvent*>(e);
|
|
|
|
auto Tab = tabAt(0);
|
|
|
|
int Size = d->isHorizontal() ? ev->size().height() : ev->size().width();
|
|
|
|
int TabSize = d->isHorizontal() ? Tab->size().height() : Tab->size().width();
|
|
|
|
// If the size of the side bar is less than the size of the first tab
|
|
|
|
// then there are no visible tabs in this side bar. This check will
|
|
|
|
// fail if someone will force a very big border via CSS!!
|
|
|
|
if (Size < TabSize)
|
|
|
|
{
|
|
|
|
hide();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
hide();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return Super::event(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//============================================================================
|
|
|
|
bool CSideTabBar::eventFilter(QObject *watched, QEvent *event)
|
|
|
|
{
|
|
|
|
if (event->type() != QEvent::ShowToParent)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// As soon as on tab is shhown, we need to show the side tab bar
|
|
|
|
auto Tab = qobject_cast<CDockWidgetSideTab*>(watched);
|
|
|
|
if (Tab)
|
|
|
|
{
|
|
|
|
show();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-14 14:17:28 +08:00
|
|
|
//============================================================================
|
|
|
|
void CSideTabBar::paintEvent(QPaintEvent* event)
|
|
|
|
{
|
2022-10-12 17:17:54 +08:00
|
|
|
Q_UNUSED(event)
|
|
|
|
|
2022-09-14 14:17:28 +08:00
|
|
|
QStyleOption option;
|
|
|
|
option.initFrom(this);
|
|
|
|
QPainter painter(this);
|
|
|
|
style()->drawPrimitive(QStyle::PE_Widget, &option, &painter, this);
|
|
|
|
}
|
2022-09-14 14:25:11 +08:00
|
|
|
|
|
|
|
|
|
|
|
//============================================================================
|
|
|
|
Qt::Orientation CSideTabBar::orientation() const
|
|
|
|
{
|
|
|
|
return d->Orientation;
|
|
|
|
}
|
2022-09-14 17:39:57 +08:00
|
|
|
|
|
|
|
|
|
|
|
//============================================================================
|
|
|
|
CDockWidgetSideTab* CSideTabBar::tabAt(int index) const
|
|
|
|
{
|
|
|
|
return qobject_cast<CDockWidgetSideTab*>(d->TabsLayout->itemAt(index)->widget());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//============================================================================
|
|
|
|
int CSideTabBar::tabCount() const
|
|
|
|
{
|
|
|
|
return d->TabsLayout->count();
|
|
|
|
}
|
2022-10-26 15:50:16 +08:00
|
|
|
|
|
|
|
|
|
|
|
//============================================================================
|
2022-10-27 16:22:28 +08:00
|
|
|
SideBarLocation CSideTabBar::sideTabBarArea() const
|
2022-10-26 15:50:16 +08:00
|
|
|
{
|
|
|
|
return d->SideTabArea;
|
|
|
|
}
|
2022-10-28 17:11:35 +08:00
|
|
|
|
2022-10-31 02:44:33 +08:00
|
|
|
|
|
|
|
//============================================================================
|
|
|
|
void CSideTabBar::saveState(QXmlStreamWriter& s) const
|
|
|
|
{
|
|
|
|
if (!tabCount())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
s.writeStartElement("SideBar");
|
|
|
|
s.writeAttribute("Area", QString::number(sideTabBarArea()));
|
|
|
|
s.writeAttribute("Tabs", QString::number(tabCount()));
|
|
|
|
|
|
|
|
for (auto i = 0; i < tabCount(); ++i)
|
|
|
|
{
|
|
|
|
auto Tab = tabAt(i);
|
|
|
|
if (!Tab)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto DockArea = Tab->dockWidget()->dockAreaWidget();
|
|
|
|
DockArea->saveState(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
s.writeEndElement();
|
|
|
|
}
|
2022-09-05 17:29:42 +08:00
|
|
|
}
|