File: PixmapCachingSheetView.cpp

package info (click to toggle)
calligra 1%3A2.4.4-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 290,028 kB
  • sloc: cpp: 1,105,019; xml: 24,940; ansic: 11,807; python: 8,457; perl: 2,792; sh: 1,507; yacc: 1,307; ruby: 1,248; sql: 903; lex: 455; makefile: 89
file content (254 lines) | stat: -rw-r--r-- 8,533 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/* This file is part of the KDE project
   Copyright 2010 Marijn Kruisselbrink <mkruisselbrink@kde.org>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

#include "PixmapCachingSheetView.h"

#include "CellView.h"

#include "../Sheet.h"
#include "../part/CanvasBase.h"

#include <QCache>
#include <QPainter>

#include <kdebug.h>

#ifdef CALLIGRA_SHEETS_MT
#include <ThreadWeaver/Job>
#include <ThreadWeaver/Weaver>
#endif

using namespace Calligra::Sheets;

#define TILESIZE 256

class PixmapCachingSheetView::Private
{
public:
    Private(PixmapCachingSheetView* q) : q(q) {}
    PixmapCachingSheetView* q;
    QCache<int, QPixmap> tileCache;
    QPointF lastScale;

    QPixmap* getTile(const Sheet* sheet, int x, int y, CanvasBase* canvas);
};

#ifdef CALLIGRA_SHEETS_MT
class TileDrawingJob : public ThreadWeaver::Job
#else
class TileDrawingJob
#endif
{
public:
    TileDrawingJob(const Sheet* sheet, SheetView* sheetView, CanvasBase* canvas, const QPointF& scale, int x, int y);
    ~TileDrawingJob();
    void run();
private:
    const Sheet* m_sheet;
    SheetView* m_sheetView;
public:
    CanvasBase* m_canvas;
    QPointF m_scale;
    int m_x;
    int m_y;
    QImage m_image;
};

TileDrawingJob::TileDrawingJob(const Sheet *sheet, SheetView* sheetView, CanvasBase* canvas, const QPointF& scale, int x, int y)
    : m_sheet(sheet), m_sheetView(sheetView), m_canvas(canvas), m_scale(scale), m_x(x), m_y(y)
    , m_image(TILESIZE, TILESIZE, QImage::Format_ARGB32)
{
    kDebug() << "new job for " << x << "," << y << " " << m_scale;
}

TileDrawingJob::~TileDrawingJob()
{
    kDebug() << "end job for " << m_x << "," << m_y << " " << m_scale;
}

void TileDrawingJob::run()
{
    kDebug() << "start draw for " << m_x << "," << m_y << " " << m_scale;
    const bool rtl = m_sheet->layoutDirection() == Qt::RightToLeft;

    m_image.fill(QColor(255, 255, 255, 0).rgba());
    QPainter pixmapPainter(&m_image);
    pixmapPainter.setClipRect(m_image.rect());
    pixmapPainter.scale(m_scale.x(), m_scale.y());

    QRect globalPixelRect(QPoint(m_x * TILESIZE, m_y * TILESIZE), QSize(TILESIZE, TILESIZE));
    QRectF docRect(
            globalPixelRect.x() / m_scale.x(),
            globalPixelRect.y() / m_scale.y(),
            globalPixelRect.width() / m_scale.x(),
            globalPixelRect.height() / m_scale.y()
    );

    if (rtl) {
        pixmapPainter.translate(docRect.x(), -docRect.y());
    } else {
        pixmapPainter.translate(-docRect.x(), -docRect.y());
    }

    qreal loffset, toffset;
    const int left = m_sheet->leftColumn(docRect.left(), loffset);
    const int right = m_sheet->rightColumn(docRect.right());
    const int top = m_sheet->topRow(docRect.top(), toffset);
    const int bottom = m_sheet->bottomRow(docRect.bottom());
    QRect cellRect(left, top, right - left + 1, bottom - top + 1);

    kDebug() << globalPixelRect << docRect;
    kDebug() << cellRect;

    m_sheetView->SheetView::paintCells(pixmapPainter, docRect, QPointF(loffset, toffset), 0, cellRect);

    //m_image.save(QString("/tmp/tile%1_%2.png").arg(m_x).arg(m_y));
    kDebug() << "end draw for " << m_x << "," << m_y << " " << m_scale;
}


PixmapCachingSheetView::PixmapCachingSheetView(const Sheet* sheet)
    : SheetView(sheet), d(new Private(this))
{
    d->tileCache.setMaxCost(128); // number of tiles to cache
}

PixmapCachingSheetView::~PixmapCachingSheetView()
{
    delete d;
}

void PixmapCachingSheetView::jobDone(ThreadWeaver::Job *tjob)
{
#ifdef CALLIGRA_SHEETS_MT
    TileDrawingJob* job = static_cast<TileDrawingJob*>(tjob);
    if (job->m_scale == d->lastScale) {
        int idx = job->m_x << 16 | job->m_y;
        d->tileCache.insert(idx, new QPixmap(QPixmap::fromImage(job->m_image)));
        // TODO: figure out what area to repaint
        job->m_canvas->update();
    }
    job->deleteLater();
#else
    Q_UNUSED(tjob);
#endif
}

QPixmap* PixmapCachingSheetView::Private::getTile(const Sheet* sheet, int x, int y, CanvasBase* canvas)
{
    int idx = x << 16 | y;
    if (tileCache.contains(idx)) return tileCache.object(idx);

#ifdef CALLIGRA_SHEETS_MT
    TileDrawingJob* job = new TileDrawingJob(sheet, q, canvas, lastScale, x, y);
    QObject::connect(job, SIGNAL(done(ThreadWeaver::Job*)), q, SLOT(jobDone(ThreadWeaver::Job*)), Qt::QueuedConnection);
    ThreadWeaver::Weaver::instance()->enqueue(job);
    QPixmap* pm = new QPixmap(TILESIZE, TILESIZE);
    pm->fill(QColor(255, 255, 255, 0));
    tileCache.insert(idx, pm);
    return pm;
#else
    TileDrawingJob job(sheet, q, canvas, lastScale, x, y);
    job.run();
    QPixmap *pm = new QPixmap(QPixmap::fromImage(job.m_image));
    tileCache.insert(idx, pm);
    return pm;
#endif
}

void PixmapCachingSheetView::paintCells(QPainter& painter, const QRectF& paintRect, const QPointF& topLeft, CanvasBase* canvas, const QRect& visibleRect)
{
    if (!canvas) {
        SheetView::paintCells(painter, paintRect, topLeft, canvas, visibleRect);
        return;
    }
    // paintRect:   the canvas area, that should be painted; in document coordinates;
    //              no layout direction consideration; scrolling offset applied;
    //              independent from painter transformations
    // topLeft:     the document coordinate of the top left cell's top left corner;
    //              no layout direction consideration; independent from painter
    //              transformations

    QTransform t = painter.transform();

    // figure out scaling from the transformation... not really perfect, but should work as long as rotation is in 90 degree steps I think
    const qreal cos_sx = t.m11();
    const qreal sin_sx = t.m12();
    const qreal msin_sy = t.m21();
    const qreal cos_sy = t.m22();

    const qreal sx = sqrt(cos_sx*cos_sx + sin_sx*sin_sx);
    const qreal sy = sqrt(cos_sy*cos_sy + msin_sy*msin_sy);
    //const qreal cost = (sx > 1e-10 ? cos_sx / sx : cos_sy / sy);
    //const qreal ang = acos(cost);

    QPointF scale = QPointF(sx, sy);
    if (scale != d->lastScale) {
        d->tileCache.clear();
    }
    d->lastScale = scale;

    QRect tiles;
    const QRect visibleCells = paintCellRange();
    const Sheet * s = sheet();
    const QPointF bottomRight(s->columnPosition(visibleCells.right() + 1), s->rowPosition(visibleCells.bottom() + 1));
    tiles.setLeft(topLeft.x() * sx / TILESIZE);
    tiles.setTop(topLeft.y() * sy / TILESIZE);
    tiles.setRight((bottomRight.x() * sx + TILESIZE-1) / TILESIZE);
    tiles.setBottom((bottomRight.y() * sy + TILESIZE-1) / TILESIZE);

    bool rtl = s->layoutDirection() == Qt::RightToLeft;

    if (rtl) {
        for (int x = qMax(0, tiles.left()); x < tiles.right(); x++) {
            for (int y = qMax(0, tiles.top()); y < tiles.bottom(); y++) {
                QPixmap *p = d->getTile(s, x, y, canvas);
                QPointF pt(paintRect.width() - (x+1) * TILESIZE / scale.x(), y * TILESIZE / scale.y());
                QRectF r(pt, QSizeF(TILESIZE / sx, TILESIZE / sy));
                painter.drawPixmap(r, *p, p->rect());
            }
        }
    } else {
        for (int x = qMax(0, tiles.left()); x < tiles.right(); x++) {
            for (int y = qMax(0, tiles.top()); y < tiles.bottom(); y++) {
                QPixmap *p = d->getTile(s, x, y, canvas);
                QPointF pt(x * TILESIZE / scale.x(), y * TILESIZE / scale.y());
                QRectF r(pt, QSizeF(TILESIZE / sx, TILESIZE / sy));
                painter.drawPixmap(r, *p, p->rect());
            }
        }
    }
}

void PixmapCachingSheetView::invalidateRange(const QRect &rect)
{
    // TODO: figure out which tiles to invalidate
    d->tileCache.clear();

    SheetView::invalidateRange(rect);
}

void PixmapCachingSheetView::invalidate()
{
    d->tileCache.clear();

    SheetView::invalidate();
}

#include "PixmapCachingSheetView.moc"