1 #include "knoteprinter.h"
4 #include <QTextDocument>
5 #include <QTextDocumentFragment>
6 #include <QAbstractTextDocumentLayout>
7 #include <QtGui/QPrinter>
8 #include <QtGui/QPrintDialog>
10 #include <kcal/journal.h>
15 KNotePrinter::KNotePrinter()
19 void KNotePrinter::setDefaultFont( const QFont
&font
)
24 QFont
KNotePrinter::defaultFont() const
29 void KNotePrinter::doPrint( const QString
&htmlText
,
30 const QString
&dialogCaption
) const
32 QPrinter
printer( QPrinter::HighResolution
);
33 //printer.setFullPage( true ); //disabled, causes asymmetric margins
34 QPrintDialog
printDialog( &printer
, 0 );
35 printDialog
.setWindowTitle( dialogCaption
);
36 if ( !printDialog
.exec() ) {
40 const int margin
= 30; //pt //set to 40 when setFullPage() works again
41 int marginX
= margin
* printer
.logicalDpiX() / 72;
42 int marginY
= margin
* printer
.logicalDpiY() / 72;
44 QRect
typeArea( marginX
, marginY
,
45 printer
.width() - marginX
* 2,
46 printer
.height() - marginY
* 2 );
48 QTextDocument textDoc
;
49 textDoc
.setHtml( htmlText
);
50 textDoc
.documentLayout()->setPaintDevice( &printer
);
51 textDoc
.setPageSize( typeArea
.size() );
52 textDoc
.setDefaultFont( m_defaultFont
);
54 QPainter
painter( &printer
);
55 QRect
clip( typeArea
);
56 painter
.translate( marginX
, marginY
);
57 clip
.translate( -marginX
, -marginY
);
59 for ( int page
= 1; page
<= textDoc
.pageCount(); page
++ ) {
60 textDoc
.drawContents( &painter
, clip
);
61 clip
.translate( 0, typeArea
.height() );
62 painter
.translate( 0, -typeArea
.height() );
64 painter
.setFont( m_defaultFont
);
66 clip
.right() - painter
.fontMetrics().width( QString::number( page
) ),
67 clip
.bottom() + painter
.fontMetrics().ascent() + 5,
68 QString::number( page
) );
70 if ( page
< textDoc
.pageCount() ) {
76 void KNotePrinter::printNote( const QString
&name
,
77 const QString
&htmlText
) const
79 QString dialogCaption
= i18n( "Print %1", name
);
80 doPrint( htmlText
, dialogCaption
);
83 void KNotePrinter::printNotes( const QList
<KCal::Journal
*>& journals
) const
85 if ( journals
.isEmpty() ) {
91 QListIterator
<KCal::Journal
*> it( journals
);
92 while ( it
.hasNext() ) {
93 KCal::Journal
*j
= it
.next();
94 htmlText
+= "<h2>" + j
->summary() + "</h2>";
96 //### ensureHtmlText() is a hack.
98 //port most parts of KNotes to HTML (aka rich) text - it is easy enough to
99 //pass plaintext as rich text (Qt:convertFromPlainText()) and the way back
100 //should be doable, too.
101 //Gold star solution: cleanly separate the two types - just never
102 //pass a chunk of text without explicit type. Probably not worth it.
104 htmlText
+= ensureHtmlText( j
->description() );
106 if ( it
.hasNext() ) {
107 htmlText
+= "<hr />";
111 QString dialogCaption
= i18np( "Print Note", "Print %1 notes",
113 doPrint( htmlText
, dialogCaption
);
116 //### This should work more reliably but it doesn't work at all.
119 ret
.setDefaultFont( m_defaultFont
);
120 QTextCursor
retC( &ret
);
121 QTextDocument separator
;
122 separator
.setHtml( "<html><body><br /><hr /><br /></body></html>" );
123 QTextDocument header
;
126 QListIterator
<KCal::Journal
*> it( journals
);
127 while ( it
.hasNext() ) {
128 KCal::Journal
*j
= it
.next();
129 header
.setHtml( "<html><body><h2>" + j
->summary() + "</h2></body></html>" );
130 retC
.insertFragment( QTextDocumentFragment( &header
) );
132 note
.setHtml( ensureHtmlText( j
->description() ) );
133 retC
.insertFragment( QTextDocumentFragment( ¬e
) );
135 if ( it
.hasNext() ) {
136 retC
.insertFragment( QTextDocumentFragment( &separator
) );
140 QString dialogCaption
= i18np( "Print Note", "Print %1 notes",
142 doPrint( ret
.toHtml(), dialogCaption
);
146 inline QString
KNotePrinter::ensureHtmlText( const QString
&maybeRichText
)
149 if ( Qt::mightBeRichText( maybeRichText
) ) {
150 return maybeRichText
; //... now probablyRichText
152 return Qt::convertFromPlainText( maybeRichText
);