TDE personal information management applications
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

157 lines
3.7 KiB

  1. #include "knoteprinter.h"
  2. #include <libkcal/journal.h>
  3. #include <tdelocale.h>
  4. #include <kprinter.h>
  5. #include <kdebug.h>
  6. #include <tqfont.h>
  7. #include <tqpaintdevicemetrics.h>
  8. #include <tqpainter.h>
  9. #include <tqrect.h>
  10. #include <tqsimplerichtext.h>
  11. #include <tqstring.h>
  12. KNotePrinter::KNotePrinter() : m_styleSheet( 0 ), m_mimeSourceFactory( 0 )
  13. {
  14. }
  15. void KNotePrinter::setContext( const TQString& context )
  16. {
  17. m_context = context;
  18. }
  19. TQString KNotePrinter::context() const
  20. {
  21. return m_context;
  22. }
  23. void KNotePrinter::setMimeSourceFactory( TQMimeSourceFactory* factory )
  24. {
  25. m_mimeSourceFactory = factory;
  26. }
  27. TQMimeSourceFactory* KNotePrinter::mimeSourceFactory() const
  28. {
  29. return m_mimeSourceFactory;
  30. }
  31. void KNotePrinter::setFont( const TQFont& font )
  32. {
  33. m_font = font;
  34. }
  35. TQFont KNotePrinter::font() const
  36. {
  37. return m_font;
  38. }
  39. void KNotePrinter::setColorGroup( const TQColorGroup& colorGroup )
  40. {
  41. m_colorGroup = colorGroup;
  42. }
  43. TQColorGroup KNotePrinter::colorGroup() const
  44. {
  45. return m_colorGroup;
  46. }
  47. void KNotePrinter::setStyleSheet( TQStyleSheet* styleSheet )
  48. {
  49. m_styleSheet = styleSheet;
  50. }
  51. TQStyleSheet* KNotePrinter::styleSheet() const
  52. {
  53. return m_styleSheet;
  54. }
  55. void KNotePrinter::doPrint( KPrinter& printer, TQPainter& painter,
  56. const TQString& content ) const
  57. {
  58. const int margin = 40; // pt
  59. TQPaintDeviceMetrics metrics( painter.device() );
  60. int marginX = margin * metrics.logicalDpiX() / 72;
  61. int marginY = margin * metrics.logicalDpiY() / 72;
  62. TQRect body( marginX, marginY,
  63. metrics.width() - marginX * 2,
  64. metrics.height() - marginY * 2 );
  65. kdDebug()<<" content :"<<content<<endl;
  66. kdDebug()<<" m_styleSheet :"<<m_styleSheet<<endl;
  67. //kdDebug()<<" m_font :"<<m_font;
  68. TQSimpleRichText text( content, m_font, m_context,
  69. m_styleSheet, m_mimeSourceFactory,
  70. body.height() /*, linkColor, linkUnderline? */ );
  71. text.setWidth( &painter, body.width() );
  72. TQRect view( body );
  73. int page = 1;
  74. for (;;)
  75. {
  76. text.draw( &painter, body.left(), body.top(), view, m_colorGroup );
  77. view.moveBy( 0, body.height() );
  78. painter.translate( 0, -body.height() );
  79. // page numbers
  80. painter.setFont( m_font );
  81. painter.drawText(
  82. view.right() - painter.fontMetrics().width( TQString::number( page ) ),
  83. view.bottom() + painter.fontMetrics().ascent() + 5, TQString::number( page )
  84. );
  85. if ( view.top() >= text.height() )
  86. break;
  87. printer.newPage();
  88. page++;
  89. }
  90. }
  91. void KNotePrinter::printNote( const TQString& name, const TQString& content ) const
  92. {
  93. KPrinter printer;
  94. printer.setFullPage( true );
  95. if ( !printer.setup( 0, i18n("Print %1").arg(name) ) )
  96. return;
  97. TQPainter painter;
  98. painter.begin( &printer );
  99. doPrint( printer, painter, content );
  100. painter.end();
  101. }
  102. void KNotePrinter::printNotes( const TQValueList<KCal::Journal*>& journals ) const
  103. {
  104. if ( journals.isEmpty() )
  105. return;
  106. KPrinter printer;
  107. printer.setFullPage( true );
  108. if ( !printer.setup( 0, i18n("Print Note", "Print %n notes", journals.count() ) ) )
  109. return;
  110. TQPainter painter;
  111. painter.begin( &printer );
  112. TQString content;
  113. TQValueListConstIterator<KCal::Journal*> it( journals.constBegin() );
  114. TQValueListConstIterator<KCal::Journal*> end( journals.constEnd() );
  115. while ( it != end ) {
  116. KCal::Journal *j = *it;
  117. it++;
  118. content += "<h2>" + j->summary() + "</h2>";
  119. content += j->description();
  120. if ( it != end )
  121. content += "<hr>";
  122. }
  123. doPrint( printer, painter, content );
  124. painter.end();
  125. }