24 #include "kdganttgraphicsview_p.h" 31 #include <QPaintEvent> 32 #include <QResizeEvent> 34 #include <QAbstractProxyModel> 40 #include "../evaldialog/evaldialog.h" 50 :
QWidget( parent ), m_offset( 0. )
55 HeaderWidget::~HeaderWidget()
59 void HeaderWidget::scrollTo(
int v )
67 void HeaderWidget::paintEvent( QPaintEvent* ev )
70 view()->grid()->paintHeader( &p, rect(), ev->rect(), m_offset, this );
73 bool HeaderWidget::event( QEvent* event )
75 if ( event->type() == QEvent::ToolTip ) {
78 QHelpEvent *e =
static_cast<QHelpEvent*
>( event );
79 QDateTime dt = grid->
mapFromChart( view()->mapToScene( e->x(), 0 ).x() ).toDateTime();
80 setToolTip( dt.toString() );
83 return QWidget::event( event );
86 void HeaderWidget::contextMenuEvent( QContextMenuEvent* event )
91 QAction* actionScaleAuto = 0;
92 QAction* actionScaleMonth = 0;
93 QAction* actionScaleWeek = 0;
94 QAction* actionScaleDay = 0;
95 QAction* actionScaleHour = 0;
96 QAction* actionZoomIn = 0;
97 QAction* actionZoomOut = 0;
100 QMenu* menuScale =
new QMenu( tr(
"Scale" ), &contextMenu );
101 QActionGroup* scaleGroup =
new QActionGroup( &contextMenu );
102 scaleGroup->setExclusive(
true );
104 actionScaleAuto =
new QAction( tr(
"Auto" ), menuScale );
105 actionScaleAuto->setCheckable(
true );
107 actionScaleMonth =
new QAction( tr(
"Month" ), menuScale );
108 actionScaleMonth->setCheckable(
true );
110 actionScaleWeek =
new QAction( tr(
"Week" ), menuScale );
111 actionScaleWeek->setCheckable(
true );
113 actionScaleDay =
new QAction( tr(
"Day" ), menuScale );
114 actionScaleDay->setCheckable(
true );
116 actionScaleHour =
new QAction( tr(
"Hour" ), menuScale );
117 actionScaleHour->setCheckable(
true );
120 scaleGroup->addAction( actionScaleAuto );
121 menuScale->addAction( actionScaleAuto );
123 scaleGroup->addAction( actionScaleMonth );
124 menuScale->addAction( actionScaleMonth );
126 scaleGroup->addAction( actionScaleWeek );
127 menuScale->addAction( actionScaleWeek );
129 scaleGroup->addAction( actionScaleDay );
130 menuScale->addAction( actionScaleDay );
132 scaleGroup->addAction( actionScaleHour );
133 menuScale->addAction( actionScaleHour );
135 contextMenu.addMenu( menuScale );
137 contextMenu.addSeparator();
139 actionZoomIn =
new QAction( tr(
"Zoom In" ), &contextMenu );
140 contextMenu.addAction( actionZoomIn );
141 actionZoomOut =
new QAction( tr(
"Zoom Out" ), &contextMenu );
142 contextMenu.addAction( actionZoomOut );
145 if ( contextMenu.isEmpty() )
151 const QAction*
const action = contextMenu.exec( event->globalPos() );
152 if ( action == 0 ) {}
153 else if ( action == actionScaleAuto )
158 else if ( action == actionScaleMonth )
163 else if ( action == actionScaleWeek )
168 else if ( action == actionScaleDay )
173 else if ( action == actionScaleHour )
178 else if ( action == actionZoomIn )
183 else if ( action == actionZoomOut )
193 : q( _q ), rowcontroller(0), headerwidget( _q )
197 void GraphicsView::Private::updateHeaderGeometry()
199 q->setViewportMargins(0,rowcontroller->headerHeight(),0,0);
200 headerwidget.setGeometry( q->viewport()->x(),
201 q->viewport()->y() - rowcontroller->headerHeight(),
202 q->viewport()->width(),
203 rowcontroller->headerHeight() );
206 void GraphicsView::Private::slotGridChanged()
208 updateHeaderGeometry();
209 headerwidget.update();
210 q->updateSceneRect();
214 void GraphicsView::Private::slotHorizontalScrollValueChanged(
int val )
216 #if QT_VERSION >= 0x040300 217 const QRectF viewRect = q->transform().mapRect( q->sceneRect() );
219 const QRectF viewRect = q->sceneRect();
221 headerwidget.scrollTo( val-q->horizontalScrollBar()->minimum()+
static_cast<int>( viewRect.left() ) );
224 void GraphicsView::Private::slotColumnsInserted(
const QModelIndex& parent,
int start,
int end )
228 QModelIndex idx = scene.model()->index( 0, 0, scene.summaryHandlingModel()->mapToSource( parent ) );
230 scene.updateRow( scene.summaryHandlingModel()->mapFromSource( idx ) );
231 }
while ( ( idx = rowcontroller->indexBelow( idx ) ) != QModelIndex() && rowcontroller->isRowVisible( idx ) );
233 q->updateSceneRect();
236 void GraphicsView::Private::slotColumnsRemoved(
const QModelIndex& parent,
int start,
int end )
245 void GraphicsView::Private::slotDataChanged(
const QModelIndex& topLeft,
const QModelIndex& bottomRight )
248 const QModelIndex parent = topLeft.parent();
249 for (
int row = topLeft.row(); row <= bottomRight.row(); ++row ) {
250 scene.updateRow( scene.summaryHandlingModel()->index( row, 0, parent ) );
254 void GraphicsView::Private::slotLayoutChanged()
260 void GraphicsView::Private::slotModelReset()
266 void GraphicsView::Private::slotRowsInserted(
const QModelIndex& parent,
int start,
int end )
274 void GraphicsView::Private::slotRowsAboutToBeRemoved(
const QModelIndex& parent,
int start,
int end )
277 for (
int row = start; row <= end; ++row ) {
278 for (
int col = 0; col < scene.summaryHandlingModel()->columnCount( parent ); ++col ) {
280 scene.removeItem( scene.summaryHandlingModel()->index( row, col, parent ) );
285 void GraphicsView::Private::slotRowsRemoved(
const QModelIndex& parent,
int start,
int end )
296 void GraphicsView::Private::slotItemClicked(
const QModelIndex& idx )
298 QModelIndex sidx = idx;
299 emit q->clicked( sidx );
300 if (q->style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick, 0, q))
301 emit q->activated( sidx );
304 void GraphicsView::Private::slotItemDoubleClicked(
const QModelIndex& idx )
306 QModelIndex sidx = idx;
307 emit q->qrealClicked( sidx );
308 if (!q->style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick, 0, q))
309 emit q->activated( sidx );
312 void GraphicsView::Private::slotHeaderContextMenuRequested(
const QPoint& pt )
314 emit q->headerContextMenuRequested( headerwidget.mapToGlobal( pt ) );
342 #if defined KDAB_EVAL 343 EvalDialog::checkEvalLicense(
"KD Gantt" );
345 connect( horizontalScrollBar(), SIGNAL( valueChanged(
int ) ),
346 this, SLOT( slotHorizontalScrollValueChanged(
int ) ) );
347 connect( &_d->scene, SIGNAL( gridChanged() ),
348 this, SLOT( slotGridChanged() ) );
349 connect( &_d->scene, SIGNAL(
entered(
const QModelIndex& ) ),
350 this, SIGNAL(
entered(
const QModelIndex& ) ) );
351 connect( &_d->scene, SIGNAL(
pressed(
const QModelIndex& ) ),
352 this, SIGNAL(
pressed(
const QModelIndex& ) ) );
353 connect( &_d->scene, SIGNAL(
clicked(
const QModelIndex& ) ),
354 this, SLOT( slotItemClicked(
const QModelIndex& ) ) );
355 connect( &_d->scene, SIGNAL(
qrealClicked(
const QModelIndex& ) ),
356 this, SLOT( slotItemDoubleClicked(
const QModelIndex& ) ) );
357 connect( &_d->scene, SIGNAL( sceneRectChanged(
const QRectF& ) ),
359 connect( &_d->headerwidget, SIGNAL( customContextMenuRequested(
const QPoint& ) ),
360 this, SLOT( slotHeaderContextMenuRequested(
const QPoint& ) ) );
361 setScene( &_d->scene );
368 setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
396 if (
d->scene.model() ) {
397 disconnect(
d->scene.model() );
400 d->scene.setModel( model );
401 connect( model, SIGNAL( dataChanged(
const QModelIndex&,
const QModelIndex& ) ),
410 return d->scene.model();
415 disconnect(
d->scene.summaryHandlingModel() );
416 d->scene.setSummaryHandlingModel( proxyModel );
421 connect( proxyModel, SIGNAL( columnsInserted(
const QModelIndex&,
int,
int ) ),
422 this, SLOT( slotColumnsInserted(
const QModelIndex&,
int,
int ) ) );
423 connect( proxyModel, SIGNAL( columnsRemoved(
const QModelIndex&,
int,
int ) ),
424 this, SLOT( slotColumnsRemoved(
const QModelIndex&,
int,
int ) ) );
425 connect( proxyModel, SIGNAL( dataChanged(
const QModelIndex&,
const QModelIndex& ) ),
426 this, SLOT( slotDataChanged(
const QModelIndex&,
const QModelIndex& ) ) );
427 connect( proxyModel, SIGNAL( layoutChanged() ),
428 this, SLOT( slotLayoutChanged() ) );
429 connect( proxyModel, SIGNAL( modelReset() ),
430 this, SLOT( slotModelReset() ) );
431 connect( proxyModel, SIGNAL( rowsInserted(
const QModelIndex&,
int,
int ) ),
432 this, SLOT( slotRowsInserted(
const QModelIndex&,
int,
int ) ) );
433 connect( proxyModel, SIGNAL( rowsAboutToBeRemoved(
const QModelIndex&,
int,
int ) ),
434 this, SLOT( slotRowsAboutToBeRemoved(
const QModelIndex&,
int,
int ) ) );
435 connect( proxyModel, SIGNAL( rowsRemoved(
const QModelIndex&,
int,
int ) ),
436 this, SLOT( slotRowsRemoved(
const QModelIndex&,
int,
int ) ) );
446 d->scene.setConstraintModel( cmodel );
453 return d->scene.constraintModel();
460 return d->scene.summaryHandlingModel();
468 d->scene.setRootIndex( idx );
475 return d->scene.rootIndex();
483 d->scene.setSelectionModel( model );
490 return d->scene.selectionModel();
498 d->scene.setItemDelegate( delegate );
505 return d->scene.itemDelegate();
515 d->rowcontroller = rowcontroller;
516 d->scene.setRowController( rowcontroller );
525 return d->rowcontroller;
535 d->scene.setGrid( grid );
536 d->slotGridChanged();
543 return d->scene.grid();
551 d->scene.setReadOnly( ro );
558 return d->scene.isReadOnly();
572 d->headerwidget.setContextMenuPolicy( p );
579 return d->headerwidget.contextMenuPolicy();
591 const QModelIndex& to,
592 Qt::KeyboardModifiers modifiers )
604 d->updateHeaderGeometry();
605 QRectF r = scene()->itemsBoundingRect();
607 r.setLeft( qMin<qreal>( 0.0, r.left() ) );
610 QSizeF size = viewport()->size();
612 if ( size.width() > r.width() ) {
613 r.setWidth( size.width() - 2 );
615 if ( size.height() > r.height() ) {
616 r.setHeight( size.height() - 2 );
619 if ( r.height() < totalh ) {
620 r.setHeight( totalh );
623 scene()->setSceneRect( r );
625 QGraphicsView::resizeEvent( ev );
637 if (
GraphicsItem* gitem = qgraphicsitem_cast<GraphicsItem*>( item ) ) {
638 return d->scene.summaryHandlingModel()->mapToSource( gitem->index() );
640 return QModelIndex();
647 d->scene.clearItems();
653 d->scene.updateRow(
d->scene.summaryHandlingModel()->mapFromSource( idx ) );
664 qreal range = horizontalScrollBar()->maximum()-horizontalScrollBar()->minimum();
665 const qreal hscroll = horizontalScrollBar()->value()/( range>0?range:1 );
666 QRectF r =
d->scene.itemsBoundingRect();
669 r.setLeft( qMin<qreal>( 0.0, r.left() ) );
670 r.setSize( r.size().expandedTo( viewport()->size() ) );
672 if ( r.height() < totalh ) r.setHeight( totalh );
673 d->scene.setSceneRect( r );
676 range = horizontalScrollBar()->maximum()-horizontalScrollBar()->minimum();
677 if ( range>0 ) horizontalScrollBar()->setValue( qRound( hscroll*range ) );
683 d->scene.invalidate( QRectF(), QGraphicsScene::BackgroundLayer );
692 if ( !
model())
return;
701 if ( scene() ) scene()->invalidate( QRectF(), QGraphicsScene::BackgroundLayer );
707 return d->scene.createItem( type );
713 d->scene.deleteSubtree(
d->scene.summaryHandlingModel()->mapFromSource( idx ) );
726 d->scene.print( printer, drawRowLabels, drawColumnLabels );
741 void GraphicsView::print( QPrinter* printer, qreal start, qreal end,
bool drawRowLabels,
bool drawColumnLabels )
743 d->scene.print( printer, start, end, drawRowLabels, drawColumnLabels );
752 void GraphicsView::print( QPainter* painter,
const QRectF& targetRect,
bool drawRowLabels,
bool drawColumnLabels )
754 d->scene.print(painter, targetRect, drawRowLabels, drawColumnLabels);
768 const QRectF& targetRect,
bool drawRowLabels,
bool drawColumnLabels )
770 d->scene.print(painter, start, end, targetRect, drawRowLabels, drawColumnLabels);
774 #include "moc_kdganttgraphicsview.cpp" QAbstractProxyModel * summaryHandlingModel() const
void setConstraintModel(ConstraintModel *)
QModelIndex rootIndex() const
virtual void addConstraint(const QModelIndex &from, const QModelIndex &to, Qt::KeyboardModifiers modifiers)
bool hasConstraint(const Constraint &c) const
virtual int totalHeight() const =0
Abstract baseclass for grids. A grid is used to convert between QModelIndex'es and gantt chart values...
void setItemDelegate(ItemDelegate *delegate)
GraphicsView(QWidget *parent=0)
AbstractRowController * rowController() const
void deleteSubtree(const QModelIndex &)
void setModel(QAbstractItemModel *)
void print(QPrinter *printer, bool drawRowLabels=true, bool drawColumnLabels=true)
A class used to represent a dependency.
void qrealClicked(const QModelIndex &index)
Class used to render gantt items in a KDGantt::GraphicsView.
virtual bool removeConstraint(const Constraint &c)
virtual void addConstraint(const Constraint &c)
Subclassing ConstraintModel and overriding addConstraint() and removeConstraint() can provide re-entr...
void setGrid(AbstractGrid *)
bool mapFromChart(const Span &span, const QModelIndex &idx, const QList< Constraint > &constraints=QList< Constraint >()) const override
void setRowController(AbstractRowController *)
ItemDelegate * itemDelegate() const
Class only listed here to document inheritance of some KDChart classes.
Qt::ContextMenuPolicy headerContextMenuPolicy() const
The GraphicsView class provides a model/view implementation of a gantt chart.
void setRootIndex(const QModelIndex &)
AbstractGrid * grid() const
void updateRow(const QModelIndex &)
void entered(const QModelIndex &index)
QItemSelectionModel * selectionModel() const
void setHeaderContextMenuPolicy(Qt::ContextMenuPolicy)
void clicked(const QModelIndex &index)
void setSummaryHandlingModel(QAbstractProxyModel *model)
void setSelectionModel(QItemSelectionModel *)
QAbstractItemModel * model() const
ConstraintModel * constraintModel() const
AbstractGrid * grid() const
QModelIndex indexAt(const QPoint &pos) const
void resizeEvent(QResizeEvent *) override
Abstract baseclass for row controllers. A row controller is used by the GraphicsView to nagivate the ...
void pressed(const QModelIndex &index)