kexi

kexisectionheader.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2004 Jaroslaw Staniek <js@iidea.pl>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18*/
19
20#include "kexisectionheader.h"
21#include "kexiviewbase.h"
22#include <kexiutils/utils.h>
23
24#include <tqlabel.h>
25#include <tqlayout.h>
26#include <tqhbox.h>
27#include <tqtooltip.h>
28
29#include <kiconloader.h>
30#include <kpushbutton.h>
31
32class KexiSectionHeader::BoxLayout : public TQBoxLayout
33{
34 public:
35 BoxLayout( KexiSectionHeader* parent, Direction d, int margin = 0,
36 int spacing = -1, const char * name = 0 );
37 virtual void addItem( TQLayoutItem * item );
38 TQGuardedPtr<KexiViewBase> view;
39};
40
41//==========================
42
44class KexiSectionHeaderPrivate
45{
46 public:
47 KexiSectionHeaderPrivate()
48 {
49 }
50
51 TQt::Orientation orientation;
52 TQLabel *lbl;
53 KexiSectionHeader::BoxLayout *lyr;
54 TQHBox *lbl_b;
55};
56
57//==========================
58
59KexiSectionHeader::KexiSectionHeader(const TQString &caption, TQt::Orientation o, TQWidget* parent )
60 : TQWidget(parent, "KexiSectionHeader")
61 , d( new KexiSectionHeaderPrivate() )
62{
63 d->orientation = o;
64 d->lyr = new BoxLayout( this, d->orientation==TQt::Vertical ? TQBoxLayout::TopToBottom : TQBoxLayout::LeftToRight );
65 d->lyr->setAutoAdd(true);
66 d->lbl_b = new TQHBox(this);
67 d->lbl = new TQLabel(TQString(" ")+caption, d->lbl_b);
68 d->lbl->setSizePolicy(TQSizePolicy::Minimum, TQSizePolicy::Preferred);
69 d->lbl->setFocusPolicy(TQWidget::StrongFocus);
70 d->lbl->installEventFilter(this);
71 installEventFilter(this);
72 setCaption(caption);
73}
74
75KexiSectionHeader::~KexiSectionHeader()
76{
77 delete d;
78}
79
80void KexiSectionHeader::addButton(const TQString& icon, const TQString& toolTip,
81 const TQObject * receiver, const char * member)
82{
83 KPushButton *btn = new KPushButton(d->lbl_b);
84 btn->setFlat(true);
85 btn->setFocusPolicy(TQWidget::NoFocus);
86 btn->setSizePolicy(TQSizePolicy::Fixed, TQSizePolicy::Preferred);
87 if (receiver && member) {
88 connect(btn, TQ_SIGNAL(clicked()), receiver, member);
89 }
90
91 if (!icon.isEmpty()) {
92 TQIconSet iset = SmallIconSet(icon);
93 btn->setIconSet( iset );
94 TQFontMetrics fm(d->lbl->font());
95 btn->setMaximumHeight( TQMAX(fm.height(), 16) );
96 }
97 if (!toolTip.isEmpty()) {
98 TQToolTip::add(btn, toolTip);
99 }
100}
101
102bool KexiSectionHeader::eventFilter( TQObject *o, TQEvent *e )
103{
104 if (o == d->lbl && e->type()==TQEvent::MouseButtonRelease) {//|| e->type()==TQEvent::FocusOut) {// && o->inherits("TQWidget")) {
105 if (d->lyr->view)
106 d->lyr->view->setFocus();
107// if (KexiUtils::hasParent( this, o)) {
108// d->lbl->setPaletteBackgroundColor( e->type()==TQEvent::FocusIn ? red : blue);
109// }
110 }
111 return TQWidget::eventFilter(o,e);
112}
113
114void KexiSectionHeader::slotFocus(bool in)
115{
116 in = in || focusWidget()==this;
117 d->lbl->setPaletteBackgroundColor(
118 in ? palette().active().color(TQColorGroup::Highlight) : palette().active().color(TQColorGroup::Background) );
119 d->lbl->setPaletteForegroundColor(
120 in ? palette().active().color(TQColorGroup::HighlightedText) : palette().active().color(TQColorGroup::Foreground) );
121}
122
123TQSize KexiSectionHeader::sizeHint() const
124{
125 if (!d->lyr->view)
126 return TQWidget::sizeHint();
127 TQSize s = d->lyr->view->sizeHint();
128 return TQSize(s.width(), d->lbl->sizeHint().height() + s.height());
129}
130
131/*void KexiSectionHeader::setFocus()
132{
133 if (d->lyr->view)
134 d->lyr->view->setFocus();
135 else
136 TQWidget::setFocus();
137}*/
138
139//======================
140
141KexiSectionHeader::BoxLayout::BoxLayout( KexiSectionHeader* parent, Direction d, int margin, int spacing, const char * name )
142 : TQBoxLayout(parent, d, margin, spacing, name )
143{
144}
145
146void KexiSectionHeader::BoxLayout::addItem( TQLayoutItem * item )
147{
148 TQBoxLayout::addItem( item );
149 if (item->widget()) {
150 item->widget()->installEventFilter( mainWidget() );
151 if (item->widget()->inherits("KexiViewBase")) {
152 view = static_cast<KexiViewBase*>(item->widget());
153 KexiSectionHeader *sh = static_cast<KexiSectionHeader*>(mainWidget());
154 connect(view,TQ_SIGNAL(focus(bool)),sh,TQ_SLOT(slotFocus(bool)));
155 sh->d->lbl->setBuddy(item->widget());
156 }
157 }
158}
159
160
161#include "kexisectionheader.moc"
162