File indexing completed on 2025-09-21 04:06:08

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