/*
PictureFlow - animated image show widget
http://pictureflow.googlecode.com
Copyright (C) 2008 Ariya Hidayat (ariya@kde.org)
Copyright (C) 2007 Ariya Hidayat (ariya@kde.org)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "pictureflow.h"
#include "qapplication.h"
//#include "QRgba64"
// detect Qt version
#if QT_VERSION >= 0x040000
#define PICTUREFLOW_QT4
#elif QT_VERSION >= 0x030000
#define PICTUREFLOW_QT3
#elif QT_VERSION >= 235
#define PICTUREFLOW_QT2
#else
#error PictureFlow widgets need Qt 2, Qt 3 or Qt 4
#endif
#ifdef PICTUREFLOW_QT4
#include <QApplication>
#include <QCache>
#include <QHash>
#include <QImage>
#include <QKeyEvent>
#include <QPainter>
#include <QPixmap>
#include <QTimer>
#include <QVector>
#include <QWidget>
#endif
#ifdef PICTUREFLOW_QT3
#include <qapplication.h>
#include <qcache.h>
#include <qimage.h>
#include <qpainter.h>
#include <qpixmap.h>
#include <qdatetime.h>
#include <qtimer.h>
#include <qvaluevector.h>
#include <qwidget.h>
#define qMax(x,y) ((x) > (y)) ? (x) : (y)
#define qMin(x,y) ((x) < (y)) ? (x) : (y)
#define QVector QValueVector
#define toImage convertToImage
#define contains find
#define modifiers state
#define ControlModifier ControlButton
#endif
#ifdef PICTUREFLOW_QT2
#include <qapplication.h>
#include <qarray.h>
#include <qcache.h>
#include <qimage.h>
#include <qintdict.h>
#include <qpainter.h>
#include <qpixmap.h>
#include <qdatetime.h>
#include <qtimer.h>
#include <qwidget.h>
#define qMax(x,y) ((x) > (y)) ? (x) : (y)
#define qMin(x,y) ((x) < (y)) ? (x) : (y)
#define QVector QArray
#define toImage convertToImage
#define contains find
#define modifiers state
#define ControlModifier ControlButton
#define flush flushX
#endif
// for fixed-point arithmetic, we need minimum 32-bit long
// long long (64-bit) might be useful for multiplication and division
typedef long PFreal;
#define PFREAL_SHIFT 10
#define PFREAL_ONE (1 << PFREAL_SHIFT)
#define IANGLE_MAX 1024
#define IANGLE_MASK 1023
inline PFreal fmul(PFreal a, PFreal b)
{
return ((long long)(a))*((long long)(b)) >> PFREAL_SHIFT;
}
inline PFreal fdiv(PFreal num, PFreal den)
{
long long p = (long long)(num) << (PFREAL_SHIFT*2);
long long q = p / (long long)den;
long long r = q >> PFREAL_SHIFT;
return r;
}
inline PFreal fsin(int iangle)
{
// warning: regenerate the table if IANGLE_MAX and PFREAL_SHIFT are changed!
static const PFreal tab[] = {
3, 103, 202, 300, 394, 485, 571, 652,
726, 793, 853, 904, 947, 980, 1004, 1019,
1023, 1018, 1003, 978, 944, 901, 849, 789,
721, 647, 566, 479, 388, 294, 196, 97,
-4, -104, -203, -301, -395, -486, -572, -653,
-727, -794, -854, -905, -948, -981, -1005, -1020,
-1024, -1019, -1004, -979, -945, -902, -850, -790,
-722, -648, -567, -480, -389, -295, -197, -98,
3
};
while(iangle < 0)
iangle += IANGLE_MAX;
iangle &= IANGLE_MASK;
int i = (iangle >> 4);
PFreal p = tab[i];
PFreal q = tab[(i+1)];
PFreal g = (q - p);
return p + g * (iangle-i*16)/16;
}
inline PFreal fcos(int iangle)
{
return fsin(iangle + (IANGLE_MAX >> 2));
}
/* ----------------------------------------------------------
PictureFlowState stores the state of all slides, i.e. all the necessary
information to be able to render them.
PictureFlowAnimator is responsible to move the slides during the
transition between slides, to achieve the effect similar to Cover Flow,
by changing the state.
PictureFlowSoftwareRenderer (or PictureFlowOpenGLRenderer) is
the actual 3-d renderer. It should render all slides given the state
(an instance of PictureFlowState).
Instances of all the above three classes are stored in
PictureFlowPrivate.
------------------------------------------------------- */
struct SlideInfo
{
int slideIndex;
int angle;
PFreal cx;
PFreal cy;
int blend;
};
class PictureFlowState
{
public:
PictureFlowState();
~PictureFlowState();
void reposition();
void reset();
QRgb backgroundColor;
int slideWidth;
int slideHeight;
PictureFlow::ReflectionEffect reflectionEffect;
QVector<QImage*> slideImages;
int angle;
int spacing;
PFreal offsetX;
PFreal offsetY;
SlideInfo centerSlide;
QVector<SlideInfo> leftSlides;
QVector<SlideInfo> rightSlides;
int centerIndex;
};
class PictureFlowAnimator
{
public:
PictureFlowAnimator();
PictureFlowState* state;
void start(int slide);
void stop(int slide);
void update();
int target;
int step;
int frame;
QTimer animateTimer;
};
class PictureFlowAbstractRenderer
{
public:
PictureFlowAbstractRenderer(): state(0), dirty(false), widget(0) {}
virtual ~PictureFlowAbstractRenderer() {}
PictureFlowState* state;
bool dirty;
QWidget* widget;
virtual void init() = 0;
virtual void paint() = 0;
};
class PictureFlowSoftwareRenderer: public PictureFlowAbstractRenderer
{
public:
PictureFlowSoftwareRenderer();
~PictureFlowSoftwareRenderer();
virtual void init();
virtual void paint();
private:
QSize size;
QRgb bgcolor;
int effect;
QImage buffer;
QVector<PFreal> rays;
QImage* blankSurface;
#ifdef PICTUREFLOW_QT4
QCache<int,QImage> surfaceCache;
QHash<int,QImage*> imageHash;
#endif
#ifdef PICTUREFLOW_QT3
QCache<QImage> surfaceCache;
QMap<int,QImage*> imageHash;
#endif
#ifdef PICTUREFLOW_QT2
QCache<QImage> surfaceCache;
QIntDict<QImage> imageHash;
#endif
void render();
void renderSlides();
QRect renderSlide(const SlideInfo &slide, int col1 = -1, int col2 = -1);
QImage* surface(int slideIndex);
};
// ------------- PictureFlowState ---------------------------------------
PictureFlowState::PictureFlowState():
backgroundColor(qRgba(255,255,255,0)), slideWidth(150), slideHeight(200),
reflectionEffect(PictureFlow::BlurredReflection), centerIndex(0)
{
}
PictureFlowState::~PictureFlowState()
{
for(int i = 0; i < (int)slideImages.count(); i++)
delete slideImages[i];
}
// readjust the settings, call this when slide dimension is changed
void PictureFlowState::reposition()
{
angle = 90 * IANGLE_MAX / 360; // approx. 70 degrees tilted
offsetX = slideWidth/2 * (PFREAL_ONE-fcos(angle));
offsetY = slideWidth/2 * fsin(angle);
offsetX += slideWidth * PFREAL_ONE;
offsetY += slideWidth * PFREAL_ONE / 4;
spacing = 120;
}
// adjust slides so that they are in "steady state" position
void PictureFlowState::reset()
{
centerSlide.angle = 0;
centerSlide.cx = 0;
centerSlide.cy = 0;
centerSlide.slideIndex = centerIndex;
centerSlide.blend = 256;
leftSlides.resize(6);
for(int i = 0; i < (int)leftSlides.count(); i++)
{
SlideInfo& si = leftSlides[i];
si.angle = angle;
si.cx = -(offsetX + spacing*i*PFREAL_ONE);
si.cy = offsetY;
si.slideIndex = centerIndex-1-i;
si.blend = 256;
if(i == (int)leftSlides.count()-2)
si.blend = 128;
if(i == (int)leftSlides.count()-1)
si.b