kexi

kexitableviewheader.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2005-2006 Jaroslaw Staniek <js@iidea.pl>
00003 
00004    This program 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 program 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 program; see the file COPYING.  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 "kexitableviewheader.h"
00021 
00022 #include <qapplication.h>
00023 #include <qtooltip.h>
00024 #include <qstyle.h>
00025 
00026 #include <kexiutils/utils.h>
00027 #include <kexiutils/styleproxy.h>
00028 
00031 class KexiTableViewHeaderStyle : public KexiUtils::StyleProxy
00032 {
00033     public:
00034         KexiTableViewHeaderStyle(QStyle *parentStyle, QWidget *widget)
00035          : KexiUtils::StyleProxy(parentStyle)
00036         {
00037             setBackgroundColor( widget->palette().active().background() );
00038         }
00039         ~KexiTableViewHeaderStyle() {}
00040 
00041         virtual void drawPrimitive( PrimitiveElement pe,
00042             QPainter *p, const QRect &r, const QColorGroup &cg, SFlags flags = Style_Default,
00043             const QStyleOption& option = QStyleOption::Default ) const
00044         {
00045             if (pe==QStyle::PE_HeaderSection) {
00046                 QColorGroup newCg(cg);
00047                 newCg.setColor(QColorGroup::Button, m_backgroundColor);
00048                 newCg.setColor(QColorGroup::Background, m_backgroundColor); //set background color as well (e.g. for thinkeramik)
00049                 m_style->drawPrimitive( pe, p, r, newCg, flags, option );
00050                 return;
00051             }
00052             m_style->drawPrimitive( pe, p, r, cg, flags, option );
00053         }
00054 
00055         void setBackgroundColor( const QColor& color ) { m_backgroundColor = color; }
00056 
00057     protected:
00058         QColor m_backgroundColor;
00059 };
00060 
00061 KexiTableViewHeader::KexiTableViewHeader(QWidget * parent, const char * name) 
00062     : QHeader(parent, name)
00063     , m_lastToolTipSection(-1)
00064     , m_selectionBackgroundColor(qApp->palette().active().highlight())
00065     , m_selectedSection(-1)
00066     , m_styleChangeEnabled(true)
00067 {
00068     styleChange( style() );
00069     installEventFilter(this);
00070     connect(this, SIGNAL(sizeChange(int,int,int)), 
00071         this, SLOT(slotSizeChange(int,int,int)));
00072 }
00073 
00074 KexiTableViewHeader::~KexiTableViewHeader()
00075 {
00076 }
00077 
00078 void KexiTableViewHeader::styleChange( QStyle& oldStyle )
00079 {
00080     QHeader::styleChange( oldStyle );
00081     if (!m_styleChangeEnabled)
00082         return;
00083     m_styleChangeEnabled = false;
00084     setStyle( new KexiTableViewHeaderStyle(&qApp->style(), this) );
00085     m_styleChangeEnabled = true;
00086 }
00087 
00088 int KexiTableViewHeader::addLabel ( const QString & s, int size )
00089 {
00090     m_toolTips += "";
00091     slotSizeChange(0,0,0);//refresh
00092     return QHeader::addLabel(s, size);
00093 }
00094 
00095 int KexiTableViewHeader::addLabel ( const QIconSet & iconset, const QString & s, int size )
00096 {
00097     m_toolTips += "";
00098     slotSizeChange(0,0,0);//refresh
00099     return QHeader::addLabel(iconset, s, size);
00100 }
00101 
00102 void KexiTableViewHeader::removeLabel( int section )
00103 {
00104     if (section < 0 || section >= count())
00105         return;
00106     QStringList::Iterator it = m_toolTips.begin();
00107     it += section;
00108     m_toolTips.remove(it);
00109     slotSizeChange(0,0,0);//refresh
00110     QHeader::removeLabel(section);
00111 }
00112 
00113 void KexiTableViewHeader::setToolTip( int section, const QString & toolTip )
00114 {
00115     if (section < 0 || section >= (int)m_toolTips.count())
00116         return;
00117     m_toolTips[ section ] = toolTip;
00118 }
00119 
00120 bool KexiTableViewHeader::eventFilter(QObject * watched, QEvent * e)
00121 {
00122     if (e->type()==QEvent::MouseMove) {
00123         const int section = sectionAt( static_cast<QMouseEvent*>(e)->x() );
00124         if (section != m_lastToolTipSection && section >= 0 && section < (int)m_toolTips.count()) {
00125             QToolTip::remove(this, m_toolTipRect);
00126             QString tip = m_toolTips[ section ];
00127             if (tip.isEmpty()) { //try label
00128                 QFontMetrics fm(font());
00129                 int minWidth = fm.width( label( section ) ) + style().pixelMetric( QStyle::PM_HeaderMargin );
00130                 QIconSet *iset = iconSet( section );
00131                 if (iset)
00132                     minWidth += (2+iset->pixmap( QIconSet::Small, QIconSet::Normal ).width()); //taken from QHeader::sectionSizeHint()
00133                 if (minWidth > sectionSize( section ))
00134                     tip = label( section );
00135             }
00136             if (tip.isEmpty()) {
00137                 m_lastToolTipSection = -1;
00138             }
00139             else {
00140                 QToolTip::add(this, m_toolTipRect = sectionRect(section), tip);
00141                 m_lastToolTipSection = section;
00142             }
00143         }
00144     }
00145 //          if (e->type()==QEvent::MouseButtonPress) {
00146 //  todo
00147 //          }
00148     return QHeader::eventFilter(watched, e);
00149 }
00150 
00151 void KexiTableViewHeader::slotSizeChange(int /*section*/, int /*oldSize*/, int /*newSize*/ )
00152 {
00153     if (m_lastToolTipSection>0)
00154         QToolTip::remove(this, m_toolTipRect);
00155     m_lastToolTipSection = -1; //tooltip's rect is now invalid
00156 }
00157 
00158 void KexiTableViewHeader::setSelectionBackgroundColor(const QColor &color)
00159 {
00160     m_selectionBackgroundColor = color;
00161 }
00162 
00163 QColor KexiTableViewHeader::selectionBackgroundColor() const
00164 {
00165     return m_selectionBackgroundColor;
00166 }
00167 
00168 void KexiTableViewHeader::setSelectedSection(int section)
00169 {
00170     if (m_selectedSection==section || (section!=-1 && section>=count()))
00171         return;
00172     const int oldSection = m_selectedSection;
00173     m_selectedSection = section;
00174     if (oldSection!=-1)
00175         update(sRect(oldSection));
00176     if (m_selectedSection!=-1)
00177         update(sRect(m_selectedSection));
00178 }
00179 
00180 int KexiTableViewHeader::selectedSection() const
00181 {
00182     return m_selectedSection;
00183 }
00184 
00185 void KexiTableViewHeader::paintSection( QPainter * p, int index, const QRect & fr )
00186 {
00187     const bool paintSelection = index==m_selectedSection && index != -1;
00188     if (paintSelection) {
00189         static_cast<KexiTableViewHeaderStyle&>(style()).setBackgroundColor(
00190             KexiUtils::blendedColors( 
00191                 palette().active().background(), m_selectionBackgroundColor, 2, 1) );
00192     }
00193 
00194     QHeader::paintSection( p, index, fr );
00195 
00196     if (paintSelection) { //revert the color for subsequent paints
00197         static_cast<KexiTableViewHeaderStyle&>(style()).setBackgroundColor(
00198             palette().active().background());
00199     }
00200 }
00201 
00202 #include "kexitableviewheader.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys