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

Added support for printing custom items (taking a screenshot

parent 7a9e624b
......@@ -52,7 +52,18 @@ void QmlPrinter::paintItem(QQuickItem *item, QQuickWindow *window, QPainter *pai
if(!item || !item->isVisible())
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();
if(item->clip()) {
painter->setClipping(true);
......@@ -77,7 +88,6 @@ void QmlPrinter::paintItem(QQuickItem *item, QQuickWindow *window, QPainter *pai
}
painter->restore();
}
const QObjectList children = item->children();
foreach(QObject *obj, children) {
paintItem(qobject_cast<QQuickItem*>(obj), window, painter);
......@@ -293,3 +303,19 @@ bool QmlPrinter::inherits(const QMetaObject *metaObject, const QString &name)
}
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
Q_OBJECT
private:
QList<QString> printableItems;
void paintItem(QQuickItem *item, QQuickWindow *window, QPainter *painter);
void paintQQuickRectangle(QQuickItem *item, QPainter *painter);
void paintQQuickText(QQuickItem *item, QPainter *painter);
......@@ -30,12 +32,14 @@ private:
void paintQQuickCanvasItem(QQuickItem *item, QQuickWindow *window, QPainter *painter);
bool inherits(const QMetaObject *metaObject, const QString &name);
bool isCustomPrintItem(const QString &item);
public:
explicit QmlPrinter(QObject *parent = 0);
virtual ~QmlPrinter();
void printPDF(const QString &location, QQuickItem *item, bool showPDF = false);
void addPrintableItem(const QString &item);
signals:
public slots:
......
Markdown is supported
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