From 2ce8aab6833f0a8ab3edf24da29230d31ed58314 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Lemaire?= Date: Tue, 24 Apr 2012 12:22:17 +0000 Subject: [PATCH] paysages : Logarithmic preview scaling. git-svn-id: https://subversion.assembla.com/svn/thunderk/paysages@305 b1fd45b6-86a6-48da-8261-f70d1f35bdcc --- gui_qt/basepreview.cpp | 113 +++++++++++++++++++++++++++-------------- gui_qt/basepreview.h | 6 ++- 2 files changed, 79 insertions(+), 40 deletions(-) diff --git a/gui_qt/basepreview.cpp b/gui_qt/basepreview.cpp index 99fc254..c25ee8c 100644 --- a/gui_qt/basepreview.cpp +++ b/gui_qt/basepreview.cpp @@ -53,7 +53,9 @@ BasePreview::BasePreview(QWidget* parent) : this->conf_scale_max = 1.0; this->conf_scale_init = 1.0; this->conf_scale_step = 0.0; + this->conf_scroll_logarithmic = false; this->scaling = 1.0; + this->scalingbase = 1.0; this->xoffset = 0.0; this->yoffset = 0.0; 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); } -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(); @@ -104,7 +106,14 @@ void BasePreview::configScaling(double min, double max, double step, double init conf_scale_max = max / size; conf_scale_step = step / 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(); } } @@ -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) { if (event->button() == Qt::LeftButton) @@ -354,57 +375,71 @@ void BasePreview::wheelEvent(QWheelEvent* event) { factor = 1.0; } - - old_scaling = scaling; - width = pixbuf->width(); - height = pixbuf->height(); if (event->orientation() == Qt::Vertical) { - if (event->delta() > 0 && scaling > conf_scale_min) + if (event->delta() > 0 && scalingbase > conf_scale_min) { - scaling -= conf_scale_step * factor; - if (scaling < conf_scale_min) + scalingbase -= factor * conf_scale_step; + if (scalingbase < conf_scale_min) { - scaling = conf_scale_min; + scalingbase = conf_scale_min; } - - lock_drawing->lock(); - new_width = (int)floor(((double)width) * scaling / old_scaling); - new_height = (int)floor(((double)height) * scaling / old_scaling); - QImage part = pixbuf->copy((width - new_width) / 2, (height - new_height) / 2, new_width, new_height).scaled(width, height, Qt::IgnoreAspectRatio, Qt::SmoothTransformation); - QPainter painter(pixbuf); - pixbuf->fill(0x00000000); - painter.setOpacity(0.99); - painter.drawImage(0, 0, part); - need_render = true; - lock_drawing->unlock(); - - emit contentChange(); } - else if (event->delta() < 0 && scaling < conf_scale_max) + else if (event->delta() < 0 && scalingbase < conf_scale_max) { - scaling += conf_scale_step * factor; - if (scaling > conf_scale_max) + scalingbase += factor * conf_scale_step; + if (scalingbase > conf_scale_max) { - scaling = conf_scale_max; + scalingbase = conf_scale_max; } - - 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); - QPainter painter(pixbuf); - pixbuf->fill(0x00000000); - painter.setOpacity(0.99); - painter.drawImage((width - part.width()) / 2, (height - part.height()) / 2, part); - need_render = true; - lock_drawing->unlock(); - - emit contentChange(); } - event->accept(); + else + { + event->ignore(); + return; + } } else { event->ignore(); + return; } + + old_scaling = scaling; + updateScaling(); + + width = pixbuf->width(); + height = pixbuf->height(); + + if (scaling < old_scaling) + { + lock_drawing->lock(); + new_width = (int)floor(((double)width) * scaling / old_scaling); + new_height = (int)floor(((double)height) * scaling / old_scaling); + QImage part = pixbuf->copy((width - new_width) / 2, (height - new_height) / 2, new_width, new_height).scaled(width, height, Qt::IgnoreAspectRatio, Qt::SmoothTransformation); + QPainter painter(pixbuf); + pixbuf->fill(0x00000000); + painter.setOpacity(0.99); + painter.drawImage(0, 0, part); + need_render = true; + lock_drawing->unlock(); + + emit contentChange(); + } + else if (scaling > old_scaling) + { + 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); + QPainter painter(pixbuf); + pixbuf->fill(0x00000000); + painter.setOpacity(0.99); + painter.drawImage((width - part.width()) / 2, (height - part.height()) / 2, part); + need_render = true; + lock_drawing->unlock(); + + emit contentChange(); + } + + event->accept(); } diff --git a/gui_qt/basepreview.h b/gui_qt/basepreview.h index ca7ce06..bc049b8 100644 --- a/gui_qt/basepreview.h +++ b/gui_qt/basepreview.h @@ -22,7 +22,7 @@ protected: virtual void updateData(); 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); double xoffset; @@ -31,6 +31,7 @@ protected: private: void renderPixbuf(); + void updateScaling(); void resizeEvent(QResizeEvent* event); void paintEvent(QPaintEvent* event); @@ -47,6 +48,8 @@ private: int mousex; int mousey; + double scalingbase; + bool alive; bool need_restart; bool need_render; @@ -62,6 +65,7 @@ private: double conf_scale_max; double conf_scale_init; double conf_scale_step; + bool conf_scroll_logarithmic; signals: void contentChange();