File indexing completed on 2024-05-19 15:27:34

0001 /*
0002  * Copyright (C) 2001-2015 Klaralvdalens Datakonsult AB.  All rights reserved.
0003  *
0004  * This file is part of the KGantt library.
0005  *
0006  * This program is free software; you can redistribute it and/or
0007  * modify it under the terms of the GNU General Public License as
0008  * published by the Free Software Foundation; either version 2 of
0009  * the License, or (at your option) any later version.
0010  *
0011  * This program is distributed in the hope that it will be useful,
0012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0014  * GNU General Public License for more details.
0015  *
0016  * You should have received a copy of the GNU General Public License
0017  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
0018  */
0019 
0020 #ifndef KGANTTGRAPHICSSCENE_H
0021 #define KGANTTGRAPHICSSCENE_H
0022 
0023 #include <QDateTime>
0024 #include <QList>
0025 #include <QGraphicsScene>
0026 #include <QModelIndex>
0027 
0028 #include "kganttglobal.h"
0029 
0030 QT_BEGIN_NAMESPACE
0031 class QAbstractProxyModel;
0032 class QItemSelectionModel;
0033 class QItemSelection;
0034 class QPrinter;
0035 class QAbstractItemModel;
0036 
0037 
0038 /*!\class KGantt::GraphicsScene
0039  * \internal
0040  */
0041 class QGraphicsSceneMouseEvent;
0042 QT_END_NAMESPACE
0043 
0044 namespace KGantt {
0045     class AbstractGrid;
0046     class AbstractRowController;
0047     class GraphicsItem;
0048     class Constraint;
0049     class ConstraintModel;
0050     class ConstraintGraphicsItem;
0051     class ItemDelegate;
0052     class PrintingContext;
0053 
0054     class KGANTT_EXPORT GraphicsScene : public QGraphicsScene {
0055         Q_OBJECT
0056         KGANTT_DECLARE_PRIVATE_BASE_POLYMORPHIC( GraphicsScene )
0057     public:
0058         explicit GraphicsScene( QObject* parent = nullptr );
0059         virtual ~GraphicsScene();
0060 
0061         //qreal dateTimeToSceneX( const QDateTime& dt ) const;
0062         //QDateTime sceneXtoDateTime( qreal x ) const;
0063 
0064         static QModelIndex mainIndex( const QModelIndex& idx );
0065 
0066         /*! Returns the index pointing to the last column
0067          * in the same row as idx. This can be thought of
0068          * as in "inverse" of mainIndex()
0069          */
0070         static QModelIndex dataIndex( const QModelIndex& idx );
0071 
0072         QAbstractItemModel* model() const;
0073         QAbstractProxyModel* summaryHandlingModel() const;
0074         QModelIndex rootIndex() const;
0075         ConstraintModel* constraintModel() const;
0076         QItemSelectionModel* selectionModel() const;
0077 
0078         void insertItem( const QPersistentModelIndex&, GraphicsItem* );
0079         void removeItem( const QModelIndex& );
0080         using QGraphicsScene::removeItem;
0081         GraphicsItem* findItem( const QModelIndex& ) const;
0082         GraphicsItem* findItem( const QPersistentModelIndex& ) const;
0083 
0084         void updateItems();
0085         void clearItems();
0086         void deleteSubtree( const QModelIndex& );
0087 
0088         ConstraintGraphicsItem* findConstraintItem( const Constraint& ) const;
0089         QList<ConstraintGraphicsItem*> findConstraintItems( const QModelIndex& idx ) const;
0090 
0091         void setItemDelegate( ItemDelegate* );
0092         ItemDelegate* itemDelegate() const;
0093 
0094         void setRowController( AbstractRowController* rc );
0095         AbstractRowController* rowController() const;
0096 
0097         /**
0098          * Set the grid to @p grid.
0099          * The current grid (if set) is deleted.
0100          * If @p grid is nullptr, the scene reverts to use the default_grid
0101          */
0102         void setGrid( AbstractGrid* grid );
0103         /**
0104          * @return the grid set with setGrid().
0105          * Note: Returns nullptr if no grid has been set.
0106          */
0107         AbstractGrid* grid() const;
0108         /**
0109          * @return the current grid.
0110          */
0111         const AbstractGrid *getGrid() const;
0112         /**
0113          * @return the grid set with setGrid()
0114          * Note: Returns nullptr if no grid has been set.
0115          * The scene reverts to use the default_grid.
0116          */
0117         AbstractGrid *takeGrid();
0118 
0119         bool isReadOnly() const;
0120 
0121         void updateRow( const QModelIndex& idx );
0122 
0123         /*! Creates a new item of type type.
0124          */
0125         GraphicsItem* createItem( ItemType type ) const;
0126 
0127         /* used by GraphicsItem */
0128         void itemEntered( const QModelIndex& );
0129         void itemPressed( const QModelIndex& idx, QGraphicsSceneMouseEvent *event );
0130         void itemClicked( const QModelIndex& );
0131         void itemDoubleClicked( const QModelIndex& );
0132         void setDragSource( GraphicsItem* item );
0133         GraphicsItem* dragSource() const;
0134 
0135         /* Printing */
0136 
0137         /*! Print the Gantt chart using \a printer. If \a drawRowLabels
0138          * is true (the default), each row will have it's label printed
0139          * on the left side. If \a drawColumnLabels is true (the
0140          * default), each column will have it's label printed at the
0141          * top side.
0142          *
0143          * This version of print() will print multiple pages.
0144          */
0145         void print( QPrinter* printer, bool drawRowLabels = true, bool drawColumnLabels = true );
0146 
0147         /*! Print part of the Gantt chart from \a start to \a end using \a printer.
0148          * If \a drawRowLabels is true (the default), each row will have it's
0149          * label printed on the left side. If \a drawColumnLabels is true (the
0150          * default), each column will have it's label printed at the
0151          * top side.
0152          *
0153          * This version of print() will print multiple pages.
0154          *
0155          * To print a certain range of a chart with a DateTimeGrid, use
0156          * qreal DateTimeGrid::mapFromDateTime( const QDateTime& dt) const
0157          * to figure out the values for \a start and \a end.
0158          */
0159         void print( QPrinter* printer, qreal start, qreal end, bool drawRowLabels = true, bool drawColumnLabels = true );
0160 
0161         /*! Render the GanttView inside the rectangle \a target using the painter \a painter.
0162          * If \a drawRowLabels is true (the default), each row will have it's
0163          * label printed on the left side. If \a drawColumnLabels is true (the
0164          * default), each column will have it's label printed at the
0165          * top side.
0166          */
0167         void print( QPainter* painter, const QRectF& target = QRectF(), bool drawRowLabels=true, bool drawColumnLabels = true );
0168 
0169         /*! Render the GanttView inside the rectangle \a target using the painter \a painter.
0170          * If \a drawRowLabels is true (the default), each row will have it's
0171          * label printed on the left side. If \a drawColumnLabels is true (the
0172          * default), each column will have it's label printed at the
0173          * top side.
0174          *
0175          * To print a certain range of a chart with a DateTimeGrid, use
0176          * qreal DateTimeGrid::mapFromDateTime( const QDateTime& dt) const
0177          * to figure out the values for \a start and \a end.
0178          */
0179         void print( QPainter* painter, qreal start, qreal end, const QRectF& target = QRectF(), bool drawRowLabels=true, bool drawColumnLabels = true );
0180 
0181         /*! Print the Gantt chart on the \a printer in accordance with the PrintingContext \a context
0182          * 
0183          * \see PrintingContext
0184          * 
0185          * \since 2.8.0
0186          */
0187         void printDiagram( QPrinter *printer, const PrintingContext &context );
0188 
0189     Q_SIGNALS:
0190         void gridChanged();
0191 
0192         void clicked( const QModelIndex & index );
0193         void qrealClicked( const QModelIndex & index );
0194         void entered( const QModelIndex & index );
0195         void pressed( const QModelIndex & index );
0196 
0197     protected:
0198         /*reimp*/ void helpEvent( QGraphicsSceneHelpEvent *helpEvent ) override;
0199         /*reimp*/ void drawBackground( QPainter* painter, const QRectF& rect ) override;
0200         /*reimp*/ void drawForeground( QPainter* painter, const QRectF& rect ) override;
0201 
0202     public Q_SLOTS:
0203         void setModel( QAbstractItemModel* );
0204         void setSummaryHandlingModel( QAbstractProxyModel* );
0205         void setConstraintModel( ConstraintModel* );
0206         void setRootIndex( const QModelIndex& idx );
0207         void setSelectionModel( QItemSelectionModel* selectionmodel );
0208         void setReadOnly( bool );
0209 
0210     private Q_SLOTS:
0211         /* slots for ConstraintModel */
0212         void slotConstraintAdded( const KGantt::Constraint& );
0213         void slotConstraintRemoved( const KGantt::Constraint& );
0214         void slotGridChanged();
0215         void slotSelectionChanged(const QItemSelection &selected, const QItemSelection &deselected);
0216         void selectionModelChanged(QAbstractItemModel *);
0217 
0218     private:
0219         /*\internal
0220          */
0221         void doPrint( QPainter* painter, const QRectF& targetRect,
0222                       qreal start, qreal end,
0223                       QPrinter* printer, bool drawRowLabels, bool drawColumnLabels );
0224 
0225         void doPrintScene( QPrinter *printer, QPainter *painter, const QRectF& targetRect, const PrintingContext &context );
0226 
0227         void drawLabelsHeader( QPainter *painter, const QRectF &sourceRect, const QRectF &targetRect );
0228     };
0229 }
0230 
0231 #endif /* KGANTTGRAPHICSSCENE_H */