paysages : Logarithmic preview scaling.

git-svn-id: https://subversion.assembla.com/svn/thunderk/paysages@305 b1fd45b6-86a6-48da-8261-f70d1f35bdcc
This commit is contained in:
Michaël Lemaire 2012-04-24 12:22:17 +00:00 committed by ThunderK
parent e28cb30dfe
commit 2ce8aab683
2 changed files with 79 additions and 40 deletions

View file

@ -53,7 +53,9 @@ BasePreview::BasePreview(QWidget* parent) :
this->conf_scale_max = 1.0; this->conf_scale_max = 1.0;
this->conf_scale_init = 1.0; this->conf_scale_init = 1.0;
this->conf_scale_step = 0.0; this->conf_scale_step = 0.0;
this->conf_scroll_logarithmic = false;
this->scaling = 1.0; this->scaling = 1.0;
this->scalingbase = 1.0;
this->xoffset = 0.0; this->xoffset = 0.0;
this->yoffset = 0.0; this->yoffset = 0.0;
this->pixbuf = new QImage(this->size(), QImage::Format_ARGB32); this->pixbuf = new QImage(this->size(), QImage::Format_ARGB32);
@ -94,7 +96,7 @@ QColor BasePreview::getColor(double x, double y)
return QColor(0, 0, 0); return QColor(0, 0, 0);
} }
void BasePreview::configScaling(double min, double max, double step, double init) void BasePreview::configScaling(double min, double max, double step, double init, bool logarithmic)
{ {
double size = (double)width(); double size = (double)width();
@ -104,7 +106,14 @@ void BasePreview::configScaling(double min, double max, double step, double init
conf_scale_max = max / size; conf_scale_max = max / size;
conf_scale_step = step / size; conf_scale_step = step / size;
conf_scale_init = init / size; conf_scale_init = init / size;
scaling = init / size; conf_scroll_logarithmic = logarithmic;
scalingbase = init / size;
if (conf_scroll_logarithmic && conf_scale_max - conf_scale_min > 0.0000001)
{
scalingbase = pow((scalingbase - conf_scale_min) / (conf_scale_max - conf_scale_min), 0.5) * (conf_scale_max - conf_scale_min) + conf_scale_min;
}
updateScaling();
redraw(); redraw();
} }
} }
@ -235,6 +244,18 @@ void BasePreview::renderPixbuf()
} }
} }
void BasePreview::updateScaling()
{
if (conf_scroll_logarithmic && conf_scale_max - conf_scale_min > 0.0000001)
{
scaling = pow((scalingbase - conf_scale_min) / (conf_scale_max - conf_scale_min), 2.0) * (conf_scale_max - conf_scale_min) + conf_scale_min;
}
else
{
scaling = scalingbase;
}
}
void BasePreview::mousePressEvent(QMouseEvent* event) void BasePreview::mousePressEvent(QMouseEvent* event)
{ {
if (event->button() == Qt::LeftButton) if (event->button() == Qt::LeftButton)
@ -355,20 +376,44 @@ void BasePreview::wheelEvent(QWheelEvent* event)
factor = 1.0; factor = 1.0;
} }
if (event->orientation() == Qt::Vertical)
{
if (event->delta() > 0 && scalingbase > conf_scale_min)
{
scalingbase -= factor * conf_scale_step;
if (scalingbase < conf_scale_min)
{
scalingbase = conf_scale_min;
}
}
else if (event->delta() < 0 && scalingbase < conf_scale_max)
{
scalingbase += factor * conf_scale_step;
if (scalingbase > conf_scale_max)
{
scalingbase = conf_scale_max;
}
}
else
{
event->ignore();
return;
}
}
else
{
event->ignore();
return;
}
old_scaling = scaling; old_scaling = scaling;
updateScaling();
width = pixbuf->width(); width = pixbuf->width();
height = pixbuf->height(); height = pixbuf->height();
if (event->orientation() == Qt::Vertical) if (scaling < old_scaling)
{ {
if (event->delta() > 0 && scaling > conf_scale_min)
{
scaling -= conf_scale_step * factor;
if (scaling < conf_scale_min)
{
scaling = conf_scale_min;
}
lock_drawing->lock(); lock_drawing->lock();
new_width = (int)floor(((double)width) * scaling / old_scaling); new_width = (int)floor(((double)width) * scaling / old_scaling);
new_height = (int)floor(((double)height) * scaling / old_scaling); new_height = (int)floor(((double)height) * scaling / old_scaling);
@ -382,14 +427,8 @@ void BasePreview::wheelEvent(QWheelEvent* event)
emit contentChange(); emit contentChange();
} }
else if (event->delta() < 0 && scaling < conf_scale_max) else if (scaling > old_scaling)
{ {
scaling += conf_scale_step * factor;
if (scaling > conf_scale_max)
{
scaling = conf_scale_max;
}
lock_drawing->lock(); lock_drawing->lock();
QImage part = pixbuf->scaled((int)floor(((double)width) * old_scaling / scaling), (int)floor(((double)height) * old_scaling / scaling), Qt::IgnoreAspectRatio, Qt::SmoothTransformation); QImage part = pixbuf->scaled((int)floor(((double)width) * old_scaling / scaling), (int)floor(((double)height) * old_scaling / scaling), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
QPainter painter(pixbuf); QPainter painter(pixbuf);
@ -401,10 +440,6 @@ void BasePreview::wheelEvent(QWheelEvent* event)
emit contentChange(); emit contentChange();
} }
event->accept(); event->accept();
}
else
{
event->ignore();
}
} }

View file

@ -22,7 +22,7 @@ protected:
virtual void updateData(); virtual void updateData();
virtual QColor getColor(double x, double y); virtual QColor getColor(double x, double y);
void configScaling(double min, double max, double step, double init); void configScaling(double min, double max, double step, double init, bool logarithmic=true);
void configScrolling(double xmin, double xmax, double xinit, double ymin, double ymax, double yinit); void configScrolling(double xmin, double xmax, double xinit, double ymin, double ymax, double yinit);
double xoffset; double xoffset;
@ -31,6 +31,7 @@ protected:
private: private:
void renderPixbuf(); void renderPixbuf();
void updateScaling();
void resizeEvent(QResizeEvent* event); void resizeEvent(QResizeEvent* event);
void paintEvent(QPaintEvent* event); void paintEvent(QPaintEvent* event);
@ -47,6 +48,8 @@ private:
int mousex; int mousex;
int mousey; int mousey;
double scalingbase;
bool alive; bool alive;
bool need_restart; bool need_restart;
bool need_render; bool need_render;
@ -62,6 +65,7 @@ private:
double conf_scale_max; double conf_scale_max;
double conf_scale_init; double conf_scale_init;
double conf_scale_step; double conf_scale_step;
bool conf_scroll_logarithmic;
signals: signals:
void contentChange(); void contentChange();