File indexing completed on 2024-04-28 11:36:29

0001 /* This file is part of the KDE libraries
0002    Copyright (C) 2005 David Faure <faure@kde.org>
0003 
0004    This library is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU Library General Public
0006    License version 2 as published by the Free Software Foundation.
0007 
0008    This library is distributed in the hope that it will be useful,
0009    but WITHOUT ANY WARRANTY; without even the implied warranty of
0010    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0011    Library General Public License for more details.
0012 
0013    You should have received a copy of the GNU Library General Public License
0014    along with this library; see the file COPYING.LIB.  If not, write to
0015    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0016    Boston, MA 02110-1301, USA.
0017 */
0018 
0019 #include "khbox.h"
0020 
0021 #include <QEvent>
0022 #include <QApplication>
0023 #include <QHBoxLayout>
0024 #include <QVBoxLayout>
0025 
0026 KHBox::KHBox(QWidget *parent)
0027     : QFrame(parent),
0028       d(nullptr)
0029 {
0030     QHBoxLayout *layout = new QHBoxLayout(this);
0031     layout->setSpacing(0);
0032     layout->setContentsMargins(0, 0, 0, 0);
0033 
0034     setLayout(layout);
0035 }
0036 
0037 KHBox::KHBox(bool /*vertical*/, QWidget *parent)
0038     : QFrame(parent),
0039       d(nullptr)
0040 {
0041     QVBoxLayout *layout = new QVBoxLayout(this);
0042     layout->setSpacing(0);
0043     layout->setContentsMargins(0, 0, 0, 0);
0044 
0045     setLayout(layout);
0046 }
0047 
0048 KHBox::~KHBox()
0049 {
0050 }
0051 
0052 void KHBox::childEvent(QChildEvent *event)
0053 {
0054     switch (event->type()) {
0055     case QEvent::ChildAdded: {
0056         QChildEvent *childEvent = static_cast<QChildEvent *>(event);
0057         if (childEvent->child()->isWidgetType()) {
0058             QWidget *widget = static_cast<QWidget *>(childEvent->child());
0059             static_cast<QBoxLayout *>(layout())->addWidget(widget);
0060         }
0061 
0062         break;
0063     }
0064     case QEvent::ChildRemoved: {
0065         QChildEvent *childEvent = static_cast<QChildEvent *>(event);
0066         if (childEvent->child()->isWidgetType()) {
0067             QWidget *widget = static_cast<QWidget *>(childEvent->child());
0068             static_cast<QBoxLayout *>(layout())->removeWidget(widget);
0069         }
0070 
0071         break;
0072     }
0073     default:
0074         break;
0075     }
0076     QFrame::childEvent(event);
0077 }
0078 
0079 QSize KHBox::sizeHint() const
0080 {
0081     KHBox *that = const_cast<KHBox *>(this);
0082     QApplication::sendPostedEvents(that, QEvent::ChildAdded);
0083 
0084     return QFrame::sizeHint();
0085 }
0086 
0087 QSize KHBox::minimumSizeHint() const
0088 {
0089     KHBox *that = const_cast<KHBox *>(this);
0090     QApplication::sendPostedEvents(that, QEvent::ChildAdded);
0091 
0092     return QFrame::minimumSizeHint();
0093 }
0094 
0095 void KHBox::setSpacing(int spacing)
0096 {
0097     layout()->setSpacing(spacing);
0098 }
0099 
0100 void KHBox::setStretchFactor(QWidget *widget, int stretch)
0101 {
0102     static_cast<QBoxLayout *>(layout())->setStretchFactor(widget, stretch);
0103 }
0104 
0105 void KHBox::setMargin(int margin)
0106 {
0107     layout()->setMargin(margin);
0108 }
0109 
0110 #include "moc_khbox.cpp"