kexitableviewheader.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2005-2006 Jaroslaw Staniek <js@iidea.pl>
3
4 This program 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 program 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 program; see the file COPYING. 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 "kexitableviewheader.h"
21
22#include <tqapplication.h>
23#include <tqtooltip.h>
24#include <tqstyle.h>
25
26#include <kexiutils/utils.h>
27#include <kexiutils/styleproxy.h>
28
31class KexiTableViewHeaderStyle : public KexiUtils::StyleProxy
32{
33 public:
34 KexiTableViewHeaderStyle(TQStyle *parentStyle, TQWidget *widget)
35 : KexiUtils::StyleProxy(parentStyle)
36 {
37 setBackgroundColor( widget->palette().active().background() );
38 }
39 ~KexiTableViewHeaderStyle() {}
40
41 virtual void drawPrimitive( PrimitiveElement pe,
42 TQPainter *p, const TQRect &r, const TQColorGroup &cg, SFlags flags = Style_Default,
43 const TQStyleOption& option = TQStyleOption::Default ) const
44 {
45 if (pe==TQStyle::PE_HeaderSection) {
46 TQColorGroup newCg(cg);
47 newCg.setColor(TQColorGroup::Button, m_backgroundColor);
48 newCg.setColor(TQColorGroup::Background, m_backgroundColor); //set background color as well (e.g. for thinkeramik)
49 m_style->drawPrimitive( pe, p, r, newCg, flags, option );
50 return;
51 }
52 m_style->drawPrimitive( pe, p, r, cg, flags, option );
53 }
54
55 void setBackgroundColor( const TQColor& color ) { m_backgroundColor = color; }
56
57 protected:
58 TQColor m_backgroundColor;
59};
60
61KexiTableViewHeader::KexiTableViewHeader(TQWidget * parent, const char * name)
62 : TQHeader(parent, name)
63 , m_lastToolTipSection(-1)
64 , m_selectionBackgroundColor(tqApp->palette().active().highlight())
65 , m_selectedSection(-1)
66 , m_styleChangeEnabled(true)
67{
68 styleChange( style() );
69 installEventFilter(this);
70 connect(this, TQ_SIGNAL(sizeChange(int,int,int)),
71 this, TQ_SLOT(slotSizeChange(int,int,int)));
72}
73
74KexiTableViewHeader::~KexiTableViewHeader()
75{
76}
77
78void KexiTableViewHeader::styleChange( TQStyle& oldStyle )
79{
80 TQHeader::styleChange( oldStyle );
81 if (!m_styleChangeEnabled)
82 return;
83 m_styleChangeEnabled = false;
84 setStyle( new KexiTableViewHeaderStyle(&tqApp->style(), this) );
85 m_styleChangeEnabled = true;
86}
87
88int KexiTableViewHeader::addLabel ( const TQString & s, int size )
89{
90 m_toolTips += "";
91 slotSizeChange(0,0,0);//refresh
92 return TQHeader::addLabel(s, size);
93}
94
95int KexiTableViewHeader::addLabel ( const TQIconSet & iconset, const TQString & s, int size )
96{
97 m_toolTips += "";
98 slotSizeChange(0,0,0);//refresh
99 return TQHeader::addLabel(iconset, s, size);
100}
101
102void KexiTableViewHeader::removeLabel( int section )
103{
104 if (section < 0 || section >= count())
105 return;
106 TQStringList::Iterator it = m_toolTips.begin();
107 it += section;
108 m_toolTips.remove(it);
109 slotSizeChange(0,0,0);//refresh
110 TQHeader::removeLabel(section);
111}
112
113void KexiTableViewHeader::setToolTip( int section, const TQString & toolTip )
114{
115 if (section < 0 || section >= (int)m_toolTips.count())
116 return;
117 m_toolTips[ section ] = toolTip;
118}
119
120bool KexiTableViewHeader::eventFilter(TQObject * watched, TQEvent * e)
121{
122 if (e->type()==TQEvent::MouseMove) {
123 const int section = sectionAt( static_cast<TQMouseEvent*>(e)->x() );
124 if (section != m_lastToolTipSection && section >= 0 && section < (int)m_toolTips.count()) {
125 TQToolTip::remove(this, m_toolTipRect);
126 TQString tip = m_toolTips[ section ];
127 if (tip.isEmpty()) { //try label
128 TQFontMetrics fm(font());
129 int minWidth = fm.width( label( section ) ) + style().pixelMetric( TQStyle::PM_HeaderMargin );
130 TQIconSet *iset = iconSet( section );
131 if (iset)
132 minWidth += (2+iset->pixmap( TQIconSet::Small, TQIconSet::Normal ).width()); //taken from TQHeader::sectionSizeHint()
133 if (minWidth > sectionSize( section ))
134 tip = label( section );
135 }
136 if (tip.isEmpty()) {
137 m_lastToolTipSection = -1;
138 }
139 else {
140 TQToolTip::add(this, m_toolTipRect = sectionRect(section), tip);
141 m_lastToolTipSection = section;
142 }
143 }
144 }
145// if (e->type()==TQEvent::MouseButtonPress) {
146// todo
147// }
148 return TQHeader::eventFilter(watched, e);
149}
150
151void KexiTableViewHeader::slotSizeChange(int /*section*/, int /*oldSize*/, int /*newSize*/ )
152{
153 if (m_lastToolTipSection>0)
154 TQToolTip::remove(this, m_toolTipRect);
155 m_lastToolTipSection = -1; //tooltip's rect is now invalid
156}
157
158void KexiTableViewHeader::setSelectionBackgroundColor(const TQColor &color)
159{
160 m_selectionBackgroundColor = color;
161}
162
163TQColor KexiTableViewHeader::selectionBackgroundColor() const
164{
165 return m_selectionBackgroundColor;
166}
167
168void KexiTableViewHeader::setSelectedSection(int section)
169{
170 if (m_selectedSection==section || (section!=-1 && section>=count()))
171 return;
172 const int oldSection = m_selectedSection;
173 m_selectedSection = section;
174 if (oldSection!=-1)
175 update(sRect(oldSection));
176 if (m_selectedSection!=-1)
177 update(sRect(m_selectedSection));
178}
179
180int KexiTableViewHeader::selectedSection() const
181{
182 return m_selectedSection;
183}
184
185void KexiTableViewHeader::paintSection( TQPainter * p, int index, const TQRect & fr )
186{
187 const bool paintSelection = index==m_selectedSection && index != -1;
188 if (paintSelection) {
189 static_cast<KexiTableViewHeaderStyle&>(style()).setBackgroundColor(
191 palette().active().background(), m_selectionBackgroundColor, 2, 1) );
192 }
193
194 TQHeader::paintSection( p, index, fr );
195
196 if (paintSelection) { //revert the color for subsequent paints
197 static_cast<KexiTableViewHeaderStyle&>(style()).setBackgroundColor(
198 palette().active().background());
199 }
200}
201
202#include "kexitableviewheader.moc"
void setToolTip(int section, const TQString &toolTip)
a TQStyle proxy allowing to customizing the currently used style
Definition styleproxy.h:49
StyleProxy(TQStyle *parentStyle)
General Utils.
Definition identifier.h:27
type * findParent(TQObject *o, const char *className)
KEXIUTILS_EXPORT TQColor blendedColors(const TQColor &c1, const TQColor &c2, int factor1=1, int factor2=1)