rer *renderer;
QTimer triggerTimer;
};
PictureFlow::PictureFlow(QWidget* parent): QWidget(parent)
{
d = new PictureFlowPrivate;
d->state = new PictureFlowState;
d->state->reset();
d->state->reposition();
d->renderer = new PictureFlowSoftwareRenderer;
d->renderer->state = d->state;
d->renderer->widget = this;
d->renderer->init();
d->animator = new PictureFlowAnimator;
d->animator->state = d->state;
connect(&d->animator->animateTimer, &QTimer::timeout, this, &PictureFlow::updateAnimation);
connect(&d->triggerTimer, &QTimer::timeout, this, &PictureFlow::render);
}
PictureFlow::~PictureFlow()
{
delete d->renderer;
delete d->animator;
delete d->state;
delete d;
}
int PictureFlow::slideCount() const
{
return d->state->slideImages.count();
}
QColor PictureFlow::backgroundColor() const
{
return QColor(d->state->backgroundColor);
}
void PictureFlow::setBackgroundColor(const QColor& c)
{
d->state->backgroundColor = c.rgb();
triggerRender();
}
QSize PictureFlow::slideSize() const
{
return QSize(d->state->slideWidth, d->state->slideHeight);
}
void PictureFlow::setSlideSize(QSize size)
{
d->state->slideWidth = size.width();
d->state->slideHeight = size.height();
d->state->reposition();
triggerRender();
}
PictureFlow::ReflectionEffect PictureFlow::reflectionEffect() const
{
return d->state->reflectionEffect;
}
void PictureFlow::setReflectionEffect(ReflectionEffect effect)
{
d->state->reflectionEffect = effect;
triggerRender();
}
QImage PictureFlow::slide(int index) const
{
QImage *i = 0;
if((index >= 0) && (index < slideCount()))
i = d->state->slideImages[index];
return i ? QImage(*i) : QImage();
}
void PictureFlow::addSlide(const QPixmap& pixmap)
{
QImage image = pixmap.toImage();
int c = d->state->slideImages.count();
d->state->slideImages.resize(c+1);
d->state->slideImages[c] = new QImage(image);
triggerRender();
}
void PictureFlow::setSlide(int index, const QPixmap& pixmap)
{
if((index >= 0) && (index < slideCount()))
{
QImage image = pixmap.toImage();
QImage* i = image.isNull() ? 0 : new QImage(image);
delete d->state->slideImages[index];
d->state->slideImages[index] = i;
triggerRender();
}
}
int PictureFlow::centerIndex() const
{
return d->state->centerIndex;
}
void PictureFlow::setCenterIndex(int index)
{
index = qMin(index, slideCount()-1);
index = qMax(index, 0);
d->state->centerIndex = index;
d->state->reset();
d->animator->stop(index);
triggerRender();
}
void PictureFlow::clear()
{
int c = d->state->slideImages.count();
for(int i = 0; i < c; i++)
delete d->state->slideImages[i];
d->state->slideImages.resize(0);
d->state->reset();
triggerRender();
}
void PictureFlow::render()
{
d->renderer->dirty = true;
update();
}
void PictureFlow::triggerRender()
{
d->triggerTimer.setSingleShot(true);
d->triggerTimer.start(0);
}
void PictureFlow::showPrevious()
{
int step = d->animator->step;
int center = d->state->centerIndex;
if(step > 0)
d->animator->start(center);
else if(step == 0)
{
if(center > 0)
d->animator->start(center - 1);
}
else
d->animator->target = qMax(0, center - 2);
}
void PictureFlow::showNext()
{
int step = d->animator->step;
int center = d->state->centerIndex;
if(step < 0)
d->animator->start(center);
else if(step == 0)
{
if(center < slideCount()-1)
d->animator->start(center + 1);
}
else
d->animator->target = qMin(center + 2, slideCount()-1);
}
void PictureFlow::showSlide(int index)
{
index = qMax(index, 0);
index = qMin(slideCount()-1, index);
if(index != d->state->centerSlide.slideIndex)
d->animator->start(index);
}
void PictureFlow::mousePressEvent(QMouseEvent *event)
{
if(event->button() == Qt::LeftButton)
{
if(event->x() > width()/2 && event->y() > 20)
showNext();
else if(event->x() <= width()/2 && event->y() > 20)
showPrevious();
else
event->ignore();
}
}
void PictureFlow::paintEvent(QPaintEvent *event)
{
d->renderer->paint();
}
void PictureFlow::updateAnimation()
{
int old_center = d->state->centerIndex;
d->animator->update();
triggerRender();
}