diff options
author | Thomas Zander <thomas.zander@trolltech.com> | 2009-05-18 13:03:08 +0200 |
---|---|---|
committer | Thomas Zander <thomas.zander@trolltech.com> | 2009-05-18 13:03:08 +0200 |
commit | ce90ce1490732150e2ab4b24d09edd4bd1307404 (patch) | |
tree | 99fcedfc95d56cb2e12c53a8df31c29957d7863f | |
parent | 5fad7c697197a941a5213fe535758cf592486ee0 (diff) |
Make printing work properly when there is a column wider than our paper
-rw-r--r-- | examples/spreadsheet/spreadsheet.cpp | 69 |
1 files changed, 42 insertions, 27 deletions
diff --git a/examples/spreadsheet/spreadsheet.cpp b/examples/spreadsheet/spreadsheet.cpp index 77ba13f..65fb574 100644 --- a/examples/spreadsheet/spreadsheet.cpp +++ b/examples/spreadsheet/spreadsheet.cpp @@ -72,8 +72,6 @@ void SpreadSheet::print() printer.setPaperSize(QSizeF(106, 80), QPrinter::Millimeter); printer.setResolution(600); QPainter painter(&printer); -//painter.setPen(QPen(QColor(Qt::red))); -//painter.drawRect(QRectF(QPointF(), printer.pageRect().size())); const qreal scaleX = printer.logicalDpiX() / (qreal) qt_defaultDpiX(); const qreal scaleY = printer.logicalDpiY() / (qreal) qt_defaultDpiY(); @@ -87,14 +85,19 @@ void SpreadSheet::print() QtGraphicsTableView *view = controller->view(); Q_ASSERT(view); QGraphicsScene *scene = view->scene(); + Q_ASSERT(scene); const int oldFirstRow = view->firstRow(); const int oldFirstColumn = view->firstColumn(); const QRectF oldGeometry = view->geometry(); + const qreal oldHorizontalOffset = view->horizontalOffset(); + view->setGeometry(0, 0, visibleSize.width(), visibleSize.height()); + view->setHorizontalOffset(0); int row = 0; int column; bool first = true; + qreal offsetInColumn = 0; // for those columns too wide and thus split over more than one page while (row < controller->model()->rowCount()) { column = 0; view->setFirstRow(row); @@ -113,12 +116,25 @@ void SpreadSheet::print() printer.newPage(); first = false; view->setFirstColumn(column); + view->setHorizontalOffset(offsetInColumn); qreal width = visibleSize.width(); while (true) { const qreal columnWidth = controller->view()->columnWidth(column); - if (width - columnWidth < 0) + if (width == visibleSize.width() && columnWidth - offsetInColumn > visibleSize.width()) { + // the column doesn't fit on a page. Lets split it. + offsetInColumn += visibleSize.width(); break; + } else if (offsetInColumn > 0) { // we still have a part of a split column to print! + width -= columnWidth - offsetInColumn; + offsetInColumn = 0; + ++column; + continue; + } + + if (width - columnWidth + offsetInColumn < 0) { + break; + } if (column >= controller->model()->columnCount()) break; ++column; @@ -128,34 +144,31 @@ void SpreadSheet::print() painter.setPen(QPen(QColor(Qt::green))); painter.drawRect(QRectF(0, 0, visibleSize.width() - width, visibleSize.height() - height)); #endif - view->setGeometry(0, 0, visibleSize.width() - width, visibleSize.height() - height); view->doLayout(); - // paint children - if (scene) { - QList<QGraphicsItem*> items = scene->items(view->rect()); - QList<QGraphicsItem*>::Iterator iter = items.begin(); - for (;iter != items.end(); ++iter) { - QGraphicsItem *parent = *iter; - while (parent != view && parent != 0) - parent = parent->parentItem(); - if (parent == 0) - continue; - if (!(*iter)->isVisible()) - continue; - painter.save(); - painter.translate((*iter)->mapToItem(view, QPointF())); + // find and paint children which are the cells + QList<QGraphicsItem*> items = scene->items(view->rect()); + QList<QGraphicsItem*>::Iterator iter = items.begin(); + for (;iter != items.end(); ++iter) { + QGraphicsItem *parent = *iter; + while (parent != view && parent != 0) + parent = parent->parentItem(); + if (parent == 0) + continue; + if (!(*iter)->isVisible()) + continue; + painter.save(); + painter.translate((*iter)->mapToItem(view, QPointF())); #ifdef DEBUG_TABLES - QGraphicsWidget *w = dynamic_cast<QGraphicsWidget*>(*iter); - if (w) { - painter.save(); - painter.setPen(QPen(QColor(Qt::red))); - painter.drawRect(QRectF(QPointF(), w->size())); - painter.restore(); - } -#endif - (*iter)->paint(&painter, 0); + QGraphicsWidget *w = dynamic_cast<QGraphicsWidget*>(*iter); + if (w) { + painter.save(); + painter.setPen(QPen(QColor(Qt::red))); + painter.drawRect(QRectF(QPointF(), w->size())); painter.restore(); } +#endif + (*iter)->paint(&painter, 0); + painter.restore(); } } } @@ -163,6 +176,7 @@ void SpreadSheet::print() view->setFirstRow(oldFirstRow); view->setFirstColumn(oldFirstColumn); view->setGeometry(oldGeometry); + view->setHorizontalOffset(oldHorizontalOffset); } void SpreadSheet::updateStatus(int row, int column) @@ -292,4 +306,5 @@ void SpreadSheet::setupContents() table->setItem(9, 5, new QtTableDefaultItem("sum F2 F9")); table->item(9,5)->setBackground(Qt::lightGray); + } |