knotes
knoteprinter.cpp
Go to the documentation of this file.00001 #include "knoteprinter.h"
00002
00003 #include <QPainter>
00004 #include <QTextDocument>
00005 #include <QTextDocumentFragment>
00006 #include <QAbstractTextDocumentLayout>
00007 #include <QtGui/QPrinter>
00008 #include <QtGui/QPrintDialog>
00009
00010 #include <kcal/journal.h>
00011
00012 #include <klocale.h>
00013
00014
00015 KNotePrinter::KNotePrinter()
00016 {
00017 }
00018
00019 void KNotePrinter::setDefaultFont( const QFont &font )
00020 {
00021 m_defaultFont = font;
00022 }
00023
00024 QFont KNotePrinter::defaultFont() const
00025 {
00026 return m_defaultFont;
00027 }
00028
00029 void KNotePrinter::doPrint( const QString &htmlText,
00030 const QString &dialogCaption ) const
00031 {
00032 QPrinter printer( QPrinter::HighResolution );
00033
00034 QPrintDialog printDialog( &printer, 0 );
00035 printDialog.setWindowTitle( dialogCaption );
00036 if ( !printDialog.exec() ) {
00037 return;
00038 }
00039
00040 const int margin = 30;
00041 int marginX = margin * printer.logicalDpiX() / 72;
00042 int marginY = margin * printer.logicalDpiY() / 72;
00043
00044 QRect typeArea( marginX, marginY,
00045 printer.width() - marginX * 2,
00046 printer.height() - marginY * 2 );
00047
00048 QTextDocument textDoc;
00049 textDoc.setHtml( htmlText );
00050 textDoc.documentLayout()->setPaintDevice( &printer );
00051 textDoc.setPageSize( typeArea.size() );
00052 textDoc.setDefaultFont( m_defaultFont );
00053
00054 QPainter painter( &printer );
00055 QRect clip( typeArea );
00056 painter.translate( marginX, marginY );
00057 clip.translate( -marginX, -marginY );
00058
00059 for ( int page = 1; page <= textDoc.pageCount(); page++ ) {
00060 textDoc.drawContents( &painter, clip );
00061 clip.translate( 0, typeArea.height() );
00062 painter.translate( 0, -typeArea.height() );
00063
00064 painter.setFont( m_defaultFont );
00065 painter.drawText(
00066 clip.right() - painter.fontMetrics().width( QString::number( page ) ),
00067 clip.bottom() + painter.fontMetrics().ascent() + 5,
00068 QString::number( page ) );
00069
00070 if ( page < textDoc.pageCount() ) {
00071 printer.newPage();
00072 }
00073 }
00074 }
00075
00076 void KNotePrinter::printNote( const QString &name,
00077 const QString &htmlText ) const
00078 {
00079 QString dialogCaption = i18n( "Print %1", name );
00080 doPrint( htmlText, dialogCaption );
00081 }
00082
00083 void KNotePrinter::printNotes( const QList<KCal::Journal *>& journals ) const
00084 {
00085 if ( journals.isEmpty() ) {
00086 return;
00087 }
00088
00089 QString htmlText;
00090
00091 QListIterator<KCal::Journal *> it( journals );
00092 while ( it.hasNext() ) {
00093 KCal::Journal *j = it.next();
00094 htmlText += "<h2>" + j->summary() + "</h2>";
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104 htmlText += ensureHtmlText( j->description() );
00105
00106 if ( it.hasNext() ) {
00107 htmlText += "<hr />";
00108 }
00109 }
00110
00111 QString dialogCaption = i18np( "Print Note", "Print %1 notes",
00112 journals.count() );
00113 doPrint( htmlText, dialogCaption );
00114
00115 #if 0
00116
00117
00118 QTextDocument ret;
00119 ret.setDefaultFont( m_defaultFont );
00120 QTextCursor retC( &ret );
00121 QTextDocument separator;
00122 separator.setHtml( "<html><body><br /><hr /><br /></body></html>" );
00123 QTextDocument header;
00124 QTextDocument note;
00125
00126 QListIterator<KCal::Journal *> it( journals );
00127 while ( it.hasNext() ) {
00128 KCal::Journal *j = it.next();
00129 header.setHtml( "<html><body><h2>" + j->summary() + "</h2></body></html>" );
00130 retC.insertFragment( QTextDocumentFragment( &header ) );
00131
00132 note.setHtml( ensureHtmlText( j->description() ) );
00133 retC.insertFragment( QTextDocumentFragment( ¬e ) );
00134
00135 if ( it.hasNext() ) {
00136 retC.insertFragment( QTextDocumentFragment( &separator ) );
00137 }
00138 }
00139
00140 QString dialogCaption = i18np( "Print Note", "Print %1 notes",
00141 journals.count() );
00142 doPrint( ret.toHtml(), dialogCaption );
00143 #endif
00144 }
00145
00146 inline QString KNotePrinter::ensureHtmlText( const QString &maybeRichText )
00147 const
00148 {
00149 if ( Qt::mightBeRichText( maybeRichText ) ) {
00150 return maybeRichText;
00151 } else {
00152 return Qt::convertFromPlainText( maybeRichText );
00153 }
00154 }
00155