Ok, the long awaited config wizard is here (at least in a very basic state). There...
[quassel.git] / src / contrib / libqxt-2007-10-24 / src / gui / qxtheaderview.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) Qxt Foundation. Some rights reserved.
4 **
5 ** This file is part of the QxtGui module of the Qt eXTension library
6 **
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
9 ** IBM.
10 **
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.
15 **
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.
20 **
21 ** <http://libqxt.sourceforge.net>  <foundation@libqxt.org>
22 **
23 ****************************************************************************/
24 #if 0
25 #include "QxtHeaderView.h"
26 #include <QPainter>
27 #include <QApplication>
28 #include <QDebug>
29 #include <QMouseEvent>
30 #include <QAction>
31
32
33 class QxtHeaderViewPrivate
34 {
35 public:
36
37     QxtHeaderViewPrivate()
38     {
39         space=10;
40         action_size=NULL;
41     }
42
43     QSize action_size_c() const
44     {
45         return *action_size;
46     }
47
48     QList<QAction *> actions;
49     QSize * action_size;
50     int space;
51
52 };
53
54
55 /*!
56     \class QxtHeaderView QxtHeaderView
57     \ingroup QxtGui
58     \brief a headerview that can have QActions
59
60     draws actions directly into the header. it's like a toolbar for your ItemView.
61
62     \image html qxtheaderview.png "QxtHeaderView with a few actions."
63  */
64
65 /*!
66     \fn QxtHeaderView::QxtHeaderView()
67
68    default Constructor
69  */
70
71 QxtHeaderView::QxtHeaderView (Qt::Orientation o ,QWidget *parent):QHeaderView(o,parent)
72 {
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 );
79 }
80
81
82
83 //-----------------------------------------------------------------
84 /*!
85     adds a new QAction \a action to the header.
86  */
87 void QxtHeaderView::addAction(QAction * action)
88 {
89     priv->actions.append(action);
90 }
91
92 //-----------------------------------------------------------------
93
94 void  QxtHeaderView::mouseMoveEvent ( QMouseEvent * m )
95 {
96     if (!priv->action_size)
97     {
98         setToolTip (QString());
99         leaveEvent ( m );
100         return;
101     }
102     int moved = subWidth(priv->action_size_c(),priv->space);
103     int wm=width()-moved;
104     if (m->x()>wm)
105     {
106         setToolTip (QString());
107         leaveEvent ( m );
108         return;
109     }
110     int i=0;
111     wm-=priv->space;
112     while (wm>0)
113     {
114         wm-=priv->action_size_c().width();
115         wm-=priv->space;
116
117         if (i>(priv->actions.count()-1))break;
118
119         if (m->x() >  wm)
120         {
121             setToolTip (priv->actions[i]->toolTip ());
122             return;
123         }
124         i++;
125     }
126
127     setToolTip (QString());
128     leaveEvent ( m );
129
130 }
131
132
133 void QxtHeaderView::paintSection ( QPainter * painter, const QRect & rm, int logicalIndex ) const
134 {
135     QRect rect=rm;
136
137
138     painter->save();
139     QHeaderView::paintSection(painter,rect,logicalIndex);
140     painter->restore();
141
142
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);
146
147     rect.adjust(0,0,-priv->space,0);
148     QAction * a;
149     foreach(a, priv->actions)
150     {
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
155     }
156 }
157
158 void  QxtHeaderView::mousePressEvent ( QMouseEvent * m )
159 {
160     if (!priv->action_size)return;
161
162     subClick(m,priv->action_size_c(), priv->space ) ;
163     int moved = subWidth(priv->action_size_c(),priv->space);
164
165
166     int wm=width()-moved;
167
168     if (m->x()>wm)return;
169
170     int i=0;
171     wm-=priv->space;
172     while (wm>0)
173     {
174         wm-=priv->action_size_c().width();
175         wm-=priv->space;
176
177         if (i>(priv->actions.count()-1))break;
178
179         if (m->x() >  wm)
180         {
181             priv->actions[i]->trigger();
182             break;
183         }
184
185         i++;
186     }
187
188
189 //                 v
190 //         | x | x | x |
191
192 }
193
194
195 //-----------------------------------------------------------------
196 /*!
197         reimplement this to add your own icons, widgets, or whatever to the header.\n
198  */
199
200 void QxtHeaderView::subPaint(QPainter * , const QRect & , int ,QSize , int ) const
201     {}
202 /*!
203         reimplement this to receive clicks to your own icons,widgets, etc...
204  */
205
206 void QxtHeaderView::subClick(QMouseEvent * ,QSize , int)
207 {}
208 /*!
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.
211  */
212
213 int QxtHeaderView::subWidth(QSize , int ) const
214 {
215     return 0;
216 }
217 #endif