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 ) {
76 DateTimeGrid*
const grid = qobject_cast< DateTimeGrid* >( view()->grid() );
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 )
90 DateTimeGrid*
const grid = qobject_cast< DateTimeGrid* >( view()->grid() );
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 );
106 actionScaleAuto->setChecked( grid->scale() == DateTimeGrid::ScaleAuto );
107 actionScaleMonth =
new QAction( tr(
"Month" ), menuScale );
108 actionScaleMonth->setCheckable(
true );
109 actionScaleMonth->setChecked( grid->scale() == DateTimeGrid::ScaleMonth );
110 actionScaleWeek =
new QAction( tr(
"Week" ), menuScale );
111 actionScaleWeek->setCheckable(
true );
112 actionScaleWeek->setChecked( grid->scale() == DateTimeGrid::ScaleWeek );
113 actionScaleDay =
new QAction( tr(
"Day" ), menuScale );
114 actionScaleDay->setCheckable(
true );
115 actionScaleDay->setChecked( grid->scale() == DateTimeGrid::ScaleDay );
116 actionScaleHour =
new QAction( tr(
"Hour" ), menuScale );
117 actionScaleHour->setCheckable(
true );
118 actionScaleHour->setChecked( grid->scale() == DateTimeGrid::ScaleHour );
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 )
156 grid->setScale( DateTimeGrid::ScaleAuto );
158 else if ( action == actionScaleMonth )
161 grid->setScale( DateTimeGrid::ScaleMonth );
163 else if ( action == actionScaleWeek )
166 grid->setScale( DateTimeGrid::ScaleWeek );
168 else if ( action == actionScaleDay )
171 grid->setScale( DateTimeGrid::ScaleDay );
173 else if ( action == actionScaleHour )
176 grid->setScale( DateTimeGrid::ScaleHour );
178 else if ( action == actionZoomIn )
181 grid->setDayWidth( qMax(0.1, grid->dayWidth() + grid->dayWidth() * 0.2) );
183 else if ( action == actionZoomOut )
186 grid->setDayWidth( qMax(0.1, grid->dayWidth() - grid->dayWidth() * 0.2) );
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 const QRectF viewRect = q->transform().mapRect( q->sceneRect() );
217 headerwidget.scrollTo( val-q->horizontalScrollBar()->minimum()+
static_cast<int>( viewRect.left() ) );
220 void GraphicsView::Private::slotColumnsInserted(
const QModelIndex& parent,
int start,
int end )
224 QModelIndex idx = scene.model()->index( 0, 0, scene.summaryHandlingModel()->mapToSource( parent ) );
226 scene.updateRow( scene.summaryHandlingModel()->mapFromSource( idx ) );
227 }
while ( ( idx = rowcontroller->indexBelow( idx ) ) != QModelIndex() && rowcontroller->isRowVisible( idx ) );
229 q->updateSceneRect();
232 void GraphicsView::Private::slotColumnsRemoved(
const QModelIndex& parent,
int start,
int end )
241 void GraphicsView::Private::slotDataChanged(
const QModelIndex& topLeft,
const QModelIndex& bottomRight )
244 const QModelIndex parent = topLeft.parent();
245 for (
int row = topLeft.row(); row <= bottomRight.row(); ++row ) {
246 scene.updateRow( scene.summaryHandlingModel()->index( row, 0, parent ) );
250 void GraphicsView::Private::slotLayoutChanged()
256 void GraphicsView::Private::slotModelReset()
262 void GraphicsView::Private::slotRowsInserted(
const QModelIndex& parent,
int start,
int end )
270 void GraphicsView::Private::slotRowsAboutToBeRemoved(
const QModelIndex& parent,
int start,
int end )
273 for (
int row = start; row <= end; ++row ) {
274 for (
int col = 0; col < scene.summaryHandlingModel()->columnCount( parent ); ++col ) {
276 scene.removeItem( scene.summaryHandlingModel()->index( row, col, parent ) );
281 void GraphicsView::Private::slotRowsRemoved(
const QModelIndex& parent,
int start,
int end )
292 void GraphicsView::Private::slotItemClicked(
const QModelIndex& idx )
294 QModelIndex sidx = idx;
295 emit q->clicked( sidx );
296 if (q->style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick, 0, q))
297 emit q->activated( sidx );
300 void GraphicsView::Private::slotItemDoubleClicked(
const QModelIndex& idx )
302 QModelIndex sidx = idx;
303 emit q->qrealClicked( sidx );
304 if (!q->style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick, 0, q))
305 emit q->activated( sidx );
308 void GraphicsView::Private::slotHeaderContextMenuRequested(
const QPoint& pt )
310 emit q->headerContextMenuRequested( headerwidget.mapToGlobal( pt ) );
338 #if defined KDAB_EVAL
339 EvalDialog::checkEvalLicense(
"KD Gantt" );
341 connect( horizontalScrollBar(), SIGNAL( valueChanged(
int ) ),
342 this, SLOT( slotHorizontalScrollValueChanged(
int ) ) );
343 connect( &_d->scene, SIGNAL( gridChanged() ),
344 this, SLOT( slotGridChanged() ) );
345 connect( &_d->scene, SIGNAL(
entered(
const QModelIndex& ) ),
346 this, SIGNAL(
entered(
const QModelIndex& ) ) );
347 connect( &_d->scene, SIGNAL(
pressed(
const QModelIndex& ) ),
348 this, SIGNAL(
pressed(
const QModelIndex& ) ) );
349 connect( &_d->scene, SIGNAL(
clicked(
const QModelIndex& ) ),
350 this, SLOT( slotItemClicked(
const QModelIndex& ) ) );
351 connect( &_d->scene, SIGNAL(
qrealClicked(
const QModelIndex& ) ),
352 this, SLOT( slotItemDoubleClicked(
const QModelIndex& ) ) );
353 connect( &_d->scene, SIGNAL( sceneRectChanged(
const QRectF& ) ),
355 connect( &_d->headerwidget, SIGNAL( customContextMenuRequested(
const QPoint& ) ),
356 this, SLOT( slotHeaderContextMenuRequested(
const QPoint& ) ) );
357 setScene( &_d->scene );
364 setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
392 if (
d->scene.model() ) {
393 disconnect(
d->scene.model() );
396 d->scene.setModel(
model );
397 connect(
model, SIGNAL( dataChanged(
const QModelIndex&,
const QModelIndex& ) ),
406 return d->scene.model();
411 disconnect(
d->scene.summaryHandlingModel() );
412 d->scene.setSummaryHandlingModel( proxyModel );
417 connect( proxyModel, SIGNAL( columnsInserted(
const QModelIndex&,
int,
int ) ),
418 this, SLOT( slotColumnsInserted(
const QModelIndex&,
int,
int ) ) );
419 connect( proxyModel, SIGNAL( columnsRemoved(
const QModelIndex&,
int,
int ) ),
420 this, SLOT( slotColumnsRemoved(
const QModelIndex&,
int,
int ) ) );
421 connect( proxyModel, SIGNAL( dataChanged(
const QModelIndex&,
const QModelIndex& ) ),
422 this, SLOT( slotDataChanged(
const QModelIndex&,
const QModelIndex& ) ) );
423 connect( proxyModel, SIGNAL( layoutChanged() ),
424 this, SLOT( slotLayoutChanged() ) );
425 connect( proxyModel, SIGNAL( modelReset() ),
426 this, SLOT( slotModelReset() ) );
427 connect( proxyModel, SIGNAL( rowsInserted(
const QModelIndex&,
int,
int ) ),
428 this, SLOT( slotRowsInserted(
const QModelIndex&,
int,
int ) ) );
429 connect( proxyModel, SIGNAL( rowsAboutToBeRemoved(
const QModelIndex&,
int,
int ) ),
430 this, SLOT( slotRowsAboutToBeRemoved(
const QModelIndex&,
int,
int ) ) );
431 connect( proxyModel, SIGNAL( rowsRemoved(
const QModelIndex&,
int,
int ) ),
432 this, SLOT( slotRowsRemoved(
const QModelIndex&,
int,
int ) ) );
442 d->scene.setConstraintModel( cmodel );
449 return d->scene.constraintModel();
456 return d->scene.summaryHandlingModel();
464 d->scene.setRootIndex( idx );
471 return d->scene.rootIndex();
479 d->scene.setSelectionModel(
model );
486 return d->scene.selectionModel();
494 d->scene.setItemDelegate( delegate );
501 return d->scene.itemDelegate();
511 d->rowcontroller = rowcontroller;
512 d->scene.setRowController( rowcontroller );
521 return d->rowcontroller;
531 d->scene.setGrid(
grid );
532 d->slotGridChanged();
539 return d->scene.grid();
547 d->scene.setReadOnly( ro );
554 return d->scene.isReadOnly();
568 d->headerwidget.setContextMenuPolicy( p );
575 return d->headerwidget.contextMenuPolicy();
587 const QModelIndex& to,
588 Qt::KeyboardModifiers modifiers )
600 d->updateHeaderGeometry();
601 QRectF r = scene()->itemsBoundingRect();
603 r.setLeft( qMin<qreal>( 0.0, r.left() ) );
606 QSizeF size = viewport()->size();
608 if ( size.width() > r.width() ) {
609 r.setWidth( size.width() - 2 );
611 if ( size.height() > r.height() ) {
612 r.setHeight( size.height() - 2 );
615 if ( r.height() < totalh ) {
616 r.setHeight( totalh );
619 scene()->setSceneRect( r );
621 QGraphicsView::resizeEvent( ev );
633 if (
GraphicsItem* gitem = qgraphicsitem_cast<GraphicsItem*>( item ) ) {
634 return d->scene.summaryHandlingModel()->mapToSource( gitem->index() );
636 return QModelIndex();
643 d->scene.clearItems();
649 d->scene.updateRow(
d->scene.summaryHandlingModel()->mapFromSource( idx ) );
660 qreal range = horizontalScrollBar()->maximum()-horizontalScrollBar()->minimum();
661 const qreal hscroll = horizontalScrollBar()->value()/( range>0?range:1 );
662 QRectF r =
d->scene.itemsBoundingRect();
665 r.setLeft( qMin<qreal>( 0.0, r.left() ) );
666 r.setSize( r.size().expandedTo( viewport()->size() ) );
668 if ( r.height() < totalh ) r.setHeight( totalh );
669 d->scene.setSceneRect( r );
672 range = horizontalScrollBar()->maximum()-horizontalScrollBar()->minimum();
673 if ( range>0 ) horizontalScrollBar()->setValue( qRound( hscroll*range ) );
679 d->scene.invalidate( QRectF(), QGraphicsScene::BackgroundLayer );
688 if ( !
model())
return;
697 if ( scene() ) scene()->invalidate( QRectF(), QGraphicsScene::BackgroundLayer );
703 return d->scene.createItem( type );
709 d->scene.deleteSubtree(
d->scene.summaryHandlingModel()->mapFromSource( idx ) );
722 d->scene.print( printer, drawRowLabels, drawColumnLabels );
737 void GraphicsView::print( QPrinter* printer, qreal start, qreal end,
bool drawRowLabels,
bool drawColumnLabels )
739 d->scene.print( printer, start, end, drawRowLabels, drawColumnLabels );
748 void GraphicsView::print( QPainter* painter,
const QRectF& targetRect,
bool drawRowLabels,
bool drawColumnLabels )
750 d->scene.print(painter, targetRect, drawRowLabels, drawColumnLabels);
764 const QRectF& targetRect,
bool drawRowLabels,
bool drawColumnLabels )
766 d->scene.print(painter, start, end, targetRect, drawRowLabels, drawColumnLabels);
770 #include "moc_kdganttgraphicsview.cpp"