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++
1.8 KB
Raw Blame
/*
 *  SPDX-FileCopyrightText: 2019 Dmitry Kazakov <[email protected]>
 *
 *  SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "KisMouseClickEater.h"

#include <QMouseEvent>
#include "kis_debug.h"

KisMouseClickEater::KisMouseClickEater(Qt::MouseButtons buttons,
                                       int clicksToEat,
                                       QObject *parent)
    : QObject(parent),
      m_buttons(buttons),
      m_clicksToEat(clicksToEat)
{
    reset();
}

KisMouseClickEater::~KisMouseClickEater()
{
}

void KisMouseClickEater::reset()
{
    m_clicksHappened = 0;
    m_timeSinceReset.start();
}

bool KisMouseClickEater::eventFilter(QObject *watched, QEvent *event)
{
#ifdef Q_OS_WIN
    const int tabletMouseEventsFlowDelay = 500;
#else
    const int tabletMouseEventsFlowDelay = 256;
#endif

    bool blockTimedRelease = false;

    if (event->type() == QEvent::TabletMove) {
        blockTimedRelease = true;
    }

    if (!blockTimedRelease &&
        m_timeSinceReset.elapsed() > tabletMouseEventsFlowDelay) {
        return QObject::eventFilter(watched, event);
    }

    if (event->type() == QEvent::MouseButtonPress ||
        event->type() == QEvent::MouseButtonRelease) {

        QMouseEvent *mevent = static_cast<QMouseEvent*>(event);
        if (mevent->button() & m_buttons) {
            if (m_clicksHappened >= m_clicksToEat) {
                return false;
            }

            if (event->type() == QEvent::MouseButtonRelease) {
                m_clicksHappened++;
            }

            return true;
        }
    }

    if (event->type() == QEvent::MouseMove) {
        QMouseEvent *mevent = static_cast<QMouseEvent*>(event);
        if (mevent->buttons() & m_buttons) {
            return m_clicksHappened < m_clicksToEat;
        }
    }

    return QObject::eventFilter(watched, event);
}