File indexing completed on 2024-05-12 16:40:56

0001 /* This file is part of the KDE project
0002    Copyright (C) 2004-2007 Jarosław Staniek <staniek@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 as published by the Free Software Foundation; either
0007    version 2 of the License, or (at your option) any later version.
0008 
0009    This library is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    Library General Public License for more details.
0013 
0014    You should have received a copy of the GNU Library General Public License
0015    along with this library; see the file COPYING.LIB.  If not, write to
0016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017  * Boston, MA 02110-1301, USA.
0018 */
0019 
0020 #include "kexisectionheader.h"
0021 #include <kexiutils/utils.h>
0022 #include <kexiutils/SmallToolButton.h>
0023 #include <KexiView.h>
0024 
0025 #include <QDebug>
0026 #include <QLabel>
0027 #include <QBoxLayout>
0028 #include <QEvent>
0029 #include <QApplication>
0030 
0031 //! @internal
0032 class Q_DECL_HIDDEN KexiSectionHeader::Private
0033 {
0034 public:
0035     Private() {
0036     }
0037 
0038     Qt::Orientation orientation;
0039     QLabel *lbl;
0040     QBoxLayout *lyr;
0041     QWidget *lbl_b;
0042     QHBoxLayout *lbl_bLyr;
0043 };
0044 
0045 //==========================
0046 
0047 KexiSectionHeader::KexiSectionHeader(const QString &caption,
0048                                      Qt::Orientation o, QWidget* parent)
0049         : QWidget(parent)
0050         , d(new Private())
0051 {
0052     d->orientation = o;
0053     d->lyr = new QBoxLayout(
0054         d->orientation == Qt::Vertical ? QBoxLayout::TopToBottom : QBoxLayout::LeftToRight,
0055         this);
0056     d->lyr->setContentsMargins(0, 0, 0, 0);
0057     d->lyr->setSpacing(0);
0058 
0059     d->lbl_b = new QWidget(this);
0060     d->lbl_bLyr = new QHBoxLayout(d->lbl_b);
0061     d->lbl_bLyr->setMargin(0);
0062     d->lyr->addWidget(d->lbl_b);
0063     d->lbl = new QLabel(caption, d->lbl_b);
0064     d->lbl_bLyr->addWidget(d->lbl);
0065     d->lbl->setContentsMargins(6, 0, 0, 0);
0066     d->lbl->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Preferred);
0067     d->lbl->setAutoFillBackground(true);
0068     d->lbl->installEventFilter(this);
0069     installEventFilter(this);
0070     setWindowTitle(caption);
0071 }
0072 
0073 KexiSectionHeader::~KexiSectionHeader()
0074 {
0075     delete d;
0076 }
0077 
0078 void KexiSectionHeader::setWidget(QWidget * widget)
0079 {
0080     QLayoutItem *item = d->lyr->itemAt(1); //for sanity
0081     if (!widget || (item && widget == item->widget()))
0082         return;
0083     if (item)
0084         d->lyr->removeItem(item);
0085 //! @todo disconnect
0086     d->lyr->addWidget(widget);
0087     widget->installEventFilter(this);
0088     KexiView* view = dynamic_cast<KexiView*>(widget);
0089     if (view) {
0090         connect(view, SIGNAL(focus(bool)), this, SLOT(slotFocus(bool)));
0091         d->lbl->setBuddy(view);
0092     }
0093 }
0094 
0095 void KexiSectionHeader::addButton(const QIcon& icon, const QString& toolTip,
0096                                   const QObject * receiver, const char * member)
0097 {
0098     KexiSmallToolButton *btn = new KexiSmallToolButton(icon, QString(), d->lbl_b);
0099     d->lbl_bLyr->addWidget(btn);
0100     if (receiver && member) {
0101         connect(btn, SIGNAL(clicked()), receiver, member);
0102     }
0103     if (!toolTip.isEmpty())
0104         btn->setToolTip(toolTip);
0105 }
0106 
0107 bool KexiSectionHeader::eventFilter(QObject *o, QEvent *e)
0108 {
0109     if (o == d->lbl && e->type() == QEvent::MouseButtonRelease) {
0110         QLayoutItem *item = d->lyr->itemAt(1);
0111         if (item && item->widget())
0112             item->widget()->setFocus();
0113     }
0114     return QWidget::eventFilter(o, e);
0115 }
0116 
0117 void KexiSectionHeader::slotFocus(bool in)
0118 {
0119     qDebug() << in;
0120     in = in || qApp->focusWidget() == this;
0121     QPalette pal(d->lbl->palette());
0122     pal.setBrush(QPalette::Window,
0123                  palette().brush(in ? QPalette::Highlight : d->lbl->backgroundRole()));
0124     pal.setBrush(QPalette::WindowText,
0125                  palette().brush(in ? QPalette::HighlightedText : d->lbl->foregroundRole()));
0126     d->lbl->setPalette(pal);
0127 }
0128 
0129 QSize KexiSectionHeader::sizeHint() const
0130 {
0131     QLayoutItem *item = d->lyr->itemAt(1);
0132     if (!item || !item->widget())
0133         return QWidget::sizeHint();
0134     QSize s(item->widget()->sizeHint());
0135     return QSize(s.width(), d->lbl->sizeHint().height() + s.height());
0136 }
0137 
0138 void KexiSectionHeader::setCaption(const QString& caption)
0139 {
0140     d->lbl->setText(caption);
0141 }
0142 
0143 QString KexiSectionHeader::caption() const
0144 {
0145     return d->lbl->text();
0146 }
0147