knotes

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