modified printing such that line breaks are handled properly

This commit is contained in:
nemu
2009-05-26 07:03:28 +00:00
parent 5cadc57e13
commit 78aa1f09fc

View File

@ -29,10 +29,8 @@
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/ ***************************************************************************/
/*
#include <iostream> #include <iostream>
using namespace std; using namespace std;
*/
#include <qtextedit.h> #include <qtextedit.h>
#include <qaction.h> #include <qaction.h>
@ -601,33 +599,35 @@ void PTextEdit::filePrint()
if ( printer.setup( this ) ) { if ( printer.setup( this ) ) {
QPainter p( &printer ); QPainter p( &printer );
// Check that there is a valid device to print to. // Check that there is a valid device to print to.
if ( !p.device() ) return; if ( !p.device() )
QPaintDeviceMetrics metrics( p.device() ); return;
int dpiy = metrics.logicalDpiY();
int margin = (int) ( (2/2.54)*dpiy ); // 2 cm margins
QRect view( margin, margin, metrics.width() - 2*margin, metrics.height() - 2*margin );
QFont font( currentEditor()->QWidget::font() ); QFont font( currentEditor()->QWidget::font() );
font.setPointSize( 10 ); // we define 10pt to be a nice base size for printing font.setPointSize( 10 ); // we define 10pt to be a nice base size for printing
p.setFont( font );
QSimpleRichText richText( currentEditor()->text(), font, int yPos = 0; // y-position for each line
currentEditor()->context(), QFontMetrics fm = p.fontMetrics();
currentEditor()->styleSheet(), QPaintDeviceMetrics metrics( &printer ); // need width/height
currentEditor()->mimeSourceFactory(), int dpiy = metrics.logicalDpiY();
view.height() ); int margin = (int) ( (2/2.54)*dpiy ); // 2 cm margins
richText.setWidth( &p, view.width() );
int page = 1; // print msr-file
do { QStringList strList = QStringList::split("\n", currentEditor()->text());
richText.draw( &p, margin, margin, view, colorGroup() ); for (QStringList::Iterator it = strList.begin(); it != strList.end(); ++it) {
view.moveBy( 0, view.height() ); // new page needed?
p.translate( 0 , -view.height() ); if ( margin + yPos > metrics.height() - margin ) {
p.setFont( font ); printer.newPage(); // no more room on this page
p.drawText( view.right() - p.fontMetrics().width( QString::number( page ) ), yPos = 0; // back to top of page
view.bottom() + p.fontMetrics().ascent() + 5, QString::number( page ) ); }
if ( view.top() - margin >= richText.height() )
break; // print data
printer.newPage(); p.drawText(margin, margin+yPos, metrics.width(), fm.lineSpacing(),
page++; ExpandTabs | DontClip, *it);
} while (TRUE); yPos += fm.lineSpacing();
}
p.end();
} }
#endif #endif
} }