forked from qt-creator/qt-creator
Imageviewer: zoom in and out using fixed set of common-sense zoom values
Previously scale factor for zoom in/out was calculated as $(currentScaleFactor)^1.2 This results in weird zoom levels such as 83.3% or 144%. Replace this approach with using pre-defined set of 'normal' zoom level values such as {25%, 50%, .. 100%, 150%, 200% .. }, similar to what most graphic editors do To find the nearest greater or lesser fixed zoom level for any given floating point scale factor I borrowed some code from qmldesigner/ timelinewidget I didn't know what to do with zooming using mouse wheel so I left the old code in place Change-Id: Id037ef99f89cce5e8dcd58bb9ad0e6f3c8536f36 Reviewed-by: Alessandro Portale <alessandro.portale@qt.io> Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org> Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
This commit is contained in:
@@ -57,10 +57,31 @@
|
||||
namespace ImageViewer {
|
||||
namespace Constants {
|
||||
const qreal DEFAULT_SCALE_FACTOR = 1.2;
|
||||
const qreal zoomLevels[] = { 0.25, 0.5, 0.75, 1.0, 1.5, 2.0, 4.0, 8.0 };
|
||||
}
|
||||
|
||||
namespace Internal {
|
||||
|
||||
static qreal nextLevel(qreal currentLevel)
|
||||
{
|
||||
auto iter = std::find_if(std::begin(Constants::zoomLevels), std::end(Constants::zoomLevels), [&](qreal val) {
|
||||
return val > currentLevel;
|
||||
});
|
||||
if (iter != std::end(Constants::zoomLevels))
|
||||
return *iter;
|
||||
return currentLevel;
|
||||
}
|
||||
|
||||
static qreal previousLevel(qreal currentLevel)
|
||||
{
|
||||
auto iter = std::find_if(std::rbegin(Constants::zoomLevels), std::rend(Constants::zoomLevels), [&](qreal val) {
|
||||
return val < currentLevel;
|
||||
});
|
||||
if (iter != std::rend(Constants::zoomLevels))
|
||||
return *iter;
|
||||
return currentLevel;
|
||||
}
|
||||
|
||||
ImageView::ImageView(ImageViewerFile *file)
|
||||
: m_file(file)
|
||||
{
|
||||
@@ -245,16 +266,7 @@ void ImageView::setViewOutline(bool enable)
|
||||
|
||||
void ImageView::doScale(qreal factor)
|
||||
{
|
||||
qreal currentScale = transform().m11();
|
||||
qreal newScale = currentScale * factor;
|
||||
qreal actualFactor = factor;
|
||||
// cap to 0.001 - 1000
|
||||
if (newScale > 1000)
|
||||
actualFactor = 1000./currentScale;
|
||||
else if (newScale < 0.001)
|
||||
actualFactor = 0.001/currentScale;
|
||||
|
||||
scale(actualFactor, actualFactor);
|
||||
scale(factor, factor);
|
||||
emitScaleFactor();
|
||||
if (auto pixmapItem = dynamic_cast<QGraphicsPixmapItem *>(m_imageItem))
|
||||
pixmapItem->setTransformationMode(
|
||||
@@ -264,18 +276,26 @@ void ImageView::doScale(qreal factor)
|
||||
void ImageView::wheelEvent(QWheelEvent *event)
|
||||
{
|
||||
qreal factor = qPow(Constants::DEFAULT_SCALE_FACTOR, event->angleDelta().y() / 240.0);
|
||||
doScale(factor);
|
||||
qreal currentScale = transform().m11();
|
||||
qreal newScale = currentScale * factor;
|
||||
// cap to 0.001 - 1000
|
||||
qreal actualFactor = qBound(0.001, factor, 1000.0);
|
||||
doScale(actualFactor);
|
||||
event->accept();
|
||||
}
|
||||
|
||||
void ImageView::zoomIn()
|
||||
{
|
||||
doScale(Constants::DEFAULT_SCALE_FACTOR);
|
||||
qreal nextZoomLevel = nextLevel(transform().m11());
|
||||
resetTransform();
|
||||
doScale(nextZoomLevel);
|
||||
}
|
||||
|
||||
void ImageView::zoomOut()
|
||||
{
|
||||
doScale(1. / Constants::DEFAULT_SCALE_FACTOR);
|
||||
qreal previousZoomLevel = previousLevel(transform().m11());
|
||||
resetTransform();
|
||||
doScale(previousZoomLevel);
|
||||
}
|
||||
|
||||
void ImageView::resetToOriginalSize()
|
||||
|
Reference in New Issue
Block a user