File indexing completed on 2024-04-28 04:37:34

0001 /*
0002     SPDX-FileCopyrightText: 2018 Friedrich W. H. Kossebau <kossebau@kde.org>
0003     SPDX-FileCopyrightText: 2015 Milian Wolff <mail@milianw.de>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "viewbarcontainer.h"
0009 
0010 // Qt
0011 #include <QStackedLayout>
0012 #include <QDebug>
0013 
0014 namespace {
0015 
0016 class ViewBarStackedLayout : public QStackedLayout
0017 {
0018     Q_OBJECT
0019 
0020 public:
0021     QSize sizeHint() const override
0022     {
0023         if (currentWidget()) {
0024             return currentWidget()->sizeHint();
0025         }
0026         return QStackedLayout::sizeHint();
0027     }
0028 
0029     QSize minimumSize() const override
0030     {
0031         if (currentWidget()) {
0032             return currentWidget()->minimumSize();
0033         }
0034         return QStackedLayout::minimumSize();
0035     }
0036 };
0037 
0038 }
0039 
0040 namespace Sublime {
0041 
0042 class ViewBarContainerPrivate
0043 {
0044 public:
0045     ViewBarContainerPrivate(ViewBarContainer* q)
0046         : layout(new ViewBarStackedLayout)
0047     {
0048         q->setLayout(layout);
0049     }
0050 
0051 public:
0052     ViewBarStackedLayout* layout;
0053 };
0054 
0055 ViewBarContainer::ViewBarContainer(QWidget *parent)
0056     : QWidget(parent)
0057     , d_ptr(new ViewBarContainerPrivate(this))
0058 {
0059 }
0060 
0061 ViewBarContainer::~ViewBarContainer()
0062 {
0063     Q_D(ViewBarContainer);
0064 
0065     // unparent any viewbars which may still exist
0066     // other code is still tracking those
0067     for (int i = d->layout->count(); i > 0; --i) {
0068         auto widget = d->layout->itemAt(i-1)->widget();
0069         if (widget) {
0070             d->layout->removeWidget(widget);
0071             widget->setParent(nullptr);
0072             widget->hide();
0073         }
0074     }
0075 }
0076 
0077 void ViewBarContainer::addViewBar(QWidget* viewBar)
0078 {
0079     Q_D(ViewBarContainer);
0080 
0081     Q_ASSERT(viewBar);
0082     d->layout->addWidget(viewBar);
0083 }
0084 
0085 void ViewBarContainer::removeViewBar(QWidget* viewBar)
0086 {
0087     Q_D(ViewBarContainer);
0088 
0089     Q_ASSERT(viewBar);
0090     d->layout->removeWidget(viewBar);
0091     viewBar->hide();
0092 
0093     if (viewBar == d->layout->currentWidget()) {
0094         hide();
0095     }
0096 }
0097 
0098 void ViewBarContainer::setCurrentViewBar(QWidget* viewBar)
0099 {
0100     Q_D(ViewBarContainer);
0101 
0102     Q_ASSERT(viewBar);
0103     d->layout->setCurrentWidget(viewBar);
0104 }
0105 
0106 void ViewBarContainer::showViewBar(QWidget* viewBar)
0107 {
0108     Q_D(ViewBarContainer);
0109 
0110     Q_ASSERT(viewBar);
0111     d->layout->setCurrentWidget(viewBar);
0112     viewBar->show();
0113 
0114     show();
0115 }
0116 
0117 void ViewBarContainer::hideViewBar(QWidget* viewBar)
0118 {
0119     Q_D(ViewBarContainer);
0120 
0121     Q_ASSERT(viewBar);
0122     d->layout->setCurrentWidget(viewBar);
0123     viewBar->hide();
0124 
0125     hide();
0126 }
0127 
0128 }
0129 
0130 #include "viewbarcontainer.moc"
0131 #include "moc_viewbarcontainer.cpp"