• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

kdgantt2

  • sources
  • kde-4.12
  • kdepim
  • kdgantt2
kdganttlegend.cpp
Go to the documentation of this file.
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 distributed and/or modified under the terms of the
7  ** GNU General Public License version 2 as published by the Free Software
8  ** Foundation and appearing in the file LICENSE.GPL included in the
9  ** packaging of this file.
10  **
11  ** Licensees holding valid commercial KD Gantt licenses may use this file in
12  ** accordance with the KD Gantt Commercial License Agreement provided with
13  ** the Software.
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  ** See http://www.kdab.net/kdgantt for
19  ** information about KD Gantt Commercial License Agreements.
20  **
21  ** Contact info@kdab.net if any conditions of this
22  ** licensing are not clear to you.
23  **
24  **********************************************************************/
25 #include "kdganttlegend.h"
26 #include "kdganttlegend_p.h"
27 
28 #include "kdganttitemdelegate.h"
29 
30 #include <QApplication>
31 #include <QPainter>
32 
33 #include <cassert>
34 
35 using namespace KDGantt;
36 
47 Legend::Legend( QWidget* parent )
48  : QAbstractItemView( parent ),
49  _d( new Private )
50 {
51  setItemDelegate( new ItemDelegate( this ) );
52  setFrameStyle( QFrame::NoFrame );
53 }
54 
56 Legend::~Legend()
57 {
58  delete _d;
59 }
60 
61 #define d d_func()
62 
63 QModelIndex Legend::indexAt( const QPoint& point ) const
64 {
65  Q_UNUSED( point );
66  return QModelIndex();
67 }
68 
69 QRect Legend::visualRect( const QModelIndex& index ) const
70 {
71  Q_UNUSED( index );
72  return QRect();
73 }
74 
75 QSize Legend::sizeHint() const
76 {
77  return measureItem( rootIndex() );
78 }
79 
80 QSize Legend::minimumSizeHint() const
81 {
82  return measureItem( rootIndex() );
83 }
84 
85 void Legend::setModel( QAbstractItemModel* model )
86 {
87  if( this->model() != 0 )
88  {
89  disconnect( this->model(), SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(modelDataChanged()) );
90  disconnect( this->model(), SIGNAL(rowsRemoved(QModelIndex,int,int)), this, SLOT(modelDataChanged()) );
91  disconnect( this->model(), SIGNAL(columnsRemoved(QModelIndex,int,int)), this, SLOT(modelDataChanged()) );
92  }
93 
94  QAbstractItemView::setModel( model );
95  d->proxyModel.setSourceModel( model );
96 
97  if( this->model() != 0 )
98  {
99  connect( this->model(), SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(modelDataChanged()) );
100  connect( this->model(), SIGNAL(rowsRemoved(QModelIndex,int,int)), this, SLOT(modelDataChanged()) );
101  connect( this->model(), SIGNAL(columnsRemoved(QModelIndex,int,int)), this, SLOT(modelDataChanged()) );
102  }
103 
104 }
105 
108 void Legend::modelDataChanged()
109 {
110  updateGeometry();
111  viewport()->update();
112 }
113 
114 void Legend::paintEvent( QPaintEvent* event )
115 {
116  Q_UNUSED( event );
117  // no model, no legend...
118  if( model() == 0 )
119  return;
120 
121  QPainter p( viewport() );
122  p.fillRect( viewport()->rect(), palette().color( QPalette::Window ) );
123  drawItem( &p, rootIndex() );
124 }
125 
129 StyleOptionGanttItem Legend::getStyleOption( const QModelIndex& index ) const
130 {
131  StyleOptionGanttItem opt;
132  opt.displayPosition = StyleOptionGanttItem::Right;
133  opt.displayAlignment = Qt::Alignment( d->proxyModel.data( index, Qt::TextAlignmentRole ).toInt() );
134  opt.text = index.model()->data( index, LegendRole ).toString();
135  opt.font = qVariantValue< QFont >( index.model()->data( index, Qt::FontRole ) );
136  return opt;
137 }
138 
144 QRect Legend::drawItem( QPainter* painter, const QModelIndex& index, const QPoint& pos ) const
145 {
146  int xPos = pos.x();
147  int yPos = pos.y();
148 
149  if( index.isValid() && index.model() == &d->proxyModel )
150  {
151  ItemDelegate* const delegate = qobject_cast< ItemDelegate* >( itemDelegate( index ) );
152  assert( delegate != 0 );
153  const QRect r( pos, measureItem( index, false ) );
154  StyleOptionGanttItem opt = getStyleOption( index );
155  opt.rect = r;
156  opt.rect.setWidth( r.height() );
157 
158  const ItemType typ = static_cast<ItemType>( index.model()->data( index, ItemTypeRole ).toInt() );
159  const int dx = (typ == TypeEvent) ? (r.height() / 2) : 0;
160 
161  opt.itemRect = opt.rect.adjusted(dx,0,dx,0);
162  opt.boundingRect = r;
163  opt.boundingRect.setWidth( r.width() + r.height() );
164  if( !opt.text.isNull() )
165  delegate->paintGanttItem( painter, opt, index );
166 
167  xPos = r.right();
168  yPos = r.bottom();
169  }
170 
171 
172  const int rowCount = d->proxyModel.rowCount( index );
173  for( int row = 0; row < rowCount; ++row )
174  {
175  const QRect r = drawItem( painter, d->proxyModel.index( row, 0, index ), QPoint( pos.x(), yPos ) );
176  xPos = qMax( xPos, r.right() );
177  yPos = qMax( yPos, r.bottom() );
178  }
179 
180  return QRect( pos, QPoint( xPos, yPos ) );
181 }
182 
186 QSize Legend::measureItem( const QModelIndex& index, bool recursive ) const
187 {
188  if( model() == 0 )
189  return QSize();
190 
191  QSize baseSize;
192  if( index.model() != 0 )
193  {
194  QFontMetrics fm( qVariantValue< QFont >( index.model()->data( index, Qt::FontRole ) ) );
195  const QString text = index.model()->data( index, LegendRole ).toString();
196  if( !text.isEmpty() )
197  baseSize += QSize( fm.width( text ) + fm.height() + 2, fm.height() + 2 );
198  }
199 
200  if( !recursive )
201  return baseSize;
202 
203  QSize childrenSize;
204 
205  const int rowCount = d->proxyModel.rowCount( index );
206  for( int row = 0; row < rowCount; ++row )
207  {
208  const QSize childSize = measureItem( d->proxyModel.index( row, 0, index ) );
209  childrenSize.setWidth( qMax( childrenSize.width(), childSize.width() ) );
210  childrenSize.rheight() += childSize.height();
211  }
212  return baseSize + childrenSize;
213 }
KDGantt::StyleOptionGanttItem::text
QString text
Definition: kdganttstyleoptionganttitem.h:48
KDGantt::ItemDelegate
Class used to render gantt items in a KDGantt::GraphicsView.
Definition: kdganttitemdelegate.h:39
KDGantt::StyleOptionGanttItem::displayPosition
Position displayPosition
Definition: kdganttstyleoptionganttitem.h:46
KDGantt::Legend::~Legend
virtual ~Legend()
Definition: kdganttlegend.cpp:56
kdganttlegend.h
KDGantt::TypeEvent
Definition: kdganttglobal.h:213
QWidget
KDGantt::ItemType
ItemType
Definition: kdganttglobal.h:211
KDGantt::Legend::drawItem
virtual QRect drawItem(QPainter *painter, const QModelIndex &index, const QPoint &pos=QPoint()) const
Definition: kdganttlegend.cpp:144
KDGantt::Legend::indexAt
QModelIndex indexAt(const QPoint &point) const
Definition: kdganttlegend.cpp:63
KDGantt::ItemTypeRole
Definition: kdganttglobal.h:207
KDGantt::Legend::Private
Definition: kdganttlegend_p.h:33
KDGantt::Legend::Legend
Legend(QWidget *parent=0)
Definition: kdganttlegend.cpp:47
KDGantt::Legend::paintEvent
void paintEvent(QPaintEvent *event)
Definition: kdganttlegend.cpp:114
KDGantt::LegendRole
Definition: kdganttglobal.h:208
kdganttitemdelegate.h
kdganttlegend_p.h
KDGantt::Legend::minimumSizeHint
QSize minimumSizeHint() const
Definition: kdganttlegend.cpp:80
QAbstractItemView
KDGantt::Legend::modelDataChanged
virtual void modelDataChanged()
Definition: kdganttlegend.cpp:108
KDGantt::Legend::getStyleOption
virtual StyleOptionGanttItem getStyleOption(const QModelIndex &index) const
Definition: kdganttlegend.cpp:129
KDGantt::Legend::sizeHint
QSize sizeHint() const
Definition: kdganttlegend.cpp:75
KDGantt::Legend::measureItem
virtual QSize measureItem(const QModelIndex &index, bool recursive=true) const
Definition: kdganttlegend.cpp:186
KDGantt::StyleOptionGanttItem::itemRect
QRectF itemRect
Definition: kdganttstyleoptionganttitem.h:45
KDGantt::StyleOptionGanttItem
QStyleOption subclass for gantt items.
Definition: kdganttstyleoptionganttitem.h:36
KDGantt::Legend::visualRect
QRect visualRect(const QModelIndex &index) const
Definition: kdganttlegend.cpp:69
d
#define d
Definition: kdganttlegend.cpp:61
KDGantt::StyleOptionGanttItem::Right
Definition: kdganttstyleoptionganttitem.h:38
KDGantt::ItemDelegate::paintGanttItem
virtual void paintGanttItem(QPainter *p, const StyleOptionGanttItem &opt, const QModelIndex &idx)
Definition: kdganttitemdelegate.cpp:225
KDGantt::StyleOptionGanttItem::boundingRect
QRectF boundingRect
Definition: kdganttstyleoptionganttitem.h:44
KDGantt::Legend::setModel
void setModel(QAbstractItemModel *model)
Definition: kdganttlegend.cpp:85
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:55:05 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kdgantt2

Skip menu "kdgantt2"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal