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/>.
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
//============================================================================
|
2022-11-01 20:41:36 +08:00
|
|
|
/// \file AutoHideSideBar.cpp
|
2022-09-05 17:29:42 +08:00
|
|
|
/// \author Syarif Fakhri
|
|
|
|
/// \date 05.09.2022
|
2022-11-01 20:41:36 +08:00
|
|
|
/// \brief Implementation of CAutoHideSideBar class
|
2022-09-05 17:29:42 +08:00
|
|
|
//============================================================================
|
|
|
|
|
|
|
|
|
|
|
|
//============================================================================
|
|
|
|
// INCLUDES
|
|
|
|
//============================================================================
|
2022-11-01 20:41:36 +08:00
|
|
|
#include "AutoHideSideBar.h"
|
|
|
|
|
2022-09-05 17:29:42 +08:00
|
|
|
#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 "DockWidgetTab.h"
|
|
|
|
#include "DockFocusController.h"
|
|
|
|
#include "AutoHideDockContainer.h"
|
2022-10-31 02:44:33 +08:00
|
|
|
#include "DockAreaWidget.h"
|
2022-11-01 18:02:01 +08:00
|
|
|
#include "DockingStateReader.h"
|
2022-11-01 20:41:36 +08:00
|
|
|
#include "AutoHideTab.h"
|
2022-10-27 19:25:40 +08:00
|
|
|
|
2022-09-05 17:29:42 +08:00
|
|
|
namespace ads
|
|
|
|
{
|
2022-11-03 18:34:04 +08:00
|
|
|
class CTabsWidget;
|
|
|
|
|
2022-09-05 17:29:42 +08:00
|
|
|
/**
|
|
|
|
* Private data class of CSideTabBar class (pimpl)
|
|
|
|
*/
|
2022-11-01 19:06:59 +08:00
|
|
|
struct AutoHideSideBarPrivate
|
2022-09-05 17:29:42 +08:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Private data constructor
|
|
|
|
*/
|
2022-11-01 19:06:59 +08:00
|
|
|
AutoHideSideBarPrivate(CAutoHideSideBar* _public);
|
2022-09-05 17:29:42 +08:00
|
|
|
|
2022-11-01 19:06:59 +08:00
|
|
|
CAutoHideSideBar* _this;
|
2022-09-05 17:29:42 +08:00
|
|
|
CDockContainerWidget* ContainerWidget;
|
2022-11-03 18:34:04 +08:00
|
|
|
CTabsWidget* TabsContainerWidget;
|
2022-09-05 17:29:42 +08:00
|
|
|
QBoxLayout* TabsLayout;
|
2022-09-13 10:42:58 +08:00
|
|
|
Qt::Orientation Orientation;
|
2022-11-03 22:28:01 +08:00
|
|
|
SideBarLocation SideTabArea = SideBarLocation::SideBarLeft;
|
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-11-03 18:34:04 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Called from viewport to forward event handling to this
|
|
|
|
*/
|
|
|
|
void handleViewportEvent(QEvent* e);
|
2022-11-01 19:06:59 +08:00
|
|
|
}; // struct AutoHideSideBarPrivate
|
2022-09-05 17:29:42 +08:00
|
|
|
|
2022-11-03 18:34:04 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This widget stores the tab buttons
|
|
|
|
*/
|
|
|
|
class CTabsWidget : public QWidget
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
using QWidget::QWidget;
|
|
|
|
using Super = QWidget;
|
|
|
|
AutoHideSideBarPrivate* EventHandler;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the size hint as minimum size hint
|
|
|
|
*/
|
|
|
|
virtual QSize minimumSizeHint() const override
|
|
|
|
{
|
|
|
|
return Super::sizeHint();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Forward event handling to EventHandler
|
|
|
|
*/
|
|
|
|
/*virtual bool event(QEvent* e) override
|
|
|
|
{
|
|
|
|
EventHandler->handleViewportEvent(e);
|
|
|
|
return Super::event(e);
|
|
|
|
}*/
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2022-09-05 17:29:42 +08:00
|
|
|
//============================================================================
|
2022-11-01 19:06:59 +08:00
|
|
|
AutoHideSideBarPrivate::AutoHideSideBarPrivate(CAutoHideSideBar* _public) :
|
2022-09-05 17:29:42 +08:00
|
|
|
_this(_public)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-11-03 18:34:04 +08:00
|
|
|
//============================================================================
|
|
|
|
void AutoHideSideBarPrivate::handleViewportEvent(QEvent* e)
|
|
|
|
{
|
|
|
|
switch (e->type())
|
|
|
|
{
|
|
|
|
case QEvent::ChildRemoved:
|
|
|
|
if (TabsLayout->isEmpty())
|
|
|
|
{
|
|
|
|
_this->hide();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case QEvent::Resize:
|
|
|
|
if (_this->tabCount())
|
|
|
|
{
|
|
|
|
auto ev = static_cast<QResizeEvent*>(e);
|
|
|
|
auto Tab = _this->tabAt(0);
|
|
|
|
int Size = isHorizontal() ? ev->size().height() : ev->size().width();
|
|
|
|
int TabSize = 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)
|
|
|
|
{
|
|
|
|
_this->hide();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_this->hide();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-05 17:29:42 +08:00
|
|
|
//============================================================================
|
2022-11-01 19:06:59 +08:00
|
|
|
CAutoHideSideBar::CAutoHideSideBar(CDockContainerWidget* parent, SideBarLocation area) :
|
2022-10-26 15:50:16 +08:00
|
|
|
Super(parent),
|
2022-11-01 19:06:59 +08:00
|
|
|
d(new AutoHideSideBarPrivate(this))
|
2022-09-05 17:29:42 +08:00
|
|
|
{
|
2022-10-26 15:50:16 +08:00
|
|
|
d->SideTabArea = area;
|
2022-09-05 17:29:42 +08:00
|
|
|
d->ContainerWidget = parent;
|
2022-11-03 22:28:01 +08:00
|
|
|
d->Orientation = (area == SideBarLocation::SideBarBottom || area == SideBarLocation::SideBarTop)
|
2022-10-26 15:50:16 +08:00
|
|
|
? Qt::Horizontal : Qt::Vertical;
|
2022-09-05 17:29:42 +08:00
|
|
|
|
2022-11-03 18:34:04 +08:00
|
|
|
setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
|
|
|
|
setFrameStyle(QFrame::NoFrame);
|
|
|
|
setWidgetResizable(true);
|
|
|
|
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
|
|
|
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
|
|
|
|
|
|
|
d->TabsContainerWidget = new CTabsWidget();
|
|
|
|
d->TabsContainerWidget->EventHandler = d;
|
|
|
|
d->TabsContainerWidget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
|
|
|
|
d->TabsContainerWidget->setObjectName("sideTabsContainerWidget");
|
|
|
|
|
2022-09-14 15:52:34 +08:00
|
|
|
|
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);
|
2022-11-03 18:34:04 +08:00
|
|
|
d->TabsLayout->setSpacing(12);
|
|
|
|
d->TabsLayout->addStretch(1);
|
|
|
|
d->TabsContainerWidget->setLayout(d->TabsLayout);
|
|
|
|
setWidget(d->TabsContainerWidget);
|
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);
|
|
|
|
}
|
|
|
|
|
2022-11-03 18:34:04 +08:00
|
|
|
qDebug() << "d->TabsLayout->count " << d->TabsLayout->count();
|
|
|
|
|
2022-10-26 15:50:16 +08:00
|
|
|
hide();
|
2022-09-05 17:29:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//============================================================================
|
2022-11-01 19:06:59 +08:00
|
|
|
CAutoHideSideBar::~CAutoHideSideBar()
|
2022-09-05 17:29:42 +08:00
|
|
|
{
|
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
|
2022-11-01 19:06:59 +08:00
|
|
|
auto Tabs = findChildren<CAutoHideTab*>(QString(), Qt::FindDirectChildrenOnly);
|
2022-10-27 16:23:11 +08:00
|
|
|
for (auto Tab : Tabs)
|
|
|
|
{
|
|
|
|
Tab->setParent(nullptr);
|
|
|
|
}
|
2022-09-05 17:29:42 +08:00
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//============================================================================
|
2022-11-01 20:34:08 +08:00
|
|
|
void CAutoHideSideBar::insertTab(int Index, CAutoHideTab* SideTab)
|
2022-09-05 17:29:42 +08:00
|
|
|
{
|
2022-11-01 19:06:59 +08:00
|
|
|
SideTab->setSideBar(this);
|
2022-11-03 18:34:04 +08:00
|
|
|
SideTab->installEventFilter(this);
|
|
|
|
if (Index < 0)
|
|
|
|
{
|
|
|
|
d->TabsLayout->insertWidget(d->TabsLayout->count() - 1, SideTab);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
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
|
|
|
//============================================================================
|
2022-11-01 19:06:59 +08:00
|
|
|
CAutoHideDockContainer* CAutoHideSideBar::insertDockWidget(int Index, CDockWidget* DockWidget)
|
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);
|
2022-11-01 20:34:08 +08:00
|
|
|
auto Tab = AutoHideContainer->autoHideTab();
|
2022-11-02 21:29:23 +08:00
|
|
|
DockWidget->setSideTabWidget(Tab);
|
2022-11-01 20:34:08 +08:00
|
|
|
insertTab(Index, Tab);
|
2022-10-27 19:25:40 +08:00
|
|
|
return AutoHideContainer;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//============================================================================
|
2022-11-01 20:34:08 +08:00
|
|
|
void CAutoHideSideBar::removeTab(CAutoHideTab* SideTab)
|
2022-09-05 17:29:42 +08:00
|
|
|
{
|
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
|
|
|
//============================================================================
|
2022-11-01 19:06:59 +08:00
|
|
|
bool CAutoHideSideBar::eventFilter(QObject *watched, QEvent *event)
|
2022-10-28 17:11:35 +08:00
|
|
|
{
|
|
|
|
if (event->type() != QEvent::ShowToParent)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-11-03 18:34:04 +08:00
|
|
|
// As soon as on tab is shown, we need to show the side tab bar
|
2022-11-01 19:06:59 +08:00
|
|
|
auto Tab = qobject_cast<CAutoHideTab*>(watched);
|
2022-10-28 17:11:35 +08:00
|
|
|
if (Tab)
|
|
|
|
{
|
|
|
|
show();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-09-14 14:25:11 +08:00
|
|
|
//============================================================================
|
2022-11-01 19:06:59 +08:00
|
|
|
Qt::Orientation CAutoHideSideBar::orientation() const
|
2022-09-14 14:25:11 +08:00
|
|
|
{
|
|
|
|
return d->Orientation;
|
|
|
|
}
|
2022-09-14 17:39:57 +08:00
|
|
|
|
|
|
|
|
|
|
|
//============================================================================
|
2022-11-01 19:06:59 +08:00
|
|
|
CAutoHideTab* CAutoHideSideBar::tabAt(int index) const
|
2022-09-14 17:39:57 +08:00
|
|
|
{
|
2022-11-01 19:06:59 +08:00
|
|
|
return qobject_cast<CAutoHideTab*>(d->TabsLayout->itemAt(index)->widget());
|
2022-09-14 17:39:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//============================================================================
|
2022-11-01 19:06:59 +08:00
|
|
|
int CAutoHideSideBar::tabCount() const
|
2022-09-14 17:39:57 +08:00
|
|
|
{
|
2022-11-03 18:34:04 +08:00
|
|
|
return d->TabsLayout->count() - 1;
|
2022-09-14 17:39:57 +08:00
|
|
|
}
|
2022-10-26 15:50:16 +08:00
|
|
|
|
|
|
|
|
|
|
|
//============================================================================
|
2022-11-01 20:34:08 +08:00
|
|
|
SideBarLocation CAutoHideSideBar::sideBarLocation() 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
|
|
|
|
|
|
|
//============================================================================
|
2022-11-01 19:06:59 +08:00
|
|
|
void CAutoHideSideBar::saveState(QXmlStreamWriter& s) const
|
2022-10-31 02:44:33 +08:00
|
|
|
{
|
|
|
|
if (!tabCount())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
s.writeStartElement("SideBar");
|
2022-11-01 20:34:08 +08:00
|
|
|
s.writeAttribute("Area", QString::number(sideBarLocation()));
|
2022-10-31 02:44:33 +08:00
|
|
|
s.writeAttribute("Tabs", QString::number(tabCount()));
|
|
|
|
|
|
|
|
for (auto i = 0; i < tabCount(); ++i)
|
|
|
|
{
|
|
|
|
auto Tab = tabAt(i);
|
|
|
|
if (!Tab)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-11-01 18:02:01 +08:00
|
|
|
Tab->dockWidget()->autoHideDockContainer()->saveState(s);
|
2022-10-31 02:44:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
s.writeEndElement();
|
|
|
|
}
|
2022-11-01 18:02:01 +08:00
|
|
|
|
2022-11-03 18:34:04 +08:00
|
|
|
//===========================================================================
|
|
|
|
QSize CAutoHideSideBar::minimumSizeHint() const
|
|
|
|
{
|
|
|
|
QSize Size = sizeHint();
|
|
|
|
Size.setWidth(10);
|
|
|
|
return Size;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//===========================================================================
|
|
|
|
QSize CAutoHideSideBar::sizeHint() const
|
|
|
|
{
|
|
|
|
return d->TabsContainerWidget->sizeHint();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//===========================================================================
|
|
|
|
int CAutoHideSideBar::spacing() const
|
|
|
|
{
|
|
|
|
return d->TabsLayout->spacing();
|
|
|
|
}
|
|
|
|
|
|
|
|
//===========================================================================
|
|
|
|
void CAutoHideSideBar::setSpacing(int Spacing)
|
|
|
|
{
|
|
|
|
d->TabsLayout->setSpacing(Spacing);
|
|
|
|
}
|
2022-11-01 18:02:01 +08:00
|
|
|
|
|
|
|
} // namespace ads
|
2022-11-03 18:34:04 +08:00
|
|
|
|