KD Chart 2  [rev.2.6]
kdganttlegend.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 ** Copyright (C) 2001-2019 Klaralvdalens Datakonsult AB. All rights reserved.
3 **
4 ** This file is part of the KD Chart library.
5 **
6 ** Licensees holding valid commercial KD Chart licenses may use this file in
7 ** accordance with the KD Chart Commercial License Agreement provided with
8 ** the Software.
9 **
10 **
11 ** This file may be distributed and/or modified under the terms of the
12 ** GNU General Public License version 2 and version 3 as published by the
13 ** Free Software Foundation and appearing in the file LICENSE.GPL.txt included.
14 **
15 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
17 **
18 ** Contact info@kdab.com if any conditions of this licensing are not
19 ** clear to you.
20 **
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 
46  : QAbstractItemView( parent ),
47  _d( new Private )
48 {
49  setItemDelegate( new ItemDelegate( this ) );
50  setFrameStyle( QFrame::NoFrame );
51 }
52 
55 {
56  delete _d;
57 }
58 
59 #define d d_func()
60 
61 QModelIndex Legend::indexAt( const QPoint& point ) const
62 {
63  Q_UNUSED( point );
64  return QModelIndex();
65 }
66 
67 QRect Legend::visualRect( const QModelIndex& index ) const
68 {
69  Q_UNUSED( index );
70  return QRect();
71 }
72 
73 QSize Legend::sizeHint() const
74 {
75  return measureItem( rootIndex() );
76 }
77 
79 {
80  return measureItem( rootIndex() );
81 }
82 
83 void Legend::setModel( QAbstractItemModel* model )
84 {
85  if ( this->model() != 0 )
86  {
87  disconnect( this->model(), SIGNAL( dataChanged( QModelIndex, QModelIndex ) ), this, SLOT( modelDataChanged() ) );
88  disconnect( this->model(), SIGNAL( rowsRemoved( QModelIndex, int, int ) ), this, SLOT( modelDataChanged() ) );
89  disconnect( this->model(), SIGNAL( columnsRemoved( QModelIndex, int, int ) ), this, SLOT( modelDataChanged() ) );
90  }
91 
92  QAbstractItemView::setModel( model );
93  d->proxyModel.setSourceModel( model );
94 
95  if ( this->model() != 0 )
96  {
97  connect( this->model(), SIGNAL( dataChanged( QModelIndex, QModelIndex ) ), this, SLOT( modelDataChanged() ) );
98  connect( this->model(), SIGNAL( rowsRemoved( QModelIndex, int, int ) ), this, SLOT( modelDataChanged() ) );
99  connect( this->model(), SIGNAL( columnsRemoved( QModelIndex, int, int ) ), this, SLOT( modelDataChanged() ) );
100  }
101 
102 }
103 
107 {
108  updateGeometry();
109  viewport()->update();
110 }
111 
112 void Legend::paintEvent( QPaintEvent* event )
113 {
114  Q_UNUSED( event );
115  // no model, no legend...
116  if ( model() == 0 )
117  return;
118 
119  QPainter p( viewport() );
120  p.fillRect( viewport()->rect(), palette().color( QPalette::Window ) );
121  drawItem( &p, rootIndex() );
122 }
123 
127 StyleOptionGanttItem Legend::getStyleOption( const QModelIndex& index ) const
128 {
131  opt.displayAlignment = Qt::Alignment( d->proxyModel.data( index, Qt::TextAlignmentRole ).toInt() );
132  opt.text = index.model()->data( index, LegendRole ).toString();
133  opt.font = ( index.model()->data( index, Qt::FontRole ) ).value< QFont >();
134  return opt;
135 }
136 
142 QRect Legend::drawItem( QPainter* painter, const QModelIndex& index, const QPoint& pos ) const
143 {
144  int xPos = pos.x();
145  int yPos = pos.y();
146 
147  if ( index.isValid() && index.model() == &d->proxyModel )
148  {
149  ItemDelegate* const delegate = qobject_cast< ItemDelegate* >( itemDelegate( index ) );
150  assert( delegate != 0 );
151  const QRect r( pos, measureItem( index, false ) );
152  StyleOptionGanttItem opt = getStyleOption( index );
153  opt.rect = r;
154  opt.rect.setWidth( r.height() );
155 
156  const ItemType typ = static_cast<ItemType>( index.model()->data( index, ItemTypeRole ).toInt() );
157  const int dx = (typ == TypeEvent) ? (r.height() / 2) : 0;
158 
159  opt.itemRect = opt.rect.adjusted(dx,0,dx,0);
160  opt.boundingRect = r;
161  opt.boundingRect.setWidth( r.width() + r.height() );
162  if ( !opt.text.isNull() )
163  delegate->paintGanttItem( painter, opt, index );
164 
165  xPos = r.right();
166  yPos = r.bottom();
167  }
168 
169 
170  const int rowCount = d->proxyModel.rowCount( index );
171  for ( int row = 0; row < rowCount; ++row )
172  {
173  const QRect r = drawItem( painter, d->proxyModel.index( row, 0, index ), QPoint( pos.x(), yPos ) );
174  xPos = qMax( xPos, r.right() );
175  yPos = qMax( yPos, r.bottom() );
176  }
177 
178  return QRect( pos, QPoint( xPos, yPos ) );
179 }
180 
184 QSize Legend::measureItem( const QModelIndex& index, bool recursive ) const
185 {
186  if ( model() == 0 )
187  return QSize();
188 
189  QSize baseSize;
190  if ( index.model() != 0 )
191  {
192  QFontMetrics fm( ( index.model()->data( index, Qt::FontRole ) ).value< QFont >() );
193  const QString text = index.model()->data( index, LegendRole ).toString();
194  if ( !text.isEmpty() )
195  baseSize += QSize( fm.width( text ) + fm.height() + 2, fm.height() + 2 );
196  }
197 
198  if ( !recursive )
199  return baseSize;
200 
201  QSize childrenSize;
202 
203  const int rowCount = d->proxyModel.rowCount( index );
204  for ( int row = 0; row < rowCount; ++row )
205  {
206  const QSize childSize = measureItem( d->proxyModel.index( row, 0, index ) );
207  childrenSize.setWidth( qMax( childrenSize.width(), childSize.width() ) );
208  childrenSize.rheight() += childSize.height();
209  }
210  return baseSize + childrenSize;
211 }
QSize sizeHint() const
QModelIndex indexAt(const QPoint &point) const
QRect visualRect(const QModelIndex &index) const
Class used to render gantt items in a KDGantt::GraphicsView.
QSize minimumSizeHint() const
Class only listed here to document inheritance of some KDChart classes.
virtual StyleOptionGanttItem getStyleOption(const QModelIndex &index) const
virtual void paintGanttItem(QPainter *p, const StyleOptionGanttItem &opt, const QModelIndex &idx)
Legend(QWidget *parent=0)
QStyleOption subclass for gantt items.
virtual void modelDataChanged()
virtual QSize measureItem(const QModelIndex &index, bool recursive=true) const
void paintEvent(QPaintEvent *event)
void setModel(QAbstractItemModel *model)
#define d
Class only listed here to document inheritance of some KDChart classes.
virtual QRect drawItem(QPainter *painter, const QModelIndex &index, const QPoint &pos=QPoint()) const

Klarälvdalens Datakonsult AB (KDAB)
Qt-related services and products
https://www.kdab.com/
https://www.kdab.com/products/kd-chart/