summaryrefslogtreecommitdiffstats
path: root/kexi/widget/tableview/kexitableviewheader.cpp
blob: d217337b7843881ada47aa8bc66120cf27b0a437 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/* This file is part of the KDE project
   Copyright (C) 2005-2006 Jaroslaw Staniek <js@iidea.pl>

   This program is free software; you can redistribute it and,or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this program; see the file COPYING.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
*/

#include "kexitableviewheader.h"

#include <tqapplication.h>
#include <tqtooltip.h>
#include <tqstyle.h>

#include <kexiutils/utils.h>
#include <kexiutils/styleproxy.h>

//! @internal A style that allows to temporary change background color while
//!           drawing header section primitive. Used in KexiTableViewHeader.
class KexiTableViewHeaderStyle : public KexiUtils::StyleProxy
{
	public:
		KexiTableViewHeaderStyle(TQStyle *parentStyle, TQWidget *widget)
		 : KexiUtils::StyleProxy(parentStyle)
		{
			setBackgroundColor( widget->tqpalette().active().background() );
		}
		~KexiTableViewHeaderStyle() {}

		virtual void tqdrawPrimitive( TQ_PrimitiveElement pe,
			TQPainter *p, const TQRect &r, const TQColorGroup &cg, SFlags flags = Style_Default,
			const TQStyleOption& option = TQStyleOption::Default ) const
		{
			if (pe==TQStyle::PE_HeaderSection) {
				TQColorGroup newCg(cg);
				newCg.setColor(TQColorGroup::Button, m_backgroundColor);
				newCg.setColor(TQColorGroup::Background, m_backgroundColor); //set background color as well (e.g. for thinkeramik)
				m_style->tqdrawPrimitive( pe, p, r, newCg, flags, option );
				return;
			}
			m_style->tqdrawPrimitive( pe, p, r, cg, flags, option );
		}

		void setBackgroundColor( const TQColor& color ) { m_backgroundColor = color; }

	protected:
		TQColor m_backgroundColor;
};

KexiTableViewHeader::KexiTableViewHeader(TQWidget * parent, const char * name) 
	: TQHeader(parent, name)
	, m_lastToolTipSection(-1)
	, m_selectionBackgroundColor(tqApp->tqpalette().active().highlight())
	, m_selectedSection(-1)
	, m_styleChangeEnabled(true)
{
	styleChange( tqstyle() );
	installEventFilter(this);
	connect(this, TQT_SIGNAL(sizeChange(int,int,int)), 
		this, TQT_SLOT(slotSizeChange(int,int,int)));
}

KexiTableViewHeader::~KexiTableViewHeader()
{
}

void KexiTableViewHeader::styleChange( TQStyle& oldStyle )
{
	TQHeader::styleChange( oldStyle );
	if (!m_styleChangeEnabled)
		return;
	m_styleChangeEnabled = false;
	setStyle( new KexiTableViewHeaderStyle(&tqApp->tqstyle(), this) );
	m_styleChangeEnabled = true;
}

int KexiTableViewHeader::addLabel ( const TQString & s, int size )
{
	m_toolTips += "";
	slotSizeChange(0,0,0);//refresh
	return TQHeader::addLabel(s, size);
}

int KexiTableViewHeader::addLabel ( const TQIconSet & iconset, const TQString & s, int size )
{
	m_toolTips += "";
	slotSizeChange(0,0,0);//refresh
	return TQHeader::addLabel(iconset, s, size);
}

void KexiTableViewHeader::removeLabel( int section )
{
	if (section < 0 || section >= count())
		return;
	TQStringList::Iterator it = m_toolTips.begin();
	it += section;
	m_toolTips.remove(it);
	slotSizeChange(0,0,0);//refresh
	TQHeader::removeLabel(section);
}

void KexiTableViewHeader::setToolTip( int section, const TQString & toolTip )
{
	if (section < 0 || section >= (int)m_toolTips.count())
		return;
	m_toolTips[ section ] = toolTip;
}

bool KexiTableViewHeader::eventFilter(TQObject * watched, TQEvent * e)
{
	if (e->type()==TQEvent::MouseMove) {
		const int section = sectionAt( TQT_TQMOUSEEVENT(e)->x() );
		if (section != m_lastToolTipSection && section >= 0 && section < (int)m_toolTips.count()) {
			TQToolTip::remove(this, m_toolTipRect);
			TQString tip = m_toolTips[ section ];
			if (tip.isEmpty()) { //try label
				TQFontMetrics fm(font());
				int minWidth = fm.width( label( section ) ) + tqstyle().pixelMetric( TQStyle::PM_HeaderMargin );
				TQIconSet *iset = iconSet( section );
				if (iset)
					minWidth += (2+iset->pixmap( TQIconSet::Small, TQIconSet::Normal ).width()); //taken from TQHeader::sectionSizeHint()
				if (minWidth > sectionSize( section ))
					tip = label( section );
			}
			if (tip.isEmpty()) {
				m_lastToolTipSection = -1;
			}
			else {
				TQToolTip::add(this, m_toolTipRect = sectionRect(section), tip);
				m_lastToolTipSection = section;
			}
		}
	}
//			if (e->type()==TQEvent::MouseButtonPress) {
//	todo
//			}
	return TQHeader::eventFilter(watched, e);
}

void KexiTableViewHeader::slotSizeChange(int /*section*/, int /*oldSize*/, int /*newSize*/ )
{
	if (m_lastToolTipSection>0)
		TQToolTip::remove(this, m_toolTipRect);
	m_lastToolTipSection = -1; //tooltip's rect is now invalid
}

void KexiTableViewHeader::setSelectionBackgroundColor(const TQColor &color)
{
	m_selectionBackgroundColor = color;
}

TQColor KexiTableViewHeader::selectionBackgroundColor() const
{
	return m_selectionBackgroundColor;
}

void KexiTableViewHeader::setSelectedSection(int section)
{
	if (m_selectedSection==section || (section!=-1 && section>=count()))
		return;
	const int oldSection = m_selectedSection;
	m_selectedSection = section;
	if (oldSection!=-1)
		update(sRect(oldSection));
	if (m_selectedSection!=-1)
		update(sRect(m_selectedSection));
}

int KexiTableViewHeader::selectedSection() const
{
	return m_selectedSection;
}

void KexiTableViewHeader::paintSection( TQPainter * p, int index, const TQRect & fr )
{
	const bool paintSelection = index==m_selectedSection && index != -1;
	if (paintSelection) {
		static_cast<KexiTableViewHeaderStyle&>(tqstyle()).setBackgroundColor(
			KexiUtils::blendedColors( 
				tqpalette().active().background(), m_selectionBackgroundColor, 2, 1) );
	}

	TQHeader::paintSection( p, index, fr );

	if (paintSelection) { //revert the color for subsequent paints
		static_cast<KexiTableViewHeaderStyle&>(tqstyle()).setBackgroundColor(
			tqpalette().active().background());
	}
}

#include "kexitableviewheader.moc"