~ubuntu-branches/ubuntu/raring/calligra/raring-proposed

« back to all changes in this revision

Viewing changes to 3rdparty/kdgantt/kdganttlegend.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2013-01-15 17:26:10 UTC
  • mfrom: (1.1.17)
  • Revision ID: package-import@ubuntu.com-20130115172610-b193s9546hyssmym
Tags: 1:2.5.94-0ubuntu1
New upstream RC release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
 ** Copyright (C) 2001-2006 Klarälvdalens Datakonsult AB.  All rights reserved.
 
3
 **
 
4
 ** This file is part of the KD Gantt library.
 
5
 **
 
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).
 
13
 **
 
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.
 
18
 **
 
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.
 
21
 **
 
22
 **********************************************************************/
 
23
#include "kdganttlegend.h"
 
24
#include "kdganttlegend_p.h"
 
25
 
 
26
#include "kdganttitemdelegate.h"
 
27
 
 
28
#include <QApplication>
 
29
#include <QPainter>
 
30
 
 
31
#include <cassert>
 
32
 
 
33
using namespace KDGantt;
 
34
 
 
35
/*!\class KDGantt::Legend kdganttlegend.h KDGanttLegend
 
36
 * \ingroup KDGantt
 
37
 * \brief Legend showing an image and a description for Gantt items
 
38
 *
 
39
 * This is an item view class showing a small Gantt item and it's
 
40
 * text defined by LegendRole.
 
41
 */
 
42
 
 
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 ),
 
47
      _d( new Private )
 
48
{
 
49
    setItemDelegate( new ItemDelegate( this ) );
 
50
    setFrameStyle( QFrame::NoFrame );
 
51
}
 
52
 
 
53
/*! Destructor. Does nothing */
 
54
Legend::~Legend()
 
55
{
 
56
    delete _d;
 
57
}
 
58
 
 
59
#define d d_func()
 
60
 
 
61
QModelIndex Legend::indexAt( const QPoint& /*point */) const
 
62
{
 
63
    return QModelIndex();
 
64
}
 
65
 
 
66
QRect Legend::visualRect( const QModelIndex& /*index */) const
 
67
{
 
68
    return QRect();
 
69
}
 
70
 
 
71
QSize Legend::sizeHint() const
 
72
{
 
73
    return measureItem( rootIndex() );
 
74
}
 
75
 
 
76
QSize Legend::minimumSizeHint() const
 
77
{
 
78
    return measureItem( rootIndex() );
 
79
}
 
80
 
 
81
void Legend::setModel( QAbstractItemModel* model )
 
82
{
 
83
    if( this->model() != 0 )
 
84
    {
 
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() ) );
 
88
    }
 
89
 
 
90
    QAbstractItemView::setModel( model );
 
91
    d->proxyModel.setSourceModel( model );
 
92
 
 
93
    if( this->model() != 0 )
 
94
    {
 
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() ) );
 
98
    }
 
99
 
 
100
}
 
101
 
 
102
/*! Triggers repainting of the legend.
 
103
 */
 
104
void Legend::modelDataChanged()
 
105
{
 
106
    updateGeometry();
 
107
    viewport()->update();
 
108
}
 
109
 
 
110
void Legend::paintEvent( QPaintEvent* event )
 
111
{
 
112
    Q_UNUSED( event );
 
113
    // no model, no legend...
 
114
    if( model() == 0 )
 
115
        return;
 
116
 
 
117
    QPainter p( viewport() );
 
118
    p.fillRect( viewport()->rect(), palette().color( QPalette::Window ) );
 
119
    drawItem( &p, rootIndex() );
 
120
}
 
121
 
 
122
/*! Creates a StyleOptionGanttItem with all style options filled in
 
123
 *  except the target rectangles.
 
124
 */
 
125
StyleOptionGanttItem Legend::getStyleOption( const QModelIndex& index ) const
 
126
{
 
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 ) );
 
132
    return opt;
 
133
}
 
134
 
 
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.
 
139
 */
 
140
QRect Legend::drawItem( QPainter* painter, const QModelIndex& index, const QPoint& pos ) const
 
141
{
 
142
    int xPos = pos.x();
 
143
    int yPos = pos.y();
 
144
 
 
145
    if( index.isValid() && index.model() == &d->proxyModel )
 
146
    {
 
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 );
 
151
        opt.rect = r;
 
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 );
 
158
 
 
159
        xPos = r.right();
 
160
        yPos = r.bottom();
 
161
    }
 
162
 
 
163
 
 
164
    const int rowCount = d->proxyModel.rowCount( index );
 
165
    for( int row = 0; row < rowCount; ++row )
 
166
    {
 
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() );
 
170
    }
 
171
 
 
172
    return QRect( pos, QPoint( xPos, yPos ) );
 
173
}
 
174
 
 
175
/*! Calculates the needed space for the legend item at \a index and, if \a recursive is true,
 
176
 *  all child items.
 
177
 */
 
178
QSize Legend::measureItem( const QModelIndex& index, bool recursive ) const
 
179
{
 
180
    if( model() == 0 )
 
181
        return QSize();
 
182
 
 
183
    QSize baseSize;
 
184
    if( index.model() != 0 )
 
185
    {
 
186
        QFontMetrics fm( qVariantValue< QFont >( index.model()->data( index, Qt::FontRole ) ) );
 
187
        const QString text = index.model()->data( index, LegendRole ).toString();
 
188
        if( !text.isNull() )
 
189
            baseSize += QSize( fm.width( text ) + fm.height() + 2, fm.height() + 2 );
 
190
    }
 
191
 
 
192
    if( !recursive )
 
193
        return baseSize;
 
194
 
 
195
    QSize childrenSize;
 
196
 
 
197
    const int rowCount = d->proxyModel.rowCount( index );
 
198
    for( int row = 0; row < rowCount; ++row )
 
199
    {
 
200
        const QSize childSize = measureItem( d->proxyModel.index( row, 0, index ) );
 
201
        childrenSize.setWidth( qMax( childrenSize.width(), childSize.width() ) );
 
202
        childrenSize.rheight() += childSize.height();
 
203
    }
 
204
    return baseSize + childrenSize;
 
205
}