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 KexiSectionHeader::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         KexiSectionHeader::BoxLayout *lyr;
00054         QHBox *lbl_b;
00055 };
00056 
00057 //==========================
00058 
00059 KexiSectionHeader::KexiSectionHeader(const QString &caption, Orientation o, QWidget* parent )
00060     : QWidget(parent, "KexiSectionHeader")
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->setFocusPolicy(StrongFocus);
00070     d->lbl->installEventFilter(this);
00071     installEventFilter(this);
00072     setCaption(caption);
00073 }
00074 
00075 KexiSectionHeader::~KexiSectionHeader()
00076 {
00077     delete d;
00078 }
00079 
00080 void KexiSectionHeader::addButton(const QString& icon, const QString& toolTip,
00081     const QObject * receiver, const char * member)
00082 {
00083     KPushButton *btn = new KPushButton(d->lbl_b);
00084     btn->setFlat(true);
00085     btn->setFocusPolicy(NoFocus);
00086     btn->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred);
00087     if (receiver && member) {
00088         connect(btn, SIGNAL(clicked()), receiver, member);
00089     }
00090 
00091     if (!icon.isEmpty()) {
00092         QIconSet iset = SmallIconSet(icon);
00093         btn->setIconSet( iset );
00094         QFontMetrics fm(d->lbl->font());
00095         btn->setMaximumHeight( QMAX(fm.height(), 16) );
00096     }
00097     if (!toolTip.isEmpty()) {
00098         QToolTip::add(btn, toolTip);
00099     }
00100 }
00101 
00102 bool KexiSectionHeader::eventFilter( QObject *o, QEvent *e )
00103 {
00104     if (o == d->lbl && e->type()==QEvent::MouseButtonRelease) {//|| e->type()==QEvent::FocusOut) {// && o->inherits("QWidget")) {
00105         if (d->lyr->view)
00106             d->lyr->view->setFocus();
00107 //      if (KexiUtils::hasParent( this, static_cast<QWidget*>(o))) {
00108 //          d->lbl->setPaletteBackgroundColor( e->type()==QEvent::FocusIn ? red : blue);
00109 //      }
00110     }
00111     return QWidget::eventFilter(o,e);
00112 }
00113 
00114 void KexiSectionHeader::slotFocus(bool in)
00115 {
00116     in = in || focusWidget()==this;
00117     d->lbl->setPaletteBackgroundColor( 
00118         in ? palette().active().color(QColorGroup::Highlight) : palette().active().color(QColorGroup::Background) );
00119     d->lbl->setPaletteForegroundColor( 
00120         in ? palette().active().color(QColorGroup::HighlightedText) : palette().active().color(QColorGroup::Foreground) );
00121 }
00122 
00123 QSize KexiSectionHeader::sizeHint() const
00124 {
00125     if (!d->lyr->view)
00126         return QWidget::sizeHint();
00127     QSize s = d->lyr->view->sizeHint();
00128     return QSize(s.width(), d->lbl->sizeHint().height() + s.height());
00129 }
00130 
00131 /*void KexiSectionHeader::setFocus()
00132 {
00133     if (d->lyr->view)
00134         d->lyr->view->setFocus();
00135     else
00136         QWidget::setFocus();
00137 }*/
00138 
00139 //======================
00140 
00141 KexiSectionHeader::BoxLayout::BoxLayout( KexiSectionHeader* parent, Direction d, int margin, int spacing, const char * name )
00142  : QBoxLayout(parent, d, margin, spacing, name )
00143 {
00144 }
00145 
00146 void KexiSectionHeader::BoxLayout::addItem( QLayoutItem * item )
00147 {
00148     QBoxLayout::addItem( item );
00149     if (item->widget()) {
00150         item->widget()->installEventFilter( mainWidget() );
00151         if (item->widget()->inherits("KexiViewBase")) {
00152             view = static_cast<KexiViewBase*>(item->widget());
00153             KexiSectionHeader *sh = static_cast<KexiSectionHeader*>(mainWidget());
00154             connect(view,SIGNAL(focus(bool)),sh,SLOT(slotFocus(bool)));
00155             sh->d->lbl->setBuddy(item->widget());
00156         }
00157     }
00158 }
00159 
00160 
00161 #include "kexisectionheader.moc"
00162 
KDE Home | KDE Accessibility Home | Description of Access Keys