1 /*
2  * Copyright (C) 2001-2015 Klaralvdalens Datakonsult AB.  All rights reserved.
3  *
4  * This file is part of the KGantt library.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef KGANTTITEMDELEGATE_H
21 #define KGANTTITEMDELEGATE_H
22 
23 #include <QItemDelegate>
24 #include <QBrush>
25 #include <QPen>
26 #include <QDebug>
27 
28 #include "kganttglobal.h"
29 
30 namespace KGantt {
31     class StyleOptionGanttItem;
32     class Constraint;
33 
34     /*!\class KGantt::ItemDelegate kganttitemdelegate.h KGanttItemDelegate
35      *\ingroup KGantt
36      *\brief Class used to render gantt items in a KGantt::GraphicsView
37      *
38      */
39     class KGANTT_EXPORT ItemDelegate : public QItemDelegate {
40         Q_OBJECT
41         KGANTT_DECLARE_PRIVATE_BASE_POLYMORPHIC( ItemDelegate )
42     public:
43         /*!\enum KGantt::ItemDelegate::InteractionState
44          * This enum is used for communication between the view and
45          * the delegate about user interaction with gantt items.
46          *
47          * \see KGantt::ItemDelegate::interactionStateFor
48          */
49         enum InteractionState { State_None = 0,
50                                 State_Move,
51                                 State_ExtendLeft,
52                                 State_ExtendRight,
53                                 State_DragConstraint
54         };
55 
56         /*! Constructor. Creates an ItemDelegate with parent \a parent */
57         explicit ItemDelegate( QObject* parent = nullptr );
58 
59         /*! Destructor */
60         virtual ~ItemDelegate();
61 
62         /*! Sets the default brush used for items of type \a type to
63          * \a brush. The default brush is used in the case when the model
64          * does not provide an explicit brush.
65          *
66          * \todo Move this to GraphicsView to make delegate stateless.
67          */
68         void setDefaultBrush( ItemType type, const QBrush& brush );
69 
70         /*!\returns The default brush for item type \a type
71          *
72          * \todo Move this to GraphicsView to make delegate stateless.
73          */
74         QBrush defaultBrush( ItemType type ) const;
75 
76         /*! Sets the default pen used for items of type \a type to
77          * \a pen. The default pen is used in the case when the model
78          * does not provide an explicit pen.
79          *
80          * \todo Move this to GraphicsView to make delegate stateless.
81          */
82         void setDefaultPen( ItemType type, const QPen& pen );
83 
84         /*!\returns The default pen for item type \a type
85          *
86          * \todo Move this to GraphicsView to make delegate stateless.
87          */
88         QPen defaultPen( ItemType type ) const;
89 
90         /*! \returns The bounding Span for the item identified by \a idx
91          * when rendered with options \a opt. This is often the same as the
92          * span given by the AbstractGrid for \a idx, but it might be larger
93          * in case there are additional texts or decorations on the item.
94          *
95          * Override this to implement new itemtypes or to change the look
96          * of the existing ones.
97          */
98         virtual Span itemBoundingSpan(const StyleOptionGanttItem& opt, const QModelIndex& idx) const;
99 
100         /*! \return The bounding rectangle for the graphics used to represent
101          * a constraint between points \a start and \a end (typically an
102          * arrow)
103          */
104         virtual QRectF constraintBoundingRect( const QPointF& start, const QPointF& end, const Constraint &constraint ) const;
105 
106         /*! \returns The interaction state for position \a pos on item \a idx
107          * when rendered with options \a opt. This is used to tell the view
108          * about how the item should react to mouse click/drag.
109          *
110          * Override to implement new items or interactions.
111          */
112         virtual InteractionState interactionStateFor( const QPointF& pos,
113                                                       const StyleOptionGanttItem& opt,
114                                                       const QModelIndex& idx ) const;
115 
116         /*! Paints the gantt item \a idx using \a painter and \a opt
117          */
118         virtual void paintGanttItem( QPainter* p, const StyleOptionGanttItem& opt, const QModelIndex& idx );
119 
120         /*! Paints the \a constraint between points \a start and \a end
121          * using \a painter and \a opt.
122          *
123          * \todo Review \a opt's type
124          */
125         virtual void paintConstraintItem( QPainter* p, const QStyleOptionGraphicsItem& opt,
126                                           const QPointF& start, const QPointF& end, const Constraint &constraint );
127 
128 
129         /*!\returns The tooltip for index \a idx
130          */
131         virtual QString toolTip( const QModelIndex &idx ) const;
132 
133     protected:
134         void paintFinishStartConstraint( QPainter* p, const QStyleOptionGraphicsItem& opt,
135                 const QPointF& start, const QPointF& end, const Constraint &constraint );
136         QPolygonF finishStartLine( const QPointF& start, const QPointF& end ) const;
137         QPolygonF finishStartArrow( const QPointF& start, const QPointF& end ) const;
138 
139         void paintFinishFinishConstraint( QPainter* p, const QStyleOptionGraphicsItem& opt,
140                 const QPointF& start, const QPointF& end, const Constraint &constraint );
141         QPolygonF finishFinishLine( const QPointF& start, const QPointF& end ) const;
142         QPolygonF finishFinishArrow( const QPointF& start, const QPointF& end ) const;
143 
144         void paintStartStartConstraint( QPainter* p, const QStyleOptionGraphicsItem& opt,
145                 const QPointF& start, const QPointF& end, const Constraint &constraint );
146         QPolygonF startStartLine( const QPointF& start, const QPointF& end ) const;
147         QPolygonF startStartArrow( const QPointF& start, const QPointF& end ) const;
148 
149         void paintStartFinishConstraint( QPainter* p, const QStyleOptionGraphicsItem& opt,
150                 const QPointF& start, const QPointF& end, const Constraint &constraint );
151         QPolygonF startFinishLine( const QPointF& start, const QPointF& end ) const;
152         QPolygonF startFinishArrow( const QPointF& start, const QPointF& end ) const;
153     };
154 }
155 
156 #ifndef QT_NO_DEBUG_STREAM
157 QDebug operator<<( QDebug dbg, KGantt::ItemDelegate::InteractionState );
158 #endif
159 
160 #endif /* KGANTTITEMDELEGATE_H */
161 
162