[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    =?utf-8?q?=5Bcalligra=5D_libs/textlayout=3A_Fix_displaying_of_st?=
From:       Pierre Stirnweiss <pstirnweiss () googlemail ! com>
Date:       2011-05-31 19:38:52
Message-ID: 20110531193852.59E84A60B0 () git ! kde ! org
[Download RAW message or body]

Git commit 8ca62350a872ce8b28d7d8cc83b5fc3e931722b8 by Pierre Stirnweiss.
Committed on 29/05/2011 at 23:09.
Pushed by pstirnweiss into branch 'master'.

Fix displaying of style previews in the text style docker.

M  +94   -6    libs/textlayout/KoStyleThumbnailer.cpp     
M  +21   -0    libs/textlayout/KoStyleThumbnailer.h     

http://commits.kde.org/calligra/8ca62350a872ce8b28d7d8cc83b5fc3e931722b8

diff --git a/libs/textlayout/KoStyleThumbnailer.cpp \
b/libs/textlayout/KoStyleThumbnailer.cpp index 81e0c1c..e148111 100644
--- a/libs/textlayout/KoStyleThumbnailer.cpp
+++ b/libs/textlayout/KoStyleThumbnailer.cpp
@@ -33,14 +33,21 @@
 #include "KoSectionStyle.h"
 #include "KoTextDocument.h"
 #include "KoTextDocumentLayout.h"
+#include "KoTextLayoutRootArea.h"
 
-#include <QTextLayout>
+#include <klocale.h>
+
+#include <QFont>
+#include <QMap>
+#include <QPixmap>
+#include <QRect>
+#include <QTextTable>
+#include <QTextTableFormat>
 #include <QTextBlock>
 #include <QTextCursor>
-#include <QPixmap>
-#include <QMap>
+#include <QTextLayout>
+
 #include <kdebug.h>
-#include <klocale.h>
 
 class KoStyleThumbnailer::Private
 {
@@ -48,6 +55,7 @@ public:
     Private() : pixmapHelperDocument(0){ }
 
     QTextDocument *pixmapHelperDocument;
+    KoTextDocumentLayout *documentLayout;
     QMap<int,QPixmap> pixmapMap; // map of pixmap representations of the styles
 };
 
@@ -66,6 +74,10 @@ void KoStyleThumbnailer::setPixmapHelperDocument(QTextDocument \
*pixmapHelperDocu  if (d->pixmapHelperDocument)
         return;
     d->pixmapHelperDocument = pixmapHelperDocument;
+/*    d->pixmapHelperDocument = new QTextDocument;
+    d->documentLayout = new KoTextDocumentLayout(d->pixmapHelperDocument);
+    d->pixmapHelperDocument->setDocumentLayout(d->documentLayout);
+*/
 }
 
 QPixmap KoStyleThumbnailer::thumbnail(KoParagraphStyle *style)
@@ -90,12 +102,81 @@ QPixmap KoStyleThumbnailer::thumbnail(KoParagraphStyle *style)
     style->applyStyle(block, true);
     dynamic_cast<KoTextDocumentLayout *> \
(d->pixmapHelperDocument->documentLayout())->layout();  
-    d->pixmapHelperDocument->drawContents(&p);
+    KoTextDocumentLayout::PaintContext pc;
+    dynamic_cast<KoTextDocumentLayout*>(d->pixmapHelperDocument->documentLayout())->rootAreaForPosition(0)->paint(&p, \
pc);  
     d->pixmapMap.insert(style->styleId(), pm);
     return pm;
 }
 
+QPixmap KoStyleThumbnailer::thumbnail(KoParagraphStyle *style, QSize size)
+{
+    QPixmap pm(size.width(), size.height());
+    pm.fill(Qt::transparent);
+    QPainter p(&pm);
+    p.setRenderHint(QPainter::Antialiasing);
+
+    //calculate the space needed for the font size indicator (should the preview big \
too big with the style's font size +    QString sizeHint = \
QString::number(style->characterStyle()->fontPointSize()) + "pt"; +    p.save();
+    QFont sizeHintFont = p.font();
+    sizeHintFont.setPointSize(8);
+    p.setFont(sizeHintFont);
+    QRect sizeHintRect(p.boundingRect(0, 0, 1, 1, Qt::AlignCenter, sizeHint));
+
+    QTextCursor cursor (d->pixmapHelperDocument);
+
+    cursor.select(QTextCursor::Document);
+    cursor.setBlockFormat(QTextBlockFormat());
+    cursor.setBlockCharFormat(QTextCharFormat());
+    cursor.setCharFormat(QTextCharFormat());
+    cursor.insertText(style->name());
+    QTextBlock block = cursor.block();
+    style->applyStyle(block, true);
+    dynamic_cast<KoTextDocumentLayout *> \
(d->pixmapHelperDocument->documentLayout())->layout(); +
+    QSizeF documentSize = dynamic_cast<KoTextDocumentLayout *> \
(d->pixmapHelperDocument->documentLayout())->documentSize(); +    if \
(documentSize.width() > size.width() || documentSize.height() > size.height()) { +    \
//calculate the font reduction factor so that the text + the sizeHint fits +        \
int reductionFactor = \
(int)((size.width()+2*sizeHintRect.width())/documentSize.width()); +
+        cursor.select(QTextCursor::Document);
+        QTextTableFormat tbFormat;
+        tbFormat.setCellPadding(0);
+        tbFormat.setCellSpacing(0);
+        tbFormat.setBorderStyle(QTextFrameFormat::BorderStyle_None);
+        QTextTable *table = cursor.insertTable(1, 2, tbFormat);
+        style->characterStyle()->setFontPointSize(style->characterStyle()->fontPointSize()*reductionFactor);
 +        style->characterStyle()->applyStyle(&cursor);
+        cursor.insertText(style->name());
+        cursor.movePosition(QTextCursor::NextCell);
+        cursor.setBlockFormat(QTextBlockFormat());
+        cursor.setBlockCharFormat(QTextCharFormat());
+        QTextCharFormat charFormat;
+        charFormat.setFont(sizeHintFont);
+        cursor.setCharFormat(charFormat);
+        cursor.insertText(sizeHint);
+
+        dynamic_cast<KoTextDocumentLayout *> \
(d->pixmapHelperDocument->documentLayout())->layout(); +        documentSize = \
dynamic_cast<KoTextDocumentLayout *> \
(d->pixmapHelperDocument->documentLayout())->documentSize(); +        while \
(documentSize.width() > size.width() || documentSize.height() > size.height()) { +    \
cursor.setPosition(table->cellAt(1, 1).firstCursorPosition().position()); +           \
cursor.setPosition(table->cellAt(1, 1).lastCursorPosition().position(), \
QTextCursor::KeepAnchor); +            QTextCharFormat fmt = cursor.charFormat();
+            fmt.setFontPointSize(fmt.fontPointSize() - 1);
+            dynamic_cast<KoTextDocumentLayout *> \
(d->pixmapHelperDocument->documentLayout())->layout(); +            documentSize = \
dynamic_cast<KoTextDocumentLayout *> \
(d->pixmapHelperDocument->documentLayout())->documentSize(); +        }
+    }
+
+//    p.translate(0, 1.5);
+
+    d->pixmapHelperDocument->drawContents(&p);
+
+//    d->pixmapMap.insert(style->styleId(), pm);
+    return pm;
+}
+
 QPixmap KoStyleThumbnailer::thumbnail(KoCharacterStyle *style)
 {
     if (d->pixmapMap.contains(style->styleId())) {
@@ -118,8 +199,15 @@ QPixmap KoStyleThumbnailer::thumbnail(KoCharacterStyle *style)
     style->applyStyle(block);
     dynamic_cast<KoTextDocumentLayout*> \
(d->pixmapHelperDocument->documentLayout())->layout();  
-    d->pixmapHelperDocument->drawContents(&p);
+    KoTextDocumentLayout::PaintContext pc;
+    dynamic_cast<KoTextDocumentLayout*>(d->pixmapHelperDocument->documentLayout())->rootAreaForPosition(0)->paint(&p, \
pc);  
     d->pixmapMap.insert(style->styleId(), pm);
     return pm;
 }
+
+QPixmap KoStyleThumbnailer::thumbnail(KoCharacterStyle *style, QSize size)
+{
+    return QPixmap();
+}
+
diff --git a/libs/textlayout/KoStyleThumbnailer.h \
b/libs/textlayout/KoStyleThumbnailer.h index 1e15313..3420df0 100644
--- a/libs/textlayout/KoStyleThumbnailer.h
+++ b/libs/textlayout/KoStyleThumbnailer.h
@@ -28,6 +28,7 @@
 
 #include <QPixmap>
 
+class QSize;
 class QTextDocument;
 class KoCharacterStyle;
 class KoParagraphStyle;
@@ -57,14 +58,34 @@ public:
 
     /**
      * Return a thumbnail representing the style
+     * The thunbnail is 250*48 pt.
+     * The created thumbnail is cached.
      */
     QPixmap thumbnail(KoParagraphStyle *style);
 
     /**
+     * @returns a thumbnail representing the @param style, constrained into the \
@param size. +     * If the given @param size is too small, the font size will be \
decreased, so the thumbnail fits. +     * The real font size is indicated in this \
case. +     * The thumbnail is NOT cached.
+     */
+    QPixmap thumbnail(KoParagraphStyle *style, QSize size);
+
+    /**
      * Return a thumbnail representing the style
+     * The thunbnail is 250*48 pt.
+     * The created thumbnail is cached.
      */
     QPixmap thumbnail(KoCharacterStyle *style);
 
+    /**
+     * @returns a thumbnail representing the @param style, constrained into the \
@param size. +     * If the given @param size is too small, the font size will be \
decreased, so the thumbnail fits. +     * The real font size is indicated in this \
case. +     * The thumbnail is NOT cached.
+     */
+    QPixmap thumbnail(KoCharacterStyle *style, QSize size);
+
 private:
     class Private;
     Private* const d;


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic