• 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
kdganttitemdelegate.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 "kdganttitemdelegate_p.h"
26 #include "kdganttglobal.h"
27 #include "kdganttstyleoptionganttitem.h"
28 
29 #include <QPainter>
30 #include <QPainterPath>
31 #include <QPen>
32 #include <QModelIndex>
33 #include <QAbstractItemModel>
34 #include <QApplication>
35 
36 #ifndef QT_NO_DEBUG_STREAM
37 
38 #define PRINT_INTERACTIONSTATE(x) \
39  case x: dbg << #x; break;
40 
41 
42 QDebug operator<<( QDebug dbg, KDGantt::ItemDelegate::InteractionState state )
43 {
44  switch( state ) {
45  PRINT_INTERACTIONSTATE( KDGantt::ItemDelegate::State_None );
46  PRINT_INTERACTIONSTATE( KDGantt::ItemDelegate::State_Move );
47  PRINT_INTERACTIONSTATE( KDGantt::ItemDelegate::State_ExtendLeft );
48  PRINT_INTERACTIONSTATE( KDGantt::ItemDelegate::State_ExtendRight );
49  default:
50  break;
51  }
52  return dbg;
53 }
54 
55 #undef PRINT_INTERACTIONSTATE
56 
57 #endif /* QT_NO_DEBUG_STREAM */
58 
59 using namespace KDGantt;
60 
74 ItemDelegate::Private::Private()
75 {
76  // Brushes
77  QLinearGradient taskgrad( 0., 0., 0., QApplication::fontMetrics().height() );
78  taskgrad.setColorAt( 0., Qt::green );
79  taskgrad.setColorAt( 1., Qt::darkGreen );
80 
81  QLinearGradient summarygrad( 0., 0., 0., QApplication::fontMetrics().height() );
82  summarygrad.setColorAt( 0., Qt::blue );
83  summarygrad.setColorAt( 1., Qt::darkBlue );
84 
85  QLinearGradient eventgrad( 0., 0., 0., QApplication::fontMetrics().height() );
86  eventgrad.setColorAt( 0., Qt::red );
87  eventgrad.setColorAt( 1., Qt::darkRed );
88 
89  defaultbrush[TypeTask] = taskgrad;
90  defaultbrush[TypeSummary] = summarygrad;
91  defaultbrush[TypeEvent] = eventgrad;
92 
93  // Pens
94  QPen pen( Qt::black, 1. );
95 
96  defaultpen[TypeTask] = pen;
97  defaultpen[TypeSummary] = pen;
98  defaultpen[TypeEvent] = pen;
99 }
100 
102 ItemDelegate::ItemDelegate( QObject* parent )
103  : QItemDelegate( parent ), _d( new Private )
104 {
105 }
106 
108 ItemDelegate::~ItemDelegate()
109 {
110  delete _d;
111 }
112 
113 #define d d_func()
114 
121 void ItemDelegate::setDefaultBrush( ItemType type, const QBrush& brush )
122 {
123  d->defaultbrush[type] = brush;
124 }
125 
130 QBrush ItemDelegate::defaultBrush( ItemType type ) const
131 {
132  return d->defaultbrush[type];
133 }
134 
141 void ItemDelegate::setDefaultPen( ItemType type, const QPen& pen )
142 {
143  d->defaultpen[type]=pen;
144 }
145 
150 QPen ItemDelegate::defaultPen( ItemType type ) const
151 {
152  return d->defaultpen[type];
153 }
154 
163 Span ItemDelegate::itemBoundingSpan( const StyleOptionGanttItem& opt,
164  const QModelIndex& idx ) const
165 {
166  if ( !idx.isValid() ) return Span();
167 
168  const QString txt = idx.model()->data( idx, Qt::DisplayRole ).toString();
169  const int typ = idx.model()->data( idx, ItemTypeRole ).toInt();
170  QRectF itemRect = opt.itemRect;
171 
172 
173  if ( typ == TypeEvent ) {
174  itemRect = QRectF( itemRect.left()-itemRect.height()/2.,
175  itemRect.top(),
176  itemRect.height(),
177  itemRect.height() );
178  }
179 
180  int tw = opt.fontMetrics.width( txt );
181  tw += static_cast<int>( itemRect.height()/2. );
182  Span s;
183  switch ( opt.displayPosition ) {
184  case StyleOptionGanttItem::Left:
185  s = Span( itemRect.left()-tw, itemRect.width()+tw ); break;
186  case StyleOptionGanttItem::Right:
187  s = Span( itemRect.left(), itemRect.width()+tw ); break;
188  case StyleOptionGanttItem::Center:
189  s = Span( itemRect.left(), itemRect.width() ); break;
190  }
191  return s;
192 }
193 
200 ItemDelegate::InteractionState ItemDelegate::interactionStateFor( const QPointF& pos,
201  const StyleOptionGanttItem& opt,
202  const QModelIndex& idx ) const
203 {
204  if ( !idx.isValid() ) return State_None;
205  if ( !( idx.model()->flags( idx ) & Qt::ItemIsEditable ) ) return State_None;
206 
207  const int typ = static_cast<ItemType>( idx.model()->data( idx, ItemTypeRole ).toInt() );
208  if ( typ == TypeNone || typ == TypeSummary ) return State_None;
209  if ( typ == TypeEvent ) return State_Move;
210  if ( !opt.itemRect.contains(pos) ) return State_None;
211 
212  qreal delta = 5.;
213  if ( opt.itemRect.width() < 15 ) delta = 1.;
214  if( pos.x() >= opt.itemRect.left() && pos.x() < opt.itemRect.left()+delta ) {
215  return State_ExtendLeft;
216  } else if( pos.x() <= opt.itemRect.right() && pos.x() > opt.itemRect.right()-delta ) {
217  return State_ExtendRight;
218  } else {
219  return State_Move;
220  }
221 }
222 
225 void ItemDelegate::paintGanttItem( QPainter* painter,
226  const StyleOptionGanttItem& opt,
227  const QModelIndex& idx )
228 {
229  if ( !idx.isValid() ) return;
230  const ItemType typ = static_cast<ItemType>( idx.model()->data( idx, ItemTypeRole ).toInt() );
231  const QString& txt = opt.text;
232  QRectF itemRect = opt.itemRect;
233  QRectF boundingRect = opt.boundingRect;
234  boundingRect.setY( itemRect.y() );
235  boundingRect.setHeight( itemRect.height() );
236 
237  //qDebug() << "itemRect="<<itemRect<<", boundingRect="<<boundingRect;
238  //qDebug() << painter->font() << opt.fontMetrics.height() << painter->device()->width() << painter->device()->height();
239 
240  painter->save();
241 
242  QPen pen = defaultPen( typ );
243  if ( opt.state & QStyle::State_Selected ) pen.setWidth( 2*pen.width() );
244  painter->setPen( pen );
245  painter->setBrush( defaultBrush( typ ) );
246 
247  qreal pw = painter->pen().width()/2.;
248  switch( typ ) {
249  case TypeTask:
250  if ( itemRect.isValid() ) {
251  // TODO
252  qreal pw = painter->pen().width()/2.;
253  pw-=1;
254  QRectF r = itemRect;
255  r.translate( 0., r.height()/6. );
256  r.setHeight( 2.*r.height()/3. );
257  painter->setBrushOrigin( itemRect.topLeft() );
258  painter->save();
259  painter->translate( 0.5, 0.5 );
260  painter->drawRect( r );
261  bool ok;
262  qreal completion = idx.model()->data( idx, KDGantt::TaskCompletionRole ).toDouble( &ok );
263  if ( ok ) {
264  qreal h = r.height();
265  QRectF cr( r.x(), r.y()+h/4.,
266  r.width()*completion/100., h/2.+1 /*??*/ );
267  QColor compcolor( painter->pen().color() );
268  compcolor.setAlpha( 150 );
269  painter->fillRect( cr, compcolor );
270  }
271  painter->restore();
272  Qt::Alignment ta;
273  switch( opt.displayPosition ) {
274  case StyleOptionGanttItem::Left: ta = Qt::AlignLeft; break;
275  case StyleOptionGanttItem::Right: ta = Qt::AlignRight; break;
276  case StyleOptionGanttItem::Center: ta = Qt::AlignCenter; break;
277  }
278  painter->drawText( boundingRect, ta | Qt::AlignVCenter, txt );
279  }
280  break;
281  case TypeSummary:
282  if ( opt.itemRect.isValid() ) {
283  // TODO
284  pw-=1;
285  const QRectF r = QRectF( opt.itemRect ).adjusted( -pw, -pw, pw, pw );
286  QPainterPath path;
287  const qreal delta = r.height()/2.;
288  path.moveTo( r.topLeft() );
289  path.lineTo( r.topRight() );
290  path.lineTo( QPointF( r.right(), r.top() + 2.*delta ) );
291  //path.lineTo( QPointF( r.right()-3./2.*delta, r.top() + delta ) );
292  path.quadTo( QPointF( r.right()-.5*delta, r.top() + delta ), QPointF( r.right()-2.*delta, r.top() + delta ) );
293  //path.lineTo( QPointF( r.left()+3./2.*delta, r.top() + delta ) );
294  path.lineTo( QPointF( r.left() + 2.*delta, r.top() + delta ) );
295  path.quadTo( QPointF( r.left()+.5*delta, r.top() + delta ), QPointF( r.left(), r.top() + 2.*delta ) );
296  path.closeSubpath();
297  painter->setBrushOrigin( itemRect.topLeft() );
298  painter->save();
299  painter->translate( 0.5, 0.5 );
300  painter->drawPath( path );
301  painter->restore();
302  Qt::Alignment ta;
303  switch( opt.displayPosition ) {
304  case StyleOptionGanttItem::Left: ta = Qt::AlignLeft; break;
305  case StyleOptionGanttItem::Right: ta = Qt::AlignRight; break;
306  case StyleOptionGanttItem::Center: ta = Qt::AlignCenter; break;
307  }
308  painter->drawText( boundingRect, ta | Qt::AlignVCenter, txt );
309  }
310  break;
311  case TypeEvent: /* TODO */
312  //qDebug() << opt.boundingRect << opt.itemRect;
313  if ( opt.boundingRect.isValid() ) {
314  const qreal pw = painter->pen().width() / 2. - 1;
315  const QRectF r = QRectF( opt.itemRect ).adjusted( -pw, -pw, pw, pw ).translated( -opt.itemRect.height()/2, 0 );
316  QPainterPath path;
317  const qreal delta = static_cast< int >( r.height() / 2 );
318  path.moveTo( delta, 0. );
319  path.lineTo( 2.*delta, delta );
320  path.lineTo( delta, 2.*delta );
321  path.lineTo( 0., delta );
322  path.closeSubpath();
323  painter->save();
324  painter->translate( r.topLeft() );
325  painter->translate( 0, 0.5 );
326  painter->drawPath( path );
327  painter->restore();
328  Qt::Alignment ta;
329  switch( opt.displayPosition ) {
330  case StyleOptionGanttItem::Left: ta = Qt::AlignLeft; break;
331  case StyleOptionGanttItem::Right: ta = Qt::AlignRight; break;
332  case StyleOptionGanttItem::Center: ta = Qt::AlignCenter; break;
333  }
334  painter->drawText( boundingRect, ta | Qt::AlignVCenter, txt );
335 #if 0
336  painter->setBrush( Qt::NoBrush );
337  painter->setPen( Qt::black );
338  painter->drawRect( opt.boundingRect );
339  painter->setPen( Qt::red );
340  painter->drawRect( r );
341 #endif
342  }
343  break;
344  default:
345  break;
346  }
347  painter->restore();
348 }
349 
350 static const qreal TURN = 10.;
351 static const qreal PW = 1.5;
352 
357 QRectF ItemDelegate::constraintBoundingRect( const QPointF& start, const QPointF& end ) const
358 {
359  QPolygonF poly;
360  if ( start.x() > end.x()-TURN ) {
361  if ( start.y() < end.y() ) {
362  poly << QPointF( start.x()+TURN, start.y()-TURN/2. )
363  << QPointF( end.x()-TURN, end.y()+TURN/2. );
364  } else {
365  poly << QPointF( start.x()+TURN, start.y()+TURN/2. )
366  << QPointF( end.x()-TURN, end.y()-TURN/2. );
367  }
368  } else {
369  if ( start.y() < end.y() ) {
370  poly << QPointF( start.x(), start.y()-TURN/2. )
371  << QPointF( end.x(), end.y()+TURN/2. );
372  } else {
373  poly << QPointF( start.x(), start.y()+TURN/2. )
374  << QPointF( end.x(), end.y()-TURN/2. );
375  }
376  }
377  return poly.boundingRect().adjusted( -PW, -PW, PW, PW );
378 }
379 
380 
386 void ItemDelegate::paintConstraintItem( QPainter* painter, const QStyleOptionGraphicsItem& opt,
387  const QPointF& start, const QPointF& end, const QPen& pen )
388 {
389  Q_UNUSED( opt );
390  qreal midx = end.x() - TURN;
391  qreal midy = ( end.y()-start.y() )/2. + start.y();
392 
393  painter->setPen( pen );
394  painter->setBrush( pen.color() );
395 
396  if ( start.x() > end.x()-TURN ) {
397  QPolygonF poly;
398  poly << start
399  << QPointF( start.x()+TURN, start.y() )
400  << QPointF( start.x()+TURN, midy )
401  << QPointF( end.x()-TURN, midy )
402  << QPointF( end.x()-TURN, end.y() )
403  << end;
404  painter->drawPolyline( poly );
405  QPolygonF arrow;
406  arrow << end
407  << QPointF( end.x()-TURN/2., end.y()-TURN/2. )
408  << QPointF( end.x()-TURN/2., end.y()+TURN/2. );
409  painter->drawPolygon( arrow );
410  } else {
411  QPolygonF poly;
412  poly << start
413  << QPointF( midx, start.y() )
414  << QPointF( midx, end.y() )
415  << end;
416  painter->drawPolyline( poly );
417  QPolygonF arrow;
418  arrow << end
419  << QPointF( end.x()-TURN/2., end.y()-TURN/2. )
420  << QPointF( end.x()-TURN/2., end.y()+TURN/2. );
421  painter->drawPolygon( arrow );
422  }
423 }
424 
425 #include "moc_kdganttitemdelegate.cpp"
KDGantt::ItemDelegate::Private::defaultpen
QHash< ItemType, QPen > defaultpen
Definition: kdganttitemdelegate_p.h:38
KDGantt::StyleOptionGanttItem::text
QString text
Definition: kdganttstyleoptionganttitem.h:48
KDGantt::StyleOptionGanttItem::displayPosition
Position displayPosition
Definition: kdganttstyleoptionganttitem.h:46
operator<<
QDebug operator<<(QDebug dbg, KDGantt::ItemDelegate::InteractionState state)
Definition: kdganttitemdelegate.cpp:42
KDGantt::TypeSummary
Definition: kdganttglobal.h:215
KDGantt::StyleOptionGanttItem::Center
Definition: kdganttstyleoptionganttitem.h:38
KDGantt::ItemDelegate::InteractionState
InteractionState
Definition: kdganttitemdelegate.h:43
kdganttglobal.h
KDGantt::ItemDelegate::State_ExtendRight
Definition: kdganttitemdelegate.h:46
KDGantt::TypeEvent
Definition: kdganttglobal.h:213
KDGantt::ItemType
ItemType
Definition: kdganttglobal.h:211
KDGantt::ItemDelegate::setDefaultPen
void setDefaultPen(ItemType type, const QPen &pen)
Definition: kdganttitemdelegate.cpp:141
kdganttstyleoptionganttitem.h
QObject
KDGantt::ItemDelegate::interactionStateFor
virtual InteractionState interactionStateFor(const QPointF &pos, const StyleOptionGanttItem &opt, const QModelIndex &idx) const
Definition: kdganttitemdelegate.cpp:200
KDGantt::ItemDelegate::itemBoundingSpan
virtual Span itemBoundingSpan(const StyleOptionGanttItem &opt, const QModelIndex &idx) const
Definition: kdganttitemdelegate.cpp:163
KDGantt::TypeTask
Definition: kdganttglobal.h:214
KDGantt::ItemDelegate::State_ExtendLeft
Definition: kdganttitemdelegate.h:45
KDGantt::ItemDelegate::Private::defaultbrush
QHash< ItemType, QBrush > defaultbrush
Definition: kdganttitemdelegate_p.h:37
KDGantt::Span
A class representing a start point and a length.
Definition: kdganttglobal.h:220
KDGantt::ItemDelegate::defaultBrush
QBrush defaultBrush(ItemType type) const
Definition: kdganttitemdelegate.cpp:130
KDGantt::ItemTypeRole
Definition: kdganttglobal.h:207
KDGantt::ItemDelegate::Private::Private
Private()
Definition: kdganttitemdelegate.cpp:74
KDGantt::StyleOptionGanttItem::Left
Definition: kdganttstyleoptionganttitem.h:38
KDGantt::ItemDelegate::~ItemDelegate
virtual ~ItemDelegate()
Definition: kdganttitemdelegate.cpp:108
kdganttitemdelegate_p.h
KDGantt::ItemDelegate::defaultPen
QPen defaultPen(ItemType type) const
Definition: kdganttitemdelegate.cpp:150
d
#define d
Definition: kdganttitemdelegate.cpp:113
KDGantt::ItemDelegate::State_None
Definition: kdganttitemdelegate.h:43
KDGantt::ItemDelegate::constraintBoundingRect
virtual QRectF constraintBoundingRect(const QPointF &start, const QPointF &end) const
Definition: kdganttitemdelegate.cpp:357
PRINT_INTERACTIONSTATE
#define PRINT_INTERACTIONSTATE(x)
Definition: kdganttitemdelegate.cpp:38
QItemDelegate
KDGantt::TaskCompletionRole
Definition: kdganttglobal.h:206
PW
static const qreal PW
Definition: kdganttitemdelegate.cpp:351
KDGantt::ItemDelegate::setDefaultBrush
void setDefaultBrush(ItemType type, const QBrush &brush)
Definition: kdganttitemdelegate.cpp:121
KDGantt::TypeNone
Definition: kdganttglobal.h:212
TURN
static const qreal TURN
Definition: kdganttitemdelegate.cpp:350
KDGantt::StyleOptionGanttItem::itemRect
QRectF itemRect
Definition: kdganttstyleoptionganttitem.h:45
KDGantt::ItemDelegate::State_Move
Definition: kdganttitemdelegate.h:44
KDGantt::StyleOptionGanttItem
QStyleOption subclass for gantt items.
Definition: kdganttstyleoptionganttitem.h:36
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::ItemDelegate::ItemDelegate
ItemDelegate(QObject *parent=0)
Definition: kdganttitemdelegate.cpp:102
KDGantt::ItemDelegate::Private
Definition: kdganttitemdelegate_p.h:33
KDGantt::ItemDelegate::paintConstraintItem
virtual void paintConstraintItem(QPainter *p, const QStyleOptionGraphicsItem &opt, const QPointF &start, const QPointF &end, const QPen &pen)
Definition: kdganttitemdelegate.cpp:386
KDGantt::StyleOptionGanttItem::boundingRect
QRectF boundingRect
Definition: kdganttstyleoptionganttitem.h:44
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