[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    KDE/kdepim/kdgantt
From:       Dag Andersen <danders () get2net ! dk>
Date:       2008-09-02 10:33:45
Message-ID: 1220351625.794031.28690.nullmailer () svn ! kde ! org
[Download RAW message or body]

SVN commit 856198 by danders:

Enable printing.
The code needs refactoring (it's a mess). I've tried to reuse
existing code (like QGraphicsScene::render()) but this makes things
a bit difficult. Also there seems to be problems with painter clipping
and printer paper/page rect. (I doubt these are qt bugs, hopefully, it's
just my system acting up)



 M  +3 -0      kdganttabstractgrid.h  
 M  +35 -1     kdganttdatetimegrid.cpp  
 M  +3 -1      kdganttdatetimegrid.h  
 M  +60 -3     kdganttgraphicsscene.cpp  
 M  +5 -3      kdganttgraphicsscene.h  
 M  +20 -2     kdganttgraphicsview.cpp  
 M  +5 -2      kdganttgraphicsview.h  
 M  +2 -0      kdganttgraphicsview_p.h  
 M  +5 -31     kdganttview.cpp  
 M  +3 -2      kdganttview.h  


--- trunk/KDE/kdepim/kdgantt/kdganttabstractgrid.h #856197:856198
@@ -55,6 +55,9 @@
                                 AbstractRowController* rowController = 0, QWidget* \
                widget=0 ) = 0;
         virtual void paintHeader( QPainter* painter, const QRectF& headerRect, const \
QRectF& exposedRect,  qreal offset, QWidget* widget=0 ) = 0;
+        
+        virtual void render( QPainter* /*painter*/,  const QRectF & /*target*/, \
const QRectF&  /*headerRect*/, const QRectF& /*exposedRect*/, QWidget* /*widget*/, \
Qt::AspectRatioMode /*aspectRatioMode*/ = Qt::KeepAspectRatio ) {} +        
     public Q_SLOTS:
         /*internal*/ virtual void setModel( QAbstractItemModel* model );
         /*internal*/ virtual void setRootIndex( const QModelIndex& idx );
--- trunk/KDE/kdepim/kdgantt/kdganttdatetimegrid.cpp #856197:856198
@@ -303,7 +303,7 @@
             painter->fillRect( QRectF( x, exposedRect.top(), dayWidth(), \
exposedRect.height() ), painter->brush() );  }
 
-        painter->drawLine( QPointF( x, sceneRect.top() ), QPointF( x, \
sceneRect.bottom() ) ); +        painter->drawLine( QPointF( x, exposedRect.top() ), \
QPointF( x, exposedRect.bottom() ) );  }
     if ( rowController && d->rowSeparators ) {
         // First draw the rows
@@ -324,6 +324,40 @@
         }
     }
 }
+
+void DateTimeGrid::render( QPainter* painter,  const QRectF &target, const QRectF& \
headerRect, const QRectF& exposedRect, QWidget *widget, Qt::AspectRatioMode \
aspectRatioMode ) +{
+    painter->save();
+    
+    qreal xratio = target.width() / exposedRect.width();
+    qreal yratio = target.height() / exposedRect.height();
+    //qDebug()<<"QGraphicsScene::render()"<<xratio<<yratio;
+    // Scale according to the aspect ratio mode.
+    switch (aspectRatioMode) {
+        case Qt::KeepAspectRatio:
+            xratio = yratio = qMin(xratio, yratio);
+            break;
+        case Qt::KeepAspectRatioByExpanding:
+            xratio = yratio = qMax(xratio, yratio);
+            break;
+        case Qt::IgnoreAspectRatio:
+            break;
+    }
+
+    //qDebug()<<"DateTimeGrid::render()"<<"target="<<target<<"exposedRect="<<exposedRect<<"xr="<<xratio<<"yr="<<yratio;
 +    
+    painter->setClipRect( target );
+    QTransform painterTransform;
+    painterTransform *= QTransform()
+            .translate(target.left(), target.top())
+            .scale(xratio, yratio)
+            .translate(-exposedRect.left(), -exposedRect.top());
+    painter->setWorldTransform(painterTransform, true);
+    
+    paintHeader( painter, headerRect, exposedRect, 0.0, widget );
+    painter->restore();
+}
+
 void DateTimeGrid::paintHeader( QPainter* painter,  const QRectF& headerRect, const \
QRectF& exposedRect,  qreal offset, QWidget* widget )
 {
--- trunk/KDE/kdepim/kdgantt/kdganttdatetimegrid.h #856197:856198
@@ -65,7 +65,9 @@
         /*reimp*/ void paintHeader( QPainter* painter, const QRectF& headerRect, \
const QRectF& exposedRect,  qreal offset, QWidget* widget=0 );
 
-	protected:
+        void render( QPainter* painter,  const QRectF &target, const QRectF& \
headerRect, const QRectF& exposedRect, QWidget *widget, Qt::AspectRatioMode \
aspectRatioMode = Qt::KeepAspectRatio ); +	
+    protected:
 		virtual void paintHourScaleHeader( QPainter* painter, const QRectF& headerRect, \
const QRectF& exposedRect,  qreal offset, QWidget* widget=0 );
 		virtual void paintDayScaleHeader( QPainter* painter, const QRectF& headerRect, \
                const QRectF& exposedRect,
--- trunk/KDE/kdepim/kdgantt/kdganttgraphicsscene.cpp #856197:856198
@@ -29,6 +29,7 @@
 #include "kdganttabstractrowcontroller.h"
 #include "kdganttdatetimegrid.h"
 #include "kdganttsummaryhandlingproxymodel.h"
+#include "kdganttgraphicsview.h"
 
 #include <QToolTip>
 #include <QGraphicsSceneHelpEvent>
@@ -591,8 +592,41 @@
     return d->dragSource;
 }
 
-void GraphicsScene::print( QPainter* painter, const QRectF& target, bool \
drawRowLabels ) +QRectF GraphicsScene::printRect(bool drawRowLabels )
 {
+    assert(rowController());
+
+    QVector<QGraphicsTextItem*> labelItems;
+    if(drawRowLabels) {
+        labelItems.reserve(d->items.size());
+        qreal textWidth = 0.;
+        qreal rowHeight = 0.;
+        {Q_FOREACH( GraphicsItem* item, d->items ) {
+            QModelIndex sidx = summaryHandlingModel()->mapToSource( item->index() );
+            if( sidx.parent().isValid() && sidx.parent().data( ItemTypeRole \
).toInt() == TypeMulti ) { +                continue;
+            }
+            const Span rg=rowController()->rowGeometry( sidx );
+            const QString txt = item->index().data( Qt::DisplayRole ).toString();
+            QGraphicsTextItem* ti = new QGraphicsTextItem(txt,0,this);
+            ti->setPos( 0, rg.start() );
+            if( ti->document()->size().width() > textWidth ) textWidth = \
ti->document()->size().width(); +            if( rg.length() > rowHeight ) rowHeight \
= rg.length(); +            labelItems << ti;
+        }}
+        {Q_FOREACH( QGraphicsTextItem* item, labelItems ) {
+            item->setPos( -textWidth-rowHeight, item->pos().y() );
+            item->show();
+        }}
+    }
+    QRectF res = itemsBoundingRect();
+    qDeleteAll(labelItems);
+    qDebug()<<"printRect()"<<res;
+    return res;
+}
+
+void GraphicsScene::print( QPainter* painter, const QRectF& target, const QRectF& \
source, bool drawRowLabels, GraphicsView *view ) +{
   QRectF targetRect(target);
 
   assert(rowController());
@@ -622,8 +656,31 @@
   }
   QRectF oldSceneRect( sceneRect() );
   setSceneRect( itemsBoundingRect() );
-  if(targetRect.isNull()) targetRect = sceneRect();
-  render( painter, target );
+  QRectF sourceRect = source;
+  //qDebug()<<"GraphicsScene::print() 1"<<sceneRect()<<targetRect<<sourceRect;
+  if ( view ) {
+      Qt::AspectRatioMode mode = Qt::KeepAspectRatio;
+      QRectF s = sourceRect;
+      s.setHeight( view->headerHeight() );
+      qreal xr = targetRect.width() / sourceRect.width();
+      qreal yr = (qreal)1.0;
+      if ( sourceRect.height() > s.height() ) {
+          yr = targetRect.height() / sourceRect.height();
+      }
+      xr = yr = qMin( xr, yr );
+      QRectF t = targetRect;
+      t.setHeight( s.height() * yr );
+      view->renderHeader( painter, t, s, mode );
+      targetRect.translate( (qreal)0.0, t.height() );
+      //qDebug()<<"GraphicsScene::print() \
2"<<"t="<<t<<"s="<<s<<"yr="<<yr<<(s.height() * yr)<<"targetRect="<<targetRect; +  }
+  //qDebug()<<"GraphicsScene::print() 3"<<sceneRect()<<targetRect<<sourceRect;
+  if ( targetRect.width() > sourceRect.width() && targetRect.height() > \
sourceRect.height() ) { +      targetRect.setSize( sourceRect.size() );
+      render( painter, targetRect, sourceRect, Qt::IgnoreAspectRatio );
+  } else {
+      render( painter, targetRect, sourceRect );
+  }
   qDeleteAll(labelItems);
 
   setSceneRect( oldSceneRect );
--- trunk/KDE/kdepim/kdgantt/kdganttgraphicsscene.h #856197:856198
@@ -41,7 +41,8 @@
     class ConstraintModel;
     class ConstraintGraphicsItem;
     class ItemDelegate;
-
+    class GraphicsView;
+    
     class GraphicsScene : public QGraphicsScene {
         Q_OBJECT
         KDGANTT_DECLARE_PRIVATE_BASE_POLYMORPHIC( GraphicsScene )
@@ -97,8 +98,9 @@
         GraphicsItem* dragSource() const;
 
         /* Printing */
-        void print( QPainter* painter, const QRectF& target = QRectF(), bool \
                drawRowLabels=true );
-
+        void print( QPainter* painter, const QRectF& target = QRectF(), const \
QRectF& source = QRectF(), bool drawRowLabels=true, GraphicsView *view=0 ); +        \
QRectF printRect(bool drawRowLabels=true ); +        
     Q_SIGNALS:
         void gridChanged();
 
--- trunk/KDE/kdepim/kdgantt/kdganttgraphicsview.cpp #856197:856198
@@ -69,6 +69,11 @@
     view()->grid()->paintHeader( &p, rect(), ev->rect(), m_offset, this );
 }
 
+void HeaderWidget::render( QPainter* painter, const QRectF &targetRect, const QRectF \
&sourceRect, Qt::AspectRatioMode aspectRatioMode) +{
+    view()->grid()->render( painter, targetRect, rect(), sourceRect, this, \
aspectRatioMode ); +}
+
 void HeaderWidget::contextMenuEvent( QContextMenuEvent* event )
 {
     QMenu contextMenu;
@@ -625,11 +630,24 @@
  * If \a drawRowLabels is true, an additional per-row label
  * is drawn, mimicing the look of a listview.
  */
-void GraphicsView::print( QPainter* painter, const QRectF& target, bool \
drawRowLabels ) +void GraphicsView::print( QPainter* painter, const QRectF& target, \
const QRectF& source, bool drawRowLabels, bool drawHeader )  {
-  d->scene.print(painter,target,drawRowLabels);
+  d->scene.print(painter,target,source,drawRowLabels,(drawHeader ? this : 0));
 }
+QRectF GraphicsView::printRect(bool drawRowLabels )
+{
+    return d->scene.printRect(drawRowLabels);
+}
 
+void GraphicsView::renderHeader( QPainter* painter, const QRectF& target, const \
QRectF& source, Qt::AspectRatioMode aspectRatioMode ) +{
+    d->headerwidget.render(painter,target,source,aspectRatioMode);
+}
 
+qreal GraphicsView::headerHeight() const
+{
+    return d->headerwidget.rect().height();
+}
+
 #include "moc_kdganttgraphicsview.cpp"
 #include "moc_kdganttgraphicsview_p.cpp"
--- trunk/KDE/kdepim/kdgantt/kdganttgraphicsview.h #856197:856198
@@ -87,8 +87,11 @@
         void updateSceneRect();
         void deleteSubtree( const QModelIndex& );
 
-        void print( QPainter* painter, const QRectF& target = QRectF(), bool \
                drawRowLabels=true );
-
+        void print( QPainter* painter, const QRectF& target = QRectF(), const \
QRectF& source = QRectF(), bool drawRowLabels=true, bool drawHeader=true ); +        \
QRectF printRect(bool drawRowLabels=true ); +        void renderHeader( QPainter* \
painter, const QRectF& target, const QRectF& source, Qt::AspectRatioMode \
aspectRatioMode = Qt::KeepAspectRatio ); +        qreal headerHeight() const;
+        
     public Q_SLOTS:
         void setModel( QAbstractItemModel* );
         void setSummaryHandlingModel( QAbstractProxyModel* model );
--- trunk/KDE/kdepim/kdgantt/kdganttgraphicsview_p.h #856197:856198
@@ -38,6 +38,8 @@
 
         GraphicsView* view() const { return qobject_cast<GraphicsView*>( parent() \
);}  
+        void render( QPainter* painter, const QRectF &targetRect, const QRectF \
&sourceRect, Qt::AspectRatioMode aspectRatioMode ); +        
     public Q_SLOTS:
         void scrollTo( int );
     protected:
--- trunk/KDE/kdepim/kdgantt/kdganttview.cpp #856197:856198
@@ -428,40 +428,14 @@
 /*! Render the GanttView inside the rectangle \a target using the painter \a \
                painter.
  * \see KDGantt::GraphicsView::print(QPainter* painter, const QRectF& target,bool \
                drawRowLabels)
  */
-void View::print( QPainter* painter, const QRectF& target, bool drawRowLabels)
+void View::print( QPainter* painter, const QRectF& target, const QRectF& source, \
bool drawRowLabels, bool drawHeader)  {
-#if 0
+    
     QRectF targetRect = target;
-    if( targetRect.isNull() )
-        targetRect.setRect(0, 0,
-			   painter->device()->width(),
-			   painter->device()->height());
-
-    QPixmap leftw = QPixmap::grabWidget(d->leftWidget->viewport());
-
-    /* Now figure out the size of the whole thing to fit it into
-     * targetRect */
-    QRectF targetLeft( leftw.rect() );
-    QRectF targetRight( d->gfxview.scene()->sceneRect() );
-    qreal sw = (targetLeft.width()+targetRight.width())/targetRect.width();
-    qreal sh = (targetLeft.height()+targetRight.height())/targetRect.height();
-    if( sw > 1. || sh > 1. ) {
-      // we need to scale
-      qreal s = qMax(sw,sh);
-      targetLeft.setWidth(targetLeft.width()/s);
-      targetLeft.setHeight(targetLeft.height()/s);
-      targetRight.setWidth(targetRight.width()/s);
-      targetRight.setHeight(targetRight.height()/s);
+    if( targetRect.isNull() ) {
+        targetRect.setRect(0, 0, painter->device()->width(), \
painter->device()->height());  }
-    targetRight.translate( targetLeft.width(),0 );
-
-    painter->drawPixmap( targetLeft, leftw, leftw.rect() );
-    d->gfxview.scene()->render( painter,
-				targetRight);
-#endif
-    d->gfxview.print( painter,
-		      target,
-		      drawRowLabels);
+    d->gfxview.print( painter, targetRect, source, drawRowLabels, drawHeader);
 }
 
 
--- trunk/KDE/kdepim/kdgantt/kdganttview.h #856197:856198
@@ -39,7 +39,8 @@
     class AbstractGrid;
     class GraphicsView;
     class AbstractRowController;
-
+    class GraphicsItem;
+    
     class KDGANTT_EXPORT View : public QWidget {
         Q_OBJECT
         KDGANTT_DECLARE_PRIVATE_BASE_POLYMORPHIC_QWIDGET(View)
@@ -74,7 +75,7 @@
         const QAbstractProxyModel* ganttProxyModel() const;
         QAbstractProxyModel* ganttProxyModel();
 
-        void print( QPainter* painter, const QRectF& target = QRectF(), bool \
drawRowLabels=true); +        void print( QPainter* painter, const QRectF& target = \
QRectF(), const QRectF& source = QRectF(), bool drawRowLabels=true, bool \
drawHeader=true);  
     public Q_SLOTS:
         void setModel(QAbstractItemModel* model);


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic