knoteprinter.cpp
00001 #include "knoteprinter.h" 00002 00003 #include <libkcal/journal.h> 00004 00005 #include <klocale.h> 00006 #include <kprinter.h> 00007 #include <kdebug.h> 00008 #include <tqfont.h> 00009 #include <tqpaintdevicemetrics.h> 00010 #include <tqpainter.h> 00011 #include <tqrect.h> 00012 #include <tqsimplerichtext.h> 00013 #include <tqstring.h> 00014 00015 KNotePrinter::KNotePrinter() : m_styleSheet( 0 ), m_mimeSourceFactory( 0 ) 00016 { 00017 } 00018 00019 void KNotePrinter::setContext( const TQString& context ) 00020 { 00021 m_context = context; 00022 } 00023 00024 TQString KNotePrinter::context() const 00025 { 00026 return m_context; 00027 } 00028 00029 void KNotePrinter::setMimeSourceFactory( TQMimeSourceFactory* factory ) 00030 { 00031 m_mimeSourceFactory = factory; 00032 } 00033 00034 TQMimeSourceFactory* KNotePrinter::mimeSourceFactory() const 00035 { 00036 return m_mimeSourceFactory; 00037 } 00038 00039 void KNotePrinter::setFont( const TQFont& font ) 00040 { 00041 m_font = font; 00042 } 00043 00044 TQFont KNotePrinter::font() const 00045 { 00046 return m_font; 00047 } 00048 00049 void KNotePrinter::setColorGroup( const TQColorGroup& colorGroup ) 00050 { 00051 m_colorGroup = colorGroup; 00052 } 00053 00054 TQColorGroup KNotePrinter::colorGroup() const 00055 { 00056 return m_colorGroup; 00057 } 00058 00059 void KNotePrinter::setStyleSheet( TQStyleSheet* styleSheet ) 00060 { 00061 m_styleSheet = styleSheet; 00062 } 00063 00064 TQStyleSheet* KNotePrinter::styleSheet() const 00065 { 00066 return m_styleSheet; 00067 } 00068 00069 void KNotePrinter::doPrint( KPrinter& printer, TQPainter& painter, 00070 const TQString& content ) const 00071 { 00072 const int margin = 40; // pt 00073 00074 TQPaintDeviceMetrics metrics( painter.device() ); 00075 int marginX = margin * metrics.logicalDpiX() / 72; 00076 int marginY = margin * metrics.logicalDpiY() / 72; 00077 00078 TQRect body( marginX, marginY, 00079 metrics.width() - marginX * 2, 00080 metrics.height() - marginY * 2 ); 00081 00082 kdDebug()<<" content :"<<content<<endl; 00083 kdDebug()<<" m_styleSheet :"<<m_styleSheet<<endl; 00084 //kdDebug()<<" m_font :"<<m_font; 00085 TQSimpleRichText text( content, m_font, m_context, 00086 m_styleSheet, m_mimeSourceFactory, 00087 body.height() /*, linkColor, linkUnderline? */ ); 00088 00089 text.setWidth( &painter, body.width() ); 00090 TQRect view( body ); 00091 00092 int page = 1; 00093 00094 for (;;) 00095 { 00096 text.draw( &painter, body.left(), body.top(), view, m_colorGroup ); 00097 view.moveBy( 0, body.height() ); 00098 painter.translate( 0, -body.height() ); 00099 00100 // page numbers 00101 painter.setFont( m_font ); 00102 painter.drawText( 00103 view.right() - painter.fontMetrics().width( TQString::number( page ) ), 00104 view.bottom() + painter.fontMetrics().ascent() + 5, TQString::number( page ) 00105 ); 00106 00107 if ( view.top() >= text.height() ) 00108 break; 00109 00110 printer.newPage(); 00111 page++; 00112 } 00113 } 00114 00115 void KNotePrinter::printNote( const TQString& name, const TQString& content ) const 00116 { 00117 KPrinter printer; 00118 printer.setFullPage( true ); 00119 00120 if ( !printer.setup( 0, i18n("Print %1").arg(name) ) ) 00121 return; 00122 TQPainter painter; 00123 painter.begin( &printer ); 00124 doPrint( printer, painter, content ); 00125 painter.end(); 00126 } 00127 00128 void KNotePrinter::printNotes( const TQValueList<KCal::Journal*>& journals ) const 00129 { 00130 if ( journals.isEmpty() ) 00131 return; 00132 00133 KPrinter printer; 00134 printer.setFullPage( true ); 00135 00136 if ( !printer.setup( 0, i18n("Print Note", "Print %n notes", journals.count() ) ) ) 00137 return; 00138 00139 TQPainter painter; 00140 painter.begin( &printer ); 00141 TQString content; 00142 TQValueListConstIterator<KCal::Journal*> it( journals.constBegin() ); 00143 TQValueListConstIterator<KCal::Journal*> end( journals.constEnd() ); 00144 while ( it != end ) { 00145 KCal::Journal *j = *it; 00146 it++; 00147 content += "<h2>" + j->summary() + "</h2>"; 00148 content += j->description(); 00149 if ( it != end ) 00150 content += "<hr>"; 00151 } 00152 doPrint( printer, painter, content ); 00153 painter.end(); 00154 } 00155 00156