1
/****************************************************************************
2
** Copyright (C) 2001-2006 Klarälvdalens Datakonsult AB. All rights reserved.
4
** This file is part of the KD Gantt library.
6
** This file may be used under the terms of the GNU General Public
7
** License versions 2.0 or 3.0 as published by the Free Software
8
** Foundation and appearing in the files LICENSE.GPL2 and LICENSE.GPL3
9
** included in the packaging of this file. Alternatively you may (at
10
** your option) use any later version of the GNU General Public
11
** License if such license has been publicly approved by
12
** Klarälvdalens Datakonsult AB (or its successors, if any).
14
** This file is provided "AS IS" with NO WARRANTY OF ANY KIND,
15
** INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR
16
** A PARTICULAR PURPOSE. Klarälvdalens Datakonsult AB reserves all rights
17
** not expressly granted herein.
19
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
20
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
22
**********************************************************************/
23
#include "kdganttlegend.h"
24
#include "kdganttlegend_p.h"
26
#include "kdganttitemdelegate.h"
28
#include <QApplication>
33
using namespace KDGantt;
35
/*!\class KDGantt::Legend kdganttlegend.h KDGanttLegend
37
* \brief Legend showing an image and a description for Gantt items
39
* This is an item view class showing a small Gantt item and it's
40
* text defined by LegendRole.
43
/*! Constructor. Creates a Legend with parent \a parent.
44
* The QObject parent is not used for anything internally. */
45
Legend::Legend( QWidget* parent )
46
: QAbstractItemView( parent ),
49
setItemDelegate( new ItemDelegate( this ) );
50
setFrameStyle( QFrame::NoFrame );
53
/*! Destructor. Does nothing */
61
QModelIndex Legend::indexAt( const QPoint& /*point */) const
66
QRect Legend::visualRect( const QModelIndex& /*index */) const
71
QSize Legend::sizeHint() const
73
return measureItem( rootIndex() );
76
QSize Legend::minimumSizeHint() const
78
return measureItem( rootIndex() );
81
void Legend::setModel( QAbstractItemModel* model )
83
if( this->model() != 0 )
85
disconnect( this->model(), SIGNAL( dataChanged( QModelIndex, QModelIndex ) ), this, SLOT( modelDataChanged() ) );
86
disconnect( this->model(), SIGNAL( rowsRemoved( QModelIndex, int, int ) ), this, SLOT( modelDataChanged() ) );
87
disconnect( this->model(), SIGNAL( columnsRemoved( QModelIndex, int, int ) ), this, SLOT( modelDataChanged() ) );
90
QAbstractItemView::setModel( model );
91
d->proxyModel.setSourceModel( model );
93
if( this->model() != 0 )
95
connect( this->model(), SIGNAL( dataChanged( QModelIndex, QModelIndex ) ), this, SLOT( modelDataChanged() ) );
96
connect( this->model(), SIGNAL( rowsRemoved( QModelIndex, int, int ) ), this, SLOT( modelDataChanged() ) );
97
connect( this->model(), SIGNAL( columnsRemoved( QModelIndex, int, int ) ), this, SLOT( modelDataChanged() ) );
102
/*! Triggers repainting of the legend.
104
void Legend::modelDataChanged()
107
viewport()->update();
110
void Legend::paintEvent( QPaintEvent* event )
113
// no model, no legend...
117
QPainter p( viewport() );
118
p.fillRect( viewport()->rect(), palette().color( QPalette::Window ) );
119
drawItem( &p, rootIndex() );
122
/*! Creates a StyleOptionGanttItem with all style options filled in
123
* except the target rectangles.
125
StyleOptionGanttItem Legend::getStyleOption( const QModelIndex& index ) const
127
StyleOptionGanttItem opt;
128
opt.displayPosition = StyleOptionGanttItem::Right;
129
opt.displayAlignment = Qt::Alignment( d->proxyModel.data( index, Qt::TextAlignmentRole ).toInt() );
130
opt.text = index.model()->data( index, LegendRole ).toString();
131
opt.font = qVariantValue< QFont >( index.model()->data( index, Qt::FontRole ) );
135
/*! Draws the legend item at \a index and all of it's children recursively
136
* at \a pos onto \a painter.
137
* Reimplement this if you want to draw items in an user defined way.
138
* \returns the rectangle drawn.
140
QRect Legend::drawItem( QPainter* painter, const QModelIndex& index, const QPoint& pos ) const
145
if( index.isValid() && index.model() == &d->proxyModel )
147
ItemDelegate* const delegate = qobject_cast< ItemDelegate* >( itemDelegate( index ) );
148
assert( delegate != 0 );
149
const QRect r( pos, measureItem( index, false ) );
150
StyleOptionGanttItem opt = getStyleOption( index );
152
opt.rect.setWidth( r.height() );
153
opt.itemRect = opt.rect;
154
opt.boundingRect = r;
155
opt.boundingRect.setWidth( r.width() + r.height() );
156
if( !opt.text.isNull() )
157
delegate->paintGanttItem( painter, opt, index );
164
const int rowCount = d->proxyModel.rowCount( index );
165
for( int row = 0; row < rowCount; ++row )
167
const QRect r = drawItem( painter, d->proxyModel.index( row, 0, index ), QPoint( pos.x(), yPos ) );
168
xPos = qMax( xPos, r.right() );
169
yPos = qMax( yPos, r.bottom() );
172
return QRect( pos, QPoint( xPos, yPos ) );
175
/*! Calculates the needed space for the legend item at \a index and, if \a recursive is true,
178
QSize Legend::measureItem( const QModelIndex& index, bool recursive ) const
184
if( index.model() != 0 )
186
QFontMetrics fm( qVariantValue< QFont >( index.model()->data( index, Qt::FontRole ) ) );
187
const QString text = index.model()->data( index, LegendRole ).toString();
189
baseSize += QSize( fm.width( text ) + fm.height() + 2, fm.height() + 2 );
197
const int rowCount = d->proxyModel.rowCount( index );
198
for( int row = 0; row < rowCount; ++row )
200
const QSize childSize = measureItem( d->proxyModel.index( row, 0, index ) );
201
childrenSize.setWidth( qMax( childrenSize.width(), childSize.width() ) );
202
childrenSize.rheight() += childSize.height();
204
return baseSize + childrenSize;