• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

kdgantt2

  • sources
  • kde-4.12
  • kdepim
  • kdgantt2
kdganttview.cpp
Go to the documentation of this file.
1 /****************************************************************************
2  ** Copyright (C) 2001-2006 Klarälvdalens Datakonsult AB. All rights reserved.
3  **
4  ** This file is part of the KD Gantt library.
5  **
6  ** This file may be distributed and/or modified under the terms of the
7  ** GNU General Public License version 2 as published by the Free Software
8  ** Foundation and appearing in the file LICENSE.GPL included in the
9  ** packaging of this file.
10  **
11  ** Licensees holding valid commercial KD Gantt licenses may use this file in
12  ** accordance with the KD Gantt Commercial License Agreement provided with
13  ** the Software.
14  **
15  ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
17  **
18  ** See http://www.kdab.net/kdgantt for
19  ** information about KD Gantt Commercial License Agreements.
20  **
21  ** Contact info@kdab.net if any conditions of this
22  ** licensing are not clear to you.
23  **
24  **********************************************************************/
25 #include "kdganttview.h"
26 #include "kdganttview_p.h"
27 
28 #include "kdganttitemdelegate.h"
29 #include "kdganttgraphicsitem.h"
30 
31 #include <QAbstractItemModel>
32 #include <QHeaderView>
33 #include <QVBoxLayout>
34 #include <QGraphicsItem>
35 #include <QGraphicsRectItem>
36 #include <QScrollBar>
37 #include <QPaintEvent>
38 
39 #include <QDebug>
40 
41 #include <cassert>
42 
43 #if defined KDAB_EVAL
44 #include "../evaldialog/evaldialog.h"
45 #endif
46 
47 using namespace KDGantt;
48 
49 namespace {
50  class HeaderView : public QHeaderView {
51  public:
52  explicit HeaderView( QWidget* parent=0 ) : QHeaderView( Qt::Horizontal, parent ) {
53  }
54 
55  QSize sizeHint() const { QSize s = QHeaderView::sizeHint(); s.rheight() *= 2; return s; }
56  };
57 }
58 
59 KDGanttTreeView::KDGanttTreeView( QAbstractProxyModel* proxy, QWidget* parent )
60  : QTreeView( parent ),
61  m_controller( this, proxy )
62 {
63  setHeader( new HeaderView );
64 }
65 
66 KDGanttTreeView::~KDGanttTreeView()
67 {
68 }
69 
70 View::Private::Private(View* v)
71  : q(v),
72  splitter(v),
73  rowController(0),
74  gfxview(&splitter),
75  model(0)
76 {
77  //init();
78 }
79 
80 View::Private::~Private()
81 {
82 }
83 
84 void View::Private::init()
85 {
86  KDGanttTreeView* tw = new KDGanttTreeView( &ganttProxyModel, &splitter );
87  tw->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
88  tw->setVerticalScrollMode( QAbstractItemView::ScrollPerPixel );
89 
90  q->setLeftView( tw );
91  q->setRowController( tw->rowController() );
92 
93  gfxview.setAlignment(Qt::AlignTop|Qt::AlignLeft);
94  //gfxview.setRenderHints( QPainter::Antialiasing );
95 
96  tw->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
97  gfxview.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
98 
99  QVBoxLayout* layout = new QVBoxLayout(q);
100  layout->setMargin(0);
101  layout->addWidget(&splitter);
102  q->setLayout(layout);
103 
104  constraintProxy.setProxyModel( &ganttProxyModel );
105  constraintProxy.setDestinationModel( &mappedConstraintModel );
106  gfxview.setSelectionModel( leftWidget->selectionModel() );
107  gfxview.setConstraintModel( &mappedConstraintModel );
108 }
109 
110 void View::Private::updateScene()
111 {
112  gfxview.clearItems();
113  if( !model) return;
114 
115  if( QTreeView* tw = qobject_cast<QTreeView*>(leftWidget)) {
116  QModelIndex idx = ganttProxyModel.mapFromSource( model->index( 0, 0, leftWidget->rootIndex() ) );
117  do {
118  gfxview.updateRow( idx );
119  } while ( ( idx = tw->indexBelow( idx ) ) != QModelIndex() &&
120  gfxview.rowController()->isRowVisible(idx) );
121  gfxview.updateSceneRect();
122  } else {
123  const QModelIndex rootidx = ganttProxyModel.mapFromSource( leftWidget->rootIndex() );
124  for( int r = 0; r < ganttProxyModel.rowCount(rootidx); ++r ) {
125  gfxview.updateRow( ganttProxyModel.index( r, 0, rootidx ) );
126  }
127  }
128 }
129 
130 void View::Private::slotCollapsed(const QModelIndex& _idx)
131 {
132  QTreeView* tw = qobject_cast<QTreeView*>(leftWidget);
133  if(!tw) return;
134 
135  bool blocked = gfxview.blockSignals( true );
136 
137  QModelIndex idx( _idx );
138  const QAbstractItemModel* model = leftWidget->model();
139  const QModelIndex pidx = ganttProxyModel.mapFromSource(idx);
140  bool isMulti = false;
141  for( QModelIndex treewalkidx = pidx; treewalkidx.isValid(); treewalkidx = treewalkidx.parent() ) {
142  if ( treewalkidx.data( ItemTypeRole ).toInt() == TypeMulti
143  && !gfxview.rowController()->isRowExpanded( treewalkidx ) ) {
144  isMulti = true;
145  break;
146  }
147  }
148 
149  if ( !isMulti ) {
150  for ( int i = 0; i < model->rowCount( idx ); ++i ) {
151  gfxview.deleteSubtree( ganttProxyModel.index( i, 0, pidx ) );
152  }
153  } else {
154  gfxview.updateRow(pidx);
155  }
156  //qDebug() << "Looking to update from " << idx;
157  while ( ( idx=tw->indexBelow( idx ) ) != QModelIndex() &&
158  gfxview.rowController()->isRowVisible( ganttProxyModel.mapFromSource(idx) ) ) {
159  const QModelIndex proxyidx( ganttProxyModel.mapFromSource( idx ) );
160  gfxview.updateRow(proxyidx);
161  }
162  gfxview.blockSignals( blocked );
163  gfxview.updateSceneRect();
164 }
165 
166 void View::Private::slotExpanded(const QModelIndex& _idx)
167 {
168  QModelIndex idx( ganttProxyModel.mapFromSource( _idx ) );
169  do {
170  //qDebug() << "Updating row" << idx << idx.data( Qt::DisplayRole ).toString();
171  gfxview.updateRow(idx);
172  } while( ( idx=gfxview.rowController()->indexBelow( idx ) ) != QModelIndex()
173  && gfxview.rowController()->isRowVisible( idx ) );
174  gfxview.updateSceneRect();
175 }
176 
177 void View::Private::slotVerticalScrollValueChanged( int val )
178 {
179 #if 0
180  qDebug() << "View::Private::slotVerticalScrollValueChanged("<<val<<")="
181  << val/gfxview.verticalScrollBar()->singleStep();
182 #endif
183  leftWidget->verticalScrollBar()->setValue( val/gfxview.verticalScrollBar()->singleStep() );
184 }
185 
186 void View::Private::slotLeftWidgetVerticalRangeChanged(int min, int max )
187 {
188  //qDebug() << "View::Private::slotLeftWidgetVerticalRangeChanged("<<min<<max<<")";
189  gfxview.verticalScrollBar()->setRange( min, max );
190  gfxview.updateSceneRect();
191 }
192 
193 void View::Private::slotGfxViewVerticalRangeChanged( int min, int max )
194 {
195  //qDebug() << "View::Private::slotGfxViewVerticalRangeChanged("<<min<<max<<")";
196  int leftMin = leftWidget->verticalScrollBar()->minimum();
197  int leftMax = leftWidget->verticalScrollBar()->maximum();
198  bool blocked = gfxview.verticalScrollBar()->blockSignals( true );
199  gfxview.verticalScrollBar()->setRange( qMax( min, leftMin ), qMax( max, leftMax ) );
200  gfxview.verticalScrollBar()->blockSignals( blocked );
201 }
202 
216 View::View(QWidget* parent)
217  : QWidget(parent),
218  _d(new Private(this))
219 {
220 #if defined KDAB_EVAL
221  EvalDialog::checkEvalLicense( "KD Gantt" );
222 #endif
223  _d->init();
224 }
225 
226 View::~View()
227 {
228  delete _d;
229 }
230 
231 #define d d_func()
232 
238 void View::setLeftView( QAbstractItemView* aiv )
239 {
240  assert( aiv );
241  if ( aiv==d->leftWidget ) return;
242  if ( !d->leftWidget.isNull() ) {
243  d->leftWidget->disconnect( this );
244  d->leftWidget->hide();
245  d->leftWidget->verticalScrollBar()->disconnect( d->gfxview.verticalScrollBar() );
246  d->gfxview.verticalScrollBar()->disconnect( d->leftWidget->verticalScrollBar() );
247  }
248 
249  d->leftWidget = aiv;
250  d->splitter.insertWidget( 0, d->leftWidget );
251 
252  if( qobject_cast<QTreeView*>(d->leftWidget) ) {
253  connect( d->leftWidget, SIGNAL(collapsed(QModelIndex)),
254  this, SLOT(slotCollapsed(QModelIndex)) );
255  connect( d->leftWidget, SIGNAL(expanded(QModelIndex)),
256  this, SLOT(slotExpanded(QModelIndex)) );
257  }
258 
259  connect( d->gfxview.verticalScrollBar(), SIGNAL(valueChanged(int)),
260  d->leftWidget->verticalScrollBar(), SLOT(setValue(int)) );
261  connect( d->leftWidget->verticalScrollBar(), SIGNAL(valueChanged(int)),
262  d->gfxview.verticalScrollBar(), SLOT(setValue(int)) );
263  connect( d->leftWidget->verticalScrollBar(), SIGNAL(rangeChanged(int,int)),
264  this, SLOT(slotLeftWidgetVerticalRangeChanged(int,int)) );
265  connect( d->gfxview.verticalScrollBar(), SIGNAL(rangeChanged(int,int)),
266  this, SLOT(slotGfxViewVerticalRangeChanged(int,int)) );
267 }
268 
274 void View::setRowController( AbstractRowController* ctrl )
275 {
276  if ( ctrl == d->rowController ) return;
277  d->rowController = ctrl;
278  d->gfxview.setRowController( d->rowController );
279 }
280 
284 AbstractRowController* View::rowController()
285 {
286  return d->rowController;
287 }
288 
291 const AbstractRowController* View::rowController() const
292 {
293  return d->rowController;
294 }
295 
300 const QAbstractItemView* View::leftView() const
301 {
302  return d->leftWidget;
303 }
304 
308 QAbstractItemView* View::leftView()
309 {
310  return d->leftWidget;
311 }
312 
316 const GraphicsView* View::graphicsView() const
317 {
318  return &d->gfxview;
319 }
320 
324 GraphicsView* View::graphicsView()
325 {
326  return &d->gfxview;
327 }
328 
332 const QSplitter* View::splitter() const
333 {
334  return &d->splitter;
335 }
336 
340 QSplitter* View::splitter()
341 {
342  return &d->splitter;
343 }
344 
345 
348 QAbstractItemModel* View::model() const
349 {
350  return leftView()->model();
351 }
352 
358 void View::setModel( QAbstractItemModel* model )
359 {
360  leftView()->setModel( model );
361  d->ganttProxyModel.setSourceModel( model );
362  d->gfxview.setModel( &d->ganttProxyModel );
363 }
364 
367 QItemSelectionModel* View::selectionModel() const
368 {
369  return leftView()->selectionModel();
370 }
371 
375 void View::setSelectionModel( QItemSelectionModel* smodel )
376 {
377  leftView()->setSelectionModel( smodel );
378  d->gfxview.setSelectionModel( new QItemSelectionModel( &( d->ganttProxyModel ),this ) );
379 }
380 
386 void View::setGrid( AbstractGrid* grid )
387 {
388  d->gfxview.setGrid( grid );
389 }
390 
393 AbstractGrid* View::grid() const
394 {
395  return d->gfxview.grid();
396 }
397 
400 QModelIndex View::rootIndex() const
401 {
402  return leftView()->rootIndex();
403 }
404 
408 void View::setRootIndex( const QModelIndex& idx )
409 {
410  leftView()->setRootIndex( idx );
411  d->gfxview.setRootIndex( idx );
412 }
413 
416 ItemDelegate* View::itemDelegate() const
417 {
418  return d->gfxview.itemDelegate();
419 }
420 
424 void View::setItemDelegate( ItemDelegate* delegate )
425 {
426  leftView()->setItemDelegate( delegate );
427  d->gfxview.setItemDelegate( delegate );
428 }
429 
433 void View::setConstraintModel( ConstraintModel* cm )
434 {
435  d->constraintProxy.setSourceModel( cm );
436  d->gfxview.setConstraintModel( &d->mappedConstraintModel );
437 }
438 
441 ConstraintModel* View::constraintModel() const
442 {
443  return d->constraintProxy.sourceModel();
444 }
445 
446 const QAbstractProxyModel* View::ganttProxyModel() const
447 {
448  return &( d->ganttProxyModel );
449 }
450 
451 QAbstractProxyModel* View::ganttProxyModel()
452 {
453  return &( d->ganttProxyModel );
454 }
455 
456 void View::resizeEvent(QResizeEvent*ev)
457 {
458  QWidget::resizeEvent(ev);
459 }
460 
467 QModelIndex View::indexAt( const QPoint& pos ) const
468 {
469  return d->gfxview.indexAt( pos );
470 }
471 
478 void View::print( QPrinter* printer, bool drawRowLabels )
479 {
480  graphicsView()->print( printer, drawRowLabels );
481 }
482 
493 void View::print( QPrinter* printer, qreal start, qreal end, bool drawRowLabels )
494 {
495  graphicsView()->print( printer, start, end, drawRowLabels );
496 }
497 
502 void View::print( QPainter* painter, const QRectF& target, bool drawRowLabels)
503 {
504  d->gfxview.print( painter,
505  target,
506  drawRowLabels);
507 }
508 
517 void View::print( QPainter* painter, qreal start, qreal end, const QRectF& target, bool drawRowLabels)
518 {
519  d->gfxview.print( painter,
520  start, end,
521  target,
522  drawRowLabels);
523 }
524 
525 
526 #include "moc_kdganttview.cpp"
527 
528 #ifndef KDAB_NO_UNIT_TESTS
529 #include "unittest/test.h"
530 
531 #include "kdganttlistviewrowcontroller.h"
532 #include <QApplication>
533 #include <QTimer>
534 #include <QPixmap>
535 #include <QListView>
536 
537 namespace {
538  std::ostream& operator<<( std::ostream& os, const QImage& img )
539  {
540  os << "QImage[ size=("<<img.width()<<", "<<img.height()<<")]";
541  return os;
542  }
543 }
544 
545 KDAB_SCOPED_UNITTEST_SIMPLE( KDGantt, View, "test" ) {
546  View view( 0 );
547 #if 0 // GUI tests do not work well on the server
548  QTimer::singleShot( 1000, qApp, SLOT(quit()) );
549  view.show();
550 
551  qApp->exec();
552  QPixmap screenshot1 = QPixmap::grabWidget( &view );
553 
554  QTreeView* tv = new QTreeView;
555  view.setLeftView( tv );
556  view.setRowController( new TreeViewRowController(tv,view.ganttProxyModel()) );
557 
558  QTimer::singleShot( 1000, qApp, SLOT(quit()) );
559 
560  qApp->exec();
561  QPixmap screenshot2 = QPixmap::grabWidget( &view );
562 
563  assertEqual( screenshot1.toImage(), screenshot2.toImage() );
564 
565  QListView* lv = new QListView;
566  view.setLeftView(lv);
567  view.setRowController( new ListViewRowController(lv,view.ganttProxyModel()));
568  view.show();
569  QTimer::singleShot( 1000, qApp, SLOT(quit()) );
570  qApp->exec();
571 #endif
572 }
573 #endif /* KDAB_NO_UNIT_TESTS */
KDGantt::View::setSelectionModel
void setSelectionModel(QItemSelectionModel *smodel)
Definition: kdganttview.cpp:375
KDGantt::View::model
QAbstractItemModel * model() const
Definition: kdganttview.cpp:348
KDGantt::View::selectionModel
QItemSelectionModel * selectionModel() const
Definition: kdganttview.cpp:367
KDGantt::View::rootIndex
QModelIndex rootIndex() const
Definition: kdganttview.cpp:400
KDGantt::View::print
void print(QPrinter *printer, bool drawRowLabels=true)
Definition: kdganttview.cpp:478
KDGantt::ItemDelegate
Class used to render gantt items in a KDGantt::GraphicsView.
Definition: kdganttitemdelegate.h:39
KDGantt::View::setLeftView
void setLeftView(QAbstractItemView *)
Definition: kdganttview.cpp:238
KDGantt::View
This widget that consists of a QTreeView and a GraphicsView.
Definition: kdganttview.h:47
d
#define d
Definition: kdganttview.cpp:231
KDGantt::GraphicsView
The GraphicsView class provides a model/view implementation of a gantt chart.
Definition: kdganttgraphicsview.h:44
KDGantt::GraphicsView::print
void print(QPrinter *printer, bool drawRowLabels=true)
Definition: kdganttgraphicsview.cpp:684
QWidget
KDGantt::View::setGrid
void setGrid(AbstractGrid *)
Definition: kdganttview.cpp:386
KDGantt::View::Private::~Private
virtual ~Private()
Definition: kdganttview.cpp:80
operator<<
QDebug operator<<(QDebug dbg, const Constraint &c)
Definition: kdganttconstraint.cpp:154
KDGantt::View::leftView
const QAbstractItemView * leftView() const
Definition: kdganttview.cpp:300
KDGantt::View::grid
AbstractGrid * grid() const
Definition: kdganttview.cpp:393
KDGantt::View::constraintModel
ConstraintModel * constraintModel() const
Definition: kdganttview.cpp:441
KDGantt::View::Private::init
void init()
Definition: kdganttview.cpp:84
KDGantt::ListViewRowController
Definition: kdganttlistviewrowcontroller.h:34
KDGantt::View::setRootIndex
void setRootIndex(const QModelIndex &idx)
Definition: kdganttview.cpp:408
KDGantt::ItemTypeRole
Definition: kdganttglobal.h:207
KDGantt::AbstractGrid
Abstract baseclass for grids. A grid is used to convert between QModelIndex'es and gantt chart values...
Definition: kdganttabstractgrid.h:40
KDGantt::View::View
View(QWidget *parent=0)
Definition: kdganttview.cpp:216
QTreeView
KDGantt::TreeViewRowController
Definition: kdgantttreeviewrowcontroller.h:34
KDGantt::View::graphicsView
const GraphicsView * graphicsView() const
Definition: kdganttview.cpp:316
KDGantt::View::Private::slotVerticalScrollValueChanged
void slotVerticalScrollValueChanged(int)
Definition: kdganttview.cpp:177
kdganttlistviewrowcontroller.h
KDGantt::View::setModel
void setModel(QAbstractItemModel *model)
Definition: kdganttview.cpp:358
kdganttitemdelegate.h
KDGantt::View::setRowController
void setRowController(AbstractRowController *)
Definition: kdganttview.cpp:274
KDGantt::View::setItemDelegate
void setItemDelegate(ItemDelegate *)
Definition: kdganttview.cpp:424
KDGantt::View::indexAt
QModelIndex indexAt(const QPoint &pos) const
Definition: kdganttview.cpp:467
QAbstractProxyModel
QAbstractItemView
kdganttgraphicsitem.h
KDGantt::View::Private::Private
Private(View *)
Definition: kdganttview.cpp:70
QListView
KDGantt::View::Private::slotGfxViewVerticalRangeChanged
void slotGfxViewVerticalRangeChanged(int, int)
Definition: kdganttview.cpp:193
KDGantt::View::Private::updateScene
void updateScene()
Definition: kdganttview.cpp:110
KDGantt::KDGanttTreeView::KDGanttTreeView
KDGanttTreeView(QAbstractProxyModel *proxy, QWidget *parent=0)
Definition: kdganttview.cpp:59
KDAB_SCOPED_UNITTEST_SIMPLE
KDAB_SCOPED_UNITTEST_SIMPLE(KDGantt, View,"test")
Definition: kdganttview.cpp:545
KDGantt::TypeMulti
Definition: kdganttglobal.h:216
KDGantt::View::Private::slotLeftWidgetVerticalRangeChanged
void slotLeftWidgetVerticalRangeChanged(int, int)
Definition: kdganttview.cpp:186
KDGantt::View::Private
Definition: kdganttview_p.h:64
KDGantt::KDGanttTreeView::~KDGanttTreeView
virtual ~KDGanttTreeView()
Definition: kdganttview.cpp:66
KDGantt::View::~View
virtual ~View()
Definition: kdganttview.cpp:226
KDGantt::View::rowController
AbstractRowController * rowController()
Definition: kdganttview.cpp:284
KDGantt::View::resizeEvent
void resizeEvent(QResizeEvent *)
Definition: kdganttview.cpp:456
KDGantt::View::Private::slotCollapsed
void slotCollapsed(const QModelIndex &)
Definition: kdganttview.cpp:130
KDGantt::AbstractRowController
Abstract baseclass for row controllers. A row controller is used by the GraphicsView to nagivate the ...
Definition: kdganttabstractrowcontroller.h:34
KDGantt::View::splitter
const QSplitter * splitter() const
Definition: kdganttview.cpp:332
KDGantt::View::setConstraintModel
void setConstraintModel(ConstraintModel *)
Definition: kdganttview.cpp:433
KDGantt::View::ganttProxyModel
const QAbstractProxyModel * ganttProxyModel() const
Definition: kdganttview.cpp:446
kdganttview_p.h
KDGantt::View::Private::slotExpanded
void slotExpanded(const QModelIndex &)
Definition: kdganttview.cpp:166
KDGantt::ConstraintModel
Definition: kdganttconstraintmodel.h:35
KDGantt::View::itemDelegate
ItemDelegate * itemDelegate() const
Definition: kdganttview.cpp:416
KDGantt::KDGanttTreeView::rowController
AbstractRowController * rowController()
Definition: kdganttview_p.h:59
kdganttview.h
KDGantt::KDGanttTreeView
Definition: kdganttview_p.h:54
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:55:05 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kdgantt2

Skip menu "kdgantt2"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal