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
19using namespace KGantt;
20
21
22Legend::Legend( QWidget* parent )
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
38QModelIndex Legend::indexAt( const QPoint& point ) const
39{
40 Q_UNUSED( point );
41 return QModelIndex();
42}
43
44QRect Legend::visualRect( const QModelIndex& index ) const
45{
46 Q_UNUSED( index );
47 return QRect();
48}
49
50QSize Legend::sizeHint() const
51{
52 return measureItem( rootIndex() );
53}
54
55QSize Legend::minimumSizeHint() const
56{
57 return measureItem( rootIndex() );
58}
59
60void 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
87
88void 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
112QRect 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 ) );
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
152QSize Legend::measureItem( const QModelIndex& index, bool recursive ) const
153{
154 if ( model() == nullptr )
155 return QSize();
156
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}
Class used to render gantt items in a KGantt::GraphicsView.
virtual void paintGanttItem(QPainter *p, const StyleOptionGanttItem &opt, const QModelIndex &idx)
virtual void modelDataChanged()
~Legend() override
virtual StyleOptionGanttItem getStyleOption(const QModelIndex &index) const
virtual QSize measureItem(const QModelIndex &index, bool recursive=true) const
virtual QRect drawItem(QPainter *painter, const QModelIndex &index, const QPoint &pos=QPoint()) const
QStyleOption subclass for gantt items.
Global namespace.
@ ItemTypeRole
The item type.
@ LegendRole
The Legend text.
virtual QVariant data(const QModelIndex &index, int role) const const=0
virtual void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QList< int > &roles)
virtual bool event(QEvent *event) override
QAbstractItemDelegate * itemDelegate() const const
QAbstractItemModel * model() const const
QModelIndex rootIndex() const const
void setItemDelegate(QAbstractItemDelegate *delegate)
virtual void setModel(QAbstractItemModel *model)
QWidget * viewport() const const
QRect boundingRect(QChar ch) const const
int height() const const
void setFrameStyle(int style)
bool isValid() const const
const QAbstractItemModel * model() const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
bool disconnect(const QMetaObject::Connection &connection)
T qobject_cast(QObject *object)
int bottom() const const
int height() const const
int right() const const
int width() const const
int x() const const
void setWidth(qreal width)
int height() const const
int & rheight()
void setWidth(int width)
int width() const const
bool isEmpty() const const
bool isNull() const const
typedef Alignment
TextAlignmentRole
QString toString() const const
void update()
void updateGeometry()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Sep 27 2024 11:49:41 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.