add dumb image cache to see performance characteristic
2 files changed, 24 insertions(+), 6 deletions(-)

M quickitemdelegate.cpp
M quickitemdelegate.h
M quickitemdelegate.cpp +21 -6
@@ 18,7 18,8 @@ QuickItemDelegate::QuickItemDelegate(QOb
       engine_(new QQmlEngine(this)),
       component_(new QQmlComponent(engine_, this)),
       //renderControl_(new QQuickRenderControl(this)),
-      window_(std::make_unique<QQuickWindow>(/*renderControl_*/))
+      window_(std::make_unique<QQuickWindow>(/*renderControl_*/)),
+      imageCache_()
 {
     // XXX
     /*

          
@@ 97,10 98,24 @@ QuickItemDelegate::~QuickItemDelegate() 
 void QuickItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
                               const QModelIndex &index) const
 {
-    window_->resize(option.rect.size());
-    auto *context = engine_->rootContext();
-    context->setContextProperty(QStringLiteral("itemIndex"), index.row());
-    context->setContextProperty(QStringLiteral("itemText"), index.data());
+    QImage image;
+    if (index.row() < imageCache_.size()) {
+        // TODO: invalidate cache if model data changed
+        image = imageCache_.at(index.row());
+    }
+
+    if (image.isNull() || image.width() < option.rect.width()) {
+        window_->resize(option.rect.size());
+        auto *context = engine_->rootContext();
+        context->setContextProperty(QStringLiteral("itemIndex"), index.row());
+        context->setContextProperty(QStringLiteral("itemText"), index.data());
+        image = window_->grabWindow();
+        if (imageCache_.size() <= index.row()) {
+            imageCache_.resize(index.row() + 1);
+        }
+        imageCache_[index.row()] = image;
+    }
+
     /*
     renderControl_->polishItems();
     renderControl_->sync();

          
@@ 109,7 124,7 @@ void QuickItemDelegate::paint(QPainter *
     painter->save();
     painter->setClipRect(option.rect);
     // TODO: synchronized?
-    painter->drawImage(option.rect.left(), option.rect.top(), window_->grabWindow());
+    painter->drawImage(option.rect.left(), option.rect.top(), image);
     //painter->drawImage(0, 0, renderControl_->grab());
     painter->restore();
 }

          
M quickitemdelegate.h +3 -0
@@ 1,7 1,9 @@ 
 #ifndef QUICKITEMDELEGATE_H
 #define QUICKITEMDELEGATE_H
 
+#include <QImage>
 #include <QStyledItemDelegate>
+#include <QVector>
 #include <memory>
 
 class QQmlComponent;

          
@@ 25,6 27,7 @@ private:
     QQmlComponent *component_;
     QQuickRenderControl *renderControl_;
     std::unique_ptr<QQuickWindow> window_;
+    mutable QVector<QImage> imageCache_;  // TODO: LRU
 };
 
 #endif // QUICKITEMDELEGATE_H