KGantt

kganttlegend.cpp
1 /*
2  * SPDX-FileCopyrightText: 2001-2015 Klaralvdalens Datakonsult AB. All rights reserved.
3  *
4  * This file is part of the KGantt library.
5  *
6  * SPDX-License-Identifier: GPL-2.0-or-later
7  */
8 
9 #include "kganttlegend.h"
10 #include "kganttlegend_p.h"
11 
12 #include "kganttitemdelegate.h"
13 
14 #include <QApplication>
15 #include <QPainter>
16 
17 #include <cassert>
18 
19 using namespace KGantt;
20 
21 
23  : QAbstractItemView( parent ),
24  _d( new Private )
25 {
26  setItemDelegate( new ItemDelegate( this ) );
28 }
29 
30 
32 {
33  delete _d;
34 }
35 
36 #define d d_func()
37 
38 QModelIndex Legend::indexAt( const QPoint& point ) const
39 {
40  Q_UNUSED( point );
41  return QModelIndex();
42 }
43 
44 QRect Legend::visualRect( const QModelIndex& index ) const
45 {
46  Q_UNUSED( index );
47  return QRect();
48 }
49 
50 QSize Legend::sizeHint() const
51 {
52  return measureItem( rootIndex() );
53 }
54 
55 QSize Legend::minimumSizeHint() const
56 {
57  return measureItem( rootIndex() );
58 }
59 
60 void Legend::setModel( QAbstractItemModel* model )
61 {
62  if ( this->model() != nullptr )
63  {
64  disconnect( this->model(), SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(modelDataChanged()) );
65  disconnect( this->model(), SIGNAL(rowsRemoved(QModelIndex,int,int)), this, SLOT(modelDataChanged()) );
66  disconnect( this->model(), SIGNAL(columnsRemoved(QModelIndex,int,int)), this, SLOT(modelDataChanged()) );
67  }
68 
70  d->proxyModel.setSourceModel( model );
71 
72  if ( this->model() != nullptr )
73  {
74  connect( this->model(), SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(modelDataChanged()) );
75  connect( this->model(), SIGNAL(rowsRemoved(QModelIndex,int,int)), this, SLOT(modelDataChanged()) );
76  connect( this->model(), SIGNAL(columnsRemoved(QModelIndex,int,int)), this, SLOT(modelDataChanged()) );
77  }
78 
79 }
80 
81 
83 {
85  viewport()->update();
86 }
87 
88 void Legend::paintEvent( QPaintEvent* event )
89 {
90  Q_UNUSED( event );
91  // no model, no legend...
92  if ( model() == nullptr )
93  return;
94 
95  QPainter p( viewport() );
96  p.fillRect( viewport()->rect(), palette().color( QPalette::Window ) );
97  drawItem( &p, rootIndex() );
98 }
99 
100 
102 {
104  opt.displayPosition = StyleOptionGanttItem::Right;
105  opt.displayAlignment = Qt::Alignment( d->proxyModel.data( index, Qt::TextAlignmentRole ).toInt() );
106  opt.text = index.model()->data( index, LegendRole ).toString();
107  opt.font = ( index.model()->data( index, Qt::FontRole ) ).value< QFont >();
108  return opt;
109 }
110 
111 
112 QRect Legend::drawItem( QPainter* painter, const QModelIndex& index, const QPoint& pos ) const
113 {
114  int xPos = pos.x();
115  int yPos = pos.y();
116 
117  if ( index.isValid() && index.model() == &d->proxyModel )
118  {
119  ItemDelegate* const delegate = qobject_cast< ItemDelegate* >( itemDelegate( index ) );
120  assert( delegate != nullptr );
121  const QRect r( pos, measureItem( index, false ) );
122  StyleOptionGanttItem opt = getStyleOption( index );
123  opt.rect = r;
124  opt.rect.setWidth( r.height() );
125 
126  const ItemType typ = static_cast<ItemType>( index.model()->data( index, ItemTypeRole ).toInt() );
127  const int dx = (typ == TypeEvent) ? (r.height() / 2) : 0;
128 
129  opt.itemRect = opt.rect.adjusted(dx,0,dx,0);
130  opt.boundingRect = r;
131  opt.boundingRect.setWidth( r.width() + r.height() );
132  if ( !opt.text.isNull() )
133  delegate->paintGanttItem( painter, opt, index );
134 
135  xPos = r.right();
136  yPos = r.bottom();
137  }
138 
139 
140  const int rowCount = d->proxyModel.rowCount( index );
141  for ( int row = 0; row < rowCount; ++row )
142  {
143  const QRect r = drawItem( painter, d->proxyModel.index( row, 0, index ), QPoint( pos.x(), yPos ) );
144  xPos = qMax( xPos, r.right() );
145  yPos = qMax( yPos, r.bottom() );
146  }
147 
148  return QRect( pos, QPoint( xPos, yPos ) );
149 }
150 
151 
152 QSize Legend::measureItem( const QModelIndex& index, bool recursive ) const
153 {
154  if ( model() == nullptr )
155  return QSize();
156 
157  QSize baseSize;
158  if ( index.model() != nullptr )
159  {
160  QFontMetrics fm( ( index.model()->data( index, Qt::FontRole ) ).value< QFont >() );
161  const QString text = index.model()->data( index, LegendRole ).toString();
162  if ( !text.isEmpty() )
163  baseSize += QSize( fm.boundingRect( text ).width() + fm.height() + 2, fm.height() + 2 );
164  }
165 
166  if ( !recursive )
167  return baseSize;
168 
169  QSize childrenSize;
170 
171  const int rowCount = d->proxyModel.rowCount( index );
172  for ( int row = 0; row < rowCount; ++row )
173  {
174  const QSize childSize = measureItem( d->proxyModel.index( row, 0, index ) );
175  childrenSize.setWidth( qMax( childrenSize.width(), childSize.width() ) );
176  childrenSize.rheight() += childSize.height();
177  }
178  return baseSize + childrenSize;
179 }
The item type.
Definition: kganttglobal.h:217
const QPalette & palette() const const
int width() const const
void fillRect(const QRectF &rectangle, const QBrush &brush)
void updateGeometry()
int right() const const
The Legend text.
Definition: kganttglobal.h:218
QStyleOption subclass for gantt items.
virtual void setModel(QAbstractItemModel *model)
QWidget * viewport() const const
int height() const const
void setFrameStyle(int style)
virtual QRect drawItem(QPainter *painter, const QModelIndex &index, const QPoint &pos=QPoint()) const
QSize baseSize() const const
bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *method)
void update()
typedef Alignment
virtual void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector< int > &roles)
int x() const const
int y() const const
int & rheight()
bool isNull() const const
virtual StyleOptionGanttItem getStyleOption(const QModelIndex &index) const
bool isValid() const const
int toInt(bool *ok) const const
void setWidth(int width)
TextAlignmentRole
void setItemDelegate(QAbstractItemDelegate *delegate)
QAbstractItemDelegate * itemDelegate() const const
QPoint pos() const const
virtual QVariant data(const QModelIndex &index, int role) const const =0
Global namespace.
QRect rect() const const
virtual void paintGanttItem(QPainter *p, const StyleOptionGanttItem &opt, const QModelIndex &idx)
QModelIndex rootIndex() const const
virtual void modelDataChanged()
Legend(QWidget *parent=nullptr)
virtual bool event(QEvent *event) override
int width() const const
const QAbstractItemModel * model() const const
void setWidth(qreal width)
int height() const const
int bottom() const const
virtual QSize measureItem(const QModelIndex &index, bool recursive=true) const
QAbstractItemModel * model() const const
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
T qobject_cast(QObject *object)
QString toString() const const
Class used to render gantt items in a KGantt::GraphicsView.
~Legend() override
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sun Dec 5 2021 22:32:42 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.