From 06d37bb7ad71977d5869b447be27ea04d7742cd4 Mon Sep 17 00:00:00 2001 From: nds Date: Fri, 11 Apr 2008 04:21:39 +0000 Subject: [PATCH] Print functionality (SLN). --- src/SUIT/SUIT_ViewWindow.cxx | 85 ++++++++++++++++++++++ src/SUIT/SUIT_ViewWindow.h | 1 + src/SVTK/SVTK_MainWindow.cxx | 74 +------------------ src/TableViewer/TableViewer_ViewWindow.cxx | 13 +++- src/TableViewer/TableViewer_ViewWindow.h | 2 + 5 files changed, 101 insertions(+), 74 deletions(-) diff --git a/src/SUIT/SUIT_ViewWindow.cxx b/src/SUIT/SUIT_ViewWindow.cxx index 4e9236a62..e26b6f143 100755 --- a/src/SUIT/SUIT_ViewWindow.cxx +++ b/src/SUIT/SUIT_ViewWindow.cxx @@ -32,6 +32,10 @@ #include #include #include +#include +#include +#include + /*!\class SUIT_ViewWindow * Class provide view window. @@ -213,3 +217,84 @@ QString SUIT_ViewWindow::getVisualParameters() void SUIT_ViewWindow::setVisualParameters( const QString& /*parameters*/ ) { } + +/*! + Prints given image + \param theImage - the image to print +*/ +void SUIT_ViewWindow::printImage( const QImage& theImage, QWidget* theWidget ) +{ + if ( theImage.isNull() ) + return; + + // stored settings for further starts + static QString aPrinterName; + static int anOrientation = -1; + + QPrinter aPrinter; + + // restore settinds from previous launching + + // printer name + if ( !aPrinterName.isEmpty() ) + aPrinter.setPrinterName( aPrinterName ); + else + { + // Nothing to do for the first printing. aPrinter contains default printer name by default + } + + if ( anOrientation >= 0 ) + aPrinter.setOrientation( (QPrinter::Orientation)anOrientation ); + else + aPrinter.setOrientation( QPrinter::Landscape ); + + + QPrintDialog printDlg( &aPrinter, theWidget ); + printDlg.setPrintRange( QAbstractPrintDialog::AllPages ); + if ( printDlg.exec() != QDialog::Accepted ) + return; + + // store printer settings for further starts + aPrinterName = aPrinter.printerName(); + anOrientation = aPrinter.orientation(); + + int W, H; + QPainter aPainter; + + // work arround for printing on real printer + if ( aPrinter.outputFileName().isEmpty() && aPrinter.orientation() == QPrinter::Landscape ) + { + aPrinter.setFullPage( true ); + // set paper orientation and rotate painter + aPrinter.setOrientation( QPrinter::Portrait ); + + W = aPrinter.height(); + H = aPrinter.width(); + + int wBorder = aPrinter.paperRect().height() - W; + int hBorder = aPrinter.paperRect().width() - H; + + aPainter.begin( &aPrinter ); + aPainter.translate( QPoint( H + hBorder, wBorder ) ); + aPainter.rotate( 90 ); + } + else + { + aPrinter.setFullPage( false ); + aPainter.begin( &aPrinter ); + W = aPrinter.width(); + H = aPrinter.height(); + } + + QImage anImage = theImage; + if ( anImage.width() > W || anImage.height() > H ) + anImage = anImage.scaled( W, H, Qt::KeepAspectRatio, Qt::SmoothTransformation ); + + // place image in the center of page + int offsetW = ( W - anImage.width() ) / 2; + int offsetH = ( H - anImage.height() ) / 2; + + aPainter.drawImage( offsetW, offsetH, anImage ); + + aPainter.end(); +} diff --git a/src/SUIT/SUIT_ViewWindow.h b/src/SUIT/SUIT_ViewWindow.h index e5aaa5bc1..944c9d5fa 100755 --- a/src/SUIT/SUIT_ViewWindow.h +++ b/src/SUIT/SUIT_ViewWindow.h @@ -44,6 +44,7 @@ public: virtual QImage dumpView(); bool dumpViewToFormat( const QString& fileName, const QString& format ); + virtual void printImage( const QImage&, QWidget* ); bool onAccelAction( int ); diff --git a/src/SVTK/SVTK_MainWindow.cxx b/src/SVTK/SVTK_MainWindow.cxx index 276760378..abd7cccf5 100644 --- a/src/SVTK/SVTK_MainWindow.cxx +++ b/src/SVTK/SVTK_MainWindow.cxx @@ -942,78 +942,8 @@ SVTK_MainWindow void SVTK_MainWindow::onPrintView() { QImage img = dumpView(); - if ( img.isNull() ) - return; - - // stored settings for further starts - static QString aPrinterName; - static int anOrientation = -1; - - QPrinter aPrinter; - - // restore settinds from previous launching - - // printer name - if ( !aPrinterName.isEmpty() ) - aPrinter.setPrinterName( aPrinterName ); - else - { - // Nothing to do for the first printing. aPrinter contains default printer name by default - } - - if ( anOrientation >= 0 ) - aPrinter.setOrientation( (QPrinter::Orientation)anOrientation ); - else - aPrinter.setOrientation( QPrinter::Landscape ); - - - QPrintDialog printDlg( &aPrinter, this ); - printDlg.setPrintRange( QAbstractPrintDialog::AllPages ); - if ( printDlg.exec() != QDialog::Accepted ) - return; - - // store printer settings for further starts - aPrinterName = aPrinter.printerName(); - anOrientation = aPrinter.orientation(); - - int W, H; - QPainter aPainter; - - // work arround for printing on real printer - if ( aPrinter.outputFileName().isEmpty() && aPrinter.orientation() == QPrinter::Landscape ) - { - aPrinter.setFullPage( true ); - // set paper orientation and rotate painter - aPrinter.setOrientation( QPrinter::Portrait ); - - W = aPrinter.height(); - H = aPrinter.width(); - - int wBorder = aPrinter.paperRect().height() - W; - int hBorder = aPrinter.paperRect().width() - H; - - aPainter.begin( &aPrinter ); - aPainter.translate( QPoint( H + hBorder, wBorder ) ); - aPainter.rotate( 90 ); - } - else - { - aPrinter.setFullPage( false ); - aPainter.begin( &aPrinter ); - W = aPrinter.width(); - H = aPrinter.height(); - } - - if ( img.width() > W || img.height() > H ) - img = img.scaled( W, H, Qt::KeepAspectRatio, Qt::SmoothTransformation ); - - // place image in the center of page - int offsetW = ( W - img.width() ) / 2; - int offsetH = ( H - img.height() ) / 2; - - aPainter.drawImage( offsetW, offsetH, img ); - - aPainter.end(); + if ( myViewWindow ) + myViewWindow->printImage( img, this ); } diff --git a/src/TableViewer/TableViewer_ViewWindow.cxx b/src/TableViewer/TableViewer_ViewWindow.cxx index 1f53c9965..4e1251e05 100755 --- a/src/TableViewer/TableViewer_ViewWindow.cxx +++ b/src/TableViewer/TableViewer_ViewWindow.cxx @@ -107,7 +107,8 @@ void TableViewer_ViewWindow::initLayout() QImage TableViewer_ViewWindow::dumpView() { - return QPixmap::grabWindow( table()->winId() ).toImage(); + return QPixmap::grabWidget( table() ).toImage(); + //return QPixmap::grabWindow( table()->winId() ).toImage(); } void TableViewer_ViewWindow::createActions() @@ -296,6 +297,9 @@ void TableViewer_ViewWindow::actionActivated( const int id ) case PasteId: pasteData(); break; + case PrintId: + printData(); + break; default: break; } @@ -485,7 +489,6 @@ bool TableViewer_ViewWindow::canPasteData() bool aCanPaste = true; int aLeftCol = myTable->columnCount(), aTopRow = myTable->rowCount(); QModelIndexList::const_iterator anIt = anItems.begin(), aLast = anItems.end(); - QTableWidgetItem* anItem; int aCol, aRow; for ( ; anIt != aLast; ++anIt ) { aRow = (*anIt).row(); @@ -510,6 +513,12 @@ bool TableViewer_ViewWindow::canPasteData() return aCanPaste; } +void TableViewer_ViewWindow::printData() +{ + QImage img = dumpView(); + printImage( img, this ); +} + void TableViewer_ViewWindow::exportTableData( Handle(HTMLService_HTMLTable)& table, const ContentType type, const int rowOffset, const int colOffset ) diff --git a/src/TableViewer/TableViewer_ViewWindow.h b/src/TableViewer/TableViewer_ViewWindow.h index 6441844f7..f9aa82242 100755 --- a/src/TableViewer/TableViewer_ViewWindow.h +++ b/src/TableViewer/TableViewer_ViewWindow.h @@ -58,6 +58,8 @@ public: void pasteData(); bool canPasteData(); + void printData(); + protected: typedef enum { VerticalHeader, HorizontalHeader, Cells } ContentType; typedef enum { DumpId, CopyId, PasteId, PrintId, ExportId, Custom } ActionId; -- 2.39.2