Commit 1c18a7b0 authored by Taneli Peltoniemi's avatar Taneli Peltoniemi
Browse files

Added support for printing custom items (taking a screenshot

parent 7a9e624b
...@@ -52,7 +52,18 @@ void QmlPrinter::paintItem(QQuickItem *item, QQuickWindow *window, QPainter *pai ...@@ -52,7 +52,18 @@ void QmlPrinter::paintItem(QQuickItem *item, QQuickWindow *window, QPainter *pai
if(!item || !item->isVisible()) if(!item || !item->isVisible())
return; return;
if(item->flags().testFlag(QQuickItem::ItemHasContents)) { if(isCustomPrintItem(item->metaObject()->className())) {
painter->save();
if(item->clip()) {
painter->setClipping(true);
painter->setClipRect(item->clipRect());
}
const QRectF rect = item->mapRectToScene(item->boundingRect());
QImage image = window->grabWindow();
painter->drawImage(rect.x(), rect.y(), image, rect.x(), rect.y(), rect.width(), rect.height());
painter->restore();
} else if(item->flags().testFlag(QQuickItem::ItemHasContents)) {
painter->save(); painter->save();
if(item->clip()) { if(item->clip()) {
painter->setClipping(true); painter->setClipping(true);
...@@ -77,7 +88,6 @@ void QmlPrinter::paintItem(QQuickItem *item, QQuickWindow *window, QPainter *pai ...@@ -77,7 +88,6 @@ void QmlPrinter::paintItem(QQuickItem *item, QQuickWindow *window, QPainter *pai
} }
painter->restore(); painter->restore();
} }
const QObjectList children = item->children(); const QObjectList children = item->children();
foreach(QObject *obj, children) { foreach(QObject *obj, children) {
paintItem(qobject_cast<QQuickItem*>(obj), window, painter); paintItem(qobject_cast<QQuickItem*>(obj), window, painter);
...@@ -293,3 +303,19 @@ bool QmlPrinter::inherits(const QMetaObject *metaObject, const QString &name) ...@@ -293,3 +303,19 @@ bool QmlPrinter::inherits(const QMetaObject *metaObject, const QString &name)
} }
return false; return false;
} }
void QmlPrinter::addPrintableItem(const QString &item)
{
printableItems.push_back(item);
}
bool QmlPrinter::isCustomPrintItem(const QString &item)
{
QListIterator<QString> it(printableItems);
while(it.hasNext()) {
QString printableItem = it.next();
if(item.contains(printableItem))
return true;
}
return false;
}
...@@ -23,6 +23,8 @@ class QmlPrinter : public QObject ...@@ -23,6 +23,8 @@ class QmlPrinter : public QObject
Q_OBJECT Q_OBJECT
private: private:
QList<QString> printableItems;
void paintItem(QQuickItem *item, QQuickWindow *window, QPainter *painter); void paintItem(QQuickItem *item, QQuickWindow *window, QPainter *painter);
void paintQQuickRectangle(QQuickItem *item, QPainter *painter); void paintQQuickRectangle(QQuickItem *item, QPainter *painter);
void paintQQuickText(QQuickItem *item, QPainter *painter); void paintQQuickText(QQuickItem *item, QPainter *painter);
...@@ -30,12 +32,14 @@ private: ...@@ -30,12 +32,14 @@ private:
void paintQQuickCanvasItem(QQuickItem *item, QQuickWindow *window, QPainter *painter); void paintQQuickCanvasItem(QQuickItem *item, QQuickWindow *window, QPainter *painter);
bool inherits(const QMetaObject *metaObject, const QString &name); bool inherits(const QMetaObject *metaObject, const QString &name);
bool isCustomPrintItem(const QString &item);
public: public:
explicit QmlPrinter(QObject *parent = 0); explicit QmlPrinter(QObject *parent = 0);
virtual ~QmlPrinter(); virtual ~QmlPrinter();
void printPDF(const QString &location, QQuickItem *item, bool showPDF = false); void printPDF(const QString &location, QQuickItem *item, bool showPDF = false);
void addPrintableItem(const QString &item);
signals: signals:
public slots: public slots:
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment