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 KGANTTABSTRACTGRID_H
21 #define KGANTTABSTRACTGRID_H
22 
23 #include "kganttglobal.h"
24 #include "kganttconstraint.h"
25 
26 QT_BEGIN_NAMESPACE
27 class QPainter;
28 class QRectF;
29 class QAbstractItemModel;
30 class QModelIndex;
31 QT_END_NAMESPACE
32 
33 namespace KGantt {
34     class AbstractRowController;
35     class Span;
36 
37 
38 
39     /*!\class KGantt::AbstractGrid kganttabstractgrid.h KGanttAbstractGrid
40      * \ingroup KGantt
41      * \brief Abstract baseclass for grids. A grid is used to convert between
42      * QModelIndex'es and gantt chart values (qreals) and to paint the
43      * background and header of the view.
44      *
45      * \see KGantt::DateTimeGrid
46      */
47     class KGANTT_EXPORT AbstractGrid : public QObject {
48         Q_OBJECT
49         KGANTT_DECLARE_PRIVATE_BASE_POLYMORPHIC( AbstractGrid )
50 	friend class GraphicsScene;
51     public:
52         /*! Constructor. Creates an AbstractGrid with parent \a parent.
53          * The QObject parent is not used for anything internally. */
54         AbstractGrid(QObject* parent = nullptr);
55 
56 
57         /*! Destructor. Does nothing */
58         virtual ~AbstractGrid();
59 
60         /*!\returns The QAbstractItemModel used by this grid */
61         QAbstractItemModel* model() const;
62 
63         /*!\returns the current root index for this grid */
64         QModelIndex rootIndex() const;
65 
66         /*!\fn virtual Span AbstractGrid::mapToChart( const QModelIndex& idx ) const
67          * Implement this to map from the data in the model to the location of
68          * the corresponding item in the view.
69          */
70         virtual Span mapToChart( const QModelIndex& idx ) const = 0;
71 
72         /*!\fn virtual bool AbstractGrid::mapFromChart( const Span& span, const QModelIndex& idx, const QList<Constraint>& constraints ) const
73          * Implement this to update the model data based on the location of the item. Check
74          * against the \a constraints list to make sure no hard constraints are violated by
75          * writing back to the model.
76          * \returns true if the update succeeded.
77          */
78         virtual bool mapFromChart( const Span& span, const QModelIndex& idx,
79                                    const QList<Constraint>& constraints=QList<Constraint>() ) const = 0;
80 
81         /*!
82         * Implement this to map from \a value to the corresponding location in the view.
83         * Return a negative value if \a value cannot be mapped.
84         * The default implementation returns -1.0.
85         */
86         virtual qreal mapToChart( const QVariant &value ) const;
87 
88         /*!
89          * Implement this to map from \a x to the corresponding location in the view.
90          * Return an invalid value if \a x cannot be mapped.
91          */
92         virtual QVariant mapFromChart( qreal x ) const;
93 
94         /*!\returns true if the startpoint is before the endpoint
95          * of the constraint \a c.
96          */
97         bool isSatisfiedConstraint( const Constraint& c ) const;
98 
99         /*!\fn virtual void AbstractGrid::paintGrid( QPainter* painter, const QRectF& sceneRect, const QRectF& exposedRect, AbstractRowController* rowController=0, QWidget* widget=0 )
100          *
101          * Implement this to paint the background of the view -- typically
102          * with some grid lines.
103          * \param painter -- the QPainter to paint with.
104          * \param sceneRect -- the total bounding rectangle of the scene.
105          * \param exposedRect -- the rectangle that needs to be painted.
106          * \param rowController -- the row controller used by the view -- may be 0.
107          * \param widget -- the widget used by the view -- may be 0.
108          */
109         virtual void paintGrid( QPainter* painter, const QRectF& sceneRect, const QRectF& exposedRect,
110                                 AbstractRowController* rowController = nullptr, QWidget* widget = nullptr ) = 0;
111 
112 
113         /*!\fn virtual void AbstractGrid::paintHeader( QPainter* painter, const QRectF& headerRect, const QRectF& exposedRect, qreal offset, QWidget* widget=0 )
114          *
115          * Implement this to paint the header part of the view.
116          * \param painter -- the QPainter to paint with.
117          * \param headerRect -- the total rectangle occupied by the header.
118          * \param exposedRect -- the rectangle that needs to be painted.
119          * \param offset -- the horizontal scroll offset of the view.
120          * \param widget -- the widget used by the view -- may be 0.
121          */
122         virtual void paintHeader( QPainter* painter, const QRectF& headerRect, const QRectF& exposedRect,
123                                   qreal offset, QWidget* widget = nullptr ) = 0;
124 
125     public Q_SLOTS:
126         /*! Sets the QAbstractItemModel used by this grid implementation.
127          * This is called by the view, you should never need to call this
128          * from client code.  */
129         /*internal*/ virtual void setModel( QAbstractItemModel* model );
130 
131         /*! Sets the root index used by this grid implementation.
132          * This is called by the view, you should never need to call this
133          * from client code.  */
134         /*internal*/ virtual void setRootIndex( const QModelIndex& idx );
135     Q_SIGNALS:
136         void gridChanged();
137 
138     protected:
139         /*!
140         \todo document this function
141         */
142         virtual void drawBackground(QPainter* paint, const QRectF& rect);
143 
144         /*!
145         \todo document this function
146         */
147         virtual void drawForeground(QPainter* paint, const QRectF& rect);
148     };
149 }
150 
151 #endif /* KGANTTABSTRACTGRID_H */
152