File indexing completed on 2024-05-12 04:20:44

0001 /*
0002  * SPDX-FileCopyrightText: 2001-2015 Klaralvdalens Datakonsult AB. All rights reserved.
0003  *
0004  * This file is part of the KGantt library.
0005  *
0006  * SPDX-License-Identifier: GPL-2.0-or-later
0007  */
0008 
0009 #ifndef KGANTTITEMDELEGATE_H
0010 #define KGANTTITEMDELEGATE_H
0011 
0012 #include <QItemDelegate>
0013 #include <QBrush>
0014 #include <QPen>
0015 #include <QDebug>
0016 
0017 #include "kganttglobal.h"
0018 
0019 namespace KGantt {
0020     class StyleOptionGanttItem;
0021     class Constraint;
0022 
0023     /*!\class KGantt::ItemDelegate kganttitemdelegate.h KGanttItemDelegate
0024      *\ingroup KGantt
0025      *\brief Class used to render gantt items in a KGantt::GraphicsView
0026      *
0027      */
0028     class KGANTT_EXPORT ItemDelegate : public QItemDelegate {
0029         Q_OBJECT
0030         KGANTT_DECLARE_PRIVATE_BASE_POLYMORPHIC( ItemDelegate )
0031     public:
0032         /*!\enum KGantt::ItemDelegate::InteractionState
0033          * This enum is used for communication between the view and
0034          * the delegate about user interaction with gantt items.
0035          *
0036          * \see KGantt::ItemDelegate::interactionStateFor
0037          */
0038         enum InteractionState { State_None = 0,
0039                                 State_Move,
0040                                 State_ExtendLeft,
0041                                 State_ExtendRight,
0042                                 State_DragConstraint
0043         };
0044 
0045         /*! Constructor. Creates an ItemDelegate with parent \a parent */
0046         explicit ItemDelegate( QObject* parent = nullptr );
0047 
0048         /*! Destructor */
0049         ~ItemDelegate() override;
0050 
0051         /*! Sets the default brush used for items of type \a type to
0052          * \a brush. The default brush is used in the case when the model
0053          * does not provide an explicit brush.
0054          *
0055          * \todo Move this to GraphicsView to make delegate stateless.
0056          */
0057         void setDefaultBrush( ItemType type, const QBrush& brush );
0058 
0059         /*!\returns The default brush for item type \a type
0060          *
0061          * \todo Move this to GraphicsView to make delegate stateless.
0062          */
0063         QBrush defaultBrush( ItemType type ) const;
0064 
0065         /*! Sets the default pen used for items of type \a type to
0066          * \a pen. The default pen is used in the case when the model
0067          * does not provide an explicit pen.
0068          *
0069          * \todo Move this to GraphicsView to make delegate stateless.
0070          */
0071         void setDefaultPen( ItemType type, const QPen& pen );
0072 
0073         /*!\returns The default pen for item type \a type
0074          *
0075          * \todo Move this to GraphicsView to make delegate stateless.
0076          */
0077         QPen defaultPen( ItemType type ) const;
0078 
0079         /*! \returns The bounding Span for the item identified by \a idx
0080          * when rendered with options \a opt. This is often the same as the
0081          * span given by the AbstractGrid for \a idx, but it might be larger
0082          * in case there are additional texts or decorations on the item.
0083          *
0084          * Override this to implement new itemtypes or to change the look
0085          * of the existing ones.
0086          */
0087         virtual Span itemBoundingSpan(const StyleOptionGanttItem& opt, const QModelIndex& idx) const;
0088 
0089         /*! \return The bounding rectangle for the graphics used to represent
0090          * a constraint between points \a start and \a end (typically an
0091          * arrow)
0092          */
0093         virtual QRectF constraintBoundingRect( const QPointF& start, const QPointF& end, const Constraint &constraint ) const;
0094 
0095         /*! \returns The interaction state for position \a pos on item \a idx
0096          * when rendered with options \a opt. This is used to tell the view
0097          * about how the item should react to mouse click/drag.
0098          *
0099          * Override to implement new items or interactions.
0100          */
0101         virtual InteractionState interactionStateFor( const QPointF& pos,
0102                                                       const StyleOptionGanttItem& opt,
0103                                                       const QModelIndex& idx ) const;
0104 
0105         /*! Paints the gantt item \a idx using \a painter and \a opt
0106          */
0107         virtual void paintGanttItem( QPainter* p, const StyleOptionGanttItem& opt, const QModelIndex& idx );
0108 
0109         /*! Paints the \a constraint between points \a start and \a end
0110          * using \a painter and \a opt.
0111          *
0112          * \todo Review \a opt's type
0113          */
0114         virtual void paintConstraintItem( QPainter* p, const QStyleOptionGraphicsItem& opt,
0115                                           const QPointF& start, const QPointF& end, const Constraint &constraint );
0116         
0117 
0118         /*!\returns The tooltip for index \a idx
0119          */
0120         virtual QString toolTip( const QModelIndex &idx ) const;
0121 
0122     protected:
0123         void paintFinishStartConstraint( QPainter* p, const QStyleOptionGraphicsItem& opt,
0124                 const QPointF& start, const QPointF& end, const Constraint &constraint );
0125         QPolygonF finishStartLine( const QPointF& start, const QPointF& end ) const;
0126         QPolygonF finishStartArrow( const QPointF& start, const QPointF& end ) const;
0127 
0128         void paintFinishFinishConstraint( QPainter* p, const QStyleOptionGraphicsItem& opt,
0129                 const QPointF& start, const QPointF& end, const Constraint &constraint );
0130         QPolygonF finishFinishLine( const QPointF& start, const QPointF& end ) const;
0131         QPolygonF finishFinishArrow( const QPointF& start, const QPointF& end ) const;
0132         
0133         void paintStartStartConstraint( QPainter* p, const QStyleOptionGraphicsItem& opt,
0134                 const QPointF& start, const QPointF& end, const Constraint &constraint );
0135         QPolygonF startStartLine( const QPointF& start, const QPointF& end ) const;
0136         QPolygonF startStartArrow( const QPointF& start, const QPointF& end ) const;
0137         
0138         void paintStartFinishConstraint( QPainter* p, const QStyleOptionGraphicsItem& opt,
0139                 const QPointF& start, const QPointF& end, const Constraint &constraint );
0140         QPolygonF startFinishLine( const QPointF& start, const QPointF& end ) const;
0141         QPolygonF startFinishArrow( const QPointF& start, const QPointF& end ) const;
0142     };
0143 }
0144 
0145 #ifndef QT_NO_DEBUG_STREAM
0146 QDebug operator<<( QDebug dbg, KGantt::ItemDelegate::InteractionState );
0147 #endif
0148 
0149 #endif /* KGANTTITEMDELEGATE_H */
0150