Logo Search packages:      
Sourcecode: kdesdk version File versions

void UMLView::print ( KPrinter *  pPrinter,
QPainter &  pPainter 
)

contains the implementation for printing functionality

Definition at line 211 of file umlview.cpp.

References forceUpdateWidgetFontMetrics(), getDiagram(), getDiagramRect(), and getName().

Referenced by UMLDoc::print().

                                                           {
      int height, width;
      //get the size of the page
      QPaintDeviceMetrics metrics(pPrinter);
      QFontMetrics fm = pPainter.fontMetrics(); // use the painter font metrics, not the screen fm!
      int fontHeight  = fm.lineSpacing();
      int marginX = pPrinter->margins().width();
      int marginY = pPrinter->margins().height();

      // The printer will probably use a different font with different font metrics,
      // force the widgets to update accordingly on paint
      forceUpdateWidgetFontMetrics(&pPainter);

      //double margin at botton of page as it doesn't print down there
      //on my printer, so play safe as default.
      if(pPrinter->orientation() == KPrinter::Portrait) {
            width = metrics.width() - marginX * 2;
            height = metrics.height() - fontHeight - 4 - marginY * 3;
      } else {
            marginX *= 2;
            width = metrics.width() - marginX * 2;
            height = metrics.height() - fontHeight - 4 - marginY * 2;
      }
      //get the smallest rect holding the diagram
      QRect rect = getDiagramRect();
      //now draw to printer

#if 0
      int offsetX = 0, offsetY = 0, widthX = 0, heightY = 0;
      // respect the margin
      pPainter.translate(marginX, marginY);

      // clip away everything outside of the margin
      pPainter.setClipRect(marginX, marginY,
                                     width, metrics.height() - marginY * 2);

      //loop until all of the picture is printed
      int numPagesX = (int)ceil((double)rect.width()/(double)width);
      int numPagesY = (int)ceil((double)rect.height()/(double)height);
      int page = 0;

      // print the canvas to multiple pages
      for (int pageY = 0; pageY < numPagesY; ++pageY) {
            // tile vertically
            offsetY = pageY * height + rect.y();
            heightY = (pageY + 1) * height > rect.height()
                        ? rect.height() - pageY * height
                        : height;
            for (int pageX = 0; pageX < numPagesX; ++pageX) {
                  // tile horizontally
                  offsetX = pageX * width + rect.x();
                  widthX = (pageX + 1) * width > rect.width()
                        ? rect.width() - pageX * width
                        : width;

                  // make sure the part of the diagram is painted at the correct
                  // place in the printout
                  pPainter.translate(-offsetX,-offsetY);
                  getDiagram(QRect(offsetX, offsetY,widthX, heightY),
                           pPainter);
                  // undo the translation so the coordinates for the painter
                  // correspond to the page again
                  pPainter.translate(offsetX,offsetY);

                  //draw foot note
                  QString string = i18n("Diagram: %2 Page %1").arg(page + 1).arg(getName());
                  QColor textColor(50, 50, 50);
                  pPainter.setPen(textColor);
                  pPainter.drawLine(0, height + 2, width, height + 2);
                  pPainter.drawText(0, height + 4, width, fontHeight, AlignLeft, string);

                  if(pageX+1 < numPagesX || pageY+1 < numPagesY) {
                        pPrinter -> newPage();
                        page++;
                  }
            }
      }
#else
      // be gentle - as described in Qt-Doc "The Coordinate System"
      pPainter.save();

      int diagramHeight = rect.height();
      // + 4+fontHeight between diagram and footline as space-buffer
      // + 2            between line and foot-text
      // + 1            for foot-line
      // + fontHeight   for foot-text
      // ==============
      // (2*fontHeight) + 7
      int footHeight = (2*fontHeight) + 7;
      int footTop    = rect.y() + diagramHeight  + 4+fontHeight;
      int drawHeight = diagramHeight  + footHeight;

      // set window of painter to dimensions of diagram
      // set window to viewport relation so that x:y isn't changed
      double dScaleX = (double)rect.width()/ (double)width;
      double dScaleY = (double)drawHeight/ (double)height;
      // select the scaling factor so that the larger dimension
      // fits on the printer page -> use the larger scaling factor
      // -> the virtual diagram window has some additional space at the
      // shorter dimension
      double dScaleUse = ( dScaleX > dScaleY )?dScaleX:dScaleY;

      int windowWidth  = (int)ceil(dScaleUse*width);
      int windowHeight = (int)ceil(dScaleUse*height);
#ifdef DEBUG_PRINTING
      kdDebug() << "drawHeight: " << drawHeight << ", width: " << rect.width()
            << "\nPageHeight: " << height << ", PageWidht: " << width
            << "\nScaleY: " << dScaleY << ", ScaleX: " << dScaleX
            << "\ndScaleUse: " << dScaleUse
            << "\nVirtualSize: Width: " << windowWidth << ", Height: " << windowHeight
            << "\nFoot Top: " << footTop
            << endl;
#endif
      // set virtual drawing area window - where diagram fits 100% in
      pPainter.setWindow( rect.x(), rect.y(), windowWidth, windowHeight );

      // set viewport - the physical mapping
      // --> Qt's QPainter will map all drawed elements from diagram area ( window )
      //     to printer area ( viewport )
      pPainter.setViewport( marginX, marginY, width, height );

      // get Diagram
      getDiagram(QRect(rect.x(), rect.y(), windowWidth, diagramHeight), pPainter);

      //draw foot note
      QString string = i18n("Diagram: %2 Page %1").arg( 1).arg(getName());
      QColor textColor(50, 50, 50);
      pPainter.setPen(textColor);
      pPainter.drawLine(rect.x(), footTop    , windowWidth, footTop);
      pPainter.drawText(rect.x(), footTop + 3, windowWidth, fontHeight, AlignLeft, string);

      // now restore scaling
      pPainter.restore();

#endif
      // next painting will most probably be to a different device (i.e. the screen)
      forceUpdateWidgetFontMetrics(0);
}


Generated by  Doxygen 1.6.0   Back to index