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 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
|
/* This file is part of the KDE project
Copyright (C) 2010 KO GmbH <jos.van.den.oever@kogmbh.com>
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 "slideview.h"
#include "slideloader.h"
#include <QDebug>
#include <QEvent>
#include <QGraphicsItem>
#include <QImage>
#include <QVBoxLayout>
#include <QPixmapCache>
#include <QGraphicsSceneEvent>
#include <QScrollBar>
#include <QTransform>
#include <QWheelEvent>
#include <QGLWidget>
#include <cmath>
class GraphicsSlideItem : public QGraphicsItem {
public:
#ifdef QT46
class Cache {
private:
QPixmapCache::Key key;
public:
bool find(QPixmap& pixmap) const {
return QPixmapCache::find(key, &pixmap);
}
void clear() const { QPixmapCache::remove(key); }
void add(const QPixmap& pixmap) {
clear();
key = QPixmapCache::insert(pixmap);
}
};
#else
class Cache {
private:
const QString key;
public:
Cache() :key(QString("%1 GraphicsSlideItem").arg((qlonglong)this)) {}
bool find(QPixmap& pixmap) const {
return QPixmapCache::find(key, pixmap);
}
void clear() const { QPixmapCache::remove(key); }
void add(const QPixmap& pixmap) {
clear();
QPixmapCache::insert(key, pixmap);
}
};
#endif
Cache cache;
QRectF rect;
QPainterPath path;
SlideView* const view;
int position;
int slideVersion;
explicit GraphicsSlideItem(int pos, SlideView* v)
:QGraphicsItem(), view(v), position(pos) {
slideVersion = -1;
}
void setPosition(int pos) {
if (position != pos) {
position = pos;
slideVersion = -1;
}
if (view->loader->slideVersion(position) != slideVersion) {
update(rect);
}
}
void paint(QPainter* painter, const QStyleOptionGraphicsItem * /*option*/,
QWidget * /*widget*/ = 0) {
QPixmap pixmap;
if (slideVersion != view->loader->slideVersion(position)
|| !cache.find(pixmap)) {
slideVersion = view->loader->slideVersion(position);
pixmap = view->loader->loadSlide(position, rect.size().toSize());
cache.add(pixmap);
}
if (pixmap.isNull()) {
painter->fillRect(rect, Qt::gray);
} else {
painter->drawPixmap(rect.topLeft(), pixmap);
}
}
QRectF boundingRect() const {
return rect;
}
QPainterPath opaqueArea() const {
return path;
}
void setBoundingRect(const QRectF& newrect) {
if (rect != newrect) {
if (rect.size() != newrect.size()) {
cache.clear();
}
prepareGeometryChange();
rect = newrect;
path = QPainterPath();
path.addRect(rect);
}
}
void mouseDoubleClickEvent(QGraphicsSceneMouseEvent * /*event*/) {
view->toggleSlideZoom(this);
}
};
SlideView::SlideView(SlideLoader* l, QWidget* parent) :QWidget(parent),
loader(l), zoomfactor(0.25), sendingChange(false)
{
// use opengl canvas
view.setViewport(new QGLWidget(QGLFormat(QGL::SampleBuffers)));
// listen to resize and wheel events
view.viewport()->installEventFilter(this);
connect(loader, SIGNAL(slidesChanged()), this, SLOT(slotUpdateSlides()));
connect(view.verticalScrollBar(), SIGNAL(valueChanged(int)),
this, SLOT(slotViewChanged()));
connect(view.horizontalScrollBar(), SIGNAL(valueChanged(int)),
this, SLOT(slotViewChanged()));
QVBoxLayout *layout = new QVBoxLayout();
layout->setContentsMargins(0, 0, 0, 0);
layout->setSpacing(0);
layout->addWidget(&view);
layout->addWidget(&progressBar);
setLayout(layout);
progressBar.setVisible(false);
scene.setBackgroundBrush(Qt::Dense4Pattern);
view.setScene(&scene);
slotUpdateSlides();
}
void SlideView::slotUpdateSlides() {
int numberOfSlides = loader->numberOfSlides();
QList<QGraphicsItem*> items = scene.items();
if (items.size() > numberOfSlides) {
// remove surplus items
for (int i = numberOfSlides; i < items.size(); ++i) {
scene.removeItem(items[i]);
}
} else if (numberOfSlides > items.size()) {
// add new items
for (int i=items.size(); i<numberOfSlides; ++i) {
GraphicsSlideItem* item = new GraphicsSlideItem(i, this);
item->setVisible(true);
scene.addItem(item);
}
}
items = scene.items();
for (int i=0; i<items.size(); ++i) {
static_cast<GraphicsSlideItem*>(items[i])->setPosition(i);
}
layout();
}
void SlideView::layout() {
const qreal spacing = 2;
QSizeF slidesize = loader->slideSize();
int slidesPerRow = (zoomfactor > 1) ?1 :1/zoomfactor;
if (zoomfactor <= 1) {
view.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
} else {
view.setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
}
qreal viewwidth = view.viewport()->width();
qreal x = 0;
qreal y = 0;
qreal screenslidewidth = zoomfactor * viewwidth;
qreal w, h, viewscale;
if (screenslidewidth > slidesize.width()) {
viewscale = screenslidewidth / slidesize.width();
w = slidesize.width();
h = slidesize.height();
} else {
viewscale = 1;
qreal slidescale = screenslidewidth / slidesize.width();
w = slidesize.width() * slidescale;
h = slidesize.height() * slidescale;
}
QTransform transform;
transform.scale(viewscale, viewscale);
view.setTransform(transform);
const QList<QGraphicsItem *> items = scene.items();
qreal dx = w + spacing;
qreal dy = h + spacing;
for (int i=0; i < items.size(); ++i) {
GraphicsSlideItem* item = static_cast<GraphicsSlideItem*>(items[i]);
item->setBoundingRect(QRectF(x, y, w, h));
if ((i+1) % slidesPerRow) {
x += dx;
} else {
x = 0;
y += dy;
}
}
if (x == 0) {
scene.setSceneRect(0, 0, slidesPerRow*dx, y);
} else {
scene.setSceneRect(0, 0, slidesPerRow*dx, y + dy);
}
}
bool SlideView::eventFilter(QObject * obj, QEvent *event) {
if (obj != view.viewport()) return false;
if (event->type() == QEvent::Wheel) {
const QWheelEvent* e = static_cast<QWheelEvent*>(event);
if (e->modifiers() == Qt::ControlModifier) {
zoomfactor *= pow(1.1, e->delta()/120.0);
layout();
slotViewChanged();
return true;
}
} else if (event->type() == QEvent::Resize) {
layout();
slotViewChanged();
}
return false;
}
void
SlideView::setView(qreal zoomfactor, int h, int v) {
if (sendingChange) return;
this->zoomfactor = zoomfactor;
if (scene.items().size())
layout();
view.horizontalScrollBar()->setValue(h);
view.verticalScrollBar()->setValue(v);
}
void SlideView::slotViewChanged() {
sendingChange = true;
int h = view.horizontalScrollBar()->value();
int v = view.verticalScrollBar()->value();
emit viewChanged(zoomfactor, h, v);
sendingChange = false;
}
void SlideView::toggleSlideZoom(const GraphicsSlideItem* item) {
// zoom to zoomfactor 1 with clicked slide on the top, unless that slide
// is already active, then zoom to level 0.25 in the range of the clicked
// slide
// this function does not work well, the behavior of the scrollbar is a
// mystery, but the magic dance seems to work ok so far
QScrollBar* sb = view.verticalScrollBar();
int offset = 2;
qreal y = item->boundingRect().top();
qDebug() << y << " " << sb->value();
if (zoomfactor == 1
&& (qAbs(y - sb->value()) <= offset+1
|| sb->value()+1 >= sb->maximum())) {
zoomfactor = 0.25;
} else {
zoomfactor = 1;
}
layout();
// magic dance
layout();
view.mapToScene(0, 0).y();
// end of magic dance
y = item->boundingRect().top();
view.verticalScrollBar()->setValue(y - offset); // small offset looks nice
}
void SlideView::SlideGraphicsScene::dragEnterEvent(
QGraphicsSceneDragDropEvent *event)
{
event->ignore();
}
|