qmlprinter.h 1.98 KB
Newer Older
1 2 3 4
#ifndef QMLPRINTER_H
#define QMLPRINTER_H

#include <QObject>
5 6
#include <QVariantMap>
#include <functional>
7

Michael Winkelmann's avatar
Michael Winkelmann committed
8 9 10 11 12 13 14
QT_BEGIN_NAMESPACE
class QQuickItem;
class QQuickWindow;
class QPainter;
class QPrinter;
class QPrinterInfo;
QT_END_NAMESPACE
15

16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
struct ItemWithProperties {
  QQuickItem* item;
  QVariantMap properties;
};

using ItemList = std::vector<ItemWithProperties>;
using PrintStrategy = std::function<bool(QQuickItem*,ItemList&,ItemList&)>;

struct SubItemStrategy {
    bool operator()(QQuickItem*, ItemList&, ItemList&);
};

struct StackLayoutStrategy {
    StackLayoutStrategy(QVariantMap const&options);
    bool operator()(QQuickItem*, ItemList&, ItemList&);
private:
    QVariantMap m_options;
};


Michael Winkelmann's avatar
Michael Winkelmann committed
36
class QmlPrinter {
37
  Q_GADGET
Michael Winkelmann's avatar
Michael Winkelmann committed
38
public:
39 40 41 42 43 44 45 46
  enum class Strategy {
    Items,
    StackLayout,
    State
  };
  Q_ENUM(Strategy)


Michael Winkelmann's avatar
Michael Winkelmann committed
47 48
  explicit QmlPrinter();
  virtual ~QmlPrinter();
49

50 51 52
  bool printPdf(const QString &location, QQuickItem *rootItem);
  bool printItems(QPrinter& printer, ItemList const& items, ItemList const& masterItems) const;
  bool printItems(QPrinter& printer, QQuickItem *rootItem, PrintStrategy) const;
53

54 55 56 57
  int resolution() const;
  void setResolution(int resolution);

private:
Michael Winkelmann's avatar
Michael Winkelmann committed
58
  static QMatrix transformMatrix(QQuickItem *);
59

60 61 62 63 64
  void paintItem(QQuickItem *item, QQuickWindow *window, QPainter *painter) const;
  void paintItem(ItemWithProperties const&, QPainter *painter) const;
  void paintQQuickRectangle(QQuickItem *item, QPainter *painter) const;
  void paintQQuickText(QQuickItem *item, QPainter *painter) const;
  void paintQQuickImage(QQuickItem *item, QPainter *painter) const;
Michael Winkelmann's avatar
Michael Winkelmann committed
65
  void paintQQuickCanvasItem(QQuickItem *item, QQuickWindow *window,
66
                             QPainter *painter) const;
67

68
  static bool inherits(const QMetaObject *metaObject, const QString &name);
69

70
  static void changePrinterOrientation(QPrinter &printer, const int &width,
Michael Winkelmann's avatar
Michael Winkelmann committed
71
                                const int &height);
72 73 74 75 76 77

  Strategy m_strategy = Strategy::Items;
  QVariantMap m_options;



78 79 80
};

#endif // HURPRINTER_H