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

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 KGANTTGRAPHICSVIEW_H
0010 #define KGANTTGRAPHICSVIEW_H
0011 
0012 #include <QGraphicsView>
0013 
0014 #include "kganttglobal.h"
0015 
0016 QT_BEGIN_NAMESPACE
0017 class QPrinter;
0018 class QModelIndex;
0019 class QAbstractItemModel;
0020 class QAbstractProxyModel;
0021 class QItemSelectionModel;
0022 QT_END_NAMESPACE
0023 
0024 namespace KGantt {
0025     class AbstractRowController;
0026     class AbstractGrid;
0027     class GraphicsItem;
0028     class ConstraintModel;
0029     class ItemDelegate;
0030     class PrintingContext;
0031 
0032     /*!\class KGantt::GraphicsView kganttgraphicsview.h KGanttGraphicsView
0033      * \ingroup KGantt
0034      * \brief The GraphicsView class provides a model/view implementation of a gantt chart.
0035      */
0036     class KGANTT_EXPORT GraphicsView : public QGraphicsView {
0037         Q_OBJECT
0038         KGANTT_DECLARE_PRIVATE_BASE_POLYMORPHIC(GraphicsView)
0039 
0040         Q_PROPERTY( bool readOnly READ isReadOnly WRITE setReadOnly )
0041 
0042         Q_PRIVATE_SLOT( d, void slotGridChanged() )
0043         Q_PRIVATE_SLOT( d, void slotHorizontalScrollValueChanged( int ) )
0044 
0045 
0046         Q_PRIVATE_SLOT( d, void slotHeaderContextMenuRequested( const QPoint& ) )
0047         /* slots for QAbstractItemModel signals */
0048         Q_PRIVATE_SLOT( d, void slotColumnsInserted( const QModelIndex& parent,  int start, int end ) )
0049         Q_PRIVATE_SLOT( d, void slotColumnsRemoved( const QModelIndex& parent,  int start, int end ) )
0050         Q_PRIVATE_SLOT( d, void slotDataChanged( const QModelIndex& topLeft, const QModelIndex& bottomRight ) )
0051         Q_PRIVATE_SLOT( d, void slotLayoutChanged() )
0052         Q_PRIVATE_SLOT( d, void slotModelReset() )
0053         Q_PRIVATE_SLOT( d, void slotRowsInserted( const QModelIndex& parent,  int start, int end ) )
0054         Q_PRIVATE_SLOT( d, void slotRowsAboutToBeRemoved( const QModelIndex& parent,  int start, int end ) )
0055         Q_PRIVATE_SLOT( d, void slotRowsRemoved( const QModelIndex& parent,  int start, int end ) )
0056 
0057         Q_PRIVATE_SLOT( d, void slotItemClicked( const QModelIndex& idx ) )
0058         Q_PRIVATE_SLOT( d, void slotItemDoubleClicked( const QModelIndex& idx ) )
0059     public:
0060         /*! Constructor. Creates a new KGantt::GraphicsView with parent
0061          * \a parent.
0062          */
0063         explicit GraphicsView( QWidget* parent = nullptr );
0064 
0065 
0066         /*! Destroys this view. */
0067         ~GraphicsView() override;
0068 
0069         /*! \returns the current model displayed by this view
0070          * 
0071          * Note: The returned model is not the model set with \a setModel()
0072          * 
0073          * \see GraphicsView::setModel
0074          */
0075         QAbstractItemModel* model() const;
0076 
0077         /*! \returns the KGantt::SummaryHandlingProxyModel used by this view.
0078          */
0079         QAbstractProxyModel* summaryHandlingModel() const;
0080 
0081         /*! \returns the KGantt::ConstraintModel displayed by this view.
0082          */
0083         ConstraintModel* constraintModel() const;
0084 
0085         /*! \returns the rootindex for this view.
0086          */
0087         QModelIndex rootIndex() const;
0088 
0089         /*! \returns the QItemSelectionModel used by this view
0090          */
0091         QItemSelectionModel* selectionModel() const;
0092 
0093 
0094         /*! \returns the AbstractRowController
0095          * for this view. \see setRowController
0096          */
0097         AbstractRowController* rowController() const;
0098 
0099         /*! \returns the AbstractGrid used by this view.
0100          */
0101         AbstractGrid* grid() const;
0102 
0103         /*! \returns the AbstractGrid used by this view.
0104          */
0105         AbstractGrid* takeGrid();
0106 
0107         /*! \returns the ItemDelegate used by this view to render items
0108         */
0109         ItemDelegate* itemDelegate() const;
0110 
0111         /*!\returns true if the view is in read-only mode
0112          */
0113         bool isReadOnly() const;
0114 
0115         /*! Sets the context menu policy for the header. The default value
0116          * Qt::DefaultContextMenu results in a standard context menu on the header
0117          * that allows the user to set the scale and zoom.
0118          *
0119          * Setting this to Qt::CustomContextMenu will cause the signal
0120          * headerContextMenuRequested(const QPoint& pt) to be emitted instead.
0121          *
0122          * \see QWidget::setContextMenuPolicy( Qt::ContextMenuPolicy )
0123          */
0124         void setHeaderContextMenuPolicy( Qt::ContextMenuPolicy );
0125 
0126         /*! \returns the context menu policy for the header
0127          */
0128         Qt::ContextMenuPolicy headerContextMenuPolicy() const;
0129 
0130         /*!\returns The QModelIndex for the item located at
0131          * position \a pos in the view or an invalid index
0132          * if no item was present at that position.
0133          *
0134          * This is useful for for example contextmenus.
0135          */
0136         QModelIndex indexAt( const QPoint& pos ) const;
0137 
0138         /*! Adds a constraint from \a from to \a to. \a modifiers are the
0139          * keyboard modifiers pressed by the user when the action is invoked.
0140          *
0141          * Override this to control how constraints are added. The default
0142          * implementation adds a soft constraint unless the Shift key is pressed,
0143          * in that case it adds a hard constraint. If a constraint is already
0144          * present, it is removed and nothing is added.
0145          */
0146         virtual void addConstraint( const QModelIndex& from,
0147                                     const QModelIndex& to,
0148                                     Qt::KeyboardModifiers modifiers );
0149 
0150 
0151         /*! \internal */
0152         void updateRow( const QModelIndex& );
0153 
0154         /*! \internal
0155          * Resets the state of the view.
0156          */
0157         void updateScene();
0158 
0159 #if 0
0160         TODO: For 3.0
0161 
0162 
0163         /*! Creates a new GraphicsItem
0164          * Re-iplement to create your own flavour of GraphicsItem
0165          */
0166         virtual GraphicsItem* createItem( ItemType type ) const;
0167 #endif
0168 
0169     public Q_SLOTS:
0170         /*! \internal
0171          * Adjusts the bounding rectangle of the scene.
0172          */
0173         void updateSceneRect();
0174 
0175     public:
0176         /*! \internal */
0177         void deleteSubtree( const QModelIndex& );
0178 
0179         /*! Print the Gantt chart using \a printer. If \a drawRowLabels
0180          * is true (the default), each row will have it's label printed
0181          * on the left side. If \a drawColumnLabels is true (the
0182          * default), each column will have it's label printed at the
0183          * top side.
0184          *
0185          * This version of print() will print multiple pages.
0186          */
0187         void print( QPrinter* printer, bool drawRowLabels = true, bool drawColumnLabels = true );
0188         
0189         /*! Print part of the Gantt chart from \a start to \a end using \a printer.
0190          * If \a drawRowLabels is true (the default), each row will have it's
0191          * label printed on the left side. If \a drawColumnLabels is true (the
0192          * default), each column will have it's label printed at the
0193          * top side.
0194          *
0195          * This version of print() will print multiple pages.
0196          *
0197          * To print a certain range of a chart with a DateTimeGrid, use
0198          * qreal DateTimeGrid::mapFromDateTime( const QDateTime& dt) const
0199          * to figure out the values for \a start and \a end.
0200          */
0201         void print( QPrinter* printer, qreal start, qreal end, bool drawRowLabels = true, bool drawColumnLabels = true );
0202 
0203         /*! Render the GanttView inside the rectangle \a target using the painter \a painter.
0204          * If \a drawRowLabels is true (the default), each row will have it's
0205          * label printed on the left side. If \a drawColumnLabels is true (the
0206          * default), each column will have it's label printed at the
0207          * top side.
0208          */
0209         void print( QPainter* painter, const QRectF& target = QRectF(), bool drawRowLabels = true, bool drawColumnLabels = true );
0210 
0211         /*! Render the GanttView inside the rectangle \a target using the painter \a painter.
0212          * If \a drawRowLabels is true (the default), each row will have it's
0213          * label printed on the left side. If \a drawColumnLabels is true (the
0214          * default), each column will have it's label printed at the
0215          * top side.
0216          *
0217          * To print a certain range of a chart with a DateTimeGrid, use
0218          * qreal DateTimeGrid::mapFromDateTime( const QDateTime& dt) const
0219          * to figure out the values for \a start and \a end.
0220          */
0221         void print( QPainter* painter, qreal start, qreal end,
0222                     const QRectF& target = QRectF(), bool drawRowLabels = true, bool drawColumnLabels = true );
0223 
0224         /*! Print the Gantt chart on the \a printer in accordance with the PrintingContext \a context
0225          * 
0226          * \see PrintingContext
0227          * 
0228          * \since 2.8.0
0229          */
0230         void printDiagram( QPrinter *printer, const PrintingContext &context );
0231 
0232     public Q_SLOTS:
0233         /*! Sets the model to be displayed in this view to
0234          * \a model. The view does not take ownership of the model.
0235          *
0236          * To make a model work well with GraphicsView it must
0237          * have a certain layout. Whether the model is flat or has a
0238          * treestrucure is not important, as long as an
0239          * AbstractRowController is provided that can navigate the
0240          * model.
0241          *
0242          * GraphicsView operates per row in the model. The data is always
0243          * taken from the _last_ item in the row. The ItemRoles used are
0244          * Qt::DisplayRole and the roles defined in KGantt::ItemDataRole.
0245          * 
0246          * Note: This model is not returned by \a model()
0247          * 
0248          * \see GraphicsView::model
0249          */
0250         void setModel( QAbstractItemModel* );
0251         void setSummaryHandlingModel( QAbstractProxyModel* model );
0252 
0253         /*! Sets the constraintmodel displayed by this view.
0254          * \see KGantt::ConstraintModel.
0255          */
0256         void setConstraintModel( KGantt::ConstraintModel* );
0257 
0258         /*! Sets the root index of the model displayed by this view.
0259          * Similar to QAbstractItemView::setRootIndex, default is QModelIndex().
0260          */
0261         void setRootIndex( const QModelIndex& );
0262 
0263         /*! Sets the QItemSelectionModel used by this view to manage
0264          * selections. Similar to QAbstractItemView::setSelectionModel
0265          */
0266         void setSelectionModel( QItemSelectionModel* );
0267 
0268         /*! Sets the AbstractRowController used by this view. The
0269          * AbstractRowController deals with the height and position
0270          * of each row and with which parts of the model are
0271          * displayed. \see AbstractRowController
0272          */
0273         void setRowController( KGantt::AbstractRowController* );
0274 
0275         /*! Sets the AbstractGrid for this view. The grid is an
0276          * object that controls how QModelIndexes are mapped
0277          * to and from the view and how the background and header
0278          * is rendered. \see AbstractGrid and DateTimeGrid.
0279          */
0280         void setGrid( KGantt::AbstractGrid* );
0281 
0282         /*! Sets the KGantt::ItemDelegate used for rendering items on this
0283          * view. \see ItemDelegate and QAbstractItemDelegate.
0284          */
0285         void setItemDelegate( KGantt::ItemDelegate* delegate );
0286 
0287         /*! Sets the view to read-only mode if \a to is true. The default is
0288          * read/write if the model permits it.
0289          */
0290         void setReadOnly( bool );
0291 
0292     Q_SIGNALS:
0293         /*! \fn void GraphicsView::activated( const QModelIndex & index ) */
0294         void activated( const QModelIndex & index );
0295 
0296         /*! \fn void GraphicsView::clicked( const QModelIndex & index ); */
0297         void clicked( const QModelIndex & index );
0298 
0299         /*! \fn void GraphicsView::qrealClicked( const QModelIndex & index ); */
0300         void qrealClicked( const QModelIndex & index );
0301 
0302         /*! \fn void GraphicsView::entered( const QModelIndex & index ); */
0303         void entered( const QModelIndex & index );
0304 
0305         /*! \fn void GraphicsView::pressed( const QModelIndex & index ); */
0306         void pressed( const QModelIndex & index );
0307 
0308         /*! \fn void GraphicsView::headerContextMenuRequested( const QPoint& pt )
0309          * This signal is emitted when the header has contextMenuPolicy Qt::CustomContextMenu
0310          * and the widget wants to show a context menu for the header. Unlike in
0311          * QWidget::customContextMenuRequested() signal, \a pt is here in global coordinates.
0312          */
0313         void headerContextMenuRequested( const QPoint& pt );
0314 
0315     protected:
0316         /*! \internal */
0317         void clearItems();
0318         /*reimp*/void resizeEvent( QResizeEvent* ) override;
0319     private:
0320         friend class View;
0321     };
0322 }
0323 
0324 #endif /* KGANTTGRAPHICSVIEW_H */
0325