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

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 KGANTTVIEW_H
0010 #define KGANTTVIEW_H
0011 
0012 #include <QWidget>
0013 #include <QModelIndex>
0014 #include "kganttglobal.h"
0015 #include "kganttprintingcontext.h"
0016 
0017 QT_BEGIN_NAMESPACE
0018 class QAbstractItemModel;
0019 class QAbstractProxyModel;
0020 class QAbstractItemView;
0021 class QItemSelectionModel;
0022 class QPrinter;
0023 class QSplitter;
0024 QT_END_NAMESPACE
0025 
0026 namespace KGantt {
0027     class ItemDelegate;
0028     class Constraint;
0029     class ConstraintModel;
0030     class AbstractGrid;
0031     class GraphicsView;
0032     class AbstractRowController;
0033 
0034 
0035     /*!\class KGantt::View kganttview.h KGanttView
0036      * \ingroup KGantt
0037      * \brief This widget that consists of a QTreeView and a GraphicsView
0038      *
0039      * This is the easy to use, complete gantt chart widget. It
0040      * consists of a QTreeView on the left and a KGantt::GraphicsView
0041      * on the right separated by a QSplitter. The two views share the same
0042      * model.
0043      */
0044     class KGANTT_EXPORT View : public QWidget {
0045         Q_OBJECT
0046         KGANTT_DECLARE_PRIVATE_BASE_POLYMORPHIC_QWIDGET(View)
0047         Q_PRIVATE_SLOT( d, void slotCollapsed(const QModelIndex&) )
0048         Q_PRIVATE_SLOT( d, void slotExpanded(const QModelIndex&) )
0049         Q_PRIVATE_SLOT( d, void slotVerticalScrollValueChanged( int ) )
0050         Q_PRIVATE_SLOT( d, void slotLeftWidgetVerticalRangeChanged( int, int ) )
0051         Q_PRIVATE_SLOT( d, void slotGfxViewVerticalRangeChanged( int, int ) )
0052 
0053     public:
0054         /*! Constructor. Creates a View with parent \a parent,
0055          * a DateTimeGrid as default grid implementation and no model etc.
0056          */
0057         explicit View(QWidget* parent = nullptr);
0058         ~View() override;
0059 
0060         /*! \returns the current model displayed by this view
0061          */
0062         QAbstractItemModel* model() const;
0063 
0064         /*! \returns the QItemSelectionModel used by this view
0065          */
0066         QItemSelectionModel* selectionModel() const;
0067 
0068         /*! \returns the ItemDelegate used by this view to render items
0069         */
0070         ItemDelegate* itemDelegate() const;
0071 
0072         /*! \returns the KGantt::ConstraintModel displayed by this view.
0073          */
0074         ConstraintModel* constraintModel() const;
0075 
0076         /*! \returns the AbstractGrid used by this view.
0077          */
0078         AbstractGrid* grid() const;
0079 
0080         /*! \returns the rootindex for this view.
0081          */
0082         QModelIndex rootIndex() const;
0083 
0084         /*!\returns The QModelIndex for the item located at
0085          * position \a pos in the view or an invalid index
0086          * if no item was present at that position.
0087          *
0088          * \see GraphicsView::indexAt
0089          */
0090         QModelIndex indexAt( const QPoint& pos ) const;
0091 
0092 
0093 
0094         /*! Replaces the left widget with a custom QAbstractItemView.
0095          *
0096          * \param aiv The view to be used to the left, instead of the default tree view
0097          * \sa setRowController()
0098          */
0099         void setLeftView( QAbstractItemView* );
0100 
0101         /*!
0102          * \returns a pointer to the QAbstractItemView in the left
0103          * part of the widget.
0104          * */
0105         const QAbstractItemView* leftView() const;
0106 
0107         /*!
0108          * \overload const QAbstractItemView* KGantt::View::leftView() const
0109          */
0110         QAbstractItemView* leftView();
0111 
0112 
0113 
0114         /*!
0115          * \overload const QSplitter* KGantt::View::splitter() const
0116          */
0117         const QSplitter* splitter() const;
0118         /*!
0119          * \returns a pointer to the QSplitter that manages the left view and graphicsView
0120          */
0121         QSplitter* splitter();
0122 
0123         /*! Sets \a ctrl to be the rowcontroller used by this View.
0124          * The default rowcontroller is owned by KGantt::View and is
0125          * suitable for the default treeview in the left part of the view.
0126          * You probably only want to change this if you replace the treeview.
0127          */
0128         void setRowController( AbstractRowController* );
0129 
0130 
0131         /*! \returns a pointer to the current rowcontroller.
0132          * \see AbstractRowController
0133          */
0134         AbstractRowController* rowController();
0135         /*! \overload AbstractRowController* KGantt::View::rowController()
0136          */
0137         const AbstractRowController* rowController() const;
0138 
0139         /*! Set the GraphicsView to be used for this View. It only makes sense to call this
0140          * if you need to subclass GraphicsView.
0141          *
0142          * NOTE: _Only_ call this right after creating the View, before setting a model or any other
0143          * attributes.
0144          */
0145         void setGraphicsView( GraphicsView* );
0146 
0147         /*!
0148          * \overload const GraphicsView* KGantt::View::graphicsView() const
0149          */
0150         const GraphicsView* graphicsView() const;
0151 
0152         /*!
0153          * \returns a pointer to the GraphicsView
0154          */
0155         GraphicsView* graphicsView();
0156         const QAbstractProxyModel* ganttProxyModel() const;
0157         QAbstractProxyModel* ganttProxyModel();
0158 
0159         void ensureVisible(const QModelIndex& index);
0160 
0161         /*! Print the Gantt chart using \a printer. If \a drawRowLabels
0162          * is true (the default), each row will have it's label printed
0163          * 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          * This version of print() will print multiple pages.
0168          */
0169         void print( QPrinter* printer, bool drawRowLabels=true, bool drawColumnLabels=true );
0170 
0171         /*! Print part of the Gantt chart from \a start to \a end using \a printer.
0172          * If \a drawRowLabels is true (the default), each row will have it's
0173          * label printed on the left side. If \a drawColumnLabels is true (the
0174          * default), each column will have it's label printed at the
0175          * top side.
0176          *
0177          * This version of print() will print multiple pages.
0178          *
0179          * To print a certain range of a chart with a DateTimeGrid, use
0180          * qreal DateTimeGrid::mapFromDateTime( const QDateTime& dt) const
0181          * to figure out the values for \a start and \a end.
0182          */
0183         void print( QPrinter* printer, qreal start, qreal end, bool drawRowLabels=true, bool drawColumnLabels=true );
0184 
0185         /*! Render the GanttView inside the rectangle \a target using the painter \a painter.
0186          * If \a drawRowLabels is true (the default), each row will have it's
0187          * label printed on the left side. If \a drawColumnLabels is true (the
0188          * default), each column will have it's label printed at the
0189          * top side.
0190          */
0191         void print( QPainter* painter, const QRectF& target = QRectF(), bool drawRowLabels=true, bool drawColumnLabels=true);
0192 
0193         /*! Render the GanttView inside the rectangle \a target using the painter \a painter.
0194          * If \a drawRowLabels is true (the default), each row will have it's
0195          * label printed on the left side. If \a drawColumnLabels is true (the
0196          * default), each column will have it's label printed at the
0197          * top side.
0198          *
0199          * To print a certain range of a chart with a DateTimeGrid, use
0200          * qreal DateTimeGrid::mapFromDateTime( const QDateTime& dt) const
0201          * to figure out the values for \a start and \a end.
0202          */
0203         void print( QPainter* painter, qreal start, qreal end,
0204                     const QRectF& target = QRectF(), bool drawRowLabels=true, bool drawColumnLabels=true);
0205 
0206         /*! Print the Gantt chart on the \a printer in accordance with the PrintingContext \a context
0207          * 
0208          * \see PrintingContext
0209          * 
0210          * \since 2.8.0
0211          */
0212         void printDiagram( QPrinter *printer, const PrintingContext &context = PrintingContext() );
0213 
0214     public Q_SLOTS:
0215         /*! Sets the QAbstractItemModel to be displayed in this view
0216          * to \a model.
0217          *
0218          * \see GraphicsView::setModel
0219          */
0220         void setModel(QAbstractItemModel* model);
0221 
0222         /*! Sets the root index of the model displayed by this view.
0223          * Similar to QAbstractItemView::setRootIndex, default is QModelIndex().
0224          */
0225         void setRootIndex( const QModelIndex& idx );
0226 
0227         /*! Sets the QItemSelectionModel used by this view to manage
0228          * selections. Similar to QAbstractItemView::setSelectionModel
0229          */
0230         void setSelectionModel( QItemSelectionModel* smodel );
0231 
0232         /*! Sets the KGantt::ItemDelegate used for rendering items on this
0233          * view. \see ItemDelegate and QAbstractItemDelegate.
0234          */
0235         void setItemDelegate( KGantt::ItemDelegate* );
0236 
0237         /*! Sets the constraintmodel displayed by this view.
0238          * \see KGantt::ConstraintModel.
0239          */
0240         void setConstraintModel( KGantt::ConstraintModel* );
0241 
0242         /*! Sets the AbstractGrid for this view. The grid is an
0243          * object that controls how QModelIndexes are mapped
0244          * to and from the view and how the background and header
0245          * is rendered. \see AbstractGrid and DateTimeGrid.
0246          */
0247         void setGrid( KGantt::AbstractGrid* );
0248         void expandAll( QModelIndex index = QModelIndex() );
0249         void collapseAll( QModelIndex index = QModelIndex() );
0250 
0251     protected:
0252         /*reimp*/ void resizeEvent(QResizeEvent*) override;
0253     };
0254 }
0255 
0256 #endif /* KGANTTVIEW_H */