1 /****************************************************************************
3 ** Copyright (C) Qxt Foundation. Some rights reserved.
5 ** This file is part of the QxtGui module of the Qt eXTension library
7 ** This library is free software; you can redistribute it and/or modify it
8 ** under the terms of th Common Public License, version 1.0, as published by
11 ** This file is provided "AS IS", without WARRANTIES OR CONDITIONS OF ANY
12 ** KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY
13 ** WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR
14 ** FITNESS FOR A PARTICULAR PURPOSE.
16 ** You should have received a copy of the CPL along with this file.
17 ** See the LICENSE file and the cpl1.0.txt file included with the source
18 ** distribution for more information. If you did not receive a copy of the
19 ** license, contact the Qxt Foundation.
21 ** <http://libqxt.sourceforge.net> <foundation@libqxt.org>
23 ****************************************************************************/
25 #include "QxtHeaderView.h"
27 #include <QApplication>
29 #include <QMouseEvent>
33 class QxtHeaderViewPrivate
37 QxtHeaderViewPrivate()
43 QSize action_size_c() const
48 QList<QAction *> actions;
56 \class QxtHeaderView QxtHeaderView
58 \brief a headerview that can have QActions
60 draws actions directly into the header. it's like a toolbar for your ItemView.
62 \image html qxtheaderview.png "QxtHeaderView with a few actions."
66 \fn QxtHeaderView::QxtHeaderView()
71 QxtHeaderView::QxtHeaderView (Qt::Orientation o ,QWidget *parent):QHeaderView(o,parent)
73 priv = new QxtHeaderViewPrivate;
74 setStretchLastSection(true);
75 QStyleOptionViewItem option;
76 option.initFrom(this);
77 priv->action_size= new QSize( QApplication::style()->subElementRect(QStyle::SE_ViewItemCheckIndicator,&option).size());
78 setMouseTracking (true );
83 //-----------------------------------------------------------------
85 adds a new QAction \a action to the header.
87 void QxtHeaderView::addAction(QAction * action)
89 priv->actions.append(action);
92 //-----------------------------------------------------------------
94 void QxtHeaderView::mouseMoveEvent ( QMouseEvent * m )
96 if (!priv->action_size)
98 setToolTip (QString());
102 int moved = subWidth(priv->action_size_c(),priv->space);
103 int wm=width()-moved;
106 setToolTip (QString());
114 wm-=priv->action_size_c().width();
117 if (i>(priv->actions.count()-1))break;
121 setToolTip (priv->actions[i]->toolTip ());
127 setToolTip (QString());
133 void QxtHeaderView::paintSection ( QPainter * painter, const QRect & rm, int logicalIndex ) const
139 QHeaderView::paintSection(painter,rect,logicalIndex);
143 subPaint(painter, rect, logicalIndex,priv->action_size_c(),priv->space);
144 int moved = subWidth(priv->action_size_c(),priv->space);
145 rect.adjust(0,0,-moved,0);
147 rect.adjust(0,0,-priv->space,0);
149 foreach(a, priv->actions)
151 QIcon img = a->icon();
152 QRect r=QStyle::alignedRect ( Qt::LeftToRight, Qt::AlignRight | Qt::AlignVCenter, *priv->action_size,rect);
153 img.paint(painter, r.x(), r.y(), r.width(), r.height(), Qt::AlignCenter);
154 rect.adjust(0,0,-priv->action_size->width()-priv->space,0); ///shrink the available space rect
158 void QxtHeaderView::mousePressEvent ( QMouseEvent * m )
160 if (!priv->action_size)return;
162 subClick(m,priv->action_size_c(), priv->space ) ;
163 int moved = subWidth(priv->action_size_c(),priv->space);
166 int wm=width()-moved;
168 if (m->x()>wm)return;
174 wm-=priv->action_size_c().width();
177 if (i>(priv->actions.count()-1))break;
181 priv->actions[i]->trigger();
195 //-----------------------------------------------------------------
197 reimplement this to add your own icons, widgets, or whatever to the header.\n
200 void QxtHeaderView::subPaint(QPainter * , const QRect & , int ,QSize , int ) const
203 reimplement this to receive clicks to your own icons,widgets, etc...
206 void QxtHeaderView::subClick(QMouseEvent * ,QSize , int)
209 when reimplementing subPaint and/or subClick you must also override this function and return the width your custom drawing takes,
210 so the QActions know where to start.
213 int QxtHeaderView::subWidth(QSize , int ) const