summaryrefslogtreecommitdiffstats
path: root/knotes/knoteprinter.cpp
diff options
context:
space:
mode:
authortoma <toma@283d02a7-25f6-0310-bc7c-ecb5cbfe19da>2009-11-25 17:56:58 +0000
committertoma <toma@283d02a7-25f6-0310-bc7c-ecb5cbfe19da>2009-11-25 17:56:58 +0000
commit460c52653ab0dcca6f19a4f492ed2c5e4e963ab0 (patch)
tree67208f7c145782a7e90b123b982ca78d88cc2c87 /knotes/knoteprinter.cpp
downloadtdepim-460c52653ab0dcca6f19a4f492ed2c5e4e963ab0.tar.gz
tdepim-460c52653ab0dcca6f19a4f492ed2c5e4e963ab0.zip
Copy the KDE 3.5 branch to branches/trinity for new KDE 3.5 features.
BUG:215923 git-svn-id: svn://anonsvn.kde.org/home/kde/branches/trinity/kdepim@1054174 283d02a7-25f6-0310-bc7c-ecb5cbfe19da
Diffstat (limited to 'knotes/knoteprinter.cpp')
-rw-r--r--knotes/knoteprinter.cpp153
1 files changed, 153 insertions, 0 deletions
diff --git a/knotes/knoteprinter.cpp b/knotes/knoteprinter.cpp
new file mode 100644
index 000000000..d1e6ffd59
--- /dev/null
+++ b/knotes/knoteprinter.cpp
@@ -0,0 +1,153 @@
+#include "knoteprinter.h"
+
+#include <libkcal/journal.h>
+
+#include <klocale.h>
+#include <kprinter.h>
+
+#include <qfont.h>
+#include <qpaintdevicemetrics.h>
+#include <qpainter.h>
+#include <qrect.h>
+#include <qsimplerichtext.h>
+#include <qstring.h>
+
+KNotePrinter::KNotePrinter() : m_styleSheet( 0 ), m_mimeSourceFactory( 0 )
+{
+}
+
+void KNotePrinter::setContext( const QString& context )
+{
+ m_context = context;
+}
+
+QString KNotePrinter::context() const
+{
+ return m_context;
+}
+
+void KNotePrinter::setMimeSourceFactory( QMimeSourceFactory* factory )
+{
+ m_mimeSourceFactory = factory;
+}
+
+QMimeSourceFactory* KNotePrinter::mimeSourceFactory() const
+{
+ return m_mimeSourceFactory;
+}
+
+void KNotePrinter::setFont( const QFont& font )
+{
+ m_font = font;
+}
+
+QFont KNotePrinter::font() const
+{
+ return m_font;
+}
+
+void KNotePrinter::setColorGroup( const QColorGroup& colorGroup )
+{
+ m_colorGroup = colorGroup;
+}
+
+QColorGroup KNotePrinter::colorGroup() const
+{
+ return m_colorGroup;
+}
+
+void KNotePrinter::setStyleSheet( QStyleSheet* styleSheet )
+{
+ m_styleSheet = styleSheet;
+}
+
+QStyleSheet* KNotePrinter::styleSheet() const
+{
+ return m_styleSheet;
+}
+
+void KNotePrinter::doPrint( KPrinter& printer, QPainter& painter,
+ const QString& content ) const
+{
+ const int margin = 40; // pt
+
+ QPaintDeviceMetrics metrics( painter.device() );
+ int marginX = margin * metrics.logicalDpiX() / 72;
+ int marginY = margin * metrics.logicalDpiY() / 72;
+
+ QRect body( marginX, marginY,
+ metrics.width() - marginX * 2,
+ metrics.height() - marginY * 2 );
+
+ QSimpleRichText text( content, m_font, m_context,
+ m_styleSheet, m_mimeSourceFactory,
+ body.height() /*, linkColor, linkUnderline? */ );
+
+ text.setWidth( &painter, body.width() );
+ QRect view( body );
+
+ int page = 1;
+
+ for (;;)
+ {
+ text.draw( &painter, body.left(), body.top(), view, m_colorGroup );
+ view.moveBy( 0, body.height() );
+ painter.translate( 0, -body.height() );
+
+ // page numbers
+ painter.setFont( m_font );
+ painter.drawText(
+ view.right() - painter.fontMetrics().width( QString::number( page ) ),
+ view.bottom() + painter.fontMetrics().ascent() + 5, QString::number( page )
+ );
+
+ if ( view.top() >= text.height() )
+ break;
+
+ printer.newPage();
+ page++;
+ }
+}
+
+void KNotePrinter::printNote( const QString& name, const QString& content ) const
+{
+ KPrinter printer;
+ printer.setFullPage( true );
+
+ if ( !printer.setup( 0, i18n("Print %1").arg(name) ) )
+ return;
+ QPainter painter;
+ painter.begin( &printer );
+ doPrint( printer, painter, content );
+ painter.end();
+}
+
+void KNotePrinter::printNotes( const QValueList<KCal::Journal*>& journals ) const
+{
+ if ( journals.isEmpty() )
+ return;
+
+ KPrinter printer;
+ printer.setFullPage( true );
+
+ if ( !printer.setup( 0, i18n("Print Note", "Print %n notes", journals.count() ) ) )
+ return;
+
+ QPainter painter;
+ painter.begin( &printer );
+ QString content;
+ QValueListConstIterator<KCal::Journal*> it( journals.begin() );
+ QValueListConstIterator<KCal::Journal*> end( journals.end() );
+ while ( it != end ) {
+ KCal::Journal *j = *it;
+ it++;
+ content += "<h2>" + j->summary() + "</h2>";
+ content += j->description();
+ if ( it != end )
+ content += "<hr>";
+ }
+ doPrint( printer, painter, content );
+ painter.end();
+}
+
+