<commit_before>/*
 * galleryenlargeshrinkplugin.cpp
 *
 * Copyright (C) 2012 Igalia, S.L.
 * Author: <NAME> <<EMAIL>>
 *
 * This file is part of the Gallery Enlarge/Shrink Plugin.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library. If not, see http://www.gnu.org/licenses/ *
 */

#include "galleryenlargeshrinkplugin.h"
#include "galleryenlargeshrinkplugin_p.h"
#include "galleryenlargeshrinkwidget.h"

#include <galleryedituiprovider.h>
#include <MLibrary>
#include <MApplication>
#include <MBanner>
#include <MMessageBox>
#include <MLabel>
#include <QGraphicsSceneMouseEvent>
#include <QDesktopServices>
#include <QUrl>
#include <QTextOption>


#include <QuillImageFilter>


static const float   EFFECT_FORCE          = 1.0;
static const int     TAP_DISTANCE          = 20;
static const int     PORTRAIT_HEIGHT       = 224;
static const int     LANDSCAPE_HEIGHT      = 112;
static const int     INFOBANNER_TIMEOUT    = 2 * 1000;
static const int     IMAGE_MAX_HEIGHT      = 512;
static const int     IMAGE_MAX_WIDTH       = 512;

M_LIBRARY

GalleryEnlargeShrinkPluginPrivate::GalleryEnlargeShrinkPluginPrivate() :
    m_focusPosition(),
    m_validImage(true)
{
}

GalleryEnlargeShrinkPluginPrivate::~GalleryEnlargeShrinkPluginPrivate()
{
}

GalleryEnlargeShrinkPlugin::GalleryEnlargeShrinkPlugin(QObject* parent) :
    GalleryEditPlugin(parent),
    d_ptr(new GalleryEnlargeShrinkPluginPrivate())
{
}


GalleryEnlargeShrinkPlugin::~GalleryEnlargeShrinkPlugin()
{
    delete d_ptr;
}

QString GalleryEnlargeShrinkPlugin::name() const
{
    return QString("Enlarge - Shrink");
}


QString GalleryEnlargeShrinkPlugin::iconID() const
{
    return QString("icon-m-image-edit-enlarge-shrink");
}

bool GalleryEnlargeShrinkPlugin::containsUi() const
{
    return true;
}

bool GalleryEnlargeShrinkPlugin::zoomingAllowed() const
{
    return true;
}

QGraphicsWidget* GalleryEnlargeShrinkPlugin::createToolBarWidget(QGraphicsItem* parent)
{
    GalleryEnlargeShrinkWidget* widget = new GalleryEnlargeShrinkWidget(parent);
    connect(widget, SIGNAL(aboutLinkActivated(QString)),
            this,   SLOT(onAboutLinkActivated(QString)));
    return widget;
}

const QSize GalleryEnlargeShrinkPlugin::toolBarWidgetSize(const M::Orientation& orientation) const
{
    QSize size = GalleryEditPlugin::toolBarWidgetSize(orientation);

    if (M::Portrait == orientation) {
        size.setHeight(PORTRAIT_HEIGHT);
    } else {
        size.setHeight(LANDSCAPE_HEIGHT);
    }

    return size;
}

bool GalleryEnlargeShrinkPlugin::receiveMouseEvent(QGraphicsSceneMouseEvent *event)
{
    if (event &&
        event->type() == QEvent::GraphicsSceneMouseRelease &&
        event->button() == Qt::LeftButton &&
        (event->scenePos() - event->buttonDownScenePos(Qt::LeftButton)).manhattanLength() < TAP_DISTANCE) {
        Q_D(GalleryEnlargeShrinkPlugin);
        if (d->m_validImage) {
            d->m_focusPosition = event->pos().toPoint();
            performEditOperation();
            return true;
        } else {
            showInfoBanner("Plugin disabled for this image size");
        }
    }
    return false;
}

void GalleryEnlargeShrinkPlugin::activate()
{
    if (editUiProvider()) {
        Q_D(GalleryEnlargeShrinkPlugin);
        d->m_validImage = editUiProvider()->fullImageSize().height() <= IMAGE_MAX_HEIGHT &&
                          editUiProvider()->fullImageSize().width()  <= IMAGE_MAX_WIDTH;
        if (d->m_validImage) {
            showInfoBanner("Tap on an area to keep it focused");
        } else {
            showMessageBox("Enlarge Shrink plugin limitations",
                           "Gallery Enlarge Shrink plugin is currently limited to "
                           "small images (512x512)<br />"
                           "For a given image:"
                           "<ol>"
                           "<li>Scale it or crop it</li>"
                           "<li>Save it with a different name</li>"
                           "<li>Apply the filter to the new one</li>"
                           "</ol>");
            GalleryEnlargeShrinkWidget* widget = static_cast<GalleryEnlargeShrinkWidget*>(toolBarWidget());
            widget->enableInput(d->m_validImage);
        }
    }
}

void GalleryEnlargeShrinkPlugin::performEditOperation()
{
    if (editUiProvider()) {
        Q_D(GalleryEnlargeShrinkPlugin);
        QHash<QuillImageFilter::QuillFilterOption, QVariant> optionHash;
        const QPoint imagePosition = editUiProvider()->convertScreenCoordToImageCoord(d->m_focusPosition);
        if (imagePosition != QPoint(-1, -1)) {
            GalleryEnlargeShrinkWidget* widget = static_cast<GalleryEnlargeShrinkWidget*>(toolBarWidget());
            optionHash.insert(QuillImageFilter::Center, QVariant(imagePosition));
            double radius = widget->radius() / 100.0 * qMin(editUiProvider()->fullImageSize().width(),<commit_msg>Corrected tapping instructions<commit_after>/*
 * galleryenlargeshrinkplugin.cpp
 *
 * Copyright (C) 2012 Igalia, S.L.
 * Author: <NAME> <<EMAIL>>
 *
 * This file is part of the Gallery Enlarge/Shrink Plugin.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library. If not, see http://www.gnu.org/licenses/ *
 */

#include "galleryenlargeshrinkplugin.h"
#include "galleryenlargeshrinkplugin_p.h"
#include "galleryenlargeshrinkwidget.h"

#include <galleryedituiprovider.h>
#include <MLibrary>
#include <MApplication>
#include <MBanner>
#include <MMessageBox>
#include <MLabel>
#include <QGraphicsSceneMouseEvent>
#include <QDesktopServices>
#include <QUrl>
#include <QTextOption>


#include <QuillImageFilter>


static const float   EFFECT_FORCE          = 1.0;
static const int     TAP_DISTANCE          = 20;
static const int     PORTRAIT_HEIGHT       = 224;
static const int     LANDSCAPE_HEIGHT      = 112;
static const int     INFOBANNER_TIMEOUT    = 2 * 1000;
static const int     IMAGE_MAX_HEIGHT      = 512;
static const int     IMAGE_MAX_WIDTH       = 512;
static const char*   TAPPING_INSTRUCTIONS  = "Tap on the image to apply the selected effect";

M_LIBRARY

GalleryEnlargeShrinkPluginPrivate::GalleryEnlargeShrinkPluginPrivate() :
    m_focusPosition(),
    m_validImage(true)
{
}

GalleryEnlargeShrinkPluginPrivate::~GalleryEnlargeShrinkPluginPrivate()
{
}

GalleryEnlargeShrinkPlugin::GalleryEnlargeShrinkPlugin(QObject* parent) :
    GalleryEditPlugin(parent),
    d_ptr(new GalleryEnlargeShrinkPluginPrivate())
{
}


GalleryEnlargeShrinkPlugin::~GalleryEnlargeShrinkPlugin()
{
    delete d_ptr;
}

QString GalleryEnlargeShrinkPlugin::name() const
{
    return QString("Enlarge - Shrink");
}


QString GalleryEnlargeShrinkPlugin::iconID() const
{
    return QString("icon-m-image-edit-enlarge-shrink");
}

bool GalleryEnlargeShrinkPlugin::containsUi() const
{
    return true;
}

bool GalleryEnlargeShrinkPlugin::zoomingAllowed() const
{
    return true;
}

QGraphicsWidget* GalleryEnlargeShrinkPlugin::createToolBarWidget(QGraphicsItem* parent)
{
    GalleryEnlargeShrinkWidget* widget = new GalleryEnlargeShrinkWidget(parent);
    connect(widget, SIGNAL(aboutLinkActivated(QString)),
            this,   SLOT(onAboutLinkActivated(QString)));
    return widget;
}

const QSize GalleryEnlargeShrinkPlugin::toolBarWidgetSize(const M::Orientation& orientation) const
{
    QSize size = GalleryEditPlugin::toolBarWidgetSize(orientation);

    if (M::Portrait == orientation) {
        size.setHeight(PORTRAIT_HEIGHT);
    } else {
        size.setHeight(LANDSCAPE_HEIGHT);
    }

    return size;
}

bool GalleryEnlargeShrinkPlugin::receiveMouseEvent(QGraphicsSceneMouseEvent *event)
{
    if (event &&
        event->type() == QEvent::GraphicsSceneMouseRelease &&
        event->button() == Qt::LeftButton &&
        (event->scenePos() - event->buttonDownScenePos(Qt::LeftButton)).manhattanLength() < TAP_DISTANCE) {
        Q_D(GalleryEnlargeShrinkPlugin);
        if (d->m_validImage) {
            d->m_focusPosition = event->pos().toPoint();
            performEditOperation();
            return true;
        } else {
            showInfoBanner("Plugin disabled for this image size");
        }
    }
    return false;
}

void GalleryEnlargeShrinkPlugin::activate()
{
    if (editUiProvider()) {
        Q_D(GalleryEnlargeShrinkPlugin);
        d->m_validImage = editUiProvider()->fullImageSize().height() <= IMAGE_MAX_HEIGHT &&
                          editUiProvider()->fullImageSize().width()  <= IMAGE_MAX_WIDTH;
        if (d->m_validImage) {
            showInfoBanner(TAPPING_INSTRUCTIONS);
        } else {
            showMessageBox("Enlarge Shrink plugin limitations",
                           "Gallery Enlarge Shrink plugin is currently limited to "
                           "small images (512x512)<br />"
                           "For a given image:"
                           "<ol>"
                           "<li>Scale it or crop it</li>"
                           "<li>Save it with a different name</li>"
                           "<li>Apply the filter to the new one</li>"
                           "</ol>");
            GalleryEnlargeShrinkWidget* widget = static_cast<GalleryEnlargeShrinkWidget*>(toolBarWidget());
            widget->enableInput(d->m_validImage);
        }
    }
}

void GalleryEnlargeShrinkPlugin::performEditOperation()
{
    if (editUiProvider()) {
        Q_D(GalleryEnlargeShrinkPlugin);
        QHash<QuillImageFilter::QuillFilterOption, QVariant> optionHash;
        const QPoint imagePosition = editUiProvider()->convertScreenCoordToImageCoord(d->m_focusPosition);
        if (imagePosition != QPoint(-1, -1)) {
            GalleryEnlargeShrinkWidget* widget = static_cast<GalleryEnlargeShrinkWidget*>(toolBarWidget());
            optionHash.insert(QuillImageFilter::Center, QVariant(imagePosition));
            double radius = widget->radius() / 100.0 * qMin(editUiProvider()->fullImageSize().width(),
{'fraction_non_alphanumeric': 0.10056425191117582, 'fraction_numerical': 0.007098653076083, 'mean_word_length': 3.3111023930953314, 'pattern_counts': {'":': 0, '<': 61, '<?xml version=': 0, '>': 89, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6984389', 'n_tokens_mistral': 3319, 'n_tokens_neox': 3212, 'n_words': 741}