Skip to content

armgit / krita

Public Imported from GitHub
0

Krita is a free and open source cross-platform application that offers an end-to-end solution for creating digital art files from scratch built on the KDE and Qt frameworks.

C++
2.1 KB
Raw Blame
/* This file is part of the KDE project
 * SPDX-FileCopyrightText: 2006-2007 Thomas Zander <[email protected]>
 * SPDX-FileCopyrightText: 2007 C. Boemann <[email protected]>
 *
 * SPDX-License-Identifier: LGPL-2.0-or-later
 */

#include "KoZoomStrategy.h"
#include "KoShapeRubberSelectStrategy_p.h"
#include "KoZoomTool.h"
#include "KoCanvasBase.h"
#include "KoCanvasController.h"
#include "KoViewConverter.h"
#include "KoViewTransformStillPoint.h"

#include <FlakeDebug.h>

#include <QTransform>

KoZoomStrategy::KoZoomStrategy(KoZoomTool *tool, KoCanvasController *controller, const QPointF &clicked)
        : KoShapeRubberSelectStrategy(tool, clicked, false),
        m_controller(controller),
        m_forceZoomOut(false)
{
}

void KoZoomStrategy::finishInteraction(Qt::KeyboardModifiers modifiers)
{
    Q_D(KoShapeRubberSelectStrategy);

    const QTransform documentToWidget =
            m_controller->canvas()->viewConverter()->documentToView() *
            m_controller->canvas()->viewConverter()->viewToWidget();

    const QRect pixelRect = documentToWidget.mapRect(d->selectedRect()).toRect();

    bool m_zoomOut = m_forceZoomOut;
    if (modifiers & Qt::ControlModifier) {
        m_zoomOut = !m_zoomOut;
    }

    auto makeStillPoint = [&] () -> KoViewTransformStillPoint {
        const QPointF center = pixelRect.center();
        return m_controller->canvas()->viewConverter()->makeWidgetStillPoint(center);
    };

    if (m_zoomOut) {
        m_controller->zoomOut(makeStillPoint());
    } else if (pixelRect.width() > 5 && pixelRect.height() > 5) {
        m_controller->zoomTo(pixelRect);
    } else {
        m_controller->zoomIn(makeStillPoint());
    }
}

void KoZoomStrategy::cancelInteraction()
{
    Q_D(KoShapeRubberSelectStrategy);
    d->tool->canvas()->updateCanvas(d->selectedRect().normalized() | d->tool->decorationsRect());
}

KoShapeRubberSelectStrategy::SelectionMode KoZoomStrategy::currentMode() const
{
    return CoveringSelection;
}

void KoZoomStrategy::forceZoomOut()
{
    m_forceZoomOut = true;
}

void KoZoomStrategy::forceZoomIn()
{
    m_forceZoomOut = false;
}