kexi

kexisectionheader.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2004 Jaroslaw Staniek <js@iidea.pl>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this library; see the file COPYING.LIB.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #include "kexisectionheader.h"
00021 #include "kexiviewbase.h"
00022 #include <kexiutils/utils.h>
00023 
00024 #include <qlabel.h>
00025 #include <qlayout.h>
00026 #include <qhbox.h>
00027 #include <qtooltip.h>
00028 
00029 #include <kiconloader.h>
00030 #include <kpushbutton.h>
00031 
00032 class BoxLayout : public QBoxLayout
00033 {
00034     public:
00035         BoxLayout( KexiSectionHeader* parent, Direction d, int margin = 0, 
00036             int spacing = -1, const char * name = 0 );
00037         virtual void addItem( QLayoutItem * item );
00038         QGuardedPtr<KexiViewBase> view;
00039 };
00040 
00041 //==========================
00042 
00044 class KexiSectionHeaderPrivate
00045 {
00046     public:
00047         KexiSectionHeaderPrivate() 
00048         {
00049         }
00050     
00051         Qt::Orientation orientation;
00052         QLabel *lbl;
00053         BoxLayout *lyr;
00054         QHBox *lbl_b;
00055 };
00056 
00057 //==========================
00058 
00059 KexiSectionHeader::KexiSectionHeader(const QString &caption, Orientation o, QWidget* parent, const char * name )
00060     : QWidget(parent, name)
00061     , d( new KexiSectionHeaderPrivate() )
00062 {
00063     d->orientation = o;
00064     d->lyr = new BoxLayout( this, d->orientation==Vertical ? QBoxLayout::TopToBottom : QBoxLayout::LeftToRight );
00065     d->lyr->setAutoAdd(true);
00066     d->lbl_b = new QHBox(this);
00067     d->lbl = new QLabel(QString(" ")+caption, d->lbl_b);
00068     d->lbl->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Preferred);
00069     d->lbl->installEventFilter(this);
00070     installEventFilter(this);
00071     setCaption(caption);
00072 }
00073 
00074 KexiSectionHeader::~KexiSectionHeader()
00075 {
00076     delete d;
00077 }
00078 
00079 void KexiSectionHeader::addButton(const QString& icon, const QString& toolTip,
00080     const QObject * receiver, const char * member)
00081 {
00082     KPushButton *btn = new KPushButton(d->lbl_b);
00083     btn->setFlat(true);
00084     btn->setFocusPolicy(NoFocus);
00085     btn->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred);
00086     if (receiver && member) {
00087         connect(btn, SIGNAL(clicked()), receiver, member);
00088     }
00089 
00090     if (!icon.isEmpty()) {
00091         QIconSet iset = SmallIconSet(icon);
00092         btn->setIconSet( iset );
00093         QFontMetrics fm(d->lbl->font());
00094         btn->setMaximumHeight( QMAX(fm.height(), 16) );
00095     }
00096     if (!toolTip.isEmpty()) {
00097         QToolTip::add(btn, toolTip);
00098     }
00099 }
00100 
00101 bool KexiSectionHeader::eventFilter( QObject *o, QEvent *e )
00102 {
00103     if (o == d->lbl && e->type()==QEvent::MouseButtonRelease) {//|| e->type()==QEvent::FocusOut) {// && o->inherits("QWidget")) {
00104         if (d->lyr->view)
00105             d->lyr->view->setFocus();
00106 //      if (KexiUtils::hasParent( this, static_cast<QWidget*>(o))) {
00107 //          d->lbl->setPaletteBackgroundColor( e->type()==QEvent::FocusIn ? red : blue);
00108 //      }
00109     }
00110     return QWidget::eventFilter(o,e);
00111 }
00112 
00113 
00114 void KexiSectionHeader::slotFocus(bool in) {
00115     in = in || focusWidget()==this;
00116     d->lbl->setPaletteBackgroundColor( 
00117         in ? palette().active().color(QColorGroup::Highlight) : palette().active().color(QColorGroup::Background) );
00118     d->lbl->setPaletteForegroundColor( 
00119         in ? palette().active().color(QColorGroup::HighlightedText) : palette().active().color(QColorGroup::Foreground) );
00120 }
00121 
00122 QSize KexiSectionHeader::sizeHint() const
00123 {
00124     if (!d->lyr->view)
00125         return QWidget::sizeHint();
00126     QSize s = d->lyr->view->sizeHint();
00127     return QSize(s.width(), d->lbl->sizeHint().height() + s.height());
00128 }
00129 
00130 /*void KexiSectionHeader::setFocus()
00131 {
00132     if (d->lyr->view)
00133         d->lyr->view->setFocus();
00134     else
00135         QWidget::setFocus();
00136 }*/
00137 
00138 //======================
00139 
00140 BoxLayout::BoxLayout( KexiSectionHeader* parent, Direction d, int margin, int spacing, const char * name )
00141  : QBoxLayout(parent, d, margin, spacing, name )
00142 {
00143 }
00144 
00145 void BoxLayout::addItem( QLayoutItem * item )
00146 {
00147     QBoxLayout::addItem( item );
00148     if (item->widget()) {
00149         item->widget()->installEventFilter( mainWidget() );
00150         if (item->widget()->inherits("KexiViewBase")) {
00151             view = static_cast<KexiViewBase*>(item->widget());
00152             KexiSectionHeader *sh = static_cast<KexiSectionHeader*>(mainWidget());
00153             connect(view,SIGNAL(focus(bool)),sh,SLOT(slotFocus(bool)));
00154         }
00155     }
00156 }
00157 
00158 
00159 #include "kexisectionheader.moc"
00160 
KDE Home | KDE Accessibility Home | Description of Access Keys